reset errno to zero on entry to the application's main function. This fixes
[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/ParameterAttributes.h"
21 #include "llvm/Function.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/Assembly/Parser.h"
24 #include "llvm/ADT/StringExtras.h"
25
26
27 // Global variables exported from the lexer...
28
29 extern int llvmAsmlineno;         /// FIXME: Not threading friendly
30 extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
31
32 extern std::string &llvmAsmTextin;
33
34 // functions exported from the lexer
35 void set_scan_file(FILE * F);
36 void set_scan_string (const char * str);
37
38 // Globals exported by the parser...
39 extern char* llvmAsmtext;
40 extern int   llvmAsmleng;
41
42 namespace llvm {
43
44 // Globals exported by the parser...
45 extern std::string CurFilename;   /// FIXME: Not threading friendly
46
47 class Module;
48 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
49
50 // Parse a string directly
51 Module *RunVMAsmParser(const char * AsmString, Module * M);
52
53
54 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
55 // appropriate character.  If AllowNull is set to false, a \00 value will cause
56 // an error.
57 //
58 // If AllowNull is set to true, the return value of the function points to the
59 // last character of the string in memory.
60 //
61 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
62
63
64 // ThrowException - Wrapper around the ParseException class that automatically
65 // fills in file line number and column number and options info.
66 //
67 // This also helps me because I keep typing 'throw new ParseException' instead
68 // of just 'throw ParseException'... sigh...
69 //
70 extern void GenerateError(const std::string &message, int LineNo = -1);
71
72 /// InlineAsmDescriptor - This is a simple class that holds info about inline
73 /// asm blocks, for use by ValID.
74 struct InlineAsmDescriptor {
75   std::string AsmString, Constraints;
76   bool HasSideEffects;
77   
78   InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
79     : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
80 };
81
82
83 // ValID - Represents a reference of a definition of some sort.  This may either
84 // be a numeric reference or a symbolic (%var) reference.  This is just a
85 // discriminated union.
86 //
87 // Note that I can't implement this class in a straight forward manner with
88 // constructors and stuff because it goes in a union.
89 //
90 struct ValID {
91   enum {
92     LocalID, GlobalID, LocalName, GlobalName,
93     ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
94     ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
95   } Type;
96
97   union {
98     unsigned Num;         // If it's a numeric reference like %1234
99     char    *Name;        // If it's a named reference.  Memory must be free'd.
100     int64_t  ConstPool64; // Constant pool reference.  This is the value
101     uint64_t UConstPool64;// Unsigned constant pool reference.
102     double   ConstPoolFP; // Floating point constant pool reference
103     Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
104     InlineAsmDescriptor *IAD;
105   };
106
107   static ValID createLocalID(unsigned Num) {
108     ValID D; D.Type = LocalID; D.Num = Num; return D;
109   }
110   static ValID createGlobalID(unsigned Num) {
111     ValID D; D.Type = GlobalID; D.Num = Num; return D;
112   }
113   static ValID createLocalName(char *Name) {
114     ValID D; D.Type = LocalName; D.Name = Name; return D;
115   }
116   static ValID createGlobalName(char *Name) {
117     ValID D; D.Type = GlobalName; D.Name = Name; return D;
118   }
119   
120   static ValID create(int64_t Val) {
121     ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
122   }
123
124   static ValID create(uint64_t Val) {
125     ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
126   }
127
128   static ValID create(double Val) {
129     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
130   }
131
132   static ValID createNull() {
133     ValID D; D.Type = ConstNullVal; return D;
134   }
135
136   static ValID createUndef() {
137     ValID D; D.Type = ConstUndefVal; return D;
138   }
139
140   static ValID createZeroInit() {
141     ValID D; D.Type = ConstZeroVal; return D;
142   }
143   
144   static ValID create(Constant *Val) {
145     ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
146   }
147   
148   static ValID createInlineAsm(const std::string &AsmString,
149                                const std::string &Constraints,
150                                bool HasSideEffects) {
151     ValID D;
152     D.Type = InlineAsmVal;
153     D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
154     return D;
155   }
156
157   inline void destroy() const {
158     if (Type == LocalName || Type == GlobalName)
159       free(Name);    // Free this strdup'd memory.
160     else if (Type == InlineAsmVal)
161       delete IAD;
162   }
163
164   inline ValID copy() const {
165     if (Type != LocalName && Type != GlobalName) return *this;
166     ValID Result = *this;
167     Result.Name = strdup(Name);
168     return Result;
169   }
170
171   inline std::string getName() const {
172     switch (Type) {
173     case LocalID       : return '%' + utostr(Num);
174     case GlobalID      : return '@' + utostr(Num);
175     case LocalName     : return Name;
176     case GlobalName    : return Name;
177     case ConstFPVal    : return ftostr(ConstPoolFP);
178     case ConstNullVal  : return "null";
179     case ConstUndefVal : return "undef";
180     case ConstZeroVal  : return "zeroinitializer";
181     case ConstUIntVal  :
182     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
183     case ConstantVal:
184       if (ConstantValue == ConstantInt::getTrue()) return "true";
185       if (ConstantValue == ConstantInt::getFalse()) return "false";
186       return "<constant expression>";
187     default:
188       assert(0 && "Unknown value!");
189       abort();
190       return "";
191     }
192   }
193
194   bool operator<(const ValID &V) const {
195     if (Type != V.Type) return Type < V.Type;
196     switch (Type) {
197     case LocalID:
198     case GlobalID:      return Num < V.Num;
199     case LocalName:
200     case GlobalName:    return strcmp(Name, V.Name) < 0;
201     case ConstSIntVal:  return ConstPool64  < V.ConstPool64;
202     case ConstUIntVal:  return UConstPool64 < V.UConstPool64;
203     case ConstFPVal:    return ConstPoolFP  < V.ConstPoolFP;
204     case ConstNullVal:  return false;
205     case ConstUndefVal: return false;
206     case ConstZeroVal: return false;
207     case ConstantVal:   return ConstantValue < V.ConstantValue;
208     default:  assert(0 && "Unknown value type!"); return false;
209     }
210   }
211
212   bool operator==(const ValID &V) const {
213     if (Type == V.Type) {
214       switch (Type) {
215         case LocalID:
216         case GlobalID: return Num == V.Num;
217         case LocalName:
218         case GlobalName: return strcmp(Name, V.Name) == 0;
219         case ConstSIntVal:  return ConstPool64  == V.ConstPool64;
220         case ConstUIntVal:  return UConstPool64 == V.UConstPool64;
221         case ConstFPVal:    return ConstPoolFP  == V.ConstPoolFP;
222         case ConstantVal:   return ConstantValue == V.ConstantValue;
223         case ConstNullVal:  return true;
224         case ConstUndefVal: return true;
225         case ConstZeroVal:  return true;
226         default:  assert(0 && "Unknown value type!"); return false;
227       }
228     }
229     return false;
230   }
231 };
232
233 struct TypeWithAttrs {
234   llvm::PATypeHolder *Ty;
235   uint16_t Attrs;
236 };
237
238 typedef std::vector<TypeWithAttrs> TypeWithAttrsList; 
239
240 struct ArgListEntry {
241   uint16_t Attrs;
242   llvm::PATypeHolder *Ty;
243   char *Name;
244 };
245
246 typedef std::vector<struct ArgListEntry> ArgListType;
247
248 struct ValueRefListEntry {
249   Value *Val;
250   uint16_t Attrs;
251 };
252
253 typedef std::vector<ValueRefListEntry> ValueRefList;
254
255
256 } // End llvm namespace
257
258 #endif