make "locations" a class instead of a typedef.
[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   class SubClassReference;
28   
29   struct LetRecord {
30     std::string Name;
31     std::vector<unsigned> Bits;
32     Init *Value;
33     TGLoc Loc;
34     LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
35               TGLoc L)
36       : Name(N), Bits(B), Value(V), Loc(L) {
37     }
38   };
39   
40 class TGParser {
41   TGLexer Lex;
42   std::vector<std::vector<LetRecord> > LetStack;
43   std::map<std::string, MultiClass*> MultiClasses;
44   
45   /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the 
46   /// current value.
47   MultiClass *CurMultiClass;
48 public:
49   
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
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
91   Init *ParseIDValue(Record *CurRec);
92   Init *ParseIDValue(Record *CurRec, const std::string &Name, TGLoc NameLoc);
93   Init *ParseSimpleValue(Record *CurRec);
94   Init *ParseValue(Record *CurRec);
95   std::vector<Init*> ParseValueList(Record *CurRec);
96   std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
97   bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
98   bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
99   std::vector<unsigned> ParseRangeList();
100   bool ParseRangePiece(std::vector<unsigned> &Ranges);
101   RecTy *ParseType();
102   std::string ParseObjectName();
103   Record *ParseClassID();
104   Record *ParseDefmID();
105 };
106   
107 } // end namespace llvm
108
109 #endif