Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- SemaType.cpp - Semantic Analysis for Types -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 3fafa10 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Daniel Dunbar | cc7b160 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 18 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 21 | /// \brief Convert the specified declspec to the appropriate type |
| 22 | /// object. |
| 23 | /// \param DS the declaration specifiers |
| 24 | /// \returns The type described by the declaration specifiers, or NULL |
| 25 | /// if there was an error. |
Chris Lattner | 99dbc96 | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 26 | QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 27 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 28 | // checking. |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 29 | QualType Result; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 30 | |
| 31 | switch (DS.getTypeSpecType()) { |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 32 | case DeclSpec::TST_void: |
| 33 | Result = Context.VoidTy; |
| 34 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 35 | case DeclSpec::TST_char: |
| 36 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 37 | Result = Context.CharTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 38 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 39 | Result = Context.SignedCharTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 40 | else { |
| 41 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 42 | "Unknown TSS value"); |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 43 | Result = Context.UnsignedCharTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 44 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 45 | break; |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 46 | case DeclSpec::TST_wchar: |
| 47 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
| 48 | Result = Context.WCharTy; |
| 49 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 50 | Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
| 51 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 52 | Result = Context.getSignedWCharType(); |
| 53 | } else { |
| 54 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 55 | "Unknown TSS value"); |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 56 | Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
| 57 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 58 | Result = Context.getUnsignedWCharType(); |
| 59 | } |
| 60 | break; |
Chris Lattner | 6ab935b | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 61 | case DeclSpec::TST_unspecified: |
Chris Lattner | 4a68fe0 | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 62 | // "<proto1,proto2>" is an objc qualified ID with a missing id. |
Chris Lattner | cce4224 | 2008-10-20 02:01:50 +0000 | [diff] [blame] | 63 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
Chris Lattner | ada6379 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 64 | Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ, |
Chris Lattner | 4a68fe0 | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 65 | DS.getNumProtocolQualifiers()); |
| 66 | break; |
| 67 | } |
| 68 | |
Chris Lattner | 6ab935b | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 69 | // Unspecified typespec defaults to int in C90. However, the C90 grammar |
| 70 | // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, |
| 71 | // type-qualifier, or storage-class-specifier. If not, emit an extwarn. |
| 72 | // Note that the one exception to this is function definitions, which are |
| 73 | // allowed to be completely missing a declspec. This is handled in the |
| 74 | // parser already though by it pretending to have seen an 'int' in this |
| 75 | // case. |
| 76 | if (getLangOptions().ImplicitInt) { |
| 77 | if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier | |
| 78 | DeclSpec::PQ_TypeSpecifier | |
| 79 | DeclSpec::PQ_TypeQualifier)) == 0) |
| 80 | Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec); |
Douglas Gregor | ae17c7a | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 81 | } else if (!DS.hasTypeSpecifier()) { |
Chris Lattner | 6ab935b | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 82 | // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: |
| 83 | // "At least one type specifier shall be given in the declaration |
| 84 | // specifiers in each declaration, and in the specifier-qualifier list in |
| 85 | // each struct declaration and type name." |
Douglas Gregor | ae17c7a | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 86 | // FIXME: Does Microsoft really have the implicit int extension in C++? |
| 87 | unsigned DK = getLangOptions().CPlusPlus && !getLangOptions().Microsoft? |
| 88 | diag::err_missing_type_specifier |
| 89 | : diag::ext_missing_type_specifier; |
| 90 | Diag(DS.getSourceRange().getBegin(), DK); |
Chris Lattner | 6ab935b | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // FALL THROUGH. |
Chris Lattner | 5328f31 | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 94 | case DeclSpec::TST_int: { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 95 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 96 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 97 | case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; |
| 98 | case DeclSpec::TSW_short: Result = Context.ShortTy; break; |
| 99 | case DeclSpec::TSW_long: Result = Context.LongTy; break; |
| 100 | case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 101 | } |
| 102 | } else { |
| 103 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 104 | case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; |
| 105 | case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; |
| 106 | case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; |
| 107 | case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 110 | break; |
Chris Lattner | 5328f31 | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 111 | } |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 112 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 113 | case DeclSpec::TST_double: |
| 114 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 115 | Result = Context.LongDoubleTy; |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 116 | else |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 117 | Result = Context.DoubleTy; |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 118 | break; |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 119 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 120 | case DeclSpec::TST_decimal32: // _Decimal32 |
| 121 | case DeclSpec::TST_decimal64: // _Decimal64 |
| 122 | case DeclSpec::TST_decimal128: // _Decimal128 |
| 123 | assert(0 && "FIXME: GNU decimal extensions not supported yet!"); |
Chris Lattner | 2e78db3 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 124 | case DeclSpec::TST_class: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 125 | case DeclSpec::TST_enum: |
| 126 | case DeclSpec::TST_union: |
| 127 | case DeclSpec::TST_struct: { |
| 128 | Decl *D = static_cast<Decl *>(DS.getTypeRep()); |
Chris Lattner | 2e78db3 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 129 | assert(D && "Didn't get a decl for a class/enum/union/struct?"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 130 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 131 | DS.getTypeSpecSign() == 0 && |
| 132 | "Can't handle qualifiers on typedef names yet!"); |
| 133 | // TypeQuals handled by caller. |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 134 | Result = Context.getTypeDeclType(cast<TypeDecl>(D)); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 135 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 136 | } |
Douglas Gregor | a60c62e | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 137 | case DeclSpec::TST_typename: { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 138 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 139 | DS.getTypeSpecSign() == 0 && |
| 140 | "Can't handle qualifiers on typedef names yet!"); |
Douglas Gregor | a60c62e | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 141 | Result = QualType::getFromOpaquePtr(DS.getTypeRep()); |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 142 | |
Douglas Gregor | a60c62e | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 143 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
| 144 | // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so |
| 145 | // we have this "hack" for now... |
| 146 | if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType()) |
| 147 | Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(), |
| 148 | (ObjCProtocolDecl**)PQ, |
| 149 | DS.getNumProtocolQualifiers()); |
| 150 | else if (Result == Context.getObjCIdType()) |
Chris Lattner | ada6379 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 151 | // id<protocol-list> |
| 152 | Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ, |
| 153 | DS.getNumProtocolQualifiers()); |
Steve Naroff | c84582b | 2009-02-21 19:50:43 +0000 | [diff] [blame] | 154 | else |
| 155 | Diag(DS.getSourceRange().getBegin(), |
| 156 | diag::warn_ignoring_objc_qualifiers) << DS.getSourceRange(); |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 157 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 158 | // TypeQuals handled by caller. |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 159 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 160 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 161 | case DeclSpec::TST_typeofType: |
| 162 | Result = QualType::getFromOpaquePtr(DS.getTypeRep()); |
| 163 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 164 | // TypeQuals handled by caller. |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 165 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 166 | break; |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 167 | case DeclSpec::TST_typeofExpr: { |
| 168 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 169 | assert(E && "Didn't get an expression for typeof?"); |
| 170 | // TypeQuals handled by caller. |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 171 | Result = Context.getTypeOfExpr(E); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 172 | break; |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 173 | } |
Douglas Gregor | 6c0f406 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 174 | case DeclSpec::TST_error: |
| 175 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 176 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 177 | |
| 178 | // Handle complex types. |
Douglas Gregor | 91ca7f0 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 179 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { |
| 180 | if (getLangOptions().Freestanding) |
| 181 | Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 182 | Result = Context.getComplexType(Result); |
Douglas Gregor | 91ca7f0 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 183 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 184 | |
| 185 | assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && |
| 186 | "FIXME: imaginary types not supported yet!"); |
| 187 | |
Chris Lattner | d496fb9 | 2008-02-20 22:04:11 +0000 | [diff] [blame] | 188 | // See if there are any attributes on the declspec that apply to the type (as |
| 189 | // opposed to the decl). |
Chris Lattner | 99dbc96 | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 190 | if (const AttributeList *AL = DS.getAttributes()) |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 191 | ProcessTypeAttributeList(Result, AL); |
Chris Lattner | 9e98250 | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 193 | // Apply const/volatile/restrict qualifiers to T. |
| 194 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 195 | |
| 196 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 197 | // or incomplete types shall not be restrict-qualified." C++ also allows |
| 198 | // restrict-qualified references. |
| 199 | if (TypeQuals & QualType::Restrict) { |
Chris Lattner | cfac88d | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 200 | if (const PointerLikeType *PT = Result->getAsPointerLikeType()) { |
| 201 | QualType EltTy = PT->getPointeeType(); |
| 202 | |
| 203 | // If we have a pointer or reference, the pointee must have an object or |
| 204 | // incomplete type. |
| 205 | if (!EltTy->isIncompleteOrObjectType()) { |
| 206 | Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 207 | diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 208 | << EltTy << DS.getSourceRange(); |
Chris Lattner | cfac88d | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 209 | TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. |
| 210 | } |
| 211 | } else { |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 212 | Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 213 | diag::err_typecheck_invalid_restrict_not_pointer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 214 | << Result << DS.getSourceRange(); |
Chris Lattner | cfac88d | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 215 | TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 216 | } |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification |
| 220 | // of a function type includes any type qualifiers, the behavior is |
| 221 | // undefined." |
| 222 | if (Result->isFunctionType() && TypeQuals) { |
| 223 | // Get some location to point at, either the C or V location. |
| 224 | SourceLocation Loc; |
| 225 | if (TypeQuals & QualType::Const) |
| 226 | Loc = DS.getConstSpecLoc(); |
| 227 | else { |
| 228 | assert((TypeQuals & QualType::Volatile) && |
| 229 | "Has CV quals but not C or V?"); |
| 230 | Loc = DS.getVolatileSpecLoc(); |
| 231 | } |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 232 | Diag(Loc, diag::warn_typecheck_function_qualifiers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 233 | << Result << DS.getSourceRange(); |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Douglas Gregor | b7b28a2 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 236 | // C++ [dcl.ref]p1: |
| 237 | // Cv-qualified references are ill-formed except when the |
| 238 | // cv-qualifiers are introduced through the use of a typedef |
| 239 | // (7.1.3) or of a template type argument (14.3), in which |
| 240 | // case the cv-qualifiers are ignored. |
Douglas Gregor | a60c62e | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 241 | // FIXME: Shouldn't we be checking SCS_typedef here? |
| 242 | if (DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | b7b28a2 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 243 | TypeQuals && Result->isReferenceType()) { |
| 244 | TypeQuals &= ~QualType::Const; |
| 245 | TypeQuals &= ~QualType::Volatile; |
| 246 | } |
| 247 | |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 248 | Result = Result.getQualifiedType(TypeQuals); |
| 249 | } |
Chris Lattner | 9e98250 | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 250 | return Result; |
| 251 | } |
| 252 | |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 253 | /// GetTypeForDeclarator - Convert the type for the specified |
| 254 | /// declarator to Type instances. Skip the outermost Skip type |
| 255 | /// objects. |
Sebastian Redl | 66df3ef | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 256 | QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) { |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 257 | bool OmittedReturnType = false; |
| 258 | |
| 259 | if (D.getContext() == Declarator::BlockLiteralContext |
| 260 | && Skip == 0 |
| 261 | && !D.getDeclSpec().hasTypeSpecifier() |
| 262 | && (D.getNumTypeObjects() == 0 |
| 263 | || (D.getNumTypeObjects() == 1 |
| 264 | && D.getTypeObject(0).Kind == DeclaratorChunk::Function))) |
| 265 | OmittedReturnType = true; |
| 266 | |
Chris Lattner | 11f20f9 | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 267 | // long long is a C99 feature. |
Chris Lattner | 1a7d991 | 2007-08-28 16:41:29 +0000 | [diff] [blame] | 268 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Chris Lattner | 11f20f9 | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 269 | D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) |
| 270 | Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 271 | |
| 272 | // Determine the type of the declarator. Not all forms of declarator |
| 273 | // have a type. |
| 274 | QualType T; |
| 275 | switch (D.getKind()) { |
| 276 | case Declarator::DK_Abstract: |
| 277 | case Declarator::DK_Normal: |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 278 | case Declarator::DK_Operator: { |
| 279 | const DeclSpec& DS = D.getDeclSpec(); |
| 280 | if (OmittedReturnType) |
| 281 | // We default to a dependent type initially. Can be modified by |
| 282 | // the first return statement. |
| 283 | T = Context.DependentTy; |
Douglas Gregor | 6c0f406 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 284 | else { |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 285 | T = ConvertDeclSpecToType(DS); |
Douglas Gregor | 6c0f406 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 286 | if (T.isNull()) |
| 287 | return T; |
| 288 | } |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 289 | break; |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 290 | } |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 291 | |
| 292 | case Declarator::DK_Constructor: |
| 293 | case Declarator::DK_Destructor: |
| 294 | case Declarator::DK_Conversion: |
| 295 | // Constructors and destructors don't have return types. Use |
| 296 | // "void" instead. Conversion operators will check their return |
| 297 | // types separately. |
| 298 | T = Context.VoidTy; |
| 299 | break; |
| 300 | } |
Sebastian Redl | 19fec9d | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 301 | |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 302 | // Walk the DeclTypeInfo, building the recursive type as we go. |
| 303 | // DeclTypeInfos are ordered from the identifier out, which is |
| 304 | // opposite of what we want :). |
Sebastian Redl | 66df3ef | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 305 | for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) { |
| 306 | DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 307 | switch (DeclType.Kind) { |
| 308 | default: assert(0 && "Unknown decltype!"); |
Steve Naroff | 7aa5475 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 309 | case DeclaratorChunk::BlockPointer: |
| 310 | if (DeclType.Cls.TypeQuals) |
| 311 | Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type); |
| 312 | if (!T.getTypePtr()->isFunctionType()) |
| 313 | Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type); |
| 314 | else |
| 315 | T = Context.getBlockPointerType(T); |
| 316 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 317 | case DeclaratorChunk::Pointer: |
Chris Lattner | 36be3d8 | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 318 | if (T->isReferenceType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 319 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 320 | Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference) |
| 321 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 322 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 323 | T = Context.IntTy; |
| 324 | } |
| 325 | |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 326 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 327 | // object or incomplete types shall not be restrict-qualified." |
| 328 | if ((DeclType.Ptr.TypeQuals & QualType::Restrict) && |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 329 | !T->isIncompleteOrObjectType()) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 330 | Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 331 | << T; |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 332 | DeclType.Ptr.TypeQuals &= ~QualType::Restrict; |
| 333 | } |
| 334 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 335 | // Apply the pointer typequals to the pointer object. |
| 336 | T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals); |
| 337 | break; |
Douglas Gregor | b7b28a2 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 338 | case DeclaratorChunk::Reference: { |
| 339 | // Whether we should suppress the creation of the reference. |
| 340 | bool SuppressReference = false; |
| 341 | if (T->isReferenceType()) { |
| 342 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 343 | // |
| 344 | // According to C++ DR 106, references to references are only |
| 345 | // diagnosed when they are written directly (e.g., "int & &"), |
| 346 | // but not when they happen via a typedef: |
| 347 | // |
| 348 | // typedef int& intref; |
| 349 | // typedef intref& intref2; |
| 350 | // |
| 351 | // Parser::ParserDeclaratorInternal diagnoses the case where |
| 352 | // references are written directly; here, we handle the |
| 353 | // collapsing of references-to-references as described in C++ |
| 354 | // DR 106 and amended by C++ DR 540. |
| 355 | SuppressReference = true; |
| 356 | } |
| 357 | |
| 358 | // C++ [dcl.ref]p1: |
| 359 | // A declarator that specifies the type “reference to cv void” |
| 360 | // is ill-formed. |
| 361 | if (T->isVoidType()) { |
| 362 | Diag(DeclType.Loc, diag::err_reference_to_void); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 363 | D.setInvalidType(true); |
Douglas Gregor | b7b28a2 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 364 | T = Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 367 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 368 | // object or incomplete types shall not be restrict-qualified." |
| 369 | if (DeclType.Ref.HasRestrict && |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 370 | !T->isIncompleteOrObjectType()) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 371 | Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 372 | << T; |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 373 | DeclType.Ref.HasRestrict = false; |
| 374 | } |
| 375 | |
Douglas Gregor | b7b28a2 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 376 | if (!SuppressReference) |
| 377 | T = Context.getReferenceType(T); |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 378 | |
| 379 | // Handle restrict on references. |
| 380 | if (DeclType.Ref.HasRestrict) |
| 381 | T.addRestrict(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 382 | break; |
Douglas Gregor | b7b28a2 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 383 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 384 | case DeclaratorChunk::Array: { |
Chris Lattner | 67d3c8d | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 385 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 47958f6 | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 386 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 387 | ArrayType::ArraySizeModifier ASM; |
| 388 | if (ATI.isStar) |
| 389 | ASM = ArrayType::Star; |
| 390 | else if (ATI.hasStatic) |
| 391 | ASM = ArrayType::Static; |
| 392 | else |
| 393 | ASM = ArrayType::Normal; |
| 394 | |
| 395 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
| 396 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 397 | if (DiagnoseIncompleteType(D.getIdentifierLoc(), T, |
| 398 | diag::err_illegal_decl_array_incomplete_type)) { |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 399 | T = Context.IntTy; |
| 400 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 401 | } else if (T->isFunctionType()) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 402 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions) |
| 403 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 404 | T = Context.getPointerType(T); |
| 405 | D.setInvalidType(true); |
Chris Lattner | f0c4a0a | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 406 | } else if (const ReferenceType *RT = T->getAsReferenceType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 407 | // C++ 8.3.2p4: There shall be no ... arrays of references ... |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 408 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references) |
| 409 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Chris Lattner | cfac88d | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 410 | T = RT->getPointeeType(); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 411 | D.setInvalidType(true); |
Chris Lattner | 36be3d8 | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 412 | } else if (const RecordType *EltTy = T->getAsRecordType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 413 | // If the element type is a struct or union that contains a variadic |
Douglas Gregor | ec793e8 | 2009-02-10 21:49:46 +0000 | [diff] [blame] | 414 | // array, accept it as a GNU extension: C99 6.7.2.1p2. |
| 415 | if (EltTy->getDecl()->hasFlexibleArrayMember()) |
| 416 | Diag(DeclType.Loc, diag::ext_flexible_array_in_array) << T; |
Chris Lattner | 31fccaf | 2008-08-18 22:49:54 +0000 | [diff] [blame] | 417 | } else if (T->isObjCInterfaceType()) { |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 418 | Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 419 | } |
Chris Lattner | 31fccaf | 2008-08-18 22:49:54 +0000 | [diff] [blame] | 420 | |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 421 | // C99 6.7.5.2p1: The size expression shall have integer type. |
| 422 | if (ArraySize && !ArraySize->getType()->isIntegerType()) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 423 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 424 | << ArraySize->getType() << ArraySize->getSourceRange(); |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 425 | D.setInvalidType(true); |
Ted Kremenek | 06f217c | 2009-02-07 01:51:40 +0000 | [diff] [blame] | 426 | ArraySize->Destroy(Context); |
Chris Lattner | 67d3c8d | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 427 | ATI.NumElts = ArraySize = 0; |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 428 | } |
Eli Friedman | d431428 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 429 | llvm::APSInt ConstVal(32); |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 430 | if (!ArraySize) { |
| 431 | T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals); |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 432 | } else if (ArraySize->isValueDependent()) { |
| 433 | T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals); |
Eli Friedman | d431428 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 434 | } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || |
Sebastian Redl | 66df3ef | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 435 | !T->isConstantSizeType()) { |
Eli Friedman | 2ff28d1 | 2008-05-14 00:40:18 +0000 | [diff] [blame] | 436 | // Per C99, a variable array is an array with either a non-constant |
| 437 | // size or an element type that has a non-constant-size |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 438 | T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals); |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 439 | } else { |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 440 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 441 | // have a value greater than zero. |
| 442 | if (ConstVal.isSigned()) { |
| 443 | if (ConstVal.isNegative()) { |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 444 | Diag(ArraySize->getLocStart(), |
| 445 | diag::err_typecheck_negative_array_size) |
| 446 | << ArraySize->getSourceRange(); |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 447 | D.setInvalidType(true); |
| 448 | } else if (ConstVal == 0) { |
| 449 | // GCC accepts zero sized static arrays. |
Eli Friedman | d431428 | 2009-02-21 00:44:51 +0000 | [diff] [blame] | 450 | Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 451 | << ArraySize->getSourceRange(); |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 454 | T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals); |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 455 | } |
Chris Lattner | 47958f6 | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 456 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
Chris Lattner | 306d4df | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 457 | if (!getLangOptions().C99) { |
| 458 | if (ArraySize && !ArraySize->isValueDependent() && |
| 459 | !ArraySize->isIntegerConstantExpr(Context)) |
| 460 | Diag(D.getIdentifierLoc(), diag::ext_vla); |
| 461 | else if (ASM != ArrayType::Normal || ATI.TypeQuals != 0) |
| 462 | Diag(D.getIdentifierLoc(), diag::ext_c99_array_usage); |
| 463 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 464 | break; |
| 465 | } |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 466 | case DeclaratorChunk::Function: { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 467 | // If the function declarator has a prototype (i.e. it is not () and |
| 468 | // does not have a K&R-style identifier list), then the arguments are part |
| 469 | // of the type, otherwise the argument list is (). |
| 470 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Chris Lattner | bccfc15 | 2007-12-19 18:01:43 +0000 | [diff] [blame] | 471 | |
Chris Lattner | 6ad7e88 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 472 | // C99 6.7.5.3p1: The return type may not be a function or array type. |
Chris Lattner | bccfc15 | 2007-12-19 18:01:43 +0000 | [diff] [blame] | 473 | if (T->isArrayType() || T->isFunctionType()) { |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 474 | Diag(DeclType.Loc, diag::err_func_returning_array_function) << T; |
Chris Lattner | 6ad7e88 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 475 | T = Context.IntTy; |
| 476 | D.setInvalidType(true); |
| 477 | } |
| 478 | |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 479 | if (FTI.NumArgs == 0) { |
Argiris Kirtzidis | 1c51d47 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 480 | if (getLangOptions().CPlusPlus) { |
| 481 | // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the |
| 482 | // function takes no arguments. |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 483 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals); |
Douglas Gregor | 88a25f8 | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 484 | } else if (FTI.isVariadic) { |
| 485 | // We allow a zero-parameter variadic function in C if the |
| 486 | // function is marked with the "overloadable" |
| 487 | // attribute. Scan for this attribute now. |
| 488 | bool Overloadable = false; |
| 489 | for (const AttributeList *Attrs = D.getAttributes(); |
| 490 | Attrs; Attrs = Attrs->getNext()) { |
| 491 | if (Attrs->getKind() == AttributeList::AT_overloadable) { |
| 492 | Overloadable = true; |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if (!Overloadable) |
| 498 | Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg); |
| 499 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0); |
Argiris Kirtzidis | 1c51d47 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 500 | } else { |
| 501 | // Simple void foo(), where the incoming T is the result type. |
| 502 | T = Context.getFunctionTypeNoProto(T); |
| 503 | } |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 504 | } else if (FTI.ArgInfo[0].Param == 0) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 505 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 506 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 507 | } else { |
| 508 | // Otherwise, we have a function with an argument list that is |
| 509 | // potentially variadic. |
| 510 | llvm::SmallVector<QualType, 16> ArgTys; |
| 511 | |
| 512 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 513 | ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param; |
| 514 | QualType ArgTy = Param->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 515 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 516 | // |
| 517 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 518 | // This matches the conversion that is done in |
Nate Begeman | 2240f54 | 2007-11-13 21:49:48 +0000 | [diff] [blame] | 519 | // Sema::ActOnParamDeclarator(). Without this conversion, the |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 520 | // argument type in the function prototype *will not* match the |
| 521 | // type in ParmVarDecl (which makes the code generator unhappy). |
| 522 | // |
| 523 | // FIXME: We still apparently need the conversion in |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 524 | // Sema::ActOnParamDeclarator(). This doesn't make any sense, since |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 525 | // it should be driving off the type being created here. |
| 526 | // |
| 527 | // FIXME: If a source translation tool needs to see the original type, |
| 528 | // then we need to consider storing both types somewhere... |
| 529 | // |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 530 | if (ArgTy->isArrayType()) { |
| 531 | ArgTy = Context.getArrayDecayedType(ArgTy); |
Chris Lattner | c08564a | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 532 | } else if (ArgTy->isFunctionType()) |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 533 | ArgTy = Context.getPointerType(ArgTy); |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 534 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 535 | // Look for 'void'. void is allowed only as a single argument to a |
| 536 | // function with no other parameters (C99 6.7.5.3p10). We record |
| 537 | // int(void) as a FunctionTypeProto with an empty argument list. |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 538 | else if (ArgTy->isVoidType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 539 | // If this is something like 'float(int, void)', reject it. 'void' |
| 540 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 541 | // have arguments of incomplete type. |
| 542 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 543 | Diag(DeclType.Loc, diag::err_void_only_param); |
| 544 | ArgTy = Context.IntTy; |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 545 | Param->setType(ArgTy); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 546 | } else if (FTI.ArgInfo[i].Ident) { |
| 547 | // Reject, but continue to parse 'int(void abc)'. |
| 548 | Diag(FTI.ArgInfo[i].IdentLoc, |
| 549 | diag::err_param_with_void_type); |
| 550 | ArgTy = Context.IntTy; |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 551 | Param->setType(ArgTy); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 552 | } else { |
| 553 | // Reject, but continue to parse 'float(const void)'. |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 554 | if (ArgTy.getCVRQualifiers()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 555 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
| 556 | |
| 557 | // Do not add 'void' to the ArgTys list. |
| 558 | break; |
| 559 | } |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 560 | } else if (!FTI.hasPrototype) { |
| 561 | if (ArgTy->isPromotableIntegerType()) { |
| 562 | ArgTy = Context.IntTy; |
| 563 | } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) { |
| 564 | if (BTy->getKind() == BuiltinType::Float) |
| 565 | ArgTy = Context.DoubleTy; |
| 566 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | ArgTys.push_back(ArgTy); |
| 570 | } |
| 571 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 572 | FTI.isVariadic, FTI.TypeQuals); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 573 | } |
| 574 | break; |
| 575 | } |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 576 | case DeclaratorChunk::MemberPointer: |
| 577 | // The scope spec must refer to a class, or be dependent. |
| 578 | DeclContext *DC = static_cast<DeclContext*>( |
| 579 | DeclType.Mem.Scope().getScopeRep()); |
| 580 | QualType ClsType; |
| 581 | // FIXME: Extend for dependent types when it's actually supported. |
| 582 | // See ActOnCXXNestedNameSpecifier. |
| 583 | if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) { |
| 584 | ClsType = Context.getTagDeclType(RD); |
| 585 | } else { |
| 586 | if (DC) { |
| 587 | Diag(DeclType.Mem.Scope().getBeginLoc(), |
| 588 | diag::err_illegal_decl_mempointer_in_nonclass) |
| 589 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") |
| 590 | << DeclType.Mem.Scope().getRange(); |
| 591 | } |
| 592 | D.setInvalidType(true); |
| 593 | ClsType = Context.IntTy; |
| 594 | } |
| 595 | |
| 596 | // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member |
| 597 | // with reference type, or "cv void." |
| 598 | if (T->isReferenceType()) { |
| 599 | Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference) |
| 600 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
| 601 | D.setInvalidType(true); |
| 602 | T = Context.IntTy; |
| 603 | } |
| 604 | if (T->isVoidType()) { |
| 605 | Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void) |
| 606 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
| 607 | T = Context.IntTy; |
| 608 | } |
| 609 | |
| 610 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 611 | // object or incomplete types shall not be restrict-qualified." |
| 612 | if ((DeclType.Mem.TypeQuals & QualType::Restrict) && |
| 613 | !T->isIncompleteOrObjectType()) { |
| 614 | Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 615 | << T; |
| 616 | DeclType.Mem.TypeQuals &= ~QualType::Restrict; |
| 617 | } |
| 618 | |
Sebastian Redl | ba38756 | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 619 | T = Context.getMemberPointerType(T, ClsType.getTypePtr()). |
| 620 | getQualifiedType(DeclType.Mem.TypeQuals); |
Sebastian Redl | 7555503 | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 621 | |
| 622 | break; |
| 623 | } |
| 624 | |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 625 | // See if there are any attributes on this declarator chunk. |
| 626 | if (const AttributeList *AL = DeclType.getAttrs()) |
| 627 | ProcessTypeAttributeList(T, AL); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 628 | } |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 629 | |
| 630 | if (getLangOptions().CPlusPlus && T->isFunctionType()) { |
| 631 | const FunctionTypeProto *FnTy = T->getAsFunctionTypeProto(); |
| 632 | assert(FnTy && "Why oh why is there not a FunctionTypeProto here ?"); |
| 633 | |
| 634 | // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type |
| 635 | // for a nonstatic member function, the function type to which a pointer |
| 636 | // to member refers, or the top-level function type of a function typedef |
| 637 | // declaration. |
| 638 | if (FnTy->getTypeQuals() != 0 && |
| 639 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Douglas Gregor | c5cbc24 | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 640 | ((D.getContext() != Declarator::MemberContext && |
| 641 | (!D.getCXXScopeSpec().isSet() || |
| 642 | !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep()) |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 643 | ->isRecord())) || |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 644 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 645 | if (D.isFunctionDeclarator()) |
| 646 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type); |
| 647 | else |
| 648 | Diag(D.getIdentifierLoc(), |
| 649 | diag::err_invalid_qualified_typedef_function_type_use); |
| 650 | |
| 651 | // Strip the cv-quals from the type. |
| 652 | T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 653 | FnTy->getNumArgs(), FnTy->isVariadic(), 0); |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 656 | |
Chris Lattner | f9e90cc | 2008-06-29 00:19:33 +0000 | [diff] [blame] | 657 | // If there were any type attributes applied to the decl itself (not the |
| 658 | // type, apply the type attribute to the type!) |
| 659 | if (const AttributeList *Attrs = D.getAttributes()) |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 660 | ProcessTypeAttributeList(T, Attrs); |
Chris Lattner | f9e90cc | 2008-06-29 00:19:33 +0000 | [diff] [blame] | 661 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 662 | return T; |
| 663 | } |
| 664 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 665 | /// ObjCGetTypeForMethodDefinition - Builds the type for a method definition |
Fariborz Jahanian | ff746bc | 2007-11-09 22:27:59 +0000 | [diff] [blame] | 666 | /// declarator |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 667 | QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) { |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 668 | ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(static_cast<Decl *>(D)); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 669 | QualType T = MDecl->getResultType(); |
| 670 | llvm::SmallVector<QualType, 16> ArgTys; |
| 671 | |
Fariborz Jahanian | ea86cb8 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 672 | // Add the first two invisible argument types for self and _cmd. |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 673 | if (MDecl->isInstanceMethod()) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 674 | QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); |
Fariborz Jahanian | 8473b22 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 675 | selfTy = Context.getPointerType(selfTy); |
| 676 | ArgTys.push_back(selfTy); |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 677 | } else |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 678 | ArgTys.push_back(Context.getObjCIdType()); |
| 679 | ArgTys.push_back(Context.getObjCSelType()); |
Fariborz Jahanian | ea86cb8 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 680 | |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 681 | for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), |
| 682 | E = MDecl->param_end(); PI != E; ++PI) { |
| 683 | QualType ArgTy = (*PI)->getType(); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 684 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 685 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 686 | // This matches the conversion that is done in |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 687 | // Sema::ActOnParamDeclarator(). |
| 688 | if (ArgTy->isArrayType()) |
| 689 | ArgTy = Context.getArrayDecayedType(ArgTy); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 690 | else if (ArgTy->isFunctionType()) |
| 691 | ArgTy = Context.getPointerType(ArgTy); |
| 692 | ArgTys.push_back(ArgTy); |
| 693 | } |
| 694 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 695 | MDecl->isVariadic(), 0); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 696 | return T; |
| 697 | } |
| 698 | |
Sebastian Redl | 8ebd8fb | 2009-01-26 19:54:48 +0000 | [diff] [blame] | 699 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 700 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 701 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 702 | /// or pointer-to-member types, or if they are not similar at this |
| 703 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 704 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 705 | /// be called in a loop that successively "unwraps" pointer and |
| 706 | /// pointer-to-member types to compare them at each level. |
Chris Lattner | 6d0d871 | 2009-02-16 21:43:00 +0000 | [diff] [blame] | 707 | bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) { |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 708 | const PointerType *T1PtrType = T1->getAsPointerType(), |
| 709 | *T2PtrType = T2->getAsPointerType(); |
| 710 | if (T1PtrType && T2PtrType) { |
| 711 | T1 = T1PtrType->getPointeeType(); |
| 712 | T2 = T2PtrType->getPointeeType(); |
| 713 | return true; |
| 714 | } |
| 715 | |
Sebastian Redl | ba38756 | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 716 | const MemberPointerType *T1MPType = T1->getAsMemberPointerType(), |
| 717 | *T2MPType = T2->getAsMemberPointerType(); |
Sebastian Redl | f41a58c | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 718 | if (T1MPType && T2MPType && |
| 719 | Context.getCanonicalType(T1MPType->getClass()) == |
| 720 | Context.getCanonicalType(T2MPType->getClass())) { |
Sebastian Redl | ba38756 | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 721 | T1 = T1MPType->getPointeeType(); |
| 722 | T2 = T2MPType->getPointeeType(); |
| 723 | return true; |
| 724 | } |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 725 | return false; |
| 726 | } |
| 727 | |
Sebastian Redl | 66df3ef | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 728 | Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 729 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 730 | // the parser. |
| 731 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
| 732 | |
Sebastian Redl | 66df3ef | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 733 | QualType T = GetTypeForDeclarator(D, S); |
Douglas Gregor | 6c0f406 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 734 | if (T.isNull()) |
| 735 | return true; |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 736 | |
Douglas Gregor | 2b9422f | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 737 | // Check that there are no default arguments (C++ only). |
| 738 | if (getLangOptions().CPlusPlus) |
| 739 | CheckExtraCXXDefaultArguments(D); |
| 740 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 741 | return T.getAsOpaquePtr(); |
| 742 | } |
| 743 | |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 744 | |
| 745 | |
| 746 | //===----------------------------------------------------------------------===// |
| 747 | // Type Attribute Processing |
| 748 | //===----------------------------------------------------------------------===// |
| 749 | |
| 750 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 751 | /// specified type. The attribute contains 1 argument, the id of the address |
| 752 | /// space for the type. |
| 753 | static void HandleAddressSpaceTypeAttribute(QualType &Type, |
| 754 | const AttributeList &Attr, Sema &S){ |
| 755 | // If this type is already address space qualified, reject it. |
| 756 | // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers |
| 757 | // for two or more different address spaces." |
| 758 | if (Type.getAddressSpace()) { |
| 759 | S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); |
| 760 | return; |
| 761 | } |
| 762 | |
| 763 | // Check the attribute arguments. |
| 764 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 765 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 766 | return; |
| 767 | } |
| 768 | Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 769 | llvm::APSInt addrSpace(32); |
| 770 | if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 771 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) |
| 772 | << ASArgExpr->getSourceRange(); |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 773 | return; |
| 774 | } |
| 775 | |
| 776 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 777 | Type = S.Context.getAddrSpaceQualType(Type, ASIdx); |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 780 | /// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the |
| 781 | /// specified type. The attribute contains 1 argument, weak or strong. |
| 782 | static void HandleObjCGCTypeAttribute(QualType &Type, |
Chris Lattner | d9b85c3 | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 783 | const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 784 | if (Type.getObjCGCAttr() != QualType::GCNone) { |
Fariborz Jahanian | 31804e1 | 2009-02-18 18:52:41 +0000 | [diff] [blame] | 785 | S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc); |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 786 | return; |
| 787 | } |
| 788 | |
| 789 | // Check the attribute arguments. |
Fariborz Jahanian | 80ff83c | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 790 | if (!Attr.getParameterName()) { |
| 791 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 792 | << "objc_gc" << 1; |
| 793 | return; |
| 794 | } |
Chris Lattner | d9b85c3 | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 795 | QualType::GCAttrTypes GCAttr; |
Fariborz Jahanian | 80ff83c | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 796 | if (Attr.getNumArgs() != 0) { |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 797 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 798 | return; |
| 799 | } |
| 800 | if (Attr.getParameterName()->isStr("weak")) |
Chris Lattner | d9b85c3 | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 801 | GCAttr = QualType::Weak; |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 802 | else if (Attr.getParameterName()->isStr("strong")) |
Chris Lattner | d9b85c3 | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 803 | GCAttr = QualType::Strong; |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 804 | else { |
| 805 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 806 | << "objc_gc" << Attr.getParameterName(); |
| 807 | return; |
| 808 | } |
| 809 | |
Chris Lattner | d9b85c3 | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 810 | Type = S.Context.getObjCGCQualType(Type, GCAttr); |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 813 | void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) { |
Chris Lattner | 1aaeeb9 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 814 | // Scan through and apply attributes to this type where it makes sense. Some |
| 815 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 816 | // type, but others can be present in the type specifiers even though they |
Chris Lattner | 99dbc96 | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 817 | // apply to the decl. Here we apply type attributes and ignore the rest. |
| 818 | for (; AL; AL = AL->getNext()) { |
Chris Lattner | 1aaeeb9 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 819 | // If this is an attribute we can handle, do so now, otherwise, add it to |
| 820 | // the LeftOverAttrs list for rechaining. |
Chris Lattner | 99dbc96 | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 821 | switch (AL->getKind()) { |
Chris Lattner | 1aaeeb9 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 822 | default: break; |
| 823 | case AttributeList::AT_address_space: |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 824 | HandleAddressSpaceTypeAttribute(Result, *AL, *this); |
| 825 | break; |
Fariborz Jahanian | af23809 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 826 | case AttributeList::AT_objc_gc: |
| 827 | HandleObjCGCTypeAttribute(Result, *AL, *this); |
| 828 | break; |
Chris Lattner | 1aaeeb9 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 829 | } |
Chris Lattner | 1aaeeb9 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 830 | } |
Chris Lattner | 1aaeeb9 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 833 | /// @brief If the type T is incomplete and cannot be completed, |
| 834 | /// produce a suitable diagnostic. |
| 835 | /// |
| 836 | /// This routine checks whether the type @p T is complete in any |
| 837 | /// context where a complete type is required. If @p T is a complete |
| 838 | /// type, returns false. If @p T is incomplete, issues the diagnostic |
| 839 | /// @p diag (giving it the type @p T) and returns true. |
| 840 | /// |
| 841 | /// @param Loc The location in the source that the incomplete type |
| 842 | /// diagnostic should refer to. |
| 843 | /// |
| 844 | /// @param T The type that this routine is examining for completeness. |
| 845 | /// |
| 846 | /// @param diag The diagnostic value (e.g., |
| 847 | /// @c diag::err_typecheck_decl_incomplete_type) that will be used |
| 848 | /// for the error message if @p T is incomplete. |
| 849 | /// |
| 850 | /// @param Range1 An optional range in the source code that will be a |
| 851 | /// part of the "incomplete type" error message. |
| 852 | /// |
| 853 | /// @param Range2 An optional range in the source code that will be a |
| 854 | /// part of the "incomplete type" error message. |
| 855 | /// |
| 856 | /// @param PrintType If non-NULL, the type that should be printed |
| 857 | /// instead of @p T. This parameter should be used when the type that |
| 858 | /// we're checking for incompleteness isn't the type that should be |
| 859 | /// displayed to the user, e.g., when T is a type and PrintType is a |
| 860 | /// pointer to T. |
| 861 | /// |
| 862 | /// @returns @c true if @p T is incomplete and a diagnostic was emitted, |
| 863 | /// @c false otherwise. |
| 864 | /// |
| 865 | /// @todo When Clang gets proper support for C++ templates, this |
| 866 | /// routine will also be able perform template instantiation when @p T |
| 867 | /// is a class template specialization. |
| 868 | bool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag, |
| 869 | SourceRange Range1, SourceRange Range2, |
| 870 | QualType PrintType) { |
| 871 | // If we have a complete type, we're done. |
| 872 | if (!T->isIncompleteType()) |
| 873 | return false; |
Eli Friedman | 86ad522 | 2008-05-27 03:33:27 +0000 | [diff] [blame] | 874 | |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 875 | if (PrintType.isNull()) |
| 876 | PrintType = T; |
| 877 | |
| 878 | // We have an incomplete type. Produce a diagnostic. |
| 879 | Diag(Loc, diag) << PrintType << Range1 << Range2; |
| 880 | |
| 881 | // If the type was a forward declaration of a class/struct/union |
| 882 | // type, produce |
| 883 | const TagType *Tag = 0; |
| 884 | if (const RecordType *Record = T->getAsRecordType()) |
| 885 | Tag = Record; |
| 886 | else if (const EnumType *Enum = T->getAsEnumType()) |
| 887 | Tag = Enum; |
| 888 | |
| 889 | if (Tag && !Tag->getDecl()->isInvalidDecl()) |
| 890 | Diag(Tag->getDecl()->getLocation(), |
| 891 | Tag->isBeingDefined() ? diag::note_type_being_defined |
| 892 | : diag::note_forward_declaration) |
| 893 | << QualType(Tag, 0); |
| 894 | |
| 895 | return true; |
| 896 | } |