blob: 9fc2b2f40d1896cbdda59fe3f973f8d7cf525c0e [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"
23using namespace mlir;
24using llvm::SMLoc;
25using llvm::SMRange;
26
27SMLoc Token::getLoc() const {
28 return SMLoc::getFromPointer(spelling.data());
29}
30
31SMLoc Token::getEndLoc() const {
32 return SMLoc::getFromPointer(spelling.data() + spelling.size());
33}
34
35SMRange Token::getLocRange() const {
36 return SMRange(getLoc(), getEndLoc());
37}
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070038
39/// For an integer token, return its value as an unsigned. If it doesn't fit,
40/// return None.
Chris Lattnered65a732018-06-28 20:45:33 -070041Optional<unsigned> Token::getUnsignedIntegerValue() const {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070042 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 Lattnered65a732018-06-28 20:45:33 -070049
Chris Lattner7121b802018-07-04 20:45:39 -070050/// For an integer token, return its value as a uint64_t. If it doesn't fit,
51/// return None.
52Optional<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 Pienaar84491092018-07-31 17:15:15 -070061/// For a floatliteral, return its value as a double. Return None if the value
62/// underflows or overflows.
63Optional<double> Token::getFloatingPointValue() const {
64 double result = 0;
65 if (spelling.getAsDouble(result))
66 return None;
67 return result;
68}
Chris Lattner7121b802018-07-04 20:45:39 -070069
Chris Lattnerf958bbe2018-06-29 22:08:05 -070070/// For an inttype token, return its bitwidth.
71Optional<unsigned> Token::getIntTypeBitwidth() const {
Chris Lattner7121b802018-07-04 20:45:39 -070072 unsigned result = 0;
Chris Lattnerf958bbe2018-06-29 22:08:05 -070073 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 Lattnered65a732018-06-28 20:45:33 -070081/// Given a 'string' token, return its value, including removing the quote
82/// characters and unescaping the contents of the string.
83std::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 Lattner8da0c282018-06-29 11:15:56 -070089
Chris Lattner6119d382018-07-20 18:41:34 -070090/// 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.
93Optional<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 Lattner8da0c282018-06-29 11:15:56 -0700100
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.
104StringRef Token::getTokenSpelling(Kind kind) {
Chris Lattner7121b802018-07-04 20:45:39 -0700105 switch (kind) {
Jacques Pienaar16916002018-07-07 15:48:05 -0700106 default: llvm_unreachable("This token kind has no fixed spelling");
Chris Lattner8da0c282018-06-29 11:15:56 -0700107#define TOK_PUNCTUATION(NAME, SPELLING) case NAME: return SPELLING;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700108#define TOK_OPERATOR(NAME, SPELLING) case NAME: return SPELLING;
Chris Lattner8da0c282018-06-29 11:15:56 -0700109#define TOK_KEYWORD(SPELLING) case kw_##SPELLING: return #SPELLING;
110#include "TokenKinds.def"
Chris Lattner7121b802018-07-04 20:45:39 -0700111 }
112}
113
114/// Return true if this is one of the keyword token kinds (e.g. kw_if).
115bool 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 Lattner8da0c282018-06-29 11:15:56 -0700121}