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