clang API Documentation

Types.cpp
Go to the documentation of this file.
00001 //===--- Types.cpp - Driver input & temporary type information ------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "clang/Driver/Types.h"
00011 #include "llvm/ADT/STLExtras.h"
00012 #include "llvm/ADT/StringSwitch.h"
00013 #include <cassert>
00014 #include <string.h>
00015 
00016 using namespace clang::driver;
00017 using namespace clang::driver::types;
00018 
00019 struct TypeInfo {
00020   const char *Name;
00021   const char *Flags;
00022   const char *TempSuffix;
00023   ID PreprocessedType;
00024 };
00025 
00026 static const TypeInfo TypeInfos[] = {
00027 #define TYPE(NAME, ID, PP_TYPE, TEMP_SUFFIX, FLAGS) \
00028   { NAME, FLAGS, TEMP_SUFFIX, TY_##PP_TYPE, },
00029 #include "clang/Driver/Types.def"
00030 #undef TYPE
00031 };
00032 static const unsigned numTypes = llvm::array_lengthof(TypeInfos);
00033 
00034 static const TypeInfo &getInfo(unsigned id) {
00035   assert(id > 0 && id - 1 < numTypes && "Invalid Type ID.");
00036   return TypeInfos[id - 1];
00037 }
00038 
00039 const char *types::getTypeName(ID Id) {
00040   return getInfo(Id).Name;
00041 }
00042 
00043 types::ID types::getPreprocessedType(ID Id) {
00044   return getInfo(Id).PreprocessedType;
00045 }
00046 
00047 const char *types::getTypeTempSuffix(ID Id, bool CLMode) {
00048   if (Id == TY_Object && CLMode)
00049     return "obj";
00050   if (Id == TY_Image && CLMode)
00051     return "exe";
00052   if (Id == TY_PP_Asm && CLMode)
00053     return "asm";
00054   return getInfo(Id).TempSuffix;
00055 }
00056 
00057 bool types::onlyAssembleType(ID Id) {
00058   return strchr(getInfo(Id).Flags, 'a');
00059 }
00060 
00061 bool types::onlyPrecompileType(ID Id) {
00062   return strchr(getInfo(Id).Flags, 'p');
00063 }
00064 
00065 bool types::canTypeBeUserSpecified(ID Id) {
00066   return strchr(getInfo(Id).Flags, 'u');
00067 }
00068 
00069 bool types::appendSuffixForType(ID Id) {
00070   return strchr(getInfo(Id).Flags, 'A');
00071 }
00072 
00073 bool types::canLipoType(ID Id) {
00074   return (Id == TY_Nothing ||
00075           Id == TY_Image ||
00076           Id == TY_Object ||
00077           Id == TY_LTO_BC);
00078 }
00079 
00080 bool types::isAcceptedByClang(ID Id) {
00081   switch (Id) {
00082   default:
00083     return false;
00084 
00085   case TY_Asm:
00086   case TY_C: case TY_PP_C:
00087   case TY_CL:
00088   case TY_CUDA:
00089   case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
00090   case TY_CXX: case TY_PP_CXX:
00091   case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
00092   case TY_CHeader: case TY_PP_CHeader:
00093   case TY_CLHeader:
00094   case TY_ObjCHeader: case TY_PP_ObjCHeader:
00095   case TY_CXXHeader: case TY_PP_CXXHeader:
00096   case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
00097   case TY_AST: case TY_ModuleFile:
00098   case TY_LLVM_IR: case TY_LLVM_BC:
00099     return true;
00100   }
00101 }
00102 
00103 bool types::isObjC(ID Id) {
00104   switch (Id) {
00105   default:
00106     return false;
00107 
00108   case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
00109   case TY_ObjCXX: case TY_PP_ObjCXX:
00110   case TY_ObjCHeader: case TY_PP_ObjCHeader:
00111   case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader: case TY_PP_ObjCXX_Alias:
00112     return true;
00113   }
00114 }
00115 
00116 bool types::isCXX(ID Id) {
00117   switch (Id) {
00118   default:
00119     return false;
00120 
00121   case TY_CXX: case TY_PP_CXX:
00122   case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
00123   case TY_CXXHeader: case TY_PP_CXXHeader:
00124   case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
00125   case TY_CUDA:
00126     return true;
00127   }
00128 }
00129 
00130 types::ID types::lookupTypeForExtension(const char *Ext) {
00131   return llvm::StringSwitch<types::ID>(Ext)
00132            .Case("c", TY_C)
00133            .Case("i", TY_PP_C)
00134            .Case("m", TY_ObjC)
00135            .Case("M", TY_ObjCXX)
00136            .Case("h", TY_CHeader)
00137            .Case("C", TY_CXX)
00138            .Case("H", TY_CXXHeader)
00139            .Case("f", TY_PP_Fortran)
00140            .Case("F", TY_Fortran)
00141            .Case("s", TY_PP_Asm)
00142            .Case("asm", TY_PP_Asm)
00143            .Case("S", TY_Asm)
00144            .Case("o", TY_Object)
00145            .Case("obj", TY_Object)
00146            .Case("lib", TY_Object)
00147            .Case("ii", TY_PP_CXX)
00148            .Case("mi", TY_PP_ObjC)
00149            .Case("mm", TY_ObjCXX)
00150            .Case("bc", TY_LLVM_BC)
00151            .Case("cc", TY_CXX)
00152            .Case("CC", TY_CXX)
00153            .Case("cl", TY_CL)
00154            .Case("cp", TY_CXX)
00155            .Case("cu", TY_CUDA)
00156            .Case("hh", TY_CXXHeader)
00157            .Case("ll", TY_LLVM_IR)
00158            .Case("hpp", TY_CXXHeader)
00159            .Case("ads", TY_Ada)
00160            .Case("adb", TY_Ada)
00161            .Case("ast", TY_AST)
00162            .Case("c++", TY_CXX)
00163            .Case("C++", TY_CXX)
00164            .Case("cxx", TY_CXX)
00165            .Case("cpp", TY_CXX)
00166            .Case("CPP", TY_CXX)
00167            .Case("CXX", TY_CXX)
00168            .Case("for", TY_PP_Fortran)
00169            .Case("FOR", TY_PP_Fortran)
00170            .Case("fpp", TY_Fortran)
00171            .Case("FPP", TY_Fortran)
00172            .Case("f90", TY_PP_Fortran)
00173            .Case("f95", TY_PP_Fortran)
00174            .Case("F90", TY_Fortran)
00175            .Case("F95", TY_Fortran)
00176            .Case("mii", TY_PP_ObjCXX)
00177            .Case("pcm", TY_ModuleFile)
00178            .Case("pch", TY_PCH)
00179            .Case("gch", TY_PCH)
00180            .Default(TY_INVALID);
00181 }
00182 
00183 types::ID types::lookupTypeForTypeSpecifier(const char *Name) {
00184   for (unsigned i=0; i<numTypes; ++i) {
00185     types::ID Id = (types::ID) (i + 1);
00186     if (canTypeBeUserSpecified(Id) &&
00187         strcmp(Name, getInfo(Id).Name) == 0)
00188       return Id;
00189   }
00190 
00191   return TY_INVALID;
00192 }
00193 
00194 // FIXME: Why don't we just put this list in the defs file, eh.
00195 void types::getCompilationPhases(ID Id, llvm::SmallVectorImpl<phases::ID> &P) {
00196   if (Id != TY_Object) {
00197     if (getPreprocessedType(Id) != TY_INVALID) {
00198       P.push_back(phases::Preprocess);
00199     }
00200 
00201     if (onlyPrecompileType(Id)) {
00202       P.push_back(phases::Precompile);
00203     } else {
00204       if (!onlyAssembleType(Id)) {
00205         P.push_back(phases::Compile);
00206       }
00207       P.push_back(phases::Assemble);
00208     }
00209   }
00210   if (!onlyPrecompileType(Id)) {
00211     P.push_back(phases::Link);
00212   }
00213   assert(0 < P.size() && "Not enough phases in list");
00214   assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
00215   return;
00216 }
00217 
00218 ID types::lookupCXXTypeForCType(ID Id) {
00219   switch (Id) {
00220   default:
00221     return Id;
00222 
00223   case types::TY_C:
00224     return types::TY_CXX;
00225   case types::TY_PP_C:
00226     return types::TY_PP_CXX;
00227   case types::TY_CHeader:
00228     return types::TY_CXXHeader;
00229   case types::TY_PP_CHeader:
00230     return types::TY_PP_CXXHeader;
00231   }
00232 }