Add new linkage types to support a real frontend
[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 "llvm/Constants.h"
12 #include "llvm/iOther.h"
13 #include "llvm/Function.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/Assembly/Parser.h"
16 #include "Support/StringExtras.h"
17
18 class Module;
19
20 // Global variables exported from the lexer...
21 extern std::FILE *llvmAsmin;
22 extern int llvmAsmlineno;
23
24 // Globals exported by the parser...
25 extern std::string CurFilename;
26 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
27
28 extern char* llvmAsmtext;
29 extern int   llvmAsmleng;
30
31 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
32 // appropriate character.  If AllowNull is set to false, a \00 value will cause
33 // an exception to be thrown.
34 //
35 // If AllowNull is set to true, the return value of the function points to the
36 // last character of the string in memory.
37 //
38 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
39
40
41 // ThrowException - Wrapper around the ParseException class that automatically
42 // fills in file line number and column number and options info.
43 //
44 // This also helps me because I keep typing 'throw new ParseException' instead 
45 // of just 'throw ParseException'... sigh...
46 //
47 static inline void ThrowException(const std::string &message,
48                                   int LineNo = -1) {
49   if (LineNo == -1) LineNo = llvmAsmlineno;
50   // TODO: column number in exception
51   throw ParseException(CurFilename, message, LineNo);
52 }
53
54 // ValID - Represents a reference of a definition of some sort.  This may either
55 // be a numeric reference or a symbolic (%var) reference.  This is just a 
56 // discriminated union.
57 //
58 // Note that I can't implement this class in a straight forward manner with 
59 // constructors and stuff because it goes in a union, and GCC doesn't like 
60 // putting classes with ctor's in unions.  :(
61 //
62 struct ValID {
63   enum {
64     NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
65     ConstantVal,
66   } Type;
67
68   union {
69     int      Num;         // If it's a numeric reference
70     char    *Name;        // If it's a named reference.  Memory must be free'd.
71     int64_t  ConstPool64; // Constant pool reference.  This is the value
72     uint64_t UConstPool64;// Unsigned constant pool reference.
73     double   ConstPoolFP; // Floating point constant pool reference
74     Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
75   };
76
77   static ValID create(int Num) {
78     ValID D; D.Type = NumberVal; D.Num = Num; return D;
79   }
80
81   static ValID create(char *Name) {
82     ValID D; D.Type = NameVal; D.Name = Name; return D;
83   }
84
85   static ValID create(int64_t Val) {
86     ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
87   }
88
89   static ValID create(uint64_t Val) {
90     ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
91   }
92
93   static ValID create(double Val) {
94     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
95   }
96
97   static ValID createNull() {
98     ValID D; D.Type = ConstNullVal; return D;
99   }
100
101   static ValID create(Constant *Val) {
102     ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
103   }
104
105   inline void destroy() const {
106     if (Type == NameVal)
107       free(Name);    // Free this strdup'd memory...
108   }
109
110   inline ValID copy() const {
111     if (Type != NameVal) return *this;
112     ValID Result = *this;
113     Result.Name = strdup(Name);
114     return Result;
115   }
116
117   inline std::string getName() const {
118     switch (Type) {
119     case NumberVal     : return std::string("#") + itostr(Num);
120     case NameVal       : return Name;
121     case ConstFPVal    : return ftostr(ConstPoolFP);
122     case ConstNullVal  : return "null";
123     case ConstUIntVal  :
124     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
125     case ConstantVal:
126       if (ConstantValue == ConstantBool::True) return "true";
127       if (ConstantValue == ConstantBool::False) return "false";
128       return "<constant expression>";
129     default:
130       assert(0 && "Unknown value!");
131       abort();
132       return "";
133     }
134   }
135
136   bool operator<(const ValID &V) const {
137     if (Type != V.Type) return Type < V.Type;
138     switch (Type) {
139     case NumberVal:     return Num < V.Num;
140     case NameVal:       return strcmp(Name, V.Name) < 0;
141     case ConstSIntVal:  return ConstPool64  < V.ConstPool64;
142     case ConstUIntVal:  return UConstPool64 < V.UConstPool64;
143     case ConstFPVal:    return ConstPoolFP  < V.ConstPoolFP;
144     case ConstNullVal:  return false;
145     case ConstantVal:   return ConstantValue < V.ConstantValue;
146     default:  assert(0 && "Unknown value type!"); return false;
147     }
148   }
149 };
150
151
152
153 template<class SuperType>
154 class PlaceholderValue : public SuperType {
155   ValID D;
156   int LineNum;
157 public:
158   PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
159     LineNum = llvmAsmlineno;
160   }
161   ValID &getDef() { return D; }
162   int getLineNum() const { return LineNum; }
163 };
164
165 struct InstPlaceHolderHelper : public Instruction {
166   InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
167
168   virtual Instruction *clone() const { abort(); return 0; }
169   virtual const char *getOpcodeName() const { return "placeholder"; }
170 };
171
172 struct BBPlaceHolderHelper : public BasicBlock {
173   BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
174     assert(Ty == Type::LabelTy);
175   }
176 };
177
178 struct MethPlaceHolderHelper : public Function {
179   MethPlaceHolderHelper(const Type *Ty)
180     : Function(cast<FunctionType>(Ty), InternalLinkage) {}
181 };
182
183 typedef PlaceholderValue<InstPlaceHolderHelper>  ValuePlaceHolder;
184 typedef PlaceholderValue<BBPlaceHolderHelper>    BBPlaceHolder;
185
186 static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
187   const Type *Ty = Val->getType();
188   if (isa<PointerType>(Ty) &&
189       isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
190     Ty = cast<PointerType>(Ty)->getElementType();
191
192   switch (Ty->getPrimitiveID()) {
193   case Type::LabelTyID:  return ((BBPlaceHolder*)Val)->getDef();
194   default:               return ((ValuePlaceHolder*)Val)->getDef();
195   }
196 }
197
198 static inline int getLineNumFromPlaceHolder(const Value *Val) {
199   const Type *Ty = Val->getType();
200   if (isa<PointerType>(Ty) &&
201       isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
202     Ty = cast<PointerType>(Ty)->getElementType();
203
204   switch (Ty->getPrimitiveID()) {
205   case Type::LabelTyID:  return ((BBPlaceHolder*)Val)->getLineNum();
206   default:               return ((ValuePlaceHolder*)Val)->getLineNum();
207   }
208 }
209
210 #endif