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/LangOptions.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 using namespace clang;
22 static const Builtin::Info BuiltinInfo[] = {
23 { "not a builtin function", 0, 0, 0, ALL_LANGUAGES },
24 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
25 #define LANGBUILTIN(ID, TYPE, ATTRS, BUILTIN_LANG) { #ID, TYPE, ATTRS, 0, BUILTIN_LANG },
26 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\
28 #include "clang/Basic/Builtins.def"
31 const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const {
32 if (ID < Builtin::FirstTSBuiltin)
33 return BuiltinInfo[ID];
34 assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!");
35 return TSRecords[ID - Builtin::FirstTSBuiltin];
38 Builtin::Context::Context() {
39 // Get the target specific builtins from the target.
44 void Builtin::Context::InitializeTarget(const TargetInfo &Target) {
45 assert(NumTSRecords == 0 && "Already initialized target?");
46 Target.getTargetBuiltins(TSRecords, NumTSRecords);
49 bool Builtin::Context::BuiltinIsSupported(const Builtin::Info &BuiltinInfo,
50 const LangOptions &LangOpts) {
51 bool BuiltinsUnsupported = LangOpts.NoBuiltin &&
52 strchr(BuiltinInfo.Attributes, 'f');
53 bool MathBuiltinsUnsupported =
54 LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
55 llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
56 bool GnuModeUnsupported = !LangOpts.GNUMode &&
57 (BuiltinInfo.builtin_lang & GNU_LANG);
58 bool MSModeUnsupported = !LangOpts.MicrosoftExt &&
59 (BuiltinInfo.builtin_lang & MS_LANG);
60 bool ObjCUnsupported = !LangOpts.ObjC1 &&
61 BuiltinInfo.builtin_lang == OBJC_LANG;
62 return !BuiltinsUnsupported && !MathBuiltinsUnsupported &&
63 !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported;
66 /// InitializeBuiltins - Mark the identifiers for all the builtins with their
67 /// appropriate builtin ID # and mark any non-portable builtin identifiers as
69 void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
70 const LangOptions& LangOpts) {
71 // Step #1: mark all target-independent builtins with their ID's.
72 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
73 if (BuiltinIsSupported(BuiltinInfo[i], LangOpts)) {
74 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
77 // Step #2: Register target-specific builtins.
78 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
79 if (!LangOpts.NoBuiltin || !strchr(TSRecords[i].Attributes, 'f'))
80 Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
84 Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names) {
85 // Final all target-independent names
86 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
87 if (!strchr(BuiltinInfo[i].Attributes, 'f'))
88 Names.push_back(BuiltinInfo[i].Name);
90 // Find target-specific names.
91 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
92 if (!strchr(TSRecords[i].Attributes, 'f'))
93 Names.push_back(TSRecords[i].Name);
96 void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
97 Table.get(GetRecord(ID).Name).setBuiltinID(0);
101 Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
102 bool &HasVAListArg) {
103 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
107 HasVAListArg = (*Printf == 'P');
110 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
113 assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
114 FormatIdx = strtol(Printf, 0, 10);
118 // FIXME: Refactor with isPrintfLike.
120 Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
121 bool &HasVAListArg) {
122 const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS");
126 HasVAListArg = (*Scanf == 'S');
129 assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'");
132 assert(strchr(Scanf, ':') && "printf specifier must end with a ':'");
133 FormatIdx = strtol(Scanf, 0, 10);