Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 1 | //===--- Builtins.cpp - Builtin function implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements various things for builtin functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 5abdec7 | 2009-06-14 01:05:48 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Builtins.h" |
Chris Lattner | ef6b136 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 15 | #include "clang/Basic/IdentifierTable.h" |
Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 16 | #include "clang/Basic/LangOptions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Eli Bendersky | aefa5e2 | 2013-07-23 00:13:01 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringRef.h" |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
| 21 | static const Builtin::Info BuiltinInfo[] = { |
Craig Topper | 07d3b62 | 2015-08-07 05:14:44 +0000 | [diff] [blame] | 22 | { "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 Christopher | 35869a2 | 2015-08-05 21:04:28 +0000 | [diff] [blame] | 25 | #define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \ |
Craig Topper | 07d3b62 | 2015-08-07 05:14:44 +0000 | [diff] [blame] | 26 | { #ID, TYPE, ATTRS, nullptr, LANGS, nullptr }, |
Eric Christopher | 35869a2 | 2015-08-05 21:04:28 +0000 | [diff] [blame] | 27 | #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \ |
Craig Topper | 07d3b62 | 2015-08-07 05:14:44 +0000 | [diff] [blame] | 28 | { #ID, TYPE, ATTRS, HEADER, LANGS, nullptr }, |
Chris Lattner | 5abdec7 | 2009-06-14 01:05:48 +0000 | [diff] [blame] | 29 | #include "clang/Basic/Builtins.def" |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 32 | const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const { |
Chris Lattner | 10a5b38 | 2007-01-29 05:24:35 +0000 | [diff] [blame] | 33 | if (ID < Builtin::FirstTSBuiltin) |
| 34 | return BuiltinInfo[ID]; |
Craig Topper | 6c03a54 | 2015-10-19 04:51:35 +0000 | [diff] [blame] | 35 | assert(((ID - Builtin::FirstTSBuiltin) < |
| 36 | (TSRecords.size() + AuxTSRecords.size())) && |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 37 | "Invalid builtin ID!"); |
| 38 | if (isAuxBuiltinID(ID)) |
| 39 | return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin]; |
Chris Lattner | 10a5b38 | 2007-01-29 05:24:35 +0000 | [diff] [blame] | 40 | return TSRecords[ID - Builtin::FirstTSBuiltin]; |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 43 | void Builtin::Context::InitializeTarget(const TargetInfo &Target, |
| 44 | const TargetInfo *AuxTarget) { |
Craig Topper | 6c03a54 | 2015-10-19 04:51:35 +0000 | [diff] [blame] | 45 | assert(TSRecords.empty() && "Already initialized target?"); |
| 46 | TSRecords = Target.getTargetBuiltins(); |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 47 | if (AuxTarget) |
Craig Topper | 6c03a54 | 2015-10-19 04:51:35 +0000 | [diff] [blame] | 48 | AuxTSRecords = AuxTarget->getTargetBuiltins(); |
Chris Lattner | 4ef49c1 | 2009-06-16 16:18:48 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Chad Rosier | 7dbc9cf | 2016-01-06 14:35:46 +0000 | [diff] [blame] | 51 | bool 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 Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 60 | bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo, |
Eli Bendersky | aefa5e2 | 2013-07-23 00:13:01 +0000 | [diff] [blame] | 61 | const LangOptions &LangOpts) { |
Chad Rosier | 7dbc9cf | 2016-01-06 14:35:46 +0000 | [diff] [blame] | 62 | bool BuiltinsUnsupported = |
| 63 | (LangOpts.NoBuiltin || LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)) && |
| 64 | strchr(BuiltinInfo.Attributes, 'f'); |
Eli Bendersky | aefa5e2 | 2013-07-23 00:13:01 +0000 | [diff] [blame] | 65 | bool MathBuiltinsUnsupported = |
Rachel Craik | f55d058 | 2015-09-14 14:08:18 +0000 | [diff] [blame] | 66 | LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName && |
Eli Bendersky | aefa5e2 | 2013-07-23 00:13:01 +0000 | [diff] [blame] | 67 | llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h"); |
Eric Christopher | 35869a2 | 2015-08-05 21:04:28 +0000 | [diff] [blame] | 68 | 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 Stulova | 7f8d6dc | 2016-07-04 16:07:18 +0000 | [diff] [blame] | 72 | bool OclCUnsupported = LangOpts.OpenCLVersion != 200 && |
| 73 | BuiltinInfo.Langs == OCLC20_LANG; |
Xiuli Pan | bb4d8d3 | 2016-01-26 04:03:48 +0000 | [diff] [blame] | 74 | return !BuiltinsUnsupported && !MathBuiltinsUnsupported && !OclCUnsupported && |
Reid Kleckner | cf8933d | 2013-11-13 22:47:22 +0000 | [diff] [blame] | 75 | !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported; |
Eli Bendersky | aefa5e2 | 2013-07-23 00:13:01 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Ismail Donmez | a8d3a2e | 2015-09-15 09:53:14 +0000 | [diff] [blame] | 78 | /// initializeBuiltins - Mark the identifiers for all the builtins with their |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 79 | /// appropriate builtin ID # and mark any non-portable builtin identifiers as |
| 80 | /// such. |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 81 | void Builtin::Context::initializeBuiltins(IdentifierTable &Table, |
Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 82 | const LangOptions& LangOpts) { |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 83 | // Step #1: mark all target-independent builtins with their ID's. |
Chris Lattner | 10a5b38 | 2007-01-29 05:24:35 +0000 | [diff] [blame] | 84 | for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 85 | if (builtinIsSupported(BuiltinInfo[i], LangOpts)) { |
Benjamin Kramer | 82598ec | 2013-05-31 16:29:28 +0000 | [diff] [blame] | 86 | Table.get(BuiltinInfo[i].Name).setBuiltinID(i); |
Eli Bendersky | aefa5e2 | 2013-07-23 00:13:01 +0000 | [diff] [blame] | 87 | } |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 88 | |
Douglas Gregor | f89771c | 2009-04-22 04:56:28 +0000 | [diff] [blame] | 89 | // Step #2: Register target-specific builtins. |
Craig Topper | 6c03a54 | 2015-10-19 04:51:35 +0000 | [diff] [blame] | 90 | for (unsigned i = 0, e = TSRecords.size(); i != e; ++i) |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 91 | if (builtinIsSupported(TSRecords[i], LangOpts)) |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 92 | Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin); |
| 93 | |
| 94 | // Step #3: Register target-specific builtins for AuxTarget. |
Craig Topper | 6c03a54 | 2015-10-19 04:51:35 +0000 | [diff] [blame] | 95 | for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i) |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 96 | Table.get(AuxTSRecords[i].Name) |
Craig Topper | 6c03a54 | 2015-10-19 04:51:35 +0000 | [diff] [blame] | 97 | .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size()); |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 100 | void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) { |
| 101 | Table.get(getRecord(ID).Name).setBuiltinID(0); |
Douglas Gregor | 9246b68 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 104 | bool 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 Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 112 | const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt); |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 113 | if (!Like) |
Douglas Gregor | ac5d4c5 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 114 | return false; |
| 115 | |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 116 | HasVAListArg = (*Like == Fmt[1]); |
Douglas Gregor | ac5d4c5 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 117 | |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 118 | ++Like; |
| 119 | assert(*Like == ':' && "Format specifier must be followed by a ':'"); |
| 120 | ++Like; |
Douglas Gregor | ac5d4c5 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 121 | |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 122 | assert(::strchr(Like, ':') && "Format specifier must end with a ':'"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 123 | FormatIdx = ::strtol(Like, nullptr, 10); |
Douglas Gregor | ac5d4c5 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 124 | return true; |
| 125 | } |
| 126 | |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 127 | bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx, |
| 128 | bool &HasVAListArg) { |
| 129 | return isLike(ID, FormatIdx, HasVAListArg, "pP"); |
Ted Kremenek | 5932c35 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 132 | bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx, |
| 133 | bool &HasVAListArg) { |
| 134 | return isLike(ID, FormatIdx, HasVAListArg, "sS"); |
| 135 | } |