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" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeLocVisitor.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PartialDiagnostic.h" |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 23 | #include "clang/Parse/DeclSpec.h" |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 27 | /// \brief Perform adjustment on the parameter type of a function. |
| 28 | /// |
| 29 | /// This routine adjusts the given parameter type @p T to the actual |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | /// parameter type used by semantic analysis (C99 6.7.5.3p[7,8], |
| 31 | /// C++ [dcl.fct]p3). The adjusted parameter type is returned. |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 32 | QualType Sema::adjustParameterType(QualType T) { |
| 33 | // C99 6.7.5.3p7: |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 34 | // A declaration of a parameter as "array of type" shall be |
| 35 | // adjusted to "qualified pointer to type", where the type |
| 36 | // qualifiers (if any) are those specified within the [ and ] of |
| 37 | // the array type derivation. |
| 38 | if (T->isArrayType()) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 39 | return Context.getArrayDecayedType(T); |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 40 | |
| 41 | // C99 6.7.5.3p8: |
| 42 | // A declaration of a parameter as "function returning type" |
| 43 | // shall be adjusted to "pointer to function returning type", as |
| 44 | // in 6.3.2.1. |
| 45 | if (T->isFunctionType()) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 46 | return Context.getPointerType(T); |
| 47 | |
| 48 | return T; |
| 49 | } |
| 50 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 51 | /// \brief Convert the specified declspec to the appropriate type |
| 52 | /// object. |
| 53 | /// \param DS the declaration specifiers |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 54 | /// \param DeclLoc The location of the declarator identifier or invalid if none. |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 55 | /// \returns The type described by the declaration specifiers. This function |
| 56 | /// never returns null. |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 57 | static QualType ConvertDeclSpecToType(const DeclSpec &DS, |
| 58 | SourceLocation DeclLoc, |
| 59 | bool &isInvalid, Sema &TheSema) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 61 | // checking. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 62 | QualType Result; |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 63 | |
| 64 | ASTContext &Context = TheSema.Context; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | switch (DS.getTypeSpecType()) { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 67 | case DeclSpec::TST_void: |
| 68 | Result = Context.VoidTy; |
| 69 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | case DeclSpec::TST_char: |
| 71 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 72 | Result = Context.CharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 74 | Result = Context.SignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | else { |
| 76 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 77 | "Unknown TSS value"); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 78 | Result = Context.UnsignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 80 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 81 | case DeclSpec::TST_wchar: |
| 82 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
| 83 | Result = Context.WCharTy; |
| 84 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 85 | TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 86 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 87 | Result = Context.getSignedWCharType(); |
| 88 | } else { |
| 89 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 90 | "Unknown TSS value"); |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 91 | TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 92 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 93 | Result = Context.getUnsignedWCharType(); |
| 94 | } |
| 95 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 96 | case DeclSpec::TST_char16: |
| 97 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 98 | "Unknown TSS value"); |
| 99 | Result = Context.Char16Ty; |
| 100 | break; |
| 101 | case DeclSpec::TST_char32: |
| 102 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 103 | "Unknown TSS value"); |
| 104 | Result = Context.Char32Ty; |
| 105 | break; |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 106 | case DeclSpec::TST_unspecified: |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 107 | // "<proto1,proto2>" is an objc qualified ID with a missing id. |
Chris Lattner | 097e916 | 2008-10-20 02:01:50 +0000 | [diff] [blame] | 108 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 110 | (ObjCProtocolDecl**)PQ, |
Steve Naroff | 683087f | 2009-06-29 16:22:52 +0000 | [diff] [blame] | 111 | DS.getNumProtocolQualifiers()); |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 112 | break; |
| 113 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 115 | // Unspecified typespec defaults to int in C90. However, the C90 grammar |
| 116 | // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, |
| 117 | // type-qualifier, or storage-class-specifier. If not, emit an extwarn. |
| 118 | // Note that the one exception to this is function definitions, which are |
| 119 | // allowed to be completely missing a declspec. This is handled in the |
| 120 | // parser already though by it pretending to have seen an 'int' in this |
| 121 | // case. |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 122 | if (TheSema.getLangOptions().ImplicitInt) { |
Chris Lattner | 35d276f | 2009-02-27 18:53:28 +0000 | [diff] [blame] | 123 | // In C89 mode, we only warn if there is a completely missing declspec |
| 124 | // when one is not allowed. |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 125 | if (DS.isEmpty()) { |
| 126 | if (DeclLoc.isInvalid()) |
| 127 | DeclLoc = DS.getSourceRange().getBegin(); |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 128 | TheSema.Diag(DeclLoc, diag::ext_missing_declspec) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 129 | << DS.getSourceRange() |
Chris Lattner | 173144a | 2009-02-27 22:31:56 +0000 | [diff] [blame] | 130 | << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(), |
| 131 | "int"); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 132 | } |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 133 | } else if (!DS.hasTypeSpecifier()) { |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 134 | // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: |
| 135 | // "At least one type specifier shall be given in the declaration |
| 136 | // specifiers in each declaration, and in the specifier-qualifier list in |
| 137 | // each struct declaration and type name." |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 138 | // FIXME: Does Microsoft really have the implicit int extension in C++? |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 139 | if (DeclLoc.isInvalid()) |
| 140 | DeclLoc = DS.getSourceRange().getBegin(); |
| 141 | |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 142 | if (TheSema.getLangOptions().CPlusPlus && |
| 143 | !TheSema.getLangOptions().Microsoft) { |
| 144 | TheSema.Diag(DeclLoc, diag::err_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 145 | << DS.getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 147 | // When this occurs in C++ code, often something is very broken with the |
| 148 | // value being declared, poison it as invalid so we don't get chains of |
| 149 | // errors. |
| 150 | isInvalid = true; |
| 151 | } else { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 152 | TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 153 | << DS.getSourceRange(); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 155 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | |
| 157 | // FALL THROUGH. |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 158 | case DeclSpec::TST_int: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 160 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 161 | case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; |
| 162 | case DeclSpec::TSW_short: Result = Context.ShortTy; break; |
| 163 | case DeclSpec::TSW_long: Result = Context.LongTy; break; |
| 164 | case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | } |
| 166 | } else { |
| 167 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 168 | case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; |
| 169 | case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; |
| 170 | case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; |
| 171 | case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 174 | break; |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 175 | } |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 176 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 177 | case DeclSpec::TST_double: |
| 178 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 179 | Result = Context.LongDoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 180 | else |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 181 | Result = Context.DoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 182 | break; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 183 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | case DeclSpec::TST_decimal32: // _Decimal32 |
| 185 | case DeclSpec::TST_decimal64: // _Decimal64 |
| 186 | case DeclSpec::TST_decimal128: // _Decimal128 |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 187 | TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 188 | Result = Context.IntTy; |
| 189 | isInvalid = true; |
| 190 | break; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 191 | case DeclSpec::TST_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | case DeclSpec::TST_enum: |
| 193 | case DeclSpec::TST_union: |
| 194 | case DeclSpec::TST_struct: { |
| 195 | Decl *D = static_cast<Decl *>(DS.getTypeRep()); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 196 | if (!D) { |
| 197 | // This can happen in C++ with ambiguous lookups. |
| 198 | Result = Context.IntTy; |
| 199 | isInvalid = true; |
| 200 | break; |
| 201 | } |
| 202 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 203 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 204 | DS.getTypeSpecSign() == 0 && |
| 205 | "Can't handle qualifiers on typedef names yet!"); |
| 206 | // TypeQuals handled by caller. |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 207 | Result = Context.getTypeDeclType(cast<TypeDecl>(D)); |
John McCall | 2191b20 | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 208 | |
| 209 | // In C++, make an ElaboratedType. |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 210 | if (TheSema.getLangOptions().CPlusPlus) { |
John McCall | 2191b20 | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 211 | TagDecl::TagKind Tag |
| 212 | = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType()); |
| 213 | Result = Context.getElaboratedType(Result, Tag); |
| 214 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 216 | if (D->isInvalidDecl()) |
| 217 | isInvalid = true; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 218 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | } |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 220 | case DeclSpec::TST_typename: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 221 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 222 | DS.getTypeSpecSign() == 0 && |
| 223 | "Can't handle qualifiers on typedef names yet!"); |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 224 | Result = TheSema.GetTypeFromParser(DS.getTypeRep()); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 225 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 226 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 227 | if (const ObjCInterfaceType * |
| 228 | Interface = Result->getAs<ObjCInterfaceType>()) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 229 | // It would be nice if protocol qualifiers were only stored with the |
| 230 | // ObjCObjectPointerType. Unfortunately, this isn't possible due |
| 231 | // to the following typedef idiom (which is uncommon, but allowed): |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | // |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 233 | // typedef Foo<P> T; |
| 234 | // static void func() { |
| 235 | // Foo<P> *yy; |
| 236 | // T *zz; |
| 237 | // } |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 238 | Result = Context.getObjCInterfaceType(Interface->getDecl(), |
| 239 | (ObjCProtocolDecl**)PQ, |
| 240 | DS.getNumProtocolQualifiers()); |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 241 | } else if (Result->isObjCIdType()) |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 242 | // id<protocol-list> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 244 | (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers()); |
| 245 | else if (Result->isObjCClassType()) { |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 246 | if (DeclLoc.isInvalid()) |
| 247 | DeclLoc = DS.getSourceRange().getBegin(); |
Steve Naroff | 4262a07 | 2009-02-23 18:53:24 +0000 | [diff] [blame] | 248 | // Class<protocol-list> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy, |
Steve Naroff | 470301b | 2009-07-22 16:07:01 +0000 | [diff] [blame] | 250 | (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers()); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 251 | } else { |
| 252 | if (DeclLoc.isInvalid()) |
| 253 | DeclLoc = DS.getSourceRange().getBegin(); |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 254 | TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 255 | << DS.getSourceRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 256 | isInvalid = true; |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 257 | } |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 258 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 260 | // If this is a reference to an invalid typedef, propagate the invalidity. |
| 261 | if (TypedefType *TDT = dyn_cast<TypedefType>(Result)) |
| 262 | if (TDT->getDecl()->isInvalidDecl()) |
| 263 | isInvalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 265 | // TypeQuals handled by caller. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 266 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 268 | case DeclSpec::TST_typeofType: |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 269 | // FIXME: Preserve type source info. |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 270 | Result = TheSema.GetTypeFromParser(DS.getTypeRep()); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 271 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 272 | // TypeQuals handled by caller. |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 273 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 274 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 275 | case DeclSpec::TST_typeofExpr: { |
| 276 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 277 | assert(E && "Didn't get an expression for typeof?"); |
| 278 | // TypeQuals handled by caller. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 279 | Result = Context.getTypeOfExprType(E); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 280 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 281 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 282 | case DeclSpec::TST_decltype: { |
| 283 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 284 | assert(E && "Didn't get an expression for decltype?"); |
| 285 | // TypeQuals handled by caller. |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 286 | Result = TheSema.BuildDecltypeType(E); |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 287 | if (Result.isNull()) { |
| 288 | Result = Context.IntTy; |
| 289 | isInvalid = true; |
| 290 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 291 | break; |
| 292 | } |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 293 | case DeclSpec::TST_auto: { |
| 294 | // TypeQuals handled by caller. |
| 295 | Result = Context.UndeducedAutoTy; |
| 296 | break; |
| 297 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 299 | case DeclSpec::TST_error: |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 300 | Result = Context.IntTy; |
| 301 | isInvalid = true; |
| 302 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 303 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 305 | // Handle complex types. |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 306 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 307 | if (TheSema.getLangOptions().Freestanding) |
| 308 | TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 309 | Result = Context.getComplexType(Result); |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 310 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 312 | assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && |
| 313 | "FIXME: imaginary types not supported yet!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 38d8b98 | 2008-02-20 22:04:11 +0000 | [diff] [blame] | 315 | // See if there are any attributes on the declspec that apply to the type (as |
| 316 | // opposed to the decl). |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 317 | if (const AttributeList *AL = DS.getAttributes()) |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 318 | TheSema.ProcessTypeAttributeList(Result, AL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 320 | // Apply const/volatile/restrict qualifiers to T. |
| 321 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 322 | |
| 323 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 324 | // or incomplete types shall not be restrict-qualified." C++ also allows |
| 325 | // restrict-qualified references. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 326 | if (TypeQuals & DeclSpec::TQ_restrict) { |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 327 | if (Result->isPointerType() || Result->isReferenceType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | QualType EltTy = Result->isPointerType() ? |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 329 | Result->getAs<PointerType>()->getPointeeType() : |
| 330 | Result->getAs<ReferenceType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 332 | // If we have a pointer or reference, the pointee must have an object |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 333 | // incomplete type. |
| 334 | if (!EltTy->isIncompleteOrObjectType()) { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 335 | TheSema.Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 336 | diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 337 | << EltTy << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 338 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 339 | } |
| 340 | } else { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 341 | TheSema.Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 342 | diag::err_typecheck_invalid_restrict_not_pointer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 343 | << Result << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 344 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 345 | } |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 346 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 348 | // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification |
| 349 | // of a function type includes any type qualifiers, the behavior is |
| 350 | // undefined." |
| 351 | if (Result->isFunctionType() && TypeQuals) { |
| 352 | // Get some location to point at, either the C or V location. |
| 353 | SourceLocation Loc; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 354 | if (TypeQuals & DeclSpec::TQ_const) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 355 | Loc = DS.getConstSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 356 | else if (TypeQuals & DeclSpec::TQ_volatile) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 357 | Loc = DS.getVolatileSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 358 | else { |
| 359 | assert((TypeQuals & DeclSpec::TQ_restrict) && |
| 360 | "Has CVR quals but not C, V, or R?"); |
| 361 | Loc = DS.getRestrictSpecLoc(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 362 | } |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 363 | TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 364 | << Result << DS.getSourceRange(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 365 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 367 | // C++ [dcl.ref]p1: |
| 368 | // Cv-qualified references are ill-formed except when the |
| 369 | // cv-qualifiers are introduced through the use of a typedef |
| 370 | // (7.1.3) or of a template type argument (14.3), in which |
| 371 | // case the cv-qualifiers are ignored. |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 372 | // FIXME: Shouldn't we be checking SCS_typedef here? |
| 373 | if (DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 374 | TypeQuals && Result->isReferenceType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 375 | TypeQuals &= ~DeclSpec::TQ_const; |
| 376 | TypeQuals &= ~DeclSpec::TQ_volatile; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | } |
| 378 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 379 | Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals); |
| 380 | Result = Context.getQualifiedType(Result, Quals); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 381 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 382 | |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 383 | return Result; |
| 384 | } |
| 385 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 386 | static std::string getPrintableNameForEntity(DeclarationName Entity) { |
| 387 | if (Entity) |
| 388 | return Entity.getAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 390 | return "type name"; |
| 391 | } |
| 392 | |
| 393 | /// \brief Build a pointer type. |
| 394 | /// |
| 395 | /// \param T The type to which we'll be building a pointer. |
| 396 | /// |
| 397 | /// \param Quals The cvr-qualifiers to be applied to the pointer type. |
| 398 | /// |
| 399 | /// \param Loc The location of the entity whose type involves this |
| 400 | /// pointer type or, if there is no such entity, the location of the |
| 401 | /// type that will have pointer type. |
| 402 | /// |
| 403 | /// \param Entity The name of the entity that involves the pointer |
| 404 | /// type, if known. |
| 405 | /// |
| 406 | /// \returns A suitable pointer type, if there are no |
| 407 | /// errors. Otherwise, returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | QualType Sema::BuildPointerType(QualType T, unsigned Quals, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 409 | SourceLocation Loc, DeclarationName Entity) { |
| 410 | if (T->isReferenceType()) { |
| 411 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
| 412 | Diag(Loc, diag::err_illegal_decl_pointer_to_reference) |
| 413 | << getPrintableNameForEntity(Entity); |
| 414 | return QualType(); |
| 415 | } |
| 416 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 417 | Qualifiers Qs = Qualifiers::fromCVRMask(Quals); |
| 418 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 419 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 420 | // object or incomplete types shall not be restrict-qualified." |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 421 | if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 422 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 423 | << T; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 424 | Qs.removeRestrict(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | // Build the pointer type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 428 | return Context.getQualifiedType(Context.getPointerType(T), Qs); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /// \brief Build a reference type. |
| 432 | /// |
| 433 | /// \param T The type to which we'll be building a reference. |
| 434 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 435 | /// \param CVR The cvr-qualifiers to be applied to the reference type. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 436 | /// |
| 437 | /// \param Loc The location of the entity whose type involves this |
| 438 | /// reference type or, if there is no such entity, the location of the |
| 439 | /// type that will have reference type. |
| 440 | /// |
| 441 | /// \param Entity The name of the entity that involves the reference |
| 442 | /// type, if known. |
| 443 | /// |
| 444 | /// \returns A suitable reference type, if there are no |
| 445 | /// errors. Otherwise, returns a NULL type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 446 | QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue, |
| 447 | unsigned CVR, SourceLocation Loc, |
| 448 | DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 449 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 450 | |
| 451 | bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>(); |
| 452 | |
| 453 | // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a |
| 454 | // reference to a type T, and attempt to create the type "lvalue |
| 455 | // reference to cv TD" creates the type "lvalue reference to T". |
| 456 | // We use the qualifiers (restrict or none) of the original reference, |
| 457 | // not the new ones. This is consistent with GCC. |
| 458 | |
| 459 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 460 | // |
| 461 | // According to C++ DR 106, references to references are only |
| 462 | // diagnosed when they are written directly (e.g., "int & &"), |
| 463 | // but not when they happen via a typedef: |
| 464 | // |
| 465 | // typedef int& intref; |
| 466 | // typedef intref& intref2; |
| 467 | // |
| 468 | // Parser::ParseDeclaratorInternal diagnoses the case where |
| 469 | // references are written directly; here, we handle the |
| 470 | // collapsing of references-to-references as described in C++ |
| 471 | // DR 106 and amended by C++ DR 540. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 472 | |
| 473 | // C++ [dcl.ref]p1: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 474 | // A declarator that specifies the type "reference to cv void" |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 475 | // is ill-formed. |
| 476 | if (T->isVoidType()) { |
| 477 | Diag(Loc, diag::err_reference_to_void); |
| 478 | return QualType(); |
| 479 | } |
| 480 | |
| 481 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 482 | // object or incomplete types shall not be restrict-qualified." |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 483 | if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 484 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 485 | << T; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 486 | Quals.removeRestrict(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // C++ [dcl.ref]p1: |
| 490 | // [...] Cv-qualified references are ill-formed except when the |
| 491 | // cv-qualifiers are introduced through the use of a typedef |
| 492 | // (7.1.3) or of a template type argument (14.3), in which case |
| 493 | // the cv-qualifiers are ignored. |
| 494 | // |
| 495 | // We diagnose extraneous cv-qualifiers for the non-typedef, |
| 496 | // non-template type argument case within the parser. Here, we just |
| 497 | // ignore any extraneous cv-qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 498 | Quals.removeConst(); |
| 499 | Quals.removeVolatile(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 500 | |
| 501 | // Handle restrict on references. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 502 | if (LValueRef) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 503 | return Context.getQualifiedType( |
| 504 | Context.getLValueReferenceType(T, SpelledAsLValue), Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 505 | return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /// \brief Build an array type. |
| 509 | /// |
| 510 | /// \param T The type of each element in the array. |
| 511 | /// |
| 512 | /// \param ASM C99 array size modifier (e.g., '*', 'static'). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | /// |
| 514 | /// \param ArraySize Expression describing the size of the array. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 515 | /// |
| 516 | /// \param Quals The cvr-qualifiers to be applied to the array's |
| 517 | /// element type. |
| 518 | /// |
| 519 | /// \param Loc The location of the entity whose type involves this |
| 520 | /// array type or, if there is no such entity, the location of the |
| 521 | /// type that will have array type. |
| 522 | /// |
| 523 | /// \param Entity The name of the entity that involves the array |
| 524 | /// type, if known. |
| 525 | /// |
| 526 | /// \returns A suitable array type, if there are no errors. Otherwise, |
| 527 | /// returns a NULL type. |
| 528 | QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, |
| 529 | Expr *ArraySize, unsigned Quals, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 530 | SourceRange Brackets, DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 531 | |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 532 | SourceLocation Loc = Brackets.getBegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 534 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | if (RequireCompleteType(Loc, T, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 536 | diag::err_illegal_decl_array_incomplete_type)) |
| 537 | return QualType(); |
| 538 | |
| 539 | if (T->isFunctionType()) { |
| 540 | Diag(Loc, diag::err_illegal_decl_array_of_functions) |
| 541 | << getPrintableNameForEntity(Entity); |
| 542 | return QualType(); |
| 543 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 545 | // C++ 8.3.2p4: There shall be no ... arrays of references ... |
| 546 | if (T->isReferenceType()) { |
| 547 | Diag(Loc, diag::err_illegal_decl_array_of_references) |
| 548 | << getPrintableNameForEntity(Entity); |
| 549 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 551 | |
Anders Carlsson | e7cf07d | 2009-06-26 19:33:28 +0000 | [diff] [blame] | 552 | if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | Diag(Loc, diag::err_illegal_decl_array_of_auto) |
Anders Carlsson | e7cf07d | 2009-06-26 19:33:28 +0000 | [diff] [blame] | 554 | << getPrintableNameForEntity(Entity); |
| 555 | return QualType(); |
| 556 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 558 | if (const RecordType *EltTy = T->getAs<RecordType>()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 559 | // If the element type is a struct or union that contains a variadic |
| 560 | // array, accept it as a GNU extension: C99 6.7.2.1p2. |
| 561 | if (EltTy->getDecl()->hasFlexibleArrayMember()) |
| 562 | Diag(Loc, diag::ext_flexible_array_in_array) << T; |
| 563 | } else if (T->isObjCInterfaceType()) { |
Chris Lattner | c7c11b1 | 2009-04-27 01:55:56 +0000 | [diff] [blame] | 564 | Diag(Loc, diag::err_objc_array_of_interfaces) << T; |
| 565 | return QualType(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 566 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 568 | // C99 6.7.5.2p1: The size expression shall have integer type. |
| 569 | if (ArraySize && !ArraySize->isTypeDependent() && |
| 570 | !ArraySize->getType()->isIntegerType()) { |
| 571 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) |
| 572 | << ArraySize->getType() << ArraySize->getSourceRange(); |
| 573 | ArraySize->Destroy(Context); |
| 574 | return QualType(); |
| 575 | } |
| 576 | llvm::APSInt ConstVal(32); |
| 577 | if (!ArraySize) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 578 | if (ASM == ArrayType::Star) |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 579 | T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets); |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 580 | else |
| 581 | T = Context.getIncompleteArrayType(T, ASM, Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 582 | } else if (ArraySize->isValueDependent()) { |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 583 | T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 584 | } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || |
| 585 | (!T->isDependentType() && !T->isConstantSizeType())) { |
| 586 | // Per C99, a variable array is an array with either a non-constant |
| 587 | // size or an element type that has a non-constant-size |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 588 | T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 589 | } else { |
| 590 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 591 | // have a value greater than zero. |
| 592 | if (ConstVal.isSigned()) { |
| 593 | if (ConstVal.isNegative()) { |
| 594 | Diag(ArraySize->getLocStart(), |
| 595 | diag::err_typecheck_negative_array_size) |
| 596 | << ArraySize->getSourceRange(); |
| 597 | return QualType(); |
| 598 | } else if (ConstVal == 0) { |
| 599 | // GCC accepts zero sized static arrays. |
| 600 | Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size) |
| 601 | << ArraySize->getSourceRange(); |
| 602 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 603 | } |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 604 | T = Context.getConstantArrayType(T, ConstVal, ASM, Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 605 | } |
| 606 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
| 607 | if (!getLangOptions().C99) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | if (ArraySize && !ArraySize->isTypeDependent() && |
| 609 | !ArraySize->isValueDependent() && |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 610 | !ArraySize->isIntegerConstantExpr(Context)) |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 611 | Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 612 | else if (ASM != ArrayType::Normal || Quals != 0) |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 613 | Diag(Loc, |
| 614 | getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx |
| 615 | : diag::ext_c99_array_usage); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | return T; |
| 619 | } |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 620 | |
| 621 | /// \brief Build an ext-vector type. |
| 622 | /// |
| 623 | /// Run the required checks for the extended vector type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 625 | SourceLocation AttrLoc) { |
| 626 | |
| 627 | Expr *Arg = (Expr *)ArraySize.get(); |
| 628 | |
| 629 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 630 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | if (!T->isDependentType() && |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 632 | !T->isIntegerType() && !T->isRealFloatingType()) { |
| 633 | Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T; |
| 634 | return QualType(); |
| 635 | } |
| 636 | |
| 637 | if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { |
| 638 | llvm::APSInt vecSize(32); |
| 639 | if (!Arg->isIntegerConstantExpr(vecSize, Context)) { |
| 640 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
| 641 | << "ext_vector_type" << Arg->getSourceRange(); |
| 642 | return QualType(); |
| 643 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | |
| 645 | // unlike gcc's vector_size attribute, the size is specified as the |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 646 | // number of elements, not the number of bytes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 647 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 648 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 649 | if (vectorSize == 0) { |
| 650 | Diag(AttrLoc, diag::err_attribute_zero_size) |
| 651 | << Arg->getSourceRange(); |
| 652 | return QualType(); |
| 653 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 655 | if (!T->isDependentType()) |
| 656 | return Context.getExtVectorType(T, vectorSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(), |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 660 | AttrLoc); |
| 661 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 663 | /// \brief Build a function type. |
| 664 | /// |
| 665 | /// This routine checks the function type according to C++ rules and |
| 666 | /// under the assumption that the result type and parameter types have |
| 667 | /// just been instantiated from a template. It therefore duplicates |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 668 | /// some of the behavior of GetTypeForDeclarator, but in a much |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 669 | /// simpler form that is only suitable for this narrow use case. |
| 670 | /// |
| 671 | /// \param T The return type of the function. |
| 672 | /// |
| 673 | /// \param ParamTypes The parameter types of the function. This array |
| 674 | /// will be modified to account for adjustments to the types of the |
| 675 | /// function parameters. |
| 676 | /// |
| 677 | /// \param NumParamTypes The number of parameter types in ParamTypes. |
| 678 | /// |
| 679 | /// \param Variadic Whether this is a variadic function type. |
| 680 | /// |
| 681 | /// \param Quals The cvr-qualifiers to be applied to the function type. |
| 682 | /// |
| 683 | /// \param Loc The location of the entity whose type involves this |
| 684 | /// function type or, if there is no such entity, the location of the |
| 685 | /// type that will have function type. |
| 686 | /// |
| 687 | /// \param Entity The name of the entity that involves the function |
| 688 | /// type, if known. |
| 689 | /// |
| 690 | /// \returns A suitable function type, if there are no |
| 691 | /// errors. Otherwise, returns a NULL type. |
| 692 | QualType Sema::BuildFunctionType(QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | QualType *ParamTypes, |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 694 | unsigned NumParamTypes, |
| 695 | bool Variadic, unsigned Quals, |
| 696 | SourceLocation Loc, DeclarationName Entity) { |
| 697 | if (T->isArrayType() || T->isFunctionType()) { |
| 698 | Diag(Loc, diag::err_func_returning_array_function) << T; |
| 699 | return QualType(); |
| 700 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 702 | bool Invalid = false; |
| 703 | for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) { |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 704 | QualType ParamType = adjustParameterType(ParamTypes[Idx]); |
| 705 | if (ParamType->isVoidType()) { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 706 | Diag(Loc, diag::err_param_with_void_type); |
| 707 | Invalid = true; |
| 708 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 709 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 710 | ParamTypes[Idx] = ParamType; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | if (Invalid) |
| 714 | return QualType(); |
| 715 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic, |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 717 | Quals); |
| 718 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 719 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 720 | /// \brief Build a member pointer type \c T Class::*. |
| 721 | /// |
| 722 | /// \param T the type to which the member pointer refers. |
| 723 | /// \param Class the class type into which the member pointer points. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 724 | /// \param CVR Qualifiers applied to the member pointer type |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 725 | /// \param Loc the location where this type begins |
| 726 | /// \param Entity the name of the entity that will have this member pointer type |
| 727 | /// |
| 728 | /// \returns a member pointer type, if successful, or a NULL type if there was |
| 729 | /// an error. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | QualType Sema::BuildMemberPointerType(QualType T, QualType Class, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 731 | unsigned CVR, SourceLocation Loc, |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 732 | DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 733 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
| 734 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 735 | // Verify that we're not building a pointer to pointer to function with |
| 736 | // exception specification. |
| 737 | if (CheckDistantExceptionSpec(T)) { |
| 738 | Diag(Loc, diag::err_distant_exception_spec); |
| 739 | |
| 740 | // FIXME: If we're doing this as part of template instantiation, |
| 741 | // we should return immediately. |
| 742 | |
| 743 | // Build the type anyway, but use the canonical type so that the |
| 744 | // exception specifiers are stripped off. |
| 745 | T = Context.getCanonicalType(T); |
| 746 | } |
| 747 | |
| 748 | // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member |
| 749 | // with reference type, or "cv void." |
| 750 | if (T->isReferenceType()) { |
Anders Carlsson | 8d4655d | 2009-06-30 00:06:57 +0000 | [diff] [blame] | 751 | Diag(Loc, diag::err_illegal_decl_mempointer_to_reference) |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 752 | << (Entity? Entity.getAsString() : "type name"); |
| 753 | return QualType(); |
| 754 | } |
| 755 | |
| 756 | if (T->isVoidType()) { |
| 757 | Diag(Loc, diag::err_illegal_decl_mempointer_to_void) |
| 758 | << (Entity? Entity.getAsString() : "type name"); |
| 759 | return QualType(); |
| 760 | } |
| 761 | |
| 762 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 763 | // object or incomplete types shall not be restrict-qualified." |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 764 | if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) { |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 765 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 766 | << T; |
| 767 | |
| 768 | // FIXME: If we're doing this as part of template instantiation, |
| 769 | // we should return immediately. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 770 | Quals.removeRestrict(); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | if (!Class->isDependentType() && !Class->isRecordType()) { |
| 774 | Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class; |
| 775 | return QualType(); |
| 776 | } |
| 777 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 778 | return Context.getQualifiedType( |
| 779 | Context.getMemberPointerType(T, Class.getTypePtr()), Quals); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 780 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 782 | /// \brief Build a block pointer type. |
| 783 | /// |
| 784 | /// \param T The type to which we'll be building a block pointer. |
| 785 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 786 | /// \param CVR The cvr-qualifiers to be applied to the block pointer type. |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 787 | /// |
| 788 | /// \param Loc The location of the entity whose type involves this |
| 789 | /// block pointer type or, if there is no such entity, the location of the |
| 790 | /// type that will have block pointer type. |
| 791 | /// |
| 792 | /// \param Entity The name of the entity that involves the block pointer |
| 793 | /// type, if known. |
| 794 | /// |
| 795 | /// \returns A suitable block pointer type, if there are no |
| 796 | /// errors. Otherwise, returns a NULL type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 797 | QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 798 | SourceLocation Loc, |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 799 | DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 800 | if (!T->isFunctionType()) { |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 801 | Diag(Loc, diag::err_nonfunction_block_type); |
| 802 | return QualType(); |
| 803 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 804 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 805 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
| 806 | return Context.getQualifiedType(Context.getBlockPointerType(T), Quals); |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 809 | QualType Sema::GetTypeFromParser(TypeTy *Ty, DeclaratorInfo **DInfo) { |
| 810 | QualType QT = QualType::getFromOpaquePtr(Ty); |
| 811 | DeclaratorInfo *DI = 0; |
| 812 | if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) { |
| 813 | QT = LIT->getType(); |
| 814 | DI = LIT->getDeclaratorInfo(); |
| 815 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 817 | if (DInfo) *DInfo = DI; |
| 818 | return QT; |
| 819 | } |
| 820 | |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 821 | |
| 822 | /// isOmittedBlockReturnType - Return true if this declarator is missing a |
| 823 | /// return type because this is a omitted return type on a block literal. |
| 824 | static bool isOmittedBlockReturnType(const Declarator &D, unsigned Skip) { |
| 825 | if (D.getContext() != Declarator::BlockLiteralContext || |
| 826 | Skip != 0 || D.getDeclSpec().hasTypeSpecifier()) |
| 827 | return false; |
| 828 | |
| 829 | if (D.getNumTypeObjects() == 0) |
| 830 | return true; |
| 831 | |
| 832 | if (D.getNumTypeObjects() == 1 && |
| 833 | D.getTypeObject(0).Kind == DeclaratorChunk::Function) |
| 834 | return true; |
| 835 | |
| 836 | return false; |
| 837 | } |
| 838 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 839 | /// GetTypeForDeclarator - Convert the type for the specified |
| 840 | /// declarator to Type instances. Skip the outermost Skip type |
| 841 | /// objects. |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 842 | /// |
| 843 | /// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq |
| 844 | /// owns the declaration of a type (e.g., the definition of a struct |
| 845 | /// type), then *OwnedDecl will receive the owned declaration. |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 846 | QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, |
| 847 | DeclaratorInfo **DInfo, unsigned Skip, |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 848 | TagDecl **OwnedDecl) { |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 849 | // long long is a C99 feature. |
Chris Lattner | d1eb332 | 2007-08-28 16:41:29 +0000 | [diff] [blame] | 850 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 851 | D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) |
| 852 | Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 853 | |
| 854 | // Determine the type of the declarator. Not all forms of declarator |
| 855 | // have a type. |
| 856 | QualType T; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 857 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 858 | switch (D.getKind()) { |
| 859 | case Declarator::DK_Abstract: |
| 860 | case Declarator::DK_Normal: |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 861 | case Declarator::DK_Operator: |
| 862 | case Declarator::DK_TemplateId: { |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 863 | const DeclSpec &DS = D.getDeclSpec(); |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 864 | if (isOmittedBlockReturnType(D, Skip)) { |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 865 | // We default to a dependent type initially. Can be modified by |
| 866 | // the first return statement. |
| 867 | T = Context.DependentTy; |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 868 | } else { |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 869 | bool isInvalid = false; |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame^] | 870 | T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid, *this); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 871 | if (isInvalid) |
| 872 | D.setInvalidType(true); |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 873 | else if (OwnedDecl && DS.isTypeSpecOwned()) |
| 874 | *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep()); |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 875 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 876 | break; |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 877 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 878 | |
| 879 | case Declarator::DK_Constructor: |
| 880 | case Declarator::DK_Destructor: |
| 881 | case Declarator::DK_Conversion: |
| 882 | // Constructors and destructors don't have return types. Use |
| 883 | // "void" instead. Conversion operators will check their return |
| 884 | // types separately. |
| 885 | T = Context.VoidTy; |
| 886 | break; |
| 887 | } |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 888 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 889 | if (T == Context.UndeducedAutoTy) { |
| 890 | int Error = -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 892 | switch (D.getContext()) { |
| 893 | case Declarator::KNRTypeListContext: |
| 894 | assert(0 && "K&R type lists aren't allowed in C++"); |
| 895 | break; |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 896 | case Declarator::PrototypeContext: |
| 897 | Error = 0; // Function prototype |
| 898 | break; |
| 899 | case Declarator::MemberContext: |
| 900 | switch (cast<TagDecl>(CurContext)->getTagKind()) { |
| 901 | case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break; |
| 902 | case TagDecl::TK_struct: Error = 1; /* Struct member */ break; |
| 903 | case TagDecl::TK_union: Error = 2; /* Union member */ break; |
| 904 | case TagDecl::TK_class: Error = 3; /* Class member */ break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | } |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 906 | break; |
| 907 | case Declarator::CXXCatchContext: |
| 908 | Error = 4; // Exception declaration |
| 909 | break; |
| 910 | case Declarator::TemplateParamContext: |
| 911 | Error = 5; // Template parameter |
| 912 | break; |
| 913 | case Declarator::BlockLiteralContext: |
| 914 | Error = 6; // Block literal |
| 915 | break; |
| 916 | case Declarator::FileContext: |
| 917 | case Declarator::BlockContext: |
| 918 | case Declarator::ForContext: |
| 919 | case Declarator::ConditionContext: |
| 920 | case Declarator::TypeNameContext: |
| 921 | break; |
| 922 | } |
| 923 | |
| 924 | if (Error != -1) { |
| 925 | Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed) |
| 926 | << Error; |
| 927 | T = Context.IntTy; |
| 928 | D.setInvalidType(true); |
| 929 | } |
| 930 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 932 | // The name we're declaring, if any. |
| 933 | DeclarationName Name; |
| 934 | if (D.getIdentifier()) |
| 935 | Name = D.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 937 | // Walk the DeclTypeInfo, building the recursive type as we go. |
| 938 | // DeclTypeInfos are ordered from the identifier out, which is |
| 939 | // opposite of what we want :). |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 940 | for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) { |
| 941 | DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 942 | switch (DeclType.Kind) { |
| 943 | default: assert(0 && "Unknown decltype!"); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 944 | case DeclaratorChunk::BlockPointer: |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 945 | // If blocks are disabled, emit an error. |
| 946 | if (!LangOpts.Blocks) |
| 947 | Diag(DeclType.Loc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | |
| 949 | T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(), |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 950 | Name); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 951 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 952 | case DeclaratorChunk::Pointer: |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 953 | // Verify that we're not building a pointer to pointer to function with |
| 954 | // exception specification. |
| 955 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 956 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 957 | D.setInvalidType(true); |
| 958 | // Build the type anyway. |
| 959 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 960 | if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 961 | const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 962 | T = Context.getObjCObjectPointerType(T, |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 963 | (ObjCProtocolDecl **)OIT->qual_begin(), |
| 964 | OIT->getNumProtocols()); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 965 | break; |
| 966 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 967 | T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 968 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 969 | case DeclaratorChunk::Reference: { |
| 970 | Qualifiers Quals; |
| 971 | if (DeclType.Ref.HasRestrict) Quals.addRestrict(); |
| 972 | |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 973 | // Verify that we're not building a reference to pointer to function with |
| 974 | // exception specification. |
| 975 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 976 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 977 | D.setInvalidType(true); |
| 978 | // Build the type anyway. |
| 979 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 980 | T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 981 | DeclType.Loc, Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 982 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 983 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 984 | case DeclaratorChunk::Array: { |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 985 | // Verify that we're not building an array of pointers to function with |
| 986 | // exception specification. |
| 987 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 988 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 989 | D.setInvalidType(true); |
| 990 | // Build the type anyway. |
| 991 | } |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 992 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 993 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 994 | ArrayType::ArraySizeModifier ASM; |
| 995 | if (ATI.isStar) |
| 996 | ASM = ArrayType::Star; |
| 997 | else if (ATI.hasStatic) |
| 998 | ASM = ArrayType::Static; |
| 999 | else |
| 1000 | ASM = ArrayType::Normal; |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 1001 | if (ASM == ArrayType::Star && |
| 1002 | D.getContext() != Declarator::PrototypeContext) { |
| 1003 | // FIXME: This check isn't quite right: it allows star in prototypes |
| 1004 | // for function definitions, and disallows some edge cases detailed |
| 1005 | // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html |
| 1006 | Diag(DeclType.Loc, diag::err_array_star_outside_prototype); |
| 1007 | ASM = ArrayType::Normal; |
| 1008 | D.setInvalidType(true); |
| 1009 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1010 | T = BuildArrayType(T, ASM, ArraySize, |
| 1011 | Qualifiers::fromCVRMask(ATI.TypeQuals), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1012 | SourceRange(DeclType.Loc, DeclType.EndLoc), Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1013 | break; |
| 1014 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1015 | case DeclaratorChunk::Function: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1016 | // If the function declarator has a prototype (i.e. it is not () and |
| 1017 | // does not have a K&R-style identifier list), then the arguments are part |
| 1018 | // of the type, otherwise the argument list is (). |
| 1019 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1020 | |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 1021 | // 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] | 1022 | if (T->isArrayType() || T->isFunctionType()) { |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1023 | Diag(DeclType.Loc, diag::err_func_returning_array_function) << T; |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 1024 | T = Context.IntTy; |
| 1025 | D.setInvalidType(true); |
| 1026 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1027 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1028 | if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) { |
| 1029 | // C++ [dcl.fct]p6: |
| 1030 | // Types shall not be defined in return or parameter types. |
| 1031 | TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep()); |
| 1032 | if (Tag->isDefinition()) |
| 1033 | Diag(Tag->getLocation(), diag::err_type_defined_in_result_type) |
| 1034 | << Context.getTypeDeclType(Tag); |
| 1035 | } |
| 1036 | |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1037 | // Exception specs are not allowed in typedefs. Complain, but add it |
| 1038 | // anyway. |
| 1039 | if (FTI.hasExceptionSpec && |
| 1040 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
| 1041 | Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef); |
| 1042 | |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1043 | if (FTI.NumArgs == 0) { |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 1044 | if (getLangOptions().CPlusPlus) { |
| 1045 | // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the |
| 1046 | // function takes no arguments. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1047 | llvm::SmallVector<QualType, 4> Exceptions; |
| 1048 | Exceptions.reserve(FTI.NumExceptions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1049 | for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1050 | // FIXME: Preserve type source info. |
| 1051 | QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1052 | // Check that the type is valid for an exception spec, and drop it |
| 1053 | // if not. |
| 1054 | if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) |
| 1055 | Exceptions.push_back(ET); |
| 1056 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1057 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals, |
| 1058 | FTI.hasExceptionSpec, |
| 1059 | FTI.hasAnyExceptionSpec, |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1060 | Exceptions.size(), Exceptions.data()); |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 1061 | } else if (FTI.isVariadic) { |
| 1062 | // We allow a zero-parameter variadic function in C if the |
| 1063 | // function is marked with the "overloadable" |
| 1064 | // attribute. Scan for this attribute now. |
| 1065 | bool Overloadable = false; |
| 1066 | for (const AttributeList *Attrs = D.getAttributes(); |
| 1067 | Attrs; Attrs = Attrs->getNext()) { |
| 1068 | if (Attrs->getKind() == AttributeList::AT_overloadable) { |
| 1069 | Overloadable = true; |
| 1070 | break; |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | if (!Overloadable) |
| 1075 | Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg); |
| 1076 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 1077 | } else { |
| 1078 | // Simple void foo(), where the incoming T is the result type. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1079 | T = Context.getFunctionNoProtoType(T); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 1080 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1081 | } else if (FTI.ArgInfo[0].Param == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1082 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1084 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1085 | } else { |
| 1086 | // Otherwise, we have a function with an argument list that is |
| 1087 | // potentially variadic. |
| 1088 | llvm::SmallVector<QualType, 16> ArgTys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1090 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1091 | ParmVarDecl *Param = |
| 1092 | cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>()); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1093 | QualType ArgTy = Param->getType(); |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 1094 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1095 | |
| 1096 | // Adjust the parameter type. |
Douglas Gregor | beb58cb | 2009-03-23 23:17:00 +0000 | [diff] [blame] | 1097 | assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1098 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1099 | // Look for 'void'. void is allowed only as a single argument to a |
| 1100 | // function with no other parameters (C99 6.7.5.3p10). We record |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1101 | // int(void) as a FunctionProtoType with an empty argument list. |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1102 | if (ArgTy->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1103 | // If this is something like 'float(int, void)', reject it. 'void' |
| 1104 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 1105 | // have arguments of incomplete type. |
| 1106 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 1107 | Diag(DeclType.Loc, diag::err_void_only_param); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1108 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1109 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1110 | } else if (FTI.ArgInfo[i].Ident) { |
| 1111 | // Reject, but continue to parse 'int(void abc)'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1112 | Diag(FTI.ArgInfo[i].IdentLoc, |
Chris Lattner | 4565d4e | 2007-07-21 05:26:43 +0000 | [diff] [blame] | 1113 | diag::err_param_with_void_type); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1114 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1115 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1116 | } else { |
| 1117 | // Reject, but continue to parse 'float(const void)'. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1118 | if (ArgTy.hasQualifiers()) |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1119 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1121 | // Do not add 'void' to the ArgTys list. |
| 1122 | break; |
| 1123 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1124 | } else if (!FTI.hasPrototype) { |
| 1125 | if (ArgTy->isPromotableIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 1126 | ArgTy = Context.getPromotedIntegerType(ArgTy); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1127 | } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) { |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1128 | if (BTy->getKind() == BuiltinType::Float) |
| 1129 | ArgTy = Context.DoubleTy; |
| 1130 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1131 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1133 | ArgTys.push_back(ArgTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1134 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1135 | |
| 1136 | llvm::SmallVector<QualType, 4> Exceptions; |
| 1137 | Exceptions.reserve(FTI.NumExceptions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1139 | // FIXME: Preserve type source info. |
| 1140 | QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1141 | // Check that the type is valid for an exception spec, and drop it if |
| 1142 | // not. |
| 1143 | if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) |
| 1144 | Exceptions.push_back(ET); |
| 1145 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1146 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1147 | T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1148 | FTI.isVariadic, FTI.TypeQuals, |
| 1149 | FTI.hasExceptionSpec, |
| 1150 | FTI.hasAnyExceptionSpec, |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1151 | Exceptions.size(), Exceptions.data()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1152 | } |
| 1153 | break; |
| 1154 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1155 | case DeclaratorChunk::MemberPointer: |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 1156 | // Verify that we're not building a pointer to pointer to function with |
| 1157 | // exception specification. |
| 1158 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1159 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1160 | D.setInvalidType(true); |
| 1161 | // Build the type anyway. |
| 1162 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1163 | // The scope spec must refer to a class, or be dependent. |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1164 | QualType ClsType; |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1165 | if (isDependentScopeSpecifier(DeclType.Mem.Scope())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | NestedNameSpecifier *NNS |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1167 | = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep(); |
| 1168 | assert(NNS->getAsType() && "Nested-name-specifier must name a type"); |
| 1169 | ClsType = QualType(NNS->getAsType(), 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | } else if (CXXRecordDecl *RD |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1171 | = dyn_cast_or_null<CXXRecordDecl>( |
| 1172 | computeDeclContext(DeclType.Mem.Scope()))) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1173 | ClsType = Context.getTagDeclType(RD); |
| 1174 | } else { |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1175 | Diag(DeclType.Mem.Scope().getBeginLoc(), |
| 1176 | diag::err_illegal_decl_mempointer_in_nonclass) |
| 1177 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") |
| 1178 | << DeclType.Mem.Scope().getRange(); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1179 | D.setInvalidType(true); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1182 | if (!ClsType.isNull()) |
| 1183 | T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals, |
| 1184 | DeclType.Loc, D.getIdentifier()); |
| 1185 | if (T.isNull()) { |
| 1186 | T = Context.IntTy; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1187 | D.setInvalidType(true); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1188 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1189 | break; |
| 1190 | } |
| 1191 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1192 | if (T.isNull()) { |
| 1193 | D.setInvalidType(true); |
| 1194 | T = Context.IntTy; |
| 1195 | } |
| 1196 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1197 | // See if there are any attributes on this declarator chunk. |
| 1198 | if (const AttributeList *AL = DeclType.getAttrs()) |
| 1199 | ProcessTypeAttributeList(T, AL); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1200 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1201 | |
| 1202 | if (getLangOptions().CPlusPlus && T->isFunctionType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1203 | const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>(); |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 1204 | assert(FnTy && "Why oh why is there not a FunctionProtoType here?"); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1205 | |
| 1206 | // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type |
| 1207 | // for a nonstatic member function, the function type to which a pointer |
| 1208 | // to member refers, or the top-level function type of a function typedef |
| 1209 | // declaration. |
| 1210 | if (FnTy->getTypeQuals() != 0 && |
| 1211 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1212 | ((D.getContext() != Declarator::MemberContext && |
| 1213 | (!D.getCXXScopeSpec().isSet() || |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1214 | !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true) |
| 1215 | ->isRecord())) || |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1216 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1217 | if (D.isFunctionDeclarator()) |
| 1218 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type); |
| 1219 | else |
| 1220 | Diag(D.getIdentifierLoc(), |
| 1221 | diag::err_invalid_qualified_typedef_function_type_use); |
| 1222 | |
| 1223 | // Strip the cv-quals from the type. |
| 1224 | T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1225 | FnTy->getNumArgs(), FnTy->isVariadic(), 0); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1226 | } |
| 1227 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | 0bf29ad | 2008-06-29 00:19:33 +0000 | [diff] [blame] | 1229 | // If there were any type attributes applied to the decl itself (not the |
| 1230 | // type, apply the type attribute to the type!) |
| 1231 | if (const AttributeList *Attrs = D.getAttributes()) |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1232 | ProcessTypeAttributeList(T, Attrs); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1233 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1234 | if (DInfo) { |
| 1235 | if (D.isInvalidType()) |
| 1236 | *DInfo = 0; |
| 1237 | else |
| 1238 | *DInfo = GetDeclaratorInfoForDeclarator(D, T, Skip); |
| 1239 | } |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1240 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1241 | return T; |
| 1242 | } |
| 1243 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1244 | namespace { |
| 1245 | class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> { |
| 1246 | const DeclSpec &DS; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1247 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1248 | public: |
| 1249 | TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {} |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1250 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1251 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1252 | Visit(TL.getUnqualifiedLoc()); |
| 1253 | } |
| 1254 | void VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 1255 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 1256 | } |
| 1257 | void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 1258 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
Argyrios Kyrtzidis | eb66759 | 2009-09-29 19:45:22 +0000 | [diff] [blame] | 1259 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1260 | if (DS.getProtocolQualifiers()) { |
| 1261 | assert(TL.getNumProtocols() > 0); |
| 1262 | assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); |
| 1263 | TL.setLAngleLoc(DS.getProtocolLAngleLoc()); |
| 1264 | TL.setRAngleLoc(DS.getSourceRange().getEnd()); |
| 1265 | for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i) |
| 1266 | TL.setProtocolLoc(i, DS.getProtocolLocs()[i]); |
| 1267 | } else { |
| 1268 | assert(TL.getNumProtocols() == 0); |
| 1269 | TL.setLAngleLoc(SourceLocation()); |
| 1270 | TL.setRAngleLoc(SourceLocation()); |
| 1271 | } |
| 1272 | } |
| 1273 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 1274 | assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); |
| 1275 | |
| 1276 | TL.setStarLoc(SourceLocation()); |
| 1277 | |
| 1278 | if (DS.getProtocolQualifiers()) { |
| 1279 | assert(TL.getNumProtocols() > 0); |
| 1280 | assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); |
| 1281 | TL.setHasProtocolsAsWritten(true); |
| 1282 | TL.setLAngleLoc(DS.getProtocolLAngleLoc()); |
| 1283 | TL.setRAngleLoc(DS.getSourceRange().getEnd()); |
| 1284 | for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i) |
| 1285 | TL.setProtocolLoc(i, DS.getProtocolLocs()[i]); |
| 1286 | |
| 1287 | } else { |
| 1288 | assert(TL.getNumProtocols() == 0); |
| 1289 | TL.setHasProtocolsAsWritten(false); |
| 1290 | TL.setLAngleLoc(SourceLocation()); |
| 1291 | TL.setRAngleLoc(SourceLocation()); |
| 1292 | } |
| 1293 | |
| 1294 | // This might not have been written with an inner type. |
| 1295 | if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { |
| 1296 | TL.setHasBaseTypeAsWritten(false); |
| 1297 | TL.getBaseTypeLoc().initialize(SourceLocation()); |
| 1298 | } else { |
| 1299 | TL.setHasBaseTypeAsWritten(true); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1300 | Visit(TL.getBaseTypeLoc()); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1301 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1302 | } |
| 1303 | void VisitTypeLoc(TypeLoc TL) { |
| 1304 | // FIXME: add other typespec types and change this to an assert. |
| 1305 | TL.initialize(DS.getTypeSpecTypeLoc()); |
| 1306 | } |
| 1307 | }; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1308 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1309 | class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> { |
| 1310 | const DeclaratorChunk &Chunk; |
| 1311 | |
| 1312 | public: |
| 1313 | DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {} |
| 1314 | |
| 1315 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1316 | llvm::llvm_unreachable("qualified type locs not expected here!"); |
| 1317 | } |
| 1318 | |
| 1319 | void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1320 | assert(Chunk.Kind == DeclaratorChunk::BlockPointer); |
| 1321 | TL.setCaretLoc(Chunk.Loc); |
| 1322 | } |
| 1323 | void VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1324 | assert(Chunk.Kind == DeclaratorChunk::Pointer); |
| 1325 | TL.setStarLoc(Chunk.Loc); |
| 1326 | } |
| 1327 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 1328 | assert(Chunk.Kind == DeclaratorChunk::Pointer); |
| 1329 | TL.setStarLoc(Chunk.Loc); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1330 | TL.setHasBaseTypeAsWritten(true); |
| 1331 | TL.setHasProtocolsAsWritten(false); |
| 1332 | TL.setLAngleLoc(SourceLocation()); |
| 1333 | TL.setRAngleLoc(SourceLocation()); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1334 | } |
| 1335 | void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1336 | assert(Chunk.Kind == DeclaratorChunk::MemberPointer); |
| 1337 | TL.setStarLoc(Chunk.Loc); |
| 1338 | // FIXME: nested name specifier |
| 1339 | } |
| 1340 | void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 1341 | assert(Chunk.Kind == DeclaratorChunk::Reference); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1342 | // 'Amp' is misleading: this might have been originally |
| 1343 | /// spelled with AmpAmp. |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1344 | TL.setAmpLoc(Chunk.Loc); |
| 1345 | } |
| 1346 | void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 1347 | assert(Chunk.Kind == DeclaratorChunk::Reference); |
| 1348 | assert(!Chunk.Ref.LValueRef); |
| 1349 | TL.setAmpAmpLoc(Chunk.Loc); |
| 1350 | } |
| 1351 | void VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1352 | assert(Chunk.Kind == DeclaratorChunk::Array); |
| 1353 | TL.setLBracketLoc(Chunk.Loc); |
| 1354 | TL.setRBracketLoc(Chunk.EndLoc); |
| 1355 | TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts)); |
| 1356 | } |
| 1357 | void VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
| 1358 | assert(Chunk.Kind == DeclaratorChunk::Function); |
| 1359 | TL.setLParenLoc(Chunk.Loc); |
| 1360 | TL.setRParenLoc(Chunk.EndLoc); |
| 1361 | |
| 1362 | const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1363 | for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1364 | ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1365 | TL.setArg(tpi++, Param); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1366 | } |
| 1367 | // FIXME: exception specs |
| 1368 | } |
| 1369 | |
| 1370 | void VisitTypeLoc(TypeLoc TL) { |
| 1371 | llvm::llvm_unreachable("unsupported TypeLoc kind in declarator!"); |
| 1372 | } |
| 1373 | }; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1376 | /// \brief Create and instantiate a DeclaratorInfo with type source information. |
| 1377 | /// |
| 1378 | /// \param T QualType referring to the type as written in source code. |
| 1379 | DeclaratorInfo * |
| 1380 | Sema::GetDeclaratorInfoForDeclarator(Declarator &D, QualType T, unsigned Skip) { |
| 1381 | DeclaratorInfo *DInfo = Context.CreateDeclaratorInfo(T); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1382 | UnqualTypeLoc CurrTL = DInfo->getTypeLoc().getUnqualifiedLoc(); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1383 | |
| 1384 | for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1385 | DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL); |
| 1386 | CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1387 | } |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1388 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1389 | TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1390 | |
| 1391 | return DInfo; |
| 1392 | } |
| 1393 | |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1394 | /// \brief Create a LocInfoType to hold the given QualType and DeclaratorInfo. |
| 1395 | QualType Sema::CreateLocInfoType(QualType T, DeclaratorInfo *DInfo) { |
| 1396 | // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser |
| 1397 | // and Sema during declaration parsing. Try deallocating/caching them when |
| 1398 | // it's appropriate, instead of allocating them and keeping them around. |
| 1399 | LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8); |
| 1400 | new (LocT) LocInfoType(T, DInfo); |
| 1401 | assert(LocT->getTypeClass() != T->getTypeClass() && |
| 1402 | "LocInfoType's TypeClass conflicts with an existing Type class"); |
| 1403 | return QualType(LocT, 0); |
| 1404 | } |
| 1405 | |
| 1406 | void LocInfoType::getAsStringInternal(std::string &Str, |
| 1407 | const PrintingPolicy &Policy) const { |
Argyrios Kyrtzidis | 35d44e5 | 2009-08-19 01:46:06 +0000 | [diff] [blame] | 1408 | assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*" |
| 1409 | " was used directly instead of getting the QualType through" |
| 1410 | " GetTypeFromParser"); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1413 | /// ObjCGetTypeForMethodDefinition - Builds the type for a method definition |
Fariborz Jahanian | 360300c | 2007-11-09 22:27:59 +0000 | [diff] [blame] | 1414 | /// declarator |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1415 | QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) { |
| 1416 | ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>()); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1417 | QualType T = MDecl->getResultType(); |
| 1418 | llvm::SmallVector<QualType, 16> ArgTys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 1420 | // Add the first two invisible argument types for self and _cmd. |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1421 | if (MDecl->isInstanceMethod()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1422 | QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 1423 | selfTy = Context.getPointerType(selfTy); |
| 1424 | ArgTys.push_back(selfTy); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1425 | } else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1426 | ArgTys.push_back(Context.getObjCIdType()); |
| 1427 | ArgTys.push_back(Context.getObjCSelType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1429 | for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), |
| 1430 | E = MDecl->param_end(); PI != E; ++PI) { |
| 1431 | QualType ArgTy = (*PI)->getType(); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1432 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Douglas Gregor | beb58cb | 2009-03-23 23:17:00 +0000 | [diff] [blame] | 1433 | ArgTy = adjustParameterType(ArgTy); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1434 | ArgTys.push_back(ArgTy); |
| 1435 | } |
| 1436 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1437 | MDecl->isVariadic(), 0); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1438 | return T; |
| 1439 | } |
| 1440 | |
Sebastian Redl | 9e5e4aa | 2009-01-26 19:54:48 +0000 | [diff] [blame] | 1441 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 1442 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 1443 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 1444 | /// or pointer-to-member types, or if they are not similar at this |
| 1445 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 1446 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 1447 | /// be called in a loop that successively "unwraps" pointer and |
| 1448 | /// pointer-to-member types to compare them at each level. |
Chris Lattner | ecb81f2 | 2009-02-16 21:43:00 +0000 | [diff] [blame] | 1449 | bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1450 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 1451 | *T2PtrType = T2->getAs<PointerType>(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1452 | if (T1PtrType && T2PtrType) { |
| 1453 | T1 = T1PtrType->getPointeeType(); |
| 1454 | T2 = T2PtrType->getPointeeType(); |
| 1455 | return true; |
| 1456 | } |
| 1457 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1458 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 1459 | *T2MPType = T2->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1460 | if (T1MPType && T2MPType && |
| 1461 | Context.getCanonicalType(T1MPType->getClass()) == |
| 1462 | Context.getCanonicalType(T2MPType->getClass())) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1463 | T1 = T1MPType->getPointeeType(); |
| 1464 | T2 = T2MPType->getPointeeType(); |
| 1465 | return true; |
| 1466 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1467 | return false; |
| 1468 | } |
| 1469 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1470 | Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1471 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 1472 | // the parser. |
| 1473 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1475 | DeclaratorInfo *DInfo = 0; |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1476 | TagDecl *OwnedTag = 0; |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1477 | QualType T = GetTypeForDeclarator(D, S, &DInfo, /*Skip=*/0, &OwnedTag); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 1478 | if (D.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1479 | return true; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1480 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1481 | if (getLangOptions().CPlusPlus) { |
| 1482 | // Check that there are no default arguments (C++ only). |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 1483 | CheckExtraCXXDefaultArguments(D); |
| 1484 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1485 | // C++0x [dcl.type]p3: |
| 1486 | // A type-specifier-seq shall not define a class or enumeration |
| 1487 | // unless it appears in the type-id of an alias-declaration |
| 1488 | // (7.1.3). |
| 1489 | if (OwnedTag && OwnedTag->isDefinition()) |
| 1490 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier) |
| 1491 | << Context.getTypeDeclType(OwnedTag); |
| 1492 | } |
| 1493 | |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1494 | if (DInfo) |
| 1495 | T = CreateLocInfoType(T, DInfo); |
| 1496 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1497 | return T.getAsOpaquePtr(); |
| 1498 | } |
| 1499 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1500 | |
| 1501 | |
| 1502 | //===----------------------------------------------------------------------===// |
| 1503 | // Type Attribute Processing |
| 1504 | //===----------------------------------------------------------------------===// |
| 1505 | |
| 1506 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 1507 | /// specified type. The attribute contains 1 argument, the id of the address |
| 1508 | /// space for the type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | static void HandleAddressSpaceTypeAttribute(QualType &Type, |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1510 | const AttributeList &Attr, Sema &S){ |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1511 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1512 | // If this type is already address space qualified, reject it. |
| 1513 | // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers |
| 1514 | // for two or more different address spaces." |
| 1515 | if (Type.getAddressSpace()) { |
| 1516 | S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); |
| 1517 | return; |
| 1518 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1520 | // Check the attribute arguments. |
| 1521 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 1522 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1523 | return; |
| 1524 | } |
| 1525 | Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1526 | llvm::APSInt addrSpace(32); |
| 1527 | if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1528 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) |
| 1529 | << ASArgExpr->getSourceRange(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1530 | return; |
| 1531 | } |
| 1532 | |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1533 | // Bounds checking. |
| 1534 | if (addrSpace.isSigned()) { |
| 1535 | if (addrSpace.isNegative()) { |
| 1536 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative) |
| 1537 | << ASArgExpr->getSourceRange(); |
| 1538 | return; |
| 1539 | } |
| 1540 | addrSpace.setIsSigned(false); |
| 1541 | } |
| 1542 | llvm::APSInt max(addrSpace.getBitWidth()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1543 | max = Qualifiers::MaxAddressSpace; |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1544 | if (addrSpace > max) { |
| 1545 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1546 | << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange(); |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1547 | return; |
| 1548 | } |
| 1549 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1551 | Type = S.Context.getAddrSpaceQualType(Type, ASIdx); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1554 | /// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the |
| 1555 | /// specified type. The attribute contains 1 argument, weak or strong. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | static void HandleObjCGCTypeAttribute(QualType &Type, |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1557 | const AttributeList &Attr, Sema &S) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1558 | if (Type.getObjCGCAttr() != Qualifiers::GCNone) { |
Fariborz Jahanian | 5934e75 | 2009-02-18 18:52:41 +0000 | [diff] [blame] | 1559 | S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1560 | return; |
| 1561 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1562 | |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1563 | // Check the attribute arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | if (!Attr.getParameterName()) { |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1565 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1566 | << "objc_gc" << 1; |
| 1567 | return; |
| 1568 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1569 | Qualifiers::GC GCAttr; |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1570 | if (Attr.getNumArgs() != 0) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1571 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1572 | return; |
| 1573 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | if (Attr.getParameterName()->isStr("weak")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1575 | GCAttr = Qualifiers::Weak; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1576 | else if (Attr.getParameterName()->isStr("strong")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1577 | GCAttr = Qualifiers::Strong; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1578 | else { |
| 1579 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 1580 | << "objc_gc" << Attr.getParameterName(); |
| 1581 | return; |
| 1582 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1583 | |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1584 | Type = S.Context.getObjCGCQualType(Type, GCAttr); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1587 | /// HandleNoReturnTypeAttribute - Process the noreturn attribute on the |
| 1588 | /// specified type. The attribute contains 0 arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | static void HandleNoReturnTypeAttribute(QualType &Type, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1590 | const AttributeList &Attr, Sema &S) { |
| 1591 | if (Attr.getNumArgs() != 0) |
| 1592 | return; |
| 1593 | |
| 1594 | // We only apply this to a pointer to function or a pointer to block. |
| 1595 | if (!Type->isFunctionPointerType() |
| 1596 | && !Type->isBlockPointerType() |
| 1597 | && !Type->isFunctionType()) |
| 1598 | return; |
| 1599 | |
| 1600 | Type = S.Context.getNoReturnType(Type); |
| 1601 | } |
| 1602 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1603 | void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1604 | // Scan through and apply attributes to this type where it makes sense. Some |
| 1605 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 1606 | // 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] | 1607 | // apply to the decl. Here we apply type attributes and ignore the rest. |
| 1608 | for (; AL; AL = AL->getNext()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1609 | // If this is an attribute we can handle, do so now, otherwise, add it to |
| 1610 | // the LeftOverAttrs list for rechaining. |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 1611 | switch (AL->getKind()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1612 | default: break; |
| 1613 | case AttributeList::AT_address_space: |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1614 | HandleAddressSpaceTypeAttribute(Result, *AL, *this); |
| 1615 | break; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1616 | case AttributeList::AT_objc_gc: |
| 1617 | HandleObjCGCTypeAttribute(Result, *AL, *this); |
| 1618 | break; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1619 | case AttributeList::AT_noreturn: |
| 1620 | HandleNoReturnTypeAttribute(Result, *AL, *this); |
| 1621 | break; |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1622 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1623 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | /// @brief Ensure that the type T is a complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1627 | /// |
| 1628 | /// This routine checks whether the type @p T is complete in any |
| 1629 | /// context where a complete type is required. If @p T is a complete |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 1630 | /// type, returns false. If @p T is a class template specialization, |
| 1631 | /// this routine then attempts to perform class template |
| 1632 | /// instantiation. If instantiation fails, or if @p T is incomplete |
| 1633 | /// and cannot be completed, issues the diagnostic @p diag (giving it |
| 1634 | /// the type @p T) and returns true. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1635 | /// |
| 1636 | /// @param Loc The location in the source that the incomplete type |
| 1637 | /// diagnostic should refer to. |
| 1638 | /// |
| 1639 | /// @param T The type that this routine is examining for completeness. |
| 1640 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | /// @param PD The partial diagnostic that will be printed out if T is not a |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1642 | /// complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1643 | /// |
| 1644 | /// @returns @c true if @p T is incomplete and a diagnostic was emitted, |
| 1645 | /// @c false otherwise. |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 1646 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 1647 | const PartialDiagnostic &PD, |
| 1648 | std::pair<SourceLocation, |
| 1649 | PartialDiagnostic> Note) { |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 1650 | unsigned diag = PD.getDiagID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 1652 | // FIXME: Add this assertion to make sure we always get instantiation points. |
| 1653 | // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType"); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 1654 | // FIXME: Add this assertion to help us flush out problems with |
| 1655 | // checking for dependent types and type-dependent expressions. |
| 1656 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | // assert(!T->isDependentType() && |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 1658 | // "Can't ask whether a dependent type is complete"); |
| 1659 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1660 | // If we have a complete type, we're done. |
| 1661 | if (!T->isIncompleteType()) |
| 1662 | return false; |
Eli Friedman | 3c0eb16 | 2008-05-27 03:33:27 +0000 | [diff] [blame] | 1663 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1664 | // If we have a class template specialization or a class member of a |
| 1665 | // class template specialization, try to instantiate it. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1666 | if (const RecordType *Record = T->getAs<RecordType>()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1667 | if (ClassTemplateSpecializationDecl *ClassTemplateSpec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1668 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1669 | if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1670 | if (Loc.isValid()) |
John McCall | 9cc7807 | 2009-09-11 07:25:08 +0000 | [diff] [blame] | 1671 | ClassTemplateSpec->setPointOfInstantiation(Loc); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1672 | return InstantiateClassTemplateSpecialization(ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1673 | TSK_ImplicitInstantiation, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1674 | /*Complain=*/diag != 0); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1675 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | } else if (CXXRecordDecl *Rec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1677 | = dyn_cast<CXXRecordDecl>(Record->getDecl())) { |
| 1678 | if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1679 | MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo(); |
| 1680 | assert(MSInfo && "Missing member specialization information?"); |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1681 | // This record was instantiated from a class within a template. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1682 | if (MSInfo->getTemplateSpecializationKind() |
| 1683 | != TSK_ExplicitSpecialization) { |
| 1684 | MSInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1685 | return InstantiateClass(Loc, Rec, Pattern, |
| 1686 | getTemplateInstantiationArgs(Rec), |
| 1687 | TSK_ImplicitInstantiation, |
| 1688 | /*Complain=*/diag != 0); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1689 | } |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1690 | } |
| 1691 | } |
| 1692 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1693 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1694 | if (diag == 0) |
| 1695 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1697 | // We have an incomplete type. Produce a diagnostic. |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 1698 | Diag(Loc, PD) << T; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1699 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 1700 | // If we have a note, produce it. |
| 1701 | if (!Note.first.isInvalid()) |
| 1702 | Diag(Note.first, Note.second); |
| 1703 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1704 | // If the type was a forward declaration of a class/struct/union |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | // type, produce |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1706 | const TagType *Tag = 0; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1707 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1708 | Tag = Record; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1709 | else if (const EnumType *Enum = T->getAs<EnumType>()) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1710 | Tag = Enum; |
| 1711 | |
| 1712 | if (Tag && !Tag->getDecl()->isInvalidDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | Diag(Tag->getDecl()->getLocation(), |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1714 | Tag->isBeingDefined() ? diag::note_type_being_defined |
| 1715 | : diag::note_forward_declaration) |
| 1716 | << QualType(Tag, 0); |
| 1717 | |
| 1718 | return true; |
| 1719 | } |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 1720 | |
| 1721 | /// \brief Retrieve a version of the type 'T' that is qualified by the |
| 1722 | /// nested-name-specifier contained in SS. |
| 1723 | QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) { |
| 1724 | if (!SS.isSet() || SS.isInvalid() || T.isNull()) |
| 1725 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1727 | NestedNameSpecifier *NNS |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 1728 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1729 | return Context.getQualifiedNameType(NNS, T); |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 1730 | } |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 1731 | |
| 1732 | QualType Sema::BuildTypeofExprType(Expr *E) { |
| 1733 | return Context.getTypeOfExprType(E); |
| 1734 | } |
| 1735 | |
| 1736 | QualType Sema::BuildDecltypeType(Expr *E) { |
| 1737 | if (E->getType() == Context.OverloadTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 | Diag(E->getLocStart(), |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 1739 | diag::err_cannot_determine_declared_type_of_overloaded_function); |
| 1740 | return QualType(); |
| 1741 | } |
| 1742 | return Context.getDecltypeType(E); |
| 1743 | } |