Give the asmparser the ability to parse strings. Patch contributed by
[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;
29
30 extern std::string &llvmAsmTextin;
31
32 // functions exported from the lexer
33 void set_scan_file(FILE * F);
34 void set_scan_string (const char * str);
35
36 // Globals exported by the parser...
37 extern char* llvmAsmtext;
38 extern int   llvmAsmleng;
39
40 namespace llvm {
41
42 // Globals exported by the parser...
43 extern std::string CurFilename;
44
45 class Module;
46 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
47
48 // Parse a string directly
49 Module *RunVMAsmParser(const char * AsmString, Module * M);
50
51
52 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
53 // appropriate character.  If AllowNull is set to false, a \00 value will cause
54 // an exception to be thrown.
55 //
56 // If AllowNull is set to true, the return value of the function points to the
57 // last character of the string in memory.
58 //
59 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
60
61
62 // ThrowException - Wrapper around the ParseException class that automatically
63 // fills in file line number and column number and options info.
64 //
65 // This also helps me because I keep typing 'throw new ParseException' instead
66 // of just 'throw ParseException'... sigh...
67 //
68 static inline void ThrowException(const std::string &message,
69                                   int LineNo = -1) {
70   if (LineNo == -1) LineNo = llvmAsmlineno;
71   // TODO: column number in exception
72   throw ParseException(CurFilename, message, LineNo);
73 }
74
75 // ValID - Represents a reference of a definition of some sort.  This may either
76 // be a numeric reference or a symbolic (%var) reference.  This is just a
77 // discriminated union.
78 //
79 // Note that I can't implement this class in a straight forward manner with
80 // constructors and stuff because it goes in a union.
81 //
82 struct ValID {
83   enum {
84     NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
85     ConstUndefVal, ConstantVal,
86   } Type;
87
88   union {
89     int      Num;         // If it's a numeric reference
90     char    *Name;        // If it's a named reference.  Memory must be free'd.
91     int64_t  ConstPool64; // Constant pool reference.  This is the value
92     uint64_t UConstPool64;// Unsigned constant pool reference.
93     double   ConstPoolFP; // Floating point constant pool reference
94     Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
95   };
96
97   static ValID create(int Num) {
98     ValID D; D.Type = NumberVal; D.Num = Num; return D;
99   }
100
101   static ValID create(char *Name) {
102     ValID D; D.Type = NameVal; D.Name = Name; return D;
103   }
104
105   static ValID create(int64_t Val) {
106     ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
107   }
108
109   static ValID create(uint64_t Val) {
110     ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
111   }
112
113   static ValID create(double Val) {
114     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
115   }
116
117   static ValID createNull() {
118     ValID D; D.Type = ConstNullVal; return D;
119   }
120
121   static ValID createUndef() {
122     ValID D; D.Type = ConstUndefVal; return D;
123   }
124
125   static ValID create(Constant *Val) {
126     ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
127   }
128
129   inline void destroy() const {
130     if (Type == NameVal)
131       free(Name);    // Free this strdup'd memory...
132   }
133
134   inline ValID copy() const {
135     if (Type != NameVal) return *this;
136     ValID Result = *this;
137     Result.Name = strdup(Name);
138     return Result;
139   }
140
141   inline std::string getName() const {
142     switch (Type) {
143     case NumberVal     : return std::string("#") + itostr(Num);
144     case NameVal       : return Name;
145     case ConstFPVal    : return ftostr(ConstPoolFP);
146     case ConstNullVal  : return "null";
147     case ConstUndefVal : return "undef";
148     case ConstUIntVal  :
149     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
150     case ConstantVal:
151       if (ConstantValue == ConstantBool::True) return "true";
152       if (ConstantValue == ConstantBool::False) return "false";
153       return "<constant expression>";
154     default:
155       assert(0 && "Unknown value!");
156       abort();
157       return "";
158     }
159   }
160
161   bool operator<(const ValID &V) const {
162     if (Type != V.Type) return Type < V.Type;
163     switch (Type) {
164     case NumberVal:     return Num < V.Num;
165     case NameVal:       return strcmp(Name, V.Name) < 0;
166     case ConstSIntVal:  return ConstPool64  < V.ConstPool64;
167     case ConstUIntVal:  return UConstPool64 < V.UConstPool64;
168     case ConstFPVal:    return ConstPoolFP  < V.ConstPoolFP;
169     case ConstNullVal:  return false;
170     case ConstUndefVal: return false;
171     case ConstantVal:   return ConstantValue < V.ConstantValue;
172     default:  assert(0 && "Unknown value type!"); return false;
173     }
174   }
175 };
176
177 } // End llvm namespace
178
179 #endif