266fc2eb41a0fbbaafc1669ae4fbc9e8d937058b
[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 public:
57   TGParser(SourceMgr &SrcMgr, RecordKeeper &records) : 
58     Lex(SrcMgr), CurMultiClass(0), Records(records) {}
59   
60   /// ParseFile - Main entrypoint for parsing a tblgen file.  These parser
61   /// routines return true on error, or false on success.
62   bool ParseFile();
63   
64   bool Error(SMLoc L, const Twine &Msg) const {
65     PrintError(L, Msg);
66     return true;
67   }
68   bool TokError(const Twine &Msg) const {
69     return Error(Lex.getLoc(), Msg);
70   }
71   const std::vector<std::string> &getDependencies() const {
72     return Lex.getDependencies();
73   }
74 private:  // Semantic analysis methods.
75   bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
76   bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName, 
77                 const std::vector<unsigned> &BitList, Init *V);
78   bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName, 
79                 const std::vector<unsigned> &BitList, Init *V) {
80     return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V);
81   }
82   bool AddSubClass(Record *Rec, SubClassReference &SubClass);
83   bool AddSubMultiClass(MultiClass *CurMC,
84                         SubMultiClassReference &SubMultiClass);
85
86 private:  // Parser methods.
87   bool ParseObjectList(MultiClass *MC = 0);
88   bool ParseObject(MultiClass *MC);
89   bool ParseClass();
90   bool ParseMultiClass();
91   Record *InstantiateMulticlassDef(MultiClass &MC,
92                                    Record *DefProto,
93                                    const std::string &DefmPrefix,
94                                    SMLoc DefmPrefixLoc);
95   bool ResolveMulticlassDefArgs(MultiClass &MC,
96                                 Record *DefProto,
97                                 SMLoc DefmPrefixLoc,
98                                 SMLoc SubClassLoc,
99                                 const std::vector<std::string> &TArgs,
100                                 std::vector<Init *> &TemplateVals,
101                                 bool DeleteArgs);
102   bool ResolveMulticlassDef(MultiClass &MC,
103                             Record *CurRec,
104                             Record *DefProto,
105                             SMLoc DefmPrefixLoc);
106   bool ParseDefm(MultiClass *CurMultiClass);
107   bool ParseDef(MultiClass *CurMultiClass);
108   bool ParseTopLevelLet(MultiClass *CurMultiClass);
109   std::vector<LetRecord> ParseLetList();
110
111   bool ParseObjectBody(Record *CurRec);
112   bool ParseBody(Record *CurRec);
113   bool ParseBodyItem(Record *CurRec);
114
115   bool ParseTemplateArgList(Record *CurRec);
116   std::string ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
117
118   SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
119   SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
120
121   Init *ParseIDValue(Record *CurRec);
122   Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc);
123   Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0);
124   Init *ParseValue(Record *CurRec, RecTy *ItemType = 0);
125   std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0);
126   std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
127   bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
128   bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
129   std::vector<unsigned> ParseRangeList();
130   bool ParseRangePiece(std::vector<unsigned> &Ranges);
131   RecTy *ParseType();
132   Init *ParseOperation(Record *CurRec);
133   RecTy *ParseOperatorType();
134   std::string ParseObjectName();
135   Record *ParseClassID();
136   MultiClass *ParseMultiClassID();
137   Record *ParseDefmID();
138 };
139   
140 } // end namespace llvm
141
142 #endif