1 //===--- Builtins.cpp - Builtin function implementation -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements various things for builtin functions.
12 //===----------------------------------------------------------------------===//
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;
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,\
25 #include "clang/Basic/Builtins.def"
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];
35 Builtin::Context::Context() {
36 // Get the target specific builtins from the target.
41 void Builtin::Context::InitializeTarget(const TargetInfo &Target) {
42 assert(NumTSRecords == 0 && "Already initialized target?");
43 Target.getTargetBuiltins(TSRecords, NumTSRecords);
46 /// InitializeBuiltins - Mark the identifiers for all the builtins with their
47 /// appropriate builtin ID # and mark any non-portable builtin identifiers as
49 void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
50 const LangOptions& LangOpts) {
51 // Step #1: mark all target-independent builtins with their ID's.
52 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
53 if (!LangOpts.NoBuiltin || !strchr(BuiltinInfo[i].Attributes, 'f')) {
55 BuiltinInfo[i].builtin_lang != clang::OBJC_LANG)
56 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
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);
66 Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names,
68 // Final all target-independent names
69 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
70 if (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f'))
71 Names.push_back(BuiltinInfo[i].Name);
73 // Find target-specific names.
74 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
75 if (!NoBuiltins || !strchr(TSRecords[i].Attributes, 'f'))
76 Names.push_back(TSRecords[i].Name);
79 void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
80 Table.get(GetRecord(ID).Name).setBuiltinID(0);
84 Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
86 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
90 HasVAListArg = (*Printf == 'P');
93 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
96 assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
97 FormatIdx = strtol(Printf, 0, 10);
101 // FIXME: Refactor with isPrintfLike.
103 Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
104 bool &HasVAListArg) {
105 const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS");
109 HasVAListArg = (*Scanf == 'S');
112 assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'");
115 assert(strchr(Scanf, ':') && "printf specifier must end with a ':'");
116 FormatIdx = strtol(Scanf, 0, 10);