blob: b572e8e04b576d2a184f27b3264aec8c7f83c23b [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
35
36/// InitializeBuiltins - Mark the identifiers for all the builtins with their
37/// appropriate builtin ID # and mark any non-portable builtin identifiers as
38/// such.
39void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
Douglas Gregor3573c0c2009-02-14 20:49:29 +000040 const TargetInfo &Target,
41 bool Freestanding) {
Reid Spencer5f016e22007-07-11 17:01:13 +000042 // Step #1: mark all target-independent builtins with their ID's.
43 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Douglas Gregor3573c0c2009-02-14 20:49:29 +000044 if (!BuiltinInfo[i].Suppressed &&
45 (!Freestanding ||
46 !strchr(BuiltinInfo[i].Attributes, 'f')))
47 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
Reid Spencer5f016e22007-07-11 17:01:13 +000048
Chris Lattner42e67372008-03-05 01:18:20 +000049 // Step #2: Get target builtins.
50 Target.getTargetBuiltins(TSRecords, NumTSRecords);
Reid Spencer5f016e22007-07-11 17:01:13 +000051
Chris Lattner42e67372008-03-05 01:18:20 +000052 // Step #3: 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 &&
55 (!Freestanding ||
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 Gregora316e7b2009-02-14 00:32:47 +000061bool
62Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
63 bool &HasVAListArg) {
Cedric Venetea684e62009-02-14 16:15:20 +000064 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
Douglas Gregora316e7b2009-02-14 00:32:47 +000065 if (!Printf)
66 return false;
67
68 HasVAListArg = (*Printf == 'P');
69
70 ++Printf;
71 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
72 ++Printf;
73
Chris Lattner8a778d62009-02-19 06:41:13 +000074 assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
Douglas Gregora316e7b2009-02-14 00:32:47 +000075 FormatIdx = strtol(Printf, 0, 10);
76 return true;
77}
78
Reid Spencer5f016e22007-07-11 17:01:13 +000079/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
80/// pointer over the consumed characters. This returns the resultant type.
Anders Carlssondd1b5162007-11-28 05:19:59 +000081static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Douglas Gregor370ab3f2009-02-14 01:52:53 +000082 Builtin::Context::GetBuiltinTypeError &Error,
Anders Carlssondd1b5162007-11-28 05:19:59 +000083 bool AllowTypeModifiers = true) {
Reid Spencer5f016e22007-07-11 17:01:13 +000084 // Modifiers.
85 bool Long = false, LongLong = false, Signed = false, Unsigned = false;
86
87 // Read the modifiers first.
88 bool Done = false;
89 while (!Done) {
90 switch (*Str++) {
91 default: Done = true; --Str; break;
92 case 'S':
93 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
94 assert(!Signed && "Can't use 'S' modifier multiple times!");
95 Signed = true;
96 break;
97 case 'U':
98 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
99 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
100 Unsigned = true;
101 break;
102 case 'L':
103 assert(!LongLong && "Can't have LLL modifier");
104 if (Long)
105 LongLong = true;
106 else
107 Long = true;
108 break;
109 }
110 }
111
Anders Carlsson71993dd2007-08-17 05:31:46 +0000112 QualType Type;
113
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 // Read the base type.
115 switch (*Str++) {
116 default: assert(0 && "Unknown builtin type letter!");
117 case 'v':
Steve Naroffe8770422007-08-08 17:48:34 +0000118 assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'v'!");
Anders Carlsson71993dd2007-08-17 05:31:46 +0000119 Type = Context.VoidTy;
120 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 case 'f':
122 assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'f'!");
Anders Carlsson71993dd2007-08-17 05:31:46 +0000123 Type = Context.FloatTy;
124 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 case 'd':
126 assert(!LongLong && !Signed && !Unsigned && "Bad modifiers used with 'd'!");
127 if (Long)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000128 Type = Context.LongDoubleTy;
129 else
130 Type = Context.DoubleTy;
131 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 case 's':
133 assert(!LongLong && "Bad modifiers used with 's'!");
134 if (Unsigned)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000135 Type = Context.UnsignedShortTy;
136 else
137 Type = Context.ShortTy;
138 break;
Steve Naroffe8770422007-08-08 17:48:34 +0000139 case 'i':
Anders Carlsson142f36d2007-11-27 07:22:09 +0000140 if (LongLong)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000141 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000142 else if (Long)
143 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000144 else if (Unsigned)
145 Type = Context.UnsignedIntTy;
146 else
147 Type = Context.IntTy; // default is signed.
148 break;
149 case 'c':
150 assert(!Long && !LongLong && "Bad modifiers used with 'c'!");
151 if (Signed)
152 Type = Context.SignedCharTy;
153 else if (Unsigned)
154 Type = Context.UnsignedCharTy;
155 else
156 Type = Context.CharTy;
157 break;
Mon P Wang7ae48ee2008-10-18 02:49:28 +0000158 case 'b': // boolean
159 assert(!Long && !Signed && !Unsigned && "Bad modifiers for 'b'!");
160 Type = Context.BoolTy;
161 break;
Chris Lattner52735a02007-10-29 04:18:06 +0000162 case 'z': // size_t.
163 assert(!Long && !Signed && !Unsigned && "Bad modifiers for 'z'!");
164 Type = Context.getSizeType();
165 break;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000166 case 'F':
167 Type = Context.getCFConstantStringType();
168 break;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000169 case 'a':
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000170 Type = Context.getBuiltinVaListType();
Anders Carlsson793680e2007-10-12 23:56:29 +0000171 assert(!Type.isNull() && "builtin va list type not initialized!");
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000172 break;
Eli Friedman6597f982009-01-20 07:46:22 +0000173 case 'A':
174 // This is a "reference" to a va_list; however, what exactly
175 // this means depends on how va_list is defined. There are two
176 // different kinds of va_list: ones passed by value, and ones
177 // passed by reference. An example of a by-value va_list is
178 // x86, where va_list is a char*. An example of by-ref va_list
179 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
180 // we want this argument to be a char*&; for x86-64, we want
181 // it to be a __va_list_tag*.
182 Type = Context.getBuiltinVaListType();
183 assert(!Type.isNull() && "builtin va list type not initialized!");
184 if (Type->isArrayType()) {
185 Type = Context.getArrayDecayedType(Type);
186 } else {
187 Type = Context.getReferenceType(Type);
188 }
189 break;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000190 case 'V': {
191 char *End;
192
193 unsigned NumElements = strtoul(Str, &End, 10);
194 assert(End != Str && "Missing vector size");
195
196 Str = End;
197
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000198 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
Anders Carlsson142f36d2007-11-27 07:22:09 +0000199 Type = Context.getVectorType(ElementType, NumElements);
200 break;
201 }
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000202 case 'P': {
203 IdentifierInfo *II = &Context.Idents.get("FILE");
204 DeclContext::lookup_result Lookup
205 = Context.getTranslationUnitDecl()->lookup(II);
206 if (Lookup.first != Lookup.second && isa<TypeDecl>(*Lookup.first)) {
207 Type = Context.getTypeDeclType(cast<TypeDecl>(*Lookup.first));
208 break;
209 }
210 else {
211 Error = Builtin::Context::GE_Missing_FILE;
212 return QualType();
213 }
214 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 }
Anders Carlsson71993dd2007-08-17 05:31:46 +0000216
Anders Carlssondd1b5162007-11-28 05:19:59 +0000217 if (!AllowTypeModifiers)
218 return Type;
219
Anders Carlsson71993dd2007-08-17 05:31:46 +0000220 Done = false;
221 while (!Done) {
222 switch (*Str++) {
Anders Carlssondd1b5162007-11-28 05:19:59 +0000223 default: Done = true; --Str; break;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000224 case '*':
225 Type = Context.getPointerType(Type);
226 break;
Anders Carlsson793680e2007-10-12 23:56:29 +0000227 case '&':
228 Type = Context.getReferenceType(Type);
229 break;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000230 case 'C':
231 Type = Type.getQualifiedType(QualType::Const);
232 break;
233 }
234 }
235
236 return Type;
Reid Spencer5f016e22007-07-11 17:01:13 +0000237}
238
239/// GetBuiltinType - Return the type for the specified builtin.
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000240QualType Builtin::Context::GetBuiltinType(unsigned id, ASTContext &Context,
241 GetBuiltinTypeError &Error) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 const char *TypeStr = GetRecord(id).Type;
243
244 llvm::SmallVector<QualType, 8> ArgTypes;
245
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000246 Error = GE_None;
247 QualType ResType = DecodeTypeFromStr(TypeStr, Context, Error);
248 if (Error != GE_None)
249 return QualType();
Chris Lattnerf77d5452008-09-29 22:28:25 +0000250 while (TypeStr[0] && TypeStr[0] != '.') {
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000251 QualType Ty = DecodeTypeFromStr(TypeStr, Context, Error);
252 if (Error != GE_None)
253 return QualType();
254
Chris Lattnerf77d5452008-09-29 22:28:25 +0000255 // Do array -> pointer decay. The builtin should use the decayed type.
256 if (Ty->isArrayType())
257 Ty = Context.getArrayDecayedType(Ty);
258
259 ArgTypes.push_back(Ty);
260 }
Anders Carlssondd1b5162007-11-28 05:19:59 +0000261
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
263 "'.' should only occur at end of builtin type list!");
Steve Naroffe8770422007-08-08 17:48:34 +0000264
265 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
266 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
267 return Context.getFunctionTypeNoProto(ResType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 return Context.getFunctionType(ResType, &ArgTypes[0], ArgTypes.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000269 TypeStr[0] == '.', 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000270}