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