LocationParser.cpp
6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//===- LocationParser.cpp - MLIR Location Parser -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Parser.h"
using namespace mlir;
using namespace mlir::detail;
/// Parse a location.
///
/// location ::= `loc` inline-location
/// inline-location ::= '(' location-inst ')'
///
ParseResult Parser::parseLocation(LocationAttr &loc) {
// Check for 'loc' identifier.
if (parseToken(Token::kw_loc, "expected 'loc' keyword"))
return emitError();
// Parse the inline-location.
if (parseToken(Token::l_paren, "expected '(' in inline location") ||
parseLocationInstance(loc) ||
parseToken(Token::r_paren, "expected ')' in inline location"))
return failure();
return success();
}
/// Specific location instances.
///
/// location-inst ::= filelinecol-location |
/// name-location |
/// callsite-location |
/// fused-location |
/// unknown-location
/// filelinecol-location ::= string-literal ':' integer-literal
/// ':' integer-literal
/// name-location ::= string-literal
/// callsite-location ::= 'callsite' '(' location-inst 'at' location-inst ')'
/// fused-location ::= fused ('<' attribute-value '>')?
/// '[' location-inst (location-inst ',')* ']'
/// unknown-location ::= 'unknown'
///
ParseResult Parser::parseCallSiteLocation(LocationAttr &loc) {
consumeToken(Token::bare_identifier);
// Parse the '('.
if (parseToken(Token::l_paren, "expected '(' in callsite location"))
return failure();
// Parse the callee location.
LocationAttr calleeLoc;
if (parseLocationInstance(calleeLoc))
return failure();
// Parse the 'at'.
if (getToken().isNot(Token::bare_identifier) ||
getToken().getSpelling() != "at")
return emitError("expected 'at' in callsite location");
consumeToken(Token::bare_identifier);
// Parse the caller location.
LocationAttr callerLoc;
if (parseLocationInstance(callerLoc))
return failure();
// Parse the ')'.
if (parseToken(Token::r_paren, "expected ')' in callsite location"))
return failure();
// Return the callsite location.
loc = CallSiteLoc::get(calleeLoc, callerLoc);
return success();
}
ParseResult Parser::parseFusedLocation(LocationAttr &loc) {
consumeToken(Token::bare_identifier);
// Try to parse the optional metadata.
Attribute metadata;
if (consumeIf(Token::less)) {
metadata = parseAttribute();
if (!metadata)
return emitError("expected valid attribute metadata");
// Parse the '>' token.
if (parseToken(Token::greater,
"expected '>' after fused location metadata"))
return failure();
}
SmallVector<Location, 4> locations;
auto parseElt = [&] {
LocationAttr newLoc;
if (parseLocationInstance(newLoc))
return failure();
locations.push_back(newLoc);
return success();
};
if (parseToken(Token::l_square, "expected '[' in fused location") ||
parseCommaSeparatedList(parseElt) ||
parseToken(Token::r_square, "expected ']' in fused location"))
return failure();
// Return the fused location.
loc = FusedLoc::get(locations, metadata, getContext());
return success();
}
ParseResult Parser::parseNameOrFileLineColLocation(LocationAttr &loc) {
auto *ctx = getContext();
auto str = getToken().getStringValue();
consumeToken(Token::string);
// If the next token is ':' this is a filelinecol location.
if (consumeIf(Token::colon)) {
// Parse the line number.
if (getToken().isNot(Token::integer))
return emitError("expected integer line number in FileLineColLoc");
auto line = getToken().getUnsignedIntegerValue();
if (!line.hasValue())
return emitError("expected integer line number in FileLineColLoc");
consumeToken(Token::integer);
// Parse the ':'.
if (parseToken(Token::colon, "expected ':' in FileLineColLoc"))
return failure();
// Parse the column number.
if (getToken().isNot(Token::integer))
return emitError("expected integer column number in FileLineColLoc");
auto column = getToken().getUnsignedIntegerValue();
if (!column.hasValue())
return emitError("expected integer column number in FileLineColLoc");
consumeToken(Token::integer);
loc = FileLineColLoc::get(str, line.getValue(), column.getValue(), ctx);
return success();
}
// Otherwise, this is a NameLoc.
// Check for a child location.
if (consumeIf(Token::l_paren)) {
auto childSourceLoc = getToken().getLoc();
// Parse the child location.
LocationAttr childLoc;
if (parseLocationInstance(childLoc))
return failure();
// The child must not be another NameLoc.
if (childLoc.isa<NameLoc>())
return emitError(childSourceLoc,
"child of NameLoc cannot be another NameLoc");
loc = NameLoc::get(Identifier::get(str, ctx), childLoc);
// Parse the closing ')'.
if (parseToken(Token::r_paren,
"expected ')' after child location of NameLoc"))
return failure();
} else {
loc = NameLoc::get(Identifier::get(str, ctx), ctx);
}
return success();
}
ParseResult Parser::parseLocationInstance(LocationAttr &loc) {
// Handle either name or filelinecol locations.
if (getToken().is(Token::string))
return parseNameOrFileLineColLocation(loc);
// Bare tokens required for other cases.
if (!getToken().is(Token::bare_identifier))
return emitError("expected location instance");
// Check for the 'callsite' signifying a callsite location.
if (getToken().getSpelling() == "callsite")
return parseCallSiteLocation(loc);
// If the token is 'fused', then this is a fused location.
if (getToken().getSpelling() == "fused")
return parseFusedLocation(loc);
// Check for a 'unknown' for an unknown location.
if (getToken().getSpelling() == "unknown") {
consumeToken(Token::bare_identifier);
loc = UnknownLoc::get(getContext());
return success();
}
return emitError("expected location instance");
}