blob: 28695d649a86753d2146cc6bfb03a9a89e61e670 [file] [log] [blame]
Chris Lattner9561a0b2007-01-28 08:20:04 +00001//===--- Builtins.cpp - Builtin function implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner9561a0b2007-01-28 08:20:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements various things for builtin functions.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner5abdec72009-06-14 01:05:48 +000014#include "clang/Basic/Builtins.h"
Chris Lattneref6b1362007-10-07 08:58:51 +000015#include "clang/Basic/IdentifierTable.h"
Fariborz Jahaniane8473c22010-11-30 17:35:24 +000016#include "clang/Basic/LangOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Basic/TargetInfo.h"
Eli Benderskyaefa5e22013-07-23 00:13:01 +000018#include "llvm/ADT/StringRef.h"
Chris Lattner9561a0b2007-01-28 08:20:04 +000019using namespace clang;
20
21static const Builtin::Info BuiltinInfo[] = {
Craig Topper07d3b622015-08-07 05:14:44 +000022 { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr},
23#define BUILTIN(ID, TYPE, ATTRS) \
24 { #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr },
Eric Christopher35869a22015-08-05 21:04:28 +000025#define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
Craig Topper07d3b622015-08-07 05:14:44 +000026 { #ID, TYPE, ATTRS, nullptr, LANGS, nullptr },
Eric Christopher35869a22015-08-05 21:04:28 +000027#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
Craig Topper07d3b622015-08-07 05:14:44 +000028 { #ID, TYPE, ATTRS, HEADER, LANGS, nullptr },
Chris Lattner5abdec72009-06-14 01:05:48 +000029#include "clang/Basic/Builtins.def"
Chris Lattner9561a0b2007-01-28 08:20:04 +000030};
31
Eric Christopher02d5d862015-08-06 01:01:12 +000032const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
Chris Lattner10a5b382007-01-29 05:24:35 +000033 if (ID < Builtin::FirstTSBuiltin)
34 return BuiltinInfo[ID];
Craig Topper6c03a542015-10-19 04:51:35 +000035 assert(((ID - Builtin::FirstTSBuiltin) <
36 (TSRecords.size() + AuxTSRecords.size())) &&
Artem Belevichb5bc9232015-09-22 17:23:22 +000037 "Invalid builtin ID!");
38 if (isAuxBuiltinID(ID))
39 return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
Chris Lattner10a5b382007-01-29 05:24:35 +000040 return TSRecords[ID - Builtin::FirstTSBuiltin];
Chris Lattner9561a0b2007-01-28 08:20:04 +000041}
42
Artem Belevichb5bc9232015-09-22 17:23:22 +000043void Builtin::Context::InitializeTarget(const TargetInfo &Target,
44 const TargetInfo *AuxTarget) {
Craig Topper6c03a542015-10-19 04:51:35 +000045 assert(TSRecords.empty() && "Already initialized target?");
46 TSRecords = Target.getTargetBuiltins();
Artem Belevichb5bc9232015-09-22 17:23:22 +000047 if (AuxTarget)
Craig Topper6c03a542015-10-19 04:51:35 +000048 AuxTSRecords = AuxTarget->getTargetBuiltins();
Chris Lattner4ef49c12009-06-16 16:18:48 +000049}
50
Chad Rosier7dbc9cf2016-01-06 14:35:46 +000051bool Builtin::Context::isBuiltinFunc(const char *Name) {
52 StringRef FuncName(Name);
53 for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; ++i)
54 if (FuncName.equals(BuiltinInfo[i].Name))
55 return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
56
57 return false;
58}
59
Eric Christopher02d5d862015-08-06 01:01:12 +000060bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
Eli Benderskyaefa5e22013-07-23 00:13:01 +000061 const LangOptions &LangOpts) {
Chad Rosier7dbc9cf2016-01-06 14:35:46 +000062 bool BuiltinsUnsupported =
63 (LangOpts.NoBuiltin || LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)) &&
64 strchr(BuiltinInfo.Attributes, 'f');
Eli Benderskyaefa5e22013-07-23 00:13:01 +000065 bool MathBuiltinsUnsupported =
Rachel Craikf55d0582015-09-14 14:08:18 +000066 LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
Eli Benderskyaefa5e22013-07-23 00:13:01 +000067 llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
Eric Christopher35869a22015-08-05 21:04:28 +000068 bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG);
69 bool MSModeUnsupported =
70 !LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG);
71 bool ObjCUnsupported = !LangOpts.ObjC1 && BuiltinInfo.Langs == OBJC_LANG;
Anastasia Stulova7f8d6dc2016-07-04 16:07:18 +000072 bool OclCUnsupported = LangOpts.OpenCLVersion != 200 &&
73 BuiltinInfo.Langs == OCLC20_LANG;
Xiuli Panbb4d8d32016-01-26 04:03:48 +000074 return !BuiltinsUnsupported && !MathBuiltinsUnsupported && !OclCUnsupported &&
Reid Klecknercf8933d2013-11-13 22:47:22 +000075 !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported;
Eli Benderskyaefa5e22013-07-23 00:13:01 +000076}
77
Ismail Donmeza8d3a2e2015-09-15 09:53:14 +000078/// initializeBuiltins - Mark the identifiers for all the builtins with their
Chris Lattner9561a0b2007-01-28 08:20:04 +000079/// appropriate builtin ID # and mark any non-portable builtin identifiers as
80/// such.
Eric Christopher02d5d862015-08-06 01:01:12 +000081void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
Fariborz Jahaniane8473c22010-11-30 17:35:24 +000082 const LangOptions& LangOpts) {
Chris Lattner9561a0b2007-01-28 08:20:04 +000083 // Step #1: mark all target-independent builtins with their ID's.
Chris Lattner10a5b382007-01-29 05:24:35 +000084 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Eric Christopher02d5d862015-08-06 01:01:12 +000085 if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
Benjamin Kramer82598ec2013-05-31 16:29:28 +000086 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
Eli Benderskyaefa5e22013-07-23 00:13:01 +000087 }
Chris Lattner15ba9492009-06-14 01:54:56 +000088
Douglas Gregorf89771c2009-04-22 04:56:28 +000089 // Step #2: Register target-specific builtins.
Craig Topper6c03a542015-10-19 04:51:35 +000090 for (unsigned i = 0, e = TSRecords.size(); i != e; ++i)
Eric Christopher02d5d862015-08-06 01:01:12 +000091 if (builtinIsSupported(TSRecords[i], LangOpts))
Artem Belevichb5bc9232015-09-22 17:23:22 +000092 Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
93
94 // Step #3: Register target-specific builtins for AuxTarget.
Craig Topper6c03a542015-10-19 04:51:35 +000095 for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i)
Artem Belevichb5bc9232015-09-22 17:23:22 +000096 Table.get(AuxTSRecords[i].Name)
Craig Topper6c03a542015-10-19 04:51:35 +000097 .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
Chris Lattner9561a0b2007-01-28 08:20:04 +000098}
99
Eric Christopher02d5d862015-08-06 01:01:12 +0000100void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) {
101 Table.get(getRecord(ID).Name).setBuiltinID(0);
Douglas Gregor9246b682010-12-21 19:47:46 +0000102}
103
Aaron Ballman69714e72014-01-03 20:10:54 +0000104bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
105 bool &HasVAListArg, const char *Fmt) const {
106 assert(Fmt && "Not passed a format string");
107 assert(::strlen(Fmt) == 2 &&
108 "Format string needs to be two characters long");
109 assert(::toupper(Fmt[0]) == Fmt[1] &&
110 "Format string is not in the form \"xX\"");
111
Eric Christopher02d5d862015-08-06 01:01:12 +0000112 const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
Aaron Ballman69714e72014-01-03 20:10:54 +0000113 if (!Like)
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000114 return false;
115
Aaron Ballman69714e72014-01-03 20:10:54 +0000116 HasVAListArg = (*Like == Fmt[1]);
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000117
Aaron Ballman69714e72014-01-03 20:10:54 +0000118 ++Like;
119 assert(*Like == ':' && "Format specifier must be followed by a ':'");
120 ++Like;
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000121
Aaron Ballman69714e72014-01-03 20:10:54 +0000122 assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
Craig Topperf1186c52014-05-08 06:41:40 +0000123 FormatIdx = ::strtol(Like, nullptr, 10);
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000124 return true;
125}
126
Aaron Ballman69714e72014-01-03 20:10:54 +0000127bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
128 bool &HasVAListArg) {
129 return isLike(ID, FormatIdx, HasVAListArg, "pP");
Ted Kremenek5932c352010-07-16 02:11:15 +0000130}
131
Aaron Ballman69714e72014-01-03 20:10:54 +0000132bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
133 bool &HasVAListArg) {
134 return isLike(ID, FormatIdx, HasVAListArg, "sS");
135}