blob: e7ec1372a8eecf44ae70d50b187726bdbeff3cac [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[] = {
22 { "not a builtin function", 0, 0 },
23#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS },
24#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,
39 const TargetInfo &Target) {
40 // Step #1: mark all target-independent builtins with their ID's.
41 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
42 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
43
Chris Lattner42e67372008-03-05 01:18:20 +000044 // Step #2: Get target builtins.
45 Target.getTargetBuiltins(TSRecords, NumTSRecords);
Reid Spencer5f016e22007-07-11 17:01:13 +000046
Chris Lattner42e67372008-03-05 01:18:20 +000047 // Step #3: Register target-specific builtins.
Reid Spencer5f016e22007-07-11 17:01:13 +000048 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
49 Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
Reid Spencer5f016e22007-07-11 17:01:13 +000050}
51
Douglas Gregora316e7b2009-02-14 00:32:47 +000052std::string Builtin::Context::getHeaderName(unsigned ID) const {
53 char *Name = strchr(GetRecord(ID).Attributes, 'f');
54 if (!Name)
55 return 0;
56 ++Name;
57
58 if (*Name != ':')
59 return 0;
60
61 ++Name;
62 char *NameEnd = strchr(Name, ':');
63 assert(NameEnd && "Missing ':' after header name");
64 return std::string(Name, NameEnd);
65}
66
67bool
68Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
69 bool &HasVAListArg) {
70 char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
71 if (!Printf)
72 return false;
73
74 HasVAListArg = (*Printf == 'P');
75
76 ++Printf;
77 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
78 ++Printf;
79
80 char *PrintfEnd = strchr(Printf, ':');
81 assert(PrintfEnd && "printf specifier must end with a ':'");
82
83 FormatIdx = strtol(Printf, 0, 10);
84 return true;
85}
86
Reid Spencer5f016e22007-07-11 17:01:13 +000087/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
88/// pointer over the consumed characters. This returns the resultant type.
Anders Carlssondd1b5162007-11-28 05:19:59 +000089static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
90 bool AllowTypeModifiers = true) {
Reid Spencer5f016e22007-07-11 17:01:13 +000091 // Modifiers.
92 bool Long = false, LongLong = false, Signed = false, Unsigned = false;
93
94 // Read the modifiers first.
95 bool Done = false;
96 while (!Done) {
97 switch (*Str++) {
98 default: Done = true; --Str; break;
99 case 'S':
100 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
101 assert(!Signed && "Can't use 'S' modifier multiple times!");
102 Signed = true;
103 break;
104 case 'U':
105 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
106 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
107 Unsigned = true;
108 break;
109 case 'L':
110 assert(!LongLong && "Can't have LLL modifier");
111 if (Long)
112 LongLong = true;
113 else
114 Long = true;
115 break;
116 }
117 }
118
Anders Carlsson71993dd2007-08-17 05:31:46 +0000119 QualType Type;
120
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 // Read the base type.
122 switch (*Str++) {
123 default: assert(0 && "Unknown builtin type letter!");
124 case 'v':
Steve Naroffe8770422007-08-08 17:48:34 +0000125 assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'v'!");
Anders Carlsson71993dd2007-08-17 05:31:46 +0000126 Type = Context.VoidTy;
127 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 case 'f':
129 assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'f'!");
Anders Carlsson71993dd2007-08-17 05:31:46 +0000130 Type = Context.FloatTy;
131 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 case 'd':
133 assert(!LongLong && !Signed && !Unsigned && "Bad modifiers used with 'd'!");
134 if (Long)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000135 Type = Context.LongDoubleTy;
136 else
137 Type = Context.DoubleTy;
138 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 case 's':
140 assert(!LongLong && "Bad modifiers used with 's'!");
141 if (Unsigned)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000142 Type = Context.UnsignedShortTy;
143 else
144 Type = Context.ShortTy;
145 break;
Steve Naroffe8770422007-08-08 17:48:34 +0000146 case 'i':
Anders Carlsson142f36d2007-11-27 07:22:09 +0000147 if (LongLong)
Anders Carlsson71993dd2007-08-17 05:31:46 +0000148 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000149 else if (Long)
150 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000151 else if (Unsigned)
152 Type = Context.UnsignedIntTy;
153 else
154 Type = Context.IntTy; // default is signed.
155 break;
156 case 'c':
157 assert(!Long && !LongLong && "Bad modifiers used with 'c'!");
158 if (Signed)
159 Type = Context.SignedCharTy;
160 else if (Unsigned)
161 Type = Context.UnsignedCharTy;
162 else
163 Type = Context.CharTy;
164 break;
Mon P Wang7ae48ee2008-10-18 02:49:28 +0000165 case 'b': // boolean
166 assert(!Long && !Signed && !Unsigned && "Bad modifiers for 'b'!");
167 Type = Context.BoolTy;
168 break;
Chris Lattner52735a02007-10-29 04:18:06 +0000169 case 'z': // size_t.
170 assert(!Long && !Signed && !Unsigned && "Bad modifiers for 'z'!");
171 Type = Context.getSizeType();
172 break;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000173 case 'F':
174 Type = Context.getCFConstantStringType();
175 break;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000176 case 'a':
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000177 Type = Context.getBuiltinVaListType();
Anders Carlsson793680e2007-10-12 23:56:29 +0000178 assert(!Type.isNull() && "builtin va list type not initialized!");
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000179 break;
Eli Friedman6597f982009-01-20 07:46:22 +0000180 case 'A':
181 // This is a "reference" to a va_list; however, what exactly
182 // this means depends on how va_list is defined. There are two
183 // different kinds of va_list: ones passed by value, and ones
184 // passed by reference. An example of a by-value va_list is
185 // x86, where va_list is a char*. An example of by-ref va_list
186 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
187 // we want this argument to be a char*&; for x86-64, we want
188 // it to be a __va_list_tag*.
189 Type = Context.getBuiltinVaListType();
190 assert(!Type.isNull() && "builtin va list type not initialized!");
191 if (Type->isArrayType()) {
192 Type = Context.getArrayDecayedType(Type);
193 } else {
194 Type = Context.getReferenceType(Type);
195 }
196 break;
Anders Carlsson142f36d2007-11-27 07:22:09 +0000197 case 'V': {
198 char *End;
199
200 unsigned NumElements = strtoul(Str, &End, 10);
201 assert(End != Str && "Missing vector size");
202
203 Str = End;
204
Anders Carlssondd1b5162007-11-28 05:19:59 +0000205 QualType ElementType = DecodeTypeFromStr(Str, Context, false);
Anders Carlsson142f36d2007-11-27 07:22:09 +0000206 Type = Context.getVectorType(ElementType, NumElements);
207 break;
208 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 }
Anders Carlsson71993dd2007-08-17 05:31:46 +0000210
Anders Carlssondd1b5162007-11-28 05:19:59 +0000211 if (!AllowTypeModifiers)
212 return Type;
213
Anders Carlsson71993dd2007-08-17 05:31:46 +0000214 Done = false;
215 while (!Done) {
216 switch (*Str++) {
Anders Carlssondd1b5162007-11-28 05:19:59 +0000217 default: Done = true; --Str; break;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000218 case '*':
219 Type = Context.getPointerType(Type);
220 break;
Anders Carlsson793680e2007-10-12 23:56:29 +0000221 case '&':
222 Type = Context.getReferenceType(Type);
223 break;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000224 case 'C':
225 Type = Type.getQualifiedType(QualType::Const);
226 break;
227 }
228 }
229
230 return Type;
Reid Spencer5f016e22007-07-11 17:01:13 +0000231}
232
233/// GetBuiltinType - Return the type for the specified builtin.
Chris Lattner22b73ba2007-10-10 23:42:28 +0000234QualType Builtin::Context::GetBuiltinType(unsigned id,
235 ASTContext &Context) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 const char *TypeStr = GetRecord(id).Type;
237
238 llvm::SmallVector<QualType, 8> ArgTypes;
239
240 QualType ResType = DecodeTypeFromStr(TypeStr, Context);
Chris Lattnerf77d5452008-09-29 22:28:25 +0000241 while (TypeStr[0] && TypeStr[0] != '.') {
242 QualType Ty = DecodeTypeFromStr(TypeStr, Context);
243
244 // Do array -> pointer decay. The builtin should use the decayed type.
245 if (Ty->isArrayType())
246 Ty = Context.getArrayDecayedType(Ty);
247
248 ArgTypes.push_back(Ty);
249 }
Anders Carlssondd1b5162007-11-28 05:19:59 +0000250
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
252 "'.' should only occur at end of builtin type list!");
Steve Naroffe8770422007-08-08 17:48:34 +0000253
254 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
255 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
256 return Context.getFunctionTypeNoProto(ResType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 return Context.getFunctionType(ResType, &ArgTypes[0], ArgTypes.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000258 TypeStr[0] == '.', 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000259}