blob: 97a40a4dd0b1489614df39752da825aac0f1c99d [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Builtins.cpp - Builtin function implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbarde300732008-08-11 04:54:23 +000016#include "clang/AST/Decl.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/Basic/TargetInfo.h"
19using namespace clang;
20
21static const Builtin::Info BuiltinInfo[] = {
Douglas Gregor2e8a7aa2009-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 },
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregor23d23262009-02-14 20:49:29 +000040 const TargetInfo &Target,
Chris Lattner911b8672009-03-13 22:38:49 +000041 bool NoBuiltins) {
Chris Lattner4b009652007-07-25 00:24:17 +000042 // Step #1: mark all target-independent builtins with their ID's.
43 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Douglas Gregor23d23262009-02-14 20:49:29 +000044 if (!BuiltinInfo[i].Suppressed &&
Chris Lattner911b8672009-03-13 22:38:49 +000045 (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f')))
Douglas Gregor23d23262009-02-14 20:49:29 +000046 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
Chris Lattner4b009652007-07-25 00:24:17 +000047
Chris Lattnerfc457002008-03-05 01:18:20 +000048 // Step #2: Get target builtins.
49 Target.getTargetBuiltins(TSRecords, NumTSRecords);
Chris Lattner4b009652007-07-25 00:24:17 +000050
Chris Lattnerfc457002008-03-05 01:18:20 +000051 // Step #3: Register target-specific builtins.
Chris Lattner4b009652007-07-25 00:24:17 +000052 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
Douglas Gregor23d23262009-02-14 20:49:29 +000053 if (!TSRecords[i].Suppressed &&
Chris Lattner911b8672009-03-13 22:38:49 +000054 (!NoBuiltins ||
Daniel Dunbarbff577a2009-02-15 18:23:07 +000055 (TSRecords[i].Attributes &&
56 !strchr(TSRecords[i].Attributes, 'f'))))
Douglas Gregor23d23262009-02-14 20:49:29 +000057 Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
Chris Lattner4b009652007-07-25 00:24:17 +000058}
59
Douglas Gregor17429032009-02-14 00:32:47 +000060bool
61Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
62 bool &HasVAListArg) {
Cédric Venetc47ed4a2009-02-14 16:15:20 +000063 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
Douglas Gregor17429032009-02-14 00:32:47 +000064 if (!Printf)
65 return false;
66
67 HasVAListArg = (*Printf == 'P');
68
69 ++Printf;
70 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
71 ++Printf;
72
Chris Lattnerca8de5d2009-02-19 06:41:13 +000073 assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
Douglas Gregor17429032009-02-14 00:32:47 +000074 FormatIdx = strtol(Printf, 0, 10);
75 return true;
76}
77
Chris Lattner4b009652007-07-25 00:24:17 +000078/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
79/// pointer over the consumed characters. This returns the resultant type.
Anders Carlsson282a7cb2007-11-28 05:19:59 +000080static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Douglas Gregor1fa246d2009-02-14 01:52:53 +000081 Builtin::Context::GetBuiltinTypeError &Error,
Anders Carlsson282a7cb2007-11-28 05:19:59 +000082 bool AllowTypeModifiers = true) {
Chris Lattner4b009652007-07-25 00:24:17 +000083 // Modifiers.
84 bool Long = false, LongLong = false, Signed = false, Unsigned = false;
85
86 // Read the modifiers first.
87 bool Done = false;
88 while (!Done) {
89 switch (*Str++) {
90 default: Done = true; --Str; break;
91 case 'S':
92 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
93 assert(!Signed && "Can't use 'S' modifier multiple times!");
94 Signed = true;
95 break;
96 case 'U':
97 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
98 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
99 Unsigned = true;
100 break;
101 case 'L':
102 assert(!LongLong && "Can't have LLL modifier");
103 if (Long)
104 LongLong = true;
105 else
106 Long = true;
107 break;
108 }
109 }
110
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000111 QualType Type;
112
Chris Lattner4b009652007-07-25 00:24:17 +0000113 // Read the base type.
114 switch (*Str++) {
115 default: assert(0 && "Unknown builtin type letter!");
116 case 'v':
Steve Naroff48a87932007-08-08 17:48:34 +0000117 assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'v'!");
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000118 Type = Context.VoidTy;
119 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000120 case 'f':
121 assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'f'!");
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000122 Type = Context.FloatTy;
123 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000124 case 'd':
125 assert(!LongLong && !Signed && !Unsigned && "Bad modifiers used with 'd'!");
126 if (Long)
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000127 Type = Context.LongDoubleTy;
128 else
129 Type = Context.DoubleTy;
130 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000131 case 's':
132 assert(!LongLong && "Bad modifiers used with 's'!");
133 if (Unsigned)
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000134 Type = Context.UnsignedShortTy;
135 else
136 Type = Context.ShortTy;
137 break;
Steve Naroff48a87932007-08-08 17:48:34 +0000138 case 'i':
Anders Carlssonfb0d1392007-11-27 07:22:09 +0000139 if (LongLong)
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000140 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
Anders Carlssonfb0d1392007-11-27 07:22:09 +0000141 else if (Long)
142 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000143 else if (Unsigned)
144 Type = Context.UnsignedIntTy;
145 else
146 Type = Context.IntTy; // default is signed.
147 break;
148 case 'c':
149 assert(!Long && !LongLong && "Bad modifiers used with 'c'!");
150 if (Signed)
151 Type = Context.SignedCharTy;
152 else if (Unsigned)
153 Type = Context.UnsignedCharTy;
154 else
155 Type = Context.CharTy;
156 break;
Mon P Wang651c8992008-10-18 02:49:28 +0000157 case 'b': // boolean
158 assert(!Long && !Signed && !Unsigned && "Bad modifiers for 'b'!");
159 Type = Context.BoolTy;
160 break;
Chris Lattnerde4d2bd2007-10-29 04:18:06 +0000161 case 'z': // size_t.
162 assert(!Long && !Signed && !Unsigned && "Bad modifiers for 'z'!");
163 Type = Context.getSizeType();
164 break;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000165 case 'F':
166 Type = Context.getCFConstantStringType();
167 break;
Anders Carlssonfb0d1392007-11-27 07:22:09 +0000168 case 'a':
Anders Carlssonfb5b1e82007-10-11 01:00:40 +0000169 Type = Context.getBuiltinVaListType();
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000170 assert(!Type.isNull() && "builtin va list type not initialized!");
Anders Carlssonfb5b1e82007-10-11 01:00:40 +0000171 break;
Eli Friedman1d411122009-01-20 07:46:22 +0000172 case 'A':
173 // This is a "reference" to a va_list; however, what exactly
174 // this means depends on how va_list is defined. There are two
175 // different kinds of va_list: ones passed by value, and ones
176 // passed by reference. An example of a by-value va_list is
177 // x86, where va_list is a char*. An example of by-ref va_list
178 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
179 // we want this argument to be a char*&; for x86-64, we want
180 // it to be a __va_list_tag*.
181 Type = Context.getBuiltinVaListType();
182 assert(!Type.isNull() && "builtin va list type not initialized!");
183 if (Type->isArrayType()) {
184 Type = Context.getArrayDecayedType(Type);
185 } else {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000186 Type = Context.getLValueReferenceType(Type);
Eli Friedman1d411122009-01-20 07:46:22 +0000187 }
188 break;
Anders Carlssonfb0d1392007-11-27 07:22:09 +0000189 case 'V': {
190 char *End;
191
192 unsigned NumElements = strtoul(Str, &End, 10);
193 assert(End != Str && "Missing vector size");
194
195 Str = End;
196
Douglas Gregor1fa246d2009-02-14 01:52:53 +0000197 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
Anders Carlssonfb0d1392007-11-27 07:22:09 +0000198 Type = Context.getVectorType(ElementType, NumElements);
199 break;
200 }
Douglas Gregor1fa246d2009-02-14 01:52:53 +0000201 case 'P': {
202 IdentifierInfo *II = &Context.Idents.get("FILE");
203 DeclContext::lookup_result Lookup
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000204 = Context.getTranslationUnitDecl()->lookup(Context, II);
Douglas Gregor1fa246d2009-02-14 01:52:53 +0000205 if (Lookup.first != Lookup.second && isa<TypeDecl>(*Lookup.first)) {
206 Type = Context.getTypeDeclType(cast<TypeDecl>(*Lookup.first));
207 break;
208 }
209 else {
210 Error = Builtin::Context::GE_Missing_FILE;
211 return QualType();
212 }
213 }
Chris Lattner4b009652007-07-25 00:24:17 +0000214 }
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000215
Anders Carlsson282a7cb2007-11-28 05:19:59 +0000216 if (!AllowTypeModifiers)
217 return Type;
218
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000219 Done = false;
220 while (!Done) {
221 switch (*Str++) {
Anders Carlsson282a7cb2007-11-28 05:19:59 +0000222 default: Done = true; --Str; break;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000223 case '*':
224 Type = Context.getPointerType(Type);
225 break;
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000226 case '&':
Sebastian Redlce6fff02009-03-16 23:22:08 +0000227 Type = Context.getLValueReferenceType(Type);
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000228 break;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000229 // FIXME: There's no way to have a built-in with an rvalue ref arg.
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000230 case 'C':
231 Type = Type.getQualifiedType(QualType::Const);
232 break;
233 }
234 }
235
236 return Type;
Chris Lattner4b009652007-07-25 00:24:17 +0000237}
238
239/// GetBuiltinType - Return the type for the specified builtin.
Douglas Gregor1fa246d2009-02-14 01:52:53 +0000240QualType Builtin::Context::GetBuiltinType(unsigned id, ASTContext &Context,
241 GetBuiltinTypeError &Error) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000242 const char *TypeStr = GetRecord(id).Type;
243
244 llvm::SmallVector<QualType, 8> ArgTypes;
245
Douglas Gregor1fa246d2009-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 Lattner27882f42008-09-29 22:28:25 +0000250 while (TypeStr[0] && TypeStr[0] != '.') {
Douglas Gregor1fa246d2009-02-14 01:52:53 +0000251 QualType Ty = DecodeTypeFromStr(TypeStr, Context, Error);
252 if (Error != GE_None)
253 return QualType();
254
Chris Lattner27882f42008-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 Carlsson282a7cb2007-11-28 05:19:59 +0000261
Chris Lattner4b009652007-07-25 00:24:17 +0000262 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
263 "'.' should only occur at end of builtin type list!");
Steve Naroff48a87932007-08-08 17:48:34 +0000264
265 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
266 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
Douglas Gregor4fa58902009-02-26 23:50:07 +0000267 return Context.getFunctionNoProtoType(ResType);
Chris Lattner4b009652007-07-25 00:24:17 +0000268 return Context.getFunctionType(ResType, &ArgTypes[0], ArgTypes.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +0000269 TypeStr[0] == '.', 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000270}