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" |
| 15 | #include "clang/AST/ASTContext.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 19 | #include "clang/Parse/DeclSpec.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 22 | /// \brief Perform adjustment on the parameter type of a function. |
| 23 | /// |
| 24 | /// This routine adjusts the given parameter type @p T to the actual |
| 25 | /// parameter type used by semantic analysis (C99 6.7.5.3p[7,8], |
| 26 | /// C++ [dcl.fct]p3). The adjusted parameter type is returned. |
| 27 | QualType Sema::adjustParameterType(QualType T) { |
| 28 | // C99 6.7.5.3p7: |
| 29 | if (T->isArrayType()) { |
| 30 | // C99 6.7.5.3p7: |
| 31 | // A declaration of a parameter as "array of type" shall be |
| 32 | // adjusted to "qualified pointer to type", where the type |
| 33 | // qualifiers (if any) are those specified within the [ and ] of |
| 34 | // the array type derivation. |
| 35 | return Context.getArrayDecayedType(T); |
| 36 | } else if (T->isFunctionType()) |
| 37 | // C99 6.7.5.3p8: |
| 38 | // A declaration of a parameter as "function returning type" |
| 39 | // shall be adjusted to "pointer to function returning type", as |
| 40 | // in 6.3.2.1. |
| 41 | return Context.getPointerType(T); |
| 42 | |
| 43 | return T; |
| 44 | } |
| 45 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 46 | /// \brief Convert the specified declspec to the appropriate type |
| 47 | /// object. |
| 48 | /// \param DS the declaration specifiers |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 49 | /// \param DeclLoc The location of the declarator identifier or invalid if none. |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 50 | /// \returns The type described by the declaration specifiers. This function |
| 51 | /// never returns null. |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 52 | QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS, |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 53 | SourceLocation DeclLoc, |
| 54 | bool &isInvalid) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 56 | // checking. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 57 | QualType Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | |
| 59 | switch (DS.getTypeSpecType()) { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 60 | case DeclSpec::TST_void: |
| 61 | Result = Context.VoidTy; |
| 62 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | case DeclSpec::TST_char: |
| 64 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 65 | Result = Context.CharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 67 | Result = Context.SignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | else { |
| 69 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 70 | "Unknown TSS value"); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 71 | Result = Context.UnsignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 73 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 74 | case DeclSpec::TST_wchar: |
| 75 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
| 76 | Result = Context.WCharTy; |
| 77 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 78 | Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
| 79 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 80 | Result = Context.getSignedWCharType(); |
| 81 | } else { |
| 82 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 83 | "Unknown TSS value"); |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 84 | Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
| 85 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 86 | Result = Context.getUnsignedWCharType(); |
| 87 | } |
| 88 | break; |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 89 | case DeclSpec::TST_unspecified: |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 90 | // "<proto1,proto2>" is an objc qualified ID with a missing id. |
Chris Lattner | 097e916 | 2008-10-20 02:01:50 +0000 | [diff] [blame] | 91 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 92 | Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ, |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 93 | DS.getNumProtocolQualifiers()); |
| 94 | break; |
| 95 | } |
| 96 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 97 | // Unspecified typespec defaults to int in C90. However, the C90 grammar |
| 98 | // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, |
| 99 | // type-qualifier, or storage-class-specifier. If not, emit an extwarn. |
| 100 | // Note that the one exception to this is function definitions, which are |
| 101 | // allowed to be completely missing a declspec. This is handled in the |
| 102 | // parser already though by it pretending to have seen an 'int' in this |
| 103 | // case. |
| 104 | if (getLangOptions().ImplicitInt) { |
Chris Lattner | 35d276f | 2009-02-27 18:53:28 +0000 | [diff] [blame] | 105 | // In C89 mode, we only warn if there is a completely missing declspec |
| 106 | // when one is not allowed. |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 107 | if (DS.isEmpty()) { |
| 108 | if (DeclLoc.isInvalid()) |
| 109 | DeclLoc = DS.getSourceRange().getBegin(); |
| 110 | Diag(DeclLoc, diag::warn_missing_declspec) |
| 111 | << DS.getSourceRange() |
Chris Lattner | 173144a | 2009-02-27 22:31:56 +0000 | [diff] [blame] | 112 | << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(), |
| 113 | "int"); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 114 | } |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 115 | } else if (!DS.hasTypeSpecifier()) { |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 116 | // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: |
| 117 | // "At least one type specifier shall be given in the declaration |
| 118 | // specifiers in each declaration, and in the specifier-qualifier list in |
| 119 | // each struct declaration and type name." |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 120 | // FIXME: Does Microsoft really have the implicit int extension in C++? |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 121 | if (DeclLoc.isInvalid()) |
| 122 | DeclLoc = DS.getSourceRange().getBegin(); |
| 123 | |
| 124 | if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft) |
| 125 | Diag(DeclLoc, diag::err_missing_type_specifier) |
| 126 | << DS.getSourceRange(); |
| 127 | else |
| 128 | Diag(DeclLoc, diag::warn_missing_type_specifier) |
| 129 | << DS.getSourceRange(); |
Douglas Gregor | d17a9e5 | 2009-03-27 05:10:56 +0000 | [diff] [blame] | 130 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 131 | // FIXME: If we could guarantee that the result would be well-formed, it |
| 132 | // would be useful to have a code insertion hint here. However, after |
| 133 | // emitting this warning/error, we often emit other errors. |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // FALL THROUGH. |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 137 | case DeclSpec::TST_int: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 139 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 140 | case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; |
| 141 | case DeclSpec::TSW_short: Result = Context.ShortTy; break; |
| 142 | case DeclSpec::TSW_long: Result = Context.LongTy; break; |
| 143 | case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | } |
| 145 | } else { |
| 146 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 147 | case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; |
| 148 | case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; |
| 149 | case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; |
| 150 | case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 153 | break; |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 155 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 156 | case DeclSpec::TST_double: |
| 157 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 158 | Result = Context.LongDoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 159 | else |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 160 | Result = Context.DoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 161 | break; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 162 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | case DeclSpec::TST_decimal32: // _Decimal32 |
| 164 | case DeclSpec::TST_decimal64: // _Decimal64 |
| 165 | case DeclSpec::TST_decimal128: // _Decimal128 |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 166 | Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); |
| 167 | Result = Context.IntTy; |
| 168 | isInvalid = true; |
| 169 | break; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 170 | case DeclSpec::TST_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | case DeclSpec::TST_enum: |
| 172 | case DeclSpec::TST_union: |
| 173 | case DeclSpec::TST_struct: { |
| 174 | Decl *D = static_cast<Decl *>(DS.getTypeRep()); |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 175 | 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] | 176 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 177 | DS.getTypeSpecSign() == 0 && |
| 178 | "Can't handle qualifiers on typedef names yet!"); |
| 179 | // TypeQuals handled by caller. |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 180 | Result = Context.getTypeDeclType(cast<TypeDecl>(D)); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 181 | |
| 182 | if (D->isInvalidDecl()) |
| 183 | isInvalid = true; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 184 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 185 | } |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 186 | case DeclSpec::TST_typename: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 187 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 188 | DS.getTypeSpecSign() == 0 && |
| 189 | "Can't handle qualifiers on typedef names yet!"); |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 190 | Result = QualType::getFromOpaquePtr(DS.getTypeRep()); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 191 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 192 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 193 | // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so we have |
| 194 | // this "hack" for now... |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 195 | if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType()) |
| 196 | Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(), |
| 197 | (ObjCProtocolDecl**)PQ, |
| 198 | DS.getNumProtocolQualifiers()); |
| 199 | else if (Result == Context.getObjCIdType()) |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 200 | // id<protocol-list> |
| 201 | Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ, |
| 202 | DS.getNumProtocolQualifiers()); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 203 | else if (Result == Context.getObjCClassType()) { |
| 204 | if (DeclLoc.isInvalid()) |
| 205 | DeclLoc = DS.getSourceRange().getBegin(); |
Steve Naroff | 4262a07 | 2009-02-23 18:53:24 +0000 | [diff] [blame] | 206 | // Class<protocol-list> |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 207 | Diag(DeclLoc, diag::err_qualified_class_unsupported) |
| 208 | << DS.getSourceRange(); |
| 209 | } else { |
| 210 | if (DeclLoc.isInvalid()) |
| 211 | DeclLoc = DS.getSourceRange().getBegin(); |
| 212 | Diag(DeclLoc, diag::err_invalid_protocol_qualifiers) |
| 213 | << DS.getSourceRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 214 | isInvalid = true; |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 215 | } |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 216 | } |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 217 | |
| 218 | // If this is a reference to an invalid typedef, propagate the invalidity. |
| 219 | if (TypedefType *TDT = dyn_cast<TypedefType>(Result)) |
| 220 | if (TDT->getDecl()->isInvalidDecl()) |
| 221 | isInvalid = true; |
| 222 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 223 | // TypeQuals handled by caller. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 224 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 225 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 226 | case DeclSpec::TST_typeofType: |
| 227 | Result = QualType::getFromOpaquePtr(DS.getTypeRep()); |
| 228 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 229 | // TypeQuals handled by caller. |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 230 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 231 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 232 | case DeclSpec::TST_typeofExpr: { |
| 233 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 234 | assert(E && "Didn't get an expression for typeof?"); |
| 235 | // TypeQuals handled by caller. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 236 | Result = Context.getTypeOfExprType(E); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 237 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 238 | } |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 239 | case DeclSpec::TST_error: |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 240 | Result = Context.IntTy; |
| 241 | isInvalid = true; |
| 242 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 243 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 244 | |
| 245 | // Handle complex types. |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 246 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { |
| 247 | if (getLangOptions().Freestanding) |
| 248 | Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 249 | Result = Context.getComplexType(Result); |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 250 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 251 | |
| 252 | assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && |
| 253 | "FIXME: imaginary types not supported yet!"); |
| 254 | |
Chris Lattner | 38d8b98 | 2008-02-20 22:04:11 +0000 | [diff] [blame] | 255 | // See if there are any attributes on the declspec that apply to the type (as |
| 256 | // opposed to the decl). |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 257 | if (const AttributeList *AL = DS.getAttributes()) |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 258 | ProcessTypeAttributeList(Result, AL); |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 260 | // Apply const/volatile/restrict qualifiers to T. |
| 261 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 262 | |
| 263 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 264 | // or incomplete types shall not be restrict-qualified." C++ also allows |
| 265 | // restrict-qualified references. |
| 266 | if (TypeQuals & QualType::Restrict) { |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 267 | if (Result->isPointerType() || Result->isReferenceType()) { |
| 268 | QualType EltTy = Result->isPointerType() ? |
| 269 | Result->getAsPointerType()->getPointeeType() : |
| 270 | Result->getAsReferenceType()->getPointeeType(); |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 271 | |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 272 | // 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] | 273 | // incomplete type. |
| 274 | if (!EltTy->isIncompleteOrObjectType()) { |
| 275 | Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 276 | diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 277 | << EltTy << DS.getSourceRange(); |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 278 | TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. |
| 279 | } |
| 280 | } else { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 281 | Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 282 | diag::err_typecheck_invalid_restrict_not_pointer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 283 | << Result << DS.getSourceRange(); |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 284 | TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 285 | } |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification |
| 289 | // of a function type includes any type qualifiers, the behavior is |
| 290 | // undefined." |
| 291 | if (Result->isFunctionType() && TypeQuals) { |
| 292 | // Get some location to point at, either the C or V location. |
| 293 | SourceLocation Loc; |
| 294 | if (TypeQuals & QualType::Const) |
| 295 | Loc = DS.getConstSpecLoc(); |
| 296 | else { |
| 297 | assert((TypeQuals & QualType::Volatile) && |
| 298 | "Has CV quals but not C or V?"); |
| 299 | Loc = DS.getVolatileSpecLoc(); |
| 300 | } |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 301 | Diag(Loc, diag::warn_typecheck_function_qualifiers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 302 | << Result << DS.getSourceRange(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 305 | // C++ [dcl.ref]p1: |
| 306 | // Cv-qualified references are ill-formed except when the |
| 307 | // cv-qualifiers are introduced through the use of a typedef |
| 308 | // (7.1.3) or of a template type argument (14.3), in which |
| 309 | // case the cv-qualifiers are ignored. |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 310 | // FIXME: Shouldn't we be checking SCS_typedef here? |
| 311 | if (DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 312 | TypeQuals && Result->isReferenceType()) { |
| 313 | TypeQuals &= ~QualType::Const; |
| 314 | TypeQuals &= ~QualType::Volatile; |
| 315 | } |
| 316 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 317 | Result = Result.getQualifiedType(TypeQuals); |
| 318 | } |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 319 | return Result; |
| 320 | } |
| 321 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 322 | static std::string getPrintableNameForEntity(DeclarationName Entity) { |
| 323 | if (Entity) |
| 324 | return Entity.getAsString(); |
| 325 | |
| 326 | return "type name"; |
| 327 | } |
| 328 | |
| 329 | /// \brief Build a pointer type. |
| 330 | /// |
| 331 | /// \param T The type to which we'll be building a pointer. |
| 332 | /// |
| 333 | /// \param Quals The cvr-qualifiers to be applied to the pointer type. |
| 334 | /// |
| 335 | /// \param Loc The location of the entity whose type involves this |
| 336 | /// pointer type or, if there is no such entity, the location of the |
| 337 | /// type that will have pointer type. |
| 338 | /// |
| 339 | /// \param Entity The name of the entity that involves the pointer |
| 340 | /// type, if known. |
| 341 | /// |
| 342 | /// \returns A suitable pointer type, if there are no |
| 343 | /// errors. Otherwise, returns a NULL type. |
| 344 | QualType Sema::BuildPointerType(QualType T, unsigned Quals, |
| 345 | SourceLocation Loc, DeclarationName Entity) { |
| 346 | if (T->isReferenceType()) { |
| 347 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
| 348 | Diag(Loc, diag::err_illegal_decl_pointer_to_reference) |
| 349 | << getPrintableNameForEntity(Entity); |
| 350 | return QualType(); |
| 351 | } |
| 352 | |
| 353 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 354 | // object or incomplete types shall not be restrict-qualified." |
| 355 | if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) { |
| 356 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 357 | << T; |
| 358 | Quals &= ~QualType::Restrict; |
| 359 | } |
| 360 | |
| 361 | // Build the pointer type. |
| 362 | return Context.getPointerType(T).getQualifiedType(Quals); |
| 363 | } |
| 364 | |
| 365 | /// \brief Build a reference type. |
| 366 | /// |
| 367 | /// \param T The type to which we'll be building a reference. |
| 368 | /// |
| 369 | /// \param Quals The cvr-qualifiers to be applied to the reference type. |
| 370 | /// |
| 371 | /// \param Loc The location of the entity whose type involves this |
| 372 | /// reference type or, if there is no such entity, the location of the |
| 373 | /// type that will have reference type. |
| 374 | /// |
| 375 | /// \param Entity The name of the entity that involves the reference |
| 376 | /// type, if known. |
| 377 | /// |
| 378 | /// \returns A suitable reference type, if there are no |
| 379 | /// errors. Otherwise, returns a NULL type. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 380 | QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 381 | SourceLocation Loc, DeclarationName Entity) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 382 | if (LValueRef) { |
| 383 | if (const RValueReferenceType *R = T->getAsRValueReferenceType()) { |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 384 | // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a |
| 385 | // reference to a type T, and attempt to create the type "lvalue |
| 386 | // reference to cv TD" creates the type "lvalue reference to T". |
| 387 | // We use the qualifiers (restrict or none) of the original reference, |
| 388 | // not the new ones. This is consistent with GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 389 | return Context.getLValueReferenceType(R->getPointeeType()). |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 390 | getQualifiedType(T.getCVRQualifiers()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 393 | if (T->isReferenceType()) { |
| 394 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 395 | // |
| 396 | // According to C++ DR 106, references to references are only |
| 397 | // diagnosed when they are written directly (e.g., "int & &"), |
| 398 | // but not when they happen via a typedef: |
| 399 | // |
| 400 | // typedef int& intref; |
| 401 | // typedef intref& intref2; |
| 402 | // |
| 403 | // Parser::ParserDeclaratorInternal diagnoses the case where |
| 404 | // references are written directly; here, we handle the |
| 405 | // collapsing of references-to-references as described in C++ |
| 406 | // DR 106 and amended by C++ DR 540. |
| 407 | return T; |
| 408 | } |
| 409 | |
| 410 | // C++ [dcl.ref]p1: |
| 411 | // A declarator that specifies the type “reference to cv void” |
| 412 | // is ill-formed. |
| 413 | if (T->isVoidType()) { |
| 414 | Diag(Loc, diag::err_reference_to_void); |
| 415 | return QualType(); |
| 416 | } |
| 417 | |
| 418 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 419 | // object or incomplete types shall not be restrict-qualified." |
| 420 | if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) { |
| 421 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 422 | << T; |
| 423 | Quals &= ~QualType::Restrict; |
| 424 | } |
| 425 | |
| 426 | // C++ [dcl.ref]p1: |
| 427 | // [...] Cv-qualified references are ill-formed except when the |
| 428 | // cv-qualifiers are introduced through the use of a typedef |
| 429 | // (7.1.3) or of a template type argument (14.3), in which case |
| 430 | // the cv-qualifiers are ignored. |
| 431 | // |
| 432 | // We diagnose extraneous cv-qualifiers for the non-typedef, |
| 433 | // non-template type argument case within the parser. Here, we just |
| 434 | // ignore any extraneous cv-qualifiers. |
| 435 | Quals &= ~QualType::Const; |
| 436 | Quals &= ~QualType::Volatile; |
| 437 | |
| 438 | // Handle restrict on references. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 439 | if (LValueRef) |
| 440 | return Context.getLValueReferenceType(T).getQualifiedType(Quals); |
| 441 | return Context.getRValueReferenceType(T).getQualifiedType(Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /// \brief Build an array type. |
| 445 | /// |
| 446 | /// \param T The type of each element in the array. |
| 447 | /// |
| 448 | /// \param ASM C99 array size modifier (e.g., '*', 'static'). |
| 449 | /// |
| 450 | /// \param ArraySize Expression describing the size of the array. |
| 451 | /// |
| 452 | /// \param Quals The cvr-qualifiers to be applied to the array's |
| 453 | /// element type. |
| 454 | /// |
| 455 | /// \param Loc The location of the entity whose type involves this |
| 456 | /// array type or, if there is no such entity, the location of the |
| 457 | /// type that will have array type. |
| 458 | /// |
| 459 | /// \param Entity The name of the entity that involves the array |
| 460 | /// type, if known. |
| 461 | /// |
| 462 | /// \returns A suitable array type, if there are no errors. Otherwise, |
| 463 | /// returns a NULL type. |
| 464 | QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, |
| 465 | Expr *ArraySize, unsigned Quals, |
| 466 | SourceLocation Loc, DeclarationName Entity) { |
| 467 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
| 468 | // 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] | 469 | if (RequireCompleteType(Loc, T, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 470 | diag::err_illegal_decl_array_incomplete_type)) |
| 471 | return QualType(); |
| 472 | |
| 473 | if (T->isFunctionType()) { |
| 474 | Diag(Loc, diag::err_illegal_decl_array_of_functions) |
| 475 | << getPrintableNameForEntity(Entity); |
| 476 | return QualType(); |
| 477 | } |
| 478 | |
| 479 | // C++ 8.3.2p4: There shall be no ... arrays of references ... |
| 480 | if (T->isReferenceType()) { |
| 481 | Diag(Loc, diag::err_illegal_decl_array_of_references) |
| 482 | << getPrintableNameForEntity(Entity); |
| 483 | return QualType(); |
| 484 | } |
| 485 | |
| 486 | if (const RecordType *EltTy = T->getAsRecordType()) { |
| 487 | // If the element type is a struct or union that contains a variadic |
| 488 | // array, accept it as a GNU extension: C99 6.7.2.1p2. |
| 489 | if (EltTy->getDecl()->hasFlexibleArrayMember()) |
| 490 | Diag(Loc, diag::ext_flexible_array_in_array) << T; |
| 491 | } else if (T->isObjCInterfaceType()) { |
Chris Lattner | c7c11b1 | 2009-04-27 01:55:56 +0000 | [diff] [blame] | 492 | Diag(Loc, diag::err_objc_array_of_interfaces) << T; |
| 493 | return QualType(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // C99 6.7.5.2p1: The size expression shall have integer type. |
| 497 | if (ArraySize && !ArraySize->isTypeDependent() && |
| 498 | !ArraySize->getType()->isIntegerType()) { |
| 499 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) |
| 500 | << ArraySize->getType() << ArraySize->getSourceRange(); |
| 501 | ArraySize->Destroy(Context); |
| 502 | return QualType(); |
| 503 | } |
| 504 | llvm::APSInt ConstVal(32); |
| 505 | if (!ArraySize) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 506 | if (ASM == ArrayType::Star) |
| 507 | T = Context.getVariableArrayType(T, 0, ASM, Quals); |
| 508 | else |
| 509 | T = Context.getIncompleteArrayType(T, ASM, Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 510 | } else if (ArraySize->isValueDependent()) { |
| 511 | T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals); |
| 512 | } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || |
| 513 | (!T->isDependentType() && !T->isConstantSizeType())) { |
| 514 | // Per C99, a variable array is an array with either a non-constant |
| 515 | // size or an element type that has a non-constant-size |
| 516 | T = Context.getVariableArrayType(T, ArraySize, ASM, Quals); |
| 517 | } else { |
| 518 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 519 | // have a value greater than zero. |
| 520 | if (ConstVal.isSigned()) { |
| 521 | if (ConstVal.isNegative()) { |
| 522 | Diag(ArraySize->getLocStart(), |
| 523 | diag::err_typecheck_negative_array_size) |
| 524 | << ArraySize->getSourceRange(); |
| 525 | return QualType(); |
| 526 | } else if (ConstVal == 0) { |
| 527 | // GCC accepts zero sized static arrays. |
| 528 | Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size) |
| 529 | << ArraySize->getSourceRange(); |
| 530 | } |
| 531 | } |
| 532 | T = Context.getConstantArrayType(T, ConstVal, ASM, Quals); |
| 533 | } |
| 534 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
| 535 | if (!getLangOptions().C99) { |
| 536 | if (ArraySize && !ArraySize->isTypeDependent() && |
| 537 | !ArraySize->isValueDependent() && |
| 538 | !ArraySize->isIntegerConstantExpr(Context)) |
| 539 | Diag(Loc, diag::ext_vla); |
| 540 | else if (ASM != ArrayType::Normal || Quals != 0) |
| 541 | Diag(Loc, diag::ext_c99_array_usage); |
| 542 | } |
| 543 | |
| 544 | return T; |
| 545 | } |
| 546 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 547 | /// \brief Build a function type. |
| 548 | /// |
| 549 | /// This routine checks the function type according to C++ rules and |
| 550 | /// under the assumption that the result type and parameter types have |
| 551 | /// just been instantiated from a template. It therefore duplicates |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 552 | /// some of the behavior of GetTypeForDeclarator, but in a much |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 553 | /// simpler form that is only suitable for this narrow use case. |
| 554 | /// |
| 555 | /// \param T The return type of the function. |
| 556 | /// |
| 557 | /// \param ParamTypes The parameter types of the function. This array |
| 558 | /// will be modified to account for adjustments to the types of the |
| 559 | /// function parameters. |
| 560 | /// |
| 561 | /// \param NumParamTypes The number of parameter types in ParamTypes. |
| 562 | /// |
| 563 | /// \param Variadic Whether this is a variadic function type. |
| 564 | /// |
| 565 | /// \param Quals The cvr-qualifiers to be applied to the function type. |
| 566 | /// |
| 567 | /// \param Loc The location of the entity whose type involves this |
| 568 | /// function type or, if there is no such entity, the location of the |
| 569 | /// type that will have function type. |
| 570 | /// |
| 571 | /// \param Entity The name of the entity that involves the function |
| 572 | /// type, if known. |
| 573 | /// |
| 574 | /// \returns A suitable function type, if there are no |
| 575 | /// errors. Otherwise, returns a NULL type. |
| 576 | QualType Sema::BuildFunctionType(QualType T, |
| 577 | QualType *ParamTypes, |
| 578 | unsigned NumParamTypes, |
| 579 | bool Variadic, unsigned Quals, |
| 580 | SourceLocation Loc, DeclarationName Entity) { |
| 581 | if (T->isArrayType() || T->isFunctionType()) { |
| 582 | Diag(Loc, diag::err_func_returning_array_function) << T; |
| 583 | return QualType(); |
| 584 | } |
| 585 | |
| 586 | bool Invalid = false; |
| 587 | for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) { |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 588 | QualType ParamType = adjustParameterType(ParamTypes[Idx]); |
| 589 | if (ParamType->isVoidType()) { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 590 | Diag(Loc, diag::err_param_with_void_type); |
| 591 | Invalid = true; |
| 592 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 593 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 594 | ParamTypes[Idx] = ParamType; |
| 595 | } |
| 596 | |
| 597 | if (Invalid) |
| 598 | return QualType(); |
| 599 | |
| 600 | return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic, |
| 601 | Quals); |
| 602 | } |
| 603 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 604 | /// GetTypeForDeclarator - Convert the type for the specified |
| 605 | /// declarator to Type instances. Skip the outermost Skip type |
| 606 | /// objects. |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame^] | 607 | /// |
| 608 | /// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq |
| 609 | /// owns the declaration of a type (e.g., the definition of a struct |
| 610 | /// type), then *OwnedDecl will receive the owned declaration. |
| 611 | QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip, |
| 612 | TagDecl **OwnedDecl) { |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 613 | bool OmittedReturnType = false; |
| 614 | |
| 615 | if (D.getContext() == Declarator::BlockLiteralContext |
| 616 | && Skip == 0 |
| 617 | && !D.getDeclSpec().hasTypeSpecifier() |
| 618 | && (D.getNumTypeObjects() == 0 |
| 619 | || (D.getNumTypeObjects() == 1 |
| 620 | && D.getTypeObject(0).Kind == DeclaratorChunk::Function))) |
| 621 | OmittedReturnType = true; |
| 622 | |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 623 | // long long is a C99 feature. |
Chris Lattner | d1eb332 | 2007-08-28 16:41:29 +0000 | [diff] [blame] | 624 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 625 | D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) |
| 626 | Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 627 | |
| 628 | // Determine the type of the declarator. Not all forms of declarator |
| 629 | // have a type. |
| 630 | QualType T; |
| 631 | switch (D.getKind()) { |
| 632 | case Declarator::DK_Abstract: |
| 633 | case Declarator::DK_Normal: |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 634 | case Declarator::DK_Operator: { |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 635 | const DeclSpec &DS = D.getDeclSpec(); |
| 636 | if (OmittedReturnType) { |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 637 | // We default to a dependent type initially. Can be modified by |
| 638 | // the first return statement. |
| 639 | T = Context.DependentTy; |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 640 | } else { |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 641 | bool isInvalid = false; |
| 642 | T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 643 | if (isInvalid) |
| 644 | D.setInvalidType(true); |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame^] | 645 | else if (OwnedDecl && DS.isTypeSpecOwned()) |
| 646 | *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep()); |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 647 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 648 | break; |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 649 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 650 | |
| 651 | case Declarator::DK_Constructor: |
| 652 | case Declarator::DK_Destructor: |
| 653 | case Declarator::DK_Conversion: |
| 654 | // Constructors and destructors don't have return types. Use |
| 655 | // "void" instead. Conversion operators will check their return |
| 656 | // types separately. |
| 657 | T = Context.VoidTy; |
| 658 | break; |
| 659 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 660 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 661 | // The name we're declaring, if any. |
| 662 | DeclarationName Name; |
| 663 | if (D.getIdentifier()) |
| 664 | Name = D.getIdentifier(); |
| 665 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 666 | // Walk the DeclTypeInfo, building the recursive type as we go. |
| 667 | // DeclTypeInfos are ordered from the identifier out, which is |
| 668 | // opposite of what we want :). |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 669 | for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) { |
| 670 | DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 671 | switch (DeclType.Kind) { |
| 672 | default: assert(0 && "Unknown decltype!"); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 673 | case DeclaratorChunk::BlockPointer: |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 674 | // If blocks are disabled, emit an error. |
| 675 | if (!LangOpts.Blocks) |
| 676 | Diag(DeclType.Loc, diag::err_blocks_disable); |
| 677 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 678 | if (!T.getTypePtr()->isFunctionType()) |
| 679 | Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type); |
| 680 | else |
Mike Stump | 7bc8d96 | 2009-05-07 21:56:17 +0000 | [diff] [blame] | 681 | T = (Context.getBlockPointerType(T) |
| 682 | .getQualifiedType(DeclType.Cls.TypeQuals)); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 683 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 684 | case DeclaratorChunk::Pointer: |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 685 | T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 686 | break; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 687 | case DeclaratorChunk::Reference: |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 688 | T = BuildReferenceType(T, DeclType.Ref.LValueRef, |
| 689 | DeclType.Ref.HasRestrict ? QualType::Restrict : 0, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 690 | DeclType.Loc, Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 691 | break; |
| 692 | case DeclaratorChunk::Array: { |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 693 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 694 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 695 | ArrayType::ArraySizeModifier ASM; |
| 696 | if (ATI.isStar) |
| 697 | ASM = ArrayType::Star; |
| 698 | else if (ATI.hasStatic) |
| 699 | ASM = ArrayType::Static; |
| 700 | else |
| 701 | ASM = ArrayType::Normal; |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 702 | if (ASM == ArrayType::Star && |
| 703 | D.getContext() != Declarator::PrototypeContext) { |
| 704 | // FIXME: This check isn't quite right: it allows star in prototypes |
| 705 | // for function definitions, and disallows some edge cases detailed |
| 706 | // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html |
| 707 | Diag(DeclType.Loc, diag::err_array_star_outside_prototype); |
| 708 | ASM = ArrayType::Normal; |
| 709 | D.setInvalidType(true); |
| 710 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 711 | T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, DeclType.Loc, Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 712 | break; |
| 713 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 714 | case DeclaratorChunk::Function: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 715 | // If the function declarator has a prototype (i.e. it is not () and |
| 716 | // does not have a K&R-style identifier list), then the arguments are part |
| 717 | // of the type, otherwise the argument list is (). |
| 718 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Chris Lattner | 68cfd49 | 2007-12-19 18:01:43 +0000 | [diff] [blame] | 719 | |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 720 | // 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] | 721 | if (T->isArrayType() || T->isFunctionType()) { |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 722 | Diag(DeclType.Loc, diag::err_func_returning_array_function) << T; |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 723 | T = Context.IntTy; |
| 724 | D.setInvalidType(true); |
| 725 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 726 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame^] | 727 | if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) { |
| 728 | // C++ [dcl.fct]p6: |
| 729 | // Types shall not be defined in return or parameter types. |
| 730 | TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep()); |
| 731 | if (Tag->isDefinition()) |
| 732 | Diag(Tag->getLocation(), diag::err_type_defined_in_result_type) |
| 733 | << Context.getTypeDeclType(Tag); |
| 734 | } |
| 735 | |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 736 | if (FTI.NumArgs == 0) { |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 737 | if (getLangOptions().CPlusPlus) { |
| 738 | // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the |
| 739 | // function takes no arguments. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 740 | llvm::SmallVector<QualType, 4> Exceptions; |
| 741 | Exceptions.reserve(FTI.NumExceptions); |
| 742 | for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) |
| 743 | Exceptions.push_back( |
| 744 | QualType::getFromOpaquePtr(FTI.Exceptions[ei])); |
| 745 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals, |
| 746 | FTI.hasExceptionSpec, |
| 747 | FTI.hasAnyExceptionSpec, |
| 748 | FTI.NumExceptions, Exceptions.data()); |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 749 | } else if (FTI.isVariadic) { |
| 750 | // We allow a zero-parameter variadic function in C if the |
| 751 | // function is marked with the "overloadable" |
| 752 | // attribute. Scan for this attribute now. |
| 753 | bool Overloadable = false; |
| 754 | for (const AttributeList *Attrs = D.getAttributes(); |
| 755 | Attrs; Attrs = Attrs->getNext()) { |
| 756 | if (Attrs->getKind() == AttributeList::AT_overloadable) { |
| 757 | Overloadable = true; |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | if (!Overloadable) |
| 763 | Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg); |
| 764 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 765 | } else { |
| 766 | // Simple void foo(), where the incoming T is the result type. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 767 | T = Context.getFunctionNoProtoType(T); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 768 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 769 | } else if (FTI.ArgInfo[0].Param == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 770 | // 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] | 771 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 772 | } else { |
| 773 | // Otherwise, we have a function with an argument list that is |
| 774 | // potentially variadic. |
| 775 | llvm::SmallVector<QualType, 16> ArgTys; |
| 776 | |
| 777 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 778 | ParmVarDecl *Param = |
| 779 | cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>()); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 780 | QualType ArgTy = Param->getType(); |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 781 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 782 | |
| 783 | // Adjust the parameter type. |
Douglas Gregor | beb58cb | 2009-03-23 23:17:00 +0000 | [diff] [blame] | 784 | assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 785 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 786 | // Look for 'void'. void is allowed only as a single argument to a |
| 787 | // function with no other parameters (C99 6.7.5.3p10). We record |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 788 | // int(void) as a FunctionProtoType with an empty argument list. |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 789 | if (ArgTy->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 790 | // If this is something like 'float(int, void)', reject it. 'void' |
| 791 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 792 | // have arguments of incomplete type. |
| 793 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 794 | Diag(DeclType.Loc, diag::err_void_only_param); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 795 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 796 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 797 | } else if (FTI.ArgInfo[i].Ident) { |
| 798 | // Reject, but continue to parse 'int(void abc)'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 799 | Diag(FTI.ArgInfo[i].IdentLoc, |
Chris Lattner | 4565d4e | 2007-07-21 05:26:43 +0000 | [diff] [blame] | 800 | diag::err_param_with_void_type); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 801 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 802 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 803 | } else { |
| 804 | // Reject, but continue to parse 'float(const void)'. |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 805 | if (ArgTy.getCVRQualifiers()) |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 806 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
| 807 | |
| 808 | // Do not add 'void' to the ArgTys list. |
| 809 | break; |
| 810 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 811 | } else if (!FTI.hasPrototype) { |
| 812 | if (ArgTy->isPromotableIntegerType()) { |
| 813 | ArgTy = Context.IntTy; |
| 814 | } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) { |
| 815 | if (BTy->getKind() == BuiltinType::Float) |
| 816 | ArgTy = Context.DoubleTy; |
| 817 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | ArgTys.push_back(ArgTy); |
| 821 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 822 | |
| 823 | llvm::SmallVector<QualType, 4> Exceptions; |
| 824 | Exceptions.reserve(FTI.NumExceptions); |
| 825 | for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) |
| 826 | Exceptions.push_back(QualType::getFromOpaquePtr(FTI.Exceptions[ei])); |
| 827 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 828 | T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 829 | FTI.isVariadic, FTI.TypeQuals, |
| 830 | FTI.hasExceptionSpec, |
| 831 | FTI.hasAnyExceptionSpec, |
| 832 | FTI.NumExceptions, Exceptions.data()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 833 | } |
| 834 | break; |
| 835 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 836 | case DeclaratorChunk::MemberPointer: |
| 837 | // The scope spec must refer to a class, or be dependent. |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 838 | DeclContext *DC = computeDeclContext(DeclType.Mem.Scope()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 839 | QualType ClsType; |
| 840 | // FIXME: Extend for dependent types when it's actually supported. |
| 841 | // See ActOnCXXNestedNameSpecifier. |
| 842 | if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) { |
| 843 | ClsType = Context.getTagDeclType(RD); |
| 844 | } else { |
| 845 | if (DC) { |
| 846 | Diag(DeclType.Mem.Scope().getBeginLoc(), |
| 847 | diag::err_illegal_decl_mempointer_in_nonclass) |
| 848 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") |
| 849 | << DeclType.Mem.Scope().getRange(); |
| 850 | } |
| 851 | D.setInvalidType(true); |
| 852 | ClsType = Context.IntTy; |
| 853 | } |
| 854 | |
| 855 | // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member |
| 856 | // with reference type, or "cv void." |
| 857 | if (T->isReferenceType()) { |
| 858 | Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference) |
| 859 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
| 860 | D.setInvalidType(true); |
| 861 | T = Context.IntTy; |
| 862 | } |
| 863 | if (T->isVoidType()) { |
| 864 | Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void) |
| 865 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
| 866 | T = Context.IntTy; |
| 867 | } |
| 868 | |
| 869 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 870 | // object or incomplete types shall not be restrict-qualified." |
| 871 | if ((DeclType.Mem.TypeQuals & QualType::Restrict) && |
| 872 | !T->isIncompleteOrObjectType()) { |
| 873 | Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 874 | << T; |
| 875 | DeclType.Mem.TypeQuals &= ~QualType::Restrict; |
| 876 | } |
| 877 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 878 | T = Context.getMemberPointerType(T, ClsType.getTypePtr()). |
| 879 | getQualifiedType(DeclType.Mem.TypeQuals); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 880 | |
| 881 | break; |
| 882 | } |
| 883 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 884 | if (T.isNull()) { |
| 885 | D.setInvalidType(true); |
| 886 | T = Context.IntTy; |
| 887 | } |
| 888 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 889 | // See if there are any attributes on this declarator chunk. |
| 890 | if (const AttributeList *AL = DeclType.getAttrs()) |
| 891 | ProcessTypeAttributeList(T, AL); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 892 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 893 | |
| 894 | if (getLangOptions().CPlusPlus && T->isFunctionType()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 895 | const FunctionProtoType *FnTy = T->getAsFunctionProtoType(); |
| 896 | assert(FnTy && "Why oh why is there not a FunctionProtoType here ?"); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 897 | |
| 898 | // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type |
| 899 | // for a nonstatic member function, the function type to which a pointer |
| 900 | // to member refers, or the top-level function type of a function typedef |
| 901 | // declaration. |
| 902 | if (FnTy->getTypeQuals() != 0 && |
| 903 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 904 | ((D.getContext() != Declarator::MemberContext && |
| 905 | (!D.getCXXScopeSpec().isSet() || |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 906 | !computeDeclContext(D.getCXXScopeSpec())->isRecord())) || |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 907 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 908 | if (D.isFunctionDeclarator()) |
| 909 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type); |
| 910 | else |
| 911 | Diag(D.getIdentifierLoc(), |
| 912 | diag::err_invalid_qualified_typedef_function_type_use); |
| 913 | |
| 914 | // Strip the cv-quals from the type. |
| 915 | T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 916 | FnTy->getNumArgs(), FnTy->isVariadic(), 0); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 917 | } |
| 918 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 919 | |
Chris Lattner | 0bf29ad | 2008-06-29 00:19:33 +0000 | [diff] [blame] | 920 | // If there were any type attributes applied to the decl itself (not the |
| 921 | // type, apply the type attribute to the type!) |
| 922 | if (const AttributeList *Attrs = D.getAttributes()) |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 923 | ProcessTypeAttributeList(T, Attrs); |
Chris Lattner | 0bf29ad | 2008-06-29 00:19:33 +0000 | [diff] [blame] | 924 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 925 | return T; |
| 926 | } |
| 927 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 928 | /// ObjCGetTypeForMethodDefinition - Builds the type for a method definition |
Fariborz Jahanian | 360300c | 2007-11-09 22:27:59 +0000 | [diff] [blame] | 929 | /// declarator |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 930 | QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) { |
| 931 | ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>()); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 932 | QualType T = MDecl->getResultType(); |
| 933 | llvm::SmallVector<QualType, 16> ArgTys; |
| 934 | |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 935 | // Add the first two invisible argument types for self and _cmd. |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 936 | if (MDecl->isInstanceMethod()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 937 | QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 938 | selfTy = Context.getPointerType(selfTy); |
| 939 | ArgTys.push_back(selfTy); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 940 | } else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 941 | ArgTys.push_back(Context.getObjCIdType()); |
| 942 | ArgTys.push_back(Context.getObjCSelType()); |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 943 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 944 | for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), |
| 945 | E = MDecl->param_end(); PI != E; ++PI) { |
| 946 | QualType ArgTy = (*PI)->getType(); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 947 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Douglas Gregor | beb58cb | 2009-03-23 23:17:00 +0000 | [diff] [blame] | 948 | ArgTy = adjustParameterType(ArgTy); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 949 | ArgTys.push_back(ArgTy); |
| 950 | } |
| 951 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 952 | MDecl->isVariadic(), 0); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 953 | return T; |
| 954 | } |
| 955 | |
Sebastian Redl | 9e5e4aa | 2009-01-26 19:54:48 +0000 | [diff] [blame] | 956 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 957 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 958 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 959 | /// or pointer-to-member types, or if they are not similar at this |
| 960 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 961 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 962 | /// be called in a loop that successively "unwraps" pointer and |
| 963 | /// pointer-to-member types to compare them at each level. |
Chris Lattner | ecb81f2 | 2009-02-16 21:43:00 +0000 | [diff] [blame] | 964 | bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 965 | const PointerType *T1PtrType = T1->getAsPointerType(), |
| 966 | *T2PtrType = T2->getAsPointerType(); |
| 967 | if (T1PtrType && T2PtrType) { |
| 968 | T1 = T1PtrType->getPointeeType(); |
| 969 | T2 = T2PtrType->getPointeeType(); |
| 970 | return true; |
| 971 | } |
| 972 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 973 | const MemberPointerType *T1MPType = T1->getAsMemberPointerType(), |
| 974 | *T2MPType = T2->getAsMemberPointerType(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 975 | if (T1MPType && T2MPType && |
| 976 | Context.getCanonicalType(T1MPType->getClass()) == |
| 977 | Context.getCanonicalType(T2MPType->getClass())) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 978 | T1 = T1MPType->getPointeeType(); |
| 979 | T2 = T2MPType->getPointeeType(); |
| 980 | return true; |
| 981 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 982 | return false; |
| 983 | } |
| 984 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 985 | Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 986 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 987 | // the parser. |
| 988 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
| 989 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame^] | 990 | TagDecl *OwnedTag = 0; |
| 991 | QualType T = GetTypeForDeclarator(D, S, /*Skip=*/0, &OwnedTag); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 992 | if (D.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 993 | return true; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 994 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame^] | 995 | if (getLangOptions().CPlusPlus) { |
| 996 | // Check that there are no default arguments (C++ only). |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 997 | CheckExtraCXXDefaultArguments(D); |
| 998 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame^] | 999 | // C++0x [dcl.type]p3: |
| 1000 | // A type-specifier-seq shall not define a class or enumeration |
| 1001 | // unless it appears in the type-id of an alias-declaration |
| 1002 | // (7.1.3). |
| 1003 | if (OwnedTag && OwnedTag->isDefinition()) |
| 1004 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier) |
| 1005 | << Context.getTypeDeclType(OwnedTag); |
| 1006 | } |
| 1007 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1008 | return T.getAsOpaquePtr(); |
| 1009 | } |
| 1010 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1011 | |
| 1012 | |
| 1013 | //===----------------------------------------------------------------------===// |
| 1014 | // Type Attribute Processing |
| 1015 | //===----------------------------------------------------------------------===// |
| 1016 | |
| 1017 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 1018 | /// specified type. The attribute contains 1 argument, the id of the address |
| 1019 | /// space for the type. |
| 1020 | static void HandleAddressSpaceTypeAttribute(QualType &Type, |
| 1021 | const AttributeList &Attr, Sema &S){ |
| 1022 | // If this type is already address space qualified, reject it. |
| 1023 | // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers |
| 1024 | // for two or more different address spaces." |
| 1025 | if (Type.getAddressSpace()) { |
| 1026 | S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); |
| 1027 | return; |
| 1028 | } |
| 1029 | |
| 1030 | // Check the attribute arguments. |
| 1031 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 1032 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1033 | return; |
| 1034 | } |
| 1035 | Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1036 | llvm::APSInt addrSpace(32); |
| 1037 | if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1038 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) |
| 1039 | << ASArgExpr->getSourceRange(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1040 | return; |
| 1041 | } |
| 1042 | |
| 1043 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1044 | Type = S.Context.getAddrSpaceQualType(Type, ASIdx); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1047 | /// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the |
| 1048 | /// specified type. The attribute contains 1 argument, weak or strong. |
| 1049 | static void HandleObjCGCTypeAttribute(QualType &Type, |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1050 | const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1051 | if (Type.getObjCGCAttr() != QualType::GCNone) { |
Fariborz Jahanian | 5934e75 | 2009-02-18 18:52:41 +0000 | [diff] [blame] | 1052 | S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1053 | return; |
| 1054 | } |
| 1055 | |
| 1056 | // Check the attribute arguments. |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1057 | if (!Attr.getParameterName()) { |
| 1058 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1059 | << "objc_gc" << 1; |
| 1060 | return; |
| 1061 | } |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1062 | QualType::GCAttrTypes GCAttr; |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1063 | if (Attr.getNumArgs() != 0) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1064 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1065 | return; |
| 1066 | } |
| 1067 | if (Attr.getParameterName()->isStr("weak")) |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1068 | GCAttr = QualType::Weak; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1069 | else if (Attr.getParameterName()->isStr("strong")) |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1070 | GCAttr = QualType::Strong; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1071 | else { |
| 1072 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 1073 | << "objc_gc" << Attr.getParameterName(); |
| 1074 | return; |
| 1075 | } |
| 1076 | |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1077 | Type = S.Context.getObjCGCQualType(Type, GCAttr); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1080 | void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1081 | // Scan through and apply attributes to this type where it makes sense. Some |
| 1082 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 1083 | // 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] | 1084 | // apply to the decl. Here we apply type attributes and ignore the rest. |
| 1085 | for (; AL; AL = AL->getNext()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1086 | // If this is an attribute we can handle, do so now, otherwise, add it to |
| 1087 | // the LeftOverAttrs list for rechaining. |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 1088 | switch (AL->getKind()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1089 | default: break; |
| 1090 | case AttributeList::AT_address_space: |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1091 | HandleAddressSpaceTypeAttribute(Result, *AL, *this); |
| 1092 | break; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1093 | case AttributeList::AT_objc_gc: |
| 1094 | HandleObjCGCTypeAttribute(Result, *AL, *this); |
| 1095 | break; |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1096 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1097 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 1100 | /// @brief Ensure that the type T is a complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1101 | /// |
| 1102 | /// This routine checks whether the type @p T is complete in any |
| 1103 | /// 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] | 1104 | /// type, returns false. If @p T is a class template specialization, |
| 1105 | /// this routine then attempts to perform class template |
| 1106 | /// instantiation. If instantiation fails, or if @p T is incomplete |
| 1107 | /// and cannot be completed, issues the diagnostic @p diag (giving it |
| 1108 | /// the type @p T) and returns true. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1109 | /// |
| 1110 | /// @param Loc The location in the source that the incomplete type |
| 1111 | /// diagnostic should refer to. |
| 1112 | /// |
| 1113 | /// @param T The type that this routine is examining for completeness. |
| 1114 | /// |
| 1115 | /// @param diag The diagnostic value (e.g., |
| 1116 | /// @c diag::err_typecheck_decl_incomplete_type) that will be used |
| 1117 | /// for the error message if @p T is incomplete. |
| 1118 | /// |
| 1119 | /// @param Range1 An optional range in the source code that will be a |
| 1120 | /// part of the "incomplete type" error message. |
| 1121 | /// |
| 1122 | /// @param Range2 An optional range in the source code that will be a |
| 1123 | /// part of the "incomplete type" error message. |
| 1124 | /// |
| 1125 | /// @param PrintType If non-NULL, the type that should be printed |
| 1126 | /// instead of @p T. This parameter should be used when the type that |
| 1127 | /// we're checking for incompleteness isn't the type that should be |
| 1128 | /// displayed to the user, e.g., when T is a type and PrintType is a |
| 1129 | /// pointer to T. |
| 1130 | /// |
| 1131 | /// @returns @c true if @p T is incomplete and a diagnostic was emitted, |
| 1132 | /// @c false otherwise. |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 1133 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag, |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1134 | SourceRange Range1, SourceRange Range2, |
| 1135 | QualType PrintType) { |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 1136 | // FIXME: Add this assertion to help us flush out problems with |
| 1137 | // checking for dependent types and type-dependent expressions. |
| 1138 | // |
| 1139 | // assert(!T->isDependentType() && |
| 1140 | // "Can't ask whether a dependent type is complete"); |
| 1141 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1142 | // If we have a complete type, we're done. |
| 1143 | if (!T->isIncompleteType()) |
| 1144 | return false; |
Eli Friedman | 3c0eb16 | 2008-05-27 03:33:27 +0000 | [diff] [blame] | 1145 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1146 | // If we have a class template specialization or a class member of a |
| 1147 | // class template specialization, try to instantiate it. |
| 1148 | if (const RecordType *Record = T->getAsRecordType()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1149 | if (ClassTemplateSpecializationDecl *ClassTemplateSpec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1150 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1151 | if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) { |
| 1152 | // Update the class template specialization's location to |
| 1153 | // refer to the point of instantiation. |
| 1154 | if (Loc.isValid()) |
| 1155 | ClassTemplateSpec->setLocation(Loc); |
| 1156 | return InstantiateClassTemplateSpecialization(ClassTemplateSpec, |
| 1157 | /*ExplicitInstantiation=*/false); |
| 1158 | } |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1159 | } else if (CXXRecordDecl *Rec |
| 1160 | = dyn_cast<CXXRecordDecl>(Record->getDecl())) { |
| 1161 | if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) { |
| 1162 | // Find the class template specialization that surrounds this |
| 1163 | // member class. |
| 1164 | ClassTemplateSpecializationDecl *Spec = 0; |
| 1165 | for (DeclContext *Parent = Rec->getDeclContext(); |
| 1166 | Parent && !Spec; Parent = Parent->getParent()) |
| 1167 | Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent); |
| 1168 | assert(Spec && "Not a member of a class template specialization?"); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 1169 | return InstantiateClass(Loc, Rec, Pattern, Spec->getTemplateArgs(), |
| 1170 | /*ExplicitInstantiation=*/false); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1174 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1175 | if (PrintType.isNull()) |
| 1176 | PrintType = T; |
| 1177 | |
| 1178 | // We have an incomplete type. Produce a diagnostic. |
| 1179 | Diag(Loc, diag) << PrintType << Range1 << Range2; |
| 1180 | |
| 1181 | // If the type was a forward declaration of a class/struct/union |
| 1182 | // type, produce |
| 1183 | const TagType *Tag = 0; |
| 1184 | if (const RecordType *Record = T->getAsRecordType()) |
| 1185 | Tag = Record; |
| 1186 | else if (const EnumType *Enum = T->getAsEnumType()) |
| 1187 | Tag = Enum; |
| 1188 | |
| 1189 | if (Tag && !Tag->getDecl()->isInvalidDecl()) |
| 1190 | Diag(Tag->getDecl()->getLocation(), |
| 1191 | Tag->isBeingDefined() ? diag::note_type_being_defined |
| 1192 | : diag::note_forward_declaration) |
| 1193 | << QualType(Tag, 0); |
| 1194 | |
| 1195 | return true; |
| 1196 | } |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 1197 | |
| 1198 | /// \brief Retrieve a version of the type 'T' that is qualified by the |
| 1199 | /// nested-name-specifier contained in SS. |
| 1200 | QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) { |
| 1201 | if (!SS.isSet() || SS.isInvalid() || T.isNull()) |
| 1202 | return T; |
| 1203 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1204 | NestedNameSpecifier *NNS |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 1205 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1206 | return Context.getQualifiedNameType(NNS, T); |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 1207 | } |