blob: 6c78dc359405f07f03d945ac591750f2a9936b17 [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"
Benjamin Kramer49038022012-02-04 13:45:25 +000018#include "llvm/ADT/SmallVector.h"
Eli Benderskyaefa5e22013-07-23 00:13:01 +000019#include "llvm/ADT/StringRef.h"
Chris Lattner9561a0b2007-01-28 08:20:04 +000020using namespace clang;
21
22static const Builtin::Info BuiltinInfo[] = {
Craig Topperf1186c52014-05-08 06:41:40 +000023 { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES},
Eli Friedmanbb5c9ae2011-07-05 21:53:01 +000024#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
Reid Klecknercf8933d2013-11-13 22:47:22 +000025#define LANGBUILTIN(ID, TYPE, ATTRS, BUILTIN_LANG) { #ID, TYPE, ATTRS, 0, BUILTIN_LANG },
Fariborz Jahaniane8473c22010-11-30 17:35:24 +000026#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\
Eli Friedmanbb5c9ae2011-07-05 21:53:01 +000027 BUILTIN_LANG },
Chris Lattner5abdec72009-06-14 01:05:48 +000028#include "clang/Basic/Builtins.def"
Chris Lattner9561a0b2007-01-28 08:20:04 +000029};
30
Chris Lattner10a5b382007-01-29 05:24:35 +000031const 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];
Chris Lattner9561a0b2007-01-28 08:20:04 +000036}
37
Douglas Gregor83297df2011-09-01 23:39:15 +000038Builtin::Context::Context() {
Chris Lattner4ef49c12009-06-16 16:18:48 +000039 // Get the target specific builtins from the target.
Craig Topperf1186c52014-05-08 06:41:40 +000040 TSRecords = nullptr;
Chris Lattner651a2212009-06-16 17:27:50 +000041 NumTSRecords = 0;
Douglas Gregor83297df2011-09-01 23:39:15 +000042}
43
44void Builtin::Context::InitializeTarget(const TargetInfo &Target) {
45 assert(NumTSRecords == 0 && "Already initialized target?");
46 Target.getTargetBuiltins(TSRecords, NumTSRecords);
Chris Lattner4ef49c12009-06-16 16:18:48 +000047}
48
Eli Benderskyaefa5e22013-07-23 00:13:01 +000049bool 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);
Reid Klecknercf8933d2013-11-13 22:47:22 +000058 bool MSModeUnsupported = !LangOpts.MicrosoftExt &&
59 (BuiltinInfo.builtin_lang & MS_LANG);
Eli Benderskyaefa5e22013-07-23 00:13:01 +000060 bool ObjCUnsupported = !LangOpts.ObjC1 &&
61 BuiltinInfo.builtin_lang == OBJC_LANG;
62 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.
Chris Lattner10a5b382007-01-29 05:24:35 +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)
Eli Benderskyaefa5e22013-07-23 00:13:01 +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)
Bill Wendling44426052012-12-20 19:22:21 +000079 if (!LangOpts.NoBuiltin || !strchr(TSRecords[i].Attributes, 'f'))
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
Mike Stump11289f42009-09-09 15:08:12 +000083void
Eli Benderskye3cef2a2013-07-11 16:53:04 +000084Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +000085 // Final all target-independent names
86 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Eli Benderskye3cef2a2013-07-11 16:53:04 +000087 if (!strchr(BuiltinInfo[i].Attributes, 'f'))
Douglas Gregor4621c6a2009-04-22 18:49:13 +000088 Names.push_back(BuiltinInfo[i].Name);
Mike Stump11289f42009-09-09 15:08:12 +000089
Douglas Gregor4621c6a2009-04-22 18:49:13 +000090 // Find target-specific names.
91 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
Eli Benderskye3cef2a2013-07-11 16:53:04 +000092 if (!strchr(TSRecords[i].Attributes, 'f'))
Douglas Gregor4621c6a2009-04-22 18:49:13 +000093 Names.push_back(TSRecords[i].Name);
94}
95
Douglas Gregor9246b682010-12-21 19:47:46 +000096void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
97 Table.get(GetRecord(ID).Name).setBuiltinID(0);
98}
99
Aaron Ballman69714e72014-01-03 20:10:54 +0000100bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
101 bool &HasVAListArg, const char *Fmt) const {
102 assert(Fmt && "Not passed a format string");
103 assert(::strlen(Fmt) == 2 &&
104 "Format string needs to be two characters long");
105 assert(::toupper(Fmt[0]) == Fmt[1] &&
106 "Format string is not in the form \"xX\"");
107
108 const char *Like = ::strpbrk(GetRecord(ID).Attributes, Fmt);
109 if (!Like)
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000110 return false;
111
Aaron Ballman69714e72014-01-03 20:10:54 +0000112 HasVAListArg = (*Like == Fmt[1]);
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000113
Aaron Ballman69714e72014-01-03 20:10:54 +0000114 ++Like;
115 assert(*Like == ':' && "Format specifier must be followed by a ':'");
116 ++Like;
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000117
Aaron Ballman69714e72014-01-03 20:10:54 +0000118 assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
Craig Topperf1186c52014-05-08 06:41:40 +0000119 FormatIdx = ::strtol(Like, nullptr, 10);
Douglas Gregorac5d4c52009-02-14 00:32:47 +0000120 return true;
121}
122
Aaron Ballman69714e72014-01-03 20:10:54 +0000123bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
124 bool &HasVAListArg) {
125 return isLike(ID, FormatIdx, HasVAListArg, "pP");
Ted Kremenek5932c352010-07-16 02:11:15 +0000126}
127
Aaron Ballman69714e72014-01-03 20:10:54 +0000128bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
129 bool &HasVAListArg) {
130 return isLike(ID, FormatIdx, HasVAListArg, "sS");
131}