annotate mlir/lib/Parser/Token.h @ 181:df311c476dd5

CreateIdentifierInfo in ParseCbC (not yet worked)
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 31 May 2020 12:30:11 +0900
parents 0572611fdcc8
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- Token.h - MLIR Token Interface ---------------------------*- C++ -*-===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #ifndef MLIR_LIB_PARSER_TOKEN_H
anatofuz
parents:
diff changeset
10 #define MLIR_LIB_PARSER_TOKEN_H
anatofuz
parents:
diff changeset
11
anatofuz
parents:
diff changeset
12 #include "mlir/Support/LLVM.h"
anatofuz
parents:
diff changeset
13 #include "llvm/ADT/StringRef.h"
anatofuz
parents:
diff changeset
14 #include "llvm/Support/SMLoc.h"
anatofuz
parents:
diff changeset
15
anatofuz
parents:
diff changeset
16 namespace mlir {
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 /// This represents a token in the MLIR syntax.
anatofuz
parents:
diff changeset
19 class Token {
anatofuz
parents:
diff changeset
20 public:
anatofuz
parents:
diff changeset
21 enum Kind {
anatofuz
parents:
diff changeset
22 #define TOK_MARKER(NAME) NAME,
anatofuz
parents:
diff changeset
23 #define TOK_IDENTIFIER(NAME) NAME,
anatofuz
parents:
diff changeset
24 #define TOK_LITERAL(NAME) NAME,
anatofuz
parents:
diff changeset
25 #define TOK_PUNCTUATION(NAME, SPELLING) NAME,
anatofuz
parents:
diff changeset
26 #define TOK_KEYWORD(SPELLING) kw_##SPELLING,
anatofuz
parents:
diff changeset
27 #include "TokenKinds.def"
anatofuz
parents:
diff changeset
28 };
anatofuz
parents:
diff changeset
29
anatofuz
parents:
diff changeset
30 Token(Kind kind, StringRef spelling) : kind(kind), spelling(spelling) {}
anatofuz
parents:
diff changeset
31
anatofuz
parents:
diff changeset
32 // Return the bytes that make up this token.
anatofuz
parents:
diff changeset
33 StringRef getSpelling() const { return spelling; }
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 // Token classification.
anatofuz
parents:
diff changeset
36 Kind getKind() const { return kind; }
anatofuz
parents:
diff changeset
37 bool is(Kind K) const { return kind == K; }
anatofuz
parents:
diff changeset
38
anatofuz
parents:
diff changeset
39 bool isAny(Kind k1, Kind k2) const { return is(k1) || is(k2); }
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 /// Return true if this token is one of the specified kinds.
anatofuz
parents:
diff changeset
42 template <typename... T>
anatofuz
parents:
diff changeset
43 bool isAny(Kind k1, Kind k2, Kind k3, T... others) const {
anatofuz
parents:
diff changeset
44 if (is(k1))
anatofuz
parents:
diff changeset
45 return true;
anatofuz
parents:
diff changeset
46 return isAny(k2, k3, others...);
anatofuz
parents:
diff changeset
47 }
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 bool isNot(Kind k) const { return kind != k; }
anatofuz
parents:
diff changeset
50
anatofuz
parents:
diff changeset
51 /// Return true if this token isn't one of the specified kinds.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
52 template <typename... T>
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
53 bool isNot(Kind k1, Kind k2, T... others) const {
150
anatofuz
parents:
diff changeset
54 return !isAny(k1, k2, others...);
anatofuz
parents:
diff changeset
55 }
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 /// Return true if this is one of the keyword token kinds (e.g. kw_if).
anatofuz
parents:
diff changeset
58 bool isKeyword() const;
anatofuz
parents:
diff changeset
59
anatofuz
parents:
diff changeset
60 // Helpers to decode specific sorts of tokens.
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 /// For an integer token, return its value as an unsigned. If it doesn't fit,
anatofuz
parents:
diff changeset
63 /// return None.
anatofuz
parents:
diff changeset
64 Optional<unsigned> getUnsignedIntegerValue() const;
anatofuz
parents:
diff changeset
65
anatofuz
parents:
diff changeset
66 /// For an integer token, return its value as an uint64_t. If it doesn't fit,
anatofuz
parents:
diff changeset
67 /// return None.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
68 static Optional<uint64_t> getUInt64IntegerValue(StringRef spelling);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
69 Optional<uint64_t> getUInt64IntegerValue() const {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
70 return getUInt64IntegerValue(getSpelling());
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
71 }
150
anatofuz
parents:
diff changeset
72
anatofuz
parents:
diff changeset
73 /// For a floatliteral token, return its value as a double. Returns None in
anatofuz
parents:
diff changeset
74 /// the case of underflow or overflow.
anatofuz
parents:
diff changeset
75 Optional<double> getFloatingPointValue() const;
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 /// For an inttype token, return its bitwidth.
anatofuz
parents:
diff changeset
78 Optional<unsigned> getIntTypeBitwidth() const;
anatofuz
parents:
diff changeset
79
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
80 /// For an inttype token, return its signedness semantics: llvm::None means no
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
81 /// signedness semantics; true means signed integer type; false means unsigned
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
82 /// integer type.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
83 Optional<bool> getIntTypeSignedness() const;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
84
150
anatofuz
parents:
diff changeset
85 /// Given a hash_identifier token like #123, try to parse the number out of
anatofuz
parents:
diff changeset
86 /// the identifier, returning None if it is a named identifier like #x or
anatofuz
parents:
diff changeset
87 /// if the integer doesn't fit.
anatofuz
parents:
diff changeset
88 Optional<unsigned> getHashIdentifierNumber() const;
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 /// Given a token containing a string literal, return its value, including
anatofuz
parents:
diff changeset
91 /// removing the quote characters and unescaping the contents of the string.
anatofuz
parents:
diff changeset
92 std::string getStringValue() const;
anatofuz
parents:
diff changeset
93
anatofuz
parents:
diff changeset
94 // Location processing.
anatofuz
parents:
diff changeset
95 llvm::SMLoc getLoc() const;
anatofuz
parents:
diff changeset
96 llvm::SMLoc getEndLoc() const;
anatofuz
parents:
diff changeset
97 llvm::SMRange getLocRange() const;
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 /// Given a punctuation or keyword token kind, return the spelling of the
anatofuz
parents:
diff changeset
100 /// token as a string. Warning: This will abort on markers, identifiers and
anatofuz
parents:
diff changeset
101 /// literal tokens since they have no fixed spelling.
anatofuz
parents:
diff changeset
102 static StringRef getTokenSpelling(Kind kind);
anatofuz
parents:
diff changeset
103
anatofuz
parents:
diff changeset
104 private:
anatofuz
parents:
diff changeset
105 /// Discriminator that indicates the sort of token this is.
anatofuz
parents:
diff changeset
106 Kind kind;
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 /// A reference to the entire token contents; this is always a pointer into
anatofuz
parents:
diff changeset
109 /// a memory buffer owned by the source manager.
anatofuz
parents:
diff changeset
110 StringRef spelling;
anatofuz
parents:
diff changeset
111 };
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 } // end namespace mlir
anatofuz
parents:
diff changeset
114
anatofuz
parents:
diff changeset
115 #endif // MLIR_LIB_PARSER_TOKEN_H