blob: 3e1b3dc2a1f657e3a51285b9546d1be1418ac4d2 [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
14#include "clang/AST/Builtins.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/Decl.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Basic/TargetInfo.h"
19using namespace clang;
20
21static const Builtin::Info BuiltinInfo[] = {
Douglas Gregorb1152d82009-02-16 21:58:21 +000022 { "not a builtin function", 0, 0, 0, false },
23#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, false },
24#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER, false },
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "clang/AST/Builtins.def"
26};
27
28const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const {
29 if (ID < Builtin::FirstTSBuiltin)
30 return BuiltinInfo[ID];
31 assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!");
32 return TSRecords[ID - Builtin::FirstTSBuiltin];
33}
34
Douglas Gregor71dfdb92009-04-22 04:56:28 +000035/// \brief Load all of the target builtins. This must be called
36/// prior to initializing the builtin identifiers.
37void Builtin::Context::InitializeTargetBuiltins(const TargetInfo &Target) {
38 Target.getTargetBuiltins(TSRecords, NumTSRecords);
39}
Reid Spencer5f016e22007-07-11 17:01:13 +000040
41/// InitializeBuiltins - Mark the identifiers for all the builtins with their
42/// appropriate builtin ID # and mark any non-portable builtin identifiers as
43/// such.
44void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
Chris Lattner7644f072009-03-13 22:38:49 +000045 bool NoBuiltins) {
Reid Spencer5f016e22007-07-11 17:01:13 +000046 // Step #1: mark all target-independent builtins with their ID's.
47 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Douglas Gregor3573c0c2009-02-14 20:49:29 +000048 if (!BuiltinInfo[i].Suppressed &&
Chris Lattner7644f072009-03-13 22:38:49 +000049 (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f')))
Douglas Gregor3573c0c2009-02-14 20:49:29 +000050 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
Reid Spencer5f016e22007-07-11 17:01:13 +000051
Douglas Gregor71dfdb92009-04-22 04:56:28 +000052 // Step #2: Register target-specific builtins.
Reid Spencer5f016e22007-07-11 17:01:13 +000053 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
Douglas Gregor3573c0c2009-02-14 20:49:29 +000054 if (!TSRecords[i].Suppressed &&
Chris Lattner7644f072009-03-13 22:38:49 +000055 (!NoBuiltins ||
Daniel Dunbare8699902009-02-15 18:23:07 +000056 (TSRecords[i].Attributes &&
57 !strchr(TSRecords[i].Attributes, 'f'))))
Douglas Gregor3573c0c2009-02-14 20:49:29 +000058 Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
Reid Spencer5f016e22007-07-11 17:01:13 +000059}
60
Douglas Gregor2deaea32009-04-22 18:49:13 +000061void
62Builtin::Context::GetBuiltinNames(llvm::SmallVectorImpl<const char *> &Names,
63 bool NoBuiltins) {
64 // Final all target-independent names
65 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
66 if (!BuiltinInfo[i].Suppressed &&
67 (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f')))
68 Names.push_back(BuiltinInfo[i].Name);
69
70 // Find target-specific names.
71 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
72 if (!TSRecords[i].Suppressed &&
73 (!NoBuiltins ||
74 (TSRecords[i].Attributes &&
75 !strchr(TSRecords[i].Attributes, 'f'))))
76 Names.push_back(TSRecords[i].Name);
77}
78
Douglas Gregora316e7b2009-02-14 00:32:47 +000079bool
80Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
81 bool &HasVAListArg) {
Cedric Venetea684e62009-02-14 16:15:20 +000082 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
Douglas Gregora316e7b2009-02-14 00:32:47 +000083 if (!Printf)
84 return false;
85
86 HasVAListArg = (*Printf == 'P');
87
88 ++Printf;
89 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
90 ++Printf;
91
Chris Lattner8a778d62009-02-19 06:41:13 +000092 assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
Douglas Gregora316e7b2009-02-14 00:32:47 +000093 FormatIdx = strtol(Printf, 0, 10);
94 return true;
95}
96
Reid Spencer5f016e22007-07-11 17:01:13 +000097/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
98/// pointer over the consumed characters. This returns the resultant type.
Anders Carlssondd1b5162007-11-28 05:19:59 +000099static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000100 Builtin::Context::GetBuiltinTypeError &Error,
Anders Carlssondd1b5162007-11-28 05:19:59 +0000101 bool AllowTypeModifiers = true) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 // Modifiers.
103 bool Long = false, LongLong = false, Signed = false, Unsigned = false;
104
105 // Read the modifiers first.
106 bool Done = false;
107 while (!Done) {
108 switch (*Str++) {
109 default: Done = true; --Str; break;
110 case 'S':
111 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
112 assert(!Signed && "Can't use 'S' modifier multiple times!");
113 Signed = true;
114 break;
115 case 'U':
116 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
117 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
118 Unsigned = true;
119 break;
120 case 'L':
121 assert(!LongLong && "Can't have LLL modifier");
122 if (Long)
123 LongLong = true;
124 else
125 Long = true;
126 break;
127 }
128 }
129
Anders Carlsson71993dd2007-08-17 05:31:46 +0000130 QualType Type;
131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 // Read the base type.
133 switch (*Str++) {
134 default: assert(0 && "Unknown builtin type letter!");
135 case 'v':
Steve Naroffe8770422007-08-08 17:48:34 +0000136 assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'v'!");
Anders Carlsson71993dd2007-08-17 05:31:46 +0000137 Type = Context.VoidTy;
138 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 case 'f':
140 assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'f'!");
Anders Carlsson71993dd2007-08-17 05:31:46 +0000141 Type = Context.FloatTy;
142 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 case 'd':
144 assert(!LongLong && !Signed && !Unsigned && "Bad modifiers used with 'd'!");
145 if (Long)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000146 Type = Context.LongDoubleTy;
147 else
148 Type = Context.DoubleTy;
149 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 case 's':
151 assert(!LongLong && "Bad modifiers used with 's'!");
152 if (Unsigned)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000153 Type = Context.UnsignedShortTy;
154 else
155 Type = Context.ShortTy;
156 break;
Steve Naroffe8770422007-08-08 17:48:34 +0000157 case 'i':
Anders Carlsson142f36d2007-11-27 07:22:09 +0000158 if (LongLong)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000159 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000160 else if (Long)
161 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000162 else if (Unsigned)
163 Type = Context.UnsignedIntTy;
164 else
165 Type = Context.IntTy; // default is signed.
166 break;
167 case 'c':
168 assert(!Long && !LongLong && "Bad modifiers used with 'c'!");
169 if (Signed)
170 Type = Context.SignedCharTy;
171 else if (Unsigned)
172 Type = Context.UnsignedCharTy;
173 else
174 Type = Context.CharTy;
175 break;
Mon P Wang7ae48ee2008-10-18 02:49:28 +0000176 case 'b': // boolean
177 assert(!Long && !Signed && !Unsigned && "Bad modifiers for 'b'!");
178 Type = Context.BoolTy;
179 break;
Chris Lattner52735a02007-10-29 04:18:06 +0000180 case 'z': // size_t.
181 assert(!Long && !Signed && !Unsigned && "Bad modifiers for 'z'!");
182 Type = Context.getSizeType();
183 break;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000184 case 'F':
185 Type = Context.getCFConstantStringType();
186 break;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000187 case 'a':
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000188 Type = Context.getBuiltinVaListType();
Anders Carlsson793680e2007-10-12 23:56:29 +0000189 assert(!Type.isNull() && "builtin va list type not initialized!");
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000190 break;
Eli Friedman6597f982009-01-20 07:46:22 +0000191 case 'A':
192 // This is a "reference" to a va_list; however, what exactly
193 // this means depends on how va_list is defined. There are two
194 // different kinds of va_list: ones passed by value, and ones
195 // passed by reference. An example of a by-value va_list is
196 // x86, where va_list is a char*. An example of by-ref va_list
197 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
198 // we want this argument to be a char*&; for x86-64, we want
199 // it to be a __va_list_tag*.
200 Type = Context.getBuiltinVaListType();
201 assert(!Type.isNull() && "builtin va list type not initialized!");
202 if (Type->isArrayType()) {
203 Type = Context.getArrayDecayedType(Type);
204 } else {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000205 Type = Context.getLValueReferenceType(Type);
Eli Friedman6597f982009-01-20 07:46:22 +0000206 }
207 break;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000208 case 'V': {
209 char *End;
210
211 unsigned NumElements = strtoul(Str, &End, 10);
212 assert(End != Str && "Missing vector size");
213
214 Str = End;
215
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000216 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
Anders Carlsson142f36d2007-11-27 07:22:09 +0000217 Type = Context.getVectorType(ElementType, NumElements);
218 break;
219 }
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000220 case 'P': {
221 IdentifierInfo *II = &Context.Idents.get("FILE");
222 DeclContext::lookup_result Lookup
Douglas Gregor6ab35242009-04-09 21:40:53 +0000223 = Context.getTranslationUnitDecl()->lookup(Context, II);
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000224 if (Lookup.first != Lookup.second && isa<TypeDecl>(*Lookup.first)) {
225 Type = Context.getTypeDeclType(cast<TypeDecl>(*Lookup.first));
226 break;
227 }
228 else {
229 Error = Builtin::Context::GE_Missing_FILE;
230 return QualType();
231 }
232 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 }
Anders Carlsson71993dd2007-08-17 05:31:46 +0000234
Anders Carlssondd1b5162007-11-28 05:19:59 +0000235 if (!AllowTypeModifiers)
236 return Type;
237
Anders Carlsson71993dd2007-08-17 05:31:46 +0000238 Done = false;
239 while (!Done) {
240 switch (*Str++) {
Anders Carlssondd1b5162007-11-28 05:19:59 +0000241 default: Done = true; --Str; break;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000242 case '*':
243 Type = Context.getPointerType(Type);
244 break;
Anders Carlsson793680e2007-10-12 23:56:29 +0000245 case '&':
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000246 Type = Context.getLValueReferenceType(Type);
Anders Carlsson793680e2007-10-12 23:56:29 +0000247 break;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000248 // FIXME: There's no way to have a built-in with an rvalue ref arg.
Anders Carlsson71993dd2007-08-17 05:31:46 +0000249 case 'C':
250 Type = Type.getQualifiedType(QualType::Const);
251 break;
252 }
253 }
254
255 return Type;
Reid Spencer5f016e22007-07-11 17:01:13 +0000256}
257
258/// GetBuiltinType - Return the type for the specified builtin.
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000259QualType Builtin::Context::GetBuiltinType(unsigned id, ASTContext &Context,
260 GetBuiltinTypeError &Error) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 const char *TypeStr = GetRecord(id).Type;
262
263 llvm::SmallVector<QualType, 8> ArgTypes;
264
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000265 Error = GE_None;
266 QualType ResType = DecodeTypeFromStr(TypeStr, Context, Error);
267 if (Error != GE_None)
268 return QualType();
Chris Lattnerf77d5452008-09-29 22:28:25 +0000269 while (TypeStr[0] && TypeStr[0] != '.') {
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000270 QualType Ty = DecodeTypeFromStr(TypeStr, Context, Error);
271 if (Error != GE_None)
272 return QualType();
273
Chris Lattnerf77d5452008-09-29 22:28:25 +0000274 // Do array -> pointer decay. The builtin should use the decayed type.
275 if (Ty->isArrayType())
276 Ty = Context.getArrayDecayedType(Ty);
277
278 ArgTypes.push_back(Ty);
279 }
Anders Carlssondd1b5162007-11-28 05:19:59 +0000280
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
282 "'.' should only occur at end of builtin type list!");
Steve Naroffe8770422007-08-08 17:48:34 +0000283
284 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
285 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
Douglas Gregor72564e72009-02-26 23:50:07 +0000286 return Context.getFunctionNoProtoType(ResType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 return Context.getFunctionType(ResType, &ArgTypes[0], ArgTypes.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000288 TypeStr[0] == '.', 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000289}