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 #include "llvm/ADT/SmallVector.h"
19 using namespace clang;
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,\
26 #include "clang/Basic/Builtins.def"
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];
36 Builtin::Context::Context() {
37 // Get the target specific builtins from the target.
42 void Builtin::Context::InitializeTarget(const TargetInfo &Target) {
43 assert(NumTSRecords == 0 && "Already initialized target?");
44 Target.getTargetBuiltins(TSRecords, NumTSRecords);
47 /// InitializeBuiltins - Mark the identifiers for all the builtins with their
48 /// appropriate builtin ID # and mark any non-portable builtin identifiers as
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')) {
56 BuiltinInfo[i].builtin_lang != clang::OBJC_LANG)
57 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
60 // Step #2: Register target-specific builtins.
61 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
62 if (!LangOpts.NoBuiltin || !strchr(TSRecords[i].Attributes, 'f'))
63 Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
67 Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names,
69 // Final all target-independent names
70 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
71 if (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f'))
72 Names.push_back(BuiltinInfo[i].Name);
74 // Find target-specific names.
75 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
76 if (!NoBuiltins || !strchr(TSRecords[i].Attributes, 'f'))
77 Names.push_back(TSRecords[i].Name);
80 void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
81 Table.get(GetRecord(ID).Name).setBuiltinID(0);
85 Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
87 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
91 HasVAListArg = (*Printf == 'P');
94 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
97 assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
98 FormatIdx = strtol(Printf, 0, 10);
102 // FIXME: Refactor with isPrintfLike.
104 Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
105 bool &HasVAListArg) {
106 const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS");
110 HasVAListArg = (*Scanf == 'S');
113 assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'");
116 assert(strchr(Scanf, ':') && "printf specifier must end with a ':'");
117 FormatIdx = strtol(Scanf, 0, 10);