blob: 436c5b6db7469a951dd0dbc7dfad1ece82fc9444 [file] [log] [blame]
alokp@chromium.org4e4b8072011-08-07 05:36:04 +00001//
2// Copyright (c) 2011 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#ifndef COMPILER_PREPROCESSOR_TOKEN_H_
8#define COMPILER_PREPROCESSOR_TOKEN_H_
9
zmo@google.com5e75f342012-04-12 23:47:10 +000010#include <ostream>
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000011#include <string>
alokp@chromium.org3a01d1b2011-08-30 05:10:53 +000012
Corentin Wallez054f7ed2016-09-20 17:15:59 -040013#include "compiler/preprocessor/SourceLocation.h"
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000014
Geoff Lang197d5292018-04-25 14:29:00 -040015namespace angle
16{
17
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000018namespace pp
19{
20
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000021struct Token
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000022{
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000023 enum Type
24 {
Corentin Wallezdc0fa462017-02-01 14:44:43 -050025 // Calling this ERROR causes a conflict with wingdi.h
26 GOT_ERROR = -1,
Jamie Madillb980c562018-11-27 11:34:27 -050027 LAST = 0, // EOF.
alokp@chromium.org78a35192012-04-19 17:16:26 +000028
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000029 IDENTIFIER = 258,
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000030
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000031 CONST_INT,
32 CONST_FLOAT,
alokp@chromium.orgb81c4012011-08-21 06:53:11 +000033
alokp@chromium.org3f990c42012-04-03 19:50:35 +000034 OP_INC,
35 OP_DEC,
36 OP_LEFT,
37 OP_RIGHT,
38 OP_LE,
39 OP_GE,
40 OP_EQ,
41 OP_NE,
42 OP_AND,
43 OP_XOR,
44 OP_OR,
45 OP_ADD_ASSIGN,
46 OP_SUB_ASSIGN,
47 OP_MUL_ASSIGN,
48 OP_DIV_ASSIGN,
49 OP_MOD_ASSIGN,
50 OP_LEFT_ASSIGN,
51 OP_RIGHT_ASSIGN,
52 OP_AND_ASSIGN,
53 OP_XOR_ASSIGN,
alokp@chromium.org432d6fc2012-06-27 22:13:21 +000054 OP_OR_ASSIGN,
55
56 // Preprocessing token types.
57 // These types are used by the preprocessor internally.
58 // Preprocessor clients must not depend or check for them.
59 PP_HASH,
60 PP_NUMBER,
61 PP_OTHER
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000062 };
alokp@chromium.org40da4c52012-04-12 05:23:19 +000063 enum Flags
64 {
alokp@chromium.org7fc38dd2012-06-14 18:23:23 +000065 AT_START_OF_LINE = 1 << 0,
66 HAS_LEADING_SPACE = 1 << 1,
67 EXPANSION_DISABLED = 1 << 2
alokp@chromium.org40da4c52012-04-12 05:23:19 +000068 };
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000069
Jamie Madillf832c9d2016-12-12 17:38:48 -050070 Token() : type(0), flags(0) {}
alokp@chromium.org40da4c52012-04-12 05:23:19 +000071
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000072 void reset();
Zhenyao Mod526f982014-05-13 14:51:19 -070073 bool equals(const Token &other) const;
alokp@chromium.org04d7d222012-05-16 19:24:07 +000074
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000075 // Returns true if this is the first token on line.
76 // It disregards any leading whitespace.
Jamie Madillf832c9d2016-12-12 17:38:48 -050077 bool atStartOfLine() const { return (flags & AT_START_OF_LINE) != 0; }
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000078 void setAtStartOfLine(bool start);
alokp@chromium.org40da4c52012-04-12 05:23:19 +000079
Jamie Madillf832c9d2016-12-12 17:38:48 -050080 bool hasLeadingSpace() const { return (flags & HAS_LEADING_SPACE) != 0; }
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000081 void setHasLeadingSpace(bool space);
alokp@chromium.org40da4c52012-04-12 05:23:19 +000082
Jamie Madillf832c9d2016-12-12 17:38:48 -050083 bool expansionDisabled() const { return (flags & EXPANSION_DISABLED) != 0; }
alokp@chromium.org7fc38dd2012-06-14 18:23:23 +000084 void setExpansionDisabled(bool disable);
85
alokp@chromium.org2e818912012-06-29 21:26:03 +000086 // Converts text into numeric value for CONST_INT and CONST_FLOAT token.
87 // Returns false if the parsed value cannot fit into an int or float.
Zhenyao Mod526f982014-05-13 14:51:19 -070088 bool iValue(int *value) const;
89 bool uValue(unsigned int *value) const;
alokp@chromium.org2e818912012-06-29 21:26:03 +000090
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000091 int type;
alokp@chromium.org7fc38dd2012-06-14 18:23:23 +000092 unsigned int flags;
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000093 SourceLocation location;
alokp@chromium.org5b6a68e2012-06-28 20:29:13 +000094 std::string text;
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000095};
96
Zhenyao Mod526f982014-05-13 14:51:19 -070097inline bool operator==(const Token &lhs, const Token &rhs)
alokp@chromium.org98d04ec2012-05-21 22:47:20 +000098{
99 return lhs.equals(rhs);
100}
101
Zhenyao Mod526f982014-05-13 14:51:19 -0700102inline bool operator!=(const Token &lhs, const Token &rhs)
alokp@chromium.org98d04ec2012-05-21 22:47:20 +0000103{
104 return !lhs.equals(rhs);
105}
106
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400107std::ostream &operator<<(std::ostream &out, const Token &token);
alokp@chromium.orgb81c4012011-08-21 06:53:11 +0000108
Jamie Madillc3bef3e2018-10-03 07:35:09 -0400109constexpr char kDefined[] = "defined";
110
Geoff Lang197d5292018-04-25 14:29:00 -0400111} // namespace pp
112
113} // namespace angle
Geoff Lang0a73dd82014-11-19 16:18:08 -0500114
alokp@chromium.org4e4b8072011-08-07 05:36:04 +0000115#endif // COMPILER_PREPROCESSOR_TOKEN_H_