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