blob: 2c80743e3a6840068a6636ecb6e9f09ea520ef44 [file] [log] [blame]
Chris Lattnerda4ab672007-11-18 02:57:27 +00001//===- TGLexer.h - Lexer for TableGen Files ---------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda4ab672007-11-18 02:57:27 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This class represents the Lexer for tablegen files.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_TABLEGEN_TGLEXER_H
15#define LLVM_LIB_TABLEGEN_TGLEXER_H
Chris Lattnerda4ab672007-11-18 02:57:27 +000016
Rafael Espindolaa3c65092014-07-06 14:24:03 +000017#include "llvm/ADT/StringRef.h"
Michael J. Spencerab425d82010-11-29 18:47:54 +000018#include "llvm/Support/DataTypes.h"
Sean Silva3b964242013-02-07 04:30:39 +000019#include "llvm/Support/SMLoc.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000020#include <cassert>
Sean Silva3b964242013-02-07 04:30:39 +000021#include <map>
Chris Lattnerda4ab672007-11-18 02:57:27 +000022#include <string>
Chris Lattnerda4ab672007-11-18 02:57:27 +000023
24namespace llvm {
Chris Lattnerfd255752009-06-21 03:41:50 +000025class SourceMgr;
Chris Lattner526c8cb2009-06-21 03:39:35 +000026class SMLoc;
Benjamin Kramerc7583112010-09-27 17:42:11 +000027class Twine;
28
Chris Lattnerf4127dd2007-11-22 20:49:04 +000029namespace tgtok {
30 enum TokKind {
31 // Markers
32 Eof, Error,
Nicolai Haehnle169ec092018-03-09 18:32:04 +000033
Chris Lattnerf4127dd2007-11-22 20:49:04 +000034 // Tokens with no info.
35 minus, plus, // - +
36 l_square, r_square, // [ ]
37 l_brace, r_brace, // { }
38 l_paren, r_paren, // ( )
39 less, greater, // < >
Francois Pichet0fc06ee2011-03-14 02:30:32 +000040 colon, semi, // : ;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000041 comma, period, // , .
42 equal, question, // = ?
David Greene8e85b482011-10-19 13:04:43 +000043 paste, // #
44
Chris Lattnerf4127dd2007-11-22 20:49:04 +000045 // Keywords.
David Greenefb927af2012-02-22 16:09:41 +000046 Bit, Bits, Class, Code, Dag, Def, Foreach, Defm, Field, In, Int, Let, List,
Nicolai Haehnlefcd65252018-03-09 12:24:42 +000047 MultiClass, String, Defset,
Matt Arsenault1c8d9332016-11-15 06:49:28 +000048
Chris Lattnerf4127dd2007-11-22 20:49:04 +000049 // !keywords.
Matt Arsenault1c8d9332016-11-15 06:49:28 +000050 XConcat, XADD, XAND, XOR, XSRA, XSRL, XSHL, XListConcat, XStrConcat, XCast,
Nicolai Haehnle6c118652018-03-14 11:00:26 +000051 XSubst, XForEach, XFoldl, XHead, XTail, XSize, XEmpty, XIf, XEq, XIsA, XDag,
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +000052 XNe, XLe, XLt, XGe, XGt,
David Greenea9c6c5d2009-04-22 20:18:10 +000053
Chris Lattnerf4127dd2007-11-22 20:49:04 +000054 // Integer value.
55 IntVal,
Pete Cooper25977642014-08-07 05:47:00 +000056
57 // Binary constant. Note that these are sized according to the number of
58 // bits given.
59 BinaryIntVal,
Nicolai Haehnle169ec092018-03-09 18:32:04 +000060
Chris Lattnerf4127dd2007-11-22 20:49:04 +000061 // String valued tokens.
62 Id, StrVal, VarName, CodeFragment
63 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000064}
Chris Lattnerda4ab672007-11-18 02:57:27 +000065
Chris Lattnerf4127dd2007-11-22 20:49:04 +000066/// TGLexer - TableGen Lexer class.
Chris Lattnerda4ab672007-11-18 02:57:27 +000067class TGLexer {
Chris Lattnerfd255752009-06-21 03:41:50 +000068 SourceMgr &SrcMgr;
Nicolai Haehnle169ec092018-03-09 18:32:04 +000069
Chris Lattnerda4ab672007-11-18 02:57:27 +000070 const char *CurPtr;
Rafael Espindolaa3c65092014-07-06 14:24:03 +000071 StringRef CurBuf;
Chris Lattnerda4ab672007-11-18 02:57:27 +000072
Chris Lattnerf4127dd2007-11-22 20:49:04 +000073 // Information about the current token.
74 const char *TokStart;
75 tgtok::TokKind CurCode;
76 std::string CurStrVal; // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT
Dan Gohmanca0546f2008-10-17 01:33:43 +000077 int64_t CurIntVal; // This is valid for INTVAL.
Chris Lattner8db9bc72009-03-13 07:05:43 +000078
79 /// CurBuffer - This is the current buffer index we're lexing from as managed
80 /// by the SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +000081 unsigned CurBuffer;
Sean Silva3b964242013-02-07 04:30:39 +000082
83public:
84 typedef std::map<std::string, SMLoc> DependenciesMapTy;
85private:
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +000086 /// Dependencies - This is the list of all included files.
Sean Silva3b964242013-02-07 04:30:39 +000087 DependenciesMapTy Dependencies;
88
Chris Lattnerda4ab672007-11-18 02:57:27 +000089public:
Chris Lattnerfd255752009-06-21 03:41:50 +000090 TGLexer(SourceMgr &SrcMgr);
Benjamin Kramerdd0ff852015-04-11 15:32:26 +000091
Chris Lattnerf4127dd2007-11-22 20:49:04 +000092 tgtok::TokKind Lex() {
93 return CurCode = LexToken();
94 }
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +000095
Sean Silva3b964242013-02-07 04:30:39 +000096 const DependenciesMapTy &getDependencies() const {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +000097 return Dependencies;
98 }
Nicolai Haehnle169ec092018-03-09 18:32:04 +000099
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000100 tgtok::TokKind getCode() const { return CurCode; }
101
102 const std::string &getCurStrVal() const {
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000103 assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal ||
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000104 CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
105 "This token doesn't have a string value");
106 return CurStrVal;
107 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000108 int64_t getCurIntVal() const {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000109 assert(CurCode == tgtok::IntVal && "This token isn't an integer");
110 return CurIntVal;
111 }
Pete Cooper25977642014-08-07 05:47:00 +0000112 std::pair<int64_t, unsigned> getCurBinaryIntVal() const {
113 assert(CurCode == tgtok::BinaryIntVal &&
114 "This token isn't a binary integer");
115 return std::make_pair(CurIntVal, (CurPtr - TokStart)-2);
116 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000117
Chris Lattner526c8cb2009-06-21 03:39:35 +0000118 SMLoc getLoc() const;
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000119
Chris Lattnerda4ab672007-11-18 02:57:27 +0000120private:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000121 /// LexToken - Read the next token and return its code.
122 tgtok::TokKind LexToken();
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000123
Benjamin Kramerc7583112010-09-27 17:42:11 +0000124 tgtok::TokKind ReturnError(const char *Loc, const Twine &Msg);
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000125
Chris Lattnerda4ab672007-11-18 02:57:27 +0000126 int getNextChar();
David Greene9ba42082011-10-19 13:03:35 +0000127 int peekNextChar(int Index);
Chris Lattnerda4ab672007-11-18 02:57:27 +0000128 void SkipBCPLComment();
129 bool SkipCComment();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000130 tgtok::TokKind LexIdentifier();
Chris Lattnerda4ab672007-11-18 02:57:27 +0000131 bool LexInclude();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000132 tgtok::TokKind LexString();
133 tgtok::TokKind LexVarName();
134 tgtok::TokKind LexNumber();
135 tgtok::TokKind LexBracket();
136 tgtok::TokKind LexExclaim();
Chris Lattnerda4ab672007-11-18 02:57:27 +0000137};
Nicolai Haehnle169ec092018-03-09 18:32:04 +0000138
Chris Lattnerda4ab672007-11-18 02:57:27 +0000139} // end namespace llvm
140
141#endif