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