| 1 | //===- Token.cpp - MLIR Token Implementation ------------------------------===// |
| 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 | // This file implements the Token class for the MLIR textual form. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "Token.h" |
| 14 | #include "mlir/Support/LLVM.h" |
| 15 | #include "llvm/ADT/StringExtras.h" |
| 16 | #include "llvm/Support/ErrorHandling.h" |
| 17 | #include <cassert> |
| 18 | #include <cstdint> |
| 19 | #include <optional> |
| 20 | #include <string> |
| 21 | |
| 22 | using namespace mlir; |
| 23 | |
| 24 | SMLoc Token::getLoc() const { return SMLoc::getFromPointer(Ptr: spelling.data()); } |
| 25 | |
| 26 | SMLoc Token::getEndLoc() const { |
| 27 | return SMLoc::getFromPointer(Ptr: spelling.data() + spelling.size()); |
| 28 | } |
| 29 | |
| 30 | SMRange Token::getLocRange() const { return SMRange(getLoc(), getEndLoc()); } |
| 31 | |
| 32 | /// For an integer token, return its value as an unsigned. If it doesn't fit, |
| 33 | /// return std::nullopt. |
| 34 | std::optional<unsigned> Token::getUnsignedIntegerValue() const { |
| 35 | bool isHex = spelling.size() > 1 && spelling[1] == 'x'; |
| 36 | |
| 37 | unsigned result = 0; |
| 38 | if (spelling.getAsInteger(Radix: isHex ? 0 : 10, Result&: result)) |
| 39 | return std::nullopt; |
| 40 | return result; |
| 41 | } |
| 42 | |
| 43 | /// For an integer token, return its value as a uint64_t. If it doesn't fit, |
| 44 | /// return std::nullopt. |
| 45 | std::optional<uint64_t> Token::getUInt64IntegerValue(StringRef spelling) { |
| 46 | bool isHex = spelling.size() > 1 && spelling[1] == 'x'; |
| 47 | |
| 48 | uint64_t result = 0; |
| 49 | if (spelling.getAsInteger(Radix: isHex ? 0 : 10, Result&: result)) |
| 50 | return std::nullopt; |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | /// For an inttype token, return its bitwidth. |
| 55 | std::optional<unsigned> Token::getIntTypeBitwidth() const { |
| 56 | assert(getKind() == inttype); |
| 57 | unsigned bitwidthStart = (spelling[0] == 'i' ? 1 : 2); |
| 58 | unsigned result = 0; |
| 59 | if (spelling.drop_front(N: bitwidthStart).getAsInteger(Radix: 10, Result&: result)) |
| 60 | return std::nullopt; |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | std::optional<bool> Token::getIntTypeSignedness() const { |
| 65 | assert(getKind() == inttype); |
| 66 | if (spelling[0] == 'i') |
| 67 | return std::nullopt; |
| 68 | if (spelling[0] == 's') |
| 69 | return true; |
| 70 | assert(spelling[0] == 'u'); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | /// Given a token containing a string literal, return its value, including |
| 75 | /// removing the quote characters and unescaping the contents of the string. The |
| 76 | /// lexer has already verified that this token is valid. |
| 77 | std::string Token::getStringValue() const { |
| 78 | assert(getKind() == string || getKind() == code_complete || |
| 79 | (getKind() == at_identifier && getSpelling()[1] == '"')); |
| 80 | // Start by dropping the quotes. |
| 81 | StringRef bytes = getSpelling().drop_front(); |
| 82 | if (getKind() != Token::code_complete) { |
| 83 | bytes = bytes.drop_back(); |
| 84 | if (getKind() == at_identifier) |
| 85 | bytes = bytes.drop_front(); |
| 86 | } |
| 87 | |
| 88 | std::string result; |
| 89 | result.reserve(res_arg: 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: 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(c: c1); |
| 103 | continue; |
| 104 | case 'n': |
| 105 | result.push_back(c: '\n'); |
| 106 | continue; |
| 107 | case 't': |
| 108 | result.push_back(c: '\t'); |
| 109 | continue; |
| 110 | default: |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | assert(i + 1 <= e && "invalid string should be caught by lexer" ); |
| 115 | auto c2 = bytes[i++]; |
| 116 | |
| 117 | assert(llvm::isHexDigit(c1) && llvm::isHexDigit(c2) && "invalid escape" ); |
| 118 | result.push_back(c: (llvm::hexDigitValue(C: c1) << 4) | llvm::hexDigitValue(C: c2)); |
| 119 | } |
| 120 | |
| 121 | return result; |
| 122 | } |
| 123 | |
| 124 | /// Given a token containing a hex string literal, return its value or |
| 125 | /// std::nullopt if the token does not contain a valid hex string. |
| 126 | std::optional<std::string> Token::getHexStringValue() const { |
| 127 | assert(getKind() == string); |
| 128 | |
| 129 | // Get the internal string data, without the quotes. |
| 130 | StringRef bytes = getSpelling().drop_front().drop_back(); |
| 131 | |
| 132 | // Try to extract the binary data from the hex string. We expect the hex |
| 133 | // string to start with `0x` and have an even number of hex nibbles (nibbles |
| 134 | // should come in pairs). |
| 135 | std::string hex; |
| 136 | if (!bytes.consume_front(Prefix: "0x" ) || (bytes.size() & 1) || |
| 137 | !llvm::tryGetFromHex(Input: bytes, Output&: hex)) |
| 138 | return std::nullopt; |
| 139 | return hex; |
| 140 | } |
| 141 | |
| 142 | /// Given a token containing a symbol reference, return the unescaped string |
| 143 | /// value. |
| 144 | std::string Token::getSymbolReference() const { |
| 145 | assert(is(Token::at_identifier) && "expected valid @-identifier" ); |
| 146 | StringRef nameStr = getSpelling().drop_front(); |
| 147 | |
| 148 | // Check to see if the reference is a string literal, or a bare identifier. |
| 149 | if (nameStr.front() == '"') |
| 150 | return getStringValue(); |
| 151 | return std::string(nameStr); |
| 152 | } |
| 153 | |
| 154 | /// Given a hash_identifier token like #123, try to parse the number out of |
| 155 | /// the identifier, returning std::nullopt if it is a named identifier like #x |
| 156 | /// or if the integer doesn't fit. |
| 157 | std::optional<unsigned> Token::getHashIdentifierNumber() const { |
| 158 | assert(getKind() == hash_identifier); |
| 159 | unsigned result = 0; |
| 160 | if (spelling.drop_front().getAsInteger(Radix: 10, Result&: result)) |
| 161 | return std::nullopt; |
| 162 | return result; |
| 163 | } |
| 164 | |
| 165 | /// Given a punctuation or keyword token kind, return the spelling of the |
| 166 | /// token as a string. Warning: This will abort on markers, identifiers and |
| 167 | /// literal tokens since they have no fixed spelling. |
| 168 | StringRef Token::getTokenSpelling(Kind kind) { |
| 169 | switch (kind) { |
| 170 | default: |
| 171 | llvm_unreachable("This token kind has no fixed spelling" ); |
| 172 | #define TOK_PUNCTUATION(NAME, SPELLING) \ |
| 173 | case NAME: \ |
| 174 | return SPELLING; |
| 175 | #define TOK_KEYWORD(SPELLING) \ |
| 176 | case kw_##SPELLING: \ |
| 177 | return #SPELLING; |
| 178 | #include "TokenKinds.def" |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /// Return true if this is one of the keyword token kinds (e.g. kw_if). |
| 183 | bool Token::isKeyword() const { |
| 184 | switch (kind) { |
| 185 | default: |
| 186 | return false; |
| 187 | #define TOK_KEYWORD(SPELLING) \ |
| 188 | case kw_##SPELLING: \ |
| 189 | return true; |
| 190 | #include "TokenKinds.def" |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | bool Token::isCodeCompletionFor(Kind kind) const { |
| 195 | if (!isCodeCompletion() || spelling.empty()) |
| 196 | return false; |
| 197 | switch (kind) { |
| 198 | case Kind::string: |
| 199 | return spelling[0] == '"'; |
| 200 | case Kind::hash_identifier: |
| 201 | return spelling[0] == '#'; |
| 202 | case Kind::percent_identifier: |
| 203 | return spelling[0] == '%'; |
| 204 | case Kind::caret_identifier: |
| 205 | return spelling[0] == '^'; |
| 206 | case Kind::exclamation_identifier: |
| 207 | return spelling[0] == '!'; |
| 208 | default: |
| 209 | return false; |
| 210 | } |
| 211 | } |
| 212 | |