Home | History | Annotate | Download | only in Driver
      1 //===--- Types.cpp - Driver input & temporary type information ------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "clang/Driver/Types.h"
     11 
     12 #include "llvm/ADT/StringSwitch.h"
     13 #include <string.h>
     14 #include <cassert>
     15 
     16 using namespace clang::driver;
     17 using namespace clang::driver::types;
     18 
     19 struct TypeInfo {
     20   const char *Name;
     21   const char *Flags;
     22   const char *TempSuffix;
     23   ID PreprocessedType;
     24 };
     25 
     26 static const TypeInfo TypeInfos[] = {
     27 #define TYPE(NAME, ID, PP_TYPE, TEMP_SUFFIX, FLAGS) \
     28   { NAME, FLAGS, TEMP_SUFFIX, TY_##PP_TYPE, },
     29 #include "clang/Driver/Types.def"
     30 #undef TYPE
     31 };
     32 static const unsigned numTypes = sizeof(TypeInfos) / sizeof(TypeInfos[0]);
     33 
     34 static const TypeInfo &getInfo(unsigned id) {
     35   assert(id > 0 && id - 1 < numTypes && "Invalid Type ID.");
     36   return TypeInfos[id - 1];
     37 }
     38 
     39 const char *types::getTypeName(ID Id) {
     40   return getInfo(Id).Name;
     41 }
     42 
     43 types::ID types::getPreprocessedType(ID Id) {
     44   return getInfo(Id).PreprocessedType;
     45 }
     46 
     47 const char *types::getTypeTempSuffix(ID Id) {
     48   return getInfo(Id).TempSuffix;
     49 }
     50 
     51 bool types::onlyAssembleType(ID Id) {
     52   return strchr(getInfo(Id).Flags, 'a');
     53 }
     54 
     55 bool types::onlyPrecompileType(ID Id) {
     56   return strchr(getInfo(Id).Flags, 'p');
     57 }
     58 
     59 bool types::canTypeBeUserSpecified(ID Id) {
     60   return strchr(getInfo(Id).Flags, 'u');
     61 }
     62 
     63 bool types::appendSuffixForType(ID Id) {
     64   return strchr(getInfo(Id).Flags, 'A');
     65 }
     66 
     67 bool types::canLipoType(ID Id) {
     68   return (Id == TY_Nothing ||
     69           Id == TY_Image ||
     70           Id == TY_Object);
     71 }
     72 
     73 bool types::isAcceptedByClang(ID Id) {
     74   switch (Id) {
     75   default:
     76     return false;
     77 
     78   case TY_Asm:
     79   case TY_C: case TY_PP_C:
     80   case TY_CL:
     81   case TY_CUDA:
     82   case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
     83   case TY_CXX: case TY_PP_CXX:
     84   case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
     85   case TY_CHeader: case TY_PP_CHeader:
     86   case TY_CLHeader:
     87   case TY_ObjCHeader: case TY_PP_ObjCHeader:
     88   case TY_CXXHeader: case TY_PP_CXXHeader:
     89   case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
     90   case TY_AST:
     91   case TY_LLVM_IR: case TY_LLVM_BC:
     92     return true;
     93   }
     94 }
     95 
     96 bool types::isOnlyAcceptedByClang(ID Id) {
     97   switch (Id) {
     98   default:
     99     return false;
    100 
    101   case TY_AST:
    102   case TY_LLVM_IR:
    103   case TY_LLVM_BC:
    104   case TY_RewrittenObjC:
    105   case TY_RewrittenLegacyObjC:
    106     return true;
    107   }
    108 }
    109 
    110 bool types::isObjC(ID Id) {
    111   switch (Id) {
    112   default:
    113     return false;
    114 
    115   case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
    116   case TY_ObjCXX: case TY_PP_ObjCXX:
    117   case TY_ObjCHeader: case TY_PP_ObjCHeader:
    118   case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader: case TY_PP_ObjCXX_Alias:
    119     return true;
    120   }
    121 }
    122 
    123 bool types::isCXX(ID Id) {
    124   switch (Id) {
    125   default:
    126     return false;
    127 
    128   case TY_CXX: case TY_PP_CXX:
    129   case TY_ObjCXX: case TY_PP_ObjCXX:
    130   case TY_CXXHeader: case TY_PP_CXXHeader:
    131   case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
    132     return true;
    133   }
    134 }
    135 
    136 types::ID types::lookupTypeForExtension(const char *Ext) {
    137   return llvm::StringSwitch<types::ID>(Ext)
    138            .Case("c", TY_C)
    139            .Case("i", TY_PP_C)
    140            .Case("m", TY_ObjC)
    141            .Case("M", TY_ObjCXX)
    142            .Case("h", TY_CHeader)
    143            .Case("C", TY_CXX)
    144            .Case("H", TY_CXXHeader)
    145            .Case("f", TY_PP_Fortran)
    146            .Case("F", TY_Fortran)
    147            .Case("s", TY_PP_Asm)
    148            .Case("S", TY_Asm)
    149            .Case("o", TY_Object)
    150            .Case("ii", TY_PP_CXX)
    151            .Case("mi", TY_PP_ObjC)
    152            .Case("mm", TY_ObjCXX)
    153            .Case("bc", TY_LLVM_BC)
    154            .Case("cc", TY_CXX)
    155            .Case("CC", TY_CXX)
    156            .Case("cl", TY_CL)
    157            .Case("cp", TY_CXX)
    158            .Case("cu", TY_CUDA)
    159            .Case("hh", TY_CXXHeader)
    160            .Case("ll", TY_LLVM_IR)
    161            .Case("hpp", TY_CXXHeader)
    162            .Case("ads", TY_Ada)
    163            .Case("adb", TY_Ada)
    164            .Case("ast", TY_AST)
    165            .Case("c++", TY_CXX)
    166            .Case("C++", TY_CXX)
    167            .Case("cxx", TY_CXX)
    168            .Case("cpp", TY_CXX)
    169            .Case("CPP", TY_CXX)
    170            .Case("CXX", TY_CXX)
    171            .Case("for", TY_PP_Fortran)
    172            .Case("FOR", TY_PP_Fortran)
    173            .Case("fpp", TY_Fortran)
    174            .Case("FPP", TY_Fortran)
    175            .Case("f90", TY_PP_Fortran)
    176            .Case("f95", TY_PP_Fortran)
    177            .Case("F90", TY_Fortran)
    178            .Case("F95", TY_Fortran)
    179            .Case("mii", TY_PP_ObjCXX)
    180            .Default(TY_INVALID);
    181 }
    182 
    183 types::ID types::lookupTypeForTypeSpecifier(const char *Name) {
    184   unsigned N = strlen(Name);
    185 
    186   for (unsigned i=0; i<numTypes; ++i) {
    187     types::ID Id = (types::ID) (i + 1);
    188     if (canTypeBeUserSpecified(Id) &&
    189         memcmp(Name, getInfo(Id).Name, N + 1) == 0)
    190       return Id;
    191   }
    192 
    193   return TY_INVALID;
    194 }
    195 
    196 // FIXME: Why don't we just put this list in the defs file, eh.
    197 
    198 unsigned types::getNumCompilationPhases(ID Id) {
    199   if (Id == TY_Object)
    200     return 1;
    201 
    202   unsigned N = 0;
    203   if (getPreprocessedType(Id) != TY_INVALID)
    204     N += 1;
    205 
    206   if (onlyAssembleType(Id))
    207     return N + 2; // assemble, link
    208   if (onlyPrecompileType(Id))
    209     return N + 1; // precompile
    210 
    211   return N + 3; // compile, assemble, link
    212 }
    213 
    214 phases::ID types::getCompilationPhase(ID Id, unsigned N) {
    215   assert(N < getNumCompilationPhases(Id) && "Invalid index.");
    216 
    217   if (Id == TY_Object)
    218     return phases::Link;
    219 
    220   if (getPreprocessedType(Id) != TY_INVALID) {
    221     if (N == 0)
    222       return phases::Preprocess;
    223     --N;
    224   }
    225 
    226   if (onlyAssembleType(Id))
    227     return N == 0 ? phases::Assemble : phases::Link;
    228 
    229   if (onlyPrecompileType(Id))
    230     return phases::Precompile;
    231 
    232   if (N == 0)
    233     return phases::Compile;
    234   if (N == 1)
    235     return phases::Assemble;
    236 
    237   return phases::Link;
    238 }
    239 
    240 ID types::lookupCXXTypeForCType(ID Id) {
    241   switch (Id) {
    242   default:
    243     return Id;
    244 
    245   case types::TY_C:
    246     return types::TY_CXX;
    247   case types::TY_PP_C:
    248     return types::TY_PP_CXX;
    249   case types::TY_CHeader:
    250     return types::TY_CXXHeader;
    251   case types::TY_PP_CHeader:
    252     return types::TY_PP_CXXHeader;
    253   }
    254 }
    255