| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Builtins.cpp - Builtin function implementation -------------------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // | 
|  | 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 Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" | 
| Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 17 | #include "clang/Basic/IdentifierTable.h" | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" | 
|  | 19 | using namespace clang; | 
|  | 20 |  | 
|  | 21 | static const Builtin::Info BuiltinInfo[] = { | 
| Douglas Gregor | b1152d8 | 2009-02-16 21:58:21 +0000 | [diff] [blame] | 22 | { "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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | #include "clang/AST/Builtins.def" | 
|  | 26 | }; | 
|  | 27 |  | 
|  | 28 | const 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. | 
|  | 39 | void Builtin::Context::InitializeBuiltins(IdentifierTable &Table, | 
| Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 40 | const TargetInfo &Target, | 
| Chris Lattner | 7644f07 | 2009-03-13 22:38:49 +0000 | [diff] [blame^] | 41 | bool NoBuiltins) { | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | // Step #1: mark all target-independent builtins with their ID's. | 
|  | 43 | for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) | 
| Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 44 | if (!BuiltinInfo[i].Suppressed && | 
| Chris Lattner | 7644f07 | 2009-03-13 22:38:49 +0000 | [diff] [blame^] | 45 | (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f'))) | 
| Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 46 | Table.get(BuiltinInfo[i].Name).setBuiltinID(i); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 |  | 
| Chris Lattner | 42e6737 | 2008-03-05 01:18:20 +0000 | [diff] [blame] | 48 | // Step #2: Get target builtins. | 
|  | 49 | Target.getTargetBuiltins(TSRecords, NumTSRecords); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 |  | 
| Chris Lattner | 42e6737 | 2008-03-05 01:18:20 +0000 | [diff] [blame] | 51 | // Step #3: Register target-specific builtins. | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | for (unsigned i = 0, e = NumTSRecords; i != e; ++i) | 
| Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 53 | if (!TSRecords[i].Suppressed && | 
| Chris Lattner | 7644f07 | 2009-03-13 22:38:49 +0000 | [diff] [blame^] | 54 | (!NoBuiltins || | 
| Daniel Dunbar | e869990 | 2009-02-15 18:23:07 +0000 | [diff] [blame] | 55 | (TSRecords[i].Attributes && | 
|  | 56 | !strchr(TSRecords[i].Attributes, 'f')))) | 
| Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 57 | Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | } | 
|  | 59 |  | 
| Douglas Gregor | a316e7b | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 60 | bool | 
|  | 61 | Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx, | 
|  | 62 | bool &HasVAListArg) { | 
| Cedric Venet | ea684e6 | 2009-02-14 16:15:20 +0000 | [diff] [blame] | 63 | const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP"); | 
| Douglas Gregor | a316e7b | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 64 | 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 Lattner | 8a778d6 | 2009-02-19 06:41:13 +0000 | [diff] [blame] | 73 | assert(strchr(Printf, ':') && "printf specifier must end with a ':'"); | 
| Douglas Gregor | a316e7b | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 74 | FormatIdx = strtol(Printf, 0, 10); | 
|  | 75 | return true; | 
|  | 76 | } | 
|  | 77 |  | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the | 
|  | 79 | /// pointer over the consumed characters.  This returns the resultant type. | 
| Anders Carlsson | dd1b516 | 2007-11-28 05:19:59 +0000 | [diff] [blame] | 80 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, | 
| Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 81 | Builtin::Context::GetBuiltinTypeError &Error, | 
| Anders Carlsson | dd1b516 | 2007-11-28 05:19:59 +0000 | [diff] [blame] | 82 | bool AllowTypeModifiers = true) { | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | // 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 Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 111 | QualType Type; | 
|  | 112 |  | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | // Read the base type. | 
|  | 114 | switch (*Str++) { | 
|  | 115 | default: assert(0 && "Unknown builtin type letter!"); | 
|  | 116 | case 'v': | 
| Steve Naroff | e877042 | 2007-08-08 17:48:34 +0000 | [diff] [blame] | 117 | assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'v'!"); | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 118 | Type = Context.VoidTy; | 
|  | 119 | break; | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | case 'f': | 
|  | 121 | assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'f'!"); | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 122 | Type = Context.FloatTy; | 
|  | 123 | break; | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | case 'd': | 
|  | 125 | assert(!LongLong && !Signed && !Unsigned && "Bad modifiers used with 'd'!"); | 
|  | 126 | if (Long) | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 127 | Type = Context.LongDoubleTy; | 
|  | 128 | else | 
|  | 129 | Type = Context.DoubleTy; | 
|  | 130 | break; | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | case 's': | 
|  | 132 | assert(!LongLong && "Bad modifiers used with 's'!"); | 
|  | 133 | if (Unsigned) | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 134 | Type = Context.UnsignedShortTy; | 
|  | 135 | else | 
|  | 136 | Type = Context.ShortTy; | 
|  | 137 | break; | 
| Steve Naroff | e877042 | 2007-08-08 17:48:34 +0000 | [diff] [blame] | 138 | case 'i': | 
| Anders Carlsson | 142f36d | 2007-11-27 07:22:09 +0000 | [diff] [blame] | 139 | if (LongLong) | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 140 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; | 
| Anders Carlsson | 142f36d | 2007-11-27 07:22:09 +0000 | [diff] [blame] | 141 | else if (Long) | 
|  | 142 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 143 | 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 Wang | 7ae48ee | 2008-10-18 02:49:28 +0000 | [diff] [blame] | 157 | case 'b': // boolean | 
|  | 158 | assert(!Long && !Signed && !Unsigned && "Bad modifiers for 'b'!"); | 
|  | 159 | Type = Context.BoolTy; | 
|  | 160 | break; | 
| Chris Lattner | 52735a0 | 2007-10-29 04:18:06 +0000 | [diff] [blame] | 161 | case 'z':  // size_t. | 
|  | 162 | assert(!Long && !Signed && !Unsigned && "Bad modifiers for 'z'!"); | 
|  | 163 | Type = Context.getSizeType(); | 
|  | 164 | break; | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 165 | case 'F': | 
|  | 166 | Type = Context.getCFConstantStringType(); | 
|  | 167 | break; | 
| Anders Carlsson | 142f36d | 2007-11-27 07:22:09 +0000 | [diff] [blame] | 168 | case 'a': | 
| Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 169 | Type = Context.getBuiltinVaListType(); | 
| Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 170 | assert(!Type.isNull() && "builtin va list type not initialized!"); | 
| Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 171 | break; | 
| Eli Friedman | 6597f98 | 2009-01-20 07:46:22 +0000 | [diff] [blame] | 172 | 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 Carlsson | 142f36d | 2007-11-27 07:22:09 +0000 | [diff] [blame] | 189 | 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 Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 197 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); | 
| Anders Carlsson | 142f36d | 2007-11-27 07:22:09 +0000 | [diff] [blame] | 198 | Type = Context.getVectorType(ElementType, NumElements); | 
|  | 199 | break; | 
|  | 200 | } | 
| Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 201 | 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | } | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 215 |  | 
| Anders Carlsson | dd1b516 | 2007-11-28 05:19:59 +0000 | [diff] [blame] | 216 | if (!AllowTypeModifiers) | 
|  | 217 | return Type; | 
|  | 218 |  | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 219 | Done = false; | 
|  | 220 | while (!Done) { | 
|  | 221 | switch (*Str++) { | 
| Anders Carlsson | dd1b516 | 2007-11-28 05:19:59 +0000 | [diff] [blame] | 222 | default: Done = true; --Str; break; | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 223 | case '*': | 
|  | 224 | Type = Context.getPointerType(Type); | 
|  | 225 | break; | 
| Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 226 | case '&': | 
|  | 227 | Type = Context.getReferenceType(Type); | 
|  | 228 | break; | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 229 | case 'C': | 
|  | 230 | Type = Type.getQualifiedType(QualType::Const); | 
|  | 231 | break; | 
|  | 232 | } | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | return Type; | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | } | 
|  | 237 |  | 
|  | 238 | /// GetBuiltinType - Return the type for the specified builtin. | 
| Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 239 | QualType Builtin::Context::GetBuiltinType(unsigned id, ASTContext &Context, | 
|  | 240 | GetBuiltinTypeError &Error) const { | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 241 | const char *TypeStr = GetRecord(id).Type; | 
|  | 242 |  | 
|  | 243 | llvm::SmallVector<QualType, 8> ArgTypes; | 
|  | 244 |  | 
| Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 245 | Error = GE_None; | 
|  | 246 | QualType ResType = DecodeTypeFromStr(TypeStr, Context, Error); | 
|  | 247 | if (Error != GE_None) | 
|  | 248 | return QualType(); | 
| Chris Lattner | f77d545 | 2008-09-29 22:28:25 +0000 | [diff] [blame] | 249 | while (TypeStr[0] && TypeStr[0] != '.') { | 
| Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 250 | QualType Ty = DecodeTypeFromStr(TypeStr, Context, Error); | 
|  | 251 | if (Error != GE_None) | 
|  | 252 | return QualType(); | 
|  | 253 |  | 
| Chris Lattner | f77d545 | 2008-09-29 22:28:25 +0000 | [diff] [blame] | 254 | // 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 Carlsson | dd1b516 | 2007-11-28 05:19:59 +0000 | [diff] [blame] | 260 |  | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && | 
|  | 262 | "'.' should only occur at end of builtin type list!"); | 
| Steve Naroff | e877042 | 2007-08-08 17:48:34 +0000 | [diff] [blame] | 263 |  | 
|  | 264 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". | 
|  | 265 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') | 
| Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 266 | return Context.getFunctionNoProtoType(ResType); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | return Context.getFunctionType(ResType, &ArgTypes[0], ArgTypes.size(), | 
| Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 268 | TypeStr[0] == '.', 0); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 269 | } |