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" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 16 | #include "clang/Basic/IdentifierTable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | using namespace clang; |
| 19 | |
| 20 | static const Builtin::Info BuiltinInfo[] = { |
| 21 | { "not a builtin function", 0, 0 }, |
| 22 | #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS }, |
| 23 | #include "clang/AST/Builtins.def" |
| 24 | }; |
| 25 | |
| 26 | const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const { |
| 27 | if (ID < Builtin::FirstTSBuiltin) |
| 28 | return BuiltinInfo[ID]; |
| 29 | assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!"); |
| 30 | return TSRecords[ID - Builtin::FirstTSBuiltin]; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | /// InitializeBuiltins - Mark the identifiers for all the builtins with their |
| 35 | /// appropriate builtin ID # and mark any non-portable builtin identifiers as |
| 36 | /// such. |
| 37 | void Builtin::Context::InitializeBuiltins(IdentifierTable &Table, |
| 38 | const TargetInfo &Target) { |
| 39 | // Step #1: mark all target-independent builtins with their ID's. |
| 40 | for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) |
| 41 | Table.get(BuiltinInfo[i].Name).setBuiltinID(i); |
| 42 | |
| 43 | // Step #2: handle target builtins. |
| 44 | std::vector<const char *> NonPortableBuiltins; |
| 45 | Target.getTargetBuiltins(TSRecords, NumTSRecords, NonPortableBuiltins); |
| 46 | |
| 47 | // Step #2a: Register target-specific builtins. |
| 48 | for (unsigned i = 0, e = NumTSRecords; i != e; ++i) |
| 49 | Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin); |
| 50 | |
| 51 | // Step #2b: Mark non-portable builtins as such. |
| 52 | for (unsigned i = 0, e = NonPortableBuiltins.size(); i != e; ++i) |
| 53 | Table.get(NonPortableBuiltins[i]).setNonPortableBuiltin(true); |
| 54 | } |
| 55 | |
| 56 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 57 | /// pointer over the consumed characters. This returns the resultant type. |
Anders Carlsson | dd1b516 | 2007-11-28 05:19:59 +0000 | [diff] [blame] | 58 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
| 59 | bool AllowTypeModifiers = true) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | // Modifiers. |
| 61 | bool Long = false, LongLong = false, Signed = false, Unsigned = false; |
| 62 | |
| 63 | // Read the modifiers first. |
| 64 | bool Done = false; |
| 65 | while (!Done) { |
| 66 | switch (*Str++) { |
| 67 | default: Done = true; --Str; break; |
| 68 | case 'S': |
| 69 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 70 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 71 | Signed = true; |
| 72 | break; |
| 73 | case 'U': |
| 74 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 75 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 76 | Unsigned = true; |
| 77 | break; |
| 78 | case 'L': |
| 79 | assert(!LongLong && "Can't have LLL modifier"); |
| 80 | if (Long) |
| 81 | LongLong = true; |
| 82 | else |
| 83 | Long = true; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 88 | QualType Type; |
| 89 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 | // Read the base type. |
| 91 | switch (*Str++) { |
| 92 | default: assert(0 && "Unknown builtin type letter!"); |
| 93 | case 'v': |
Steve Naroff | e877042 | 2007-08-08 17:48:34 +0000 | [diff] [blame] | 94 | assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'v'!"); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 95 | Type = Context.VoidTy; |
| 96 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | case 'f': |
| 98 | assert(!Long && !Signed && !Unsigned && "Bad modifiers used with 'f'!"); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 99 | Type = Context.FloatTy; |
| 100 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | case 'd': |
| 102 | assert(!LongLong && !Signed && !Unsigned && "Bad modifiers used with 'd'!"); |
| 103 | if (Long) |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 104 | Type = Context.LongDoubleTy; |
| 105 | else |
| 106 | Type = Context.DoubleTy; |
| 107 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | case 's': |
| 109 | assert(!LongLong && "Bad modifiers used with 's'!"); |
| 110 | if (Unsigned) |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 111 | Type = Context.UnsignedShortTy; |
| 112 | else |
| 113 | Type = Context.ShortTy; |
| 114 | break; |
Steve Naroff | e877042 | 2007-08-08 17:48:34 +0000 | [diff] [blame] | 115 | case 'i': |
Anders Carlsson | 142f36d | 2007-11-27 07:22:09 +0000 | [diff] [blame] | 116 | if (LongLong) |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 117 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
Anders Carlsson | 142f36d | 2007-11-27 07:22:09 +0000 | [diff] [blame] | 118 | else if (Long) |
| 119 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 120 | else if (Unsigned) |
| 121 | Type = Context.UnsignedIntTy; |
| 122 | else |
| 123 | Type = Context.IntTy; // default is signed. |
| 124 | break; |
| 125 | case 'c': |
| 126 | assert(!Long && !LongLong && "Bad modifiers used with 'c'!"); |
| 127 | if (Signed) |
| 128 | Type = Context.SignedCharTy; |
| 129 | else if (Unsigned) |
| 130 | Type = Context.UnsignedCharTy; |
| 131 | else |
| 132 | Type = Context.CharTy; |
| 133 | break; |
Chris Lattner | 52735a0 | 2007-10-29 04:18:06 +0000 | [diff] [blame] | 134 | case 'z': // size_t. |
| 135 | assert(!Long && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 136 | Type = Context.getSizeType(); |
| 137 | break; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 138 | case 'F': |
| 139 | Type = Context.getCFConstantStringType(); |
| 140 | break; |
Anders Carlsson | 142f36d | 2007-11-27 07:22:09 +0000 | [diff] [blame] | 141 | case 'a': |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 142 | Type = Context.getBuiltinVaListType(); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 143 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 144 | break; |
Anders Carlsson | 142f36d | 2007-11-27 07:22:09 +0000 | [diff] [blame] | 145 | case 'V': { |
| 146 | char *End; |
| 147 | |
| 148 | unsigned NumElements = strtoul(Str, &End, 10); |
| 149 | assert(End != Str && "Missing vector size"); |
| 150 | |
| 151 | Str = End; |
| 152 | |
Anders Carlsson | dd1b516 | 2007-11-28 05:19:59 +0000 | [diff] [blame] | 153 | QualType ElementType = DecodeTypeFromStr(Str, Context, false); |
Anders Carlsson | 142f36d | 2007-11-27 07:22:09 +0000 | [diff] [blame] | 154 | Type = Context.getVectorType(ElementType, NumElements); |
| 155 | break; |
| 156 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 158 | |
Anders Carlsson | dd1b516 | 2007-11-28 05:19:59 +0000 | [diff] [blame] | 159 | if (!AllowTypeModifiers) |
| 160 | return Type; |
| 161 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 162 | Done = false; |
| 163 | while (!Done) { |
| 164 | switch (*Str++) { |
Anders Carlsson | dd1b516 | 2007-11-28 05:19:59 +0000 | [diff] [blame] | 165 | default: Done = true; --Str; break; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 166 | case '*': |
| 167 | Type = Context.getPointerType(Type); |
| 168 | break; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 169 | case '&': |
| 170 | Type = Context.getReferenceType(Type); |
| 171 | break; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 172 | case 'C': |
| 173 | Type = Type.getQualifiedType(QualType::Const); |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return Type; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 22b73ba | 2007-10-10 23:42:28 +0000 | [diff] [blame] | 182 | QualType Builtin::Context::GetBuiltinType(unsigned id, |
| 183 | ASTContext &Context) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | const char *TypeStr = GetRecord(id).Type; |
| 185 | |
| 186 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 187 | |
| 188 | QualType ResType = DecodeTypeFromStr(TypeStr, Context); |
| 189 | while (TypeStr[0] && TypeStr[0] != '.') |
Anders Carlsson | dd1b516 | 2007-11-28 05:19:59 +0000 | [diff] [blame] | 190 | ArgTypes.push_back(DecodeTypeFromStr(TypeStr, Context)); |
| 191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 193 | "'.' should only occur at end of builtin type list!"); |
Steve Naroff | e877042 | 2007-08-08 17:48:34 +0000 | [diff] [blame] | 194 | |
| 195 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 196 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 197 | return Context.getFunctionTypeNoProto(ResType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | return Context.getFunctionType(ResType, &ArgTypes[0], ArgTypes.size(), |
| 199 | TypeStr[0] == '.'); |
| 200 | } |