Fix this tool for use on Darwin which requires the file to come after the
[oota-llvm.git] / lib / AsmParser / ParserInternals.h
1 //===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This header file defines the various variables that are shared among the
11 //  different components of the parser...
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef PARSER_INTERNALS_H
16 #define PARSER_INTERNALS_H
17
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Function.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Assembly/Parser.h"
23 #include "llvm/ADT/StringExtras.h"
24
25
26 // Global variables exported from the lexer...
27
28 extern int llvmAsmlineno;         /// FIXME: Not threading friendly
29 extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
30
31 extern std::string &llvmAsmTextin;
32
33 // functions exported from the lexer
34 void set_scan_file(FILE * F);
35 void set_scan_string (const char * str);
36
37 // Globals exported by the parser...
38 extern char* llvmAsmtext;
39 extern int   llvmAsmleng;
40
41 namespace llvm {
42
43 // Globals exported by the parser...
44 extern std::string CurFilename;   /// FIXME: Not threading friendly
45
46 class Module;
47 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
48
49 // Parse a string directly
50 Module *RunVMAsmParser(const char * AsmString, Module * M);
51
52
53 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
54 // appropriate character.  If AllowNull is set to false, a \00 value will cause
55 // an error.
56 //
57 // If AllowNull is set to true, the return value of the function points to the
58 // last character of the string in memory.
59 //
60 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
61
62
63 // ThrowException - Wrapper around the ParseException class that automatically
64 // fills in file line number and column number and options info.
65 //
66 // This also helps me because I keep typing 'throw new ParseException' instead
67 // of just 'throw ParseException'... sigh...
68 //
69 extern void GenerateError(const std::string &message, int LineNo = -1);
70
71 /// InlineAsmDescriptor - This is a simple class that holds info about inline
72 /// asm blocks, for use by ValID.
73 struct InlineAsmDescriptor {
74   std::string AsmString, Constraints;
75   bool HasSideEffects;
76   
77   InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
78     : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
79 };
80
81
82 // ValID - Represents a reference of a definition of some sort.  This may either
83 // be a numeric reference or a symbolic (%var) reference.  This is just a
84 // discriminated union.
85 //
86 // Note that I can't implement this class in a straight forward manner with
87 // constructors and stuff because it goes in a union.
88 //
89 struct ValID {
90   enum {
91     NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
92     ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
93   } Type;
94
95   union {
96     int      Num;         // If it's a numeric reference
97     char    *Name;        // If it's a named reference.  Memory must be free'd.
98     int64_t  ConstPool64; // Constant pool reference.  This is the value
99     uint64_t UConstPool64;// Unsigned constant pool reference.
100     double   ConstPoolFP; // Floating point constant pool reference
101     Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
102     InlineAsmDescriptor *IAD;
103   };
104
105   static ValID create(int Num) {
106     ValID D; D.Type = NumberVal; D.Num = Num; return D;
107   }
108
109   static ValID create(char *Name) {
110     ValID D; D.Type = NameVal; D.Name = Name; return D;
111   }
112
113   static ValID create(int64_t Val) {
114     ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
115   }
116
117   static ValID create(uint64_t Val) {
118     ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
119   }
120
121   static ValID create(double Val) {
122     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
123   }
124
125   static ValID createNull() {
126     ValID D; D.Type = ConstNullVal; return D;
127   }
128
129   static ValID createUndef() {
130     ValID D; D.Type = ConstUndefVal; return D;
131   }
132
133   static ValID createZeroInit() {
134     ValID D; D.Type = ConstZeroVal; return D;
135   }
136   
137   static ValID create(Constant *Val) {
138     ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
139   }
140   
141   static ValID createInlineAsm(const std::string &AsmString,
142                                const std::string &Constraints,
143                                bool HasSideEffects) {
144     ValID D;
145     D.Type = InlineAsmVal;
146     D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
147     return D;
148   }
149
150   inline void destroy() const {
151     if (Type == NameVal)
152       free(Name);    // Free this strdup'd memory.
153     else if (Type == InlineAsmVal)
154       delete IAD;
155   }
156
157   inline ValID copy() const {
158     if (Type != NameVal) return *this;
159     ValID Result = *this;
160     Result.Name = strdup(Name);
161     return Result;
162   }
163
164   inline std::string getName() const {
165     switch (Type) {
166     case NumberVal     : return std::string("#") + itostr(Num);
167     case NameVal       : return Name;
168     case ConstFPVal    : return ftostr(ConstPoolFP);
169     case ConstNullVal  : return "null";
170     case ConstUndefVal : return "undef";
171     case ConstZeroVal  : return "zeroinitializer";
172     case ConstUIntVal  :
173     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
174     case ConstantVal:
175       if (ConstantValue == ConstantInt::getTrue()) return "true";
176       if (ConstantValue == ConstantInt::getFalse()) return "false";
177       return "<constant expression>";
178     default:
179       assert(0 && "Unknown value!");
180       abort();
181       return "";
182     }
183   }
184
185   bool operator<(const ValID &V) const {
186     if (Type != V.Type) return Type < V.Type;
187     switch (Type) {
188     case NumberVal:     return Num < V.Num;
189     case NameVal:       return strcmp(Name, V.Name) < 0;
190     case ConstSIntVal:  return ConstPool64  < V.ConstPool64;
191     case ConstUIntVal:  return UConstPool64 < V.UConstPool64;
192     case ConstFPVal:    return ConstPoolFP  < V.ConstPoolFP;
193     case ConstNullVal:  return false;
194     case ConstUndefVal: return false;
195     case ConstZeroVal: return false;
196     case ConstantVal:   return ConstantValue < V.ConstantValue;
197     default:  assert(0 && "Unknown value type!"); return false;
198     }
199   }
200 };
201
202 struct TypeWithAttrs {
203   llvm::PATypeHolder *Ty;
204   FunctionType::ParameterAttributes Attrs;
205 };
206
207 typedef std::vector<TypeWithAttrs> TypeWithAttrsList; 
208
209 struct ArgListEntry {
210   FunctionType::ParameterAttributes Attrs;
211   llvm::PATypeHolder *Ty;
212   char *Name;
213 };
214
215 typedef std::vector<struct ArgListEntry> ArgListType;
216
217 struct ValueRefListEntry {
218   Value *Val;
219   FunctionType::ParameterAttributes Attrs;
220 };
221
222 typedef std::vector<ValueRefListEntry> ValueRefList;
223
224
225 } // End llvm namespace
226
227 #endif