Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / AsmParser / ParserInternals.h
blob820e5ba8a655674b6f6c78717684d95b00a2279d
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...
13 //===----------------------------------------------------------------------===//
15 #ifndef PARSER_INTERNALS_H
16 #define PARSER_INTERNALS_H
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"
26 // Global variables exported from the lexer...
28 extern int llvmAsmlineno;
30 extern std::string &llvmAsmTextin;
32 // functions exported from the lexer
33 void set_scan_file(FILE * F);
34 void set_scan_string (const char * str);
36 // Globals exported by the parser...
37 extern char* llvmAsmtext;
38 extern int llvmAsmleng;
40 namespace llvm {
42 // Globals exported by the parser...
43 extern std::string CurFilename;
45 class Module;
46 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
48 // Parse a string directly
49 Module *RunVMAsmParser(const char * AsmString, Module * M);
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.
56 // If AllowNull is set to true, the return value of the function points to the
57 // last character of the string in memory.
59 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
62 // ThrowException - Wrapper around the ParseException class that automatically
63 // fills in file line number and column number and options info.
65 // This also helps me because I keep typing 'throw new ParseException' instead
66 // of just 'throw ParseException'... sigh...
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);
75 /// InlineAsmDescriptor - This is a simple class that holds info about inline
76 /// asm blocks, for use by ValID.
77 struct InlineAsmDescriptor {
78 std::string AsmString, Constraints;
79 bool HasSideEffects;
81 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
82 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
86 // ValID - Represents a reference of a definition of some sort. This may either
87 // be a numeric reference or a symbolic (%var) reference. This is just a
88 // discriminated union.
90 // Note that I can't implement this class in a straight forward manner with
91 // constructors and stuff because it goes in a union.
93 struct ValID {
94 enum {
95 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
96 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
97 } Type;
99 union {
100 int Num; // If it's a numeric reference
101 char *Name; // If it's a named reference. Memory must be free'd.
102 int64_t ConstPool64; // Constant pool reference. This is the value
103 uint64_t UConstPool64;// Unsigned constant pool reference.
104 double ConstPoolFP; // Floating point constant pool reference
105 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
106 InlineAsmDescriptor *IAD;
109 static ValID create(int Num) {
110 ValID D; D.Type = NumberVal; D.Num = Num; return D;
113 static ValID create(char *Name) {
114 ValID D; D.Type = NameVal; D.Name = Name; return D;
117 static ValID create(int64_t Val) {
118 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
121 static ValID create(uint64_t Val) {
122 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
125 static ValID create(double Val) {
126 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
129 static ValID createNull() {
130 ValID D; D.Type = ConstNullVal; return D;
133 static ValID createUndef() {
134 ValID D; D.Type = ConstUndefVal; return D;
137 static ValID createZeroInit() {
138 ValID D; D.Type = ConstZeroVal; return D;
141 static ValID create(Constant *Val) {
142 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
145 static ValID createInlineAsm(const std::string &AsmString,
146 const std::string &Constraints,
147 bool HasSideEffects) {
148 ValID D;
149 D.Type = InlineAsmVal;
150 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
151 return D;
154 inline void destroy() const {
155 if (Type == NameVal)
156 free(Name); // Free this strdup'd memory.
157 else if (Type == InlineAsmVal)
158 delete IAD;
161 inline ValID copy() const {
162 if (Type != NameVal) return *this;
163 ValID Result = *this;
164 Result.Name = strdup(Name);
165 return Result;
168 inline std::string getName() const {
169 switch (Type) {
170 case NumberVal : return std::string("#") + itostr(Num);
171 case NameVal : return Name;
172 case ConstFPVal : return ftostr(ConstPoolFP);
173 case ConstNullVal : return "null";
174 case ConstUndefVal : return "undef";
175 case ConstZeroVal : return "zeroinitializer";
176 case ConstUIntVal :
177 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
178 case ConstantVal:
179 if (ConstantValue == ConstantBool::True) return "true";
180 if (ConstantValue == ConstantBool::False) return "false";
181 return "<constant expression>";
182 default:
183 assert(0 && "Unknown value!");
184 abort();
185 return "";
189 bool operator<(const ValID &V) const {
190 if (Type != V.Type) return Type < V.Type;
191 switch (Type) {
192 case NumberVal: return Num < V.Num;
193 case NameVal: return strcmp(Name, V.Name) < 0;
194 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
195 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
196 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
197 case ConstNullVal: return false;
198 case ConstUndefVal: return false;
199 case ConstZeroVal: return false;
200 case ConstantVal: return ConstantValue < V.ConstantValue;
201 default: assert(0 && "Unknown value type!"); return false;
206 } // End llvm namespace
208 #endif