Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 1 | //===- 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 Lattner | 0497c4b | 2018-08-15 09:09:54 -0700 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 24 | using namespace mlir; |
| 25 | using llvm::SMLoc; |
| 26 | using llvm::SMRange; |
| 27 | |
| 28 | SMLoc Token::getLoc() const { |
| 29 | return SMLoc::getFromPointer(spelling.data()); |
| 30 | } |
| 31 | |
| 32 | SMLoc Token::getEndLoc() const { |
| 33 | return SMLoc::getFromPointer(spelling.data() + spelling.size()); |
| 34 | } |
| 35 | |
| 36 | SMRange Token::getLocRange() const { |
| 37 | return SMRange(getLoc(), getEndLoc()); |
| 38 | } |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 39 | |
| 40 | /// For an integer token, return its value as an unsigned. If it doesn't fit, |
| 41 | /// return None. |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 42 | Optional<unsigned> Token::getUnsignedIntegerValue() const { |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 43 | 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 Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 50 | |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 51 | /// For an integer token, return its value as a uint64_t. If it doesn't fit, |
| 52 | /// return None. |
| 53 | Optional<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 Pienaar | 8449109 | 2018-07-31 17:15:15 -0700 | [diff] [blame] | 62 | /// For a floatliteral, return its value as a double. Return None if the value |
| 63 | /// underflows or overflows. |
| 64 | Optional<double> Token::getFloatingPointValue() const { |
| 65 | double result = 0; |
| 66 | if (spelling.getAsDouble(result)) |
| 67 | return None; |
| 68 | return result; |
| 69 | } |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 70 | |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 71 | /// For an inttype token, return its bitwidth. |
| 72 | Optional<unsigned> Token::getIntTypeBitwidth() const { |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 73 | unsigned result = 0; |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 74 | if (spelling[1] == '0' || |
Nicolas Vasilache | 1b8c084 | 2018-08-27 10:26:15 -0700 | [diff] [blame] | 75 | spelling.drop_front().getAsInteger(10, result) || result == 0) |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 76 | return None; |
| 77 | return result; |
| 78 | } |
| 79 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 80 | /// Given a 'string' token, return its value, including removing the quote |
Chris Lattner | 0497c4b | 2018-08-15 09:09:54 -0700 | [diff] [blame] | 81 | /// characters and unescaping the contents of the string. The lexer has already |
| 82 | /// verified that this token is valid. |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 83 | std::string Token::getStringValue() const { |
Chris Lattner | 0497c4b | 2018-08-15 09:09:54 -0700 | [diff] [blame] | 84 | assert(getKind() == string); |
James Molloy | 3cdb8aa | 2018-08-14 01:16:45 -0700 | [diff] [blame] | 85 | // Start by dropping the quotes. |
Chris Lattner | 0497c4b | 2018-08-15 09:09:54 -0700 | [diff] [blame] | 86 | 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 Molloy | 14ad73f | 2018-08-16 08:43:55 -0700 | [diff] [blame] | 114 | assert(i + 1 <= e && "invalid string should be caught by lexer"); |
Chris Lattner | 0497c4b | 2018-08-15 09:09:54 -0700 | [diff] [blame] | 115 | 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 Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 122 | } |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 123 | |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 124 | /// 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. |
| 127 | Optional<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 Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 134 | |
| 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. |
| 138 | StringRef Token::getTokenSpelling(Kind kind) { |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 139 | switch (kind) { |
Jacques Pienaar | 1691600 | 2018-07-07 15:48:05 -0700 | [diff] [blame] | 140 | default: llvm_unreachable("This token kind has no fixed spelling"); |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 141 | #define TOK_PUNCTUATION(NAME, SPELLING) case NAME: return SPELLING; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 142 | #define TOK_OPERATOR(NAME, SPELLING) case NAME: return SPELLING; |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 143 | #define TOK_KEYWORD(SPELLING) case kw_##SPELLING: return #SPELLING; |
| 144 | #include "TokenKinds.def" |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | /// Return true if this is one of the keyword token kinds (e.g. kw_if). |
| 149 | bool 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 Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 155 | } |