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