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