1 //===--- Builtins.cpp - Builtin function implementation -------------------===// 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 // This file implements various things for builtin functions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Basic/Builtins.h" 15 #include "clang/Basic/IdentifierTable.h" 16 #include "clang/Basic/TargetInfo.h" 17 #include "clang/Basic/LangOptions.h" 18 using namespace clang; 19 20 static const Builtin::Info BuiltinInfo[] = { 21 { "not a builtin function", 0, 0, 0, ALL_LANGUAGES }, 22 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES }, 23 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\ 24 BUILTIN_LANG }, 25 #include "clang/Basic/Builtins.def" 26 }; 27 28 const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const { 29 if (ID < Builtin::FirstTSBuiltin) 30 return BuiltinInfo[ID]; 31 assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!"); 32 return TSRecords[ID - Builtin::FirstTSBuiltin]; 33 } 34 35 Builtin::Context::Context(const TargetInfo &Target) { 36 // Get the target specific builtins from the target. 37 TSRecords = 0; 38 NumTSRecords = 0; 39 Target.getTargetBuiltins(TSRecords, NumTSRecords); 40 } 41 42 /// InitializeBuiltins - Mark the identifiers for all the builtins with their 43 /// appropriate builtin ID # and mark any non-portable builtin identifiers as 44 /// such. 45 void Builtin::Context::InitializeBuiltins(IdentifierTable &Table, 46 const LangOptions& LangOpts) { 47 // Step #1: mark all target-independent builtins with their ID's. 48 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) 49 if (!LangOpts.NoBuiltin || !strchr(BuiltinInfo[i].Attributes, 'f')) { 50 if (LangOpts.ObjC1 || 51 BuiltinInfo[i].builtin_lang != clang::OBJC_LANG) 52 Table.get(BuiltinInfo[i].Name).setBuiltinID(i); 53 } 54 55 // Step #2: Register target-specific builtins. 56 for (unsigned i = 0, e = NumTSRecords; i != e; ++i) 57 if (!LangOpts.NoBuiltin || !strchr(TSRecords[i].Attributes, 'f')) 58 Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin); 59 } 60 61 void 62 Builtin::Context::GetBuiltinNames(llvm::SmallVectorImpl<const char *> &Names, 63 bool NoBuiltins) { 64 // Final all target-independent names 65 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) 66 if (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f')) 67 Names.push_back(BuiltinInfo[i].Name); 68 69 // Find target-specific names. 70 for (unsigned i = 0, e = NumTSRecords; i != e; ++i) 71 if (!NoBuiltins || !strchr(TSRecords[i].Attributes, 'f')) 72 Names.push_back(TSRecords[i].Name); 73 } 74 75 void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) { 76 Table.get(GetRecord(ID).Name).setBuiltinID(0); 77 } 78 79 bool 80 Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx, 81 bool &HasVAListArg) { 82 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP"); 83 if (!Printf) 84 return false; 85 86 HasVAListArg = (*Printf == 'P'); 87 88 ++Printf; 89 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'"); 90 ++Printf; 91 92 assert(strchr(Printf, ':') && "printf specifier must end with a ':'"); 93 FormatIdx = strtol(Printf, 0, 10); 94 return true; 95 } 96 97 // FIXME: Refactor with isPrintfLike. 98 bool 99 Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx, 100 bool &HasVAListArg) { 101 const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS"); 102 if (!Scanf) 103 return false; 104 105 HasVAListArg = (*Scanf == 'S'); 106 107 ++Scanf; 108 assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'"); 109 ++Scanf; 110 111 assert(strchr(Scanf, ':') && "printf specifier must end with a ':'"); 112 FormatIdx = strtol(Scanf, 0, 10); 113 return true; 114 } 115 116