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