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" |
| 23 | using namespace mlir; |
| 24 | using llvm::SMLoc; |
| 25 | using llvm::SMRange; |
| 26 | |
| 27 | SMLoc Token::getLoc() const { |
| 28 | return SMLoc::getFromPointer(spelling.data()); |
| 29 | } |
| 30 | |
| 31 | SMLoc Token::getEndLoc() const { |
| 32 | return SMLoc::getFromPointer(spelling.data() + spelling.size()); |
| 33 | } |
| 34 | |
| 35 | SMRange Token::getLocRange() const { |
| 36 | return SMRange(getLoc(), getEndLoc()); |
| 37 | } |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 38 | |
| 39 | /// For an integer token, return its value as an unsigned. If it doesn't fit, |
| 40 | /// return None. |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 41 | Optional<unsigned> Token::getUnsignedIntegerValue() const { |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 42 | bool isHex = spelling.size() > 1 && spelling[1] == 'x'; |
| 43 | |
| 44 | unsigned result = 0; |
| 45 | if (spelling.getAsInteger(isHex ? 0 : 10, result)) |
| 46 | return None; |
| 47 | return result; |
| 48 | } |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 49 | |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 50 | /// For an integer token, return its value as a uint64_t. If it doesn't fit, |
| 51 | /// return None. |
| 52 | Optional<uint64_t> Token::getUInt64IntegerValue() const { |
| 53 | bool isHex = spelling.size() > 1 && spelling[1] == 'x'; |
| 54 | |
| 55 | uint64_t result = 0; |
| 56 | if (spelling.getAsInteger(isHex ? 0 : 10, result)) |
| 57 | return None; |
| 58 | return result; |
| 59 | } |
| 60 | |
Jacques Pienaar | 8449109 | 2018-07-31 17:15:15 -0700 | [diff] [blame] | 61 | /// For a floatliteral, return its value as a double. Return None if the value |
| 62 | /// underflows or overflows. |
| 63 | Optional<double> Token::getFloatingPointValue() const { |
| 64 | double result = 0; |
| 65 | if (spelling.getAsDouble(result)) |
| 66 | return None; |
| 67 | return result; |
| 68 | } |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 69 | |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 70 | /// For an inttype token, return its bitwidth. |
| 71 | Optional<unsigned> Token::getIntTypeBitwidth() const { |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 72 | unsigned result = 0; |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 73 | if (spelling[1] == '0' || |
| 74 | spelling.drop_front().getAsInteger(10, result) || |
| 75 | // Arbitrary but large limit on bitwidth. |
| 76 | result > 4096 || result == 0) |
| 77 | return None; |
| 78 | return result; |
| 79 | } |
| 80 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 81 | /// Given a 'string' token, return its value, including removing the quote |
| 82 | /// characters and unescaping the contents of the string. |
| 83 | std::string Token::getStringValue() const { |
| 84 | // TODO: Handle escaping. |
| 85 | |
| 86 | // Just drop the quotes off for now. |
| 87 | return getSpelling().drop_front().drop_back().str(); |
| 88 | } |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 89 | |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 90 | /// Given a hash_identifier token like #123, try to parse the number out of |
| 91 | /// the identifier, returning None if it is a named identifier like #x or |
| 92 | /// if the integer doesn't fit. |
| 93 | Optional<unsigned> Token::getHashIdentifierNumber() const { |
| 94 | assert(getKind() == hash_identifier); |
| 95 | unsigned result = 0; |
| 96 | if (spelling.drop_front().getAsInteger(10, result)) |
| 97 | return None; |
| 98 | return result; |
| 99 | } |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 100 | |
| 101 | /// Given a punctuation or keyword token kind, return the spelling of the |
| 102 | /// token as a string. Warning: This will abort on markers, identifiers and |
| 103 | /// literal tokens since they have no fixed spelling. |
| 104 | StringRef Token::getTokenSpelling(Kind kind) { |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 105 | switch (kind) { |
Jacques Pienaar | 1691600 | 2018-07-07 15:48:05 -0700 | [diff] [blame] | 106 | default: llvm_unreachable("This token kind has no fixed spelling"); |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 107 | #define TOK_PUNCTUATION(NAME, SPELLING) case NAME: return SPELLING; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 108 | #define TOK_OPERATOR(NAME, SPELLING) case NAME: return SPELLING; |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 109 | #define TOK_KEYWORD(SPELLING) case kw_##SPELLING: return #SPELLING; |
| 110 | #include "TokenKinds.def" |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | /// Return true if this is one of the keyword token kinds (e.g. kw_if). |
| 115 | bool Token::isKeyword() const { |
| 116 | switch (kind) { |
| 117 | default: return false; |
| 118 | #define TOK_KEYWORD(SPELLING) case kw_##SPELLING: return true; |
| 119 | #include "TokenKinds.def" |
| 120 | } |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 121 | } |