Parse Def ID as Value
[oota-llvm.git] / lib / 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 "llvm/TableGen/Record.h"
18 #include "TGLexer.h"
19 #include "llvm/TableGen/Error.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include <map>
23
24 namespace llvm {
25   class Record;
26   class RecordVal;
27   class RecordKeeper;
28   class RecTy;
29   class Init;
30   struct MultiClass;
31   struct SubClassReference;
32   struct SubMultiClassReference;
33   
34   struct LetRecord {
35     std::string Name;
36     std::vector<unsigned> Bits;
37     Init *Value;
38     SMLoc Loc;
39     LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
40               SMLoc L)
41       : Name(N), Bits(B), Value(V), Loc(L) {
42     }
43   };
44   
45 class TGParser {
46   TGLexer Lex;
47   std::vector<std::vector<LetRecord> > LetStack;
48   std::map<std::string, MultiClass*> MultiClasses;
49   
50   /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the 
51   /// current value.
52   MultiClass *CurMultiClass;
53
54   // Record tracker
55   RecordKeeper &Records;
56
57   // A "named boolean" indicating how to parse identifiers.  Usually
58   // identifiers map to some existing object but in special cases
59   // (e.g. parsing def names) no such object exists yet because we are
60   // in the middle of creating in.  For those situations, allow the
61   // parser to ignore missing object errors.
62   enum IDParseMode {
63     ParseValueMode, // We are parsing a value we expect to look up.
64     ParseNameMode // We are parsing a name of an object that does not yet exist.
65   };
66
67 public:
68   TGParser(SourceMgr &SrcMgr, RecordKeeper &records) : 
69     Lex(SrcMgr), CurMultiClass(0), Records(records) {}
70   
71   /// ParseFile - Main entrypoint for parsing a tblgen file.  These parser
72   /// routines return true on error, or false on success.
73   bool ParseFile();
74   
75   bool Error(SMLoc L, const Twine &Msg) const {
76     PrintError(L, Msg);
77     return true;
78   }
79   bool TokError(const Twine &Msg) const {
80     return Error(Lex.getLoc(), Msg);
81   }
82   const std::vector<std::string> &getDependencies() const {
83     return Lex.getDependencies();
84   }
85 private:  // Semantic analysis methods.
86   bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
87   bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName, 
88                 const std::vector<unsigned> &BitList, Init *V);
89   bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName, 
90                 const std::vector<unsigned> &BitList, Init *V) {
91     return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V);
92   }
93   bool AddSubClass(Record *Rec, SubClassReference &SubClass);
94   bool AddSubMultiClass(MultiClass *CurMC,
95                         SubMultiClassReference &SubMultiClass);
96
97 private:  // Parser methods.
98   bool ParseObjectList(MultiClass *MC = 0);
99   bool ParseObject(MultiClass *MC);
100   bool ParseClass();
101   bool ParseMultiClass();
102   Record *InstantiateMulticlassDef(MultiClass &MC,
103                                    Record *DefProto,
104                                    const std::string &DefmPrefix,
105                                    SMLoc DefmPrefixLoc);
106   bool ResolveMulticlassDefArgs(MultiClass &MC,
107                                 Record *DefProto,
108                                 SMLoc DefmPrefixLoc,
109                                 SMLoc SubClassLoc,
110                                 const std::vector<Init *> &TArgs,
111                                 std::vector<Init *> &TemplateVals,
112                                 bool DeleteArgs);
113   bool ResolveMulticlassDef(MultiClass &MC,
114                             Record *CurRec,
115                             Record *DefProto,
116                             SMLoc DefmPrefixLoc);
117   bool ParseDefm(MultiClass *CurMultiClass);
118   bool ParseDef(MultiClass *CurMultiClass);
119   bool ParseTopLevelLet(MultiClass *CurMultiClass);
120   std::vector<LetRecord> ParseLetList();
121
122   bool ParseObjectBody(Record *CurRec);
123   bool ParseBody(Record *CurRec);
124   bool ParseBodyItem(Record *CurRec);
125
126   bool ParseTemplateArgList(Record *CurRec);
127   Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
128
129   SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
130   SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
131
132   Init *ParseIDValue(Record *CurRec, IDParseMode Mode = ParseValueMode);
133   Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc,
134                      IDParseMode Mode = ParseValueMode);
135   Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0,
136                          IDParseMode Mode = ParseValueMode);
137   Init *ParseValue(Record *CurRec, RecTy *ItemType = 0,
138                    IDParseMode Mode = ParseValueMode);
139   std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0);
140   std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
141   bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
142   bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
143   std::vector<unsigned> ParseRangeList();
144   bool ParseRangePiece(std::vector<unsigned> &Ranges);
145   RecTy *ParseType();
146   Init *ParseOperation(Record *CurRec);
147   RecTy *ParseOperatorType();
148   Init *ParseObjectName(MultiClass *CurMultiClass);
149   Record *ParseClassID();
150   MultiClass *ParseMultiClassID();
151   Record *ParseDefmID();
152 };
153   
154 } // end namespace llvm
155
156 #endif