Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaType.cpp - Semantic Analysis for Types -----------------------===// |
| 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 type-related semantic analysis. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 24 | /// \brief Perform adjustment on the parameter type of a function. |
| 25 | /// |
| 26 | /// This routine adjusts the given parameter type @p T to the actual |
| 27 | /// parameter type used by semantic analysis (C99 6.7.5.3p[7,8], |
| 28 | /// C++ [dcl.fct]p3). The adjusted parameter type is returned. |
| 29 | QualType Sema::adjustParameterType(QualType T) { |
| 30 | // C99 6.7.5.3p7: |
| 31 | if (T->isArrayType()) { |
| 32 | // C99 6.7.5.3p7: |
| 33 | // A declaration of a parameter as "array of type" shall be |
| 34 | // adjusted to "qualified pointer to type", where the type |
| 35 | // qualifiers (if any) are those specified within the [ and ] of |
| 36 | // the array type derivation. |
| 37 | return Context.getArrayDecayedType(T); |
| 38 | } else if (T->isFunctionType()) |
| 39 | // C99 6.7.5.3p8: |
| 40 | // A declaration of a parameter as "function returning type" |
| 41 | // shall be adjusted to "pointer to function returning type", as |
| 42 | // in 6.3.2.1. |
| 43 | return Context.getPointerType(T); |
| 44 | |
| 45 | return T; |
| 46 | } |
| 47 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 48 | /// \brief Convert the specified declspec to the appropriate type |
| 49 | /// object. |
| 50 | /// \param DS the declaration specifiers |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 51 | /// \param DeclLoc The location of the declarator identifier or invalid if none. |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 52 | /// \returns The type described by the declaration specifiers. This function |
| 53 | /// never returns null. |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 54 | QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS, |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 55 | SourceLocation DeclLoc, |
| 56 | bool &isInvalid) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 58 | // checking. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 59 | QualType Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | |
| 61 | switch (DS.getTypeSpecType()) { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 62 | case DeclSpec::TST_void: |
| 63 | Result = Context.VoidTy; |
| 64 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 65 | case DeclSpec::TST_char: |
| 66 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 67 | Result = Context.CharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 69 | Result = Context.SignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | else { |
| 71 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 72 | "Unknown TSS value"); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 73 | Result = Context.UnsignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 75 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 76 | case DeclSpec::TST_wchar: |
| 77 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
| 78 | Result = Context.WCharTy; |
| 79 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 80 | Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
| 81 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 82 | Result = Context.getSignedWCharType(); |
| 83 | } else { |
| 84 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 85 | "Unknown TSS value"); |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 86 | Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
| 87 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 88 | Result = Context.getUnsignedWCharType(); |
| 89 | } |
| 90 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 91 | case DeclSpec::TST_char16: |
| 92 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 93 | "Unknown TSS value"); |
| 94 | Result = Context.Char16Ty; |
| 95 | break; |
| 96 | case DeclSpec::TST_char32: |
| 97 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 98 | "Unknown TSS value"); |
| 99 | Result = Context.Char32Ty; |
| 100 | break; |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 101 | case DeclSpec::TST_unspecified: |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 102 | // "<proto1,proto2>" is an objc qualified ID with a missing id. |
Chris Lattner | 097e916 | 2008-10-20 02:01:50 +0000 | [diff] [blame] | 103 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
Steve Naroff | 470301b | 2009-07-22 16:07:01 +0000 | [diff] [blame] | 104 | Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 105 | (ObjCProtocolDecl**)PQ, |
Steve Naroff | 683087f | 2009-06-29 16:22:52 +0000 | [diff] [blame] | 106 | DS.getNumProtocolQualifiers()); |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 107 | break; |
| 108 | } |
| 109 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 110 | // Unspecified typespec defaults to int in C90. However, the C90 grammar |
| 111 | // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, |
| 112 | // type-qualifier, or storage-class-specifier. If not, emit an extwarn. |
| 113 | // Note that the one exception to this is function definitions, which are |
| 114 | // allowed to be completely missing a declspec. This is handled in the |
| 115 | // parser already though by it pretending to have seen an 'int' in this |
| 116 | // case. |
| 117 | if (getLangOptions().ImplicitInt) { |
Chris Lattner | 35d276f | 2009-02-27 18:53:28 +0000 | [diff] [blame] | 118 | // In C89 mode, we only warn if there is a completely missing declspec |
| 119 | // when one is not allowed. |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 120 | if (DS.isEmpty()) { |
| 121 | if (DeclLoc.isInvalid()) |
| 122 | DeclLoc = DS.getSourceRange().getBegin(); |
Eli Friedman | fcff577 | 2009-06-03 12:22:01 +0000 | [diff] [blame] | 123 | Diag(DeclLoc, diag::ext_missing_declspec) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 124 | << DS.getSourceRange() |
Chris Lattner | 173144a | 2009-02-27 22:31:56 +0000 | [diff] [blame] | 125 | << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(), |
| 126 | "int"); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 127 | } |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 128 | } else if (!DS.hasTypeSpecifier()) { |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 129 | // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: |
| 130 | // "At least one type specifier shall be given in the declaration |
| 131 | // specifiers in each declaration, and in the specifier-qualifier list in |
| 132 | // each struct declaration and type name." |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 133 | // FIXME: Does Microsoft really have the implicit int extension in C++? |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 134 | if (DeclLoc.isInvalid()) |
| 135 | DeclLoc = DS.getSourceRange().getBegin(); |
| 136 | |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 137 | if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft) { |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 138 | Diag(DeclLoc, diag::err_missing_type_specifier) |
| 139 | << DS.getSourceRange(); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 140 | |
| 141 | // When this occurs in C++ code, often something is very broken with the |
| 142 | // value being declared, poison it as invalid so we don't get chains of |
| 143 | // errors. |
| 144 | isInvalid = true; |
| 145 | } else { |
Eli Friedman | fcff577 | 2009-06-03 12:22:01 +0000 | [diff] [blame] | 146 | Diag(DeclLoc, diag::ext_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 147 | << DS.getSourceRange(); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 148 | } |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // FALL THROUGH. |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 152 | case DeclSpec::TST_int: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 153 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 154 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 155 | case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; |
| 156 | case DeclSpec::TSW_short: Result = Context.ShortTy; break; |
| 157 | case DeclSpec::TSW_long: Result = Context.LongTy; break; |
| 158 | case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | } |
| 160 | } else { |
| 161 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 162 | case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; |
| 163 | case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; |
| 164 | case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; |
| 165 | case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 168 | break; |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 169 | } |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 170 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 171 | case DeclSpec::TST_double: |
| 172 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 173 | Result = Context.LongDoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 174 | else |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 175 | Result = Context.DoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 176 | break; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 177 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | case DeclSpec::TST_decimal32: // _Decimal32 |
| 179 | case DeclSpec::TST_decimal64: // _Decimal64 |
| 180 | case DeclSpec::TST_decimal128: // _Decimal128 |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 181 | Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); |
| 182 | Result = Context.IntTy; |
| 183 | isInvalid = true; |
| 184 | break; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 185 | case DeclSpec::TST_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | case DeclSpec::TST_enum: |
| 187 | case DeclSpec::TST_union: |
| 188 | case DeclSpec::TST_struct: { |
| 189 | Decl *D = static_cast<Decl *>(DS.getTypeRep()); |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 190 | assert(D && "Didn't get a decl for a class/enum/union/struct?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 191 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 192 | DS.getTypeSpecSign() == 0 && |
| 193 | "Can't handle qualifiers on typedef names yet!"); |
| 194 | // TypeQuals handled by caller. |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 195 | Result = Context.getTypeDeclType(cast<TypeDecl>(D)); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 196 | |
| 197 | if (D->isInvalidDecl()) |
| 198 | isInvalid = true; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 199 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | } |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 201 | case DeclSpec::TST_typename: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 203 | DS.getTypeSpecSign() == 0 && |
| 204 | "Can't handle qualifiers on typedef names yet!"); |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 205 | Result = QualType::getFromOpaquePtr(DS.getTypeRep()); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 206 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 207 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 208 | if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType()) |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 209 | // It would be nice if protocol qualifiers were only stored with the |
| 210 | // ObjCObjectPointerType. Unfortunately, this isn't possible due |
| 211 | // to the following typedef idiom (which is uncommon, but allowed): |
| 212 | // |
| 213 | // typedef Foo<P> T; |
| 214 | // static void func() { |
| 215 | // Foo<P> *yy; |
| 216 | // T *zz; |
| 217 | // } |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 218 | Result = Context.getObjCInterfaceType(Interface->getDecl(), |
| 219 | (ObjCProtocolDecl**)PQ, |
| 220 | DS.getNumProtocolQualifiers()); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 221 | else if (Result->isObjCIdType()) |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 222 | // id<protocol-list> |
Steve Naroff | 470301b | 2009-07-22 16:07:01 +0000 | [diff] [blame] | 223 | Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 224 | (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers()); |
| 225 | else if (Result->isObjCClassType()) { |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 226 | if (DeclLoc.isInvalid()) |
| 227 | DeclLoc = DS.getSourceRange().getBegin(); |
Steve Naroff | 4262a07 | 2009-02-23 18:53:24 +0000 | [diff] [blame] | 228 | // Class<protocol-list> |
Steve Naroff | 470301b | 2009-07-22 16:07:01 +0000 | [diff] [blame] | 229 | Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy, |
| 230 | (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers()); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 231 | } else { |
| 232 | if (DeclLoc.isInvalid()) |
| 233 | DeclLoc = DS.getSourceRange().getBegin(); |
| 234 | Diag(DeclLoc, diag::err_invalid_protocol_qualifiers) |
| 235 | << DS.getSourceRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 236 | isInvalid = true; |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 237 | } |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 238 | } |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 239 | |
| 240 | // If this is a reference to an invalid typedef, propagate the invalidity. |
| 241 | if (TypedefType *TDT = dyn_cast<TypedefType>(Result)) |
| 242 | if (TDT->getDecl()->isInvalidDecl()) |
| 243 | isInvalid = true; |
| 244 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | // TypeQuals handled by caller. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 246 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 248 | case DeclSpec::TST_typeofType: |
| 249 | Result = QualType::getFromOpaquePtr(DS.getTypeRep()); |
| 250 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 251 | // TypeQuals handled by caller. |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 252 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 253 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 254 | case DeclSpec::TST_typeofExpr: { |
| 255 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 256 | assert(E && "Didn't get an expression for typeof?"); |
| 257 | // TypeQuals handled by caller. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 258 | Result = Context.getTypeOfExprType(E); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 259 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 260 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 261 | case DeclSpec::TST_decltype: { |
| 262 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 263 | assert(E && "Didn't get an expression for decltype?"); |
| 264 | // TypeQuals handled by caller. |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 265 | Result = BuildDecltypeType(E); |
| 266 | if (Result.isNull()) { |
| 267 | Result = Context.IntTy; |
| 268 | isInvalid = true; |
| 269 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 270 | break; |
| 271 | } |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 272 | case DeclSpec::TST_auto: { |
| 273 | // TypeQuals handled by caller. |
| 274 | Result = Context.UndeducedAutoTy; |
| 275 | break; |
| 276 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 277 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 278 | case DeclSpec::TST_error: |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 279 | Result = Context.IntTy; |
| 280 | isInvalid = true; |
| 281 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 282 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 283 | |
| 284 | // Handle complex types. |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 285 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { |
| 286 | if (getLangOptions().Freestanding) |
| 287 | Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 288 | Result = Context.getComplexType(Result); |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 289 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 290 | |
| 291 | assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && |
| 292 | "FIXME: imaginary types not supported yet!"); |
| 293 | |
Chris Lattner | 38d8b98 | 2008-02-20 22:04:11 +0000 | [diff] [blame] | 294 | // See if there are any attributes on the declspec that apply to the type (as |
| 295 | // opposed to the decl). |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 296 | if (const AttributeList *AL = DS.getAttributes()) |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 297 | ProcessTypeAttributeList(Result, AL); |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 299 | // Apply const/volatile/restrict qualifiers to T. |
| 300 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 301 | |
| 302 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 303 | // or incomplete types shall not be restrict-qualified." C++ also allows |
| 304 | // restrict-qualified references. |
| 305 | if (TypeQuals & QualType::Restrict) { |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 306 | if (Result->isPointerType() || Result->isReferenceType()) { |
| 307 | QualType EltTy = Result->isPointerType() ? |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 308 | Result->getAs<PointerType>()->getPointeeType() : |
| 309 | Result->getAs<ReferenceType>()->getPointeeType(); |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 310 | |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 311 | // If we have a pointer or reference, the pointee must have an object |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 312 | // incomplete type. |
| 313 | if (!EltTy->isIncompleteOrObjectType()) { |
| 314 | Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 315 | diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 316 | << EltTy << DS.getSourceRange(); |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 317 | TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. |
| 318 | } |
| 319 | } else { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 320 | Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 321 | diag::err_typecheck_invalid_restrict_not_pointer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 322 | << Result << DS.getSourceRange(); |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 323 | TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 324 | } |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification |
| 328 | // of a function type includes any type qualifiers, the behavior is |
| 329 | // undefined." |
| 330 | if (Result->isFunctionType() && TypeQuals) { |
| 331 | // Get some location to point at, either the C or V location. |
| 332 | SourceLocation Loc; |
| 333 | if (TypeQuals & QualType::Const) |
| 334 | Loc = DS.getConstSpecLoc(); |
| 335 | else { |
| 336 | assert((TypeQuals & QualType::Volatile) && |
| 337 | "Has CV quals but not C or V?"); |
| 338 | Loc = DS.getVolatileSpecLoc(); |
| 339 | } |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 340 | Diag(Loc, diag::warn_typecheck_function_qualifiers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 341 | << Result << DS.getSourceRange(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 344 | // C++ [dcl.ref]p1: |
| 345 | // Cv-qualified references are ill-formed except when the |
| 346 | // cv-qualifiers are introduced through the use of a typedef |
| 347 | // (7.1.3) or of a template type argument (14.3), in which |
| 348 | // case the cv-qualifiers are ignored. |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 349 | // FIXME: Shouldn't we be checking SCS_typedef here? |
| 350 | if (DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 351 | TypeQuals && Result->isReferenceType()) { |
| 352 | TypeQuals &= ~QualType::Const; |
| 353 | TypeQuals &= ~QualType::Volatile; |
| 354 | } |
| 355 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 356 | Result = Result.getQualifiedType(TypeQuals); |
| 357 | } |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 358 | return Result; |
| 359 | } |
| 360 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 361 | static std::string getPrintableNameForEntity(DeclarationName Entity) { |
| 362 | if (Entity) |
| 363 | return Entity.getAsString(); |
| 364 | |
| 365 | return "type name"; |
| 366 | } |
| 367 | |
| 368 | /// \brief Build a pointer type. |
| 369 | /// |
| 370 | /// \param T The type to which we'll be building a pointer. |
| 371 | /// |
| 372 | /// \param Quals The cvr-qualifiers to be applied to the pointer type. |
| 373 | /// |
| 374 | /// \param Loc The location of the entity whose type involves this |
| 375 | /// pointer type or, if there is no such entity, the location of the |
| 376 | /// type that will have pointer type. |
| 377 | /// |
| 378 | /// \param Entity The name of the entity that involves the pointer |
| 379 | /// type, if known. |
| 380 | /// |
| 381 | /// \returns A suitable pointer type, if there are no |
| 382 | /// errors. Otherwise, returns a NULL type. |
| 383 | QualType Sema::BuildPointerType(QualType T, unsigned Quals, |
| 384 | SourceLocation Loc, DeclarationName Entity) { |
| 385 | if (T->isReferenceType()) { |
| 386 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
| 387 | Diag(Loc, diag::err_illegal_decl_pointer_to_reference) |
| 388 | << getPrintableNameForEntity(Entity); |
| 389 | return QualType(); |
| 390 | } |
| 391 | |
| 392 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 393 | // object or incomplete types shall not be restrict-qualified." |
| 394 | if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) { |
| 395 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 396 | << T; |
| 397 | Quals &= ~QualType::Restrict; |
| 398 | } |
| 399 | |
| 400 | // Build the pointer type. |
| 401 | return Context.getPointerType(T).getQualifiedType(Quals); |
| 402 | } |
| 403 | |
| 404 | /// \brief Build a reference type. |
| 405 | /// |
| 406 | /// \param T The type to which we'll be building a reference. |
| 407 | /// |
| 408 | /// \param Quals The cvr-qualifiers to be applied to the reference type. |
| 409 | /// |
| 410 | /// \param Loc The location of the entity whose type involves this |
| 411 | /// reference type or, if there is no such entity, the location of the |
| 412 | /// type that will have reference type. |
| 413 | /// |
| 414 | /// \param Entity The name of the entity that involves the reference |
| 415 | /// type, if known. |
| 416 | /// |
| 417 | /// \returns A suitable reference type, if there are no |
| 418 | /// errors. Otherwise, returns a NULL type. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 419 | QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 420 | SourceLocation Loc, DeclarationName Entity) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 421 | if (LValueRef) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 422 | if (const RValueReferenceType *R = T->getAs<RValueReferenceType>()) { |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 423 | // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a |
| 424 | // reference to a type T, and attempt to create the type "lvalue |
| 425 | // reference to cv TD" creates the type "lvalue reference to T". |
| 426 | // We use the qualifiers (restrict or none) of the original reference, |
| 427 | // not the new ones. This is consistent with GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 428 | return Context.getLValueReferenceType(R->getPointeeType()). |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 429 | getQualifiedType(T.getCVRQualifiers()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 432 | if (T->isReferenceType()) { |
| 433 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 434 | // |
| 435 | // According to C++ DR 106, references to references are only |
| 436 | // diagnosed when they are written directly (e.g., "int & &"), |
| 437 | // but not when they happen via a typedef: |
| 438 | // |
| 439 | // typedef int& intref; |
| 440 | // typedef intref& intref2; |
| 441 | // |
| 442 | // Parser::ParserDeclaratorInternal diagnoses the case where |
| 443 | // references are written directly; here, we handle the |
| 444 | // collapsing of references-to-references as described in C++ |
| 445 | // DR 106 and amended by C++ DR 540. |
| 446 | return T; |
| 447 | } |
| 448 | |
| 449 | // C++ [dcl.ref]p1: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 450 | // A declarator that specifies the type "reference to cv void" |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 451 | // is ill-formed. |
| 452 | if (T->isVoidType()) { |
| 453 | Diag(Loc, diag::err_reference_to_void); |
| 454 | return QualType(); |
| 455 | } |
| 456 | |
| 457 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 458 | // object or incomplete types shall not be restrict-qualified." |
| 459 | if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) { |
| 460 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 461 | << T; |
| 462 | Quals &= ~QualType::Restrict; |
| 463 | } |
| 464 | |
| 465 | // C++ [dcl.ref]p1: |
| 466 | // [...] Cv-qualified references are ill-formed except when the |
| 467 | // cv-qualifiers are introduced through the use of a typedef |
| 468 | // (7.1.3) or of a template type argument (14.3), in which case |
| 469 | // the cv-qualifiers are ignored. |
| 470 | // |
| 471 | // We diagnose extraneous cv-qualifiers for the non-typedef, |
| 472 | // non-template type argument case within the parser. Here, we just |
| 473 | // ignore any extraneous cv-qualifiers. |
| 474 | Quals &= ~QualType::Const; |
| 475 | Quals &= ~QualType::Volatile; |
| 476 | |
| 477 | // Handle restrict on references. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 478 | if (LValueRef) |
| 479 | return Context.getLValueReferenceType(T).getQualifiedType(Quals); |
| 480 | return Context.getRValueReferenceType(T).getQualifiedType(Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /// \brief Build an array type. |
| 484 | /// |
| 485 | /// \param T The type of each element in the array. |
| 486 | /// |
| 487 | /// \param ASM C99 array size modifier (e.g., '*', 'static'). |
| 488 | /// |
| 489 | /// \param ArraySize Expression describing the size of the array. |
| 490 | /// |
| 491 | /// \param Quals The cvr-qualifiers to be applied to the array's |
| 492 | /// element type. |
| 493 | /// |
| 494 | /// \param Loc The location of the entity whose type involves this |
| 495 | /// array type or, if there is no such entity, the location of the |
| 496 | /// type that will have array type. |
| 497 | /// |
| 498 | /// \param Entity The name of the entity that involves the array |
| 499 | /// type, if known. |
| 500 | /// |
| 501 | /// \returns A suitable array type, if there are no errors. Otherwise, |
| 502 | /// returns a NULL type. |
| 503 | QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, |
| 504 | Expr *ArraySize, unsigned Quals, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 505 | SourceRange Brackets, DeclarationName Entity) { |
| 506 | SourceLocation Loc = Brackets.getBegin(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 507 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
| 508 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 509 | if (RequireCompleteType(Loc, T, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 510 | diag::err_illegal_decl_array_incomplete_type)) |
| 511 | return QualType(); |
| 512 | |
| 513 | if (T->isFunctionType()) { |
| 514 | Diag(Loc, diag::err_illegal_decl_array_of_functions) |
| 515 | << getPrintableNameForEntity(Entity); |
| 516 | return QualType(); |
| 517 | } |
| 518 | |
| 519 | // C++ 8.3.2p4: There shall be no ... arrays of references ... |
| 520 | if (T->isReferenceType()) { |
| 521 | Diag(Loc, diag::err_illegal_decl_array_of_references) |
| 522 | << getPrintableNameForEntity(Entity); |
| 523 | return QualType(); |
| 524 | } |
| 525 | |
Anders Carlsson | e7cf07d | 2009-06-26 19:33:28 +0000 | [diff] [blame] | 526 | if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) { |
| 527 | Diag(Loc, diag::err_illegal_decl_array_of_auto) |
| 528 | << getPrintableNameForEntity(Entity); |
| 529 | return QualType(); |
| 530 | } |
| 531 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 532 | if (const RecordType *EltTy = T->getAs<RecordType>()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 533 | // If the element type is a struct or union that contains a variadic |
| 534 | // array, accept it as a GNU extension: C99 6.7.2.1p2. |
| 535 | if (EltTy->getDecl()->hasFlexibleArrayMember()) |
| 536 | Diag(Loc, diag::ext_flexible_array_in_array) << T; |
| 537 | } else if (T->isObjCInterfaceType()) { |
Chris Lattner | c7c11b1 | 2009-04-27 01:55:56 +0000 | [diff] [blame] | 538 | Diag(Loc, diag::err_objc_array_of_interfaces) << T; |
| 539 | return QualType(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | // C99 6.7.5.2p1: The size expression shall have integer type. |
| 543 | if (ArraySize && !ArraySize->isTypeDependent() && |
| 544 | !ArraySize->getType()->isIntegerType()) { |
| 545 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) |
| 546 | << ArraySize->getType() << ArraySize->getSourceRange(); |
| 547 | ArraySize->Destroy(Context); |
| 548 | return QualType(); |
| 549 | } |
| 550 | llvm::APSInt ConstVal(32); |
| 551 | if (!ArraySize) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 552 | if (ASM == ArrayType::Star) |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 553 | T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets); |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 554 | else |
| 555 | T = Context.getIncompleteArrayType(T, ASM, Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 556 | } else if (ArraySize->isValueDependent()) { |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 557 | T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 558 | } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || |
| 559 | (!T->isDependentType() && !T->isConstantSizeType())) { |
| 560 | // Per C99, a variable array is an array with either a non-constant |
| 561 | // size or an element type that has a non-constant-size |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 562 | T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 563 | } else { |
| 564 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 565 | // have a value greater than zero. |
| 566 | if (ConstVal.isSigned()) { |
| 567 | if (ConstVal.isNegative()) { |
| 568 | Diag(ArraySize->getLocStart(), |
| 569 | diag::err_typecheck_negative_array_size) |
| 570 | << ArraySize->getSourceRange(); |
| 571 | return QualType(); |
| 572 | } else if (ConstVal == 0) { |
| 573 | // GCC accepts zero sized static arrays. |
| 574 | Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size) |
| 575 | << ArraySize->getSourceRange(); |
| 576 | } |
| 577 | } |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 578 | T = Context.getConstantArrayWithExprType(T, ConstVal, ArraySize, |
| 579 | ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 580 | } |
| 581 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
| 582 | if (!getLangOptions().C99) { |
| 583 | if (ArraySize && !ArraySize->isTypeDependent() && |
| 584 | !ArraySize->isValueDependent() && |
| 585 | !ArraySize->isIntegerConstantExpr(Context)) |
| 586 | Diag(Loc, diag::ext_vla); |
| 587 | else if (ASM != ArrayType::Normal || Quals != 0) |
| 588 | Diag(Loc, diag::ext_c99_array_usage); |
| 589 | } |
| 590 | |
| 591 | return T; |
| 592 | } |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 593 | |
| 594 | /// \brief Build an ext-vector type. |
| 595 | /// |
| 596 | /// Run the required checks for the extended vector type. |
| 597 | QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize, |
| 598 | SourceLocation AttrLoc) { |
| 599 | |
| 600 | Expr *Arg = (Expr *)ArraySize.get(); |
| 601 | |
| 602 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 603 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
| 604 | if (!T->isDependentType() && |
| 605 | !T->isIntegerType() && !T->isRealFloatingType()) { |
| 606 | Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T; |
| 607 | return QualType(); |
| 608 | } |
| 609 | |
| 610 | if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { |
| 611 | llvm::APSInt vecSize(32); |
| 612 | if (!Arg->isIntegerConstantExpr(vecSize, Context)) { |
| 613 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
| 614 | << "ext_vector_type" << Arg->getSourceRange(); |
| 615 | return QualType(); |
| 616 | } |
| 617 | |
| 618 | // unlike gcc's vector_size attribute, the size is specified as the |
| 619 | // number of elements, not the number of bytes. |
| 620 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 621 | |
| 622 | if (vectorSize == 0) { |
| 623 | Diag(AttrLoc, diag::err_attribute_zero_size) |
| 624 | << Arg->getSourceRange(); |
| 625 | return QualType(); |
| 626 | } |
| 627 | |
| 628 | if (!T->isDependentType()) |
| 629 | return Context.getExtVectorType(T, vectorSize); |
| 630 | } |
| 631 | |
| 632 | return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(), |
| 633 | AttrLoc); |
| 634 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 635 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 636 | /// \brief Build a function type. |
| 637 | /// |
| 638 | /// This routine checks the function type according to C++ rules and |
| 639 | /// under the assumption that the result type and parameter types have |
| 640 | /// just been instantiated from a template. It therefore duplicates |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 641 | /// some of the behavior of GetTypeForDeclarator, but in a much |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 642 | /// simpler form that is only suitable for this narrow use case. |
| 643 | /// |
| 644 | /// \param T The return type of the function. |
| 645 | /// |
| 646 | /// \param ParamTypes The parameter types of the function. This array |
| 647 | /// will be modified to account for adjustments to the types of the |
| 648 | /// function parameters. |
| 649 | /// |
| 650 | /// \param NumParamTypes The number of parameter types in ParamTypes. |
| 651 | /// |
| 652 | /// \param Variadic Whether this is a variadic function type. |
| 653 | /// |
| 654 | /// \param Quals The cvr-qualifiers to be applied to the function type. |
| 655 | /// |
| 656 | /// \param Loc The location of the entity whose type involves this |
| 657 | /// function type or, if there is no such entity, the location of the |
| 658 | /// type that will have function type. |
| 659 | /// |
| 660 | /// \param Entity The name of the entity that involves the function |
| 661 | /// type, if known. |
| 662 | /// |
| 663 | /// \returns A suitable function type, if there are no |
| 664 | /// errors. Otherwise, returns a NULL type. |
| 665 | QualType Sema::BuildFunctionType(QualType T, |
| 666 | QualType *ParamTypes, |
| 667 | unsigned NumParamTypes, |
| 668 | bool Variadic, unsigned Quals, |
| 669 | SourceLocation Loc, DeclarationName Entity) { |
| 670 | if (T->isArrayType() || T->isFunctionType()) { |
| 671 | Diag(Loc, diag::err_func_returning_array_function) << T; |
| 672 | return QualType(); |
| 673 | } |
| 674 | |
| 675 | bool Invalid = false; |
| 676 | for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) { |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 677 | QualType ParamType = adjustParameterType(ParamTypes[Idx]); |
| 678 | if (ParamType->isVoidType()) { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 679 | Diag(Loc, diag::err_param_with_void_type); |
| 680 | Invalid = true; |
| 681 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 682 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 683 | ParamTypes[Idx] = ParamType; |
| 684 | } |
| 685 | |
| 686 | if (Invalid) |
| 687 | return QualType(); |
| 688 | |
| 689 | return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic, |
| 690 | Quals); |
| 691 | } |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 692 | |
| 693 | /// \brief Build a member pointer type \c T Class::*. |
| 694 | /// |
| 695 | /// \param T the type to which the member pointer refers. |
| 696 | /// \param Class the class type into which the member pointer points. |
| 697 | /// \param Quals Qualifiers applied to the member pointer type |
| 698 | /// \param Loc the location where this type begins |
| 699 | /// \param Entity the name of the entity that will have this member pointer type |
| 700 | /// |
| 701 | /// \returns a member pointer type, if successful, or a NULL type if there was |
| 702 | /// an error. |
| 703 | QualType Sema::BuildMemberPointerType(QualType T, QualType Class, |
| 704 | unsigned Quals, SourceLocation Loc, |
| 705 | DeclarationName Entity) { |
| 706 | // Verify that we're not building a pointer to pointer to function with |
| 707 | // exception specification. |
| 708 | if (CheckDistantExceptionSpec(T)) { |
| 709 | Diag(Loc, diag::err_distant_exception_spec); |
| 710 | |
| 711 | // FIXME: If we're doing this as part of template instantiation, |
| 712 | // we should return immediately. |
| 713 | |
| 714 | // Build the type anyway, but use the canonical type so that the |
| 715 | // exception specifiers are stripped off. |
| 716 | T = Context.getCanonicalType(T); |
| 717 | } |
| 718 | |
| 719 | // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member |
| 720 | // with reference type, or "cv void." |
| 721 | if (T->isReferenceType()) { |
Anders Carlsson | 8d4655d | 2009-06-30 00:06:57 +0000 | [diff] [blame] | 722 | Diag(Loc, diag::err_illegal_decl_mempointer_to_reference) |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 723 | << (Entity? Entity.getAsString() : "type name"); |
| 724 | return QualType(); |
| 725 | } |
| 726 | |
| 727 | if (T->isVoidType()) { |
| 728 | Diag(Loc, diag::err_illegal_decl_mempointer_to_void) |
| 729 | << (Entity? Entity.getAsString() : "type name"); |
| 730 | return QualType(); |
| 731 | } |
| 732 | |
| 733 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 734 | // object or incomplete types shall not be restrict-qualified." |
| 735 | if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) { |
| 736 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 737 | << T; |
| 738 | |
| 739 | // FIXME: If we're doing this as part of template instantiation, |
| 740 | // we should return immediately. |
| 741 | Quals &= ~QualType::Restrict; |
| 742 | } |
| 743 | |
| 744 | if (!Class->isDependentType() && !Class->isRecordType()) { |
| 745 | Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class; |
| 746 | return QualType(); |
| 747 | } |
| 748 | |
| 749 | return Context.getMemberPointerType(T, Class.getTypePtr()) |
| 750 | .getQualifiedType(Quals); |
| 751 | } |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 752 | |
| 753 | /// \brief Build a block pointer type. |
| 754 | /// |
| 755 | /// \param T The type to which we'll be building a block pointer. |
| 756 | /// |
| 757 | /// \param Quals The cvr-qualifiers to be applied to the block pointer type. |
| 758 | /// |
| 759 | /// \param Loc The location of the entity whose type involves this |
| 760 | /// block pointer type or, if there is no such entity, the location of the |
| 761 | /// type that will have block pointer type. |
| 762 | /// |
| 763 | /// \param Entity The name of the entity that involves the block pointer |
| 764 | /// type, if known. |
| 765 | /// |
| 766 | /// \returns A suitable block pointer type, if there are no |
| 767 | /// errors. Otherwise, returns a NULL type. |
| 768 | QualType Sema::BuildBlockPointerType(QualType T, unsigned Quals, |
| 769 | SourceLocation Loc, |
| 770 | DeclarationName Entity) { |
| 771 | if (!T.getTypePtr()->isFunctionType()) { |
| 772 | Diag(Loc, diag::err_nonfunction_block_type); |
| 773 | return QualType(); |
| 774 | } |
| 775 | |
| 776 | return Context.getBlockPointerType(T).getQualifiedType(Quals); |
| 777 | } |
| 778 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 779 | /// GetTypeForDeclarator - Convert the type for the specified |
| 780 | /// declarator to Type instances. Skip the outermost Skip type |
| 781 | /// objects. |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 782 | /// |
| 783 | /// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq |
| 784 | /// owns the declaration of a type (e.g., the definition of a struct |
| 785 | /// type), then *OwnedDecl will receive the owned declaration. |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame^] | 786 | QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, |
| 787 | DeclaratorInfo **DInfo, unsigned Skip, |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 788 | TagDecl **OwnedDecl) { |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 789 | bool OmittedReturnType = false; |
| 790 | |
| 791 | if (D.getContext() == Declarator::BlockLiteralContext |
| 792 | && Skip == 0 |
| 793 | && !D.getDeclSpec().hasTypeSpecifier() |
| 794 | && (D.getNumTypeObjects() == 0 |
| 795 | || (D.getNumTypeObjects() == 1 |
| 796 | && D.getTypeObject(0).Kind == DeclaratorChunk::Function))) |
| 797 | OmittedReturnType = true; |
| 798 | |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 799 | // long long is a C99 feature. |
Chris Lattner | d1eb332 | 2007-08-28 16:41:29 +0000 | [diff] [blame] | 800 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 801 | D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) |
| 802 | Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 803 | |
| 804 | // Determine the type of the declarator. Not all forms of declarator |
| 805 | // have a type. |
| 806 | QualType T; |
| 807 | switch (D.getKind()) { |
| 808 | case Declarator::DK_Abstract: |
| 809 | case Declarator::DK_Normal: |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 810 | case Declarator::DK_Operator: { |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 811 | const DeclSpec &DS = D.getDeclSpec(); |
| 812 | if (OmittedReturnType) { |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 813 | // We default to a dependent type initially. Can be modified by |
| 814 | // the first return statement. |
| 815 | T = Context.DependentTy; |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 816 | } else { |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 817 | bool isInvalid = false; |
| 818 | T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 819 | if (isInvalid) |
| 820 | D.setInvalidType(true); |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 821 | else if (OwnedDecl && DS.isTypeSpecOwned()) |
| 822 | *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep()); |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 823 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 824 | break; |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 825 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 826 | |
| 827 | case Declarator::DK_Constructor: |
| 828 | case Declarator::DK_Destructor: |
| 829 | case Declarator::DK_Conversion: |
| 830 | // Constructors and destructors don't have return types. Use |
| 831 | // "void" instead. Conversion operators will check their return |
| 832 | // types separately. |
| 833 | T = Context.VoidTy; |
| 834 | break; |
| 835 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 836 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 837 | if (T == Context.UndeducedAutoTy) { |
| 838 | int Error = -1; |
| 839 | |
| 840 | switch (D.getContext()) { |
| 841 | case Declarator::KNRTypeListContext: |
| 842 | assert(0 && "K&R type lists aren't allowed in C++"); |
| 843 | break; |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 844 | case Declarator::PrototypeContext: |
| 845 | Error = 0; // Function prototype |
| 846 | break; |
| 847 | case Declarator::MemberContext: |
| 848 | switch (cast<TagDecl>(CurContext)->getTagKind()) { |
| 849 | case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break; |
| 850 | case TagDecl::TK_struct: Error = 1; /* Struct member */ break; |
| 851 | case TagDecl::TK_union: Error = 2; /* Union member */ break; |
| 852 | case TagDecl::TK_class: Error = 3; /* Class member */ break; |
| 853 | } |
| 854 | break; |
| 855 | case Declarator::CXXCatchContext: |
| 856 | Error = 4; // Exception declaration |
| 857 | break; |
| 858 | case Declarator::TemplateParamContext: |
| 859 | Error = 5; // Template parameter |
| 860 | break; |
| 861 | case Declarator::BlockLiteralContext: |
| 862 | Error = 6; // Block literal |
| 863 | break; |
| 864 | case Declarator::FileContext: |
| 865 | case Declarator::BlockContext: |
| 866 | case Declarator::ForContext: |
| 867 | case Declarator::ConditionContext: |
| 868 | case Declarator::TypeNameContext: |
| 869 | break; |
| 870 | } |
| 871 | |
| 872 | if (Error != -1) { |
| 873 | Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed) |
| 874 | << Error; |
| 875 | T = Context.IntTy; |
| 876 | D.setInvalidType(true); |
| 877 | } |
| 878 | } |
| 879 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 880 | // The name we're declaring, if any. |
| 881 | DeclarationName Name; |
| 882 | if (D.getIdentifier()) |
| 883 | Name = D.getIdentifier(); |
| 884 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 885 | // Walk the DeclTypeInfo, building the recursive type as we go. |
| 886 | // DeclTypeInfos are ordered from the identifier out, which is |
| 887 | // opposite of what we want :). |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 888 | for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) { |
| 889 | DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 890 | switch (DeclType.Kind) { |
| 891 | default: assert(0 && "Unknown decltype!"); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 892 | case DeclaratorChunk::BlockPointer: |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 893 | // If blocks are disabled, emit an error. |
| 894 | if (!LangOpts.Blocks) |
| 895 | Diag(DeclType.Loc, diag::err_blocks_disable); |
| 896 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 897 | T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(), |
| 898 | Name); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 899 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 900 | case DeclaratorChunk::Pointer: |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 901 | // Verify that we're not building a pointer to pointer to function with |
| 902 | // exception specification. |
| 903 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 904 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 905 | D.setInvalidType(true); |
| 906 | // Build the type anyway. |
| 907 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 908 | if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 909 | const ObjCInterfaceType *OIT = T->getAsObjCInterfaceType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 910 | T = Context.getObjCObjectPointerType(T, |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 911 | (ObjCProtocolDecl **)OIT->qual_begin(), |
| 912 | OIT->getNumProtocols()); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 913 | break; |
| 914 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 915 | T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 916 | break; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 917 | case DeclaratorChunk::Reference: |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 918 | // Verify that we're not building a reference to pointer to function with |
| 919 | // exception specification. |
| 920 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 921 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 922 | D.setInvalidType(true); |
| 923 | // Build the type anyway. |
| 924 | } |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 925 | T = BuildReferenceType(T, DeclType.Ref.LValueRef, |
| 926 | DeclType.Ref.HasRestrict ? QualType::Restrict : 0, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 927 | DeclType.Loc, Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 928 | break; |
| 929 | case DeclaratorChunk::Array: { |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 930 | // Verify that we're not building an array of pointers to function with |
| 931 | // exception specification. |
| 932 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 933 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 934 | D.setInvalidType(true); |
| 935 | // Build the type anyway. |
| 936 | } |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 937 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 938 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 939 | ArrayType::ArraySizeModifier ASM; |
| 940 | if (ATI.isStar) |
| 941 | ASM = ArrayType::Star; |
| 942 | else if (ATI.hasStatic) |
| 943 | ASM = ArrayType::Static; |
| 944 | else |
| 945 | ASM = ArrayType::Normal; |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 946 | if (ASM == ArrayType::Star && |
| 947 | D.getContext() != Declarator::PrototypeContext) { |
| 948 | // FIXME: This check isn't quite right: it allows star in prototypes |
| 949 | // for function definitions, and disallows some edge cases detailed |
| 950 | // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html |
| 951 | Diag(DeclType.Loc, diag::err_array_star_outside_prototype); |
| 952 | ASM = ArrayType::Normal; |
| 953 | D.setInvalidType(true); |
| 954 | } |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 955 | T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, |
| 956 | SourceRange(DeclType.Loc, DeclType.EndLoc), Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 957 | break; |
| 958 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 959 | case DeclaratorChunk::Function: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 960 | // If the function declarator has a prototype (i.e. it is not () and |
| 961 | // does not have a K&R-style identifier list), then the arguments are part |
| 962 | // of the type, otherwise the argument list is (). |
| 963 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 964 | |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 965 | // C99 6.7.5.3p1: The return type may not be a function or array type. |
Chris Lattner | 68cfd49 | 2007-12-19 18:01:43 +0000 | [diff] [blame] | 966 | if (T->isArrayType() || T->isFunctionType()) { |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 967 | Diag(DeclType.Loc, diag::err_func_returning_array_function) << T; |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 968 | T = Context.IntTy; |
| 969 | D.setInvalidType(true); |
| 970 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 971 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 972 | if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) { |
| 973 | // C++ [dcl.fct]p6: |
| 974 | // Types shall not be defined in return or parameter types. |
| 975 | TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep()); |
| 976 | if (Tag->isDefinition()) |
| 977 | Diag(Tag->getLocation(), diag::err_type_defined_in_result_type) |
| 978 | << Context.getTypeDeclType(Tag); |
| 979 | } |
| 980 | |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 981 | // Exception specs are not allowed in typedefs. Complain, but add it |
| 982 | // anyway. |
| 983 | if (FTI.hasExceptionSpec && |
| 984 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
| 985 | Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef); |
| 986 | |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 987 | if (FTI.NumArgs == 0) { |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 988 | if (getLangOptions().CPlusPlus) { |
| 989 | // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the |
| 990 | // function takes no arguments. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 991 | llvm::SmallVector<QualType, 4> Exceptions; |
| 992 | Exceptions.reserve(FTI.NumExceptions); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 993 | for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { |
| 994 | QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty); |
| 995 | // Check that the type is valid for an exception spec, and drop it |
| 996 | // if not. |
| 997 | if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) |
| 998 | Exceptions.push_back(ET); |
| 999 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1000 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals, |
| 1001 | FTI.hasExceptionSpec, |
| 1002 | FTI.hasAnyExceptionSpec, |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1003 | Exceptions.size(), Exceptions.data()); |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 1004 | } else if (FTI.isVariadic) { |
| 1005 | // We allow a zero-parameter variadic function in C if the |
| 1006 | // function is marked with the "overloadable" |
| 1007 | // attribute. Scan for this attribute now. |
| 1008 | bool Overloadable = false; |
| 1009 | for (const AttributeList *Attrs = D.getAttributes(); |
| 1010 | Attrs; Attrs = Attrs->getNext()) { |
| 1011 | if (Attrs->getKind() == AttributeList::AT_overloadable) { |
| 1012 | Overloadable = true; |
| 1013 | break; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | if (!Overloadable) |
| 1018 | Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg); |
| 1019 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 1020 | } else { |
| 1021 | // Simple void foo(), where the incoming T is the result type. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1022 | T = Context.getFunctionNoProtoType(T); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 1023 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1024 | } else if (FTI.ArgInfo[0].Param == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1025 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1026 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1027 | } else { |
| 1028 | // Otherwise, we have a function with an argument list that is |
| 1029 | // potentially variadic. |
| 1030 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1031 | |
| 1032 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1033 | ParmVarDecl *Param = |
| 1034 | cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>()); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1035 | QualType ArgTy = Param->getType(); |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 1036 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1037 | |
| 1038 | // Adjust the parameter type. |
Douglas Gregor | beb58cb | 2009-03-23 23:17:00 +0000 | [diff] [blame] | 1039 | assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1040 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1041 | // Look for 'void'. void is allowed only as a single argument to a |
| 1042 | // function with no other parameters (C99 6.7.5.3p10). We record |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1043 | // int(void) as a FunctionProtoType with an empty argument list. |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1044 | if (ArgTy->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1045 | // If this is something like 'float(int, void)', reject it. 'void' |
| 1046 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 1047 | // have arguments of incomplete type. |
| 1048 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 1049 | Diag(DeclType.Loc, diag::err_void_only_param); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1050 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1051 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1052 | } else if (FTI.ArgInfo[i].Ident) { |
| 1053 | // Reject, but continue to parse 'int(void abc)'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1054 | Diag(FTI.ArgInfo[i].IdentLoc, |
Chris Lattner | 4565d4e | 2007-07-21 05:26:43 +0000 | [diff] [blame] | 1055 | diag::err_param_with_void_type); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1056 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1057 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1058 | } else { |
| 1059 | // Reject, but continue to parse 'float(const void)'. |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1060 | if (ArgTy.getCVRQualifiers()) |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1061 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
| 1062 | |
| 1063 | // Do not add 'void' to the ArgTys list. |
| 1064 | break; |
| 1065 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1066 | } else if (!FTI.hasPrototype) { |
| 1067 | if (ArgTy->isPromotableIntegerType()) { |
| 1068 | ArgTy = Context.IntTy; |
| 1069 | } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) { |
| 1070 | if (BTy->getKind() == BuiltinType::Float) |
| 1071 | ArgTy = Context.DoubleTy; |
| 1072 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | ArgTys.push_back(ArgTy); |
| 1076 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1077 | |
| 1078 | llvm::SmallVector<QualType, 4> Exceptions; |
| 1079 | Exceptions.reserve(FTI.NumExceptions); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1080 | for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { |
| 1081 | QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty); |
| 1082 | // Check that the type is valid for an exception spec, and drop it if |
| 1083 | // not. |
| 1084 | if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) |
| 1085 | Exceptions.push_back(ET); |
| 1086 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1087 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1088 | T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1089 | FTI.isVariadic, FTI.TypeQuals, |
| 1090 | FTI.hasExceptionSpec, |
| 1091 | FTI.hasAnyExceptionSpec, |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1092 | Exceptions.size(), Exceptions.data()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1093 | } |
| 1094 | break; |
| 1095 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1096 | case DeclaratorChunk::MemberPointer: |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 1097 | // Verify that we're not building a pointer to pointer to function with |
| 1098 | // exception specification. |
| 1099 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1100 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1101 | D.setInvalidType(true); |
| 1102 | // Build the type anyway. |
| 1103 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1104 | // The scope spec must refer to a class, or be dependent. |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1105 | QualType ClsType; |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1106 | if (isDependentScopeSpecifier(DeclType.Mem.Scope())) { |
| 1107 | NestedNameSpecifier *NNS |
| 1108 | = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep(); |
| 1109 | assert(NNS->getAsType() && "Nested-name-specifier must name a type"); |
| 1110 | ClsType = QualType(NNS->getAsType(), 0); |
| 1111 | } else if (CXXRecordDecl *RD |
| 1112 | = dyn_cast_or_null<CXXRecordDecl>( |
| 1113 | computeDeclContext(DeclType.Mem.Scope()))) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1114 | ClsType = Context.getTagDeclType(RD); |
| 1115 | } else { |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1116 | Diag(DeclType.Mem.Scope().getBeginLoc(), |
| 1117 | diag::err_illegal_decl_mempointer_in_nonclass) |
| 1118 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") |
| 1119 | << DeclType.Mem.Scope().getRange(); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1120 | D.setInvalidType(true); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1123 | if (!ClsType.isNull()) |
| 1124 | T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals, |
| 1125 | DeclType.Loc, D.getIdentifier()); |
| 1126 | if (T.isNull()) { |
| 1127 | T = Context.IntTy; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1128 | D.setInvalidType(true); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1129 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1130 | break; |
| 1131 | } |
| 1132 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1133 | if (T.isNull()) { |
| 1134 | D.setInvalidType(true); |
| 1135 | T = Context.IntTy; |
| 1136 | } |
| 1137 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1138 | // See if there are any attributes on this declarator chunk. |
| 1139 | if (const AttributeList *AL = DeclType.getAttrs()) |
| 1140 | ProcessTypeAttributeList(T, AL); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1141 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1142 | |
| 1143 | if (getLangOptions().CPlusPlus && T->isFunctionType()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1144 | const FunctionProtoType *FnTy = T->getAsFunctionProtoType(); |
| 1145 | assert(FnTy && "Why oh why is there not a FunctionProtoType here ?"); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1146 | |
| 1147 | // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type |
| 1148 | // for a nonstatic member function, the function type to which a pointer |
| 1149 | // to member refers, or the top-level function type of a function typedef |
| 1150 | // declaration. |
| 1151 | if (FnTy->getTypeQuals() != 0 && |
| 1152 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1153 | ((D.getContext() != Declarator::MemberContext && |
| 1154 | (!D.getCXXScopeSpec().isSet() || |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1155 | !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true) |
| 1156 | ->isRecord())) || |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1157 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1158 | if (D.isFunctionDeclarator()) |
| 1159 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type); |
| 1160 | else |
| 1161 | Diag(D.getIdentifierLoc(), |
| 1162 | diag::err_invalid_qualified_typedef_function_type_use); |
| 1163 | |
| 1164 | // Strip the cv-quals from the type. |
| 1165 | T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1166 | FnTy->getNumArgs(), FnTy->isVariadic(), 0); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1169 | |
Chris Lattner | 0bf29ad | 2008-06-29 00:19:33 +0000 | [diff] [blame] | 1170 | // If there were any type attributes applied to the decl itself (not the |
| 1171 | // type, apply the type attribute to the type!) |
| 1172 | if (const AttributeList *Attrs = D.getAttributes()) |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1173 | ProcessTypeAttributeList(T, Attrs); |
Chris Lattner | 0bf29ad | 2008-06-29 00:19:33 +0000 | [diff] [blame] | 1174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1175 | return T; |
| 1176 | } |
| 1177 | |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1178 | /// CheckSpecifiedExceptionType - Check if the given type is valid in an |
| 1179 | /// exception specification. Incomplete types, or pointers to incomplete types |
| 1180 | /// other than void are not allowed. |
| 1181 | bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) { |
| 1182 | // FIXME: This may not correctly work with the fix for core issue 437, |
| 1183 | // where a class's own type is considered complete within its body. |
| 1184 | |
| 1185 | // C++ 15.4p2: A type denoted in an exception-specification shall not denote |
| 1186 | // an incomplete type. |
| 1187 | if (T->isIncompleteType()) |
| 1188 | return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec) |
| 1189 | << Range << T << /*direct*/0; |
| 1190 | |
| 1191 | // C++ 15.4p2: A type denoted in an exception-specification shall not denote |
| 1192 | // an incomplete type a pointer or reference to an incomplete type, other |
| 1193 | // than (cv) void*. |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1194 | int kind; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1195 | if (const PointerType* IT = T->getAs<PointerType>()) { |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1196 | T = IT->getPointeeType(); |
| 1197 | kind = 1; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1198 | } else if (const ReferenceType* IT = T->getAs<ReferenceType>()) { |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1199 | T = IT->getPointeeType(); |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1200 | kind = 2; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1201 | } else |
| 1202 | return false; |
| 1203 | |
| 1204 | if (T->isIncompleteType() && !T->isVoidType()) |
| 1205 | return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec) |
| 1206 | << Range << T << /*indirect*/kind; |
| 1207 | |
| 1208 | return false; |
| 1209 | } |
| 1210 | |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1211 | /// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer |
| 1212 | /// to member to a function with an exception specification. This means that |
| 1213 | /// it is invalid to add another level of indirection. |
| 1214 | bool Sema::CheckDistantExceptionSpec(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1215 | if (const PointerType *PT = T->getAs<PointerType>()) |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1216 | T = PT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1217 | else if (const MemberPointerType *PT = T->getAs<MemberPointerType>()) |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1218 | T = PT->getPointeeType(); |
| 1219 | else |
| 1220 | return false; |
| 1221 | |
| 1222 | const FunctionProtoType *FnT = T->getAsFunctionProtoType(); |
| 1223 | if (!FnT) |
| 1224 | return false; |
| 1225 | |
| 1226 | return FnT->hasExceptionSpec(); |
| 1227 | } |
| 1228 | |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 1229 | /// CheckEquivalentExceptionSpec - Check if the two types have equivalent |
| 1230 | /// exception specifications. Exception specifications are equivalent if |
| 1231 | /// they allow exactly the same set of exception types. It does not matter how |
| 1232 | /// that is achieved. See C++ [except.spec]p2. |
| 1233 | bool Sema::CheckEquivalentExceptionSpec( |
| 1234 | const FunctionProtoType *Old, SourceLocation OldLoc, |
| 1235 | const FunctionProtoType *New, SourceLocation NewLoc) { |
| 1236 | bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec(); |
| 1237 | bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec(); |
| 1238 | if (OldAny && NewAny) |
| 1239 | return false; |
| 1240 | if (OldAny || NewAny) { |
| 1241 | Diag(NewLoc, diag::err_mismatched_exception_spec); |
| 1242 | Diag(OldLoc, diag::note_previous_declaration); |
| 1243 | return true; |
| 1244 | } |
| 1245 | |
| 1246 | bool Success = true; |
| 1247 | // Both have a definite exception spec. Collect the first set, then compare |
| 1248 | // to the second. |
| 1249 | llvm::SmallPtrSet<const Type*, 8> Types; |
| 1250 | for (FunctionProtoType::exception_iterator I = Old->exception_begin(), |
| 1251 | E = Old->exception_end(); I != E; ++I) |
| 1252 | Types.insert(Context.getCanonicalType(*I).getTypePtr()); |
| 1253 | |
| 1254 | for (FunctionProtoType::exception_iterator I = New->exception_begin(), |
| 1255 | E = New->exception_end(); I != E && Success; ++I) |
| 1256 | Success = Types.erase(Context.getCanonicalType(*I).getTypePtr()); |
| 1257 | |
| 1258 | Success = Success && Types.empty(); |
| 1259 | |
| 1260 | if (Success) { |
| 1261 | return false; |
| 1262 | } |
| 1263 | Diag(NewLoc, diag::err_mismatched_exception_spec); |
| 1264 | Diag(OldLoc, diag::note_previous_declaration); |
| 1265 | return true; |
| 1266 | } |
| 1267 | |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 1268 | /// CheckExceptionSpecSubset - Check whether the second function type's |
| 1269 | /// exception specification is a subset (or equivalent) of the first function |
| 1270 | /// type. This is used by override and pointer assignment checks. |
| 1271 | bool Sema::CheckExceptionSpecSubset(unsigned DiagID, unsigned NoteID, |
| 1272 | const FunctionProtoType *Superset, SourceLocation SuperLoc, |
| 1273 | const FunctionProtoType *Subset, SourceLocation SubLoc) |
| 1274 | { |
| 1275 | // FIXME: As usual, we could be more specific in our error messages, but |
| 1276 | // that better waits until we've got types with source locations. |
| 1277 | |
| 1278 | // If superset contains everything, we're done. |
| 1279 | if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec()) |
| 1280 | return false; |
| 1281 | |
| 1282 | // It does not. If the subset contains everything, we've failed. |
| 1283 | if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) { |
| 1284 | Diag(SubLoc, DiagID); |
| 1285 | Diag(SuperLoc, NoteID); |
| 1286 | return true; |
| 1287 | } |
| 1288 | |
| 1289 | // Neither contains everything. Do a proper comparison. |
| 1290 | for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(), |
| 1291 | SubE = Subset->exception_end(); SubI != SubE; ++SubI) { |
| 1292 | // Take one type from the subset. |
| 1293 | QualType CanonicalSubT = Context.getCanonicalType(*SubI); |
| 1294 | bool SubIsPointer = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1295 | if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>()) |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 1296 | CanonicalSubT = RefTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1297 | if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) { |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 1298 | CanonicalSubT = PtrTy->getPointeeType(); |
| 1299 | SubIsPointer = true; |
| 1300 | } |
| 1301 | bool SubIsClass = CanonicalSubT->isRecordType(); |
| 1302 | CanonicalSubT.setCVRQualifiers(0); |
| 1303 | |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 1304 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 1305 | /*DetectVirtual=*/false); |
| 1306 | |
| 1307 | bool Contained = false; |
| 1308 | // Make sure it's in the superset. |
| 1309 | for (FunctionProtoType::exception_iterator SuperI = |
| 1310 | Superset->exception_begin(), SuperE = Superset->exception_end(); |
| 1311 | SuperI != SuperE; ++SuperI) { |
| 1312 | QualType CanonicalSuperT = Context.getCanonicalType(*SuperI); |
| 1313 | // SubT must be SuperT or derived from it, or pointer or reference to |
| 1314 | // such types. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1315 | if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>()) |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 1316 | CanonicalSuperT = RefTy->getPointeeType(); |
| 1317 | if (SubIsPointer) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1318 | if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>()) |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 1319 | CanonicalSuperT = PtrTy->getPointeeType(); |
| 1320 | else { |
| 1321 | continue; |
| 1322 | } |
| 1323 | } |
| 1324 | CanonicalSuperT.setCVRQualifiers(0); |
| 1325 | // If the types are the same, move on to the next type in the subset. |
| 1326 | if (CanonicalSubT == CanonicalSuperT) { |
| 1327 | Contained = true; |
| 1328 | break; |
| 1329 | } |
| 1330 | |
| 1331 | // Otherwise we need to check the inheritance. |
| 1332 | if (!SubIsClass || !CanonicalSuperT->isRecordType()) |
| 1333 | continue; |
| 1334 | |
| 1335 | Paths.clear(); |
| 1336 | if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths)) |
| 1337 | continue; |
| 1338 | |
| 1339 | if (Paths.isAmbiguous(CanonicalSuperT)) |
| 1340 | continue; |
| 1341 | |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 1342 | if (FindInaccessibleBase(CanonicalSubT, CanonicalSuperT, Paths, true)) |
| 1343 | continue; |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 1344 | |
| 1345 | Contained = true; |
| 1346 | break; |
| 1347 | } |
| 1348 | if (!Contained) { |
| 1349 | Diag(SubLoc, DiagID); |
| 1350 | Diag(SuperLoc, NoteID); |
| 1351 | return true; |
| 1352 | } |
| 1353 | } |
| 1354 | // We've run the gauntlet. |
| 1355 | return false; |
| 1356 | } |
| 1357 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1358 | /// ObjCGetTypeForMethodDefinition - Builds the type for a method definition |
Fariborz Jahanian | 360300c | 2007-11-09 22:27:59 +0000 | [diff] [blame] | 1359 | /// declarator |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1360 | QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) { |
| 1361 | ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>()); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1362 | QualType T = MDecl->getResultType(); |
| 1363 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1364 | |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 1365 | // Add the first two invisible argument types for self and _cmd. |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1366 | if (MDecl->isInstanceMethod()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1367 | QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 1368 | selfTy = Context.getPointerType(selfTy); |
| 1369 | ArgTys.push_back(selfTy); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1370 | } else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1371 | ArgTys.push_back(Context.getObjCIdType()); |
| 1372 | ArgTys.push_back(Context.getObjCSelType()); |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 1373 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1374 | for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), |
| 1375 | E = MDecl->param_end(); PI != E; ++PI) { |
| 1376 | QualType ArgTy = (*PI)->getType(); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1377 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Douglas Gregor | beb58cb | 2009-03-23 23:17:00 +0000 | [diff] [blame] | 1378 | ArgTy = adjustParameterType(ArgTy); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1379 | ArgTys.push_back(ArgTy); |
| 1380 | } |
| 1381 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1382 | MDecl->isVariadic(), 0); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1383 | return T; |
| 1384 | } |
| 1385 | |
Sebastian Redl | 9e5e4aa | 2009-01-26 19:54:48 +0000 | [diff] [blame] | 1386 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 1387 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 1388 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 1389 | /// or pointer-to-member types, or if they are not similar at this |
| 1390 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 1391 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 1392 | /// be called in a loop that successively "unwraps" pointer and |
| 1393 | /// pointer-to-member types to compare them at each level. |
Chris Lattner | ecb81f2 | 2009-02-16 21:43:00 +0000 | [diff] [blame] | 1394 | bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1395 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 1396 | *T2PtrType = T2->getAs<PointerType>(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1397 | if (T1PtrType && T2PtrType) { |
| 1398 | T1 = T1PtrType->getPointeeType(); |
| 1399 | T2 = T2PtrType->getPointeeType(); |
| 1400 | return true; |
| 1401 | } |
| 1402 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1403 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 1404 | *T2MPType = T2->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1405 | if (T1MPType && T2MPType && |
| 1406 | Context.getCanonicalType(T1MPType->getClass()) == |
| 1407 | Context.getCanonicalType(T2MPType->getClass())) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1408 | T1 = T1MPType->getPointeeType(); |
| 1409 | T2 = T2MPType->getPointeeType(); |
| 1410 | return true; |
| 1411 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1412 | return false; |
| 1413 | } |
| 1414 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1415 | Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1416 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 1417 | // the parser. |
| 1418 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
| 1419 | |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame^] | 1420 | DeclaratorInfo *DInfo = 0; |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1421 | TagDecl *OwnedTag = 0; |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame^] | 1422 | QualType T = GetTypeForDeclarator(D, S, &DInfo, /*Skip=*/0, &OwnedTag); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 1423 | if (D.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1424 | return true; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1425 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1426 | if (getLangOptions().CPlusPlus) { |
| 1427 | // Check that there are no default arguments (C++ only). |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 1428 | CheckExtraCXXDefaultArguments(D); |
| 1429 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1430 | // C++0x [dcl.type]p3: |
| 1431 | // A type-specifier-seq shall not define a class or enumeration |
| 1432 | // unless it appears in the type-id of an alias-declaration |
| 1433 | // (7.1.3). |
| 1434 | if (OwnedTag && OwnedTag->isDefinition()) |
| 1435 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier) |
| 1436 | << Context.getTypeDeclType(OwnedTag); |
| 1437 | } |
| 1438 | |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame^] | 1439 | //FIXME: Also pass DeclaratorInfo. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1440 | return T.getAsOpaquePtr(); |
| 1441 | } |
| 1442 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1443 | |
| 1444 | |
| 1445 | //===----------------------------------------------------------------------===// |
| 1446 | // Type Attribute Processing |
| 1447 | //===----------------------------------------------------------------------===// |
| 1448 | |
| 1449 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 1450 | /// specified type. The attribute contains 1 argument, the id of the address |
| 1451 | /// space for the type. |
| 1452 | static void HandleAddressSpaceTypeAttribute(QualType &Type, |
| 1453 | const AttributeList &Attr, Sema &S){ |
| 1454 | // If this type is already address space qualified, reject it. |
| 1455 | // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers |
| 1456 | // for two or more different address spaces." |
| 1457 | if (Type.getAddressSpace()) { |
| 1458 | S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); |
| 1459 | return; |
| 1460 | } |
| 1461 | |
| 1462 | // Check the attribute arguments. |
| 1463 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 1464 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1465 | return; |
| 1466 | } |
| 1467 | Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1468 | llvm::APSInt addrSpace(32); |
| 1469 | if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1470 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) |
| 1471 | << ASArgExpr->getSourceRange(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1472 | return; |
| 1473 | } |
| 1474 | |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1475 | // Bounds checking. |
| 1476 | if (addrSpace.isSigned()) { |
| 1477 | if (addrSpace.isNegative()) { |
| 1478 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative) |
| 1479 | << ASArgExpr->getSourceRange(); |
| 1480 | return; |
| 1481 | } |
| 1482 | addrSpace.setIsSigned(false); |
| 1483 | } |
| 1484 | llvm::APSInt max(addrSpace.getBitWidth()); |
| 1485 | max = QualType::MaxAddressSpace; |
| 1486 | if (addrSpace > max) { |
| 1487 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high) |
| 1488 | << QualType::MaxAddressSpace << ASArgExpr->getSourceRange(); |
| 1489 | return; |
| 1490 | } |
| 1491 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1492 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1493 | Type = S.Context.getAddrSpaceQualType(Type, ASIdx); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1496 | /// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the |
| 1497 | /// specified type. The attribute contains 1 argument, weak or strong. |
| 1498 | static void HandleObjCGCTypeAttribute(QualType &Type, |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1499 | const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1500 | if (Type.getObjCGCAttr() != QualType::GCNone) { |
Fariborz Jahanian | 5934e75 | 2009-02-18 18:52:41 +0000 | [diff] [blame] | 1501 | S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1502 | return; |
| 1503 | } |
| 1504 | |
| 1505 | // Check the attribute arguments. |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1506 | if (!Attr.getParameterName()) { |
| 1507 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1508 | << "objc_gc" << 1; |
| 1509 | return; |
| 1510 | } |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1511 | QualType::GCAttrTypes GCAttr; |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1512 | if (Attr.getNumArgs() != 0) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1513 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1514 | return; |
| 1515 | } |
| 1516 | if (Attr.getParameterName()->isStr("weak")) |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1517 | GCAttr = QualType::Weak; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1518 | else if (Attr.getParameterName()->isStr("strong")) |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1519 | GCAttr = QualType::Strong; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1520 | else { |
| 1521 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 1522 | << "objc_gc" << Attr.getParameterName(); |
| 1523 | return; |
| 1524 | } |
| 1525 | |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1526 | Type = S.Context.getObjCGCQualType(Type, GCAttr); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1529 | /// HandleNoReturnTypeAttribute - Process the noreturn attribute on the |
| 1530 | /// specified type. The attribute contains 0 arguments. |
| 1531 | static void HandleNoReturnTypeAttribute(QualType &Type, |
| 1532 | const AttributeList &Attr, Sema &S) { |
| 1533 | if (Attr.getNumArgs() != 0) |
| 1534 | return; |
| 1535 | |
| 1536 | // We only apply this to a pointer to function or a pointer to block. |
| 1537 | if (!Type->isFunctionPointerType() |
| 1538 | && !Type->isBlockPointerType() |
| 1539 | && !Type->isFunctionType()) |
| 1540 | return; |
| 1541 | |
| 1542 | Type = S.Context.getNoReturnType(Type); |
| 1543 | } |
| 1544 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1545 | void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1546 | // Scan through and apply attributes to this type where it makes sense. Some |
| 1547 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 1548 | // type, but others can be present in the type specifiers even though they |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 1549 | // apply to the decl. Here we apply type attributes and ignore the rest. |
| 1550 | for (; AL; AL = AL->getNext()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1551 | // If this is an attribute we can handle, do so now, otherwise, add it to |
| 1552 | // the LeftOverAttrs list for rechaining. |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 1553 | switch (AL->getKind()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1554 | default: break; |
| 1555 | case AttributeList::AT_address_space: |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1556 | HandleAddressSpaceTypeAttribute(Result, *AL, *this); |
| 1557 | break; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1558 | case AttributeList::AT_objc_gc: |
| 1559 | HandleObjCGCTypeAttribute(Result, *AL, *this); |
| 1560 | break; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1561 | case AttributeList::AT_noreturn: |
| 1562 | HandleNoReturnTypeAttribute(Result, *AL, *this); |
| 1563 | break; |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1564 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1565 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 1568 | /// @brief Ensure that the type T is a complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1569 | /// |
| 1570 | /// This routine checks whether the type @p T is complete in any |
| 1571 | /// context where a complete type is required. If @p T is a complete |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 1572 | /// type, returns false. If @p T is a class template specialization, |
| 1573 | /// this routine then attempts to perform class template |
| 1574 | /// instantiation. If instantiation fails, or if @p T is incomplete |
| 1575 | /// and cannot be completed, issues the diagnostic @p diag (giving it |
| 1576 | /// the type @p T) and returns true. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1577 | /// |
| 1578 | /// @param Loc The location in the source that the incomplete type |
| 1579 | /// diagnostic should refer to. |
| 1580 | /// |
| 1581 | /// @param T The type that this routine is examining for completeness. |
| 1582 | /// |
| 1583 | /// @param diag The diagnostic value (e.g., |
| 1584 | /// @c diag::err_typecheck_decl_incomplete_type) that will be used |
| 1585 | /// for the error message if @p T is incomplete. |
| 1586 | /// |
| 1587 | /// @param Range1 An optional range in the source code that will be a |
| 1588 | /// part of the "incomplete type" error message. |
| 1589 | /// |
| 1590 | /// @param Range2 An optional range in the source code that will be a |
| 1591 | /// part of the "incomplete type" error message. |
| 1592 | /// |
| 1593 | /// @param PrintType If non-NULL, the type that should be printed |
| 1594 | /// instead of @p T. This parameter should be used when the type that |
| 1595 | /// we're checking for incompleteness isn't the type that should be |
| 1596 | /// displayed to the user, e.g., when T is a type and PrintType is a |
| 1597 | /// pointer to T. |
| 1598 | /// |
| 1599 | /// @returns @c true if @p T is incomplete and a diagnostic was emitted, |
| 1600 | /// @c false otherwise. |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 1601 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag, |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1602 | SourceRange Range1, SourceRange Range2, |
| 1603 | QualType PrintType) { |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 1604 | // FIXME: Add this assertion to help us flush out problems with |
| 1605 | // checking for dependent types and type-dependent expressions. |
| 1606 | // |
| 1607 | // assert(!T->isDependentType() && |
| 1608 | // "Can't ask whether a dependent type is complete"); |
| 1609 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1610 | // If we have a complete type, we're done. |
| 1611 | if (!T->isIncompleteType()) |
| 1612 | return false; |
Eli Friedman | 3c0eb16 | 2008-05-27 03:33:27 +0000 | [diff] [blame] | 1613 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1614 | // If we have a class template specialization or a class member of a |
| 1615 | // class template specialization, try to instantiate it. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1616 | if (const RecordType *Record = T->getAs<RecordType>()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1617 | if (ClassTemplateSpecializationDecl *ClassTemplateSpec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1618 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1619 | if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) { |
| 1620 | // Update the class template specialization's location to |
| 1621 | // refer to the point of instantiation. |
| 1622 | if (Loc.isValid()) |
| 1623 | ClassTemplateSpec->setLocation(Loc); |
| 1624 | return InstantiateClassTemplateSpecialization(ClassTemplateSpec, |
| 1625 | /*ExplicitInstantiation=*/false); |
| 1626 | } |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1627 | } else if (CXXRecordDecl *Rec |
| 1628 | = dyn_cast<CXXRecordDecl>(Record->getDecl())) { |
| 1629 | if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) { |
| 1630 | // Find the class template specialization that surrounds this |
| 1631 | // member class. |
| 1632 | ClassTemplateSpecializationDecl *Spec = 0; |
| 1633 | for (DeclContext *Parent = Rec->getDeclContext(); |
| 1634 | Parent && !Spec; Parent = Parent->getParent()) |
| 1635 | Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent); |
| 1636 | assert(Spec && "Not a member of a class template specialization?"); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 1637 | return InstantiateClass(Loc, Rec, Pattern, Spec->getTemplateArgs(), |
| 1638 | /*ExplicitInstantiation=*/false); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1642 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1643 | if (PrintType.isNull()) |
| 1644 | PrintType = T; |
| 1645 | |
| 1646 | // We have an incomplete type. Produce a diagnostic. |
| 1647 | Diag(Loc, diag) << PrintType << Range1 << Range2; |
| 1648 | |
| 1649 | // If the type was a forward declaration of a class/struct/union |
| 1650 | // type, produce |
| 1651 | const TagType *Tag = 0; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1652 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1653 | Tag = Record; |
| 1654 | else if (const EnumType *Enum = T->getAsEnumType()) |
| 1655 | Tag = Enum; |
| 1656 | |
| 1657 | if (Tag && !Tag->getDecl()->isInvalidDecl()) |
| 1658 | Diag(Tag->getDecl()->getLocation(), |
| 1659 | Tag->isBeingDefined() ? diag::note_type_being_defined |
| 1660 | : diag::note_forward_declaration) |
| 1661 | << QualType(Tag, 0); |
| 1662 | |
| 1663 | return true; |
| 1664 | } |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 1665 | |
| 1666 | /// \brief Retrieve a version of the type 'T' that is qualified by the |
| 1667 | /// nested-name-specifier contained in SS. |
| 1668 | QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) { |
| 1669 | if (!SS.isSet() || SS.isInvalid() || T.isNull()) |
| 1670 | return T; |
| 1671 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1672 | NestedNameSpecifier *NNS |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 1673 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1674 | return Context.getQualifiedNameType(NNS, T); |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 1675 | } |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 1676 | |
| 1677 | QualType Sema::BuildTypeofExprType(Expr *E) { |
| 1678 | return Context.getTypeOfExprType(E); |
| 1679 | } |
| 1680 | |
| 1681 | QualType Sema::BuildDecltypeType(Expr *E) { |
| 1682 | if (E->getType() == Context.OverloadTy) { |
| 1683 | Diag(E->getLocStart(), |
| 1684 | diag::err_cannot_determine_declared_type_of_overloaded_function); |
| 1685 | return QualType(); |
| 1686 | } |
| 1687 | return Context.getDecltypeType(E); |
| 1688 | } |