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