* Fix typeo
[oota-llvm.git] / lib / AsmParser / ParserInternals.h
1 //===-- ParserInternals.h - Definitions internal to the parser ---*- C++ -*--=//
2 //
3 //  This header file defines the various variables that are shared among the 
4 //  different components of the parser...
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef PARSER_INTERNALS_H
9 #define PARSER_INTERNALS_H
10
11 #define __STDC_LIMIT_MACROS
12 #include "llvm/BasicBlock.h"
13 #include "llvm/Constants.h"
14 #include "llvm/iOther.h"
15 #include "llvm/Function.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Assembly/Parser.h"
18 #include "Support/StringExtras.h"
19
20 class Module;
21
22 // Global variables exported from the lexer...
23 extern std::FILE *llvmAsmin;
24 extern int llvmAsmlineno;
25
26 // Globals exported by the parser...
27 extern std::string CurFilename;
28 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
29
30
31 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
32 // appropriate character.  If AllowNull is set to false, a \00 value will cause
33 // an exception to be thrown.
34 //
35 // If AllowNull is set to true, the return value of the function points to the
36 // last character of the string in memory.
37 //
38 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
39
40
41 // ThrowException - Wrapper around the ParseException class that automatically
42 // fills in file line number and column number and options info.
43 //
44 // This also helps me because I keep typing 'throw new ParseException' instead 
45 // of just 'throw ParseException'... sigh...
46 //
47 static inline void ThrowException(const std::string &message,
48                                   int LineNo = -1) {
49   if (LineNo == -1) LineNo = llvmAsmlineno;
50   // TODO: column number in exception
51   throw ParseException(CurFilename, message, LineNo);
52 }
53
54 // ValID - Represents a reference of a definition of some sort.  This may either
55 // be a numeric reference or a symbolic (%var) reference.  This is just a 
56 // discriminated union.
57 //
58 // Note that I can't implement this class in a straight forward manner with 
59 // constructors and stuff because it goes in a union, and GCC doesn't like 
60 // putting classes with ctor's in unions.  :(
61 //
62 struct ValID {
63   enum {
64     NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal
65   } Type;
66
67   union {
68     int      Num;         // If it's a numeric reference
69     char    *Name;        // If it's a named reference.  Memory must be free'd.
70     int64_t  ConstPool64; // Constant pool reference.  This is the value
71     uint64_t UConstPool64;// Unsigned constant pool reference.
72     double   ConstPoolFP; // Floating point constant pool reference
73   };
74
75   static ValID create(int Num) {
76     ValID D; D.Type = NumberVal; D.Num = Num; return D;
77   }
78
79   static ValID create(char *Name) {
80     ValID D; D.Type = NameVal; D.Name = Name; return D;
81   }
82
83   static ValID create(int64_t Val) {
84     ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
85   }
86
87   static ValID create(uint64_t Val) {
88     ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
89   }
90
91   static ValID create(double Val) {
92     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
93   }
94
95   static ValID createNull() {
96     ValID D; D.Type = ConstNullVal; return D;
97   }
98
99   inline void destroy() const {
100     if (Type == NameVal)
101       free(Name);    // Free this strdup'd memory...
102   }
103
104   inline ValID copy() const {
105     if (Type != NameVal) return *this;
106     ValID Result = *this;
107     Result.Name = strdup(Name);
108     return Result;
109   }
110
111   inline std::string getName() const {
112     switch (Type) {
113     case NumberVal     : return std::string("#") + itostr(Num);
114     case NameVal       : return Name;
115     case ConstFPVal    : return ftostr(ConstPoolFP);
116     case ConstNullVal  : return "null";
117     case ConstUIntVal  :
118     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
119     default:
120       assert(0 && "Unknown value!");
121       abort();
122       return "";
123     }
124   }
125
126   bool operator<(const ValID &V) const {
127     if (Type != V.Type) return Type < V.Type;
128     switch (Type) {
129     case NumberVal:     return Num < V.Num;
130     case NameVal:       return strcmp(Name, V.Name) < 0;
131     case ConstSIntVal:  return ConstPool64  < V.ConstPool64;
132     case ConstUIntVal:  return UConstPool64 < V.UConstPool64;
133     case ConstFPVal:    return ConstPoolFP  < V.ConstPoolFP;
134     case ConstNullVal:  return false;
135     default:  assert(0 && "Unknown value type!"); return false;
136     }
137   }
138 };
139
140
141
142 template<class SuperType>
143 class PlaceholderValue : public SuperType {
144   ValID D;
145   int LineNum;
146 public:
147   PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
148     LineNum = llvmAsmlineno;
149   }
150   ValID &getDef() { return D; }
151   int getLineNum() const { return LineNum; }
152 };
153
154 struct InstPlaceHolderHelper : public Instruction {
155   InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
156
157   virtual Instruction *clone() const { abort(); return 0; }
158   virtual const char *getOpcodeName() const { return "placeholder"; }
159 };
160
161 struct BBPlaceHolderHelper : public BasicBlock {
162   BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
163     assert(Ty == Type::LabelTy);
164   }
165 };
166
167 struct MethPlaceHolderHelper : public Function {
168   MethPlaceHolderHelper(const Type *Ty)
169     : Function(cast<FunctionType>(Ty), true) {}
170 };
171
172 typedef PlaceholderValue<InstPlaceHolderHelper>  ValuePlaceHolder;
173 typedef PlaceholderValue<BBPlaceHolderHelper>    BBPlaceHolder;
174
175 static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
176   const Type *Ty = Val->getType();
177   if (isa<PointerType>(Ty) &&
178       isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
179     Ty = cast<PointerType>(Ty)->getElementType();
180
181   switch (Ty->getPrimitiveID()) {
182   case Type::LabelTyID:  return ((BBPlaceHolder*)Val)->getDef();
183   default:               return ((ValuePlaceHolder*)Val)->getDef();
184   }
185 }
186
187 static inline int getLineNumFromPlaceHolder(const Value *Val) {
188   const Type *Ty = Val->getType();
189   if (isa<PointerType>(Ty) &&
190       isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
191     Ty = cast<PointerType>(Ty)->getElementType();
192
193   switch (Ty->getPrimitiveID()) {
194   case Type::LabelTyID:  return ((BBPlaceHolder*)Val)->getLineNum();
195   default:               return ((ValuePlaceHolder*)Val)->getLineNum();
196   }
197 }
198
199 #endif