blob: f04bc1f216b03797684bba446ff6431001404038 [file] [log] [blame]
Chris Lattner9561a0b2007-01-28 08:20:04 +00001//===--- Builtins.cpp - Builtin function implementation -------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner9561a0b2007-01-28 08:20:04 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements various things for builtin functions.
10//
11//===----------------------------------------------------------------------===//
12
Chris Lattner5abdec72009-06-14 01:05:48 +000013#include "clang/Basic/Builtins.h"
Chris Lattneref6b1362007-10-07 08:58:51 +000014#include "clang/Basic/IdentifierTable.h"
Fariborz Jahaniane8473c22010-11-30 17:35:24 +000015#include "clang/Basic/LangOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Basic/TargetInfo.h"
Eli Benderskyaefa5e22013-07-23 00:13:01 +000017#include "llvm/ADT/StringRef.h"
Chris Lattner9561a0b2007-01-28 08:20:04 +000018using namespace clang;
19
20static const Builtin::Info BuiltinInfo[] = {
Craig Topper07d3b622015-08-07 05:14:44 +000021 { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr},
22#define BUILTIN(ID, TYPE, ATTRS) \
23 { #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr },
Eric Christopher35869a22015-08-05 21:04:28 +000024#define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
Craig Topper07d3b622015-08-07 05:14:44 +000025 { #ID, TYPE, ATTRS, nullptr, LANGS, nullptr },
Eric Christopher35869a22015-08-05 21:04:28 +000026#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
Craig Topper07d3b622015-08-07 05:14:44 +000027 { #ID, TYPE, ATTRS, HEADER, LANGS, nullptr },
Chris Lattner5abdec72009-06-14 01:05:48 +000028#include "clang/Basic/Builtins.def"
Chris Lattner9561a0b2007-01-28 08:20:04 +000029};
30
Eric Christopher02d5d862015-08-06 01:01:12 +000031const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
Chris Lattner10a5b382007-01-29 05:24:35 +000032 if (ID < Builtin::FirstTSBuiltin)
33 return BuiltinInfo[ID];
Craig Topper6c03a542015-10-19 04:51:35 +000034 assert(((ID - Builtin::FirstTSBuiltin) <
35 (TSRecords.size() + AuxTSRecords.size())) &&
Artem Belevichb5bc9232015-09-22 17:23:22 +000036 "Invalid builtin ID!");
37 if (isAuxBuiltinID(ID))
38 return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
Chris Lattner10a5b382007-01-29 05:24:35 +000039 return TSRecords[ID - Builtin::FirstTSBuiltin];
Chris Lattner9561a0b2007-01-28 08:20:04 +000040}
41
Artem Belevichb5bc9232015-09-22 17:23:22 +000042void Builtin::Context::InitializeTarget(const TargetInfo &Target,
43 const TargetInfo *AuxTarget) {
Craig Topper6c03a542015-10-19 04:51:35 +000044 assert(TSRecords.empty() && "Already initialized target?");
45 TSRecords = Target.getTargetBuiltins();
Artem Belevichb5bc9232015-09-22 17:23:22 +000046 if (AuxTarget)
Craig Topper6c03a542015-10-19 04:51:35 +000047 AuxTSRecords = AuxTarget->getTargetBuiltins();
Chris Lattner4ef49c12009-06-16 16:18:48 +000048}
49
Chad Rosier7dbc9cf2016-01-06 14:35:46 +000050bool Builtin::Context::isBuiltinFunc(const char *Name) {
51 StringRef FuncName(Name);
52 for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; ++i)
53 if (FuncName.equals(BuiltinInfo[i].Name))
54 return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
55
56 return false;
57}
58
Eric Christopher02d5d862015-08-06 01:01:12 +000059bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
Eli Benderskyaefa5e22013-07-23 00:13:01 +000060 const LangOptions &LangOpts) {
Chad Rosier7dbc9cf2016-01-06 14:35:46 +000061 bool BuiltinsUnsupported =
62 (LangOpts.NoBuiltin || LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)) &&
63 strchr(BuiltinInfo.Attributes, 'f');
Eli Benderskyaefa5e22013-07-23 00:13:01 +000064 bool MathBuiltinsUnsupported =
Rachel Craikf55d0582015-09-14 14:08:18 +000065 LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
Eli Benderskyaefa5e22013-07-23 00:13:01 +000066 llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
Eric Christopher35869a22015-08-05 21:04:28 +000067 bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG);
68 bool MSModeUnsupported =
69 !LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG);
Erik Pilkingtonfa983902018-10-30 20:31:30 +000070 bool ObjCUnsupported = !LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG;
Jan Vesely31ecb4b2017-09-07 19:39:10 +000071 bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 &&
72 (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG;
73 bool OclC2Unsupported = LangOpts.OpenCLVersion != 200 &&
74 (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG;
75 bool OclCUnsupported = !LangOpts.OpenCL &&
76 (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES);
Jonas Hahnfeld23604a82017-10-17 14:28:14 +000077 bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG;
Xiuli Panbb4d8d32016-01-26 04:03:48 +000078 return !BuiltinsUnsupported && !MathBuiltinsUnsupported && !OclCUnsupported &&
Jonas Hahnfeld23604a82017-10-17 14:28:14 +000079 !OclC1Unsupported && !OclC2Unsupported && !OpenMPUnsupported &&
Reid Klecknercf8933d2013-11-13 22:47:22 +000080 !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported;
Eli Benderskyaefa5e22013-07-23 00:13:01 +000081}
82
Ismail Donmeza8d3a2e2015-09-15 09:53:14 +000083/// initializeBuiltins - Mark the identifiers for all the builtins with their
Chris Lattner9561a0b2007-01-28 08:20:04 +000084/// appropriate builtin ID # and mark any non-portable builtin identifiers as
85/// such.
Eric Christopher02d5d862015-08-06 01:01:12 +000086void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
Fariborz Jahaniane8473c22010-11-30 17:35:24 +000087 const LangOptions& LangOpts) {
Chris Lattner9561a0b2007-01-28 08:20:04 +000088 // Step #1: mark all target-independent builtins with their ID's.
Chris Lattner10a5b382007-01-29 05:24:35 +000089 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Eric Christopher02d5d862015-08-06 01:01:12 +000090 if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
Benjamin Kramer82598ec2013-05-31 16:29:28 +000091 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
Eli Benderskyaefa5e22013-07-23 00:13:01 +000092 }
Chris Lattner15ba9492009-06-14 01:54:56 +000093
Douglas Gregorf89771c2009-04-22 04:56:28 +000094 // Step #2: Register target-specific builtins.
Craig Topper6c03a542015-10-19 04:51:35 +000095 for (unsigned i = 0, e = TSRecords.size(); i != e; ++i)
Eric Christopher02d5d862015-08-06 01:01:12 +000096 if (builtinIsSupported(TSRecords[i], LangOpts))
Artem Belevichb5bc9232015-09-22 17:23:22 +000097 Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
98
99 // Step #3: Register target-specific builtins for AuxTarget.
Craig Topper6c03a542015-10-19 04:51:35 +0000100 for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i)
Artem Belevichb5bc9232015-09-22 17:23:22 +0000101 Table.get(AuxTSRecords[i].Name)
Craig Topper6c03a542015-10-19 04:51:35 +0000102 .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
Chris Lattner9561a0b2007-01-28 08:20:04 +0000103}
104
Eric Christopher02d5d862015-08-06 01:01:12 +0000105void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) {
106 Table.get(getRecord(ID).Name).setBuiltinID(0);
Douglas Gregor9246b682010-12-21 19:47:46 +0000107}
108
Craig Topper74c10e32018-07-09 19:00:16 +0000109unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
110 const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
111 if (!WidthPos)
112 return 0;
113
114 ++WidthPos;
115 assert(*WidthPos == ':' &&
116 "Vector width specifier must be followed by a ':'");
117 ++WidthPos;
118
119 char *EndPos;
120 unsigned Width = ::strtol(WidthPos, &EndPos, 10);
121 assert(*EndPos == ':' && "Vector width specific must end with a ':'");
122 return Width;
123}
124
Aaron Ballman69714e72014-01-03 20:10:54 +0000125bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
126 bool &HasVAListArg, const char *Fmt) const {
127 assert(Fmt && "Not passed a format string");
128 assert(::strlen(Fmt) == 2 &&
129 "Format string needs to be two characters long");
130 assert(::toupper(Fmt[0]) == Fmt[1] &&
131 "Format string is not in the form \"xX\"");
132
Eric Christopher02d5d862015-08-06 01:01:12 +0000133 const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
Aaron Ballman69714e72014-01-03 20:10:54 +0000134 if (!Like)
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000135 return false;
136
Aaron Ballman69714e72014-01-03 20:10:54 +0000137 HasVAListArg = (*Like == Fmt[1]);
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000138
Aaron Ballman69714e72014-01-03 20:10:54 +0000139 ++Like;
140 assert(*Like == ':' && "Format specifier must be followed by a ':'");
141 ++Like;
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000142
Aaron Ballman69714e72014-01-03 20:10:54 +0000143 assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
Craig Topperf1186c52014-05-08 06:41:40 +0000144 FormatIdx = ::strtol(Like, nullptr, 10);
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000145 return true;
146}
147
Aaron Ballman69714e72014-01-03 20:10:54 +0000148bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
149 bool &HasVAListArg) {
150 return isLike(ID, FormatIdx, HasVAListArg, "pP");
Ted Kremenek5932c352010-07-16 02:11:15 +0000151}
152
Aaron Ballman69714e72014-01-03 20:10:54 +0000153bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
154 bool &HasVAListArg) {
155 return isLike(ID, FormatIdx, HasVAListArg, "sS");
156}
Erich Keane41af9712018-04-16 21:30:08 +0000157
Johannes Doerfertac991bb2019-01-19 05:36:54 +0000158bool Builtin::Context::performsCallback(unsigned ID,
159 SmallVectorImpl<int> &Encoding) const {
160 const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
161 if (!CalleePos)
162 return false;
163
164 ++CalleePos;
165 assert(*CalleePos == '<' &&
166 "Callback callee specifier must be followed by a '<'");
167 ++CalleePos;
168
169 char *EndPos;
170 int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
171 assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!");
172 Encoding.push_back(CalleeIdx);
173
174 while (*EndPos == ',') {
175 const char *PayloadPos = EndPos + 1;
176
177 int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
178 Encoding.push_back(PayloadIdx);
179 }
180
181 assert(*EndPos == '>' && "Callback callee specifier must end with a '>'");
182 return true;
183}
184
Erich Keane41af9712018-04-16 21:30:08 +0000185bool Builtin::Context::canBeRedeclared(unsigned ID) const {
186 return ID == Builtin::NotBuiltin ||
187 ID == Builtin::BI__va_start ||
188 (!hasReferenceArgsOrResult(ID) &&
189 !hasCustomTypechecking(ID));
190}