1 //===- TGParser.h - Parser for TableGen Files -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class represents the Parser for tablegen files.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/TableGen/Record.h"
19 #include "llvm/TableGen/Error.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/SourceMgr.h"
31 struct SubClassReference;
32 struct SubMultiClassReference;
36 std::vector<unsigned> Bits;
39 LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
41 : Name(N), Bits(B), Value(V), Loc(L) {
47 std::vector<std::vector<LetRecord> > LetStack;
48 std::map<std::string, MultiClass*> MultiClasses;
50 /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
52 MultiClass *CurMultiClass;
55 RecordKeeper &Records;
57 TGParser(SourceMgr &SrcMgr, RecordKeeper &records) :
58 Lex(SrcMgr), CurMultiClass(0), Records(records) {}
60 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
61 /// routines return true on error, or false on success.
64 bool Error(SMLoc L, const Twine &Msg) const {
68 bool TokError(const Twine &Msg) const {
69 return Error(Lex.getLoc(), Msg);
71 const std::vector<std::string> &getDependencies() const {
72 return Lex.getDependencies();
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);
82 bool AddSubClass(Record *Rec, SubClassReference &SubClass);
83 bool AddSubMultiClass(MultiClass *CurMC,
84 SubMultiClassReference &SubMultiClass);
86 private: // Parser methods.
87 bool ParseObjectList(MultiClass *MC = 0);
88 bool ParseObject(MultiClass *MC);
90 bool ParseMultiClass();
91 Record *InstantiateMulticlassDef(MultiClass &MC,
93 const std::string &DefmPrefix,
95 bool ResolveMulticlassDefArgs(MultiClass &MC,
99 const std::vector<std::string> &TArgs,
100 std::vector<Init *> &TemplateVals,
102 bool ResolveMulticlassDef(MultiClass &MC,
105 SMLoc DefmPrefixLoc);
106 bool ParseDefm(MultiClass *CurMultiClass);
107 bool ParseDef(MultiClass *CurMultiClass);
108 bool ParseTopLevelLet(MultiClass *CurMultiClass);
109 std::vector<LetRecord> ParseLetList();
111 bool ParseObjectBody(Record *CurRec);
112 bool ParseBody(Record *CurRec);
113 bool ParseBodyItem(Record *CurRec);
115 bool ParseTemplateArgList(Record *CurRec);
116 std::string ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
118 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
119 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
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);
132 Init *ParseOperation(Record *CurRec);
133 RecTy *ParseOperatorType();
134 std::string ParseObjectName();
135 Record *ParseClassID();
136 MultiClass *ParseMultiClassID();
137 Record *ParseDefmID();
140 } // end namespace llvm