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 #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
GetRecord(unsigned ID) const29 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
Context()36 Builtin::Context::Context() {
37 // Get the target specific builtins from the target.
38 TSRecords = 0;
39 NumTSRecords = 0;
40 }
41
InitializeTarget(const TargetInfo & Target)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.
InitializeBuiltins(IdentifierTable & Table,const LangOptions & LangOpts)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 if (LangOpts.ObjC1 ||
56 BuiltinInfo[i].builtin_lang != clang::OBJC_LANG)
57 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
58 }
59
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);
64 }
65
66 void
GetBuiltinNames(SmallVectorImpl<const char * > & Names,bool NoBuiltins)67 Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names,
68 bool NoBuiltins) {
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);
73
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);
78 }
79
ForgetBuiltin(unsigned ID,IdentifierTable & Table)80 void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
81 Table.get(GetRecord(ID).Name).setBuiltinID(0);
82 }
83
84 bool
isPrintfLike(unsigned ID,unsigned & FormatIdx,bool & HasVAListArg)85 Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
86 bool &HasVAListArg) {
87 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
88 if (!Printf)
89 return false;
90
91 HasVAListArg = (*Printf == 'P');
92
93 ++Printf;
94 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
95 ++Printf;
96
97 assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
98 FormatIdx = strtol(Printf, 0, 10);
99 return true;
100 }
101
102 // FIXME: Refactor with isPrintfLike.
103 bool
isScanfLike(unsigned ID,unsigned & FormatIdx,bool & HasVAListArg)104 Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
105 bool &HasVAListArg) {
106 const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS");
107 if (!Scanf)
108 return false;
109
110 HasVAListArg = (*Scanf == 'S');
111
112 ++Scanf;
113 assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'");
114 ++Scanf;
115
116 assert(strchr(Scanf, ':') && "printf specifier must end with a ':'");
117 FormatIdx = strtol(Scanf, 0, 10);
118 return true;
119 }
120
121