rename TGLoc -> SMLoc.
[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(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(SMLoc 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, SMLoc Loc, const RecordVal &RV);
67   bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName, 
68                 const std::vector<unsigned> &BitList, Init *V);
69   bool AddSubClass(Record *Rec, SubClassReference &SubClass);
70   bool AddSubMultiClass(MultiClass *CurMC,
71                         SubMultiClassReference &SubMultiClass);
72
73 private:  // Parser methods.
74   bool ParseObjectList();
75   bool ParseObject();
76   bool ParseClass();
77   bool ParseMultiClass();
78   bool ParseMultiClassDef(MultiClass *CurMC);
79   bool ParseDefm();
80   bool ParseTopLevelLet();
81   std::vector<LetRecord> ParseLetList();
82
83   Record *ParseDef(MultiClass *CurMultiClass);
84   bool ParseObjectBody(Record *CurRec);
85   bool ParseBody(Record *CurRec);
86   bool ParseBodyItem(Record *CurRec);
87
88   bool ParseTemplateArgList(Record *CurRec);
89   std::string ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
90
91   SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
92   SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
93
94   Init *ParseIDValue(Record *CurRec);
95   Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc);
96   Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0);
97   Init *ParseValue(Record *CurRec, RecTy *ItemType = 0);
98   std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0);
99   std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
100   bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
101   bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
102   std::vector<unsigned> ParseRangeList();
103   bool ParseRangePiece(std::vector<unsigned> &Ranges);
104   RecTy *ParseType();
105   Init *ParseOperation(Record *CurRec);
106   RecTy *ParseOperatorType();
107   std::string ParseObjectName();
108   Record *ParseClassID();
109   MultiClass *ParseMultiClassID();
110   Record *ParseDefmID();
111 };
112   
113 } // end namespace llvm
114
115 #endif