MLIR  19.0.0git
LocationParser.cpp
Go to the documentation of this file.
1 //===- LocationParser.cpp - MLIR Location Parser -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Parser.h"
10 #include "Token.h"
11 #include "mlir/IR/Attributes.h"
13 #include "mlir/IR/Location.h"
14 #include "mlir/Support/LLVM.h"
16 
17 using namespace mlir;
18 using namespace mlir::detail;
19 
20 /// Specific location instances.
21 ///
22 /// location-inst ::= filelinecol-location |
23 /// name-location |
24 /// callsite-location |
25 /// fused-location |
26 /// unknown-location
27 /// filelinecol-location ::= string-literal ':' integer-literal
28 /// ':' integer-literal
29 /// name-location ::= string-literal
30 /// callsite-location ::= 'callsite' '(' location-inst 'at' location-inst ')'
31 /// fused-location ::= fused ('<' attribute-value '>')?
32 /// '[' location-inst (location-inst ',')* ']'
33 /// unknown-location ::= 'unknown'
34 ///
36  consumeToken(Token::bare_identifier);
37 
38  // Parse the '('.
39  if (parseToken(Token::l_paren, "expected '(' in callsite location"))
40  return failure();
41 
42  // Parse the callee location.
43  LocationAttr calleeLoc;
44  if (parseLocationInstance(calleeLoc))
45  return failure();
46 
47  // Parse the 'at'.
48  if (getToken().isNot(Token::bare_identifier) ||
49  getToken().getSpelling() != "at")
50  return emitWrongTokenError("expected 'at' in callsite location");
51  consumeToken(Token::bare_identifier);
52 
53  // Parse the caller location.
54  LocationAttr callerLoc;
55  if (parseLocationInstance(callerLoc))
56  return failure();
57 
58  // Parse the ')'.
59  if (parseToken(Token::r_paren, "expected ')' in callsite location"))
60  return failure();
61 
62  // Return the callsite location.
63  loc = CallSiteLoc::get(calleeLoc, callerLoc);
64  return success();
65 }
66 
68  consumeToken(Token::bare_identifier);
69 
70  // Try to parse the optional metadata.
71  Attribute metadata;
72  if (consumeIf(Token::less)) {
73  metadata = parseAttribute();
74  if (!metadata)
75  return failure();
76 
77  // Parse the '>' token.
78  if (parseToken(Token::greater,
79  "expected '>' after fused location metadata"))
80  return failure();
81  }
82 
83  SmallVector<Location, 4> locations;
84  auto parseElt = [&] {
85  LocationAttr newLoc;
86  if (parseLocationInstance(newLoc))
87  return failure();
88  locations.push_back(newLoc);
89  return success();
90  };
91 
93  " in fused location"))
94  return failure();
95 
96  // Return the fused location.
97  loc = FusedLoc::get(locations, metadata, getContext());
98  return success();
99 }
100 
102  auto *ctx = getContext();
103  auto str = getToken().getStringValue();
104  consumeToken(Token::string);
105 
106  // If the next token is ':' this is a filelinecol location.
107  if (consumeIf(Token::colon)) {
108  // Parse the line number.
109  if (getToken().isNot(Token::integer))
110  return emitWrongTokenError(
111  "expected integer line number in FileLineColLoc");
112  auto line = getToken().getUnsignedIntegerValue();
113  if (!line)
114  return emitWrongTokenError(
115  "expected integer line number in FileLineColLoc");
116  consumeToken(Token::integer);
117 
118  // Parse the ':'.
119  if (parseToken(Token::colon, "expected ':' in FileLineColLoc"))
120  return failure();
121 
122  // Parse the column number.
123  if (getToken().isNot(Token::integer))
124  return emitWrongTokenError(
125  "expected integer column number in FileLineColLoc");
126  auto column = getToken().getUnsignedIntegerValue();
127  if (!column.has_value())
128  return emitError("expected integer column number in FileLineColLoc");
129  consumeToken(Token::integer);
130 
131  loc = FileLineColLoc::get(ctx, str, *line, *column);
132  return success();
133  }
134 
135  // Otherwise, this is a NameLoc.
136 
137  // Check for a child location.
138  if (consumeIf(Token::l_paren)) {
139  // Parse the child location.
140  LocationAttr childLoc;
141  if (parseLocationInstance(childLoc))
142  return failure();
143 
144  loc = NameLoc::get(StringAttr::get(ctx, str), childLoc);
145 
146  // Parse the closing ')'.
147  if (parseToken(Token::r_paren,
148  "expected ')' after child location of NameLoc"))
149  return failure();
150  } else {
151  loc = NameLoc::get(StringAttr::get(ctx, str));
152  }
153 
154  return success();
155 }
156 
158  // Handle aliases.
159  if (getToken().is(Token::hash_identifier)) {
160  Attribute locAttr = parseExtendedAttr(Type());
161  if (!locAttr)
162  return failure();
163  if (!(loc = dyn_cast<LocationAttr>(locAttr)))
164  return emitError("expected location attribute, but got") << locAttr;
165  return success();
166  }
167 
168  // Handle either name or filelinecol locations.
169  if (getToken().is(Token::string))
170  return parseNameOrFileLineColLocation(loc);
171 
172  // Bare tokens required for other cases.
173  if (!getToken().is(Token::bare_identifier))
174  return emitWrongTokenError("expected location instance");
175 
176  // Check for the 'callsite' signifying a callsite location.
177  if (getToken().getSpelling() == "callsite")
178  return parseCallSiteLocation(loc);
179 
180  // If the token is 'fused', then this is a fused location.
181  if (getToken().getSpelling() == "fused")
182  return parseFusedLocation(loc);
183 
184  // Check for a 'unknown' for an unknown location.
185  if (getToken().getSpelling() == "unknown") {
186  consumeToken(Token::bare_identifier);
187  loc = UnknownLoc::get(getContext());
188  return success();
189  }
190 
191  return emitWrongTokenError("expected location instance");
192 }
@ Square
Square brackets surrounding zero or more operands.
Attributes are known-constant values of operations.
Definition: Attributes.h:25
Location objects represent source locations information in MLIR.
Definition: Location.h:31
This class represents success/failure for parsing-like operations that find it important to chain tog...
std::string getStringValue() const
Given a token containing a string literal, return its value, including removing the quote characters ...
Definition: Token.cpp:86
std::optional< unsigned > getUnsignedIntegerValue() const
For an integer token, return its value as an unsigned.
Definition: Token.cpp:34
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
ParseResult parseToken(Token::Kind expectedToken, const Twine &message)
Consume the specified token if present and return success.
Definition: Parser.cpp:267
InFlightDiagnostic emitError(const Twine &message={})
Emit an error and return failure.
Definition: Parser.cpp:192
Attribute parseAttribute(Type type={})
Parse an arbitrary attribute with an optional type.
ParseResult parseLocationInstance(LocationAttr &loc)
Parse a raw location instance.
void consumeToken()
Advance the current lexer onto the next token.
Definition: Parser.h:118
MLIRContext * getContext() const
Definition: Parser.h:37
InFlightDiagnostic emitWrongTokenError(const Twine &message={})
Emit an error about a "wrong token".
Definition: Parser.cpp:215
ParseResult parseCommaSeparatedList(Delimiter delimiter, function_ref< ParseResult()> parseElementFn, StringRef contextMessage=StringRef())
Parse a list of comma-separated items with an optional delimiter.
Definition: Parser.cpp:84
ParseResult parseFusedLocation(LocationAttr &loc)
Parse a fused location instance.
ParseResult parseCallSiteLocation(LocationAttr &loc)
Parse a callsite location instance.
ParseResult parseNameOrFileLineColLocation(LocationAttr &loc)
Parse a name or FileLineCol location instance.
Attribute parseExtendedAttr(Type type)
Parse an extended attribute.
const Token & getToken() const
Return the current token the parser is inspecting.
Definition: Parser.h:102
bool consumeIf(Token::Kind kind)
If the current token has the specified kind, consume it and return true.
Definition: Parser.h:110
Detect if any of the given parameter types has a sub-element handler.
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...