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