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