2979d0e189f04b87fb1ca2faf792d4017be6a3cb
[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 "TGSourceMgr.h"
19 #include <map>
20
21 namespace llvm {
22   class Record;
23   class RecordVal;
24   struct RecTy;
25   struct Init;
26   struct MultiClass;
27   struct SubClassReference;
28   struct SubMultiClassReference;
29   
30   struct LetRecord {
31     std::string Name;
32     std::vector<unsigned> Bits;
33     Init *Value;
34     TGLoc Loc;
35     LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
36               TGLoc L)
37       : Name(N), Bits(B), Value(V), Loc(L) {
38     }
39   };
40   
41 class TGParser {
42   TGLexer Lex;
43   std::vector<std::vector<LetRecord> > LetStack;
44   std::map<std::string, MultiClass*> MultiClasses;
45   
46   /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the 
47   /// current value.
48   MultiClass *CurMultiClass;
49 public:
50   TGParser(TGSourceMgr &SrcMgr) : Lex(SrcMgr), CurMultiClass(0) {}
51   
52   void setIncludeDirs(const std::vector<std::string> &D){Lex.setIncludeDirs(D);}
53
54   /// ParseFile - Main entrypoint for parsing a tblgen file.  These parser
55   /// routines return true on error, or false on success.
56   bool ParseFile();
57   
58   bool Error(TGLoc L, const std::string &Msg) const {
59     Lex.PrintError(L, Msg);
60     return true;
61   }
62   bool TokError(const std::string &Msg) const {
63     return Error(Lex.getLoc(), Msg);
64   }
65 private:  // Semantic analysis methods.
66   bool AddValue(Record *TheRec, TGLoc Loc, const RecordVal &RV);
67   bool SetValue(Record *TheRec, TGLoc Loc, const std::string &ValName, 
68                 const std::vector<unsigned> &BitList, Init *V);
69   bool AddSubClass(Record *Rec, SubClassReference &SubClass);
70   bool AddSubMultiClass(MultiClass *MV, class SubMultiClassReference &SubMultiClass);
71
72 private:  // Parser methods.
73   bool ParseObjectList();
74   bool ParseObject();
75   bool ParseClass();
76   bool ParseMultiClass();
77   bool ParseMultiClassDef(MultiClass *CurMC);
78   bool ParseDefm();
79   bool ParseTopLevelLet();
80   std::vector<LetRecord> ParseLetList();
81
82   Record *ParseDef(MultiClass *CurMultiClass);
83   bool ParseObjectBody(Record *CurRec);
84   bool ParseBody(Record *CurRec);
85   bool ParseBodyItem(Record *CurRec);
86
87   bool ParseTemplateArgList(Record *CurRec);
88   std::string ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
89
90   SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
91   SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMultiClass);
92
93   Init *ParseIDValue(Record *CurRec);
94   Init *ParseIDValue(Record *CurRec, const std::string &Name, TGLoc NameLoc);
95   Init *ParseSimpleValue(Record *CurRec);
96   Init *ParseValue(Record *CurRec);
97   std::vector<Init*> ParseValueList(Record *CurRec);
98   std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
99   bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
100   bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
101   std::vector<unsigned> ParseRangeList();
102   bool ParseRangePiece(std::vector<unsigned> &Ranges);
103   RecTy *ParseType();
104   std::string ParseObjectName();
105   Record *ParseClassID();
106   MultiClass *ParseMultiClassID();
107   Record *ParseDefmID();
108 };
109   
110 } // end namespace llvm
111
112 #endif