Fixed a nasty layering violation in the edis source
[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/Support/SourceMgr.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     SMLoc Loc;
35     LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
36               SMLoc 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(SourceMgr &SrcMgr) : Lex(SrcMgr), CurMultiClass(0) {}
51   
52   /// ParseFile - Main entrypoint for parsing a tblgen file.  These parser
53   /// routines return true on error, or false on success.
54   bool ParseFile();
55   
56   bool Error(SMLoc L, const std::string &Msg) const {
57     Lex.PrintError(L, Msg);
58     return true;
59   }
60   bool TokError(const std::string &Msg) const {
61     return Error(Lex.getLoc(), Msg);
62   }
63 private:  // Semantic analysis methods.
64   bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
65   bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName, 
66                 const std::vector<unsigned> &BitList, Init *V);
67   bool AddSubClass(Record *Rec, SubClassReference &SubClass);
68   bool AddSubMultiClass(MultiClass *CurMC,
69                         SubMultiClassReference &SubMultiClass);
70
71 private:  // Parser methods.
72   bool ParseObjectList();
73   bool ParseObject();
74   bool ParseClass();
75   bool ParseMultiClass();
76   bool ParseMultiClassDef(MultiClass *CurMC);
77   bool ParseDefm();
78   bool ParseTopLevelLet();
79   std::vector<LetRecord> ParseLetList();
80
81   Record *ParseDef(MultiClass *CurMultiClass);
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