1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "clang/Basic/Builtins.h" |
14 | #include "clang/Basic/IdentifierTable.h" |
15 | #include "clang/Basic/LangOptions.h" |
16 | #include "clang/Basic/TargetInfo.h" |
17 | #include "llvm/ADT/StringRef.h" |
18 | using namespace clang; |
19 | |
20 | static const Builtin::Info BuiltinInfo[] = { |
21 | { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr}, |
22 | #define BUILTIN(ID, TYPE, ATTRS) \ |
23 | { #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr }, |
24 | #define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \ |
25 | { #ID, TYPE, ATTRS, nullptr, LANGS, nullptr }, |
26 | #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \ |
27 | { #ID, TYPE, ATTRS, HEADER, LANGS, nullptr }, |
28 | #include "clang/Basic/Builtins.def" |
29 | }; |
30 | |
31 | const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const { |
32 | if (ID < Builtin::FirstTSBuiltin) |
33 | return BuiltinInfo[ID]; |
34 | (0) . __assert_fail ("((ID - Builtin..FirstTSBuiltin) < (TSRecords.size() + AuxTSRecords.size())) && \"Invalid builtin ID!\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 36, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(((ID - Builtin::FirstTSBuiltin) < |
35 | (0) . __assert_fail ("((ID - Builtin..FirstTSBuiltin) < (TSRecords.size() + AuxTSRecords.size())) && \"Invalid builtin ID!\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 36, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> (TSRecords.size() + AuxTSRecords.size())) && |
36 | (0) . __assert_fail ("((ID - Builtin..FirstTSBuiltin) < (TSRecords.size() + AuxTSRecords.size())) && \"Invalid builtin ID!\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 36, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid builtin ID!"); |
37 | if (isAuxBuiltinID(ID)) |
38 | return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin]; |
39 | return TSRecords[ID - Builtin::FirstTSBuiltin]; |
40 | } |
41 | |
42 | void Builtin::Context::InitializeTarget(const TargetInfo &Target, |
43 | const TargetInfo *AuxTarget) { |
44 | (0) . __assert_fail ("TSRecords.empty() && \"Already initialized target?\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 44, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(TSRecords.empty() && "Already initialized target?"); |
45 | TSRecords = Target.getTargetBuiltins(); |
46 | if (AuxTarget) |
47 | AuxTSRecords = AuxTarget->getTargetBuiltins(); |
48 | } |
49 | |
50 | bool Builtin::Context::isBuiltinFunc(const char *Name) { |
51 | StringRef FuncName(Name); |
52 | for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; ++i) |
53 | if (FuncName.equals(BuiltinInfo[i].Name)) |
54 | return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr; |
55 | |
56 | return false; |
57 | } |
58 | |
59 | bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo, |
60 | const LangOptions &LangOpts) { |
61 | bool BuiltinsUnsupported = |
62 | (LangOpts.NoBuiltin || LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)) && |
63 | strchr(BuiltinInfo.Attributes, 'f'); |
64 | bool MathBuiltinsUnsupported = |
65 | LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName && |
66 | llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h"); |
67 | bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG); |
68 | bool MSModeUnsupported = |
69 | !LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG); |
70 | bool ObjCUnsupported = !LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG; |
71 | bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 && |
72 | (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG; |
73 | bool OclC2Unsupported = LangOpts.OpenCLVersion != 200 && |
74 | (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG; |
75 | bool OclCUnsupported = !LangOpts.OpenCL && |
76 | (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES); |
77 | bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG; |
78 | return !BuiltinsUnsupported && !MathBuiltinsUnsupported && !OclCUnsupported && |
79 | !OclC1Unsupported && !OclC2Unsupported && !OpenMPUnsupported && |
80 | !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported; |
81 | } |
82 | |
83 | |
84 | |
85 | |
86 | void Builtin::Context::initializeBuiltins(IdentifierTable &Table, |
87 | const LangOptions& LangOpts) { |
88 | |
89 | for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) |
90 | if (builtinIsSupported(BuiltinInfo[i], LangOpts)) { |
91 | Table.get(BuiltinInfo[i].Name).setBuiltinID(i); |
92 | } |
93 | |
94 | |
95 | for (unsigned i = 0, e = TSRecords.size(); i != e; ++i) |
96 | if (builtinIsSupported(TSRecords[i], LangOpts)) |
97 | Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin); |
98 | |
99 | |
100 | for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i) |
101 | Table.get(AuxTSRecords[i].Name) |
102 | .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size()); |
103 | } |
104 | |
105 | void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) { |
106 | Table.get(getRecord(ID).Name).setBuiltinID(0); |
107 | } |
108 | |
109 | unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const { |
110 | const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V'); |
111 | if (!WidthPos) |
112 | return 0; |
113 | |
114 | ++WidthPos; |
115 | (0) . __assert_fail ("*WidthPos == '.' && \"Vector width specifier must be followed by a '.'\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 116, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(*WidthPos == ':' && |
116 | (0) . __assert_fail ("*WidthPos == '.' && \"Vector width specifier must be followed by a '.'\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 116, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Vector width specifier must be followed by a ':'"); |
117 | ++WidthPos; |
118 | |
119 | char *EndPos; |
120 | unsigned Width = ::strtol(WidthPos, &EndPos, 10); |
121 | (0) . __assert_fail ("*EndPos == '.' && \"Vector width specific must end with a '.'\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 121, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(*EndPos == ':' && "Vector width specific must end with a ':'"); |
122 | return Width; |
123 | } |
124 | |
125 | bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx, |
126 | bool &HasVAListArg, const char *Fmt) const { |
127 | (0) . __assert_fail ("Fmt && \"Not passed a format string\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 127, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Fmt && "Not passed a format string"); |
128 | (0) . __assert_fail ("..strlen(Fmt) == 2 && \"Format string needs to be two characters long\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 129, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(::strlen(Fmt) == 2 && |
129 | (0) . __assert_fail ("..strlen(Fmt) == 2 && \"Format string needs to be two characters long\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 129, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Format string needs to be two characters long"); |
130 | (0) . __assert_fail ("..toupper(Fmt[0]) == Fmt[1] && \"Format string is not in the form \\\"xX\\\"\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 131, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(::toupper(Fmt[0]) == Fmt[1] && |
131 | (0) . __assert_fail ("..toupper(Fmt[0]) == Fmt[1] && \"Format string is not in the form \\\"xX\\\"\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 131, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Format string is not in the form \"xX\""); |
132 | |
133 | const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt); |
134 | if (!Like) |
135 | return false; |
136 | |
137 | HasVAListArg = (*Like == Fmt[1]); |
138 | |
139 | ++Like; |
140 | (0) . __assert_fail ("*Like == '.' && \"Format specifier must be followed by a '.'\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 140, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(*Like == ':' && "Format specifier must be followed by a ':'"); |
141 | ++Like; |
142 | |
143 | (0) . __assert_fail ("..strchr(Like, '.') && \"Format specifier must end with a '.'\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 143, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(::strchr(Like, ':') && "Format specifier must end with a ':'"); |
144 | FormatIdx = ::strtol(Like, nullptr, 10); |
145 | return true; |
146 | } |
147 | |
148 | bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx, |
149 | bool &HasVAListArg) { |
150 | return isLike(ID, FormatIdx, HasVAListArg, "pP"); |
151 | } |
152 | |
153 | bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx, |
154 | bool &HasVAListArg) { |
155 | return isLike(ID, FormatIdx, HasVAListArg, "sS"); |
156 | } |
157 | |
158 | bool Builtin::Context::performsCallback(unsigned ID, |
159 | SmallVectorImpl<int> &Encoding) const { |
160 | const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C'); |
161 | if (!CalleePos) |
162 | return false; |
163 | |
164 | ++CalleePos; |
165 | (0) . __assert_fail ("*CalleePos == '<' && \"Callback callee specifier must be followed by a '<'\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 166, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(*CalleePos == '<' && |
166 | (0) . __assert_fail ("*CalleePos == '<' && \"Callback callee specifier must be followed by a '<'\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 166, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Callback callee specifier must be followed by a '<'"); |
167 | ++CalleePos; |
168 | |
169 | char *EndPos; |
170 | int CalleeIdx = ::strtol(CalleePos, &EndPos, 10); |
171 | (0) . __assert_fail ("CalleeIdx >= 0 && \"Callee index is supposed to be positive!\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 171, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!"); |
172 | Encoding.push_back(CalleeIdx); |
173 | |
174 | while (*EndPos == ',') { |
175 | const char *PayloadPos = EndPos + 1; |
176 | |
177 | int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10); |
178 | Encoding.push_back(PayloadIdx); |
179 | } |
180 | |
181 | '") ? static_cast (0) . __assert_fail ("*EndPos == '>' && \"Callback callee specifier must end with a '>'\"", "/home/seafit/code_projects/clang_source/clang/lib/Basic/Builtins.cpp", 181, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(*EndPos == '>' && "Callback callee specifier must end with a '>'"); |
182 | return true; |
183 | } |
184 | |
185 | bool Builtin::Context::canBeRedeclared(unsigned ID) const { |
186 | return ID == Builtin::NotBuiltin || |
187 | ID == Builtin::BI__va_start || |
188 | (!hasReferenceArgsOrResult(ID) && |
189 | !hasCustomTypechecking(ID)); |
190 | } |
191 | |