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