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