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