blob: 4a8a338e6374ddd406bc96098b38289e0c763614 [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 Gregor3573c0c2009-02-14 20:49:29 +000022 { "not a builtin function", 0, 0, false },
23#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, false },
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/AST/Builtins.def"
25};
26
27const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const {
28 if (ID < Builtin::FirstTSBuiltin)
29 return BuiltinInfo[ID];
30 assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!");
31 return TSRecords[ID - Builtin::FirstTSBuiltin];
32}
33
34
35/// InitializeBuiltins - Mark the identifiers for all the builtins with their
36/// appropriate builtin ID # and mark any non-portable builtin identifiers as
37/// such.
38void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
Douglas Gregor3573c0c2009-02-14 20:49:29 +000039 const TargetInfo &Target,
40 bool Freestanding) {
Reid Spencer5f016e22007-07-11 17:01:13 +000041 // Step #1: mark all target-independent builtins with their ID's.
42 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Douglas Gregor3573c0c2009-02-14 20:49:29 +000043 if (!BuiltinInfo[i].Suppressed &&
44 (!Freestanding ||
45 !strchr(BuiltinInfo[i].Attributes, 'f')))
46 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
Reid Spencer5f016e22007-07-11 17:01:13 +000047
Chris Lattner42e67372008-03-05 01:18:20 +000048 // Step #2: Get target builtins.
49 Target.getTargetBuiltins(TSRecords, NumTSRecords);
Reid Spencer5f016e22007-07-11 17:01:13 +000050
Chris Lattner42e67372008-03-05 01:18:20 +000051 // Step #3: Register target-specific builtins.
Reid Spencer5f016e22007-07-11 17:01:13 +000052 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
Douglas Gregor3573c0c2009-02-14 20:49:29 +000053 if (!TSRecords[i].Suppressed &&
54 (!Freestanding ||
Daniel Dunbare8699902009-02-15 18:23:07 +000055 (TSRecords[i].Attributes &&
56 !strchr(TSRecords[i].Attributes, 'f'))))
Douglas Gregor3573c0c2009-02-14 20:49:29 +000057 Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
Reid Spencer5f016e22007-07-11 17:01:13 +000058}
59
Douglas Gregora316e7b2009-02-14 00:32:47 +000060std::string Builtin::Context::getHeaderName(unsigned ID) const {
Cedric Venetea684e62009-02-14 16:15:20 +000061 const char *Name = strchr(GetRecord(ID).Attributes, 'f');
Douglas Gregora316e7b2009-02-14 00:32:47 +000062 if (!Name)
63 return 0;
64 ++Name;
65
66 if (*Name != ':')
67 return 0;
68
69 ++Name;
Cedric Venetea684e62009-02-14 16:15:20 +000070 const char *NameEnd = strchr(Name, ':');
Douglas Gregora316e7b2009-02-14 00:32:47 +000071 assert(NameEnd && "Missing ':' after header name");
72 return std::string(Name, NameEnd);
73}
74
75bool
76Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
77 bool &HasVAListArg) {
Cedric Venetea684e62009-02-14 16:15:20 +000078 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
Douglas Gregora316e7b2009-02-14 00:32:47 +000079 if (!Printf)
80 return false;
81
82 HasVAListArg = (*Printf == 'P');
83
84 ++Printf;
85 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
86 ++Printf;
87
Cedric Venetea684e62009-02-14 16:15:20 +000088 const char *PrintfEnd = strchr(Printf, ':');
Douglas Gregora316e7b2009-02-14 00:32:47 +000089 assert(PrintfEnd && "printf specifier must end with a ':'");
90
91 FormatIdx = strtol(Printf, 0, 10);
92 return true;
93}
94
Reid Spencer5f016e22007-07-11 17:01:13 +000095/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
96/// pointer over the consumed characters. This returns the resultant type.
Anders Carlssondd1b5162007-11-28 05:19:59 +000097static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Douglas Gregor370ab3f2009-02-14 01:52:53 +000098 Builtin::Context::GetBuiltinTypeError &Error,
Anders Carlssondd1b5162007-11-28 05:19:59 +000099 bool AllowTypeModifiers = true) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 // Modifiers.
101 bool Long = false, LongLong = false, Signed = false, Unsigned = false;
102
103 // Read the modifiers first.
104 bool Done = false;
105 while (!Done) {
106 switch (*Str++) {
107 default: Done = true; --Str; break;
108 case 'S':
109 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
110 assert(!Signed && "Can't use 'S' modifier multiple times!");
111 Signed = true;
112 break;
113 case 'U':
114 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
115 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
116 Unsigned = true;
117 break;
118 case 'L':
119 assert(!LongLong && "Can't have LLL modifier");
120 if (Long)
121 LongLong = true;
122 else
123 Long = true;
124 break;
125 }
126 }
127
Anders Carlsson71993dd2007-08-17 05:31:46 +0000128 QualType Type;
129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 // Read the base type.
131 switch (*Str++) {
132 default: assert(0 && "Unknown builtin type letter!");
133 case 'v':
Steve Naroffe8770422007-08-08 17:48:34 +0000134 assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'v'!");
Anders Carlsson71993dd2007-08-17 05:31:46 +0000135 Type = Context.VoidTy;
136 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 case 'f':
138 assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'f'!");
Anders Carlsson71993dd2007-08-17 05:31:46 +0000139 Type = Context.FloatTy;
140 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 case 'd':
142 assert(!LongLong && !Signed && !Unsigned && "Bad modifiers used with 'd'!");
143 if (Long)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000144 Type = Context.LongDoubleTy;
145 else
146 Type = Context.DoubleTy;
147 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 case 's':
149 assert(!LongLong && "Bad modifiers used with 's'!");
150 if (Unsigned)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000151 Type = Context.UnsignedShortTy;
152 else
153 Type = Context.ShortTy;
154 break;
Steve Naroffe8770422007-08-08 17:48:34 +0000155 case 'i':
Anders Carlsson142f36d2007-11-27 07:22:09 +0000156 if (LongLong)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000157 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000158 else if (Long)
159 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000160 else if (Unsigned)
161 Type = Context.UnsignedIntTy;
162 else
163 Type = Context.IntTy; // default is signed.
164 break;
165 case 'c':
166 assert(!Long && !LongLong && "Bad modifiers used with 'c'!");
167 if (Signed)
168 Type = Context.SignedCharTy;
169 else if (Unsigned)
170 Type = Context.UnsignedCharTy;
171 else
172 Type = Context.CharTy;
173 break;
Mon P Wang7ae48ee2008-10-18 02:49:28 +0000174 case 'b': // boolean
175 assert(!Long && !Signed && !Unsigned && "Bad modifiers for 'b'!");
176 Type = Context.BoolTy;
177 break;
Chris Lattner52735a02007-10-29 04:18:06 +0000178 case 'z': // size_t.
179 assert(!Long && !Signed && !Unsigned && "Bad modifiers for 'z'!");
180 Type = Context.getSizeType();
181 break;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000182 case 'F':
183 Type = Context.getCFConstantStringType();
184 break;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000185 case 'a':
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000186 Type = Context.getBuiltinVaListType();
Anders Carlsson793680e2007-10-12 23:56:29 +0000187 assert(!Type.isNull() && "builtin va list type not initialized!");
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000188 break;
Eli Friedman6597f982009-01-20 07:46:22 +0000189 case 'A':
190 // This is a "reference" to a va_list; however, what exactly
191 // this means depends on how va_list is defined. There are two
192 // different kinds of va_list: ones passed by value, and ones
193 // passed by reference. An example of a by-value va_list is
194 // x86, where va_list is a char*. An example of by-ref va_list
195 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
196 // we want this argument to be a char*&; for x86-64, we want
197 // it to be a __va_list_tag*.
198 Type = Context.getBuiltinVaListType();
199 assert(!Type.isNull() && "builtin va list type not initialized!");
200 if (Type->isArrayType()) {
201 Type = Context.getArrayDecayedType(Type);
202 } else {
203 Type = Context.getReferenceType(Type);
204 }
205 break;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000206 case 'V': {
207 char *End;
208
209 unsigned NumElements = strtoul(Str, &End, 10);
210 assert(End != Str && "Missing vector size");
211
212 Str = End;
213
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000214 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
Anders Carlsson142f36d2007-11-27 07:22:09 +0000215 Type = Context.getVectorType(ElementType, NumElements);
216 break;
217 }
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000218 case 'P': {
219 IdentifierInfo *II = &Context.Idents.get("FILE");
220 DeclContext::lookup_result Lookup
221 = Context.getTranslationUnitDecl()->lookup(II);
222 if (Lookup.first != Lookup.second && isa<TypeDecl>(*Lookup.first)) {
223 Type = Context.getTypeDeclType(cast<TypeDecl>(*Lookup.first));
224 break;
225 }
226 else {
227 Error = Builtin::Context::GE_Missing_FILE;
228 return QualType();
229 }
230 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 }
Anders Carlsson71993dd2007-08-17 05:31:46 +0000232
Anders Carlssondd1b5162007-11-28 05:19:59 +0000233 if (!AllowTypeModifiers)
234 return Type;
235
Anders Carlsson71993dd2007-08-17 05:31:46 +0000236 Done = false;
237 while (!Done) {
238 switch (*Str++) {
Anders Carlssondd1b5162007-11-28 05:19:59 +0000239 default: Done = true; --Str; break;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000240 case '*':
241 Type = Context.getPointerType(Type);
242 break;
Anders Carlsson793680e2007-10-12 23:56:29 +0000243 case '&':
244 Type = Context.getReferenceType(Type);
245 break;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000246 case 'C':
247 Type = Type.getQualifiedType(QualType::Const);
248 break;
249 }
250 }
251
252 return Type;
Reid Spencer5f016e22007-07-11 17:01:13 +0000253}
254
255/// GetBuiltinType - Return the type for the specified builtin.
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000256QualType Builtin::Context::GetBuiltinType(unsigned id, ASTContext &Context,
257 GetBuiltinTypeError &Error) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 const char *TypeStr = GetRecord(id).Type;
259
260 llvm::SmallVector<QualType, 8> ArgTypes;
261
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000262 Error = GE_None;
263 QualType ResType = DecodeTypeFromStr(TypeStr, Context, Error);
264 if (Error != GE_None)
265 return QualType();
Chris Lattnerf77d5452008-09-29 22:28:25 +0000266 while (TypeStr[0] && TypeStr[0] != '.') {
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000267 QualType Ty = DecodeTypeFromStr(TypeStr, Context, Error);
268 if (Error != GE_None)
269 return QualType();
270
Chris Lattnerf77d5452008-09-29 22:28:25 +0000271 // Do array -> pointer decay. The builtin should use the decayed type.
272 if (Ty->isArrayType())
273 Ty = Context.getArrayDecayedType(Ty);
274
275 ArgTypes.push_back(Ty);
276 }
Anders Carlssondd1b5162007-11-28 05:19:59 +0000277
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
279 "'.' should only occur at end of builtin type list!");
Steve Naroffe8770422007-08-08 17:48:34 +0000280
281 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
282 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
283 return Context.getFunctionTypeNoProto(ResType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 return Context.getFunctionType(ResType, &ArgTypes[0], ArgTypes.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000285 TypeStr[0] == '.', 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000286}