Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Builtins.cpp - Builtin function implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements various things for builtin functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 6b15cdc | 2009-06-14 01:05:48 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Builtins.h" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 15 | #include "clang/Basic/IdentifierTable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 17 | #include "clang/Basic/LangOptions.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
| 21 | static const Builtin::Info BuiltinInfo[] = { |
Eli Friedman | e7e66f7 | 2011-07-05 21:53:01 +0000 | [diff] [blame] | 22 | { "not a builtin function", 0, 0, 0, ALL_LANGUAGES }, |
| 23 | #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES }, |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 24 | #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\ |
Eli Friedman | e7e66f7 | 2011-07-05 21:53:01 +0000 | [diff] [blame] | 25 | BUILTIN_LANG }, |
Chris Lattner | 6b15cdc | 2009-06-14 01:05:48 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Builtins.def" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const { |
| 30 | if (ID < Builtin::FirstTSBuiltin) |
| 31 | return BuiltinInfo[ID]; |
| 32 | assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!"); |
| 33 | return TSRecords[ID - Builtin::FirstTSBuiltin]; |
| 34 | } |
| 35 | |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 36 | Builtin::Context::Context() { |
Chris Lattner | 030e8fe | 2009-06-16 16:18:48 +0000 | [diff] [blame] | 37 | // Get the target specific builtins from the target. |
Chris Lattner | ff1d4d9 | 2009-06-16 17:27:50 +0000 | [diff] [blame] | 38 | TSRecords = 0; |
| 39 | NumTSRecords = 0; |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void Builtin::Context::InitializeTarget(const TargetInfo &Target) { |
| 43 | assert(NumTSRecords == 0 && "Already initialized target?"); |
| 44 | Target.getTargetBuiltins(TSRecords, NumTSRecords); |
Chris Lattner | 030e8fe | 2009-06-16 16:18:48 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | /// InitializeBuiltins - Mark the identifiers for all the builtins with their |
| 48 | /// appropriate builtin ID # and mark any non-portable builtin identifiers as |
| 49 | /// such. |
| 50 | void Builtin::Context::InitializeBuiltins(IdentifierTable &Table, |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 51 | const LangOptions& LangOpts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | // Step #1: mark all target-independent builtins with their ID's. |
| 53 | for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) |
Eli Friedman | e7e66f7 | 2011-07-05 21:53:01 +0000 | [diff] [blame] | 54 | if (!LangOpts.NoBuiltin || !strchr(BuiltinInfo[i].Attributes, 'f')) { |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 55 | if (LangOpts.ObjC1 || |
| 56 | BuiltinInfo[i].builtin_lang != clang::OBJC_LANG) |
| 57 | Table.get(BuiltinInfo[i].Name).setBuiltinID(i); |
| 58 | } |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 59 | |
Douglas Gregor | 71dfdb9 | 2009-04-22 04:56:28 +0000 | [diff] [blame] | 60 | // Step #2: Register target-specific builtins. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | for (unsigned i = 0, e = NumTSRecords; i != e; ++i) |
Eli Friedman | e7e66f7 | 2011-07-05 21:53:01 +0000 | [diff] [blame] | 62 | if (!LangOpts.NoBuiltin || !strchr(TSRecords[i].Attributes, 'f')) |
Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 63 | Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | void |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 67 | Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names, |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 68 | bool NoBuiltins) { |
| 69 | // Final all target-independent names |
| 70 | for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) |
Eli Friedman | e7e66f7 | 2011-07-05 21:53:01 +0000 | [diff] [blame] | 71 | if (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f')) |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 72 | Names.push_back(BuiltinInfo[i].Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 74 | // Find target-specific names. |
| 75 | for (unsigned i = 0, e = NumTSRecords; i != e; ++i) |
Eli Friedman | e7e66f7 | 2011-07-05 21:53:01 +0000 | [diff] [blame] | 76 | if (!NoBuiltins || !strchr(TSRecords[i].Attributes, 'f')) |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 77 | Names.push_back(TSRecords[i].Name); |
| 78 | } |
| 79 | |
Douglas Gregor | b68e399 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 80 | void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) { |
| 81 | Table.get(GetRecord(ID).Name).setBuiltinID(0); |
| 82 | } |
| 83 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | bool |
| 85 | Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx, |
Douglas Gregor | a316e7b | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 86 | bool &HasVAListArg) { |
Cedric Venet | ea684e6 | 2009-02-14 16:15:20 +0000 | [diff] [blame] | 87 | const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP"); |
Douglas Gregor | a316e7b | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 88 | if (!Printf) |
| 89 | return false; |
| 90 | |
| 91 | HasVAListArg = (*Printf == 'P'); |
| 92 | |
| 93 | ++Printf; |
| 94 | assert(*Printf == ':' && "p or P specifier must have be followed by a ':'"); |
| 95 | ++Printf; |
| 96 | |
Chris Lattner | 8a778d6 | 2009-02-19 06:41:13 +0000 | [diff] [blame] | 97 | assert(strchr(Printf, ':') && "printf specifier must end with a ':'"); |
Douglas Gregor | a316e7b | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 98 | FormatIdx = strtol(Printf, 0, 10); |
| 99 | return true; |
| 100 | } |
| 101 | |
Ted Kremenek | bee05c1 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 102 | // FIXME: Refactor with isPrintfLike. |
| 103 | bool |
| 104 | Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx, |
| 105 | bool &HasVAListArg) { |
| 106 | const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS"); |
| 107 | if (!Scanf) |
| 108 | return false; |
| 109 | |
| 110 | HasVAListArg = (*Scanf == 'S'); |
| 111 | |
| 112 | ++Scanf; |
| 113 | assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'"); |
| 114 | ++Scanf; |
| 115 | |
| 116 | assert(strchr(Scanf, ':') && "printf specifier must end with a ':'"); |
| 117 | FormatIdx = strtol(Scanf, 0, 10); |
| 118 | return true; |
| 119 | } |
| 120 | |