blob: 4e01f5a6a53fd260e085337f5f0f86a0b962b265 [file] [log] [blame]
Chris Lattnere79379a2018-06-22 10:39:19 -07001//===- Token.cpp - MLIR Token Implementation ------------------------------===//
2//
3// Copyright 2019 The MLIR Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// =============================================================================
17//
18// This file implements the Token class for the MLIR textual form.
19//
20//===----------------------------------------------------------------------===//
21
22#include "Token.h"
Chris Lattner0497c4b2018-08-15 09:09:54 -070023#include "llvm/ADT/StringExtras.h"
Chris Lattnere79379a2018-06-22 10:39:19 -070024using namespace mlir;
25using llvm::SMLoc;
26using llvm::SMRange;
27
28SMLoc Token::getLoc() const {
29 return SMLoc::getFromPointer(spelling.data());
30}
31
32SMLoc Token::getEndLoc() const {
33 return SMLoc::getFromPointer(spelling.data() + spelling.size());
34}
35
36SMRange Token::getLocRange() const {
37 return SMRange(getLoc(), getEndLoc());
38}
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070039
40/// For an integer token, return its value as an unsigned. If it doesn't fit,
41/// return None.
Chris Lattnered65a732018-06-28 20:45:33 -070042Optional<unsigned> Token::getUnsignedIntegerValue() const {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070043 bool isHex = spelling.size() > 1 && spelling[1] == 'x';
44
45 unsigned result = 0;
46 if (spelling.getAsInteger(isHex ? 0 : 10, result))
47 return None;
48 return result;
49}
Chris Lattnered65a732018-06-28 20:45:33 -070050
Chris Lattner7121b802018-07-04 20:45:39 -070051/// For an integer token, return its value as a uint64_t. If it doesn't fit,
52/// return None.
53Optional<uint64_t> Token::getUInt64IntegerValue() const {
54 bool isHex = spelling.size() > 1 && spelling[1] == 'x';
55
56 uint64_t result = 0;
57 if (spelling.getAsInteger(isHex ? 0 : 10, result))
58 return None;
59 return result;
60}
61
Jacques Pienaar84491092018-07-31 17:15:15 -070062/// For a floatliteral, return its value as a double. Return None if the value
63/// underflows or overflows.
64Optional<double> Token::getFloatingPointValue() const {
65 double result = 0;
66 if (spelling.getAsDouble(result))
67 return None;
68 return result;
69}
Chris Lattner7121b802018-07-04 20:45:39 -070070
Chris Lattnerf958bbe2018-06-29 22:08:05 -070071/// For an inttype token, return its bitwidth.
72Optional<unsigned> Token::getIntTypeBitwidth() const {
Chris Lattner7121b802018-07-04 20:45:39 -070073 unsigned result = 0;
Chris Lattnerf958bbe2018-06-29 22:08:05 -070074 if (spelling[1] == '0' ||
Nicolas Vasilache1b8c0842018-08-27 10:26:15 -070075 spelling.drop_front().getAsInteger(10, result) || result == 0)
Chris Lattnerf958bbe2018-06-29 22:08:05 -070076 return None;
77 return result;
78}
79
Chris Lattnered65a732018-06-28 20:45:33 -070080/// Given a 'string' token, return its value, including removing the quote
Chris Lattner0497c4b2018-08-15 09:09:54 -070081/// characters and unescaping the contents of the string. The lexer has already
82/// verified that this token is valid.
Chris Lattnered65a732018-06-28 20:45:33 -070083std::string Token::getStringValue() const {
Chris Lattner0497c4b2018-08-15 09:09:54 -070084 assert(getKind() == string);
James Molloy3cdb8aa2018-08-14 01:16:45 -070085 // Start by dropping the quotes.
Chris Lattner0497c4b2018-08-15 09:09:54 -070086 StringRef bytes = getSpelling().drop_front().drop_back();
87
88 std::string result;
89 result.reserve(bytes.size());
90 for (unsigned i = 0, e = bytes.size(); i != e;) {
91 auto c = bytes[i++];
92 if (c != '\\') {
93 result.push_back(c);
94 continue;
95 }
96
97 assert(i + 1 < e && "invalid string should be caught by lexer");
98 auto c1 = bytes[i++];
99 switch (c1) {
100 case '"':
101 case '\\':
102 result.push_back(c1);
103 continue;
104 case 'n':
105 result.push_back('\n');
106 continue;
107 case 't':
108 result.push_back('\t');
109 continue;
110 default:
111 break;
112 }
113
James Molloy14ad73f2018-08-16 08:43:55 -0700114 assert(i + 1 <= e && "invalid string should be caught by lexer");
Chris Lattner0497c4b2018-08-15 09:09:54 -0700115 auto c2 = bytes[i++];
116
117 assert(llvm::isHexDigit(c1) && llvm::isHexDigit(c2) && "invalid escape");
118 result.push_back((llvm::hexDigitValue(c1) << 4) | llvm::hexDigitValue(c2));
119 }
120
121 return result;
Chris Lattnered65a732018-06-28 20:45:33 -0700122}
Chris Lattner8da0c282018-06-29 11:15:56 -0700123
Chris Lattner6119d382018-07-20 18:41:34 -0700124/// Given a hash_identifier token like #123, try to parse the number out of
125/// the identifier, returning None if it is a named identifier like #x or
126/// if the integer doesn't fit.
127Optional<unsigned> Token::getHashIdentifierNumber() const {
128 assert(getKind() == hash_identifier);
129 unsigned result = 0;
130 if (spelling.drop_front().getAsInteger(10, result))
131 return None;
132 return result;
133}
Chris Lattner8da0c282018-06-29 11:15:56 -0700134
135/// Given a punctuation or keyword token kind, return the spelling of the
136/// token as a string. Warning: This will abort on markers, identifiers and
137/// literal tokens since they have no fixed spelling.
138StringRef Token::getTokenSpelling(Kind kind) {
Chris Lattner7121b802018-07-04 20:45:39 -0700139 switch (kind) {
Jacques Pienaar16916002018-07-07 15:48:05 -0700140 default: llvm_unreachable("This token kind has no fixed spelling");
Chris Lattner8da0c282018-06-29 11:15:56 -0700141#define TOK_PUNCTUATION(NAME, SPELLING) case NAME: return SPELLING;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700142#define TOK_OPERATOR(NAME, SPELLING) case NAME: return SPELLING;
Chris Lattner8da0c282018-06-29 11:15:56 -0700143#define TOK_KEYWORD(SPELLING) case kw_##SPELLING: return #SPELLING;
144#include "TokenKinds.def"
Chris Lattner7121b802018-07-04 20:45:39 -0700145 }
146}
147
148/// Return true if this is one of the keyword token kinds (e.g. kw_if).
149bool Token::isKeyword() const {
150 switch (kind) {
151 default: return false;
152#define TOK_KEYWORD(SPELLING) case kw_##SPELLING: return true;
153#include "TokenKinds.def"
154 }
Chris Lattner8da0c282018-06-29 11:15:56 -0700155}