LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ParserInternals.h

Go to the documentation of this file.
00001 //===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This header file defines the various variables that are shared among the 
00011 //  different components of the parser...
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef PARSER_INTERNALS_H
00016 #define PARSER_INTERNALS_H
00017 
00018 #include "llvm/Constants.h"
00019 #include "llvm/DerivedTypes.h"
00020 #include "llvm/Function.h"
00021 #include "llvm/Instructions.h"
00022 #include "llvm/Assembly/Parser.h"
00023 #include "llvm/ADT/StringExtras.h"
00024 
00025 // Global variables exported from the lexer...
00026 extern std::FILE *llvmAsmin;
00027 extern int llvmAsmlineno;
00028 
00029 // Globals exported by the parser...
00030 extern char* llvmAsmtext;
00031 extern int   llvmAsmleng;
00032 
00033 namespace llvm {
00034 
00035 // Globals exported by the parser...
00036 extern std::string CurFilename;
00037 
00038 class Module;
00039 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
00040 
00041 
00042 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
00043 // appropriate character.  If AllowNull is set to false, a \00 value will cause
00044 // an exception to be thrown.
00045 //
00046 // If AllowNull is set to true, the return value of the function points to the
00047 // last character of the string in memory.
00048 //
00049 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
00050 
00051 
00052 // ThrowException - Wrapper around the ParseException class that automatically
00053 // fills in file line number and column number and options info.
00054 //
00055 // This also helps me because I keep typing 'throw new ParseException' instead 
00056 // of just 'throw ParseException'... sigh...
00057 //
00058 static inline void ThrowException(const std::string &message,
00059           int LineNo = -1) {
00060   if (LineNo == -1) LineNo = llvmAsmlineno;
00061   // TODO: column number in exception
00062   throw ParseException(CurFilename, message, LineNo);
00063 }
00064 
00065 // ValID - Represents a reference of a definition of some sort.  This may either
00066 // be a numeric reference or a symbolic (%var) reference.  This is just a 
00067 // discriminated union.
00068 //
00069 // Note that I can't implement this class in a straight forward manner with 
00070 // constructors and stuff because it goes in a union.
00071 //
00072 struct ValID {
00073   enum {
00074     NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
00075     ConstUndefVal, ConstantVal,
00076   } Type;
00077 
00078   union {
00079     int      Num;         // If it's a numeric reference
00080     char    *Name;        // If it's a named reference.  Memory must be free'd.
00081     int64_t  ConstPool64; // Constant pool reference.  This is the value
00082     uint64_t UConstPool64;// Unsigned constant pool reference.
00083     double   ConstPoolFP; // Floating point constant pool reference
00084     Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
00085   };
00086 
00087   static ValID create(int Num) {
00088     ValID D; D.Type = NumberVal; D.Num = Num; return D;
00089   }
00090 
00091   static ValID create(char *Name) {
00092     ValID D; D.Type = NameVal; D.Name = Name; return D;
00093   }
00094 
00095   static ValID create(int64_t Val) {
00096     ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
00097   }
00098 
00099   static ValID create(uint64_t Val) {
00100     ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
00101   }
00102 
00103   static ValID create(double Val) {
00104     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
00105   }
00106 
00107   static ValID createNull() {
00108     ValID D; D.Type = ConstNullVal; return D;
00109   }
00110 
00111   static ValID createUndef() {
00112     ValID D; D.Type = ConstUndefVal; return D;
00113   }
00114 
00115   static ValID create(Constant *Val) {
00116     ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
00117   }
00118 
00119   inline void destroy() const {
00120     if (Type == NameVal)
00121       free(Name);    // Free this strdup'd memory...
00122   }
00123 
00124   inline ValID copy() const {
00125     if (Type != NameVal) return *this;
00126     ValID Result = *this;
00127     Result.Name = strdup(Name);
00128     return Result;
00129   }
00130 
00131   inline std::string getName() const {
00132     switch (Type) {
00133     case NumberVal     : return std::string("#") + itostr(Num);
00134     case NameVal       : return Name;
00135     case ConstFPVal    : return ftostr(ConstPoolFP);
00136     case ConstNullVal  : return "null";
00137     case ConstUndefVal : return "undef";
00138     case ConstUIntVal  :
00139     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
00140     case ConstantVal:
00141       if (ConstantValue == ConstantBool::True) return "true";
00142       if (ConstantValue == ConstantBool::False) return "false";
00143       return "<constant expression>";
00144     default:
00145       assert(0 && "Unknown value!");
00146       abort();
00147       return "";
00148     }
00149   }
00150 
00151   bool operator<(const ValID &V) const {
00152     if (Type != V.Type) return Type < V.Type;
00153     switch (Type) {
00154     case NumberVal:     return Num < V.Num;
00155     case NameVal:       return strcmp(Name, V.Name) < 0;
00156     case ConstSIntVal:  return ConstPool64  < V.ConstPool64;
00157     case ConstUIntVal:  return UConstPool64 < V.UConstPool64;
00158     case ConstFPVal:    return ConstPoolFP  < V.ConstPoolFP;
00159     case ConstNullVal:  return false;
00160     case ConstUndefVal: return false;
00161     case ConstantVal:   return ConstantValue < V.ConstantValue;
00162     default:  assert(0 && "Unknown value type!"); return false;
00163     }
00164   }
00165 };
00166 
00167 } // End llvm namespace
00168 
00169 #endif