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