blob: 4ca82f38aacb41d3e42a8634bb742d3288112a54 [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 Topperf1186c52014-05-08 06:41:40 +000022 { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES},
Eli Friedmanbb5c9ae2011-07-05 21:53:01 +000023#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
Eric Christopher35869a22015-08-05 21:04:28 +000024#define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
25 { #ID, TYPE, ATTRS, 0, LANGS } \
26 ,
27#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
28 { #ID, TYPE, ATTRS, HEADER, LANGS } \
29 ,
Chris Lattner5abdec72009-06-14 01:05:48 +000030#include "clang/Basic/Builtins.def"
Chris Lattner9561a0b2007-01-28 08:20:04 +000031};
32
Eric Christopher02d5d862015-08-06 01:01:12 +000033const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
Chris Lattner10a5b382007-01-29 05:24:35 +000034 if (ID < Builtin::FirstTSBuiltin)
35 return BuiltinInfo[ID];
36 assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!");
37 return TSRecords[ID - Builtin::FirstTSBuiltin];
Chris Lattner9561a0b2007-01-28 08:20:04 +000038}
39
Douglas Gregor83297df2011-09-01 23:39:15 +000040Builtin::Context::Context() {
Chris Lattner4ef49c12009-06-16 16:18:48 +000041 // Get the target specific builtins from the target.
Craig Topperf1186c52014-05-08 06:41:40 +000042 TSRecords = nullptr;
Chris Lattner651a2212009-06-16 17:27:50 +000043 NumTSRecords = 0;
Douglas Gregor83297df2011-09-01 23:39:15 +000044}
45
Eric Christopher02d5d862015-08-06 01:01:12 +000046void Builtin::Context::initializeTarget(const TargetInfo &Target) {
Douglas Gregor83297df2011-09-01 23:39:15 +000047 assert(NumTSRecords == 0 && "Already initialized target?");
48 Target.getTargetBuiltins(TSRecords, NumTSRecords);
Chris Lattner4ef49c12009-06-16 16:18:48 +000049}
50
Eric Christopher02d5d862015-08-06 01:01:12 +000051bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
Eli Benderskyaefa5e22013-07-23 00:13:01 +000052 const LangOptions &LangOpts) {
53 bool BuiltinsUnsupported = LangOpts.NoBuiltin &&
54 strchr(BuiltinInfo.Attributes, 'f');
55 bool MathBuiltinsUnsupported =
56 LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
57 llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
Eric Christopher35869a22015-08-05 21:04:28 +000058 bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG);
59 bool MSModeUnsupported =
60 !LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG);
61 bool ObjCUnsupported = !LangOpts.ObjC1 && BuiltinInfo.Langs == OBJC_LANG;
Eli Benderskyaefa5e22013-07-23 00:13:01 +000062 return !BuiltinsUnsupported && !MathBuiltinsUnsupported &&
Reid Klecknercf8933d2013-11-13 22:47:22 +000063 !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported;
Eli Benderskyaefa5e22013-07-23 00:13:01 +000064}
65
Chris Lattner9561a0b2007-01-28 08:20:04 +000066/// InitializeBuiltins - Mark the identifiers for all the builtins with their
67/// appropriate builtin ID # and mark any non-portable builtin identifiers as
68/// such.
Eric Christopher02d5d862015-08-06 01:01:12 +000069void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
Fariborz Jahaniane8473c22010-11-30 17:35:24 +000070 const LangOptions& LangOpts) {
Chris Lattner9561a0b2007-01-28 08:20:04 +000071 // Step #1: mark all target-independent builtins with their ID's.
Chris Lattner10a5b382007-01-29 05:24:35 +000072 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Eric Christopher02d5d862015-08-06 01:01:12 +000073 if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
Benjamin Kramer82598ec2013-05-31 16:29:28 +000074 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
Eli Benderskyaefa5e22013-07-23 00:13:01 +000075 }
Chris Lattner15ba9492009-06-14 01:54:56 +000076
Douglas Gregorf89771c2009-04-22 04:56:28 +000077 // Step #2: Register target-specific builtins.
Chris Lattner10a5b382007-01-29 05:24:35 +000078 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
Eric Christopher02d5d862015-08-06 01:01:12 +000079 if (builtinIsSupported(TSRecords[i], LangOpts))
Douglas Gregor69c79512009-02-14 20:49:29 +000080 Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
Chris Lattner9561a0b2007-01-28 08:20:04 +000081}
82
Eric Christopher02d5d862015-08-06 01:01:12 +000083void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) {
84 Table.get(getRecord(ID).Name).setBuiltinID(0);
Douglas Gregor9246b682010-12-21 19:47:46 +000085}
86
Aaron Ballman69714e72014-01-03 20:10:54 +000087bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
88 bool &HasVAListArg, const char *Fmt) const {
89 assert(Fmt && "Not passed a format string");
90 assert(::strlen(Fmt) == 2 &&
91 "Format string needs to be two characters long");
92 assert(::toupper(Fmt[0]) == Fmt[1] &&
93 "Format string is not in the form \"xX\"");
94
Eric Christopher02d5d862015-08-06 01:01:12 +000095 const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
Aaron Ballman69714e72014-01-03 20:10:54 +000096 if (!Like)
Douglas Gregorac5d4c52009-02-14 00:32:47 +000097 return false;
98
Aaron Ballman69714e72014-01-03 20:10:54 +000099 HasVAListArg = (*Like == Fmt[1]);
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000100
Aaron Ballman69714e72014-01-03 20:10:54 +0000101 ++Like;
102 assert(*Like == ':' && "Format specifier must be followed by a ':'");
103 ++Like;
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000104
Aaron Ballman69714e72014-01-03 20:10:54 +0000105 assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
Craig Topperf1186c52014-05-08 06:41:40 +0000106 FormatIdx = ::strtol(Like, nullptr, 10);
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000107 return true;
108}
109
Aaron Ballman69714e72014-01-03 20:10:54 +0000110bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
111 bool &HasVAListArg) {
112 return isLike(ID, FormatIdx, HasVAListArg, "pP");
Ted Kremenek5932c352010-07-16 02:11:15 +0000113}
114
Aaron Ballman69714e72014-01-03 20:10:54 +0000115bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
116 bool &HasVAListArg) {
117 return isLike(ID, FormatIdx, HasVAListArg, "sS");
118}