SemaType.cpp revision b1f1b267351be74013f966f4834cde1eddbe0233
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
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "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"
23e4858a65a93fb36c099d8dd2ea0a98e33e77687eDaniel Dunbar#include "clang/Parse/DeclSpec.h"
244994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl#include "llvm/ADT/SmallPtrSet.h"
2587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor#include "llvm/Support/ErrorHandling.h"
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
282dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// \brief Perform adjustment on the parameter type of a function.
292dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor///
302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// This routine adjusts the given parameter type @p T to the actual
311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
332dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorQualType Sema::adjustParameterType(QualType T) {
342dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // C99 6.7.5.3p7:
35778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   A declaration of a parameter as "array of type" shall be
36778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   adjusted to "qualified pointer to type", where the type
37778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   qualifiers (if any) are those specified within the [ and ] of
38778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   the array type derivation.
39778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  if (T->isArrayType())
402dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getArrayDecayedType(T);
41778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner
42778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  // C99 6.7.5.3p8:
43778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   A declaration of a parameter as "function returning type"
44778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   shall be adjusted to "pointer to function returning type", as
45778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   in 6.3.2.1.
46778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  if (T->isFunctionType())
472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getPointerType(T);
482dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return T;
502dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
512dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
525db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
535db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
545db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// isOmittedBlockReturnType - Return true if this declarator is missing a
555db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// return type because this is a omitted return type on a block literal.
568ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic bool isOmittedBlockReturnType(const Declarator &D) {
575db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getContext() != Declarator::BlockLiteralContext ||
588ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl      D.getDeclSpec().hasTypeSpecifier())
595db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return false;
605db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
615db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 0)
62a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^{ ... }
635db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
645db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 1 &&
655db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      D.getTypeObject(0).Kind == DeclaratorChunk::Function)
66a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^(int X, float Y) { ... }
675db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
685db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  return false;
695db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner}
705db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
7104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCalltypedef std::pair<const AttributeList*,QualType> DelayedAttribute;
7204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCalltypedef llvm::SmallVectorImpl<DelayedAttribute> DelayedAttributeSet;
7304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
7404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic void ProcessTypeAttributeList(Sema &S, QualType &Type,
75328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis                                     bool IsDeclSpec,
7604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                                     const AttributeList *Attrs,
7704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                                     DelayedAttributeSet &DelayedFnAttrs);
7804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr);
7904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
8004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic void ProcessDelayedFnAttrs(Sema &S, QualType &Type,
8104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                                  DelayedAttributeSet &Attrs) {
8204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  for (DelayedAttributeSet::iterator I = Attrs.begin(),
8304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall         E = Attrs.end(); I != E; ++I)
8404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (ProcessFnAttr(S, Type, *I->first))
8504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type)
8604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << I->first->getName() << I->second;
8704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  Attrs.clear();
8804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
8904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
9004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic void DiagnoseDelayedFnAttrs(Sema &S, DelayedAttributeSet &Attrs) {
9104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  for (DelayedAttributeSet::iterator I = Attrs.begin(),
9204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall         E = Attrs.end(); I != E; ++I) {
9304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type)
9404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << I->first->getName() << I->second;
9504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
9604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  Attrs.clear();
9704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
9804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
99930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
100930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
1015db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// \param D  the declarator containing the declaration specifier.
1025153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
1035153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
10404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic QualType ConvertDeclSpecToType(Sema &TheSema,
10504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                                      Declarator &TheDeclarator,
10604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                                      DelayedAttributeSet &Delayed) {
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
1095db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  const DeclSpec &DS = TheDeclarator.getDeclSpec();
1105db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc();
1115db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
1125db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    DeclLoc = DS.getSourceRange().getBegin();
1131564e3906cad604a42bd131e584751a75589a9c4Chris Lattner
1141564e3906cad604a42bd131e584751a75589a9c4Chris Lattner  ASTContext &Context = TheSema.Context;
1151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1165db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
1175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
11896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
11996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
12096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
1215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
123fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
125fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
129fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
131958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
13264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
13364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
13464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
13564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
1361564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
137f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
13864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
13964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
14064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
14164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
1421564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
143f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
14464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
14564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
14664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
147f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
148f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
149f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
150f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
151f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
152f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
153f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
154f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
155f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
156f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
157d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
15862f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
159097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
1601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
16114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                                (ObjCProtocolDecl**)PQ,
162683087ffcf21d2a22cd2d0424b7f119168b47a8eSteve Naroff                                                DS.getNumProtocolQualifiers());
16362f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
16462f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
1655db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
1665db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
1675db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
1688ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl    if (isOmittedBlockReturnType(TheDeclarator)) {
1695db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      Result = Context.DependentTy;
1705db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
1715db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
1721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
173d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
174d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
175d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
176d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
177d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
178d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
179d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
1801564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    if (TheSema.getLangOptions().ImplicitInt) {
18135d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
18235d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
1833f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
1841564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::ext_missing_declspec)
1853f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
186849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int");
1873f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
1884310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
189d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
190d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
191d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
192d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
1934310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
1941564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      if (TheSema.getLangOptions().CPlusPlus &&
1951564e3906cad604a42bd131e584751a75589a9c4Chris Lattner          !TheSema.getLangOptions().Microsoft) {
1961564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::err_missing_type_specifier)
1973f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
1981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
199b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
200b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
201b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
2025db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner        TheDeclarator.setInvalidType(true);
203b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
2041564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier)
2053f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
206b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
207d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
2081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
2103cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
213fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
214fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
215fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
216311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
217311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
218311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
219311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
220311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        if (!TheSema.getLangOptions().C99 &&
221311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner            !TheSema.getLangOptions().CPlusPlus0x)
222311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner          TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
223311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
2265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
227fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
228fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
229fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
230311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
231311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
232311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
233311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
234311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        if (!TheSema.getLangOptions().C99 &&
235311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner            !TheSema.getLangOptions().CPlusPlus0x)
236311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner          TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
237311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
2385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
2395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
240958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
2413cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
242fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
243958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
244958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
245fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
246958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
247fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
248958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
249fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
2505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
2525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
2531564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
2548f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
2555db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    TheDeclarator.setInvalidType(true);
2568f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
25799dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
2585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
2595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
2605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
261c7621a64717203e1f7d5d79dbf548e590b32596cDouglas Gregor    TypeDecl *D
262c7621a64717203e1f7d5d79dbf548e590b32596cDouglas Gregor      = dyn_cast_or_null<TypeDecl>(static_cast<Decl *>(DS.getTypeRep()));
2636e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
2646e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
2656e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
2665db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      TheDeclarator.setInvalidType(true);
2676e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
2686e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
2696e24726524c2b51b31bb4b622aa678a46b024f42John McCall
270a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
27154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
272a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
274a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
275a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
2765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
277a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
2782191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
2792191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    // In C++, make an ElaboratedType.
2801564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    if (TheSema.getLangOptions().CPlusPlus) {
2812191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall      TagDecl::TagKind Tag
2822191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall        = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType());
283050b78acf11900cb8c47563376200a1d7bd97f59Douglas Gregor      Result = TheSema.getQualifiedNameType(DS.getTypeSpecScope(), Result);
2842191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall      Result = Context.getElaboratedType(Result, Tag);
2852191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    }
2861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2875153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    if (D->isInvalidDecl())
2885db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      TheDeclarator.setInvalidType(true);
289958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
2901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2911a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
2925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
2935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
2945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
2951564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    Result = TheSema.GetTypeFromParser(DS.getTypeRep());
2962ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
2971a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
298f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis      if (const ObjCInterfaceType *
299f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis            Interface = Result->getAs<ObjCInterfaceType>()) {
30067ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // It would be nice if protocol qualifiers were only stored with the
30167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // ObjCObjectPointerType. Unfortunately, this isn't possible due
30267ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // to the following typedef idiom (which is uncommon, but allowed):
3031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //
30467ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // typedef Foo<P> T;
30567ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // static void func() {
30667ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        //   Foo<P> *yy;
30767ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        //   T *zz;
30867ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // }
309c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff        Result = Context.getObjCInterfaceType(Interface->getDecl(),
310c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                              (ObjCProtocolDecl**)PQ,
311c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                              DS.getNumProtocolQualifiers());
312f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis      } else if (Result->isObjCIdType())
313ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
3141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
31514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                        (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
31614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      else if (Result->isObjCClassType()) {
3174262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
3181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy,
319470301bac9c8abfc6b451b3b669c6695a9fd1518Steve Naroff                        (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
3203f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
3211564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
3223f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
3235db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner        TheDeclarator.setInvalidType(true);
3243f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
325c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
3261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
328958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
3295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
330958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
331e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
3321564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    Result = TheSema.GetTypeFromParser(DS.getTypeRep());
333958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
334d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
335fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
336958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
337d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
338d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    Expr *E = static_cast<Expr *>(DS.getTypeRep());
339d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
340d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
3414b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    Result = TheSema.BuildTypeofExprType(E);
3424b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
3434b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
3444b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      TheDeclarator.setInvalidType(true);
3454b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
346958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
347d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
3486fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
3496fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    Expr *E = static_cast<Expr *>(DS.getTypeRep());
3506fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
3516fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
3521564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    Result = TheSema.BuildDecltypeType(E);
353af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
354af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
3555db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      TheDeclarator.setInvalidType(true);
356af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
3576fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
3586fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
359e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
360e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
361e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    Result = Context.UndeducedAutoTy;
362e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
363e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
3641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
365809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
3665153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
3675db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    TheDeclarator.setInvalidType(true);
3685153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
371958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
372f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
3731564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    if (TheSema.getLangOptions().Freestanding)
3741564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
375fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
37682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
37782287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
37882287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
37982287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Result = Context.getVectorType(Result, 128/typeSize, true,
38082287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson      DS.isTypeAltiVecPixel());
381f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
3821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
383958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
384958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner         "FIXME: imaginary types not supported yet!");
3851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38638d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // See if there are any attributes on the declspec that apply to the type (as
38738d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // opposed to the decl).
388fca0ddd42965e0b7ae821213486d4e0dd71fb439Chris Lattner  if (const AttributeList *AL = DS.getAttributes())
389328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis    ProcessTypeAttributeList(TheSema, Result, true, AL, Delayed);
3901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
39296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
39396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
39496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
39596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
39696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
3970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (TypeQuals & DeclSpec::TQ_restrict) {
3982b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian      if (Result->isAnyPointerType() || Result->isReferenceType()) {
3992b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        QualType EltTy;
4002b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        if (Result->isObjCObjectPointerType())
4012b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result;
4022b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        else
4032b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result->isPointerType() ?
4042b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<PointerType>()->getPointeeType() :
4052b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<ReferenceType>()->getPointeeType();
4061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
407bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
408bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
409bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
4101564e3906cad604a42bd131e584751a75589a9c4Chris Lattner          TheSema.Diag(DS.getRestrictSpecLoc(),
411d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
412d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
4130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall          TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
414bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
415bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
4161564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DS.getRestrictSpecLoc(),
417d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner             diag::err_typecheck_invalid_restrict_not_pointer)
418d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
4190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
42096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
42196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
42496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
42596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
42696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
42796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
42896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
4290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
43096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
4310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
43296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
4330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
4340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        assert((TypeQuals & DeclSpec::TQ_restrict) &&
4350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall               "Has CVR quals but not C, V, or R?");
4360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Loc = DS.getRestrictSpecLoc();
43796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
4381564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
439d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
44096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
4411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
442f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
443f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
444f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
445f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
446f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
4471a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
4481a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
449f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
4500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
4510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
4521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
4531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
4550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Result = Context.getQualifiedType(Result, Quals);
45696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
4570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
458f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
459f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
460f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
461cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
462cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
463cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
465cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
466cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
467cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
468cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
469cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
470cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
471cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
472cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the pointer type.
473cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
474cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
475cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
476cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
477cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
478cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
479cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
480cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
481cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
482cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
4831eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildPointerType(QualType T, unsigned Quals,
484cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
485cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
486cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
487cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
488ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
489cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
490cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
491cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
4920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
4930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
494cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
495cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
4960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) {
497cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
498cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
4990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qs.removeRestrict();
500cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
501cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
50292e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor  assert(!T->isObjCInterfaceType() && "Should build ObjCObjectPointerType");
50392e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
504cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
5050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return Context.getQualifiedType(Context.getPointerType(T), Qs);
506cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
507cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
508cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
509cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
510cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
511cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
5120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR The cvr-qualifiers to be applied to the reference type.
513cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
514cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
515cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
516cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
517cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
518cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
519cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
520cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
521cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
522cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
52354e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
52454e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  unsigned CVR, SourceLocation Loc,
52554e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
5260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
52754e14c4db764c0636160d26c5bbf491637c83a76John McCall
52854e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
52954e14c4db764c0636160d26c5bbf491637c83a76John McCall
53054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
53154e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   reference to a type T, and attempt to create the type "lvalue
53254e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   reference to cv TD" creates the type "lvalue reference to T".
53354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // We use the qualifiers (restrict or none) of the original reference,
53454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // not the new ones. This is consistent with GCC.
53554e14c4db764c0636160d26c5bbf491637c83a76John McCall
53654e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
53754e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
53854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
53954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
54054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
54154e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
54254e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
54354e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
54454e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
54554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
54654e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
54754e14c4db764c0636160d26c5bbf491637c83a76John McCall  // collapsing of references-to-references as described in C++
54854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // DR 106 and amended by C++ DR 540.
549cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
550cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
55133a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
552cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
553cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
554cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
555cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
556cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
557cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
558cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
559cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
5600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
561cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
562cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
5630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Quals.removeRestrict();
564cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
565cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
566cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
567cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   [...] Cv-qualified references are ill-formed except when the
568cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   cv-qualifiers are introduced through the use of a typedef
569cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   (7.1.3) or of a template type argument (14.3), in which case
570cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   the cv-qualifiers are ignored.
571cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //
572cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // We diagnose extraneous cv-qualifiers for the non-typedef,
573cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // non-template type argument case within the parser. Here, we just
574cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // ignore any extraneous cv-qualifiers.
5750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.removeConst();
5760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.removeVolatile();
577cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
578cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
5797c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
58054e14c4db764c0636160d26c5bbf491637c83a76John McCall    return Context.getQualifiedType(
58154e14c4db764c0636160d26c5bbf491637c83a76John McCall               Context.getLValueReferenceType(T, SpelledAsLValue), Quals);
5820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals);
583cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
584cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
585cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
586cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
587cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
588cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
589cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
5911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
592cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
593cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the array's
594cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// element type.
595cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
596cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
597cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// array type or, if there is no such entity, the location of the
598cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have array type.
599cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
600cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
601cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
602cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
603cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
604cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
605cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
606cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
6077e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
6080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
6097e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
610923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (getLangOptions().CPlusPlus) {
611138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
612138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
613138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   type, the (possibly cv-qualified) type void, a function type or an
614138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
615138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
616138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
617138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
618138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
619138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
620138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
621138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
622138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
623923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (T->isVoidType()) {
624923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
625923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
626923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
627138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
628138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (RequireNonAbstractType(Brackets.getBegin(), T,
629138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
630138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
631138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
632923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
633138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
634138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
635923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
636923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
637923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
638923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
639cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
640cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
641cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
642ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
643cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
644cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
6451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
646e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
6471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Loc,  diag::err_illegal_decl_array_of_auto)
648e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson      << getPrintableNameForEntity(Entity);
649e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
650e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6526217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
653cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
654cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
655cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
656cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
657cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (T->isObjCInterfaceType()) {
658c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
659c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
660cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
6611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
662cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
663cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (ArraySize && !ArraySize->isTypeDependent() &&
664cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      !ArraySize->getType()->isIntegerType()) {
665cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
666cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
667cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    ArraySize->Destroy(Context);
668cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
669cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
670cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  llvm::APSInt ConstVal(32);
671cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
672f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
6737e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
674f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
675f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
676cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (ArraySize->isValueDependent()) {
6777e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
678cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
679923d56d436f750bc1f29db50e641078725558a1bSebastian Redl             (!T->isDependentType() && !T->isIncompleteType() &&
680923d56d436f750bc1f29db50e641078725558a1bSebastian Redl              !T->isConstantSizeType())) {
681cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Per C99, a variable array is an array with either a non-constant
682cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // size or an element type that has a non-constant-size
6837e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
684cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
685cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
686cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
687923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
688923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(ArraySize->getLocStart(),
689923d56d436f750bc1f29db50e641078725558a1bSebastian Redl           diag::err_typecheck_negative_array_size)
690923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
691923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
692923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
693923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
69402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
69502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
69602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      Diag(ArraySize->getLocStart(),
69702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
69802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
699923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
7001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
70146a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
702cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
703af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
704af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  if (!getLangOptions().C99) {
7051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (ArraySize && !ArraySize->isTypeDependent() &&
7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        !ArraySize->isValueDependent() &&
707cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        !ArraySize->isIntegerConstantExpr(Context))
708043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
709cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    else if (ASM != ArrayType::Normal || Quals != 0)
710043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      Diag(Loc,
711043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor           getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
712043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor                                     : diag::ext_c99_array_usage);
713cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
714cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
715cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
716cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
7179cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
7189cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
7199cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
7209cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
7211eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
7229cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
7239cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
7249cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Expr *Arg = (Expr *)ArraySize.get();
7259cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
7269cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
7279cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
7281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
7299cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
7309cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
7319cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
7329cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
7339cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
7349cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
7359cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
7369cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
7379cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
7389cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      << "ext_vector_type" << Arg->getSourceRange();
7399cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
7409cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
7411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
7439cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
7441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
7451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7469cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
7479cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
7489cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      << Arg->getSourceRange();
7499cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
7509cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7529cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!T->isDependentType())
7539cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return Context.getExtVectorType(T, vectorSize);
7541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
7579cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                AttrLoc);
7589cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
7591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
760724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
761724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
762724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
763724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
764724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
7652943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
766724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
767724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
768724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
769724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
770724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
771724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
772724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
773724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
774724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
775724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
776724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
777724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
778724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
779724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
780724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
781724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
782724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
783724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
784724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
785724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
786724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
787724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
788724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
789724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
7901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 QualType *ParamTypes,
791724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
792724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 bool Variadic, unsigned Quals,
793724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 SourceLocation Loc, DeclarationName Entity) {
794724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
79558408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor    Diag(Loc, diag::err_func_returning_array_function)
79658408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
797724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
798724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
7991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
800724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
801724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
8022dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    QualType ParamType = adjustParameterType(ParamTypes[Idx]);
8032dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
804724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
805724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
806724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
807cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
80854e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
809724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
810724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
811724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
812724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
813724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
8141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
815264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                 Quals, false, false, 0, 0,
816264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                 FunctionType::ExtInfo());
817724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
8181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
819949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
820949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
821949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
822949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
8230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR Qualifiers applied to the member pointer type
824949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
825949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
826949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
827949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
828949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
8291eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
8300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                      unsigned CVR, SourceLocation Loc,
831949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
8320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
8330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
834949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
835949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
836949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
837949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
838949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
839949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
840949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
841949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
842949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
843949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
844949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
845949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
846949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
847949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
848949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
849949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
8508d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
851ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
852949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
853949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
854949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
855949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
856949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
857949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
858949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
859949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
860949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
861949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
862949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // object or incomplete types shall not be restrict-qualified."
8630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
864949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
865949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << T;
866949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
867949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
868949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
8690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Quals.removeRestrict();
870949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
871949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
872949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
873949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
874949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
875949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
876949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
8770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return Context.getQualifiedType(
8780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall           Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
879949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
8801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8819a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
8829a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8839a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
8849a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
8869a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8879a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Loc The location of the entity whose type involves this
8889a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// block pointer type or, if there is no such entity, the location of the
8899a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type that will have block pointer type.
8909a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8919a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
8929a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
8939a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8949a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
8959a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
8960953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
8971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
8989a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
8990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
9009a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
9019a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
9029a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
9050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
9069a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
9079a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
908a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallQualType Sema::GetTypeFromParser(TypeTy *Ty, TypeSourceInfo **TInfo) {
909e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  QualType QT = QualType::getFromOpaquePtr(Ty);
9103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
911a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
9123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
9133f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
9143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
915a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
916e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
917e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
918a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
919e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
9201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
921a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
922e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
923e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
924e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
92598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
9268ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl/// declarator to Type instances.
927402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor///
928402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
929402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// owns the declaration of a type (e.g., the definition of a struct
930402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// type), then *OwnedDecl will receive the owned declaration.
931a1d5662d96465f0fddf8819d245da4d19b892effArgyrios KyrtzidisQualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
932a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                    TypeSourceInfo **TInfo,
933402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor                                    TagDecl **OwnedDecl) {
934930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
935930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
936930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
93705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  TypeSourceInfo *ReturnTypeInfo = 0;
93805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor
93904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  llvm::SmallVector<DelayedAttribute,4> FnAttrsFromDeclSpec;
94004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
9413f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
9423f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
9433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
9440486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
9453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
94604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    T = ConvertDeclSpecToType(*this, D, FnAttrsFromDeclSpec);
9475db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
948591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
949591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor      TagDecl* Owned = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
950b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      // Owned is embedded if it was defined here, or if it is the
951b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      // very first (i.e., canonical) declaration of this tag type.
952b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      Owned->setEmbeddedInDeclarator(Owned->isDefinition() ||
953b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor                                     Owned->isCanonicalDecl());
954591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor      if (OwnedDecl) *OwnedDecl = Owned;
955591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
956930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
957930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
9583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
9590efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
9603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
961930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
96248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // "void" instead.
963930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
96405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor
96505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    if (TInfo)
96605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor      ReturnTypeInfo = Context.getTrivialTypeSourceInfo(T,
96705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                                    D.getName().StartLocation);
968930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
96948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
97048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
97148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
97248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
97305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    T = GetTypeFromParser(D.getName().ConversionFunctionId,
97405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                          TInfo? &ReturnTypeInfo : 0);
97548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
976930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
977f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
9781f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor  if (T.isNull())
9791f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    return T;
9801f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
981baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  if (T == Context.UndeducedAutoTy) {
982baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
9831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
984baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
985baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
986baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      assert(0 && "K&R type lists aren't allowed in C++");
987baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
988baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
989baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
990baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
991baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
992baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      switch (cast<TagDecl>(CurContext)->getTagKind()) {
993baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
994baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
995baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_union:  Error = 2; /* Union member */ break;
996baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_class:  Error = 3; /* Class member */ break;
9971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
998baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
999baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
1000baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 4; // Exception declaration
1001baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1002baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
1003baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 5; // Template parameter
1004baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1005baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
1006baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 6;  // Block literal
1007baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1008baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
1009baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
1010baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
1011baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
1012baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TypeNameContext:
1013baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1014baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1015baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
1016baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
1017baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
1018baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
1019baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      T = Context.IntTy;
1020baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
1021baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1022baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
10231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1024cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
1025cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
1026cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
1027cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
10281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
102904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  llvm::SmallVector<DelayedAttribute,4> FnAttrsFromPreviousChunk;
103004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
103198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
103298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
103398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
10348ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
10358ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl    DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
10365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
10375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
10385618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
10399af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
10409af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
10419af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner        Diag(DeclType.Loc, diag::err_blocks_disable);
10421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
10449a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                Name);
10455618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
10465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
10476a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
10486a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
10496a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
10506a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
10516a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
10526a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
10536a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
105414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
1055183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
105614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        T = Context.getObjCObjectPointerType(T,
1057cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman                                         const_cast<ObjCProtocolDecl **>(
1058cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman                                           OIT->qual_begin()),
1059a42286486c85402c65f9d30df17e6b1b037a6adeFariborz Jahanian                                         OIT->getNumProtocols(),
1060a42286486c85402c65f9d30df17e6b1b037a6adeFariborz Jahanian                                         DeclType.Ptr.TypeQuals);
106114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
106214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
1063cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
10645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
10660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Qualifiers Quals;
10670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (DeclType.Ref.HasRestrict) Quals.addRestrict();
10680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10696a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
10706a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
10716a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
10726a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
10736a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
10746a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
10756a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
10760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
1077cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                             DeclType.Loc, Name);
10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
10816a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
10826a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
10836a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
10846a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
10856a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
10866a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
10876a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
1088fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
108994f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
1097f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      if (ASM == ArrayType::Star &&
1098f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman          D.getContext() != Declarator::PrototypeContext) {
1099f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
1100f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
1101f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1102f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1103f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
1104f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
1105f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
11060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      T = BuildArrayType(T, ASM, ArraySize,
11070953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                         Qualifiers::fromCVRMask(ATI.TypeQuals),
11087e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                         SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1111f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
11163cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
1117cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      // C99 6.7.5.3p1: The return type may not be a function or array type.
111858408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      // For conversion functions, we'll diagnose this particular error later.
111948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor      if ((T->isArrayType() || T->isFunctionType()) &&
112048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
112158408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor        Diag(DeclType.Loc, diag::err_func_returning_array_function)
112258408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor          << T->isFunctionType() << T;
1123cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        T = Context.IntTy;
1124cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        D.setInvalidType(true);
1125cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      }
1126465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1127402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1128402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
1129402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
1130402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1131402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        if (Tag->isDefinition())
1132402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor          Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1133402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
1134402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
1135402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
11363cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
11373cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
11383cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      if (FTI.hasExceptionSpec &&
11393cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl          D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
11403cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl        Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
11413cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
1142eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      if (FTI.NumArgs == 0) {
1143c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        if (getLangOptions().CPlusPlus) {
1144c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1145c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // function takes no arguments.
1146465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          llvm::SmallVector<QualType, 4> Exceptions;
1147465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          Exceptions.reserve(FTI.NumExceptions);
11481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1149e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis            // FIXME: Preserve type source info.
1150e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis            QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1151ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            // Check that the type is valid for an exception spec, and drop it
1152ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            // if not.
1153ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1154ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl              Exceptions.push_back(ET);
1155ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          }
1156465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1157465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                      FTI.hasExceptionSpec,
1158465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                      FTI.hasAnyExceptionSpec,
1159ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                                      Exceptions.size(), Exceptions.data(),
1160264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                      FunctionType::ExtInfo());
1161965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor        } else if (FTI.isVariadic) {
1162965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // We allow a zero-parameter variadic function in C if the
1163965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // function is marked with the "overloadable"
1164965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // attribute. Scan for this attribute now.
1165965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
1166965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
1167965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
1168965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
1169965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
1170965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
1171965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
1172965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
1173965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
1174965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
1175965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1176ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0,
1177264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                      false, false, 0, 0,
1178264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                      FunctionType::ExtInfo());
1179c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        } else {
1180c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // Simple void foo(), where the incoming T is the result type.
118172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          T = Context.getFunctionNoProtoType(T);
1182c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
1183eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      } else if (FTI.ArgInfo[0].Param == 0) {
11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
11851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
118654e14c4db764c0636160d26c5bbf491637c83a76John McCall        D.setInvalidType(true);
11875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else {
11885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
11905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
11911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1193b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner          ParmVarDecl *Param =
1194b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner            cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
11958123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
119678c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
11972dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
11982dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
1199beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor          assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
12002dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
12015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
12025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
120372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
12042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
12085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
12095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
12102ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
12118123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
12122ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
12132ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
12145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
12154565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
12162ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
12178123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
12182ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
12192ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
12200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
12212ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
12221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12232ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
12242ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
12252ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
1226eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
1227eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
1228a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
1229183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
1230eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
1231eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
1232eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
12335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
12341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
123554e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
12365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
1237465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1238465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        llvm::SmallVector<QualType, 4> Exceptions;
1239465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        Exceptions.reserve(FTI.NumExceptions);
12401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1241e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis          // FIXME: Preserve type source info.
1242e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis          QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1243ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          // Check that the type is valid for an exception spec, and drop it if
1244ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          // not.
1245ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1246ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            Exceptions.push_back(ET);
1247ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl        }
1248465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1249beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
1250465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.isVariadic, FTI.TypeQuals,
1251465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.hasExceptionSpec,
1252465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.hasAnyExceptionSpec,
1253ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                                    Exceptions.size(), Exceptions.data(),
1254264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                    FunctionType::ExtInfo());
12555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
125604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
125704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      // For GCC compatibility, we allow attributes that apply only to
125804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      // function types to be placed on a function's return type
125904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      // instead (as long as that type doesn't happen to be function
126004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      // or function-pointer itself).
126104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      ProcessDelayedFnAttrs(*this, T, FnAttrsFromPreviousChunk);
126204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
12635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
12645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1265f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
12664994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      // Verify that we're not building a pointer to pointer to function with
12674994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      // exception specification.
12684994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
12694994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
12704994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl        D.setInvalidType(true);
12714994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl        // Build the type anyway.
12724994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      }
1273f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
1274f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
1275edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin      if (DeclType.Mem.Scope().isInvalid()) {
1276edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
1277edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
1278edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin      } else if (isDependentScopeSpecifier(DeclType.Mem.Scope())
1279edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin                 || dyn_cast_or_null<CXXRecordDecl>(
128087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor                                   computeDeclContext(DeclType.Mem.Scope()))) {
12811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
1282949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
128387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
128487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
128587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
12864a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
12874a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
128887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
128987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
129087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
129187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
12929f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
129387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
129487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
129587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
129687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
129787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
129887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          if (NNSPrefix)
129987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor            ClsType = Context.getQualifiedNameType(NNSPrefix, ClsType);
130087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
130187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
1302f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
1303949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        Diag(DeclType.Mem.Scope().getBeginLoc(),
1304949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
1305949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1306949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
1307f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
1308f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1309f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1310949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
1311949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1312949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                   DeclType.Loc, D.getIdentifier());
1313949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
1314f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
1315949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
1316f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1317f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
1318f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
1319f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1320cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
1321cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
1322cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
1323cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
1324cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
132504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
132604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
1327c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
1328c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    if (const AttributeList *AL = DeclType.getAttrs())
1329328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis      ProcessTypeAttributeList(*this, T, false, AL, FnAttrsFromPreviousChunk);
13305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1331971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1332971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
1333183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
1334778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
1335971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1336971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1337971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // for a nonstatic member function, the function type to which a pointer
1338971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // to member refers, or the top-level function type of a function typedef
1339971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // declaration.
1340971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    if (FnTy->getTypeQuals() != 0 &&
1341971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
1342584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor        ((D.getContext() != Declarator::MemberContext &&
1343584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor          (!D.getCXXScopeSpec().isSet() ||
1344f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor           !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1345f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor              ->isRecord())) ||
1346971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
1347971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      if (D.isFunctionDeclarator())
1348971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1349971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      else
1350971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(),
1351971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis             diag::err_invalid_qualified_typedef_function_type_use);
1352971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1353971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      // Strip the cv-quals from the type.
1354971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
1355ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                                  FnTy->getNumArgs(), FnTy->isVariadic(), 0,
1356264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                  false, false, 0, 0, FunctionType::ExtInfo());
1357971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
1358971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
13591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
136004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Process any function attributes we might have delayed from the
136104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // declaration-specifiers.
136204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  ProcessDelayedFnAttrs(*this, T, FnAttrsFromDeclSpec);
136304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
136404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // If there were any type attributes applied to the decl itself, not
136504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // the type, apply them to the result type.  But don't do this for
136604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // block-literal expressions, which are parsed wierdly.
136704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (D.getContext() != Declarator::BlockLiteralContext)
136804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (const AttributeList *Attrs = D.getAttributes())
1369328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis      ProcessTypeAttributeList(*this, T, false, Attrs,
1370328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis                               FnAttrsFromPreviousChunk);
137104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
137204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
13734adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
1374a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) {
137554e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (D.isInvalidType())
1376a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      *TInfo = 0;
137754e14c4db764c0636160d26c5bbf491637c83a76John McCall    else
137805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor      *TInfo = GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo);
137954e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
13804adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
13815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T;
13825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
138451bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
138551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
138651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
1387f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
138851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
138951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
1390f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
139151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
139251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
139351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
139451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
139551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
139651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
139751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
139851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
139954e14c4db764c0636160d26c5bbf491637c83a76John McCall
140054e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
140154e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
140254e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
140354e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
140454e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
140554e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
140654e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
140754e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
140854e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
140954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
141054e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
141154e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
141251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
141354e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
141451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
141551bd803fbdade51d674598ed45da3d54190a656cJohn McCall
141654e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
141754e14c4db764c0636160d26c5bbf491637c83a76John McCall
141854e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
141954e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
142054e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
142154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setHasProtocolsAsWritten(true);
142254e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
142354e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
142454e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
142554e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
142654e14c4db764c0636160d26c5bbf491637c83a76John McCall
142754e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
142854e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
142954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setHasProtocolsAsWritten(false);
143054e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
143154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
143254e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
143354e14c4db764c0636160d26c5bbf491637c83a76John McCall
143454e14c4db764c0636160d26c5bbf491637c83a76John McCall      // This might not have been written with an inner type.
143554e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
143654e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setHasBaseTypeAsWritten(false);
143754e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.getBaseTypeLoc().initialize(SourceLocation());
143854e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
143954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setHasBaseTypeAsWritten(true);
144051bd803fbdade51d674598ed45da3d54190a656cJohn McCall        Visit(TL.getBaseTypeLoc());
144154e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
144251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
1443833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
1444a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
1445a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1446833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1447833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
1448833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
1449a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
1450833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        TL.initialize(DS.getTypeSpecTypeLoc());
1451833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
1452833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
1453833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1454833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      TemplateSpecializationTypeLoc OldTL =
1455a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall        cast<TemplateSpecializationTypeLoc>(TInfo->getTypeLoc());
1456833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      TL.copy(OldTL);
1457833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
1458cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1459cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
1460cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1461cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
1462cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
1463cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1464cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
1465cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1466cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
1467cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeRep());
1468cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
1469cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1470cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
1471cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
1472ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1473ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
1474ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
1475ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
1476ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
1477ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
1478ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
1479ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
1480ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
1481ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
1482ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
1483ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
1484ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
1485ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
1486ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
148751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
148851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
148951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.initialize(DS.getTypeSpecTypeLoc());
149051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
149151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
1492eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
149351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
149451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
1495f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
149651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
149751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
14984adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
149951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
15009f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
150151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
15024adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
150351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
150451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
150551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
15064adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
150751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
150851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
150951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
15104adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
151151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
151251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
151351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
151454e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setHasBaseTypeAsWritten(true);
151554e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setHasProtocolsAsWritten(false);
151654e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setLAngleLoc(SourceLocation());
151754e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setRAngleLoc(SourceLocation());
15184adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
151951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
152051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
152151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
152251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: nested name specifier
15234adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
152451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
152551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
152654e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
152754e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
152851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
152951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
153051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
153151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
153251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
153351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
153451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
153551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
153651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
153751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
153851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
153951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
154051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
154151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
154251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
154351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLParenLoc(Chunk.Loc);
154451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRParenLoc(Chunk.EndLoc);
154551bd803fbdade51d674598ed45da3d54190a656cJohn McCall
154651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
154754e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
15484adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis        ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
154954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
15504adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
155151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
15524adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
15531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
155451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
15559f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
15564adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
155751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
155851bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
15594adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
1560a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
156151bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
156251bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
156305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
156405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
156505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
156605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
156705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
1568a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
156905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
157005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
1571a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
1572a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
157351bd803fbdade51d674598ed45da3d54190a656cJohn McCall
15748ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
157551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
157651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
15774adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
1578f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
157951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
158005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor
158105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  // We have source information for the return type that was not in the
158205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  // declaration specifiers; copy that information into the current type
158305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  // location so that it will be retained. This occurs, for example, with
158405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  // a C++ conversion function, where the return type occurs within the
158505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  // declarator-id rather than in the declaration specifiers.
158605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  if (ReturnTypeInfo && D.getDeclSpec().getTypeSpecType() == TST_unspecified) {
158705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
158805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
158905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
159005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
159105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor
1592a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
15934adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
15944adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
1595a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
1596a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallQualType Sema::CreateLocInfoType(QualType T, TypeSourceInfo *TInfo) {
15971bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
15981bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
15991bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
16001bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
1601a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
16021bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
16031bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
16041bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  return QualType(LocT, 0);
16051bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
16061bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
16071bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
16081bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
160935d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis  assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
161035d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
161135d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
16121bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
16131bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
16149e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
16159e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
16169e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// they point to and return true. If T1 and T2 aren't pointer types
16179e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// or pointer-to-member types, or if they are not similar at this
16189e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// level, returns false and leaves T1 and T2 unchanged. Top-level
16199e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// qualifiers on T1 and T2 are ignored. This function will typically
16209e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// be called in a loop that successively "unwraps" pointer and
16219e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// pointer-to-member types to compare them at each level.
1622ecb81f28cb279b7d8e84296445a4131fa80b69a9Chris Lattnerbool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
16236217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const PointerType *T1PtrType = T1->getAs<PointerType>(),
16246217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek                    *T2PtrType = T2->getAs<PointerType>();
162557373266011f73418381b736015d8d2bb0381176Douglas Gregor  if (T1PtrType && T2PtrType) {
162657373266011f73418381b736015d8d2bb0381176Douglas Gregor    T1 = T1PtrType->getPointeeType();
162757373266011f73418381b736015d8d2bb0381176Douglas Gregor    T2 = T2PtrType->getPointeeType();
162857373266011f73418381b736015d8d2bb0381176Douglas Gregor    return true;
162957373266011f73418381b736015d8d2bb0381176Douglas Gregor  }
163057373266011f73418381b736015d8d2bb0381176Douglas Gregor
16316217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
16326217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek                          *T2MPType = T2->getAs<MemberPointerType>();
163321593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl  if (T1MPType && T2MPType &&
163421593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T1MPType->getClass()) ==
163521593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T2MPType->getClass())) {
16364433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T1 = T1MPType->getPointeeType();
16374433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T2 = T2MPType->getPointeeType();
16384433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    return true;
16394433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  }
164057373266011f73418381b736015d8d2bb0381176Douglas Gregor  return false;
164157373266011f73418381b736015d8d2bb0381176Douglas Gregor}
164257373266011f73418381b736015d8d2bb0381176Douglas Gregor
1643cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlSema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
16445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
16455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
16465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
16471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1648a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = 0;
1649402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  TagDecl *OwnedTag = 0;
1650a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  QualType T = GetTypeForDeclarator(D, S, &TInfo, &OwnedTag);
16515153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
1652809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
16535912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
1654402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  if (getLangOptions().CPlusPlus) {
1655402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
16566d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
16576d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
1658402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // C++0x [dcl.type]p3:
1659402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   A type-specifier-seq shall not define a class or enumeration
1660402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   unless it appears in the type-id of an alias-declaration
1661402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   (7.1.3).
1662402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    if (OwnedTag && OwnedTag->isDefinition())
1663402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1664402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        << Context.getTypeDeclType(OwnedTag);
1665402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
1666402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
1667a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo)
1668a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    T = CreateLocInfoType(T, TInfo);
1669e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
16705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T.getAsOpaquePtr();
16715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1673c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1674c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1675c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
1676c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
1677c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
1678232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
1679232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1680c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
1681c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
16821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
1683c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
16840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1685232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
1686232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1687232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
1688232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
1689c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1690c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1691232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
16921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1693232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
1694545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
1695f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1696c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1697232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1698545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1699232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
1700c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
1701dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1702dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
1703c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1704232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1705232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
1706efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
1707efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
1708efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
1709efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1710efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
1711efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
1712efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
1713efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
1714efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
1715efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
17160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
1717efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
1718efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
17190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
1720efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
1721efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
1722efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
17231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
1724f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
1725c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
1726c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1727d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1728d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// specified type.  The attribute contains 1 argument, weak or strong.
17291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleObjCGCTypeAttribute(QualType &Type,
17303b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner                                      const AttributeList &Attr, Sema &S) {
17310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
17325934e75d98d99374f72722a69c5eefe026f35c74Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
1733d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1734d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
17351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1736d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
17371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!Attr.getParameterName()) {
1738ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1739ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
1740ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    return;
1741ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
17420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
1743ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (Attr.getNumArgs() != 0) {
1744d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1745d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1746d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
17471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Attr.getParameterName()->isStr("weak"))
17480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
1749d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else if (Attr.getParameterName()->isStr("strong"))
17500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
1751d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
1752d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1753d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      << "objc_gc" << Attr.getParameterName();
1754d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1755d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
17561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17573b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  Type = S.Context.getObjCGCQualType(Type, GCAttr);
1758d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1759d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
176004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall/// Process an individual function attribute.  Returns true if the
176104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall/// attribute does not make sense to apply to this type.
176204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallbool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr) {
176304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (Attr.getKind() == AttributeList::AT_noreturn) {
176404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Complain immediately if the arg count is wrong.
176504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (Attr.getNumArgs() != 0) {
176604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
176704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return false;
176804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
17692455636163fdd18581d7fdae816433f886d88213Mike Stump
177004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Delay if this is not a function or pointer to block.
177104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (!Type->isFunctionPointerType()
177204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        && !Type->isBlockPointerType()
177304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        && !Type->isFunctionType())
177404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
17752455636163fdd18581d7fdae816433f886d88213Mike Stump
177604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Otherwise we can process right away.
177704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    Type = S.Context.getNoReturnType(Type);
177804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return false;
177904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
17802455636163fdd18581d7fdae816433f886d88213Mike Stump
1781425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  if (Attr.getKind() == AttributeList::AT_regparm) {
1782425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // The warning is emitted elsewhere
1783425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    if (Attr.getNumArgs() != 1) {
1784425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola      return false;
1785425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    }
1786425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
1787425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Delay if this is not a function or pointer to block.
1788425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    if (!Type->isFunctionPointerType()
1789425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola        && !Type->isBlockPointerType()
1790425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola        && !Type->isFunctionType())
1791425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola      return true;
1792425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
1793425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
1794425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1795425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    llvm::APSInt NumParams(32);
1796425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
1797425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // The warning is emitted elsewhere
1798425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context))
1799425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola      return false;
1800425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
1801425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    Type = S.Context.getRegParmType(Type, NumParams.getZExtValue());
1802425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    return false;
1803425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
1804425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
180504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
180604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (Attr.getNumArgs() != 0) {
180704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
180804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return false;
180904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
1810f82b4e85b1219295cad4b5851b035575bc293010John McCall
181104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  QualType T = Type;
181204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (const PointerType *PT = Type->getAs<PointerType>())
181304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    T = PT->getPointeeType();
181404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  const FunctionType *Fn = T->getAs<FunctionType>();
181504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
181604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Delay if the type didn't work out to a function.
181704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!Fn) return true;
181804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
181904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // TODO: diagnose uses of these conventions on the wrong target.
182004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  CallingConv CC;
182104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  switch (Attr.getKind()) {
182204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case AttributeList::AT_cdecl: CC = CC_C; break;
182304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
182404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
182504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  default: llvm_unreachable("unexpected attribute kind"); return false;
182604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
182704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
182804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  CallingConv CCOld = Fn->getCallConv();
1829064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis  if (S.Context.getCanonicalCallConv(CC) ==
1830064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis      S.Context.getCanonicalCallConv(CCOld)) return false;
183104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
183204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CCOld != CC_Default) {
183304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Should we diagnose reapplications of the same convention?
183404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
183504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CC)
183604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CCOld);
183704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return false;
183804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
183904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
184004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
184104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
184204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (isa<FunctionNoProtoType>(Fn)) {
184304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      S.Diag(Attr.getLoc(), diag::err_cconv_knr)
184404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
184504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return false;
184604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
184704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
184804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(Fn);
184904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
185004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      S.Diag(Attr.getLoc(), diag::err_cconv_varargs)
185104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
185204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return false;
185304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
185404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
1855f82b4e85b1219295cad4b5851b035575bc293010John McCall
185604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  Type = S.Context.getCallConvType(Type, CC);
185704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  return false;
1858f82b4e85b1219295cad4b5851b035575bc293010John McCall}
1859f82b4e85b1219295cad4b5851b035575bc293010John McCall
18606e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
18616e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
18626e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
18636e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
18646e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
18656e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
18666e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
18676e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompsonstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr, Sema &S) {
18686e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Check the attribute arugments.
18696e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
18706e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
18716e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
18726e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
18736e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
18746e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
18756e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
18766e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
18776e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << "vector_size" << sizeExpr->getSourceRange();
18786e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
18796e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
18806e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
18816e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (CurType->isVectorType() ||
18826e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
18836e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
18846e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
18856e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
18866e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
18876e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
18886e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
18896e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
18906e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
18916e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
18926e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
18936e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
18946e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
18956e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
18966e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
18976e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
18986e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
18996e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
19006e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
19016e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
19026e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
19036e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
190482287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize, false, false);
19056e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
19066e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
190704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallvoid ProcessTypeAttributeList(Sema &S, QualType &Result,
1908328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis                              bool IsDeclSpec, const AttributeList *AL,
190904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                              DelayedAttributeSet &FnAttrs) {
1910c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
1911c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
1912c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
1913c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
1914c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  for (; AL; AL = AL->getNext()) {
1915b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
1916b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
1917c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    switch (AL->getKind()) {
1918c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
191904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
1920c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
192104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      HandleAddressSpaceTypeAttribute(Result, *AL, S);
1922c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
1923d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    case AttributeList::AT_objc_gc:
192404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      HandleObjCGCTypeAttribute(Result, *AL, S);
1925d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
192604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    case AttributeList::AT_vector_size:
192704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      HandleVectorSizeAttr(Result, *AL, S);
1928f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
192904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
193004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    case AttributeList::AT_noreturn:
193104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    case AttributeList::AT_cdecl:
193204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    case AttributeList::AT_fastcall:
1933f82b4e85b1219295cad4b5851b035575bc293010John McCall    case AttributeList::AT_stdcall:
1934425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    case AttributeList::AT_regparm:
1935328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis      // Don't process these on the DeclSpec.
1936328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis      if (IsDeclSpec ||
1937328ce34c7d0c4e8696d352853b48b385dec1fef4Charles Davis          ProcessFnAttr(S, Result, *AL))
193804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        FnAttrs.push_back(DelayedAttribute(AL, Result));
19396e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
1940c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
1941c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  }
1942232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
1943232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
19441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
19454ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
19464ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
19474ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
194886447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
194986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
195086447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
195186447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
195286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
19534ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
19544ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
19554ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
19564ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
19574ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
19584ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
19591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @param PD The partial diagnostic that will be printed out if T is not a
1960b790661a15d93941d2c33a0ea328254277b3d7e3Anders Carlsson/// complete type.
19614ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
19624ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
19634ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
196491a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
19658c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               const PartialDiagnostic &PD,
19668c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               std::pair<SourceLocation,
19678c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                                         PartialDiagnostic> Note) {
196891a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  unsigned diag = PD.getDiagID();
19691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1970573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
1971573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
1972690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
1973690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
1974690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
19751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
1976690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
1977690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
19784ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
19794ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
19804ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
19814ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
1982d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
1983923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
1984923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
1985923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
198689c49f09b0292dc7c03885f6c765d667a9837597Douglas Gregor  if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
1987923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
1988923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
19892943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
1990d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
1991972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
1992972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
1993d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
19945842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor                                                      /*Complain=*/diag != 0);
19951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
1996d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1997d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1998b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
1999b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        assert(MSInfo && "Missing member specialization information?");
2000357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
2001b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        if (MSInfo->getTemplateSpecializationKind()
2002972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor                                               != TSK_ExplicitSpecialization)
2003f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
2004f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
2005f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
2006f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  /*Complain=*/diag != 0);
2007d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
2008d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
2009d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
20102943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
20115842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor  if (diag == 0)
20125842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
20131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
201401620704304f819b82ecef769ec114e541a364d7Rafael Espindola  const TagType *Tag = 0;
201501620704304f819b82ecef769ec114e541a364d7Rafael Espindola  if (const RecordType *Record = T->getAs<RecordType>())
201601620704304f819b82ecef769ec114e541a364d7Rafael Espindola    Tag = Record;
201701620704304f819b82ecef769ec114e541a364d7Rafael Espindola  else if (const EnumType *Enum = T->getAs<EnumType>())
201801620704304f819b82ecef769ec114e541a364d7Rafael Espindola    Tag = Enum;
201901620704304f819b82ecef769ec114e541a364d7Rafael Espindola
202001620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // Avoid diagnosing invalid decls as incomplete.
202101620704304f819b82ecef769ec114e541a364d7Rafael Espindola  if (Tag && Tag->getDecl()->isInvalidDecl())
202201620704304f819b82ecef769ec114e541a364d7Rafael Espindola    return true;
202301620704304f819b82ecef769ec114e541a364d7Rafael Espindola
20244ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
202591a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  Diag(Loc, PD) << T;
20263c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
20278c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  // If we have a note, produce it.
20288c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  if (!Note.first.isInvalid())
20298c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson    Diag(Note.first, Note.second);
20308c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson
20314ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
203201620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
20334ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
20341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
20354ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
20364ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
20374ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
20384ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
20394ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
20404ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
2041e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
2042fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2043fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               const PartialDiagnostic &PD) {
2044fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PD,
2045fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
2046fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
2047fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
2048fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2049fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               unsigned DiagID) {
2050fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PDiag(DiagID),
2051fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
2052fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
2053fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
2054e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor/// \brief Retrieve a version of the type 'T' that is qualified by the
2055e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor/// nested-name-specifier contained in SS.
2056e6258936178b4c52b43b3b9dbec13552961cd645Douglas GregorQualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
2057e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor  if (!SS.isSet() || SS.isInvalid() || T.isNull())
2058e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
20591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2060ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  NestedNameSpecifier *NNS
20613507369940bfb269551bfa1fec812481f60e3552Douglas Gregor    = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
2062ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  return Context.getQualifiedNameType(NNS, T);
2063e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
2064af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
2065af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders CarlssonQualType Sema::BuildTypeofExprType(Expr *E) {
20664b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  if (E->getType() == Context.OverloadTy) {
20674b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
20684b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // function template specialization wherever deduction cannot occur.
20694b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (FunctionDecl *Specialization
20704b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        = ResolveSingleFunctionTemplateSpecialization(E)) {
2071161755a09898c95d21bfff33707da9ca41cd53c5John McCall      // The access doesn't really matter in this case.
2072161755a09898c95d21bfff33707da9ca41cd53c5John McCall      DeclAccessPair Found = DeclAccessPair::make(Specialization,
2073161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                                  Specialization->getAccess());
2074161755a09898c95d21bfff33707da9ca41cd53c5John McCall      E = FixOverloadedFunctionReference(E, Found, Specialization);
20754b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      if (!E)
20764b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        return QualType();
20774b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    } else {
20784b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Diag(E->getLocStart(),
20794b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor           diag::err_cannot_determine_declared_type_of_overloaded_function)
20804b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        << false << E->getSourceRange();
20814b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      return QualType();
20824b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
20834b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  }
20844b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
2085af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
2086af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
2087af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
2088af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders CarlssonQualType Sema::BuildDecltypeType(Expr *E) {
2089af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  if (E->getType() == Context.OverloadTy) {
20904b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
20914b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // function template specialization wherever deduction cannot occur.
20924b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (FunctionDecl *Specialization
20934b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor          = ResolveSingleFunctionTemplateSpecialization(E)) {
2094161755a09898c95d21bfff33707da9ca41cd53c5John McCall      // The access doesn't really matter in this case.
2095161755a09898c95d21bfff33707da9ca41cd53c5John McCall      DeclAccessPair Found = DeclAccessPair::make(Specialization,
2096161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                                  Specialization->getAccess());
2097161755a09898c95d21bfff33707da9ca41cd53c5John McCall      E = FixOverloadedFunctionReference(E, Found, Specialization);
20984b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      if (!E)
20994b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        return QualType();
21004b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    } else {
21014b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Diag(E->getLocStart(),
21024b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor           diag::err_cannot_determine_declared_type_of_overloaded_function)
21034b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        << true << E->getSourceRange();
21044b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      return QualType();
21054b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
2106af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  }
21074b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
2108af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getDecltypeType(E);
2109af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
2110