blob: 4655cf392d7384b20ef1add20ce632c26084d64a [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,
Chris Lattner7644f072009-03-13 22:38:49 +000041 bool NoBuiltins) {
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 &&
Chris Lattner7644f072009-03-13 22:38:49 +000045 (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f')))
Douglas Gregor3573c0c2009-02-14 20:49:29 +000046 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 &&
Chris Lattner7644f072009-03-13 22:38:49 +000054 (!NoBuiltins ||
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 +000060bool
61Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
62 bool &HasVAListArg) {
Cedric Venetea684e62009-02-14 16:15:20 +000063 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
Douglas Gregora316e7b2009-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 Lattner8a778d62009-02-19 06:41:13 +000073 assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
Douglas Gregora316e7b2009-02-14 00:32:47 +000074 FormatIdx = strtol(Printf, 0, 10);
75 return true;
76}
77
Reid Spencer5f016e22007-07-11 17:01:13 +000078/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
79/// pointer over the consumed characters. This returns the resultant type.
Anders Carlssondd1b5162007-11-28 05:19:59 +000080static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Douglas Gregor370ab3f2009-02-14 01:52:53 +000081 Builtin::Context::GetBuiltinTypeError &Error,
Anders Carlssondd1b5162007-11-28 05:19:59 +000082 bool AllowTypeModifiers = true) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Carlsson71993dd2007-08-17 05:31:46 +0000111 QualType Type;
112
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 // Read the base type.
114 switch (*Str++) {
115 default: assert(0 && "Unknown builtin type letter!");
116 case 'v':
Steve Naroffe8770422007-08-08 17:48:34 +0000117 assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'v'!");
Anders Carlsson71993dd2007-08-17 05:31:46 +0000118 Type = Context.VoidTy;
119 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 case 'f':
121 assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'f'!");
Anders Carlsson71993dd2007-08-17 05:31:46 +0000122 Type = Context.FloatTy;
123 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 case 'd':
125 assert(!LongLong && !Signed && !Unsigned && "Bad modifiers used with 'd'!");
126 if (Long)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000127 Type = Context.LongDoubleTy;
128 else
129 Type = Context.DoubleTy;
130 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 case 's':
132 assert(!LongLong && "Bad modifiers used with 's'!");
133 if (Unsigned)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000134 Type = Context.UnsignedShortTy;
135 else
136 Type = Context.ShortTy;
137 break;
Steve Naroffe8770422007-08-08 17:48:34 +0000138 case 'i':
Anders Carlsson142f36d2007-11-27 07:22:09 +0000139 if (LongLong)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000140 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000141 else if (Long)
142 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
Anders Carlsson71993dd2007-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 Wang7ae48ee2008-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 Lattner52735a02007-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 Carlsson71993dd2007-08-17 05:31:46 +0000165 case 'F':
166 Type = Context.getCFConstantStringType();
167 break;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000168 case 'a':
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000169 Type = Context.getBuiltinVaListType();
Anders Carlsson793680e2007-10-12 23:56:29 +0000170 assert(!Type.isNull() && "builtin va list type not initialized!");
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000171 break;
Eli Friedman6597f982009-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 {
186 Type = Context.getReferenceType(Type);
187 }
188 break;
Anders Carlsson142f36d2007-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 Gregor370ab3f2009-02-14 01:52:53 +0000197 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
Anders Carlsson142f36d2007-11-27 07:22:09 +0000198 Type = Context.getVectorType(ElementType, NumElements);
199 break;
200 }
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000201 case 'P': {
202 IdentifierInfo *II = &Context.Idents.get("FILE");
203 DeclContext::lookup_result Lookup
204 = Context.getTranslationUnitDecl()->lookup(II);
205 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 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 }
Anders Carlsson71993dd2007-08-17 05:31:46 +0000215
Anders Carlssondd1b5162007-11-28 05:19:59 +0000216 if (!AllowTypeModifiers)
217 return Type;
218
Anders Carlsson71993dd2007-08-17 05:31:46 +0000219 Done = false;
220 while (!Done) {
221 switch (*Str++) {
Anders Carlssondd1b5162007-11-28 05:19:59 +0000222 default: Done = true; --Str; break;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000223 case '*':
224 Type = Context.getPointerType(Type);
225 break;
Anders Carlsson793680e2007-10-12 23:56:29 +0000226 case '&':
227 Type = Context.getReferenceType(Type);
228 break;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000229 case 'C':
230 Type = Type.getQualifiedType(QualType::Const);
231 break;
232 }
233 }
234
235 return Type;
Reid Spencer5f016e22007-07-11 17:01:13 +0000236}
237
238/// GetBuiltinType - Return the type for the specified builtin.
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000239QualType Builtin::Context::GetBuiltinType(unsigned id, ASTContext &Context,
240 GetBuiltinTypeError &Error) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 const char *TypeStr = GetRecord(id).Type;
242
243 llvm::SmallVector<QualType, 8> ArgTypes;
244
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000245 Error = GE_None;
246 QualType ResType = DecodeTypeFromStr(TypeStr, Context, Error);
247 if (Error != GE_None)
248 return QualType();
Chris Lattnerf77d5452008-09-29 22:28:25 +0000249 while (TypeStr[0] && TypeStr[0] != '.') {
Douglas Gregor370ab3f2009-02-14 01:52:53 +0000250 QualType Ty = DecodeTypeFromStr(TypeStr, Context, Error);
251 if (Error != GE_None)
252 return QualType();
253
Chris Lattnerf77d5452008-09-29 22:28:25 +0000254 // Do array -> pointer decay. The builtin should use the decayed type.
255 if (Ty->isArrayType())
256 Ty = Context.getArrayDecayedType(Ty);
257
258 ArgTypes.push_back(Ty);
259 }
Anders Carlssondd1b5162007-11-28 05:19:59 +0000260
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
262 "'.' should only occur at end of builtin type list!");
Steve Naroffe8770422007-08-08 17:48:34 +0000263
264 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
265 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
Douglas Gregor72564e72009-02-26 23:50:07 +0000266 return Context.getFunctionNoProtoType(ResType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 return Context.getFunctionType(ResType, &ArgTypes[0], ArgTypes.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000268 TypeStr[0] == '.', 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000269}