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