SemaType.cpp revision 19510856727e0e14a3696b2a72c35163bff2a71f
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements type-related semantic analysis.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Sema.h"
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
16a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor#include "clang/AST/CXXInheritance.h"
17980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
182943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor#include "clang/AST/DeclTemplate.h"
194adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
2051bd803fbdade51d674598ed45da3d54190a656cJohn McCall#include "clang/AST/TypeLocVisitor.h"
21e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
2291a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
23d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis#include "clang/Basic/TargetInfo.h"
2419510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
254994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl#include "llvm/ADT/SmallPtrSet.h"
2687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor#include "llvm/Support/ErrorHandling.h"
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
292dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// \brief Perform adjustment on the parameter type of a function.
302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor///
312dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// This routine adjusts the given parameter type @p T to the actual
321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
342dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorQualType Sema::adjustParameterType(QualType T) {
352dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // C99 6.7.5.3p7:
36778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   A declaration of a parameter as "array of type" shall be
37778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   adjusted to "qualified pointer to type", where the type
38778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   qualifiers (if any) are those specified within the [ and ] of
39778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   the array type derivation.
40778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  if (T->isArrayType())
412dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getArrayDecayedType(T);
42778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner
43778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  // C99 6.7.5.3p8:
44778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   A declaration of a parameter as "function returning type"
45778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   shall be adjusted to "pointer to function returning type", as
46778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   in 6.3.2.1.
47778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  if (T->isFunctionType())
482dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getPointerType(T);
492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
502dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return T;
512dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
522dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
535db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
545db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
555db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// isOmittedBlockReturnType - Return true if this declarator is missing a
565db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// return type because this is a omitted return type on a block literal.
578ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic bool isOmittedBlockReturnType(const Declarator &D) {
585db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getContext() != Declarator::BlockLiteralContext ||
598ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl      D.getDeclSpec().hasTypeSpecifier())
605db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return false;
615db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
625db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 0)
63a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^{ ... }
645db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
655db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 1 &&
665db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      D.getTypeObject(0).Kind == DeclaratorChunk::Function)
67a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^(int X, float Y) { ... }
685db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
695db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  return false;
705db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner}
715db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
7204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCalltypedef std::pair<const AttributeList*,QualType> DelayedAttribute;
7304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCalltypedef llvm::SmallVectorImpl<DelayedAttribute> DelayedAttributeSet;
7404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
7504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic void ProcessTypeAttributeList(Sema &S, QualType &Type,
76328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis                                     bool IsDeclSpec,
7704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                                     const AttributeList *Attrs,
7804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                                     DelayedAttributeSet &DelayedFnAttrs);
7904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr);
8004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
8104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic void ProcessDelayedFnAttrs(Sema &S, QualType &Type,
8204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                                  DelayedAttributeSet &Attrs) {
8304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  for (DelayedAttributeSet::iterator I = Attrs.begin(),
8404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall         E = Attrs.end(); I != E; ++I)
85e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    if (ProcessFnAttr(S, Type, *I->first)) {
8604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type)
8704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << I->first->getName() << I->second;
88e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      // Avoid any further processing of this attribute.
89e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      I->first->setInvalid();
90e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    }
9104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  Attrs.clear();
9204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
9304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
9404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic void DiagnoseDelayedFnAttrs(Sema &S, DelayedAttributeSet &Attrs) {
9504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  for (DelayedAttributeSet::iterator I = Attrs.begin(),
9604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall         E = Attrs.end(); I != E; ++I) {
9704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type)
9804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << I->first->getName() << I->second;
99e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Avoid any further processing of this attribute.
100e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    I->first->setInvalid();
10104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
10204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  Attrs.clear();
10304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
10404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
105930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
106930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
1075db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// \param D  the declarator containing the declaration specifier.
1085153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
1095153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
11004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic QualType ConvertDeclSpecToType(Sema &TheSema,
11104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                                      Declarator &TheDeclarator,
11204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                                      DelayedAttributeSet &Delayed) {
1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
1145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
1155db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  const DeclSpec &DS = TheDeclarator.getDeclSpec();
1165db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc();
1175db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
1185db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    DeclLoc = DS.getSourceRange().getBegin();
1191564e3906cad604a42bd131e584751a75589a9c4Chris Lattner
1201564e3906cad604a42bd131e584751a75589a9c4Chris Lattner  ASTContext &Context = TheSema.Context;
1211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1225db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
12496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
12596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
12696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
129fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
131fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
1345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
135fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
137958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
13864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
13964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
14064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
14164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
1421564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
143f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
14464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
14564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
14664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
14764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
1481564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
149f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
15064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
15164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
15264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
153f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
154f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
155f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
156f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
157f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
158f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
159f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
160f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
161f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
162f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
163d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
16462f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
165097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
166c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
167c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         (ObjCProtocolDecl**)PQ,
168c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         DS.getNumProtocolQualifiers());
169c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectPointerType(Result);
17062f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
17162f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
1725db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
1735db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
1745db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
1758ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl    if (isOmittedBlockReturnType(TheDeclarator)) {
1765db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      Result = Context.DependentTy;
1775db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
1785db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
1791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
180d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
181d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
182d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
183d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
184d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
185d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
186d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
1871564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    if (TheSema.getLangOptions().ImplicitInt) {
18835d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
18935d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
1903f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
1911564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::ext_missing_declspec)
1923f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
193849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int");
1943f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
1954310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
196d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
197d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
198d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
199d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
2004310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
2011564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      if (TheSema.getLangOptions().CPlusPlus &&
2021564e3906cad604a42bd131e584751a75589a9c4Chris Lattner          !TheSema.getLangOptions().Microsoft) {
2031564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::err_missing_type_specifier)
2043f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
2051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
206b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
207b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
208b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
2095db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner        TheDeclarator.setInvalidType(true);
210b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
2111564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier)
2123f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
213b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
214d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
2151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
2173cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
2185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
2195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
220fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
221fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
222fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
223311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
224311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
225311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
226311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
227311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        if (!TheSema.getLangOptions().C99 &&
228311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner            !TheSema.getLangOptions().CPlusPlus0x)
229311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner          TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
230311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
2325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
234fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
235fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
236fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
237311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
238311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
239311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
240311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
241311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        if (!TheSema.getLangOptions().C99 &&
242311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner            !TheSema.getLangOptions().CPlusPlus0x)
243311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner          TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
244311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
247958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
2483cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
249fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
250958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
251958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
252fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
253958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
254fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
255958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
256fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
2575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
2585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
2595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
2601564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
2618f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
2625db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    TheDeclarator.setInvalidType(true);
2638f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
26499dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
2655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
268c7621a64717203e1f7d5d79dbf548e590b32596cDouglas Gregor    TypeDecl *D
269c7621a64717203e1f7d5d79dbf548e590b32596cDouglas Gregor      = dyn_cast_or_null<TypeDecl>(static_cast<Decl *>(DS.getTypeRep()));
2706e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
2716e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
2726e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
2735db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      TheDeclarator.setInvalidType(true);
2746e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
2756e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
2766e24726524c2b51b31bb4b622aa678a46b024f42John McCall
277a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
27854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
279a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
281a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
282a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
2835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
284a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
2852191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
2862191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    // In C++, make an ElaboratedType.
2871564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    if (TheSema.getLangOptions().CPlusPlus) {
288465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      ElaboratedTypeKeyword Keyword
289465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara        = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
290465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      Result = TheSema.getElaboratedType(Keyword, DS.getTypeSpecScope(),
291465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                         Result);
2922191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    }
2935153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    if (D->isInvalidDecl())
2945db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      TheDeclarator.setInvalidType(true);
295958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
2961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2971a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
3005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
3011564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    Result = TheSema.GetTypeFromParser(DS.getTypeRep());
30227940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    if (Result.isNull())
30327940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall      TheDeclarator.setInvalidType(true);
30427940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    else if (DeclSpec::ProtocolQualifierListTy PQ
30527940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall               = DS.getProtocolQualifiers()) {
306c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
307c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // Silently drop any existing protocol qualifiers.
308c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // TODO: determine whether that's the right thing to do.
309c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjT->getNumProtocols())
310c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = ObjT->getBaseType();
311c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
312c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (DS.getNumProtocolQualifiers())
313c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = Context.getObjCObjectType(Result,
314c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             (ObjCProtocolDecl**) PQ,
315c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             DS.getNumProtocolQualifiers());
316c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCIdType()) {
317ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
318c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
319c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
320c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
321c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
322c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCClassType()) {
3234262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
324c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
325c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
326c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
327c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
3283f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
3291564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
3303f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
3315db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner        TheDeclarator.setInvalidType(true);
3323f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
333c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
3341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
336958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
338958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
339e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
3401564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    Result = TheSema.GetTypeFromParser(DS.getTypeRep());
341958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
342d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
343fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
344958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
345d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
346d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    Expr *E = static_cast<Expr *>(DS.getTypeRep());
347d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
348d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
3494b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    Result = TheSema.BuildTypeofExprType(E);
3504b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
3514b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
3524b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      TheDeclarator.setInvalidType(true);
3534b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
354958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
355d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
3566fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
3576fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    Expr *E = static_cast<Expr *>(DS.getTypeRep());
3586fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
3596fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
3601564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    Result = TheSema.BuildDecltypeType(E);
361af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
362af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
3635db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      TheDeclarator.setInvalidType(true);
364af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
3656fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
3666fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
367e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
368e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
369e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    Result = Context.UndeducedAutoTy;
370e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
371e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
3721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
373809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
3745153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
3755db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    TheDeclarator.setInvalidType(true);
3765153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
379958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
380f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
3811564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    if (TheSema.getLangOptions().Freestanding)
3821564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
383fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
38482287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
38582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
38682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
387788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    VectorType::AltiVecSpecific AltiVecSpec = VectorType::AltiVec;
388788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
389788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner      AltiVecSpec = VectorType::Pixel;
390788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
391788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner      AltiVecSpec = VectorType::Bool;
392788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    Result = Context.getVectorType(Result, 128/typeSize, AltiVecSpec);
393f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
3941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
395958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
396958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner         "FIXME: imaginary types not supported yet!");
3971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39838d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // See if there are any attributes on the declspec that apply to the type (as
39938d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // opposed to the decl).
400fca0ddd42965e0b7ae821213486d4e0dd71fb439Chris Lattner  if (const AttributeList *AL = DS.getAttributes())
401328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis    ProcessTypeAttributeList(TheSema, Result, true, AL, Delayed);
4021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
40496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
40596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
40696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
40796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
40896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
4090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (TypeQuals & DeclSpec::TQ_restrict) {
4102b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian      if (Result->isAnyPointerType() || Result->isReferenceType()) {
4112b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        QualType EltTy;
4122b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        if (Result->isObjCObjectPointerType())
4132b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result;
4142b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        else
4152b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result->isPointerType() ?
4162b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<PointerType>()->getPointeeType() :
4172b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<ReferenceType>()->getPointeeType();
4181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
419bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
420bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
421bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
4221564e3906cad604a42bd131e584751a75589a9c4Chris Lattner          TheSema.Diag(DS.getRestrictSpecLoc(),
423d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
424d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
4250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall          TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
426bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
427bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
4281564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DS.getRestrictSpecLoc(),
429d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner             diag::err_typecheck_invalid_restrict_not_pointer)
430d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
4310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
43296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
43396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
4341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
43696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
43796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
43896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
43996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
44096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
4410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
44296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
4430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
44496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
4450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
4460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        assert((TypeQuals & DeclSpec::TQ_restrict) &&
4470953e767ff7817f97b3ab20896b229891eeff45bJohn McCall               "Has CVR quals but not C, V, or R?");
4480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Loc = DS.getRestrictSpecLoc();
44996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
4501564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
451d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
45296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
4531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
454f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
455f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
456f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
457f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
458f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
4591a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
4601a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
461f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
4620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
4630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
4670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Result = Context.getQualifiedType(Result, Quals);
46896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
4690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
470f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
471f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
472f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
473cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
474cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
475cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
477cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
478cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
479cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
4802865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
4812865474261a608c7873b87ba4af110d17907896dJohn McCall                                  Qualifiers Qs) {
4822865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
4832865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
4842865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
4852865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
4862865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
4872865474261a608c7873b87ba4af110d17907896dJohn McCall
4882865474261a608c7873b87ba4af110d17907896dJohn McCall    const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
4892865474261a608c7873b87ba4af110d17907896dJohn McCall    if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
4902865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
4912865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
4922865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
4932865474261a608c7873b87ba4af110d17907896dJohn McCall      }
4942865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
4952865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
4962865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
4972865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
4982865474261a608c7873b87ba4af110d17907896dJohn McCall      }
4992865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
5002865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
5012865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
5022865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
5032865474261a608c7873b87ba4af110d17907896dJohn McCall      }
5042865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (!Ty->isDependentType()) {
5052865474261a608c7873b87ba4af110d17907896dJohn McCall      // FIXME: this deserves a proper diagnostic
5062865474261a608c7873b87ba4af110d17907896dJohn McCall      DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
5072865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
5082865474261a608c7873b87ba4af110d17907896dJohn McCall    }
5092865474261a608c7873b87ba4af110d17907896dJohn McCall
5102865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
5112865474261a608c7873b87ba4af110d17907896dJohn McCall      Diag(Loc, DiagID) << ProblemTy;
5122865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
5132865474261a608c7873b87ba4af110d17907896dJohn McCall    }
5142865474261a608c7873b87ba4af110d17907896dJohn McCall  }
5152865474261a608c7873b87ba4af110d17907896dJohn McCall
5162865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
5172865474261a608c7873b87ba4af110d17907896dJohn McCall}
5182865474261a608c7873b87ba4af110d17907896dJohn McCall
519cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
520cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
521cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
522cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
523cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
524cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
525cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
526cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
527cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
528cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
529cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
530cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
531cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
5322865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
533cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
534cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
535cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
536cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
537ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
538cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
539cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
540cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
541c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
54292e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
543cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
5442865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
545cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
546cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
547cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
548cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
549cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
550cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
551cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
552cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
553cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
554cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
555cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
556cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
557cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
558cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
559cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
56054e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
5612865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
56254e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
56354e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
56454e14c4db764c0636160d26c5bbf491637c83a76John McCall
56554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
56654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   reference to a type T, and attempt to create the type "lvalue
56754e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   reference to cv TD" creates the type "lvalue reference to T".
56854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // We use the qualifiers (restrict or none) of the original reference,
56954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // not the new ones. This is consistent with GCC.
57054e14c4db764c0636160d26c5bbf491637c83a76John McCall
57154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
57254e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
57354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
57454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
57554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
57654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
57754e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
57854e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
57954e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
58054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
58154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
58254e14c4db764c0636160d26c5bbf491637c83a76John McCall  // collapsing of references-to-references as described in C++
58354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // DR 106 and amended by C++ DR 540.
584cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
585cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
58633a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
587cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
588cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
589cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
590cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
591cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
592cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
593cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
5947c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
5952865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
5962865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
597cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
598cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
599cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
600cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
601cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
602cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
603cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
6041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
6051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
606cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
607cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
608cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// array type or, if there is no such entity, the location of the
609cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have array type.
610cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
611cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
612cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
613cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
614cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
615cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
616cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
617cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
6187e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
6190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
6207e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
621923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (getLangOptions().CPlusPlus) {
622138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
623138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
624138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   type, the (possibly cv-qualified) type void, a function type or an
625138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
626138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
627138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
628138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
629138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
630138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
631138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
632138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
633138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
634923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (T->isVoidType()) {
635923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
636923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
637923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
638138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
639138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (RequireNonAbstractType(Brackets.getBegin(), T,
640138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
641138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
642138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
643923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
644138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
645138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
646923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
647923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
648923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
649923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
650cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
651cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
652cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
653ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
654cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
655cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
657e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Loc,  diag::err_illegal_decl_array_of_auto)
659e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson      << getPrintableNameForEntity(Entity);
660e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
661e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
6621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6636217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
664cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
665cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
666cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
667cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
668c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
669c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
670c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
671cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
673cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
674cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (ArraySize && !ArraySize->isTypeDependent() &&
675cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      !ArraySize->getType()->isIntegerType()) {
676cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
677cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
678cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
679cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
6802767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
681cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
682f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
6837e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
684f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
685f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
686ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
6877e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
688cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
689923d56d436f750bc1f29db50e641078725558a1bSebastian Redl             (!T->isDependentType() && !T->isIncompleteType() &&
690923d56d436f750bc1f29db50e641078725558a1bSebastian Redl              !T->isConstantSizeType())) {
691cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Per C99, a variable array is an array with either a non-constant
692cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // size or an element type that has a non-constant-size
6937e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
694cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
695cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
696cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
697923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
698923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(ArraySize->getLocStart(),
699923d56d436f750bc1f29db50e641078725558a1bSebastian Redl           diag::err_typecheck_negative_array_size)
700923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
701923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
702923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
703923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
70402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
70502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
70602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      Diag(ArraySize->getLocStart(),
70702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
70802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
709923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
7102767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
7112767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor               !T->isIncompleteType()) {
7122767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      // Is the array too large?
7132767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
7142767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
7152767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
7162767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
7172767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
7182767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
7191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
7202767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
72146a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
722cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
723af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
724af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  if (!getLangOptions().C99) {
7250fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
7260fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
727204ce17e0cfd9bbe229627e1e5a20c3f2f587c8cDouglas Gregor      if (!T->isDependentType() &&
728204ce17e0cfd9bbe229627e1e5a20c3f2f587c8cDouglas Gregor          !Context.getBaseElementType(T)->isPODType()) {
7290fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
7300fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor          << Context.getBaseElementType(T);
7310fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
7320fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      }
733a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
734a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
735a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
736a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
737a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
7380fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
7390fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
7400fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::ext_vla);
7410fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
742043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      Diag(Loc,
743043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor           getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
744043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor                                     : diag::ext_c99_array_usage);
745cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
746cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
747cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
748cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
7499cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
7509cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
7519cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
7529cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
7531eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
7549cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
7559cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
7569cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Expr *Arg = (Expr *)ArraySize.get();
7579cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
7589cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
7599cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
7619cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
7629cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
7639cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
7649cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
7659cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
7669cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
7679cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
7689cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
7699cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
7709cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      << "ext_vector_type" << Arg->getSourceRange();
7719cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
7729cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
7731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
7759cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
7761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
7771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7789cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
7799cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
7809cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      << Arg->getSourceRange();
7819cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
7829cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
7831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7849cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!T->isDependentType())
7859cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return Context.getExtVectorType(T, vectorSize);
7861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
7899cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                AttrLoc);
7909cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
7911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
792724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
793724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
794724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
795724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
796724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
7972943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
798724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
799724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
800724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
801724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
802724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
803724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
804724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
805724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
806724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
807724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
808724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
809724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
810724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
811724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
812724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
813724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
814724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
815724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
816724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
817724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
818724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
819724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
820724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
821724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
8221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 QualType *ParamTypes,
823724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
824724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 bool Variadic, unsigned Quals,
825fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
826fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                 const FunctionType::ExtInfo &Info) {
827724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
82858408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor    Diag(Loc, diag::err_func_returning_array_function)
82958408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
830724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
831724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
8325291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
833724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
834724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
8352dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    QualType ParamType = adjustParameterType(ParamTypes[Idx]);
8362dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
837724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
838724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
839724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
840cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
84154e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
842724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
843724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
844724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
845724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
846724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
8471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
848fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                 Quals, false, false, 0, 0, Info);
849724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
851949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
852949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
853949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
854949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
8550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR Qualifiers applied to the member pointer type
856949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
857949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
858949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
859949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
860949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
8611eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
8622865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
863949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
864949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
865949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
866949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
867949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
868949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
869949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
870949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
871949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
872949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
873949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
874949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
875949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
876949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
877737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
878949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
879949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
8808d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
881ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
882949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
883949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
884949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
885949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
886949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
887949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
888949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
889949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
890949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
891949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
892949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
893949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
894949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
895949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
896d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // In the Microsoft ABI, the class is allowed to be an incomplete
897d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // type. In such cases, the compiler makes a worst-case assumption.
898d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // We make no such assumption right now, so emit an error if the
899d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // class isn't a complete type.
90020cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  if (Context.Target.getCXXABI() == CXXABI_Microsoft &&
901d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis      RequireCompleteType(Loc, Class, diag::err_incomplete_type))
902d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis    return QualType();
903d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis
9042865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
905949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
9061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9079a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
9089a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
9099a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
9109a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
9110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
9129a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
9139a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Loc The location of the entity whose type involves this
9149a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// block pointer type or, if there is no such entity, the location of the
9159a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type that will have block pointer type.
9169a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
9179a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
9189a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
9199a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
9209a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
9219a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
9222865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildBlockPointerType(QualType T,
9231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
9249a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
9250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
9269a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
9279a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
9289a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
9291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9302865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
9319a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
9329a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
933a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallQualType Sema::GetTypeFromParser(TypeTy *Ty, TypeSourceInfo **TInfo) {
934e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  QualType QT = QualType::getFromOpaquePtr(Ty);
9353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
936a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
9373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
9383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
9393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
940a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
941e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
942e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
943a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
944e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
9451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
946a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
947e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
948e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
949e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
95098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
9518ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl/// declarator to Type instances.
952402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor///
953402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
954402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// owns the declaration of a type (e.g., the definition of a struct
955402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// type), then *OwnedDecl will receive the owned declaration.
956bf1a028246d884a540aeafa38e89be59a269b072John McCall///
957bf1a028246d884a540aeafa38e89be59a269b072John McCall/// The result of this call will never be null, but the associated
958bf1a028246d884a540aeafa38e89be59a269b072John McCall/// type may be a null type if there's an unrecoverable error.
959bf1a028246d884a540aeafa38e89be59a269b072John McCallTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
960bf1a028246d884a540aeafa38e89be59a269b072John McCall                                           TagDecl **OwnedDecl) {
961930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
962930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
963930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
96405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  TypeSourceInfo *ReturnTypeInfo = 0;
96505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor
96604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  llvm::SmallVector<DelayedAttribute,4> FnAttrsFromDeclSpec;
96704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
9683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
9693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
9703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
9710486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
9723f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
97304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    T = ConvertDeclSpecToType(*this, D, FnAttrsFromDeclSpec);
9745db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
975591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
976591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor      TagDecl* Owned = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
977b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      // Owned is embedded if it was defined here, or if it is the
978b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      // very first (i.e., canonical) declaration of this tag type.
979b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      Owned->setEmbeddedInDeclarator(Owned->isDefinition() ||
980b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor                                     Owned->isCanonicalDecl());
981591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor      if (OwnedDecl) *OwnedDecl = Owned;
982591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
983930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
984930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
9853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
9860efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
9873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
988930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
98948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // "void" instead.
990930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
991930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
99248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
99348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
99448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
99548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
99605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    T = GetTypeFromParser(D.getName().ConversionFunctionId,
997bf1a028246d884a540aeafa38e89be59a269b072John McCall                          &ReturnTypeInfo);
99848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
999930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
1000f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
10011f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor  if (T.isNull())
1002bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
10031f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
1004baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  if (T == Context.UndeducedAutoTy) {
1005baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
10061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1007baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
1008baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
1009baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      assert(0 && "K&R type lists aren't allowed in C++");
1010baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1011baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
1012baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
1013baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1014baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
1015baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      switch (cast<TagDecl>(CurContext)->getTagKind()) {
1016465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Enum: assert(0 && "unhandled tag kind"); break;
1017465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
1018465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
1019465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
10201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
1021baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1022baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
1023baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 4; // Exception declaration
1024baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1025baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
1026baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 5; // Template parameter
1027baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1028baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
1029baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 6;  // Block literal
1030baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1031baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
1032baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
1033baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
1034baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
1035baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TypeNameContext:
1036baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1037baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1038baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
1039baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
1040baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
1041baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
1042baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      T = Context.IntTy;
1043baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
1044baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1045baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
10461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1047cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
1048cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
1049cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
1050cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
10511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
105204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  llvm::SmallVector<DelayedAttribute,4> FnAttrsFromPreviousChunk;
105304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
105498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
105598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
105698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
10578ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
10588ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl    DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
10605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
10615618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
10629af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
10639af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
10649af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner        Diag(DeclType.Loc, diag::err_blocks_disable);
10651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10662865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
10672865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
10682865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
10695618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
10716a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
10726a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
10736a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
10746a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
10756a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
10766a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
10776a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
1078c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) {
1079c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
10802865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
10812865474261a608c7873b87ba4af110d17907896dJohn McCall          T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
108214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
108314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
10842865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildPointerType(T, DeclType.Loc, Name);
10852865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
10862865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10880953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
10896a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
10906a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
10916a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
10926a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
10936a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
10946a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
10956a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
10962865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
10972865474261a608c7873b87ba4af110d17907896dJohn McCall
10982865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
10992865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
11002865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
11020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
11046a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
11056a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
11066a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
11076a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
11086a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
11096a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
11106a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
1111fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
111294f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
1120f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      if (ASM == ArrayType::Star &&
1121f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman          D.getContext() != Declarator::PrototypeContext) {
1122f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
1123f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
1124f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1125f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1126f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
1127f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
1128f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
11290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      T = BuildArrayType(T, ASM, ArraySize,
11300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                         Qualifiers::fromCVRMask(ATI.TypeQuals),
11317e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                         SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1134f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
11393cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
1140cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      // C99 6.7.5.3p1: The return type may not be a function or array type.
114158408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      // For conversion functions, we'll diagnose this particular error later.
114248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor      if ((T->isArrayType() || T->isFunctionType()) &&
114348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
114458408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor        Diag(DeclType.Loc, diag::err_func_returning_array_function)
114558408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor          << T->isFunctionType() << T;
1146cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        T = Context.IntTy;
1147cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        D.setInvalidType(true);
1148cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      }
1149465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
11505291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
11515291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
11525291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
11535291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          (!getLangOptions().CPlusPlus ||
11545291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor           (!T->isDependentType() && !T->isRecordType()))) {
11555291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        unsigned Quals = D.getDeclSpec().getTypeQualifiers();
1156de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        std::string QualStr;
1157de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        unsigned NumQuals = 0;
11585291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        SourceLocation Loc;
1159de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        if (Quals & Qualifiers::Const) {
11605291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          Loc = D.getDeclSpec().getConstSpecLoc();
1161de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          ++NumQuals;
1162de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          QualStr = "const";
1163de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        }
1164de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        if (Quals & Qualifiers::Volatile) {
1165de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          if (NumQuals == 0) {
1166de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            Loc = D.getDeclSpec().getVolatileSpecLoc();
1167de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            QualStr = "volatile";
1168de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          } else
1169de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            QualStr += " volatile";
1170de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          ++NumQuals;
1171de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        }
1172de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        if (Quals & Qualifiers::Restrict) {
1173de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          if (NumQuals == 0) {
1174de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            Loc = D.getDeclSpec().getRestrictSpecLoc();
1175de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            QualStr = "restrict";
1176de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          } else
1177de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            QualStr += " restrict";
1178de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          ++NumQuals;
11795291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        }
1180de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        assert(NumQuals > 0 && "No known qualifiers?");
1181de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor
11825291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        SemaDiagnosticBuilder DB = Diag(Loc, diag::warn_qual_return_type);
1183de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        DB << QualStr << NumQuals;
11845291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        if (Quals & Qualifiers::Const)
11855291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          DB << FixItHint::CreateRemoval(D.getDeclSpec().getConstSpecLoc());
11865291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        if (Quals & Qualifiers::Volatile)
11875291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          DB << FixItHint::CreateRemoval(D.getDeclSpec().getVolatileSpecLoc());
11885291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        if (Quals & Qualifiers::Restrict)
11895291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          DB << FixItHint::CreateRemoval(D.getDeclSpec().getRestrictSpecLoc());
11905291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      }
11915291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
1192402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1193402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
1194402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
1195402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1196402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        if (Tag->isDefinition())
1197402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor          Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1198402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
1199402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
1200402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
12013cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
12023cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
12033cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      if (FTI.hasExceptionSpec &&
12043cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl          D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
12053cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl        Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
12063cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
12072865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) {
12082865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
12092865474261a608c7873b87ba4af110d17907896dJohn McCall        T = Context.getFunctionNoProtoType(T);
12102865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
12112865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
12122865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
12132865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
12142865474261a608c7873b87ba4af110d17907896dJohn McCall        if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) {
1215965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
1216965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
1217965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
1218965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
1219965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
1220965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
1221965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
1222965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
1223965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
1224965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
1225965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1226c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
12272865474261a608c7873b87ba4af110d17907896dJohn McCall
12282865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
1229788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
1230788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
12312865474261a608c7873b87ba4af110d17907896dJohn McCall          Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
12322865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
12332865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
12342865474261a608c7873b87ba4af110d17907896dJohn McCall        }
12352865474261a608c7873b87ba4af110d17907896dJohn McCall
12365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
12385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
12392865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
12401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1242b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner          ParmVarDecl *Param =
1243b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner            cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
12448123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
124578c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
12462dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
12472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
1248beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor          assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
12492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
12505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
12515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
125272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
12532dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
12545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
12555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
12565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
12575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
12585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
12592ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
12608123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
12612ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
12622ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
12635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
12644565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
12652ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
12668123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
12672ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
12682ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
12690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
12702ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
12711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12722ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
12732ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
12742ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
1275eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
1276eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
1277a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
1278183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
1279eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
1280eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
1281eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
12825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
12831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
128454e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
12855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
1286465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1287465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        llvm::SmallVector<QualType, 4> Exceptions;
1288465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        Exceptions.reserve(FTI.NumExceptions);
12891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1290e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis          // FIXME: Preserve type source info.
1291e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis          QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1292ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          // Check that the type is valid for an exception spec, and drop it if
1293ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          // not.
1294ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1295ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            Exceptions.push_back(ET);
1296ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl        }
1297465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1298beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
1299465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.isVariadic, FTI.TypeQuals,
1300465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.hasExceptionSpec,
1301465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.hasAnyExceptionSpec,
1302ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                                    Exceptions.size(), Exceptions.data(),
1303264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                    FunctionType::ExtInfo());
13045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
130504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
130604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      // For GCC compatibility, we allow attributes that apply only to
130704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      // function types to be placed on a function's return type
130804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      // instead (as long as that type doesn't happen to be function
130904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      // or function-pointer itself).
131004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      ProcessDelayedFnAttrs(*this, T, FnAttrsFromPreviousChunk);
131104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
13125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
13135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1314f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
1315f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
13167bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
1317f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
13187bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
1319edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
1320edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
13217bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      } else if (isDependentScopeSpecifier(SS) ||
13227bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara                 dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS))) {
13231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
13247bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
132587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
132687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
132787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
13287bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
13294a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
133087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
133187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
133287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
133387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
13349f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
133587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
13367bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
133787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
133887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
133987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
13407bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // Note: if NNS is dependent, then its prefix (if any) is already
13417bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // included in ClsType; this does not hold if the NNS is
13427bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // nondependent: in this case (if there is indeed a prefix)
13437bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // ClsType needs to be wrapped into an elaborated type.
13447bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          if (NNSPrefix && !NNS->isDependent())
13457bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
134687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
134787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
1348f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
1349949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        Diag(DeclType.Mem.Scope().getBeginLoc(),
1350949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
1351949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1352949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
1353f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
1354f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1355f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1356949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
13572865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
1358949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
1359f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
1360949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
13612865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
13622865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
1363f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1364f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
1365f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
1366f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1367cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
1368cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
1369cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
1370cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
1371cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
137204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
137304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
1374c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
1375c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    if (const AttributeList *AL = DeclType.getAttrs())
1376328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis      ProcessTypeAttributeList(*this, T, false, AL, FnAttrsFromPreviousChunk);
13775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1378971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1379971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
1380183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
1381778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
1382971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1383971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1384971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // for a nonstatic member function, the function type to which a pointer
1385971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // to member refers, or the top-level function type of a function typedef
1386971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // declaration.
1387c61bb2056148891375bfa591fa2859b9b6ec2734Sebastian Redl    bool FreeFunction = (D.getContext() != Declarator::MemberContext &&
1388c61bb2056148891375bfa591fa2859b9b6ec2734Sebastian Redl        (!D.getCXXScopeSpec().isSet() ||
1389c61bb2056148891375bfa591fa2859b9b6ec2734Sebastian Redl         !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)->isRecord()));
1390971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    if (FnTy->getTypeQuals() != 0 &&
1391971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
1392c61bb2056148891375bfa591fa2859b9b6ec2734Sebastian Redl        (FreeFunction ||
1393971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
1394971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      if (D.isFunctionDeclarator())
1395971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1396971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      else
1397971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(),
1398c61bb2056148891375bfa591fa2859b9b6ec2734Sebastian Redl             diag::err_invalid_qualified_typedef_function_type_use)
1399c61bb2056148891375bfa591fa2859b9b6ec2734Sebastian Redl          << FreeFunction;
1400971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1401971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      // Strip the cv-quals from the type.
1402971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
1403ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                                  FnTy->getNumArgs(), FnTy->isVariadic(), 0,
1404264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                  false, false, 0, 0, FunctionType::ExtInfo());
1405971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
1406971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
14071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1408737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // If there's a constexpr specifier, treat it as a top-level const.
1409737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  if (D.getDeclSpec().isConstexprSpecified()) {
1410737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
1411737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
1412737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
141304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Process any function attributes we might have delayed from the
141404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // declaration-specifiers.
141504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  ProcessDelayedFnAttrs(*this, T, FnAttrsFromDeclSpec);
141604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
141704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // If there were any type attributes applied to the decl itself, not
141804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // the type, apply them to the result type.  But don't do this for
141904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // block-literal expressions, which are parsed wierdly.
142004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (D.getContext() != Declarator::BlockLiteralContext)
142104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (const AttributeList *Attrs = D.getAttributes())
1422328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis      ProcessTypeAttributeList(*this, T, false, Attrs,
1423328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis                               FnAttrsFromPreviousChunk);
142404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
142504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
14264adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
1427bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
1428bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
1429bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
1430bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
1431bf1a028246d884a540aeafa38e89be59a269b072John McCall  return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo);
14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
143451bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
143551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
143651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
1437f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
143851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
143951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
1440f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
144151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
144251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
144351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
144451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
144551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
144651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
144751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
144851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
1449c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
1450c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1451c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
1452c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
1453c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
1454c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.getBaseLoc().initialize(SourceLocation());
1455c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
1456c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
1457c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
1458c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
145954e14c4db764c0636160d26c5bbf491637c83a76John McCall
1460c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
146154e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
146254e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
146354e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
146454e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
146554e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
146654e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
146754e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
146854e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
146954e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
147054e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
147154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
147254e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
147351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
147454e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
147554e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
1476c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
147751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
1478833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
1479a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
1480a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1481833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1482833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
1483833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
1484a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
1485833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        TL.initialize(DS.getTypeSpecTypeLoc());
1486833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
1487833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
1488833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1489e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
1490e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
1491e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
1492e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TemplateSpecializationTypeLoc NamedTL =
1493e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
1494e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
1495e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
1496e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      else
1497e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
1498833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
1499cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1500cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
1501cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1502cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
1503cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
1504cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1505cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
1506cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1507cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
1508cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeRep());
1509cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
1510cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1511cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
1512cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
1513ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1514ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
1515ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
1516ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
1517ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
1518ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
1519ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
1520ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
1521ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
1522ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
1523ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
1524ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
1525ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
1526ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
1527ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
1528e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
1529e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
1530e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
1531e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (Keyword == ETK_Typename) {
1532e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
1533e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1534e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
1535e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
1536e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
1537e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
1538e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
1539e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setKeywordLoc(Keyword != ETK_None
1540e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       ? DS.getTypeSpecTypeLoc()
1541e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       : SourceLocation());
1542e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
1543e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setQualifierRange(SS.isEmpty() ? SourceRange(): SS.getRange());
1544e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
1545e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
1546e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
1547e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
1548e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
1549e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (Keyword == ETK_Typename) {
1550e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
1551e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1552e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
1553e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
1554e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
1555e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
1556e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
1557e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setKeywordLoc(Keyword != ETK_None
1558e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       ? DS.getTypeSpecTypeLoc()
1559e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       : SourceLocation());
1560e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
1561e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
1562e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      // FIXME: load appropriate source location.
156333500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
156433500955d731c73717af52088b7fc0e7a85681e7John McCall    }
156533500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
156633500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
156733500955d731c73717af52088b7fc0e7a85681e7John McCall      ElaboratedTypeKeyword Keyword
156833500955d731c73717af52088b7fc0e7a85681e7John McCall        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
156933500955d731c73717af52088b7fc0e7a85681e7John McCall      if (Keyword == ETK_Typename) {
157033500955d731c73717af52088b7fc0e7a85681e7John McCall        TypeSourceInfo *TInfo = 0;
157133500955d731c73717af52088b7fc0e7a85681e7John McCall        Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
157233500955d731c73717af52088b7fc0e7a85681e7John McCall        if (TInfo) {
157333500955d731c73717af52088b7fc0e7a85681e7John McCall          TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
157433500955d731c73717af52088b7fc0e7a85681e7John McCall                    TInfo->getTypeLoc()));
157533500955d731c73717af52088b7fc0e7a85681e7John McCall          return;
157633500955d731c73717af52088b7fc0e7a85681e7John McCall        }
157733500955d731c73717af52088b7fc0e7a85681e7John McCall      }
157833500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.initializeLocal(SourceLocation());
157933500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setKeywordLoc(Keyword != ETK_None
158033500955d731c73717af52088b7fc0e7a85681e7John McCall                       ? DS.getTypeSpecTypeLoc()
158133500955d731c73717af52088b7fc0e7a85681e7John McCall                       : SourceLocation());
158233500955d731c73717af52088b7fc0e7a85681e7John McCall      const CXXScopeSpec& SS = DS.getTypeSpecScope();
158333500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
158433500955d731c73717af52088b7fc0e7a85681e7John McCall      // FIXME: load appropriate source location.
1585e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeLoc());
1586e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
1587e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
158851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
158951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
159051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.initialize(DS.getTypeSpecTypeLoc());
159151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
159251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
1593eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
159451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
159551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
1596f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
159751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
159851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
15994adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
160051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
16019f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
160251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
16034adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
160451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
160551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
160651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
16074adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
160851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
160951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
161051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
16114adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
161251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
161351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
161451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
16154adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
161651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
161751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
161851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
161951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: nested name specifier
16204adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
162151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
162251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
162354e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
162454e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
162551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
162651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
162751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
162851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
162951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
163051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
163151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
163251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
163351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
163451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
163551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
163651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
163751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
163851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
163951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
164051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLParenLoc(Chunk.Loc);
164151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRParenLoc(Chunk.EndLoc);
164251bd803fbdade51d674598ed45da3d54190a656cJohn McCall
164351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
164454e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
16454adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis        ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
164654e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
16474adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
164851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
16494adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
165151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
16529f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
16534adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
165451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
165551bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
16564adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
1657a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
165851bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
165951bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
166005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
166105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
166205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
166305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
166405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
1665a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
166605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
166705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
1668a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
1669a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
167051bd803fbdade51d674598ed45da3d54190a656cJohn McCall
16718ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
167251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
167351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
16744adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
1675f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
167651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
167705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor
167805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  // We have source information for the return type that was not in the
167905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  // declaration specifiers; copy that information into the current type
168005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  // location so that it will be retained. This occurs, for example, with
168105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  // a C++ conversion function, where the return type occurs within the
168205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  // declarator-id rather than in the declaration specifiers.
168305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  if (ReturnTypeInfo && D.getDeclSpec().getTypeSpecType() == TST_unspecified) {
168405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
168505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
168605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
168705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
168805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor
1689a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
16904adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
16914adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
1692a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
1693a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallQualType Sema::CreateLocInfoType(QualType T, TypeSourceInfo *TInfo) {
16941bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
16951bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
16961bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
16971bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
1698a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
16991bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
17001bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
17011bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  return QualType(LocT, 0);
17021bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
17031bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
17041bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
17051bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
170635d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis  assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
170735d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
170835d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
17091bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
17101bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
1711cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlSema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
17125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
17135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
17145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
17151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1716402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  TagDecl *OwnedTag = 0;
1717bf1a028246d884a540aeafa38e89be59a269b072John McCall  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag);
1718bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
17195153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
1720809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
17215912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
1722402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  if (getLangOptions().CPlusPlus) {
1723402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
17246d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
17256d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
1726402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // C++0x [dcl.type]p3:
1727402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   A type-specifier-seq shall not define a class or enumeration
1728402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   unless it appears in the type-id of an alias-declaration
1729402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   (7.1.3).
1730402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    if (OwnedTag && OwnedTag->isDefinition())
1731402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1732402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        << Context.getTypeDeclType(OwnedTag);
1733402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
1734402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
1735bf1a028246d884a540aeafa38e89be59a269b072John McCall  T = CreateLocInfoType(T, TInfo);
17365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T.getAsOpaquePtr();
17375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1739c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1740c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1741c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
1742c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
1743c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
1744232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
1745232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1746c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
1747c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
17481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
1749c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
17500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1751232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
1752232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1753232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
1754232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
1755c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1756e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
1757c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1758232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
17591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1760232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
1761545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
1762f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1763e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
1764c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1765232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1766545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1767232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
1768ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
1769ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
1770dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1771dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
1772e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
1773c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1774232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1775232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
1776efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
1777efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
1778efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
1779efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1780efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
1781e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
1782efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
1783efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
1784efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
1785efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
1786efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
17870953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
1788efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
1789efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
17900953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
1791e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
1792efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
1793efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
1794efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
17951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
1796f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
1797c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
1798c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1799d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1800d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// specified type.  The attribute contains 1 argument, weak or strong.
18011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleObjCGCTypeAttribute(QualType &Type,
18023b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner                                      const AttributeList &Attr, Sema &S) {
18030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
18045934e75d98d99374f72722a69c5eefe026f35c74Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
1805e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
1806d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1807d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
18081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1809d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
18101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!Attr.getParameterName()) {
1811ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1812ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
1813e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
1814ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    return;
1815ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
18160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
1817ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (Attr.getNumArgs() != 0) {
1818d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1819e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
1820d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1821d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
18221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Attr.getParameterName()->isStr("weak"))
18230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
1824d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else if (Attr.getParameterName()->isStr("strong"))
18250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
1826d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
1827d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1828d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      << "objc_gc" << Attr.getParameterName();
1829e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
1830d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1831d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
18321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18333b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  Type = S.Context.getObjCGCQualType(Type, GCAttr);
1834d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1835d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
183658f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenekstatic QualType GetResultType(QualType T) {
183758f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenek  if (const PointerType *PT = T->getAs<PointerType>())
183858f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenek    T = PT->getPointeeType();
183958f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenek  else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
184058f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenek    T = BT->getPointeeType();
184158f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenek  return T->getAs<FunctionType>()->getResultType();
184258f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenek}
184358f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenek
184404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall/// Process an individual function attribute.  Returns true if the
184504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall/// attribute does not make sense to apply to this type.
184604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallbool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr) {
184704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (Attr.getKind() == AttributeList::AT_noreturn) {
184804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Complain immediately if the arg count is wrong.
184904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (Attr.getNumArgs() != 0) {
185004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1851e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
185204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return false;
185304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
18542455636163fdd18581d7fdae816433f886d88213Mike Stump
185504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Delay if this is not a function or pointer to block.
185604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (!Type->isFunctionPointerType()
185704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        && !Type->isBlockPointerType()
185804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        && !Type->isFunctionType())
185904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
186058f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenek
186158f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenek    if (!GetResultType(Type)->isVoidType()) {
186258f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenek      S.Diag(Attr.getLoc(), diag::warn_noreturn_function_has_nonvoid_result)
186358f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenek        << (Type->isBlockPointerType() ? /* blocks */ 1 : /* functions */ 0);
186458f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenek    }
186558f281f7d54976f23ed4fa23a10ff1ab9c7037feTed Kremenek
186604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Otherwise we can process right away.
186704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    Type = S.Context.getNoReturnType(Type);
186804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return false;
186904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
18702455636163fdd18581d7fdae816433f886d88213Mike Stump
1871425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  if (Attr.getKind() == AttributeList::AT_regparm) {
1872425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // The warning is emitted elsewhere
1873425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    if (Attr.getNumArgs() != 1) {
1874425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola      return false;
1875425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    }
1876425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
1877425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Delay if this is not a function or pointer to block.
1878425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    if (!Type->isFunctionPointerType()
1879425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola        && !Type->isBlockPointerType()
1880425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola        && !Type->isFunctionType())
1881425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola      return true;
1882425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
1883425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
1884425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1885425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    llvm::APSInt NumParams(32);
1886425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
1887425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // The warning is emitted elsewhere
1888ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor    if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
1889ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor        !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context))
1890425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola      return false;
1891425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
1892425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    Type = S.Context.getRegParmType(Type, NumParams.getZExtValue());
1893425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    return false;
1894425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
1895425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
189604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
189704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (Attr.getNumArgs() != 0) {
189804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1899e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
190004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return false;
190104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
1902f82b4e85b1219295cad4b5851b035575bc293010John McCall
190304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  QualType T = Type;
190404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (const PointerType *PT = Type->getAs<PointerType>())
190504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    T = PT->getPointeeType();
190604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  const FunctionType *Fn = T->getAs<FunctionType>();
190704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
190804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Delay if the type didn't work out to a function.
190904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!Fn) return true;
191004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
191104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // TODO: diagnose uses of these conventions on the wrong target.
191204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  CallingConv CC;
191304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  switch (Attr.getKind()) {
191404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case AttributeList::AT_cdecl: CC = CC_C; break;
191504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
191604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
1917f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor  case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
191804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  default: llvm_unreachable("unexpected attribute kind"); return false;
191904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
192004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
192104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  CallingConv CCOld = Fn->getCallConv();
1922064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis  if (S.Context.getCanonicalCallConv(CC) ==
1923e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      S.Context.getCanonicalCallConv(CCOld)) {
1924e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
1925e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    return false;
1926e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  }
192704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
192804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CCOld != CC_Default) {
192904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Should we diagnose reapplications of the same convention?
193004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
193104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CC)
193204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CCOld);
1933e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
193404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return false;
193504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
193604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
193704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
193804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
193904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (isa<FunctionNoProtoType>(Fn)) {
194004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      S.Diag(Attr.getLoc(), diag::err_cconv_knr)
194104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
1942e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
194304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return false;
194404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
194504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
194604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(Fn);
194704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
194804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      S.Diag(Attr.getLoc(), diag::err_cconv_varargs)
194904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
1950e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
195104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return false;
195204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
195304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
1954f82b4e85b1219295cad4b5851b035575bc293010John McCall
195504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  Type = S.Context.getCallConvType(Type, CC);
195604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  return false;
1957f82b4e85b1219295cad4b5851b035575bc293010John McCall}
1958f82b4e85b1219295cad4b5851b035575bc293010John McCall
19596e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
19606e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
19616e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
19626e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
19636e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
19646e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
19656e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
1966788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
1967788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
19686e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Check the attribute arugments.
19696e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
19706e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1971e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
19726e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
19736e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
19746e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
19756e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
1976ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
1977ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
19786e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
19796e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << "vector_size" << sizeExpr->getSourceRange();
1980e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
19816e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
19826e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
19836e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
1984f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
19856e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
1986e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
19876e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
19886e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
19896e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
19906e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
19916e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
19926e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
19936e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
19946e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
19956e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
19966e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
1997e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
19986e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
19996e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
20006e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
20016e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
20026e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
2003e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
20046e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
20056e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
20066e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
20076e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
20086e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
2009788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
2010788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                    VectorType::NotAltiVec);
20116e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
20126e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
201304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallvoid ProcessTypeAttributeList(Sema &S, QualType &Result,
2014328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis                              bool IsDeclSpec, const AttributeList *AL,
201504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                              DelayedAttributeSet &FnAttrs) {
2016c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
2017c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
2018c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
2019c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
2020c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  for (; AL; AL = AL->getNext()) {
2021e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
2022e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    if (AL->isInvalid())
2023e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
2024e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
2025b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
2026b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
2027c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    switch (AL->getKind()) {
2028c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
202904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2030c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
203104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      HandleAddressSpaceTypeAttribute(Result, *AL, S);
2032c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
2033d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    case AttributeList::AT_objc_gc:
203404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      HandleObjCGCTypeAttribute(Result, *AL, S);
2035d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
203604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    case AttributeList::AT_vector_size:
203704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      HandleVectorSizeAttr(Result, *AL, S);
2038f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
203904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
204004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    case AttributeList::AT_noreturn:
204104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    case AttributeList::AT_cdecl:
204204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    case AttributeList::AT_fastcall:
2043f82b4e85b1219295cad4b5851b035575bc293010John McCall    case AttributeList::AT_stdcall:
2044f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor    case AttributeList::AT_thiscall:
2045425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    case AttributeList::AT_regparm:
2046328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis      // Don't process these on the DeclSpec.
2047328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis      if (IsDeclSpec ||
2048328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis          ProcessFnAttr(S, Result, *AL))
204904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        FnAttrs.push_back(DelayedAttribute(AL, Result));
20506e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
2051c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
2052c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  }
2053232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
2054232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
20551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
20564ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
20574ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
20584ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
205986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
206086447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
206186447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
206286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
206386447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
20644ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
20654ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
20664ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
20674ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
20684ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
20694ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
20701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @param PD The partial diagnostic that will be printed out if T is not a
2071b790661a15d93941d2c33a0ea328254277b3d7e3Anders Carlsson/// complete type.
20724ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
20734ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
20744ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
207591a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
20768c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               const PartialDiagnostic &PD,
20778c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               std::pair<SourceLocation,
20788c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                                         PartialDiagnostic> Note) {
207991a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  unsigned diag = PD.getDiagID();
20801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2081573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
2082573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
2083690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
2084690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
2085690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
20861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
2087690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
2088690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
20894ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
20904ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
20914ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
20924ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
2093d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
2094923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
2095923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
2096923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
209789c49f09b0292dc7c03885f6c765d667a9837597Douglas Gregor  if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
2098923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
2099923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
21002943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
2101d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
2102972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
2103972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
2104d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
21055842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor                                                      /*Complain=*/diag != 0);
21061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
2107d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
2108d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
2109b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
2110b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        assert(MSInfo && "Missing member specialization information?");
2111357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
2112b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        if (MSInfo->getTemplateSpecializationKind()
2113972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor                                               != TSK_ExplicitSpecialization)
2114f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
2115f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
2116f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
2117f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  /*Complain=*/diag != 0);
2118d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
2119d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
2120d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
21212943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
21225842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor  if (diag == 0)
21235842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
21241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
212501620704304f819b82ecef769ec114e541a364d7Rafael Espindola  const TagType *Tag = 0;
212601620704304f819b82ecef769ec114e541a364d7Rafael Espindola  if (const RecordType *Record = T->getAs<RecordType>())
212701620704304f819b82ecef769ec114e541a364d7Rafael Espindola    Tag = Record;
212801620704304f819b82ecef769ec114e541a364d7Rafael Espindola  else if (const EnumType *Enum = T->getAs<EnumType>())
212901620704304f819b82ecef769ec114e541a364d7Rafael Espindola    Tag = Enum;
213001620704304f819b82ecef769ec114e541a364d7Rafael Espindola
213101620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // Avoid diagnosing invalid decls as incomplete.
213201620704304f819b82ecef769ec114e541a364d7Rafael Espindola  if (Tag && Tag->getDecl()->isInvalidDecl())
213301620704304f819b82ecef769ec114e541a364d7Rafael Espindola    return true;
213401620704304f819b82ecef769ec114e541a364d7Rafael Espindola
21354ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
213691a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  Diag(Loc, PD) << T;
21373c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
21388c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  // If we have a note, produce it.
21398c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  if (!Note.first.isInvalid())
21408c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson    Diag(Note.first, Note.second);
21418c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson
21424ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
214301620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
21444ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
21451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
21464ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
21474ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
21484ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
21494ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
21504ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
21514ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
2152e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
2153fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2154fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               const PartialDiagnostic &PD) {
2155fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PD,
2156fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
2157fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
2158fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
2159fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2160fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               unsigned DiagID) {
2161fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PDiag(DiagID),
2162fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
2163fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
2164fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
2165465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
2166465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
2167465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
2168465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
2169465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
2170e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
2171465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
2172e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
2173465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
2174465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
2175465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
2176465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
2177465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
2178465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
2179465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
2180e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
2181af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
2182af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders CarlssonQualType Sema::BuildTypeofExprType(Expr *E) {
21834b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  if (E->getType() == Context.OverloadTy) {
21844b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
21854b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // function template specialization wherever deduction cannot occur.
21864b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (FunctionDecl *Specialization
21874b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        = ResolveSingleFunctionTemplateSpecialization(E)) {
2188161755a09898c95d21bfff33707da9ca41cd53c5John McCall      // The access doesn't really matter in this case.
2189161755a09898c95d21bfff33707da9ca41cd53c5John McCall      DeclAccessPair Found = DeclAccessPair::make(Specialization,
2190161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                                  Specialization->getAccess());
2191161755a09898c95d21bfff33707da9ca41cd53c5John McCall      E = FixOverloadedFunctionReference(E, Found, Specialization);
21924b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      if (!E)
21934b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        return QualType();
21944b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    } else {
21954b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Diag(E->getLocStart(),
21964b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor           diag::err_cannot_determine_declared_type_of_overloaded_function)
21974b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        << false << E->getSourceRange();
21984b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      return QualType();
21994b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
22004b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  }
22014b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
2202af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
2203af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
2204af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
2205af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders CarlssonQualType Sema::BuildDecltypeType(Expr *E) {
2206af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  if (E->getType() == Context.OverloadTy) {
22074b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
22084b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // function template specialization wherever deduction cannot occur.
22094b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (FunctionDecl *Specialization
22104b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor          = ResolveSingleFunctionTemplateSpecialization(E)) {
2211161755a09898c95d21bfff33707da9ca41cd53c5John McCall      // The access doesn't really matter in this case.
2212161755a09898c95d21bfff33707da9ca41cd53c5John McCall      DeclAccessPair Found = DeclAccessPair::make(Specialization,
2213161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                                  Specialization->getAccess());
2214161755a09898c95d21bfff33707da9ca41cd53c5John McCall      E = FixOverloadedFunctionReference(E, Found, Specialization);
22154b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      if (!E)
22164b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        return QualType();
22174b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    } else {
22184b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Diag(E->getLocStart(),
22194b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor           diag::err_cannot_determine_declared_type_of_overloaded_function)
22204b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        << true << E->getSourceRange();
22214b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      return QualType();
22224b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
2223af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  }
22244b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
2225af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getDecltypeType(E);
2226af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
2227