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