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 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 71 | typedef std::pair<const AttributeList*,QualType> DelayedAttribute; |
| 72 | typedef llvm::SmallVectorImpl<DelayedAttribute> DelayedAttributeSet; |
| 73 | |
| 74 | static void ProcessTypeAttributeList(Sema &S, QualType &Type, |
Charles Davis | 328ce34 | 2010-02-24 02:27:18 +0000 | [diff] [blame] | 75 | bool IsDeclSpec, |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 76 | const AttributeList *Attrs, |
| 77 | DelayedAttributeSet &DelayedFnAttrs); |
| 78 | static bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr); |
| 79 | |
| 80 | static void ProcessDelayedFnAttrs(Sema &S, QualType &Type, |
| 81 | DelayedAttributeSet &Attrs) { |
| 82 | for (DelayedAttributeSet::iterator I = Attrs.begin(), |
| 83 | E = Attrs.end(); I != E; ++I) |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 84 | if (ProcessFnAttr(S, Type, *I->first)) { |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 85 | S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type) |
| 86 | << I->first->getName() << I->second; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 87 | // Avoid any further processing of this attribute. |
| 88 | I->first->setInvalid(); |
| 89 | } |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 90 | Attrs.clear(); |
| 91 | } |
| 92 | |
| 93 | static void DiagnoseDelayedFnAttrs(Sema &S, DelayedAttributeSet &Attrs) { |
| 94 | for (DelayedAttributeSet::iterator I = Attrs.begin(), |
| 95 | E = Attrs.end(); I != E; ++I) { |
| 96 | S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type) |
| 97 | << I->first->getName() << I->second; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 98 | // Avoid any further processing of this attribute. |
| 99 | I->first->setInvalid(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 100 | } |
| 101 | Attrs.clear(); |
| 102 | } |
| 103 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 104 | /// \brief Convert the specified declspec to the appropriate type |
| 105 | /// object. |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 106 | /// \param D the declarator containing the declaration specifier. |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 107 | /// \returns The type described by the declaration specifiers. This function |
| 108 | /// never returns null. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 109 | static QualType ConvertDeclSpecToType(Sema &TheSema, |
| 110 | Declarator &TheDeclarator, |
| 111 | DelayedAttributeSet &Delayed) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 112 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 113 | // checking. |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 114 | const DeclSpec &DS = TheDeclarator.getDeclSpec(); |
| 115 | SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc(); |
| 116 | if (DeclLoc.isInvalid()) |
| 117 | DeclLoc = DS.getSourceRange().getBegin(); |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 118 | |
| 119 | ASTContext &Context = TheSema.Context; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 121 | QualType Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | switch (DS.getTypeSpecType()) { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 123 | case DeclSpec::TST_void: |
| 124 | Result = Context.VoidTy; |
| 125 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | case DeclSpec::TST_char: |
| 127 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 128 | Result = Context.CharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 130 | Result = Context.SignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | else { |
| 132 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 133 | "Unknown TSS value"); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 134 | Result = Context.UnsignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 136 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 137 | case DeclSpec::TST_wchar: |
| 138 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
| 139 | Result = Context.WCharTy; |
| 140 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 141 | TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 142 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 143 | Result = Context.getSignedWCharType(); |
| 144 | } else { |
| 145 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 146 | "Unknown TSS value"); |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 147 | TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 148 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 149 | Result = Context.getUnsignedWCharType(); |
| 150 | } |
| 151 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 152 | case DeclSpec::TST_char16: |
| 153 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 154 | "Unknown TSS value"); |
| 155 | Result = Context.Char16Ty; |
| 156 | break; |
| 157 | case DeclSpec::TST_char32: |
| 158 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 159 | "Unknown TSS value"); |
| 160 | Result = Context.Char32Ty; |
| 161 | break; |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 162 | case DeclSpec::TST_unspecified: |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 163 | // "<proto1,proto2>" is an objc qualified ID with a missing id. |
Chris Lattner | 097e916 | 2008-10-20 02:01:50 +0000 | [diff] [blame] | 164 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 165 | Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, |
| 166 | (ObjCProtocolDecl**)PQ, |
| 167 | DS.getNumProtocolQualifiers()); |
| 168 | Result = Context.getObjCObjectPointerType(Result); |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 169 | break; |
| 170 | } |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 171 | |
| 172 | // If this is a missing declspec in a block literal return context, then it |
| 173 | // is inferred from the return statements inside the block. |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 174 | if (isOmittedBlockReturnType(TheDeclarator)) { |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 175 | Result = Context.DependentTy; |
| 176 | break; |
| 177 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 179 | // Unspecified typespec defaults to int in C90. However, the C90 grammar |
| 180 | // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, |
| 181 | // type-qualifier, or storage-class-specifier. If not, emit an extwarn. |
| 182 | // Note that the one exception to this is function definitions, which are |
| 183 | // allowed to be completely missing a declspec. This is handled in the |
| 184 | // parser already though by it pretending to have seen an 'int' in this |
| 185 | // case. |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 186 | if (TheSema.getLangOptions().ImplicitInt) { |
Chris Lattner | 35d276f | 2009-02-27 18:53:28 +0000 | [diff] [blame] | 187 | // In C89 mode, we only warn if there is a completely missing declspec |
| 188 | // when one is not allowed. |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 189 | if (DS.isEmpty()) { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 190 | TheSema.Diag(DeclLoc, diag::ext_missing_declspec) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 191 | << DS.getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 192 | << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int"); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 193 | } |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 194 | } else if (!DS.hasTypeSpecifier()) { |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 195 | // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: |
| 196 | // "At least one type specifier shall be given in the declaration |
| 197 | // specifiers in each declaration, and in the specifier-qualifier list in |
| 198 | // each struct declaration and type name." |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 199 | // FIXME: Does Microsoft really have the implicit int extension in C++? |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 200 | if (TheSema.getLangOptions().CPlusPlus && |
| 201 | !TheSema.getLangOptions().Microsoft) { |
| 202 | TheSema.Diag(DeclLoc, diag::err_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 203 | << DS.getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 205 | // When this occurs in C++ code, often something is very broken with the |
| 206 | // value being declared, poison it as invalid so we don't get chains of |
| 207 | // errors. |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 208 | TheDeclarator.setInvalidType(true); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 209 | } else { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 210 | TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 211 | << DS.getSourceRange(); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 212 | } |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
| 215 | // FALL THROUGH. |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 216 | case DeclSpec::TST_int: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 218 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 219 | case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; |
| 220 | case DeclSpec::TSW_short: Result = Context.ShortTy; break; |
| 221 | case DeclSpec::TSW_long: Result = Context.LongTy; break; |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 222 | case DeclSpec::TSW_longlong: |
| 223 | Result = Context.LongLongTy; |
| 224 | |
| 225 | // long long is a C99 feature. |
| 226 | if (!TheSema.getLangOptions().C99 && |
| 227 | !TheSema.getLangOptions().CPlusPlus0x) |
| 228 | TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong); |
| 229 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 230 | } |
| 231 | } else { |
| 232 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 233 | case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; |
| 234 | case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; |
| 235 | case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 236 | case DeclSpec::TSW_longlong: |
| 237 | Result = Context.UnsignedLongLongTy; |
| 238 | |
| 239 | // long long is a C99 feature. |
| 240 | if (!TheSema.getLangOptions().C99 && |
| 241 | !TheSema.getLangOptions().CPlusPlus0x) |
| 242 | TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong); |
| 243 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 246 | break; |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 247 | } |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 248 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 249 | case DeclSpec::TST_double: |
| 250 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 251 | Result = Context.LongDoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 252 | else |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 253 | Result = Context.DoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 254 | break; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 255 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 256 | case DeclSpec::TST_decimal32: // _Decimal32 |
| 257 | case DeclSpec::TST_decimal64: // _Decimal64 |
| 258 | case DeclSpec::TST_decimal128: // _Decimal128 |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 259 | TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 260 | Result = Context.IntTy; |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 261 | TheDeclarator.setInvalidType(true); |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 262 | break; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 263 | case DeclSpec::TST_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 264 | case DeclSpec::TST_enum: |
| 265 | case DeclSpec::TST_union: |
| 266 | case DeclSpec::TST_struct: { |
Douglas Gregor | c7621a6 | 2009-11-05 20:54:04 +0000 | [diff] [blame] | 267 | TypeDecl *D |
| 268 | = dyn_cast_or_null<TypeDecl>(static_cast<Decl *>(DS.getTypeRep())); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 269 | if (!D) { |
| 270 | // This can happen in C++ with ambiguous lookups. |
| 271 | Result = Context.IntTy; |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 272 | TheDeclarator.setInvalidType(true); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 273 | break; |
| 274 | } |
| 275 | |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 276 | // If the type is deprecated or unavailable, diagnose it. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 277 | TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc()); |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 278 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 280 | DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!"); |
| 281 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 282 | // TypeQuals handled by caller. |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 283 | Result = Context.getTypeDeclType(D); |
John McCall | 2191b20 | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 284 | |
| 285 | // In C++, make an ElaboratedType. |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 286 | if (TheSema.getLangOptions().CPlusPlus) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 287 | ElaboratedTypeKeyword Keyword |
| 288 | = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType()); |
| 289 | Result = TheSema.getElaboratedType(Keyword, DS.getTypeSpecScope(), |
| 290 | Result); |
John McCall | 2191b20 | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 291 | } |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 292 | if (D->isInvalidDecl()) |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 293 | TheDeclarator.setInvalidType(true); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 294 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | } |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 296 | case DeclSpec::TST_typename: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 298 | DS.getTypeSpecSign() == 0 && |
| 299 | "Can't handle qualifiers on typedef names yet!"); |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 300 | Result = TheSema.GetTypeFromParser(DS.getTypeRep()); |
John McCall | 27940d2 | 2010-07-30 05:17:22 +0000 | [diff] [blame] | 301 | if (Result.isNull()) |
| 302 | TheDeclarator.setInvalidType(true); |
| 303 | else if (DeclSpec::ProtocolQualifierListTy PQ |
| 304 | = DS.getProtocolQualifiers()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 305 | if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) { |
| 306 | // Silently drop any existing protocol qualifiers. |
| 307 | // TODO: determine whether that's the right thing to do. |
| 308 | if (ObjT->getNumProtocols()) |
| 309 | Result = ObjT->getBaseType(); |
| 310 | |
| 311 | if (DS.getNumProtocolQualifiers()) |
| 312 | Result = Context.getObjCObjectType(Result, |
| 313 | (ObjCProtocolDecl**) PQ, |
| 314 | DS.getNumProtocolQualifiers()); |
| 315 | } else if (Result->isObjCIdType()) { |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 316 | // id<protocol-list> |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 317 | Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, |
| 318 | (ObjCProtocolDecl**) PQ, |
| 319 | DS.getNumProtocolQualifiers()); |
| 320 | Result = Context.getObjCObjectPointerType(Result); |
| 321 | } else if (Result->isObjCClassType()) { |
Steve Naroff | 4262a07 | 2009-02-23 18:53:24 +0000 | [diff] [blame] | 322 | // Class<protocol-list> |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 323 | Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, |
| 324 | (ObjCProtocolDecl**) PQ, |
| 325 | DS.getNumProtocolQualifiers()); |
| 326 | Result = Context.getObjCObjectPointerType(Result); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 327 | } else { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 328 | TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 329 | << DS.getSourceRange(); |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 330 | TheDeclarator.setInvalidType(true); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 331 | } |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 332 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 334 | // TypeQuals handled by caller. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 335 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 336 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 337 | case DeclSpec::TST_typeofType: |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 338 | // FIXME: Preserve type source info. |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 339 | Result = TheSema.GetTypeFromParser(DS.getTypeRep()); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 340 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 341 | // TypeQuals handled by caller. |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 342 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 343 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 344 | case DeclSpec::TST_typeofExpr: { |
| 345 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 346 | assert(E && "Didn't get an expression for typeof?"); |
| 347 | // TypeQuals handled by caller. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 348 | Result = TheSema.BuildTypeofExprType(E); |
| 349 | if (Result.isNull()) { |
| 350 | Result = Context.IntTy; |
| 351 | TheDeclarator.setInvalidType(true); |
| 352 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 353 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 354 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 355 | case DeclSpec::TST_decltype: { |
| 356 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 357 | assert(E && "Didn't get an expression for decltype?"); |
| 358 | // TypeQuals handled by caller. |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 359 | Result = TheSema.BuildDecltypeType(E); |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 360 | if (Result.isNull()) { |
| 361 | Result = Context.IntTy; |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 362 | TheDeclarator.setInvalidType(true); |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 363 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 364 | break; |
| 365 | } |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 366 | case DeclSpec::TST_auto: { |
| 367 | // TypeQuals handled by caller. |
| 368 | Result = Context.UndeducedAutoTy; |
| 369 | break; |
| 370 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 372 | case DeclSpec::TST_error: |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 373 | Result = Context.IntTy; |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 374 | TheDeclarator.setInvalidType(true); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 375 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 376 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 378 | // Handle complex types. |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 379 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 380 | if (TheSema.getLangOptions().Freestanding) |
| 381 | TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 382 | Result = Context.getComplexType(Result); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 383 | } else if (DS.isTypeAltiVecVector()) { |
| 384 | unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result)); |
| 385 | assert(typeSize > 0 && "type size for vector must be greater than 0 bits"); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 386 | VectorType::AltiVecSpecific AltiVecSpec = VectorType::AltiVec; |
| 387 | if (DS.isTypeAltiVecPixel()) |
| 388 | AltiVecSpec = VectorType::Pixel; |
| 389 | else if (DS.isTypeAltiVecBool()) |
| 390 | AltiVecSpec = VectorType::Bool; |
| 391 | Result = Context.getVectorType(Result, 128/typeSize, AltiVecSpec); |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 392 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 394 | assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && |
| 395 | "FIXME: imaginary types not supported yet!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
Chris Lattner | 38d8b98 | 2008-02-20 22:04:11 +0000 | [diff] [blame] | 397 | // See if there are any attributes on the declspec that apply to the type (as |
| 398 | // opposed to the decl). |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 399 | if (const AttributeList *AL = DS.getAttributes()) |
Charles Davis | 328ce34 | 2010-02-24 02:27:18 +0000 | [diff] [blame] | 400 | ProcessTypeAttributeList(TheSema, Result, true, AL, Delayed); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 402 | // Apply const/volatile/restrict qualifiers to T. |
| 403 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 404 | |
| 405 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 406 | // or incomplete types shall not be restrict-qualified." C++ also allows |
| 407 | // restrict-qualified references. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 408 | if (TypeQuals & DeclSpec::TQ_restrict) { |
Fariborz Jahanian | 2b5ff1a | 2009-12-07 18:08:58 +0000 | [diff] [blame] | 409 | if (Result->isAnyPointerType() || Result->isReferenceType()) { |
| 410 | QualType EltTy; |
| 411 | if (Result->isObjCObjectPointerType()) |
| 412 | EltTy = Result; |
| 413 | else |
| 414 | EltTy = Result->isPointerType() ? |
| 415 | Result->getAs<PointerType>()->getPointeeType() : |
| 416 | Result->getAs<ReferenceType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 418 | // 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] | 419 | // incomplete type. |
| 420 | if (!EltTy->isIncompleteOrObjectType()) { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 421 | TheSema.Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 422 | diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 423 | << EltTy << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 424 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 425 | } |
| 426 | } else { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 427 | TheSema.Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 428 | diag::err_typecheck_invalid_restrict_not_pointer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 429 | << Result << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 430 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 431 | } |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 432 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 434 | // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification |
| 435 | // of a function type includes any type qualifiers, the behavior is |
| 436 | // undefined." |
| 437 | if (Result->isFunctionType() && TypeQuals) { |
| 438 | // Get some location to point at, either the C or V location. |
| 439 | SourceLocation Loc; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 440 | if (TypeQuals & DeclSpec::TQ_const) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 441 | Loc = DS.getConstSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 442 | else if (TypeQuals & DeclSpec::TQ_volatile) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 443 | Loc = DS.getVolatileSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 444 | else { |
| 445 | assert((TypeQuals & DeclSpec::TQ_restrict) && |
| 446 | "Has CVR quals but not C, V, or R?"); |
| 447 | Loc = DS.getRestrictSpecLoc(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 448 | } |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 449 | TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 450 | << Result << DS.getSourceRange(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 451 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 453 | // C++ [dcl.ref]p1: |
| 454 | // Cv-qualified references are ill-formed except when the |
| 455 | // cv-qualifiers are introduced through the use of a typedef |
| 456 | // (7.1.3) or of a template type argument (14.3), in which |
| 457 | // case the cv-qualifiers are ignored. |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 458 | // FIXME: Shouldn't we be checking SCS_typedef here? |
| 459 | if (DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 460 | TypeQuals && Result->isReferenceType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 461 | TypeQuals &= ~DeclSpec::TQ_const; |
| 462 | TypeQuals &= ~DeclSpec::TQ_volatile; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | } |
| 464 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 465 | Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals); |
| 466 | Result = Context.getQualifiedType(Result, Quals); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 467 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 468 | |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 469 | return Result; |
| 470 | } |
| 471 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 472 | static std::string getPrintableNameForEntity(DeclarationName Entity) { |
| 473 | if (Entity) |
| 474 | return Entity.getAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 476 | return "type name"; |
| 477 | } |
| 478 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 479 | QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc, |
| 480 | Qualifiers Qs) { |
| 481 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 482 | // object or incomplete types shall not be restrict-qualified." |
| 483 | if (Qs.hasRestrict()) { |
| 484 | unsigned DiagID = 0; |
| 485 | QualType ProblemTy; |
| 486 | |
| 487 | const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); |
| 488 | if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) { |
| 489 | if (!RTy->getPointeeType()->isIncompleteOrObjectType()) { |
| 490 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 491 | ProblemTy = T->getAs<ReferenceType>()->getPointeeType(); |
| 492 | } |
| 493 | } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { |
| 494 | if (!PTy->getPointeeType()->isIncompleteOrObjectType()) { |
| 495 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 496 | ProblemTy = T->getAs<PointerType>()->getPointeeType(); |
| 497 | } |
| 498 | } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) { |
| 499 | if (!PTy->getPointeeType()->isIncompleteOrObjectType()) { |
| 500 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 501 | ProblemTy = T->getAs<PointerType>()->getPointeeType(); |
| 502 | } |
| 503 | } else if (!Ty->isDependentType()) { |
| 504 | // FIXME: this deserves a proper diagnostic |
| 505 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 506 | ProblemTy = T; |
| 507 | } |
| 508 | |
| 509 | if (DiagID) { |
| 510 | Diag(Loc, DiagID) << ProblemTy; |
| 511 | Qs.removeRestrict(); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | return Context.getQualifiedType(T, Qs); |
| 516 | } |
| 517 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 518 | /// \brief Build a pointer type. |
| 519 | /// |
| 520 | /// \param T The type to which we'll be building a pointer. |
| 521 | /// |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 522 | /// \param Loc The location of the entity whose type involves this |
| 523 | /// pointer type or, if there is no such entity, the location of the |
| 524 | /// type that will have pointer type. |
| 525 | /// |
| 526 | /// \param Entity The name of the entity that involves the pointer |
| 527 | /// type, if known. |
| 528 | /// |
| 529 | /// \returns A suitable pointer type, if there are no |
| 530 | /// errors. Otherwise, returns a NULL type. |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 531 | QualType Sema::BuildPointerType(QualType T, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 532 | SourceLocation Loc, DeclarationName Entity) { |
| 533 | if (T->isReferenceType()) { |
| 534 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
| 535 | Diag(Loc, diag::err_illegal_decl_pointer_to_reference) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 536 | << getPrintableNameForEntity(Entity) << T; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 537 | return QualType(); |
| 538 | } |
| 539 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 540 | assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType"); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 541 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 542 | // Build the pointer type. |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 543 | return Context.getPointerType(T); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /// \brief Build a reference type. |
| 547 | /// |
| 548 | /// \param T The type to which we'll be building a reference. |
| 549 | /// |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 550 | /// \param Loc The location of the entity whose type involves this |
| 551 | /// reference type or, if there is no such entity, the location of the |
| 552 | /// type that will have reference type. |
| 553 | /// |
| 554 | /// \param Entity The name of the entity that involves the reference |
| 555 | /// type, if known. |
| 556 | /// |
| 557 | /// \returns A suitable reference type, if there are no |
| 558 | /// errors. Otherwise, returns a NULL type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 559 | QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue, |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 560 | SourceLocation Loc, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 561 | DeclarationName Entity) { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 562 | bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>(); |
| 563 | |
| 564 | // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a |
| 565 | // reference to a type T, and attempt to create the type "lvalue |
| 566 | // reference to cv TD" creates the type "lvalue reference to T". |
| 567 | // We use the qualifiers (restrict or none) of the original reference, |
| 568 | // not the new ones. This is consistent with GCC. |
| 569 | |
| 570 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 571 | // |
| 572 | // According to C++ DR 106, references to references are only |
| 573 | // diagnosed when they are written directly (e.g., "int & &"), |
| 574 | // but not when they happen via a typedef: |
| 575 | // |
| 576 | // typedef int& intref; |
| 577 | // typedef intref& intref2; |
| 578 | // |
| 579 | // Parser::ParseDeclaratorInternal diagnoses the case where |
| 580 | // references are written directly; here, we handle the |
| 581 | // collapsing of references-to-references as described in C++ |
| 582 | // DR 106 and amended by C++ DR 540. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 583 | |
| 584 | // C++ [dcl.ref]p1: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 585 | // A declarator that specifies the type "reference to cv void" |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 586 | // is ill-formed. |
| 587 | if (T->isVoidType()) { |
| 588 | Diag(Loc, diag::err_reference_to_void); |
| 589 | return QualType(); |
| 590 | } |
| 591 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 592 | // Handle restrict on references. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 593 | if (LValueRef) |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 594 | return Context.getLValueReferenceType(T, SpelledAsLValue); |
| 595 | return Context.getRValueReferenceType(T); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /// \brief Build an array type. |
| 599 | /// |
| 600 | /// \param T The type of each element in the array. |
| 601 | /// |
| 602 | /// \param ASM C99 array size modifier (e.g., '*', 'static'). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 603 | /// |
| 604 | /// \param ArraySize Expression describing the size of the array. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 605 | /// |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 606 | /// \param Loc The location of the entity whose type involves this |
| 607 | /// array type or, if there is no such entity, the location of the |
| 608 | /// type that will have array type. |
| 609 | /// |
| 610 | /// \param Entity The name of the entity that involves the array |
| 611 | /// type, if known. |
| 612 | /// |
| 613 | /// \returns A suitable array type, if there are no errors. Otherwise, |
| 614 | /// returns a NULL type. |
| 615 | QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, |
| 616 | Expr *ArraySize, unsigned Quals, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 617 | SourceRange Brackets, DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 618 | |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 619 | SourceLocation Loc = Brackets.getBegin(); |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 620 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 621 | // C++ [dcl.array]p1: |
| 622 | // T is called the array element type; this type shall not be a reference |
| 623 | // type, the (possibly cv-qualified) type void, a function type or an |
| 624 | // abstract class type. |
| 625 | // |
| 626 | // Note: function types are handled in the common path with C. |
| 627 | if (T->isReferenceType()) { |
| 628 | Diag(Loc, diag::err_illegal_decl_array_of_references) |
| 629 | << getPrintableNameForEntity(Entity) << T; |
| 630 | return QualType(); |
| 631 | } |
| 632 | |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 633 | if (T->isVoidType()) { |
| 634 | Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T; |
| 635 | return QualType(); |
| 636 | } |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 637 | |
| 638 | if (RequireNonAbstractType(Brackets.getBegin(), T, |
| 639 | diag::err_array_of_abstract_type)) |
| 640 | return QualType(); |
| 641 | |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 642 | } else { |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 643 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
| 644 | // 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] | 645 | if (RequireCompleteType(Loc, T, |
| 646 | diag::err_illegal_decl_array_incomplete_type)) |
| 647 | return QualType(); |
| 648 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 649 | |
| 650 | if (T->isFunctionType()) { |
| 651 | Diag(Loc, diag::err_illegal_decl_array_of_functions) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 652 | << getPrintableNameForEntity(Entity) << T; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 653 | return QualType(); |
| 654 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 655 | |
Anders Carlsson | e7cf07d | 2009-06-26 19:33:28 +0000 | [diff] [blame] | 656 | if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | Diag(Loc, diag::err_illegal_decl_array_of_auto) |
Anders Carlsson | e7cf07d | 2009-06-26 19:33:28 +0000 | [diff] [blame] | 658 | << getPrintableNameForEntity(Entity); |
| 659 | return QualType(); |
| 660 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 662 | if (const RecordType *EltTy = T->getAs<RecordType>()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 663 | // If the element type is a struct or union that contains a variadic |
| 664 | // array, accept it as a GNU extension: C99 6.7.2.1p2. |
| 665 | if (EltTy->getDecl()->hasFlexibleArrayMember()) |
| 666 | Diag(Loc, diag::ext_flexible_array_in_array) << T; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 667 | } else if (T->isObjCObjectType()) { |
Chris Lattner | c7c11b1 | 2009-04-27 01:55:56 +0000 | [diff] [blame] | 668 | Diag(Loc, diag::err_objc_array_of_interfaces) << T; |
| 669 | return QualType(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 670 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 672 | // C99 6.7.5.2p1: The size expression shall have integer type. |
| 673 | if (ArraySize && !ArraySize->isTypeDependent() && |
| 674 | !ArraySize->getType()->isIntegerType()) { |
| 675 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) |
| 676 | << ArraySize->getType() << ArraySize->getSourceRange(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 677 | return QualType(); |
| 678 | } |
| 679 | llvm::APSInt ConstVal(32); |
| 680 | if (!ArraySize) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 681 | if (ASM == ArrayType::Star) |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 682 | T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets); |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 683 | else |
| 684 | T = Context.getIncompleteArrayType(T, ASM, Quals); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 685 | } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) { |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 686 | T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 687 | } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 688 | (!T->isDependentType() && !T->isIncompleteType() && |
| 689 | !T->isConstantSizeType())) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 690 | // Per C99, a variable array is an array with either a non-constant |
| 691 | // size or an element type that has a non-constant-size |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 692 | T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 693 | } else { |
| 694 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 695 | // have a value greater than zero. |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 696 | if (ConstVal.isSigned() && ConstVal.isNegative()) { |
| 697 | Diag(ArraySize->getLocStart(), |
| 698 | diag::err_typecheck_negative_array_size) |
| 699 | << ArraySize->getSourceRange(); |
| 700 | return QualType(); |
| 701 | } |
| 702 | if (ConstVal == 0) { |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 703 | // GCC accepts zero sized static arrays. We allow them when |
| 704 | // we're not in a SFINAE context. |
| 705 | Diag(ArraySize->getLocStart(), |
| 706 | isSFINAEContext()? diag::err_typecheck_zero_array_size |
| 707 | : diag::ext_typecheck_zero_array_size) |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 708 | << ArraySize->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | } |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 710 | T = Context.getConstantArrayType(T, ConstVal, ASM, Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 711 | } |
David Chisnall | af40776 | 2010-01-11 23:08:08 +0000 | [diff] [blame] | 712 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
| 713 | if (!getLangOptions().C99) { |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 714 | if (T->isVariableArrayType()) { |
| 715 | // Prohibit the use of non-POD types in VLAs. |
Douglas Gregor | 204ce17 | 2010-05-24 20:42:30 +0000 | [diff] [blame] | 716 | if (!T->isDependentType() && |
| 717 | !Context.getBaseElementType(T)->isPODType()) { |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 718 | Diag(Loc, diag::err_vla_non_pod) |
| 719 | << Context.getBaseElementType(T); |
| 720 | return QualType(); |
| 721 | } |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 722 | // Prohibit the use of VLAs during template argument deduction. |
| 723 | else if (isSFINAEContext()) { |
| 724 | Diag(Loc, diag::err_vla_in_sfinae); |
| 725 | return QualType(); |
| 726 | } |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 727 | // Just extwarn about VLAs. |
| 728 | else |
| 729 | Diag(Loc, diag::ext_vla); |
| 730 | } else if (ASM != ArrayType::Normal || Quals != 0) |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 731 | Diag(Loc, |
| 732 | getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx |
| 733 | : diag::ext_c99_array_usage); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | return T; |
| 737 | } |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 738 | |
| 739 | /// \brief Build an ext-vector type. |
| 740 | /// |
| 741 | /// Run the required checks for the extended vector type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 743 | SourceLocation AttrLoc) { |
| 744 | |
| 745 | Expr *Arg = (Expr *)ArraySize.get(); |
| 746 | |
| 747 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 748 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | if (!T->isDependentType() && |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 750 | !T->isIntegerType() && !T->isRealFloatingType()) { |
| 751 | Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T; |
| 752 | return QualType(); |
| 753 | } |
| 754 | |
| 755 | if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { |
| 756 | llvm::APSInt vecSize(32); |
| 757 | if (!Arg->isIntegerConstantExpr(vecSize, Context)) { |
| 758 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
| 759 | << "ext_vector_type" << Arg->getSourceRange(); |
| 760 | return QualType(); |
| 761 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | |
| 763 | // unlike gcc's vector_size attribute, the size is specified as the |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 764 | // number of elements, not the number of bytes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 765 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 766 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 767 | if (vectorSize == 0) { |
| 768 | Diag(AttrLoc, diag::err_attribute_zero_size) |
| 769 | << Arg->getSourceRange(); |
| 770 | return QualType(); |
| 771 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 773 | if (!T->isDependentType()) |
| 774 | return Context.getExtVectorType(T, vectorSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(), |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 778 | AttrLoc); |
| 779 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 781 | /// \brief Build a function type. |
| 782 | /// |
| 783 | /// This routine checks the function type according to C++ rules and |
| 784 | /// under the assumption that the result type and parameter types have |
| 785 | /// just been instantiated from a template. It therefore duplicates |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 786 | /// some of the behavior of GetTypeForDeclarator, but in a much |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 787 | /// simpler form that is only suitable for this narrow use case. |
| 788 | /// |
| 789 | /// \param T The return type of the function. |
| 790 | /// |
| 791 | /// \param ParamTypes The parameter types of the function. This array |
| 792 | /// will be modified to account for adjustments to the types of the |
| 793 | /// function parameters. |
| 794 | /// |
| 795 | /// \param NumParamTypes The number of parameter types in ParamTypes. |
| 796 | /// |
| 797 | /// \param Variadic Whether this is a variadic function type. |
| 798 | /// |
| 799 | /// \param Quals The cvr-qualifiers to be applied to the function type. |
| 800 | /// |
| 801 | /// \param Loc The location of the entity whose type involves this |
| 802 | /// function type or, if there is no such entity, the location of the |
| 803 | /// type that will have function type. |
| 804 | /// |
| 805 | /// \param Entity The name of the entity that involves the function |
| 806 | /// type, if known. |
| 807 | /// |
| 808 | /// \returns A suitable function type, if there are no |
| 809 | /// errors. Otherwise, returns a NULL type. |
| 810 | QualType Sema::BuildFunctionType(QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 811 | QualType *ParamTypes, |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 812 | unsigned NumParamTypes, |
| 813 | bool Variadic, unsigned Quals, |
Eli Friedman | fa86954 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 814 | SourceLocation Loc, DeclarationName Entity, |
| 815 | const FunctionType::ExtInfo &Info) { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 816 | if (T->isArrayType() || T->isFunctionType()) { |
Douglas Gregor | 58408bc | 2010-01-11 18:46:21 +0000 | [diff] [blame] | 817 | Diag(Loc, diag::err_func_returning_array_function) |
| 818 | << T->isFunctionType() << T; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 819 | return QualType(); |
| 820 | } |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 821 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 822 | bool Invalid = false; |
| 823 | for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) { |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 824 | QualType ParamType = adjustParameterType(ParamTypes[Idx]); |
| 825 | if (ParamType->isVoidType()) { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 826 | Diag(Loc, diag::err_param_with_void_type); |
| 827 | Invalid = true; |
| 828 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 829 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 830 | ParamTypes[Idx] = ParamType; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | if (Invalid) |
| 834 | return QualType(); |
| 835 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic, |
Eli Friedman | fa86954 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 837 | Quals, false, false, 0, 0, Info); |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 838 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 839 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 840 | /// \brief Build a member pointer type \c T Class::*. |
| 841 | /// |
| 842 | /// \param T the type to which the member pointer refers. |
| 843 | /// \param Class the class type into which the member pointer points. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 844 | /// \param CVR Qualifiers applied to the member pointer type |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 845 | /// \param Loc the location where this type begins |
| 846 | /// \param Entity the name of the entity that will have this member pointer type |
| 847 | /// |
| 848 | /// \returns a member pointer type, if successful, or a NULL type if there was |
| 849 | /// an error. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | QualType Sema::BuildMemberPointerType(QualType T, QualType Class, |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 851 | SourceLocation Loc, |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 852 | DeclarationName Entity) { |
| 853 | // Verify that we're not building a pointer to pointer to function with |
| 854 | // exception specification. |
| 855 | if (CheckDistantExceptionSpec(T)) { |
| 856 | Diag(Loc, diag::err_distant_exception_spec); |
| 857 | |
| 858 | // FIXME: If we're doing this as part of template instantiation, |
| 859 | // we should return immediately. |
| 860 | |
| 861 | // Build the type anyway, but use the canonical type so that the |
| 862 | // exception specifiers are stripped off. |
| 863 | T = Context.getCanonicalType(T); |
| 864 | } |
| 865 | |
Sebastian Redl | 7378012 | 2010-06-09 21:19:43 +0000 | [diff] [blame] | 866 | // C++ 8.3.3p3: A pointer to member shall not point to ... a member |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 867 | // with reference type, or "cv void." |
| 868 | if (T->isReferenceType()) { |
Anders Carlsson | 8d4655d | 2009-06-30 00:06:57 +0000 | [diff] [blame] | 869 | Diag(Loc, diag::err_illegal_decl_mempointer_to_reference) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 870 | << (Entity? Entity.getAsString() : "type name") << T; |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 871 | return QualType(); |
| 872 | } |
| 873 | |
| 874 | if (T->isVoidType()) { |
| 875 | Diag(Loc, diag::err_illegal_decl_mempointer_to_void) |
| 876 | << (Entity? Entity.getAsString() : "type name"); |
| 877 | return QualType(); |
| 878 | } |
| 879 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 880 | if (!Class->isDependentType() && !Class->isRecordType()) { |
| 881 | Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class; |
| 882 | return QualType(); |
| 883 | } |
| 884 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 885 | return Context.getMemberPointerType(T, Class.getTypePtr()); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 886 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 888 | /// \brief Build a block pointer type. |
| 889 | /// |
| 890 | /// \param T The type to which we'll be building a block pointer. |
| 891 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 892 | /// \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] | 893 | /// |
| 894 | /// \param Loc The location of the entity whose type involves this |
| 895 | /// block pointer type or, if there is no such entity, the location of the |
| 896 | /// type that will have block pointer type. |
| 897 | /// |
| 898 | /// \param Entity The name of the entity that involves the block pointer |
| 899 | /// type, if known. |
| 900 | /// |
| 901 | /// \returns A suitable block pointer type, if there are no |
| 902 | /// errors. Otherwise, returns a NULL type. |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 903 | QualType Sema::BuildBlockPointerType(QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 | SourceLocation Loc, |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 905 | DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 906 | if (!T->isFunctionType()) { |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 907 | Diag(Loc, diag::err_nonfunction_block_type); |
| 908 | return QualType(); |
| 909 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 910 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 911 | return Context.getBlockPointerType(T); |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 912 | } |
| 913 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 914 | QualType Sema::GetTypeFromParser(TypeTy *Ty, TypeSourceInfo **TInfo) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 915 | QualType QT = QualType::getFromOpaquePtr(Ty); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 916 | if (QT.isNull()) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 917 | if (TInfo) *TInfo = 0; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 918 | return QualType(); |
| 919 | } |
| 920 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 921 | TypeSourceInfo *DI = 0; |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 922 | if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) { |
| 923 | QT = LIT->getType(); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 924 | DI = LIT->getTypeSourceInfo(); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 925 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 927 | if (TInfo) *TInfo = DI; |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 928 | return QT; |
| 929 | } |
| 930 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 931 | /// GetTypeForDeclarator - Convert the type for the specified |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 932 | /// declarator to Type instances. |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 933 | /// |
| 934 | /// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq |
| 935 | /// owns the declaration of a type (e.g., the definition of a struct |
| 936 | /// type), then *OwnedDecl will receive the owned declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 937 | /// |
| 938 | /// The result of this call will never be null, but the associated |
| 939 | /// type may be a null type if there's an unrecoverable error. |
| 940 | TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S, |
| 941 | TagDecl **OwnedDecl) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 942 | // Determine the type of the declarator. Not all forms of declarator |
| 943 | // have a type. |
| 944 | QualType T; |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 945 | TypeSourceInfo *ReturnTypeInfo = 0; |
| 946 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 947 | llvm::SmallVector<DelayedAttribute,4> FnAttrsFromDeclSpec; |
| 948 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 949 | switch (D.getName().getKind()) { |
| 950 | case UnqualifiedId::IK_Identifier: |
| 951 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 952 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 953 | case UnqualifiedId::IK_TemplateId: |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 954 | T = ConvertDeclSpecToType(*this, D, FnAttrsFromDeclSpec); |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 955 | |
Douglas Gregor | 591bd3c | 2010-02-08 22:07:33 +0000 | [diff] [blame] | 956 | if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) { |
| 957 | TagDecl* Owned = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep()); |
Douglas Gregor | b37b648 | 2010-02-12 17:40:34 +0000 | [diff] [blame] | 958 | // Owned is embedded if it was defined here, or if it is the |
| 959 | // very first (i.e., canonical) declaration of this tag type. |
| 960 | Owned->setEmbeddedInDeclarator(Owned->isDefinition() || |
| 961 | Owned->isCanonicalDecl()); |
Douglas Gregor | 591bd3c | 2010-02-08 22:07:33 +0000 | [diff] [blame] | 962 | if (OwnedDecl) *OwnedDecl = Owned; |
| 963 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 964 | break; |
| 965 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 966 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 967 | case UnqualifiedId::IK_ConstructorTemplateId: |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 968 | case UnqualifiedId::IK_DestructorName: |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 969 | // Constructors and destructors don't have return types. Use |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 970 | // "void" instead. |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 971 | T = Context.VoidTy; |
| 972 | break; |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 973 | |
| 974 | case UnqualifiedId::IK_ConversionFunctionId: |
| 975 | // The result type of a conversion function is the type that it |
| 976 | // converts to. |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 977 | T = GetTypeFromParser(D.getName().ConversionFunctionId, |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 978 | &ReturnTypeInfo); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 979 | break; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 980 | } |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 981 | |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 982 | if (T.isNull()) |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 983 | return Context.getNullTypeSourceInfo(); |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 984 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 985 | if (T == Context.UndeducedAutoTy) { |
| 986 | int Error = -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 988 | switch (D.getContext()) { |
| 989 | case Declarator::KNRTypeListContext: |
| 990 | assert(0 && "K&R type lists aren't allowed in C++"); |
| 991 | break; |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 992 | case Declarator::PrototypeContext: |
| 993 | Error = 0; // Function prototype |
| 994 | break; |
| 995 | case Declarator::MemberContext: |
| 996 | switch (cast<TagDecl>(CurContext)->getTagKind()) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 997 | case TTK_Enum: assert(0 && "unhandled tag kind"); break; |
| 998 | case TTK_Struct: Error = 1; /* Struct member */ break; |
| 999 | case TTK_Union: Error = 2; /* Union member */ break; |
| 1000 | case TTK_Class: Error = 3; /* Class member */ break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | } |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1002 | break; |
| 1003 | case Declarator::CXXCatchContext: |
| 1004 | Error = 4; // Exception declaration |
| 1005 | break; |
| 1006 | case Declarator::TemplateParamContext: |
| 1007 | Error = 5; // Template parameter |
| 1008 | break; |
| 1009 | case Declarator::BlockLiteralContext: |
| 1010 | Error = 6; // Block literal |
| 1011 | break; |
| 1012 | case Declarator::FileContext: |
| 1013 | case Declarator::BlockContext: |
| 1014 | case Declarator::ForContext: |
| 1015 | case Declarator::ConditionContext: |
| 1016 | case Declarator::TypeNameContext: |
| 1017 | break; |
| 1018 | } |
| 1019 | |
| 1020 | if (Error != -1) { |
| 1021 | Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed) |
| 1022 | << Error; |
| 1023 | T = Context.IntTy; |
| 1024 | D.setInvalidType(true); |
| 1025 | } |
| 1026 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1028 | // The name we're declaring, if any. |
| 1029 | DeclarationName Name; |
| 1030 | if (D.getIdentifier()) |
| 1031 | Name = D.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1033 | llvm::SmallVector<DelayedAttribute,4> FnAttrsFromPreviousChunk; |
| 1034 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1035 | // Walk the DeclTypeInfo, building the recursive type as we go. |
| 1036 | // DeclTypeInfos are ordered from the identifier out, which is |
| 1037 | // opposite of what we want :). |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1038 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
| 1039 | DeclaratorChunk &DeclType = D.getTypeObject(e-i-1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1040 | switch (DeclType.Kind) { |
| 1041 | default: assert(0 && "Unknown decltype!"); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1042 | case DeclaratorChunk::BlockPointer: |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 1043 | // If blocks are disabled, emit an error. |
| 1044 | if (!LangOpts.Blocks) |
| 1045 | Diag(DeclType.Loc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1047 | T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name); |
| 1048 | if (DeclType.Cls.TypeQuals) |
| 1049 | T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1050 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1051 | case DeclaratorChunk::Pointer: |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1052 | // Verify that we're not building a pointer to pointer to function with |
| 1053 | // exception specification. |
| 1054 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1055 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1056 | D.setInvalidType(true); |
| 1057 | // Build the type anyway. |
| 1058 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1059 | if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) { |
| 1060 | T = Context.getObjCObjectPointerType(T); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1061 | if (DeclType.Ptr.TypeQuals) |
| 1062 | T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1063 | break; |
| 1064 | } |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1065 | T = BuildPointerType(T, DeclType.Loc, Name); |
| 1066 | if (DeclType.Ptr.TypeQuals) |
| 1067 | T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1068 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1069 | case DeclaratorChunk::Reference: { |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1070 | // Verify that we're not building a reference to pointer to function with |
| 1071 | // exception specification. |
| 1072 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1073 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1074 | D.setInvalidType(true); |
| 1075 | // Build the type anyway. |
| 1076 | } |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1077 | T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name); |
| 1078 | |
| 1079 | Qualifiers Quals; |
| 1080 | if (DeclType.Ref.HasRestrict) |
| 1081 | T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1082 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1083 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1084 | case DeclaratorChunk::Array: { |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1085 | // Verify that we're not building an array of pointers to function with |
| 1086 | // exception specification. |
| 1087 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1088 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1089 | D.setInvalidType(true); |
| 1090 | // Build the type anyway. |
| 1091 | } |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 1092 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 1093 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1094 | ArrayType::ArraySizeModifier ASM; |
| 1095 | if (ATI.isStar) |
| 1096 | ASM = ArrayType::Star; |
| 1097 | else if (ATI.hasStatic) |
| 1098 | ASM = ArrayType::Static; |
| 1099 | else |
| 1100 | ASM = ArrayType::Normal; |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 1101 | if (ASM == ArrayType::Star && |
| 1102 | D.getContext() != Declarator::PrototypeContext) { |
| 1103 | // FIXME: This check isn't quite right: it allows star in prototypes |
| 1104 | // for function definitions, and disallows some edge cases detailed |
| 1105 | // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html |
| 1106 | Diag(DeclType.Loc, diag::err_array_star_outside_prototype); |
| 1107 | ASM = ArrayType::Normal; |
| 1108 | D.setInvalidType(true); |
| 1109 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1110 | T = BuildArrayType(T, ASM, ArraySize, |
| 1111 | Qualifiers::fromCVRMask(ATI.TypeQuals), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1112 | SourceRange(DeclType.Loc, DeclType.EndLoc), Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1113 | break; |
| 1114 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1115 | case DeclaratorChunk::Function: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1116 | // If the function declarator has a prototype (i.e. it is not () and |
| 1117 | // does not have a K&R-style identifier list), then the arguments are part |
| 1118 | // of the type, otherwise the argument list is (). |
| 1119 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1120 | |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 1121 | // C99 6.7.5.3p1: The return type may not be a function or array type. |
Douglas Gregor | 58408bc | 2010-01-11 18:46:21 +0000 | [diff] [blame] | 1122 | // For conversion functions, we'll diagnose this particular error later. |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1123 | if ((T->isArrayType() || T->isFunctionType()) && |
| 1124 | (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) { |
Douglas Gregor | 58408bc | 2010-01-11 18:46:21 +0000 | [diff] [blame] | 1125 | Diag(DeclType.Loc, diag::err_func_returning_array_function) |
| 1126 | << T->isFunctionType() << T; |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 1127 | T = Context.IntTy; |
| 1128 | D.setInvalidType(true); |
| 1129 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1130 | |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 1131 | // cv-qualifiers on return types are pointless except when the type is a |
| 1132 | // class type in C++. |
| 1133 | if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() && |
| 1134 | (!getLangOptions().CPlusPlus || |
| 1135 | (!T->isDependentType() && !T->isRecordType()))) { |
| 1136 | unsigned Quals = D.getDeclSpec().getTypeQualifiers(); |
Douglas Gregor | de80ec1 | 2010-07-13 08:50:30 +0000 | [diff] [blame] | 1137 | std::string QualStr; |
| 1138 | unsigned NumQuals = 0; |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 1139 | SourceLocation Loc; |
Douglas Gregor | de80ec1 | 2010-07-13 08:50:30 +0000 | [diff] [blame] | 1140 | if (Quals & Qualifiers::Const) { |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 1141 | Loc = D.getDeclSpec().getConstSpecLoc(); |
Douglas Gregor | de80ec1 | 2010-07-13 08:50:30 +0000 | [diff] [blame] | 1142 | ++NumQuals; |
| 1143 | QualStr = "const"; |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 1144 | } |
Douglas Gregor | de80ec1 | 2010-07-13 08:50:30 +0000 | [diff] [blame] | 1145 | if (Quals & Qualifiers::Volatile) { |
| 1146 | if (NumQuals == 0) { |
| 1147 | Loc = D.getDeclSpec().getVolatileSpecLoc(); |
| 1148 | QualStr = "volatile"; |
| 1149 | } else |
| 1150 | QualStr += " volatile"; |
| 1151 | ++NumQuals; |
| 1152 | } |
| 1153 | if (Quals & Qualifiers::Restrict) { |
| 1154 | if (NumQuals == 0) { |
| 1155 | Loc = D.getDeclSpec().getRestrictSpecLoc(); |
| 1156 | QualStr = "restrict"; |
| 1157 | } else |
| 1158 | QualStr += " restrict"; |
| 1159 | ++NumQuals; |
| 1160 | } |
| 1161 | assert(NumQuals > 0 && "No known qualifiers?"); |
| 1162 | |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 1163 | SemaDiagnosticBuilder DB = Diag(Loc, diag::warn_qual_return_type); |
Douglas Gregor | de80ec1 | 2010-07-13 08:50:30 +0000 | [diff] [blame] | 1164 | DB << QualStr << NumQuals; |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 1165 | if (Quals & Qualifiers::Const) |
| 1166 | DB << FixItHint::CreateRemoval(D.getDeclSpec().getConstSpecLoc()); |
| 1167 | if (Quals & Qualifiers::Volatile) |
| 1168 | DB << FixItHint::CreateRemoval(D.getDeclSpec().getVolatileSpecLoc()); |
| 1169 | if (Quals & Qualifiers::Restrict) |
| 1170 | DB << FixItHint::CreateRemoval(D.getDeclSpec().getRestrictSpecLoc()); |
| 1171 | } |
| 1172 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1173 | if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) { |
| 1174 | // C++ [dcl.fct]p6: |
| 1175 | // Types shall not be defined in return or parameter types. |
| 1176 | TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep()); |
| 1177 | if (Tag->isDefinition()) |
| 1178 | Diag(Tag->getLocation(), diag::err_type_defined_in_result_type) |
| 1179 | << Context.getTypeDeclType(Tag); |
| 1180 | } |
| 1181 | |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1182 | // Exception specs are not allowed in typedefs. Complain, but add it |
| 1183 | // anyway. |
| 1184 | if (FTI.hasExceptionSpec && |
| 1185 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
| 1186 | Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef); |
| 1187 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1188 | if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) { |
| 1189 | // Simple void foo(), where the incoming T is the result type. |
| 1190 | T = Context.getFunctionNoProtoType(T); |
| 1191 | } else { |
| 1192 | // We allow a zero-parameter variadic function in C if the |
| 1193 | // function is marked with the "overloadable" attribute. Scan |
| 1194 | // for this attribute now. |
| 1195 | if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) { |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 1196 | bool Overloadable = false; |
| 1197 | for (const AttributeList *Attrs = D.getAttributes(); |
| 1198 | Attrs; Attrs = Attrs->getNext()) { |
| 1199 | if (Attrs->getKind() == AttributeList::AT_overloadable) { |
| 1200 | Overloadable = true; |
| 1201 | break; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | if (!Overloadable) |
| 1206 | Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 1207 | } |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1208 | |
| 1209 | if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) { |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1210 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function |
| 1211 | // definition. |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1212 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
| 1213 | D.setInvalidType(true); |
| 1214 | break; |
| 1215 | } |
| 1216 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1217 | // Otherwise, we have a function with an argument list that is |
| 1218 | // potentially variadic. |
| 1219 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1220 | ArgTys.reserve(FTI.NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1222 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1223 | ParmVarDecl *Param = |
| 1224 | cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>()); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1225 | QualType ArgTy = Param->getType(); |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 1226 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1227 | |
| 1228 | // Adjust the parameter type. |
Douglas Gregor | beb58cb | 2009-03-23 23:17:00 +0000 | [diff] [blame] | 1229 | assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1230 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1231 | // Look for 'void'. void is allowed only as a single argument to a |
| 1232 | // function with no other parameters (C99 6.7.5.3p10). We record |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1233 | // int(void) as a FunctionProtoType with an empty argument list. |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1234 | if (ArgTy->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1235 | // If this is something like 'float(int, void)', reject it. 'void' |
| 1236 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 1237 | // have arguments of incomplete type. |
| 1238 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 1239 | Diag(DeclType.Loc, diag::err_void_only_param); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1240 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1241 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1242 | } else if (FTI.ArgInfo[i].Ident) { |
| 1243 | // Reject, but continue to parse 'int(void abc)'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1244 | Diag(FTI.ArgInfo[i].IdentLoc, |
Chris Lattner | 4565d4e | 2007-07-21 05:26:43 +0000 | [diff] [blame] | 1245 | diag::err_param_with_void_type); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1246 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1247 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1248 | } else { |
| 1249 | // Reject, but continue to parse 'float(const void)'. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1250 | if (ArgTy.hasQualifiers()) |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1251 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1253 | // Do not add 'void' to the ArgTys list. |
| 1254 | break; |
| 1255 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1256 | } else if (!FTI.hasPrototype) { |
| 1257 | if (ArgTy->isPromotableIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 1258 | ArgTy = Context.getPromotedIntegerType(ArgTy); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1259 | } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) { |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1260 | if (BTy->getKind() == BuiltinType::Float) |
| 1261 | ArgTy = Context.DoubleTy; |
| 1262 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1263 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1265 | ArgTys.push_back(ArgTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1266 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1267 | |
| 1268 | llvm::SmallVector<QualType, 4> Exceptions; |
| 1269 | Exceptions.reserve(FTI.NumExceptions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1271 | // FIXME: Preserve type source info. |
| 1272 | QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1273 | // Check that the type is valid for an exception spec, and drop it if |
| 1274 | // not. |
| 1275 | if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) |
| 1276 | Exceptions.push_back(ET); |
| 1277 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1278 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1279 | T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1280 | FTI.isVariadic, FTI.TypeQuals, |
| 1281 | FTI.hasExceptionSpec, |
| 1282 | FTI.hasAnyExceptionSpec, |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 1283 | Exceptions.size(), Exceptions.data(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1284 | FunctionType::ExtInfo()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1285 | } |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1286 | |
| 1287 | // For GCC compatibility, we allow attributes that apply only to |
| 1288 | // function types to be placed on a function's return type |
| 1289 | // instead (as long as that type doesn't happen to be function |
| 1290 | // or function-pointer itself). |
| 1291 | ProcessDelayedFnAttrs(*this, T, FnAttrsFromPreviousChunk); |
| 1292 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1293 | break; |
| 1294 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1295 | case DeclaratorChunk::MemberPointer: |
| 1296 | // The scope spec must refer to a class, or be dependent. |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1297 | QualType ClsType; |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 1298 | if (DeclType.Mem.Scope().isInvalid()) { |
| 1299 | // Avoid emitting extra errors if we already errored on the scope. |
| 1300 | D.setInvalidType(true); |
| 1301 | } else if (isDependentScopeSpecifier(DeclType.Mem.Scope()) |
| 1302 | || dyn_cast_or_null<CXXRecordDecl>( |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1303 | computeDeclContext(DeclType.Mem.Scope()))) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1304 | NestedNameSpecifier *NNS |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1305 | = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep(); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1306 | NestedNameSpecifier *NNSPrefix = NNS->getPrefix(); |
| 1307 | switch (NNS->getKind()) { |
| 1308 | case NestedNameSpecifier::Identifier: |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1309 | ClsType = Context.getDependentNameType(ETK_None, NNSPrefix, |
| 1310 | NNS->getAsIdentifier()); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1311 | break; |
| 1312 | |
| 1313 | case NestedNameSpecifier::Namespace: |
| 1314 | case NestedNameSpecifier::Global: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 1315 | llvm_unreachable("Nested-name-specifier must name a type"); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1316 | break; |
| 1317 | |
| 1318 | case NestedNameSpecifier::TypeSpec: |
| 1319 | case NestedNameSpecifier::TypeSpecWithTemplate: |
Abramo Bagnara | 22f638a | 2010-08-10 13:46:45 +0000 | [diff] [blame^] | 1320 | // Note: NNSPrefix (if any) is included in ClsType |
| 1321 | // (hence, no need to wrap ClsType in an elaborated type). |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1322 | ClsType = QualType(NNS->getAsType(), 0); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1323 | break; |
| 1324 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1325 | } else { |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1326 | Diag(DeclType.Mem.Scope().getBeginLoc(), |
| 1327 | diag::err_illegal_decl_mempointer_in_nonclass) |
| 1328 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") |
| 1329 | << DeclType.Mem.Scope().getRange(); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1330 | D.setInvalidType(true); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1333 | if (!ClsType.isNull()) |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1334 | T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier()); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1335 | if (T.isNull()) { |
| 1336 | T = Context.IntTy; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1337 | D.setInvalidType(true); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1338 | } else if (DeclType.Mem.TypeQuals) { |
| 1339 | T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1340 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1341 | break; |
| 1342 | } |
| 1343 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1344 | if (T.isNull()) { |
| 1345 | D.setInvalidType(true); |
| 1346 | T = Context.IntTy; |
| 1347 | } |
| 1348 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1349 | DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk); |
| 1350 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1351 | // See if there are any attributes on this declarator chunk. |
| 1352 | if (const AttributeList *AL = DeclType.getAttrs()) |
Charles Davis | 328ce34 | 2010-02-24 02:27:18 +0000 | [diff] [blame] | 1353 | ProcessTypeAttributeList(*this, T, false, AL, FnAttrsFromPreviousChunk); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1354 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1355 | |
| 1356 | if (getLangOptions().CPlusPlus && T->isFunctionType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1357 | const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>(); |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 1358 | assert(FnTy && "Why oh why is there not a FunctionProtoType here?"); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1359 | |
| 1360 | // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type |
| 1361 | // for a nonstatic member function, the function type to which a pointer |
| 1362 | // to member refers, or the top-level function type of a function typedef |
| 1363 | // declaration. |
Sebastian Redl | c61bb20 | 2010-07-09 21:26:08 +0000 | [diff] [blame] | 1364 | bool FreeFunction = (D.getContext() != Declarator::MemberContext && |
| 1365 | (!D.getCXXScopeSpec().isSet() || |
| 1366 | !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)->isRecord())); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1367 | if (FnTy->getTypeQuals() != 0 && |
| 1368 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Sebastian Redl | c61bb20 | 2010-07-09 21:26:08 +0000 | [diff] [blame] | 1369 | (FreeFunction || |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1370 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1371 | if (D.isFunctionDeclarator()) |
| 1372 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type); |
| 1373 | else |
| 1374 | Diag(D.getIdentifierLoc(), |
Sebastian Redl | c61bb20 | 2010-07-09 21:26:08 +0000 | [diff] [blame] | 1375 | diag::err_invalid_qualified_typedef_function_type_use) |
| 1376 | << FreeFunction; |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1377 | |
| 1378 | // Strip the cv-quals from the type. |
| 1379 | T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 1380 | FnTy->getNumArgs(), FnTy->isVariadic(), 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1381 | false, false, 0, 0, FunctionType::ExtInfo()); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | |
Sebastian Redl | 7378012 | 2010-06-09 21:19:43 +0000 | [diff] [blame] | 1385 | // If there's a constexpr specifier, treat it as a top-level const. |
| 1386 | if (D.getDeclSpec().isConstexprSpecified()) { |
| 1387 | T.addConst(); |
| 1388 | } |
| 1389 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1390 | // Process any function attributes we might have delayed from the |
| 1391 | // declaration-specifiers. |
| 1392 | ProcessDelayedFnAttrs(*this, T, FnAttrsFromDeclSpec); |
| 1393 | |
| 1394 | // If there were any type attributes applied to the decl itself, not |
| 1395 | // the type, apply them to the result type. But don't do this for |
| 1396 | // block-literal expressions, which are parsed wierdly. |
| 1397 | if (D.getContext() != Declarator::BlockLiteralContext) |
| 1398 | if (const AttributeList *Attrs = D.getAttributes()) |
Charles Davis | 328ce34 | 2010-02-24 02:27:18 +0000 | [diff] [blame] | 1399 | ProcessTypeAttributeList(*this, T, false, Attrs, |
| 1400 | FnAttrsFromPreviousChunk); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1401 | |
| 1402 | DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1403 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1404 | if (T.isNull()) |
| 1405 | return Context.getNullTypeSourceInfo(); |
| 1406 | else if (D.isInvalidType()) |
| 1407 | return Context.getTrivialTypeSourceInfo(T); |
| 1408 | return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1411 | namespace { |
| 1412 | class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> { |
| 1413 | const DeclSpec &DS; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1414 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1415 | public: |
| 1416 | TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {} |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1417 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1418 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1419 | Visit(TL.getUnqualifiedLoc()); |
| 1420 | } |
| 1421 | void VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 1422 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 1423 | } |
| 1424 | void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 1425 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1426 | } |
| 1427 | void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1428 | // Handle the base type, which might not have been written explicitly. |
| 1429 | if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { |
| 1430 | TL.setHasBaseTypeAsWritten(false); |
| 1431 | TL.getBaseLoc().initialize(SourceLocation()); |
| 1432 | } else { |
| 1433 | TL.setHasBaseTypeAsWritten(true); |
| 1434 | Visit(TL.getBaseLoc()); |
| 1435 | } |
Argyrios Kyrtzidis | eb66759 | 2009-09-29 19:45:22 +0000 | [diff] [blame] | 1436 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1437 | // Protocol qualifiers. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1438 | if (DS.getProtocolQualifiers()) { |
| 1439 | assert(TL.getNumProtocols() > 0); |
| 1440 | assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); |
| 1441 | TL.setLAngleLoc(DS.getProtocolLAngleLoc()); |
| 1442 | TL.setRAngleLoc(DS.getSourceRange().getEnd()); |
| 1443 | for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i) |
| 1444 | TL.setProtocolLoc(i, DS.getProtocolLocs()[i]); |
| 1445 | } else { |
| 1446 | assert(TL.getNumProtocols() == 0); |
| 1447 | TL.setLAngleLoc(SourceLocation()); |
| 1448 | TL.setRAngleLoc(SourceLocation()); |
| 1449 | } |
| 1450 | } |
| 1451 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1452 | TL.setStarLoc(SourceLocation()); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1453 | Visit(TL.getPointeeLoc()); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1454 | } |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1455 | void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1456 | TypeSourceInfo *TInfo = 0; |
| 1457 | Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1458 | |
| 1459 | // If we got no declarator info from previous Sema routines, |
| 1460 | // just fill with the typespec loc. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1461 | if (!TInfo) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1462 | TL.initialize(DS.getTypeSpecTypeLoc()); |
| 1463 | return; |
| 1464 | } |
| 1465 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 1466 | TypeLoc OldTL = TInfo->getTypeLoc(); |
| 1467 | if (TInfo->getType()->getAs<ElaboratedType>()) { |
| 1468 | ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL); |
| 1469 | TemplateSpecializationTypeLoc NamedTL = |
| 1470 | cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc()); |
| 1471 | TL.copy(NamedTL); |
| 1472 | } |
| 1473 | else |
| 1474 | TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL)); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1475 | } |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 1476 | void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1477 | assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr); |
| 1478 | TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); |
| 1479 | TL.setParensRange(DS.getTypeofParensRange()); |
| 1480 | } |
| 1481 | void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1482 | assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType); |
| 1483 | TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); |
| 1484 | TL.setParensRange(DS.getTypeofParensRange()); |
| 1485 | assert(DS.getTypeRep()); |
| 1486 | TypeSourceInfo *TInfo = 0; |
| 1487 | Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo); |
| 1488 | TL.setUnderlyingTInfo(TInfo); |
| 1489 | } |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 1490 | void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 1491 | // By default, use the source location of the type specifier. |
| 1492 | TL.setBuiltinLoc(DS.getTypeSpecTypeLoc()); |
| 1493 | if (TL.needsExtraLocalData()) { |
| 1494 | // Set info for the written builtin specifiers. |
| 1495 | TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs(); |
| 1496 | // Try to have a meaningful source location. |
| 1497 | if (TL.getWrittenSignSpec() != TSS_unspecified) |
| 1498 | // Sign spec loc overrides the others (e.g., 'unsigned long'). |
| 1499 | TL.setBuiltinLoc(DS.getTypeSpecSignLoc()); |
| 1500 | else if (TL.getWrittenWidthSpec() != TSW_unspecified) |
| 1501 | // Width spec loc overrides type spec loc (e.g., 'short int'). |
| 1502 | TL.setBuiltinLoc(DS.getTypeSpecWidthLoc()); |
| 1503 | } |
| 1504 | } |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 1505 | void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1506 | ElaboratedTypeKeyword Keyword |
| 1507 | = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); |
| 1508 | if (Keyword == ETK_Typename) { |
| 1509 | TypeSourceInfo *TInfo = 0; |
| 1510 | Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo); |
| 1511 | if (TInfo) { |
| 1512 | TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc())); |
| 1513 | return; |
| 1514 | } |
| 1515 | } |
| 1516 | TL.setKeywordLoc(Keyword != ETK_None |
| 1517 | ? DS.getTypeSpecTypeLoc() |
| 1518 | : SourceLocation()); |
| 1519 | const CXXScopeSpec& SS = DS.getTypeSpecScope(); |
| 1520 | TL.setQualifierRange(SS.isEmpty() ? SourceRange(): SS.getRange()); |
| 1521 | Visit(TL.getNextTypeLoc().getUnqualifiedLoc()); |
| 1522 | } |
| 1523 | void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
| 1524 | ElaboratedTypeKeyword Keyword |
| 1525 | = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); |
| 1526 | if (Keyword == ETK_Typename) { |
| 1527 | TypeSourceInfo *TInfo = 0; |
| 1528 | Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo); |
| 1529 | if (TInfo) { |
| 1530 | TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc())); |
| 1531 | return; |
| 1532 | } |
| 1533 | } |
| 1534 | TL.setKeywordLoc(Keyword != ETK_None |
| 1535 | ? DS.getTypeSpecTypeLoc() |
| 1536 | : SourceLocation()); |
| 1537 | const CXXScopeSpec& SS = DS.getTypeSpecScope(); |
| 1538 | TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange()); |
| 1539 | // FIXME: load appropriate source location. |
| 1540 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 1541 | } |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 1542 | void VisitDependentTemplateSpecializationTypeLoc( |
| 1543 | DependentTemplateSpecializationTypeLoc TL) { |
| 1544 | ElaboratedTypeKeyword Keyword |
| 1545 | = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); |
| 1546 | if (Keyword == ETK_Typename) { |
| 1547 | TypeSourceInfo *TInfo = 0; |
| 1548 | Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo); |
| 1549 | if (TInfo) { |
| 1550 | TL.copy(cast<DependentTemplateSpecializationTypeLoc>( |
| 1551 | TInfo->getTypeLoc())); |
| 1552 | return; |
| 1553 | } |
| 1554 | } |
| 1555 | TL.initializeLocal(SourceLocation()); |
| 1556 | TL.setKeywordLoc(Keyword != ETK_None |
| 1557 | ? DS.getTypeSpecTypeLoc() |
| 1558 | : SourceLocation()); |
| 1559 | const CXXScopeSpec& SS = DS.getTypeSpecScope(); |
| 1560 | TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange()); |
| 1561 | // FIXME: load appropriate source location. |
| 1562 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 1563 | } |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 1564 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1565 | void VisitTypeLoc(TypeLoc TL) { |
| 1566 | // FIXME: add other typespec types and change this to an assert. |
| 1567 | TL.initialize(DS.getTypeSpecTypeLoc()); |
| 1568 | } |
| 1569 | }; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1570 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1571 | class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> { |
| 1572 | const DeclaratorChunk &Chunk; |
| 1573 | |
| 1574 | public: |
| 1575 | DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {} |
| 1576 | |
| 1577 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 1578 | llvm_unreachable("qualified type locs not expected here!"); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1582 | assert(Chunk.Kind == DeclaratorChunk::BlockPointer); |
| 1583 | TL.setCaretLoc(Chunk.Loc); |
| 1584 | } |
| 1585 | void VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1586 | assert(Chunk.Kind == DeclaratorChunk::Pointer); |
| 1587 | TL.setStarLoc(Chunk.Loc); |
| 1588 | } |
| 1589 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 1590 | assert(Chunk.Kind == DeclaratorChunk::Pointer); |
| 1591 | TL.setStarLoc(Chunk.Loc); |
| 1592 | } |
| 1593 | void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1594 | assert(Chunk.Kind == DeclaratorChunk::MemberPointer); |
| 1595 | TL.setStarLoc(Chunk.Loc); |
| 1596 | // FIXME: nested name specifier |
| 1597 | } |
| 1598 | void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 1599 | assert(Chunk.Kind == DeclaratorChunk::Reference); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1600 | // 'Amp' is misleading: this might have been originally |
| 1601 | /// spelled with AmpAmp. |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1602 | TL.setAmpLoc(Chunk.Loc); |
| 1603 | } |
| 1604 | void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 1605 | assert(Chunk.Kind == DeclaratorChunk::Reference); |
| 1606 | assert(!Chunk.Ref.LValueRef); |
| 1607 | TL.setAmpAmpLoc(Chunk.Loc); |
| 1608 | } |
| 1609 | void VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1610 | assert(Chunk.Kind == DeclaratorChunk::Array); |
| 1611 | TL.setLBracketLoc(Chunk.Loc); |
| 1612 | TL.setRBracketLoc(Chunk.EndLoc); |
| 1613 | TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts)); |
| 1614 | } |
| 1615 | void VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
| 1616 | assert(Chunk.Kind == DeclaratorChunk::Function); |
| 1617 | TL.setLParenLoc(Chunk.Loc); |
| 1618 | TL.setRParenLoc(Chunk.EndLoc); |
| 1619 | |
| 1620 | const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1621 | for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1622 | ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1623 | TL.setArg(tpi++, Param); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1624 | } |
| 1625 | // FIXME: exception specs |
| 1626 | } |
| 1627 | |
| 1628 | void VisitTypeLoc(TypeLoc TL) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 1629 | llvm_unreachable("unsupported TypeLoc kind in declarator!"); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1630 | } |
| 1631 | }; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1634 | /// \brief Create and instantiate a TypeSourceInfo with type source information. |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1635 | /// |
| 1636 | /// \param T QualType referring to the type as written in source code. |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 1637 | /// |
| 1638 | /// \param ReturnTypeInfo For declarators whose return type does not show |
| 1639 | /// up in the normal place in the declaration specifiers (such as a C++ |
| 1640 | /// conversion function), this pointer will refer to a type source information |
| 1641 | /// for that return type. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1642 | TypeSourceInfo * |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 1643 | Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T, |
| 1644 | TypeSourceInfo *ReturnTypeInfo) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1645 | TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T); |
| 1646 | UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc(); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1647 | |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1648 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1649 | DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL); |
| 1650 | CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1651 | } |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1652 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1653 | TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL); |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 1654 | |
| 1655 | // We have source information for the return type that was not in the |
| 1656 | // declaration specifiers; copy that information into the current type |
| 1657 | // location so that it will be retained. This occurs, for example, with |
| 1658 | // a C++ conversion function, where the return type occurs within the |
| 1659 | // declarator-id rather than in the declaration specifiers. |
| 1660 | if (ReturnTypeInfo && D.getDeclSpec().getTypeSpecType() == TST_unspecified) { |
| 1661 | TypeLoc TL = ReturnTypeInfo->getTypeLoc(); |
| 1662 | assert(TL.getFullDataSize() == CurrTL.getFullDataSize()); |
| 1663 | memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize()); |
| 1664 | } |
| 1665 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1666 | return TInfo; |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1669 | /// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo. |
| 1670 | QualType Sema::CreateLocInfoType(QualType T, TypeSourceInfo *TInfo) { |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1671 | // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser |
| 1672 | // and Sema during declaration parsing. Try deallocating/caching them when |
| 1673 | // it's appropriate, instead of allocating them and keeping them around. |
| 1674 | LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1675 | new (LocT) LocInfoType(T, TInfo); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1676 | assert(LocT->getTypeClass() != T->getTypeClass() && |
| 1677 | "LocInfoType's TypeClass conflicts with an existing Type class"); |
| 1678 | return QualType(LocT, 0); |
| 1679 | } |
| 1680 | |
| 1681 | void LocInfoType::getAsStringInternal(std::string &Str, |
| 1682 | const PrintingPolicy &Policy) const { |
Argyrios Kyrtzidis | 35d44e5 | 2009-08-19 01:46:06 +0000 | [diff] [blame] | 1683 | assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*" |
| 1684 | " was used directly instead of getting the QualType through" |
| 1685 | " GetTypeFromParser"); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1688 | Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1689 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 1690 | // the parser. |
| 1691 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1693 | TagDecl *OwnedTag = 0; |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1694 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag); |
| 1695 | QualType T = TInfo->getType(); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 1696 | if (D.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1697 | return true; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1698 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1699 | if (getLangOptions().CPlusPlus) { |
| 1700 | // Check that there are no default arguments (C++ only). |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 1701 | CheckExtraCXXDefaultArguments(D); |
| 1702 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1703 | // C++0x [dcl.type]p3: |
| 1704 | // A type-specifier-seq shall not define a class or enumeration |
| 1705 | // unless it appears in the type-id of an alias-declaration |
| 1706 | // (7.1.3). |
| 1707 | if (OwnedTag && OwnedTag->isDefinition()) |
| 1708 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier) |
| 1709 | << Context.getTypeDeclType(OwnedTag); |
| 1710 | } |
| 1711 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1712 | T = CreateLocInfoType(T, TInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1713 | return T.getAsOpaquePtr(); |
| 1714 | } |
| 1715 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1716 | |
| 1717 | |
| 1718 | //===----------------------------------------------------------------------===// |
| 1719 | // Type Attribute Processing |
| 1720 | //===----------------------------------------------------------------------===// |
| 1721 | |
| 1722 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 1723 | /// specified type. The attribute contains 1 argument, the id of the address |
| 1724 | /// space for the type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1725 | static void HandleAddressSpaceTypeAttribute(QualType &Type, |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1726 | const AttributeList &Attr, Sema &S){ |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1727 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1728 | // If this type is already address space qualified, reject it. |
| 1729 | // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers |
| 1730 | // for two or more different address spaces." |
| 1731 | if (Type.getAddressSpace()) { |
| 1732 | S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1733 | Attr.setInvalid(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1734 | return; |
| 1735 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1736 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1737 | // Check the attribute arguments. |
| 1738 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 1739 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1740 | Attr.setInvalid(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1741 | return; |
| 1742 | } |
| 1743 | Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1744 | llvm::APSInt addrSpace(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1745 | if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() || |
| 1746 | !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1747 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) |
| 1748 | << ASArgExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1749 | Attr.setInvalid(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1750 | return; |
| 1751 | } |
| 1752 | |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1753 | // Bounds checking. |
| 1754 | if (addrSpace.isSigned()) { |
| 1755 | if (addrSpace.isNegative()) { |
| 1756 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative) |
| 1757 | << ASArgExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1758 | Attr.setInvalid(); |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1759 | return; |
| 1760 | } |
| 1761 | addrSpace.setIsSigned(false); |
| 1762 | } |
| 1763 | llvm::APSInt max(addrSpace.getBitWidth()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1764 | max = Qualifiers::MaxAddressSpace; |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1765 | if (addrSpace > max) { |
| 1766 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1767 | << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1768 | Attr.setInvalid(); |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1769 | return; |
| 1770 | } |
| 1771 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1773 | Type = S.Context.getAddrSpaceQualType(Type, ASIdx); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1776 | /// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the |
| 1777 | /// specified type. The attribute contains 1 argument, weak or strong. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | static void HandleObjCGCTypeAttribute(QualType &Type, |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1779 | const AttributeList &Attr, Sema &S) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1780 | if (Type.getObjCGCAttr() != Qualifiers::GCNone) { |
Fariborz Jahanian | 5934e75 | 2009-02-18 18:52:41 +0000 | [diff] [blame] | 1781 | S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1782 | Attr.setInvalid(); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1783 | return; |
| 1784 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1786 | // Check the attribute arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | if (!Attr.getParameterName()) { |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1788 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1789 | << "objc_gc" << 1; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1790 | Attr.setInvalid(); |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1791 | return; |
| 1792 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1793 | Qualifiers::GC GCAttr; |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1794 | if (Attr.getNumArgs() != 0) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1795 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1796 | Attr.setInvalid(); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1797 | return; |
| 1798 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | if (Attr.getParameterName()->isStr("weak")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1800 | GCAttr = Qualifiers::Weak; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1801 | else if (Attr.getParameterName()->isStr("strong")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1802 | GCAttr = Qualifiers::Strong; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1803 | else { |
| 1804 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 1805 | << "objc_gc" << Attr.getParameterName(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1806 | Attr.setInvalid(); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1807 | return; |
| 1808 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1810 | Type = S.Context.getObjCGCQualType(Type, GCAttr); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1813 | /// Process an individual function attribute. Returns true if the |
| 1814 | /// attribute does not make sense to apply to this type. |
| 1815 | bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr) { |
| 1816 | if (Attr.getKind() == AttributeList::AT_noreturn) { |
| 1817 | // Complain immediately if the arg count is wrong. |
| 1818 | if (Attr.getNumArgs() != 0) { |
| 1819 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1820 | Attr.setInvalid(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1821 | return false; |
| 1822 | } |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1823 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1824 | // Delay if this is not a function or pointer to block. |
| 1825 | if (!Type->isFunctionPointerType() |
| 1826 | && !Type->isBlockPointerType() |
| 1827 | && !Type->isFunctionType()) |
| 1828 | return true; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1829 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1830 | // Otherwise we can process right away. |
| 1831 | Type = S.Context.getNoReturnType(Type); |
| 1832 | return false; |
| 1833 | } |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1834 | |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1835 | if (Attr.getKind() == AttributeList::AT_regparm) { |
| 1836 | // The warning is emitted elsewhere |
| 1837 | if (Attr.getNumArgs() != 1) { |
| 1838 | return false; |
| 1839 | } |
| 1840 | |
| 1841 | // Delay if this is not a function or pointer to block. |
| 1842 | if (!Type->isFunctionPointerType() |
| 1843 | && !Type->isBlockPointerType() |
| 1844 | && !Type->isFunctionType()) |
| 1845 | return true; |
| 1846 | |
| 1847 | // Otherwise we can process right away. |
| 1848 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1849 | llvm::APSInt NumParams(32); |
| 1850 | |
| 1851 | // The warning is emitted elsewhere |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1852 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
| 1853 | !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1854 | return false; |
| 1855 | |
| 1856 | Type = S.Context.getRegParmType(Type, NumParams.getZExtValue()); |
| 1857 | return false; |
| 1858 | } |
| 1859 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1860 | // Otherwise, a calling convention. |
| 1861 | if (Attr.getNumArgs() != 0) { |
| 1862 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1863 | Attr.setInvalid(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1864 | return false; |
| 1865 | } |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 1866 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1867 | QualType T = Type; |
| 1868 | if (const PointerType *PT = Type->getAs<PointerType>()) |
| 1869 | T = PT->getPointeeType(); |
| 1870 | const FunctionType *Fn = T->getAs<FunctionType>(); |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 1871 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1872 | // Delay if the type didn't work out to a function. |
| 1873 | if (!Fn) return true; |
| 1874 | |
| 1875 | // TODO: diagnose uses of these conventions on the wrong target. |
| 1876 | CallingConv CC; |
| 1877 | switch (Attr.getKind()) { |
| 1878 | case AttributeList::AT_cdecl: CC = CC_C; break; |
| 1879 | case AttributeList::AT_fastcall: CC = CC_X86FastCall; break; |
| 1880 | case AttributeList::AT_stdcall: CC = CC_X86StdCall; break; |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 1881 | case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1882 | default: llvm_unreachable("unexpected attribute kind"); return false; |
| 1883 | } |
| 1884 | |
| 1885 | CallingConv CCOld = Fn->getCallConv(); |
Charles Davis | 064f7db | 2010-02-23 06:13:55 +0000 | [diff] [blame] | 1886 | if (S.Context.getCanonicalCallConv(CC) == |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1887 | S.Context.getCanonicalCallConv(CCOld)) { |
| 1888 | Attr.setInvalid(); |
| 1889 | return false; |
| 1890 | } |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1891 | |
| 1892 | if (CCOld != CC_Default) { |
| 1893 | // Should we diagnose reapplications of the same convention? |
| 1894 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1895 | << FunctionType::getNameForCallConv(CC) |
| 1896 | << FunctionType::getNameForCallConv(CCOld); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1897 | Attr.setInvalid(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1898 | return false; |
| 1899 | } |
| 1900 | |
| 1901 | // Diagnose the use of X86 fastcall on varargs or unprototyped functions. |
| 1902 | if (CC == CC_X86FastCall) { |
| 1903 | if (isa<FunctionNoProtoType>(Fn)) { |
| 1904 | S.Diag(Attr.getLoc(), diag::err_cconv_knr) |
| 1905 | << FunctionType::getNameForCallConv(CC); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1906 | Attr.setInvalid(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1907 | return false; |
| 1908 | } |
| 1909 | |
| 1910 | const FunctionProtoType *FnP = cast<FunctionProtoType>(Fn); |
| 1911 | if (FnP->isVariadic()) { |
| 1912 | S.Diag(Attr.getLoc(), diag::err_cconv_varargs) |
| 1913 | << FunctionType::getNameForCallConv(CC); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1914 | Attr.setInvalid(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1915 | return false; |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | Type = S.Context.getCallConvType(Type, CC); |
| 1920 | return false; |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1923 | /// HandleVectorSizeAttribute - this attribute is only applicable to integral |
| 1924 | /// and float scalars, although arrays, pointers, and function return values are |
| 1925 | /// allowed in conjunction with this construct. Aggregates with this attribute |
| 1926 | /// are invalid, even if they are of the same size as a corresponding scalar. |
| 1927 | /// The raw attribute should contain precisely 1 argument, the vector size for |
| 1928 | /// the variable, measured in bytes. If curType and rawAttr are well formed, |
| 1929 | /// this routine will return a new vector type. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1930 | static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr, |
| 1931 | Sema &S) { |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1932 | // Check the attribute arugments. |
| 1933 | if (Attr.getNumArgs() != 1) { |
| 1934 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1935 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1936 | return; |
| 1937 | } |
| 1938 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1939 | llvm::APSInt vecSize(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1940 | if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() || |
| 1941 | !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1942 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1943 | << "vector_size" << sizeExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1944 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1945 | return; |
| 1946 | } |
| 1947 | // the base type must be integer or float, and can't already be a vector. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1948 | if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) { |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1949 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1950 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1951 | return; |
| 1952 | } |
| 1953 | unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); |
| 1954 | // vecSize is specified in bytes - convert to bits. |
| 1955 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
| 1956 | |
| 1957 | // the vector size needs to be an integral multiple of the type size. |
| 1958 | if (vectorSize % typeSize) { |
| 1959 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size) |
| 1960 | << sizeExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1961 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1962 | return; |
| 1963 | } |
| 1964 | if (vectorSize == 0) { |
| 1965 | S.Diag(Attr.getLoc(), diag::err_attribute_zero_size) |
| 1966 | << sizeExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1967 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1968 | return; |
| 1969 | } |
| 1970 | |
| 1971 | // Success! Instantiate the vector type, the number of elements is > 0, and |
| 1972 | // not required to be a power of 2, unlike GCC. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1973 | CurType = S.Context.getVectorType(CurType, vectorSize/typeSize, |
| 1974 | VectorType::NotAltiVec); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1977 | void ProcessTypeAttributeList(Sema &S, QualType &Result, |
Charles Davis | 328ce34 | 2010-02-24 02:27:18 +0000 | [diff] [blame] | 1978 | bool IsDeclSpec, const AttributeList *AL, |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1979 | DelayedAttributeSet &FnAttrs) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1980 | // Scan through and apply attributes to this type where it makes sense. Some |
| 1981 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 1982 | // 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] | 1983 | // apply to the decl. Here we apply type attributes and ignore the rest. |
| 1984 | for (; AL; AL = AL->getNext()) { |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1985 | // Skip attributes that were marked to be invalid. |
| 1986 | if (AL->isInvalid()) |
| 1987 | continue; |
| 1988 | |
Abramo Bagnara | b1f1b26 | 2010-04-30 09:13:03 +0000 | [diff] [blame] | 1989 | // If this is an attribute we can handle, do so now, |
| 1990 | // otherwise, add it to the FnAttrs list for rechaining. |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 1991 | switch (AL->getKind()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1992 | default: break; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1993 | |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1994 | case AttributeList::AT_address_space: |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1995 | HandleAddressSpaceTypeAttribute(Result, *AL, S); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1996 | break; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1997 | case AttributeList::AT_objc_gc: |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1998 | HandleObjCGCTypeAttribute(Result, *AL, S); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1999 | break; |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 2000 | case AttributeList::AT_vector_size: |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2001 | HandleVectorSizeAttr(Result, *AL, S); |
| 2002 | break; |
| 2003 | |
| 2004 | case AttributeList::AT_noreturn: |
| 2005 | case AttributeList::AT_cdecl: |
| 2006 | case AttributeList::AT_fastcall: |
| 2007 | case AttributeList::AT_stdcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2008 | case AttributeList::AT_thiscall: |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 2009 | case AttributeList::AT_regparm: |
Charles Davis | 328ce34 | 2010-02-24 02:27:18 +0000 | [diff] [blame] | 2010 | // Don't process these on the DeclSpec. |
| 2011 | if (IsDeclSpec || |
| 2012 | ProcessFnAttr(S, Result, *AL)) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2013 | FnAttrs.push_back(DelayedAttribute(AL, Result)); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 2014 | break; |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 2015 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 2016 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | /// @brief Ensure that the type T is a complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2020 | /// |
| 2021 | /// This routine checks whether the type @p T is complete in any |
| 2022 | /// 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] | 2023 | /// type, returns false. If @p T is a class template specialization, |
| 2024 | /// this routine then attempts to perform class template |
| 2025 | /// instantiation. If instantiation fails, or if @p T is incomplete |
| 2026 | /// and cannot be completed, issues the diagnostic @p diag (giving it |
| 2027 | /// the type @p T) and returns true. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2028 | /// |
| 2029 | /// @param Loc The location in the source that the incomplete type |
| 2030 | /// diagnostic should refer to. |
| 2031 | /// |
| 2032 | /// @param T The type that this routine is examining for completeness. |
| 2033 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | /// @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] | 2035 | /// complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2036 | /// |
| 2037 | /// @returns @c true if @p T is incomplete and a diagnostic was emitted, |
| 2038 | /// @c false otherwise. |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 2039 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 2040 | const PartialDiagnostic &PD, |
| 2041 | std::pair<SourceLocation, |
| 2042 | PartialDiagnostic> Note) { |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 2043 | unsigned diag = PD.getDiagID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2044 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2045 | // FIXME: Add this assertion to make sure we always get instantiation points. |
| 2046 | // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType"); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 2047 | // FIXME: Add this assertion to help us flush out problems with |
| 2048 | // checking for dependent types and type-dependent expressions. |
| 2049 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2050 | // assert(!T->isDependentType() && |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 2051 | // "Can't ask whether a dependent type is complete"); |
| 2052 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2053 | // If we have a complete type, we're done. |
| 2054 | if (!T->isIncompleteType()) |
| 2055 | return false; |
Eli Friedman | 3c0eb16 | 2008-05-27 03:33:27 +0000 | [diff] [blame] | 2056 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2057 | // 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] | 2058 | // class template specialization, or an array with known size of such, |
| 2059 | // try to instantiate it. |
| 2060 | QualType MaybeTemplate = T; |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 2061 | if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T)) |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 2062 | MaybeTemplate = Array->getElementType(); |
| 2063 | if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2064 | if (ClassTemplateSpecializationDecl *ClassTemplateSpec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2065 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 2066 | if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) |
| 2067 | return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2068 | TSK_ImplicitInstantiation, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2069 | /*Complain=*/diag != 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | } else if (CXXRecordDecl *Rec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2071 | = dyn_cast<CXXRecordDecl>(Record->getDecl())) { |
| 2072 | if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2073 | MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo(); |
| 2074 | assert(MSInfo && "Missing member specialization information?"); |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 2075 | // This record was instantiated from a class within a template. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2076 | if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 2077 | != TSK_ExplicitSpecialization) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 2078 | return InstantiateClass(Loc, Rec, Pattern, |
| 2079 | getTemplateInstantiationArgs(Rec), |
| 2080 | TSK_ImplicitInstantiation, |
| 2081 | /*Complain=*/diag != 0); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 2082 | } |
| 2083 | } |
| 2084 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2085 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2086 | if (diag == 0) |
| 2087 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2088 | |
Rafael Espindola | 0162070 | 2010-03-21 22:56:43 +0000 | [diff] [blame] | 2089 | const TagType *Tag = 0; |
| 2090 | if (const RecordType *Record = T->getAs<RecordType>()) |
| 2091 | Tag = Record; |
| 2092 | else if (const EnumType *Enum = T->getAs<EnumType>()) |
| 2093 | Tag = Enum; |
| 2094 | |
| 2095 | // Avoid diagnosing invalid decls as incomplete. |
| 2096 | if (Tag && Tag->getDecl()->isInvalidDecl()) |
| 2097 | return true; |
| 2098 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2099 | // We have an incomplete type. Produce a diagnostic. |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 2100 | Diag(Loc, PD) << T; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2101 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 2102 | // If we have a note, produce it. |
| 2103 | if (!Note.first.isInvalid()) |
| 2104 | Diag(Note.first, Note.second); |
| 2105 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2106 | // If the type was a forward declaration of a class/struct/union |
Rafael Espindola | 0162070 | 2010-03-21 22:56:43 +0000 | [diff] [blame] | 2107 | // type, produce a note. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2108 | if (Tag && !Tag->getDecl()->isInvalidDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2109 | Diag(Tag->getDecl()->getLocation(), |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2110 | Tag->isBeingDefined() ? diag::note_type_being_defined |
| 2111 | : diag::note_forward_declaration) |
| 2112 | << QualType(Tag, 0); |
| 2113 | |
| 2114 | return true; |
| 2115 | } |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2117 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
| 2118 | const PartialDiagnostic &PD) { |
| 2119 | return RequireCompleteType(Loc, T, PD, |
| 2120 | std::make_pair(SourceLocation(), PDiag(0))); |
| 2121 | } |
| 2122 | |
| 2123 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
| 2124 | unsigned DiagID) { |
| 2125 | return RequireCompleteType(Loc, T, PDiag(DiagID), |
| 2126 | std::make_pair(SourceLocation(), PDiag(0))); |
| 2127 | } |
| 2128 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2129 | /// \brief Retrieve a version of the type 'T' that is elaborated by Keyword |
| 2130 | /// and qualified by the nested-name-specifier contained in SS. |
| 2131 | QualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2132 | const CXXScopeSpec &SS, QualType T) { |
| 2133 | if (T.isNull()) |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 2134 | return T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2135 | NestedNameSpecifier *NNS; |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2136 | if (SS.isValid()) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2137 | NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 2138 | else { |
| 2139 | if (Keyword == ETK_None) |
| 2140 | return T; |
| 2141 | NNS = 0; |
| 2142 | } |
| 2143 | return Context.getElaboratedType(Keyword, NNS, T); |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 2144 | } |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 2145 | |
| 2146 | QualType Sema::BuildTypeofExprType(Expr *E) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2147 | if (E->getType() == Context.OverloadTy) { |
| 2148 | // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a |
| 2149 | // function template specialization wherever deduction cannot occur. |
| 2150 | if (FunctionDecl *Specialization |
| 2151 | = ResolveSingleFunctionTemplateSpecialization(E)) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 2152 | // The access doesn't really matter in this case. |
| 2153 | DeclAccessPair Found = DeclAccessPair::make(Specialization, |
| 2154 | Specialization->getAccess()); |
| 2155 | E = FixOverloadedFunctionReference(E, Found, Specialization); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2156 | if (!E) |
| 2157 | return QualType(); |
| 2158 | } else { |
| 2159 | Diag(E->getLocStart(), |
| 2160 | diag::err_cannot_determine_declared_type_of_overloaded_function) |
| 2161 | << false << E->getSourceRange(); |
| 2162 | return QualType(); |
| 2163 | } |
| 2164 | } |
| 2165 | |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 2166 | return Context.getTypeOfExprType(E); |
| 2167 | } |
| 2168 | |
| 2169 | QualType Sema::BuildDecltypeType(Expr *E) { |
| 2170 | if (E->getType() == Context.OverloadTy) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2171 | // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a |
| 2172 | // function template specialization wherever deduction cannot occur. |
| 2173 | if (FunctionDecl *Specialization |
| 2174 | = ResolveSingleFunctionTemplateSpecialization(E)) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 2175 | // The access doesn't really matter in this case. |
| 2176 | DeclAccessPair Found = DeclAccessPair::make(Specialization, |
| 2177 | Specialization->getAccess()); |
| 2178 | E = FixOverloadedFunctionReference(E, Found, Specialization); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2179 | if (!E) |
| 2180 | return QualType(); |
| 2181 | } else { |
| 2182 | Diag(E->getLocStart(), |
| 2183 | diag::err_cannot_determine_declared_type_of_overloaded_function) |
| 2184 | << true << E->getSourceRange(); |
| 2185 | return QualType(); |
| 2186 | } |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 2187 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2188 | |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 2189 | return Context.getDecltypeType(E); |
| 2190 | } |