SemaType.cpp revision cfb708c354e2f30ccc5cba9d644650f408a1ec3e
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
71930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
72930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
735db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// \param D  the declarator containing the declaration specifier.
745153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
755153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
768ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic QualType ConvertDeclSpecToType(Declarator &TheDeclarator, Sema &TheSema){
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
795db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  const DeclSpec &DS = TheDeclarator.getDeclSpec();
805db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc();
815db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
825db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    DeclLoc = DS.getSourceRange().getBegin();
831564e3906cad604a42bd131e584751a75589a9c4Chris Lattner
841564e3906cad604a42bd131e584751a75589a9c4Chris Lattner  ASTContext &Context = TheSema.Context;
851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
865db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
8896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
8996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
9096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
93fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
95fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
99fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
101958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
10264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
10364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
10464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
10564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
1061564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
107f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
10864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
10964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
11064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
11164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
1121564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
113f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
11464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
11564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
11664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
117f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
118f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
119f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
120f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
121f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
122f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
123f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
124f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
125f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
126f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
127d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
12862f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
129097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
1301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
13114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                                (ObjCProtocolDecl**)PQ,
132683087ffcf21d2a22cd2d0424b7f119168b47a8eSteve Naroff                                                DS.getNumProtocolQualifiers());
13362f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
13462f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
1355db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
1365db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
1375db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
1388ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl    if (isOmittedBlockReturnType(TheDeclarator)) {
1395db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      Result = Context.DependentTy;
1405db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
1415db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
143d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
144d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
145d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
146d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
147d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
148d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
149d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
1501564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    if (TheSema.getLangOptions().ImplicitInt) {
15135d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
15235d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
1533f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
1541564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::ext_missing_declspec)
1553f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
156173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner        << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
157173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner                                                 "int");
1583f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
1594310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
160d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
161d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
162d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
163d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
1644310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
1651564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      if (TheSema.getLangOptions().CPlusPlus &&
1661564e3906cad604a42bd131e584751a75589a9c4Chris Lattner          !TheSema.getLangOptions().Microsoft) {
1671564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::err_missing_type_specifier)
1683f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
1691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
170b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
171b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
172b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
1735db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner        TheDeclarator.setInvalidType(true);
174b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
1751564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier)
1763f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
177b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
178d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
1791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
1813cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
184fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
185fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
186fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
187311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
188311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
189311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
190311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
191311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        if (!TheSema.getLangOptions().C99 &&
192311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner            !TheSema.getLangOptions().CPlusPlus0x)
193311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner          TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
194311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
198fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
199fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
200fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
201311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
202311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
203311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
204311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
205311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        if (!TheSema.getLangOptions().C99 &&
206311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner            !TheSema.getLangOptions().CPlusPlus0x)
207311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner          TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
208311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
2105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
211958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
2123cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
213fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
214958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
215958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
216fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
217958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
218fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
219958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
220fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
2215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
2225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
2235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
2241564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
2258f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
2265db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    TheDeclarator.setInvalidType(true);
2278f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
22899dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
2295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
232c7621a64717203e1f7d5d79dbf548e590b32596cDouglas Gregor    TypeDecl *D
233c7621a64717203e1f7d5d79dbf548e590b32596cDouglas Gregor      = dyn_cast_or_null<TypeDecl>(static_cast<Decl *>(DS.getTypeRep()));
2346e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
2356e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
2366e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
2375db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      TheDeclarator.setInvalidType(true);
2386e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
2396e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
2406e24726524c2b51b31bb4b622aa678a46b024f42John McCall
241a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
24254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
243a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
2445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
245a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
246a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
248a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
2492191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
2502191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    // In C++, make an ElaboratedType.
2511564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    if (TheSema.getLangOptions().CPlusPlus) {
2522191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall      TagDecl::TagKind Tag
2532191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall        = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType());
2542191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall      Result = Context.getElaboratedType(Result, Tag);
2552191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    }
2561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2575153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    if (D->isInvalidDecl())
2585db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      TheDeclarator.setInvalidType(true);
259958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
2601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2611a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
2645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
2651564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    Result = TheSema.GetTypeFromParser(DS.getTypeRep());
2662ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
2671a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
268f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis      if (const ObjCInterfaceType *
269f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis            Interface = Result->getAs<ObjCInterfaceType>()) {
27067ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // It would be nice if protocol qualifiers were only stored with the
27167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // ObjCObjectPointerType. Unfortunately, this isn't possible due
27267ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // to the following typedef idiom (which is uncommon, but allowed):
2731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //
27467ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // typedef Foo<P> T;
27567ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // static void func() {
27667ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        //   Foo<P> *yy;
27767ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        //   T *zz;
27867ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // }
279c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff        Result = Context.getObjCInterfaceType(Interface->getDecl(),
280c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                              (ObjCProtocolDecl**)PQ,
281c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                              DS.getNumProtocolQualifiers());
282f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis      } else if (Result->isObjCIdType())
283ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
28514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                        (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
28614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      else if (Result->isObjCClassType()) {
2874262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy,
289470301bac9c8abfc6b451b3b669c6695a9fd1518Steve Naroff                        (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
2903f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
2911564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
2923f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
2935db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner        TheDeclarator.setInvalidType(true);
2943f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
295c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
2961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
298958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
300958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
301e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
3021564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    Result = TheSema.GetTypeFromParser(DS.getTypeRep());
303958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
304d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
305fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
306958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
307d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
308d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    Expr *E = static_cast<Expr *>(DS.getTypeRep());
309d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
310d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
3114b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    Result = TheSema.BuildTypeofExprType(E);
3124b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
3134b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
3144b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      TheDeclarator.setInvalidType(true);
3154b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
316958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
317d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
3186fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
3196fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    Expr *E = static_cast<Expr *>(DS.getTypeRep());
3206fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
3216fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
3221564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    Result = TheSema.BuildDecltypeType(E);
323af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
324af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
3255db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      TheDeclarator.setInvalidType(true);
326af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
3276fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
3286fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
329e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
330e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
331e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    Result = Context.UndeducedAutoTy;
332e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
333e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
3341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
335809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
3365153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
3375db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    TheDeclarator.setInvalidType(true);
3385153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
341958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
342f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
3431564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    if (TheSema.getLangOptions().Freestanding)
3441564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
345fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
346f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
3471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
348958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
349958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner         "FIXME: imaginary types not supported yet!");
3501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35138d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // See if there are any attributes on the declspec that apply to the type (as
35238d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // opposed to the decl).
353fca0ddd42965e0b7ae821213486d4e0dd71fb439Chris Lattner  if (const AttributeList *AL = DS.getAttributes())
3541564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    TheSema.ProcessTypeAttributeList(Result, AL);
3551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
35796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
35896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
35996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
36096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
36196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
3620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (TypeQuals & DeclSpec::TQ_restrict) {
3632b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian      if (Result->isAnyPointerType() || Result->isReferenceType()) {
3642b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        QualType EltTy;
3652b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        if (Result->isObjCObjectPointerType())
3662b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result;
3672b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        else
3682b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result->isPointerType() ?
3692b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<PointerType>()->getPointeeType() :
3702b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<ReferenceType>()->getPointeeType();
3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
372bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
373bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
374bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
3751564e3906cad604a42bd131e584751a75589a9c4Chris Lattner          TheSema.Diag(DS.getRestrictSpecLoc(),
376d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
377d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
3780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall          TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
379bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
380bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
3811564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DS.getRestrictSpecLoc(),
382d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner             diag::err_typecheck_invalid_restrict_not_pointer)
383d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
3840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
38596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
38696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
3871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
38996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
39096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
39196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
39296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
39396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
3940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
39596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
3960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
39796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
3980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
3990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        assert((TypeQuals & DeclSpec::TQ_restrict) &&
4000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall               "Has CVR quals but not C, V, or R?");
4010953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Loc = DS.getRestrictSpecLoc();
40296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
4031564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
404d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
40596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
4061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
407f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
408f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
409f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
410f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
411f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
4121a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
4131a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
414f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
4150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
4160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
4171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
4181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
4200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Result = Context.getQualifiedType(Result, Quals);
42196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
4220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
423f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
424f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
425f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
426cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
427cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
428cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
4291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
430cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
431cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
432cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
433cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
434cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
435cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
436cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
437cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the pointer type.
438cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
439cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
440cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
441cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
442cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
443cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
444cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
445cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
446cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
447cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildPointerType(QualType T, unsigned Quals,
449cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
450cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
451cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
452cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
453ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
454cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
455cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
456cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
4570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
4580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
459cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
460cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
4610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) {
462cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
463cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
4640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qs.removeRestrict();
465cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
466cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
467cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
4680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return Context.getQualifiedType(Context.getPointerType(T), Qs);
469cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
470cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
471cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
472cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
473cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
474cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
4750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR The cvr-qualifiers to be applied to the reference type.
476cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
477cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
478cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
479cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
480cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
481cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
482cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
483cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
484cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
485cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
48654e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
48754e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  unsigned CVR, SourceLocation Loc,
48854e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
4890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
49054e14c4db764c0636160d26c5bbf491637c83a76John McCall
49154e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
49254e14c4db764c0636160d26c5bbf491637c83a76John McCall
49354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
49454e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   reference to a type T, and attempt to create the type "lvalue
49554e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   reference to cv TD" creates the type "lvalue reference to T".
49654e14c4db764c0636160d26c5bbf491637c83a76John McCall  // We use the qualifiers (restrict or none) of the original reference,
49754e14c4db764c0636160d26c5bbf491637c83a76John McCall  // not the new ones. This is consistent with GCC.
49854e14c4db764c0636160d26c5bbf491637c83a76John McCall
49954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
50054e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
50154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
50254e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
50354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
50454e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
50554e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
50654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
50754e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
50854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
50954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
51054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // collapsing of references-to-references as described in C++
51154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // DR 106 and amended by C++ DR 540.
512cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
513cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
51433a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
515cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
516cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
517cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
518cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
519cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
520cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
521cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
522cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
5230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
524cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
525cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
5260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Quals.removeRestrict();
527cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
528cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
529cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
530cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   [...] Cv-qualified references are ill-formed except when the
531cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   cv-qualifiers are introduced through the use of a typedef
532cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   (7.1.3) or of a template type argument (14.3), in which case
533cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   the cv-qualifiers are ignored.
534cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //
535cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // We diagnose extraneous cv-qualifiers for the non-typedef,
536cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // non-template type argument case within the parser. Here, we just
537cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // ignore any extraneous cv-qualifiers.
5380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.removeConst();
5390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.removeVolatile();
540cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
541cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
5427c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
54354e14c4db764c0636160d26c5bbf491637c83a76John McCall    return Context.getQualifiedType(
54454e14c4db764c0636160d26c5bbf491637c83a76John McCall               Context.getLValueReferenceType(T, SpelledAsLValue), Quals);
5450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals);
546cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
547cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
548cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
549cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
550cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
551cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
552cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
5531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
5541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
555cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
556cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the array's
557cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// element type.
558cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
559cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
560cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// array type or, if there is no such entity, the location of the
561cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have array type.
562cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
563cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
564cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
565cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
566cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
567cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
568cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
569cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
5707e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
5710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
5727e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
5731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // C99 6.7.5.2p1: If the element type is an incomplete or function type,
574cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
575923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // Not in C++, though. There we only dislike void.
576923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (getLangOptions().CPlusPlus) {
577923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (T->isVoidType()) {
578923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
579923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
580923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
581923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
582923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
583923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
584923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
585923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
586cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
587cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
588cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
589ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
590cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
591cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
5921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
593cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ 8.3.2p4: There shall be no ... arrays of references ...
594cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
595cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_references)
596ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
597cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
5981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
599cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
600e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
6011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Loc,  diag::err_illegal_decl_array_of_auto)
602e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson      << getPrintableNameForEntity(Entity);
603e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
604e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
6051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6066217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
607cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
608cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
609cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
610cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
611cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (T->isObjCInterfaceType()) {
612c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
613c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
614cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
616cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
617cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (ArraySize && !ArraySize->isTypeDependent() &&
618cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      !ArraySize->getType()->isIntegerType()) {
619cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
620cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
621cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    ArraySize->Destroy(Context);
622cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
623cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
624cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  llvm::APSInt ConstVal(32);
625cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
626f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
6277e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
628f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
629f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
630cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (ArraySize->isValueDependent()) {
6317e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
632cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
633923d56d436f750bc1f29db50e641078725558a1bSebastian Redl             (!T->isDependentType() && !T->isIncompleteType() &&
634923d56d436f750bc1f29db50e641078725558a1bSebastian Redl              !T->isConstantSizeType())) {
635cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Per C99, a variable array is an array with either a non-constant
636cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // size or an element type that has a non-constant-size
6377e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
638cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
639cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
640cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
641923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
642923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(ArraySize->getLocStart(),
643923d56d436f750bc1f29db50e641078725558a1bSebastian Redl           diag::err_typecheck_negative_array_size)
644923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
645923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
646923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
647923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
648923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      // GCC accepts zero sized static arrays.
649923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
650923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
65246a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
653cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
654af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
655af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  if (!getLangOptions().C99) {
6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (ArraySize && !ArraySize->isTypeDependent() &&
6571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        !ArraySize->isValueDependent() &&
658cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        !ArraySize->isIntegerConstantExpr(Context))
659043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
660cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    else if (ASM != ArrayType::Normal || Quals != 0)
661043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      Diag(Loc,
662043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor           getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
663043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor                                     : diag::ext_c99_array_usage);
664cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
665cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
666cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
667cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
6689cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6699cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
6709cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
6719cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
6739cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
6749cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6759cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Expr *Arg = (Expr *)ArraySize.get();
6769cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6779cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
6789cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
6791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
6809cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
6819cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
6829cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
6839cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
6849cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6859cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
6869cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
6879cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
6889cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
6899cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      << "ext_vector_type" << Arg->getSourceRange();
6909cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
6919cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
6921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
6949cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
6951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
6961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6979cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
6989cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
6999cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      << Arg->getSourceRange();
7009cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
7019cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
7021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7039cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!T->isDependentType())
7049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return Context.getExtVectorType(T, vectorSize);
7051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
7089cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                AttrLoc);
7099cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
7101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
711724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
712724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
713724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
714724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
715724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
7162943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
717724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
718724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
719724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
720724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
721724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
722724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
723724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
724724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
725724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
726724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
727724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
728724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
729724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
730724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
731724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
732724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
733724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
734724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
735724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
736724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
737724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
738724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
739724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
740724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
7411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 QualType *ParamTypes,
742724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
743724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 bool Variadic, unsigned Quals,
744724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 SourceLocation Loc, DeclarationName Entity) {
745724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
74658408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor    Diag(Loc, diag::err_func_returning_array_function)
74758408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
748724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
749724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
751724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
752724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
7532dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    QualType ParamType = adjustParameterType(ParamTypes[Idx]);
7542dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
755724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
756724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
757724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
758cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
75954e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
760724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
761724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
762724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
763724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
764724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
766724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 Quals);
767724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
7681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
769949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
770949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
771949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
772949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
7730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR Qualifiers applied to the member pointer type
774949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
775949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
776949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
777949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
778949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
7791eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
7800953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                      unsigned CVR, SourceLocation Loc,
781949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
7820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
7830953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
784949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
785949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
786949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
787949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
788949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
789949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
790949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
791949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
792949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
793949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
794949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
795949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
796949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
797949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
798949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
799949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
8008d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
801ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
802949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
803949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
804949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
805949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
806949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
807949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
808949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
809949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
810949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
811949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
812949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // object or incomplete types shall not be restrict-qualified."
8130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
814949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
815949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << T;
816949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
817949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
818949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
8190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Quals.removeRestrict();
820949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
821949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
822949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
823949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
824949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
825949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
826949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
8270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return Context.getQualifiedType(
8280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall           Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
829949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
8301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8319a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
8329a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8339a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
8349a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
8369a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8379a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Loc The location of the entity whose type involves this
8389a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// block pointer type or, if there is no such entity, the location of the
8399a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type that will have block pointer type.
8409a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8419a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
8429a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
8439a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8449a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
8459a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
8460953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
8471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
8489a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
8490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
8509a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
8519a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
8529a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
8531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
8550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
8569a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
8579a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
858a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallQualType Sema::GetTypeFromParser(TypeTy *Ty, TypeSourceInfo **TInfo) {
859e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  QualType QT = QualType::getFromOpaquePtr(Ty);
8603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
861a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
8623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
8633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
8643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
865a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
866e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
867e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
868a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
869e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
8701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
871a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
872e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
873e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
874e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
87598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
8768ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl/// declarator to Type instances.
877402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor///
878402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
879402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// owns the declaration of a type (e.g., the definition of a struct
880402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// type), then *OwnedDecl will receive the owned declaration.
881a1d5662d96465f0fddf8819d245da4d19b892effArgyrios KyrtzidisQualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
882a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                    TypeSourceInfo **TInfo,
883402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor                                    TagDecl **OwnedDecl) {
884930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
885930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
886930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
887f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
8883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
8893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
8903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
8910486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
8923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
8938ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl    T = ConvertDeclSpecToType(D, *this);
8945db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
8955db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    if (!D.isInvalidType() && OwnedDecl && D.getDeclSpec().isTypeSpecOwned())
8965db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      *OwnedDecl = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
897930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
898930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
8993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
9000efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
9013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
902930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
90348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // "void" instead.
904930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
905930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
90648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
90748026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
90848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
90948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
91048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    T = GetTypeFromParser(D.getName().ConversionFunctionId);
91148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
912930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
913f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
9141f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor  if (T.isNull())
9151f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    return T;
9161f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
917baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  if (T == Context.UndeducedAutoTy) {
918baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
9191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
920baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
921baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
922baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      assert(0 && "K&R type lists aren't allowed in C++");
923baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
924baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
925baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
926baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
927baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
928baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      switch (cast<TagDecl>(CurContext)->getTagKind()) {
929baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
930baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
931baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_union:  Error = 2; /* Union member */ break;
932baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_class:  Error = 3; /* Class member */ break;
9331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
934baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
935baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
936baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 4; // Exception declaration
937baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
938baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
939baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 5; // Template parameter
940baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
941baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
942baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 6;  // Block literal
943baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
944baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
945baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
946baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
947baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
948baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TypeNameContext:
949baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
950baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
951baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
952baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
953baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
954baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
955baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      T = Context.IntTy;
956baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
957baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
958baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
9591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
960cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
961cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
962cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
963cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
9641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
96698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
96798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
9688ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
9698ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl    DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
9705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
9715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
9725618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
9739af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
9749af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
9759af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner        Diag(DeclType.Loc, diag::err_blocks_disable);
9761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
9789a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                Name);
9795618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
9805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
9816a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
9826a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
9836a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
9846a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
9856a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
9866a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
9876a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
98814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
989183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
99014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        T = Context.getObjCObjectPointerType(T,
99167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff                                         (ObjCProtocolDecl **)OIT->qual_begin(),
99267ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff                                         OIT->getNumProtocols());
99314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
99414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
995cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
9965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
9970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
9980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Qualifiers Quals;
9990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (DeclType.Ref.HasRestrict) Quals.addRestrict();
10000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10016a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
10026a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
10036a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
10046a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
10056a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
10066a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
10076a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
10080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
1009cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                             DeclType.Loc, Name);
10105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
10125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
10136a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
10146a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
10156a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
10166a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
10176a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
10186a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
10196a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
1020fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
102194f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
10225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
10235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
10245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
10255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
10265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
10275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
10285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
1029f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      if (ASM == ArrayType::Star &&
1030f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman          D.getContext() != Declarator::PrototypeContext) {
1031f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
1032f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
1033f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1034f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1035f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
1036f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
1037f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
10380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      T = BuildArrayType(T, ASM, ArraySize,
10390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                         Qualifiers::fromCVRMask(ATI.TypeQuals),
10407e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                         SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
10415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1043f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
10445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
10465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
10475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
10483cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
1049cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      // C99 6.7.5.3p1: The return type may not be a function or array type.
105058408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      // For conversion functions, we'll diagnose this particular error later.
105148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor      if ((T->isArrayType() || T->isFunctionType()) &&
105248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
105358408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor        Diag(DeclType.Loc, diag::err_func_returning_array_function)
105458408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor          << T->isFunctionType() << T;
1055cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        T = Context.IntTy;
1056cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        D.setInvalidType(true);
1057cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      }
1058465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1059402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1060402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
1061402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
1062402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1063402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        if (Tag->isDefinition())
1064402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor          Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1065402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
1066402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
1067402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
10683cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
10693cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
10703cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      if (FTI.hasExceptionSpec &&
10713cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl          D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
10723cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl        Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
10733cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
1074eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      if (FTI.NumArgs == 0) {
1075c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        if (getLangOptions().CPlusPlus) {
1076c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1077c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // function takes no arguments.
1078465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          llvm::SmallVector<QualType, 4> Exceptions;
1079465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          Exceptions.reserve(FTI.NumExceptions);
10801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1081e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis            // FIXME: Preserve type source info.
1082e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis            QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1083ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            // Check that the type is valid for an exception spec, and drop it
1084ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            // if not.
1085ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1086ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl              Exceptions.push_back(ET);
1087ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          }
1088465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1089465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                      FTI.hasExceptionSpec,
1090465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                      FTI.hasAnyExceptionSpec,
1091ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl                                      Exceptions.size(), Exceptions.data());
1092965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor        } else if (FTI.isVariadic) {
1093965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // We allow a zero-parameter variadic function in C if the
1094965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // function is marked with the "overloadable"
1095965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // attribute. Scan for this attribute now.
1096965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
1097965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
1098965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
1099965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
1100965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
1101965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
1102965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
1103965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
1104965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
1105965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
1106965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1107965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
1108c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        } else {
1109c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // Simple void foo(), where the incoming T is the result type.
111072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          T = Context.getFunctionNoProtoType(T);
1111c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
1112eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      } else if (FTI.ArgInfo[0].Param == 0) {
11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
11141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
111554e14c4db764c0636160d26c5bbf491637c83a76John McCall        D.setInvalidType(true);
11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else {
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
11201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1122b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner          ParmVarDecl *Param =
1123b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner            cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
11248123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
112578c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
11262dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
11272dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
1128beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor          assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
11292dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
11305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
113272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
11332dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
11392ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
11408123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
11412ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
11422ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
11444565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
11452ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
11468123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
11472ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
11482ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
11490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
11502ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
11511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11522ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
11532ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
11542ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
1155eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
1156eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
1157a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
1158183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
1159eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
1160eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
1161eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
11625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
11631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
116454e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
11655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
1166465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1167465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        llvm::SmallVector<QualType, 4> Exceptions;
1168465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        Exceptions.reserve(FTI.NumExceptions);
11691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1170e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis          // FIXME: Preserve type source info.
1171e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis          QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1172ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          // Check that the type is valid for an exception spec, and drop it if
1173ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          // not.
1174ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1175ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            Exceptions.push_back(ET);
1176ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl        }
1177465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1178beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
1179465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.isVariadic, FTI.TypeQuals,
1180465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.hasExceptionSpec,
1181465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.hasAnyExceptionSpec,
1182ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl                                    Exceptions.size(), Exceptions.data());
11835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1186f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
11874994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      // Verify that we're not building a pointer to pointer to function with
11884994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      // exception specification.
11894994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
11904994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
11914994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl        D.setInvalidType(true);
11924994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl        // Build the type anyway.
11934994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      }
1194f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
1195f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
119687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor      if (isDependentScopeSpecifier(DeclType.Mem.Scope())
119787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor            || dyn_cast_or_null<CXXRecordDecl>(
119887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor                                   computeDeclContext(DeclType.Mem.Scope()))) {
11991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
1200949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
120187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
120287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
120387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
120487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = Context.getTypenameType(NNSPrefix, NNS->getAsIdentifier());
120587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
120687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
120787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
120887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
12099f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
121087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
121187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
121287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
121387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
121487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
121587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          if (NNSPrefix)
121687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor            ClsType = Context.getQualifiedNameType(NNSPrefix, ClsType);
121787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
121887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
1219f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
1220949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        Diag(DeclType.Mem.Scope().getBeginLoc(),
1221949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
1222949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1223949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
1224f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
1225f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1226f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1227949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
1228949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1229949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                   DeclType.Loc, D.getIdentifier());
1230949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
1231f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
1232949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
1233f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1234f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
1235f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
1236f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1237cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
1238cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
1239cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
1240cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
1241cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1242c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
1243c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    if (const AttributeList *AL = DeclType.getAttrs())
1244c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      ProcessTypeAttributeList(T, AL);
12455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1246971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1247971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
1248183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
1249778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
1250971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1251971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1252971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // for a nonstatic member function, the function type to which a pointer
1253971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // to member refers, or the top-level function type of a function typedef
1254971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // declaration.
1255971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    if (FnTy->getTypeQuals() != 0 &&
1256971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
1257584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor        ((D.getContext() != Declarator::MemberContext &&
1258584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor          (!D.getCXXScopeSpec().isSet() ||
1259f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor           !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1260f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor              ->isRecord())) ||
1261971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
1262971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      if (D.isFunctionDeclarator())
1263971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1264971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      else
1265971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(),
1266971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis             diag::err_invalid_qualified_typedef_function_type_use);
1267971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1268971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      // Strip the cv-quals from the type.
1269971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
12707fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                                  FnTy->getNumArgs(), FnTy->isVariadic(), 0);
1271971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
1272971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
12731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12740bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // If there were any type attributes applied to the decl itself (not the
12750bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // type, apply the type attribute to the type!)
12760bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  if (const AttributeList *Attrs = D.getAttributes())
1277c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(T, Attrs);
12784adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
1279a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) {
128054e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (D.isInvalidType())
1281a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      *TInfo = 0;
128254e14c4db764c0636160d26c5bbf491637c83a76John McCall    else
1283a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      *TInfo = GetTypeSourceInfoForDeclarator(D, T);
128454e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
12854adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T;
12875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
128951bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
129051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
129151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
1292f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
129351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
129451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
1295f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
129651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
129751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
129851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
129951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
130051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
130151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
130251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
130351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
130454e14c4db764c0636160d26c5bbf491637c83a76John McCall
130554e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
130654e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
130754e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
130854e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
130954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
131054e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
131154e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
131254e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
131354e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
131454e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
131554e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
131654e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
131751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
131854e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
131951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
132051bd803fbdade51d674598ed45da3d54190a656cJohn McCall
132154e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
132254e14c4db764c0636160d26c5bbf491637c83a76John McCall
132354e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
132454e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
132554e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
132654e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setHasProtocolsAsWritten(true);
132754e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
132854e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
132954e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
133054e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
133154e14c4db764c0636160d26c5bbf491637c83a76John McCall
133254e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
133354e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
133454e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setHasProtocolsAsWritten(false);
133554e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
133654e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
133754e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
133854e14c4db764c0636160d26c5bbf491637c83a76John McCall
133954e14c4db764c0636160d26c5bbf491637c83a76John McCall      // This might not have been written with an inner type.
134054e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
134154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setHasBaseTypeAsWritten(false);
134254e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.getBaseTypeLoc().initialize(SourceLocation());
134354e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
134454e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setHasBaseTypeAsWritten(true);
134551bd803fbdade51d674598ed45da3d54190a656cJohn McCall        Visit(TL.getBaseTypeLoc());
134654e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
134751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
1348833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
1349a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
1350a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1351833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1352833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
1353833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
1354a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
1355833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        TL.initialize(DS.getTypeSpecTypeLoc());
1356833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
1357833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
1358833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1359833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      TemplateSpecializationTypeLoc OldTL =
1360a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall        cast<TemplateSpecializationTypeLoc>(TInfo->getTypeLoc());
1361833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      TL.copy(OldTL);
1362833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
1363cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1364cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
1365cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1366cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
1367cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
1368cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1369cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
1370cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1371cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
1372cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeRep());
1373cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
1374cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1375cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
1376cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
137751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
137851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
137951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.initialize(DS.getTypeSpecTypeLoc());
138051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
138151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
1382eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
138351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
138451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
1385f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
138651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
138751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
13884adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
138951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
13909f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
139151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
13924adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
139351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
139451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
139551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
13964adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
139751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
139851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
139951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
14004adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
140151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
140251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
140351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
140454e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setHasBaseTypeAsWritten(true);
140554e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setHasProtocolsAsWritten(false);
140654e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setLAngleLoc(SourceLocation());
140754e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setRAngleLoc(SourceLocation());
14084adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
140951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
141051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
141151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
141251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: nested name specifier
14134adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
141451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
141551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
141654e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
141754e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
141851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
141951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
142051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
142151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
142251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
142351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
142451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
142551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
142651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
142751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
142851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
142951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
143051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
143151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
143251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
143351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLParenLoc(Chunk.Loc);
143451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRParenLoc(Chunk.EndLoc);
143551bd803fbdade51d674598ed45da3d54190a656cJohn McCall
143651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
143754e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
14384adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis        ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
143954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
14404adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
144151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
14424adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
14431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
144451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
14459f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
14464adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
144751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
144851bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
14494adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
1450a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
145151bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
145251bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
1453a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
1454a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T) {
1455a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
1456a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
145751bd803fbdade51d674598ed45da3d54190a656cJohn McCall
14588ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
145951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
146051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
14614adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
1462f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
146351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
14644adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
1465a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
14664adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
14674adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
1468a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
1469a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallQualType Sema::CreateLocInfoType(QualType T, TypeSourceInfo *TInfo) {
14701bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
14711bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
14721bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
14731bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
1474a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
14751bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
14761bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
14771bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  return QualType(LocT, 0);
14781bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
14791bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
14801bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
14811bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
148235d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis  assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
148335d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
148435d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
14851bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
14861bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
14879e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
14889e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
14899e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// they point to and return true. If T1 and T2 aren't pointer types
14909e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// or pointer-to-member types, or if they are not similar at this
14919e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// level, returns false and leaves T1 and T2 unchanged. Top-level
14929e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// qualifiers on T1 and T2 are ignored. This function will typically
14939e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// be called in a loop that successively "unwraps" pointer and
14949e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// pointer-to-member types to compare them at each level.
1495ecb81f28cb279b7d8e84296445a4131fa80b69a9Chris Lattnerbool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
14966217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const PointerType *T1PtrType = T1->getAs<PointerType>(),
14976217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek                    *T2PtrType = T2->getAs<PointerType>();
149857373266011f73418381b736015d8d2bb0381176Douglas Gregor  if (T1PtrType && T2PtrType) {
149957373266011f73418381b736015d8d2bb0381176Douglas Gregor    T1 = T1PtrType->getPointeeType();
150057373266011f73418381b736015d8d2bb0381176Douglas Gregor    T2 = T2PtrType->getPointeeType();
150157373266011f73418381b736015d8d2bb0381176Douglas Gregor    return true;
150257373266011f73418381b736015d8d2bb0381176Douglas Gregor  }
150357373266011f73418381b736015d8d2bb0381176Douglas Gregor
15046217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
15056217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek                          *T2MPType = T2->getAs<MemberPointerType>();
150621593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl  if (T1MPType && T2MPType &&
150721593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T1MPType->getClass()) ==
150821593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T2MPType->getClass())) {
15094433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T1 = T1MPType->getPointeeType();
15104433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T2 = T2MPType->getPointeeType();
15114433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    return true;
15124433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  }
151357373266011f73418381b736015d8d2bb0381176Douglas Gregor  return false;
151457373266011f73418381b736015d8d2bb0381176Douglas Gregor}
151557373266011f73418381b736015d8d2bb0381176Douglas Gregor
1516cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlSema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
15175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
15201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1521a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = 0;
1522402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  TagDecl *OwnedTag = 0;
1523a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  QualType T = GetTypeForDeclarator(D, S, &TInfo, &OwnedTag);
15245153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
1525809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
15265912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
1527402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  if (getLangOptions().CPlusPlus) {
1528402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
15296d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
15306d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
1531402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // C++0x [dcl.type]p3:
1532402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   A type-specifier-seq shall not define a class or enumeration
1533402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   unless it appears in the type-id of an alias-declaration
1534402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   (7.1.3).
1535402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    if (OwnedTag && OwnedTag->isDefinition())
1536402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1537402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        << Context.getTypeDeclType(OwnedTag);
1538402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
1539402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
1540a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo)
1541a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    T = CreateLocInfoType(T, TInfo);
1542e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
15435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T.getAsOpaquePtr();
15445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1546c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1547c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1548c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
1549c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
1550c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
1551232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
1552232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1553c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
1554c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
15551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
1556c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
15570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1558232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
1559232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1560232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
1561232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
1562c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1563c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1564232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
15651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1566232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
1567545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
1568f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1569c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1570232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1571545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1572232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
1573c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
1574dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1575dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
1576c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1577232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1578232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
1579efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
1580efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
1581efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
1582efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1583efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
1584efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
1585efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
1586efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
1587efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
1588efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
15890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
1590efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
1591efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
15920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
1593efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
1594efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
1595efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
15961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
1597f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
1598c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
1599c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1600d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1601d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// specified type.  The attribute contains 1 argument, weak or strong.
16021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleObjCGCTypeAttribute(QualType &Type,
16033b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner                                      const AttributeList &Attr, Sema &S) {
16040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
16055934e75d98d99374f72722a69c5eefe026f35c74Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
1606d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1607d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
16081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1609d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
16101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!Attr.getParameterName()) {
1611ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1612ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
1613ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    return;
1614ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
16150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
1616ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (Attr.getNumArgs() != 0) {
1617d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1618d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1619d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
16201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Attr.getParameterName()->isStr("weak"))
16210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
1622d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else if (Attr.getParameterName()->isStr("strong"))
16230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
1624d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
1625d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1626d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      << "objc_gc" << Attr.getParameterName();
1627d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1628d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
16291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16303b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  Type = S.Context.getObjCGCQualType(Type, GCAttr);
1631d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1632d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
16332455636163fdd18581d7fdae816433f886d88213Mike Stump/// HandleNoReturnTypeAttribute - Process the noreturn attribute on the
16342455636163fdd18581d7fdae816433f886d88213Mike Stump/// specified type.  The attribute contains 0 arguments.
16351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleNoReturnTypeAttribute(QualType &Type,
16362455636163fdd18581d7fdae816433f886d88213Mike Stump                                        const AttributeList &Attr, Sema &S) {
16372455636163fdd18581d7fdae816433f886d88213Mike Stump  if (Attr.getNumArgs() != 0)
16382455636163fdd18581d7fdae816433f886d88213Mike Stump    return;
16392455636163fdd18581d7fdae816433f886d88213Mike Stump
16402455636163fdd18581d7fdae816433f886d88213Mike Stump  // We only apply this to a pointer to function or a pointer to block.
16412455636163fdd18581d7fdae816433f886d88213Mike Stump  if (!Type->isFunctionPointerType()
16422455636163fdd18581d7fdae816433f886d88213Mike Stump      && !Type->isBlockPointerType()
16432455636163fdd18581d7fdae816433f886d88213Mike Stump      && !Type->isFunctionType())
16442455636163fdd18581d7fdae816433f886d88213Mike Stump    return;
16452455636163fdd18581d7fdae816433f886d88213Mike Stump
16462455636163fdd18581d7fdae816433f886d88213Mike Stump  Type = S.Context.getNoReturnType(Type);
16472455636163fdd18581d7fdae816433f886d88213Mike Stump}
16482455636163fdd18581d7fdae816433f886d88213Mike Stump
16496e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
16506e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
16516e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
16526e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
16536e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
16546e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
16556e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
16566e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompsonstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr, Sema &S) {
16576e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Check the attribute arugments.
16586e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
16596e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
16606e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
16616e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
16626e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
16636e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
16646e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
16656e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
16666e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << "vector_size" << sizeExpr->getSourceRange();
16676e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
16686e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
16696e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
16706e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (CurType->isVectorType() ||
16716e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
16726e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
16736e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
16746e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
16756e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
16766e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
16776e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
16786e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
16796e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
16806e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
16816e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
16826e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
16836e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
16846e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
16856e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
16866e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
16876e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
16886e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
16896e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
16906e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
16916e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
16926e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
16936e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
16946e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
16956e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
1696c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnervoid Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
1697c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
1698c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
1699c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
1700c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
1701c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  for (; AL; AL = AL->getNext()) {
1702c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // If this is an attribute we can handle, do so now, otherwise, add it to
1703c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // the LeftOverAttrs list for rechaining.
1704c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    switch (AL->getKind()) {
1705c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
1706c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
1707c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1708c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
1709d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    case AttributeList::AT_objc_gc:
1710d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      HandleObjCGCTypeAttribute(Result, *AL, *this);
1711d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
17122455636163fdd18581d7fdae816433f886d88213Mike Stump    case AttributeList::AT_noreturn:
17132455636163fdd18581d7fdae816433f886d88213Mike Stump      HandleNoReturnTypeAttribute(Result, *AL, *this);
17142455636163fdd18581d7fdae816433f886d88213Mike Stump      break;
17156e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    case AttributeList::AT_vector_size:
17166e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      HandleVectorSizeAttr(Result, *AL, *this);
17176e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
1718c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
1719c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  }
1720232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
1721232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
17221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
17234ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
17244ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
17254ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
172686447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
172786447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
172886447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
172986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
173086447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
17314ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
17324ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
17334ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
17344ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
17354ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
17364ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
17371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @param PD The partial diagnostic that will be printed out if T is not a
1738b790661a15d93941d2c33a0ea328254277b3d7e3Anders Carlsson/// complete type.
17394ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
17404ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
17414ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
174291a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
17438c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               const PartialDiagnostic &PD,
17448c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               std::pair<SourceLocation,
17458c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                                         PartialDiagnostic> Note) {
174691a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  unsigned diag = PD.getDiagID();
17471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1748573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
1749573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
1750690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
1751690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
1752690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
17531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
1754690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
1755690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
17564ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
17574ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
17584ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
17594ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
1760d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
1761923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
1762923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
1763923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
176489c49f09b0292dc7c03885f6c765d667a9837597Douglas Gregor  if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
1765923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
1766923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
17672943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
1768d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
1769972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
1770972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
1771d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
17725842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor                                                      /*Complain=*/diag != 0);
17731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
1774d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1775d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1776b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
1777b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        assert(MSInfo && "Missing member specialization information?");
1778357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
1779b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        if (MSInfo->getTemplateSpecializationKind()
1780972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor                                               != TSK_ExplicitSpecialization)
1781f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
1782f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
1783f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
1784f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  /*Complain=*/diag != 0);
1785d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
1786d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
1787d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
17882943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
17895842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor  if (diag == 0)
17905842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
17911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17924ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
179391a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  Diag(Loc, PD) << T;
17943c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
17958c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  // If we have a note, produce it.
17968c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  if (!Note.first.isInvalid())
17978c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson    Diag(Note.first, Note.second);
17988c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson
17994ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
18001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // type, produce
18014ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  const TagType *Tag = 0;
18026217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *Record = T->getAs<RecordType>())
18034ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Record;
1804183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  else if (const EnumType *Enum = T->getAs<EnumType>())
18054ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Enum;
18064ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
18074ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
18081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
18094ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
18104ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
18114ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
18124ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
18134ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
18144ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
1815e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
1816e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor/// \brief Retrieve a version of the type 'T' that is qualified by the
1817e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor/// nested-name-specifier contained in SS.
1818e6258936178b4c52b43b3b9dbec13552961cd645Douglas GregorQualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1819e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor  if (!SS.isSet() || SS.isInvalid() || T.isNull())
1820e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
18211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1822ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  NestedNameSpecifier *NNS
18233507369940bfb269551bfa1fec812481f60e3552Douglas Gregor    = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
1824ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  return Context.getQualifiedNameType(NNS, T);
1825e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
1826af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
1827af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders CarlssonQualType Sema::BuildTypeofExprType(Expr *E) {
18284b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  if (E->getType() == Context.OverloadTy) {
18294b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
18304b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // function template specialization wherever deduction cannot occur.
18314b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (FunctionDecl *Specialization
18324b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        = ResolveSingleFunctionTemplateSpecialization(E)) {
18334b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      E = FixOverloadedFunctionReference(E, Specialization);
18344b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      if (!E)
18354b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        return QualType();
18364b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    } else {
18374b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Diag(E->getLocStart(),
18384b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor           diag::err_cannot_determine_declared_type_of_overloaded_function)
18394b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        << false << E->getSourceRange();
18404b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      return QualType();
18414b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
18424b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  }
18434b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
1844af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
1845af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
1846af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
1847af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders CarlssonQualType Sema::BuildDecltypeType(Expr *E) {
1848af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  if (E->getType() == Context.OverloadTy) {
18494b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
18504b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // function template specialization wherever deduction cannot occur.
18514b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (FunctionDecl *Specialization
18524b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor          = ResolveSingleFunctionTemplateSpecialization(E)) {
18534b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      E = FixOverloadedFunctionReference(E, Specialization);
18544b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      if (!E)
18554b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        return QualType();
18564b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    } else {
18574b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Diag(E->getLocStart(),
18584b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor           diag::err_cannot_determine_declared_type_of_overloaded_function)
18594b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor        << true << E->getSourceRange();
18604b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      return QualType();
18614b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
1862af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  }
18634b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
1864af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getDecltypeType(E);
1865af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
1866