trailing whitespace
[oota-llvm.git] / utils / TableGen / TGParser.h
1 //===- TGParser.h - Parser for TableGen Files -------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class represents the Parser for tablegen files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef TGPARSER_H
15 #define TGPARSER_H
16
17 #include "TGLexer.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include <map>
21
22 namespace llvm {
23   class Record;
24   class RecordVal;
25   struct RecTy;
26   struct Init;
27   struct MultiClass;
28   struct SubClassReference;
29   struct SubMultiClassReference;
30   
31   struct LetRecord {
32     std::string Name;
33     std::vector<unsigned> Bits;
34     Init *Value;
35     SMLoc Loc;
36     LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
37               SMLoc L)
38       : Name(N), Bits(B), Value(V), Loc(L) {
39     }
40   };
41   
42 class TGParser {
43   TGLexer Lex;
44   std::vector<std::vector<LetRecord> > LetStack;
45   std::map<std::string, MultiClass*> MultiClasses;
46   
47   /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the 
48   /// current value.
49   MultiClass *CurMultiClass;
50 public:
51   TGParser(SourceMgr &SrcMgr) : Lex(SrcMgr), CurMultiClass(0) {}
52   
53   /// ParseFile - Main entrypoint for parsing a tblgen file.  These parser
54   /// routines return true on error, or false on success.
55   bool ParseFile();
56   
57   bool Error(SMLoc L, const Twine &Msg) const {
58     Lex.PrintError(L, Msg);
59     return true;
60   }
61   bool TokError(const Twine &Msg) const {
62     return Error(Lex.getLoc(), Msg);
63   }
64 private:  // Semantic analysis methods.
65   bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
66   bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName, 
67                 const std::vector<unsigned> &BitList, Init *V);
68   bool AddSubClass(Record *Rec, SubClassReference &SubClass);
69   bool AddSubMultiClass(MultiClass *CurMC,
70                         SubMultiClassReference &SubMultiClass);
71
72 private:  // Parser methods.
73   bool ParseObjectList(MultiClass *MC = 0);
74   bool ParseObject(MultiClass *MC);
75   bool ParseClass();
76   bool ParseMultiClass();
77   bool ParseDefm(MultiClass *CurMultiClass);
78   bool ParseDef(MultiClass *CurMultiClass);
79   bool ParseTopLevelLet(MultiClass *CurMultiClass);
80   std::vector<LetRecord> ParseLetList();
81
82   bool ParseObjectBody(Record *CurRec);
83   bool ParseBody(Record *CurRec);
84   bool ParseBodyItem(Record *CurRec);
85
86   bool ParseTemplateArgList(Record *CurRec);
87   std::string ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
88
89   SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
90   SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
91
92   Init *ParseIDValue(Record *CurRec);
93   Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc);
94   Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0);
95   Init *ParseValue(Record *CurRec, RecTy *ItemType = 0);
96   std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0);
97   std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
98   bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
99   bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
100   std::vector<unsigned> ParseRangeList();
101   bool ParseRangePiece(std::vector<unsigned> &Ranges);
102   RecTy *ParseType();
103   Init *ParseOperation(Record *CurRec);
104   RecTy *ParseOperatorType();
105   std::string ParseObjectName();
106   Record *ParseClassID();
107   MultiClass *ParseMultiClassID();
108   Record *ParseDefmID();
109 };
110   
111 } // end namespace llvm
112
113 #endif