blob: 2d78168d83d785283f7974ade8dcbd1a803b4bc3 [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
61
Chris Lattnerf958bbe2018-06-29 22:08:05 -070062/// For an inttype token, return its bitwidth.
63Optional<unsigned> Token::getIntTypeBitwidth() const {
Chris Lattner7121b802018-07-04 20:45:39 -070064 unsigned result = 0;
Chris Lattnerf958bbe2018-06-29 22:08:05 -070065 if (spelling[1] == '0' ||
66 spelling.drop_front().getAsInteger(10, result) ||
67 // Arbitrary but large limit on bitwidth.
68 result > 4096 || result == 0)
69 return None;
70 return result;
71}
72
73
Chris Lattnered65a732018-06-28 20:45:33 -070074/// Given a 'string' token, return its value, including removing the quote
75/// characters and unescaping the contents of the string.
76std::string Token::getStringValue() const {
77 // TODO: Handle escaping.
78
79 // Just drop the quotes off for now.
80 return getSpelling().drop_front().drop_back().str();
81}
Chris Lattner8da0c282018-06-29 11:15:56 -070082
Chris Lattner6119d382018-07-20 18:41:34 -070083/// Given a hash_identifier token like #123, try to parse the number out of
84/// the identifier, returning None if it is a named identifier like #x or
85/// if the integer doesn't fit.
86Optional<unsigned> Token::getHashIdentifierNumber() const {
87 assert(getKind() == hash_identifier);
88 unsigned result = 0;
89 if (spelling.drop_front().getAsInteger(10, result))
90 return None;
91 return result;
92}
Chris Lattner8da0c282018-06-29 11:15:56 -070093
94/// Given a punctuation or keyword token kind, return the spelling of the
95/// token as a string. Warning: This will abort on markers, identifiers and
96/// literal tokens since they have no fixed spelling.
97StringRef Token::getTokenSpelling(Kind kind) {
Chris Lattner7121b802018-07-04 20:45:39 -070098 switch (kind) {
Jacques Pienaar16916002018-07-07 15:48:05 -070099 default: llvm_unreachable("This token kind has no fixed spelling");
Chris Lattner8da0c282018-06-29 11:15:56 -0700100#define TOK_PUNCTUATION(NAME, SPELLING) case NAME: return SPELLING;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700101#define TOK_OPERATOR(NAME, SPELLING) case NAME: return SPELLING;
Chris Lattner8da0c282018-06-29 11:15:56 -0700102#define TOK_KEYWORD(SPELLING) case kw_##SPELLING: return #SPELLING;
103#include "TokenKinds.def"
Chris Lattner7121b802018-07-04 20:45:39 -0700104 }
105}
106
107/// Return true if this is one of the keyword token kinds (e.g. kw_if).
108bool Token::isKeyword() const {
109 switch (kind) {
110 default: return false;
111#define TOK_KEYWORD(SPELLING) case kw_##SPELLING: return true;
112#include "TokenKinds.def"
113 }
Chris Lattner8da0c282018-06-29 11:15:56 -0700114}