TGParser.h revision 226584
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/TableGen/Error.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/Support/SourceMgr.h"
21#include <map>
22
23namespace llvm {
24  class Record;
25  class RecordVal;
26  class RecordKeeper;
27  class RecTy;
28  class Init;
29  struct MultiClass;
30  struct SubClassReference;
31  struct SubMultiClassReference;
32
33  struct LetRecord {
34    std::string Name;
35    std::vector<unsigned> Bits;
36    Init *Value;
37    SMLoc Loc;
38    LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
39              SMLoc L)
40      : Name(N), Bits(B), Value(V), Loc(L) {
41    }
42  };
43
44class TGParser {
45  TGLexer Lex;
46  std::vector<std::vector<LetRecord> > LetStack;
47  std::map<std::string, MultiClass*> MultiClasses;
48
49  /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
50  /// current value.
51  MultiClass *CurMultiClass;
52
53  // Record tracker
54  RecordKeeper &Records;
55public:
56  TGParser(SourceMgr &SrcMgr, RecordKeeper &records) :
57    Lex(SrcMgr), CurMultiClass(0), Records(records) {}
58
59  /// ParseFile - Main entrypoint for parsing a tblgen file.  These parser
60  /// routines return true on error, or false on success.
61  bool ParseFile();
62
63  bool Error(SMLoc L, const Twine &Msg) const {
64    PrintError(L, Msg);
65    return true;
66  }
67  bool TokError(const Twine &Msg) const {
68    return Error(Lex.getLoc(), Msg);
69  }
70  const std::vector<std::string> &getDependencies() const {
71    return Lex.getDependencies();
72  }
73private:  // Semantic analysis methods.
74  bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
75  bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
76                const std::vector<unsigned> &BitList, Init *V);
77  bool AddSubClass(Record *Rec, SubClassReference &SubClass);
78  bool AddSubMultiClass(MultiClass *CurMC,
79                        SubMultiClassReference &SubMultiClass);
80
81private:  // Parser methods.
82  bool ParseObjectList(MultiClass *MC = 0);
83  bool ParseObject(MultiClass *MC);
84  bool ParseClass();
85  bool ParseMultiClass();
86  Record *InstantiateMulticlassDef(MultiClass &MC,
87                                   Record *DefProto,
88                                   const std::string &DefmPrefix,
89                                   SMLoc DefmPrefixLoc);
90  bool ResolveMulticlassDefArgs(MultiClass &MC,
91                                Record *DefProto,
92                                SMLoc DefmPrefixLoc,
93                                SMLoc SubClassLoc,
94                                const std::vector<std::string> &TArgs,
95                                std::vector<Init *> &TemplateVals,
96                                bool DeleteArgs);
97  bool ResolveMulticlassDef(MultiClass &MC,
98                            Record *CurRec,
99                            Record *DefProto,
100                            SMLoc DefmPrefixLoc);
101  bool ParseDefm(MultiClass *CurMultiClass);
102  bool ParseDef(MultiClass *CurMultiClass);
103  bool ParseTopLevelLet(MultiClass *CurMultiClass);
104  std::vector<LetRecord> ParseLetList();
105
106  bool ParseObjectBody(Record *CurRec);
107  bool ParseBody(Record *CurRec);
108  bool ParseBodyItem(Record *CurRec);
109
110  bool ParseTemplateArgList(Record *CurRec);
111  std::string ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
112
113  SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
114  SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
115
116  Init *ParseIDValue(Record *CurRec);
117  Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc);
118  Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0);
119  Init *ParseValue(Record *CurRec, RecTy *ItemType = 0);
120  std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0);
121  std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
122  bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
123  bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
124  std::vector<unsigned> ParseRangeList();
125  bool ParseRangePiece(std::vector<unsigned> &Ranges);
126  RecTy *ParseType();
127  Init *ParseOperation(Record *CurRec);
128  RecTy *ParseOperatorType();
129  std::string ParseObjectName();
130  Record *ParseClassID();
131  MultiClass *ParseMultiClassID();
132  Record *ParseDefmID();
133};
134
135} // end namespace llvm
136
137#endif
138