blob: 2fd00dd0b44e48ebb1da76b0285e2d2eb7404e49 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Builtins.cpp - Builtin function implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements various things for builtin functions.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6b15cdc2009-06-14 01:05:48 +000014#include "clang/Basic/Builtins.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000015#include "clang/Basic/IdentifierTable.h"
Fariborz Jahanian67aba812010-11-30 17:35:24 +000016#include "clang/Basic/LangOptions.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/TargetInfo.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000018#include "llvm/ADT/SmallVector.h"
Eli Bendersky264d2062013-07-23 00:13:01 +000019#include "llvm/ADT/StringRef.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
22static const Builtin::Info BuiltinInfo[] = {
Eli Friedmane7e66f72011-07-05 21:53:01 +000023 { "not a builtin function", 0, 0, 0, ALL_LANGUAGES },
24#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
Reid Klecknerf2941ec2013-11-13 22:47:22 +000025#define LANGBUILTIN(ID, TYPE, ATTRS, BUILTIN_LANG) { #ID, TYPE, ATTRS, 0, BUILTIN_LANG },
Fariborz Jahanian67aba812010-11-30 17:35:24 +000026#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\
Eli Friedmane7e66f72011-07-05 21:53:01 +000027 BUILTIN_LANG },
Chris Lattner6b15cdc2009-06-14 01:05:48 +000028#include "clang/Basic/Builtins.def"
Reid Spencer5f016e22007-07-11 17:01:13 +000029};
30
31const 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];
36}
37
Douglas Gregor998b3d32011-09-01 23:39:15 +000038Builtin::Context::Context() {
Chris Lattner030e8fe2009-06-16 16:18:48 +000039 // Get the target specific builtins from the target.
Chris Lattnerff1d4d92009-06-16 17:27:50 +000040 TSRecords = 0;
41 NumTSRecords = 0;
Douglas Gregor998b3d32011-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 Lattner030e8fe2009-06-16 16:18:48 +000047}
48
Eli Bendersky264d2062013-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 Klecknerf2941ec2013-11-13 22:47:22 +000058 bool MSModeUnsupported = !LangOpts.MicrosoftExt &&
59 (BuiltinInfo.builtin_lang & MS_LANG);
Eli Bendersky264d2062013-07-23 00:13:01 +000060 bool ObjCUnsupported = !LangOpts.ObjC1 &&
61 BuiltinInfo.builtin_lang == OBJC_LANG;
62 return !BuiltinsUnsupported && !MathBuiltinsUnsupported &&
Reid Klecknerf2941ec2013-11-13 22:47:22 +000063 !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported;
Eli Bendersky264d2062013-07-23 00:13:01 +000064}
65
Reid Spencer5f016e22007-07-11 17:01:13 +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.
69void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
Fariborz Jahanian67aba812010-11-30 17:35:24 +000070 const LangOptions& LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +000071 // Step #1: mark all target-independent builtins with their ID's.
72 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Eli Bendersky264d2062013-07-23 00:13:01 +000073 if (BuiltinIsSupported(BuiltinInfo[i], LangOpts)) {
Benjamin Kramerc89f02a2013-05-31 16:29:28 +000074 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
Eli Bendersky264d2062013-07-23 00:13:01 +000075 }
Chris Lattner1b63e4f2009-06-14 01:54:56 +000076
Douglas Gregor71dfdb92009-04-22 04:56:28 +000077 // Step #2: Register target-specific builtins.
Reid Spencer5f016e22007-07-11 17:01:13 +000078 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
Bill Wendlingad017fa2012-12-20 19:22:21 +000079 if (!LangOpts.NoBuiltin || !strchr(TSRecords[i].Attributes, 'f'))
Douglas Gregor3573c0c2009-02-14 20:49:29 +000080 Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
Reid Spencer5f016e22007-07-11 17:01:13 +000081}
82
Mike Stump1eb44332009-09-09 15:08:12 +000083void
Eli Bendersky97a03cf2013-07-11 16:53:04 +000084Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names) {
Douglas Gregor2deaea32009-04-22 18:49:13 +000085 // Final all target-independent names
86 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Eli Bendersky97a03cf2013-07-11 16:53:04 +000087 if (!strchr(BuiltinInfo[i].Attributes, 'f'))
Douglas Gregor2deaea32009-04-22 18:49:13 +000088 Names.push_back(BuiltinInfo[i].Name);
Mike Stump1eb44332009-09-09 15:08:12 +000089
Douglas Gregor2deaea32009-04-22 18:49:13 +000090 // Find target-specific names.
91 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
Eli Bendersky97a03cf2013-07-11 16:53:04 +000092 if (!strchr(TSRecords[i].Attributes, 'f'))
Douglas Gregor2deaea32009-04-22 18:49:13 +000093 Names.push_back(TSRecords[i].Name);
94}
95
Douglas Gregorb68e3992010-12-21 19:47:46 +000096void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
97 Table.get(GetRecord(ID).Name).setBuiltinID(0);
98}
99
Stephen Hines651f13c2014-04-23 16:59:28 -0700100bool 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 Gregora316e7b2009-02-14 00:32:47 +0000110 return false;
111
Stephen Hines651f13c2014-04-23 16:59:28 -0700112 HasVAListArg = (*Like == Fmt[1]);
Douglas Gregora316e7b2009-02-14 00:32:47 +0000113
Stephen Hines651f13c2014-04-23 16:59:28 -0700114 ++Like;
115 assert(*Like == ':' && "Format specifier must be followed by a ':'");
116 ++Like;
Douglas Gregora316e7b2009-02-14 00:32:47 +0000117
Stephen Hines651f13c2014-04-23 16:59:28 -0700118 assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
119 FormatIdx = ::strtol(Like, 0, 10);
Douglas Gregora316e7b2009-02-14 00:32:47 +0000120 return true;
121}
122
Stephen Hines651f13c2014-04-23 16:59:28 -0700123bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
124 bool &HasVAListArg) {
125 return isLike(ID, FormatIdx, HasVAListArg, "pP");
Ted Kremenekbee05c12010-07-16 02:11:15 +0000126}
127
Stephen Hines651f13c2014-04-23 16:59:28 -0700128bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
129 bool &HasVAListArg) {
130 return isLike(ID, FormatIdx, HasVAListArg, "sS");
131}