SemaType.cpp revision 8ce35b095e8fca45e04c1bda14ed0548ce7536ad
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"
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
272dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// \brief Perform adjustment on the parameter type of a function.
282dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor///
292dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// This routine adjusts the given parameter type @p T to the actual
301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
322dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorQualType Sema::adjustParameterType(QualType T) {
332dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // C99 6.7.5.3p7:
34778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   A declaration of a parameter as "array of type" shall be
35778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   adjusted to "qualified pointer to type", where the type
36778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   qualifiers (if any) are those specified within the [ and ] of
37778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   the array type derivation.
38778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  if (T->isArrayType())
392dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getArrayDecayedType(T);
40778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner
41778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  // C99 6.7.5.3p8:
42778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   A declaration of a parameter as "function returning type"
43778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   shall be adjusted to "pointer to function returning type", as
44778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   in 6.3.2.1.
45778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  if (T->isFunctionType())
462dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getPointerType(T);
472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
482dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return T;
492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
502dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
515db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
525db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
535db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// isOmittedBlockReturnType - Return true if this declarator is missing a
545db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// return type because this is a omitted return type on a block literal.
558ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic bool isOmittedBlockReturnType(const Declarator &D) {
565db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getContext() != Declarator::BlockLiteralContext ||
578ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl      D.getDeclSpec().hasTypeSpecifier())
585db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return false;
595db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
605db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 0)
615db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return true;
625db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
635db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 1 &&
645db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      D.getTypeObject(0).Kind == DeclaratorChunk::Function)
655db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return true;
665db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
675db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  return false;
685db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner}
695db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
70930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
71930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
725db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// \param D  the declarator containing the declaration specifier.
735153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
745153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
758ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic QualType ConvertDeclSpecToType(Declarator &TheDeclarator, Sema &TheSema){
765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
785db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  const DeclSpec &DS = TheDeclarator.getDeclSpec();
795db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc();
805db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
815db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    DeclLoc = DS.getSourceRange().getBegin();
821564e3906cad604a42bd131e584751a75589a9c4Chris Lattner
831564e3906cad604a42bd131e584751a75589a9c4Chris Lattner  ASTContext &Context = TheSema.Context;
841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
855db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
8796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
8896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
8996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
92fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
94fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
98fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
100958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
10164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
10264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
10364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
10464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
1051564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
106f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
10764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
10864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
10964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
11064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
1111564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
112f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
11364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
11464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
11564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
116f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
117f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
118f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
119f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
120f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
121f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
122f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
123f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
124f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
125f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
126d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
12762f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
128097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
1291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
13014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                                (ObjCProtocolDecl**)PQ,
131683087ffcf21d2a22cd2d0424b7f119168b47a8eSteve Naroff                                                DS.getNumProtocolQualifiers());
13262f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
13362f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
1345db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
1355db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
1365db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
1378ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl    if (isOmittedBlockReturnType(TheDeclarator)) {
1385db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      Result = Context.DependentTy;
1395db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
1405db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
1411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
142d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
143d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
144d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
145d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
146d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
147d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
148d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
1491564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    if (TheSema.getLangOptions().ImplicitInt) {
15035d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
15135d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
1523f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
1531564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::ext_missing_declspec)
1543f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
155173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner        << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
156173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner                                                 "int");
1573f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
1584310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
159d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
160d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
161d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
162d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
1634310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
1641564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      if (TheSema.getLangOptions().CPlusPlus &&
1651564e3906cad604a42bd131e584751a75589a9c4Chris Lattner          !TheSema.getLangOptions().Microsoft) {
1661564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::err_missing_type_specifier)
1673f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
1681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
169b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
170b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
171b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
1725db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner        TheDeclarator.setInvalidType(true);
173b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
1741564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier)
1753f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
176b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
177d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
1781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
1803cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
1815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
183fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
184fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
185fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
186311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
187311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
188311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
189311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
190311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        if (!TheSema.getLangOptions().C99 &&
191311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner            !TheSema.getLangOptions().CPlusPlus0x)
192311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner          TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
193311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
197fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
198fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
199fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
200311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
201311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
202311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
203311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
204311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        if (!TheSema.getLangOptions().C99 &&
205311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner            !TheSema.getLangOptions().CPlusPlus0x)
206311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner          TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
207311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
210958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
2113cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
212fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
213958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
214958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
215fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
216958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
217fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
218958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
219fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
2205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
2215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
2225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
2231564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
2248f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
2255db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    TheDeclarator.setInvalidType(true);
2268f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
22799dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
2285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
2295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Decl *D = static_cast<Decl *>(DS.getTypeRep());
2326e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
2336e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
2346e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
2355db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      TheDeclarator.setInvalidType(true);
2366e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
2376e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
2386e24726524c2b51b31bb4b622aa678a46b024f42John McCall
2395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
2405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
2415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
2425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
2432ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    Result = Context.getTypeDeclType(cast<TypeDecl>(D));
2442191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
2452191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    // In C++, make an ElaboratedType.
2461564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    if (TheSema.getLangOptions().CPlusPlus) {
2472191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall      TagDecl::TagKind Tag
2482191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall        = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType());
2492191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall      Result = Context.getElaboratedType(Result, Tag);
2502191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    }
2511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2525153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    if (D->isInvalidDecl())
2535db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      TheDeclarator.setInvalidType(true);
254958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
2551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2561a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
2575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
2585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
2595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
2601564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    Result = TheSema.GetTypeFromParser(DS.getTypeRep());
2612ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
2621a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
263f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis      if (const ObjCInterfaceType *
264f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis            Interface = Result->getAs<ObjCInterfaceType>()) {
26567ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // It would be nice if protocol qualifiers were only stored with the
26667ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // ObjCObjectPointerType. Unfortunately, this isn't possible due
26767ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // to the following typedef idiom (which is uncommon, but allowed):
2681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //
26967ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // typedef Foo<P> T;
27067ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // static void func() {
27167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        //   Foo<P> *yy;
27267ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        //   T *zz;
27367ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // }
274c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff        Result = Context.getObjCInterfaceType(Interface->getDecl(),
275c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                              (ObjCProtocolDecl**)PQ,
276c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                              DS.getNumProtocolQualifiers());
277f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis      } else if (Result->isObjCIdType())
278ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
2791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
28014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                        (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
28114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      else if (Result->isObjCClassType()) {
2824262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
2831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy,
284470301bac9c8abfc6b451b3b669c6695a9fd1518Steve Naroff                        (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
2853f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
2861564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
2873f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
2885db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner        TheDeclarator.setInvalidType(true);
2893f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
290c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
2911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
292eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    // If this is a reference to an invalid typedef, propagate the invalidity.
293eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    if (TypedefType *TDT = dyn_cast<TypedefType>(Result))
294eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner      if (TDT->getDecl()->isInvalidDecl())
2955db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner        TheDeclarator.setInvalidType(true);
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.
31172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    Result = Context.getTypeOfExprType(E);
312958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
313d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
3146fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
3156fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    Expr *E = static_cast<Expr *>(DS.getTypeRep());
3166fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
3176fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
3181564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    Result = TheSema.BuildDecltypeType(E);
319af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
320af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
3215db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      TheDeclarator.setInvalidType(true);
322af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
3236fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
3246fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
325e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
326e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
327e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    Result = Context.UndeducedAutoTy;
328e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
329e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
3301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
331809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
3325153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
3335db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    TheDeclarator.setInvalidType(true);
3345153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
337958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
338f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
3391564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    if (TheSema.getLangOptions().Freestanding)
3401564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
341fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
342f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
3431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
344958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
345958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner         "FIXME: imaginary types not supported yet!");
3461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34738d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // See if there are any attributes on the declspec that apply to the type (as
34838d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // opposed to the decl).
349fca0ddd42965e0b7ae821213486d4e0dd71fb439Chris Lattner  if (const AttributeList *AL = DS.getAttributes())
3501564e3906cad604a42bd131e584751a75589a9c4Chris Lattner    TheSema.ProcessTypeAttributeList(Result, AL);
3511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
35396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
35496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
35596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
35696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
35796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
3580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (TypeQuals & DeclSpec::TQ_restrict) {
359bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar      if (Result->isPointerType() || Result->isReferenceType()) {
3601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        QualType EltTy = Result->isPointerType() ?
3616217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek          Result->getAs<PointerType>()->getPointeeType() :
3626217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek          Result->getAs<ReferenceType>()->getPointeeType();
3631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
364bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
365bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
366bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
3671564e3906cad604a42bd131e584751a75589a9c4Chris Lattner          TheSema.Diag(DS.getRestrictSpecLoc(),
368d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
369d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
3700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall          TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
371bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
372bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
3731564e3906cad604a42bd131e584751a75589a9c4Chris Lattner        TheSema.Diag(DS.getRestrictSpecLoc(),
374d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner             diag::err_typecheck_invalid_restrict_not_pointer)
375d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
3760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
37796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
37896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
3791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
38196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
38296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
38396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
38496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
38596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
3860953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
38796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
3880953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
38996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
3900953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
3910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        assert((TypeQuals & DeclSpec::TQ_restrict) &&
3920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall               "Has CVR quals but not C, V, or R?");
3930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Loc = DS.getRestrictSpecLoc();
39496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
3951564e3906cad604a42bd131e584751a75589a9c4Chris Lattner      TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
396d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
39796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
3981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
399f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
400f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
401f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
402f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
403f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
4041a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
4051a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
406f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
4070953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
4080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
4091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
4120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Result = Context.getQualifiedType(Result, Quals);
41396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
4140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
415f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
416f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
417f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
418cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
419cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
420cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
4211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
422cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
423cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
424cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
425cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
426cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
427cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
428cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
429cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the pointer type.
430cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
431cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
432cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
433cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
434cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
435cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
436cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
437cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
438cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
439cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
4401eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildPointerType(QualType T, unsigned Quals,
441cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
442cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
443cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
444cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
445cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
446cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
447cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
448cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
4490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
4500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
451cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
452cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
4530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) {
454cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
455cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
4560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qs.removeRestrict();
457cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
458cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
459cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
4600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return Context.getQualifiedType(Context.getPointerType(T), Qs);
461cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
462cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
463cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
464cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
465cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
466cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
4670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR The cvr-qualifiers to be applied to the reference type.
468cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
469cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
470cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
471cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
472cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
473cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
474cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
475cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
476cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
477cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
47854e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
47954e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  unsigned CVR, SourceLocation Loc,
48054e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
4810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
48254e14c4db764c0636160d26c5bbf491637c83a76John McCall
48354e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
48454e14c4db764c0636160d26c5bbf491637c83a76John McCall
48554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
48654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   reference to a type T, and attempt to create the type "lvalue
48754e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   reference to cv TD" creates the type "lvalue reference to T".
48854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // We use the qualifiers (restrict or none) of the original reference,
48954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // not the new ones. This is consistent with GCC.
49054e14c4db764c0636160d26c5bbf491637c83a76John McCall
49154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
49254e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
49354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
49454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
49554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
49654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
49754e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
49854e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
49954e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
50054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
50154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
50254e14c4db764c0636160d26c5bbf491637c83a76John McCall  // collapsing of references-to-references as described in C++
50354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // DR 106 and amended by C++ DR 540.
504cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
505cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
50633a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
507cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
508cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
509cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
510cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
511cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
512cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
513cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
514cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
5150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
516cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
517cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
5180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Quals.removeRestrict();
519cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
520cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
521cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
522cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   [...] Cv-qualified references are ill-formed except when the
523cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   cv-qualifiers are introduced through the use of a typedef
524cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   (7.1.3) or of a template type argument (14.3), in which case
525cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   the cv-qualifiers are ignored.
526cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //
527cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // We diagnose extraneous cv-qualifiers for the non-typedef,
528cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // non-template type argument case within the parser. Here, we just
529cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // ignore any extraneous cv-qualifiers.
5300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.removeConst();
5310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.removeVolatile();
532cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
533cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
5347c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
53554e14c4db764c0636160d26c5bbf491637c83a76John McCall    return Context.getQualifiedType(
53654e14c4db764c0636160d26c5bbf491637c83a76John McCall               Context.getLValueReferenceType(T, SpelledAsLValue), Quals);
5370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals);
538cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
539cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
540cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
541cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
542cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
543cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
544cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
5451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
5461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
547cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
548cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the array's
549cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// element type.
550cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
551cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
552cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// array type or, if there is no such entity, the location of the
553cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have array type.
554cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
555cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
556cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
557cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
558cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
559cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
560cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
561cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
5627e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
5630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
5647e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
5651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // C99 6.7.5.2p1: If the element type is an incomplete or function type,
566cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (RequireCompleteType(Loc, T,
568cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                             diag::err_illegal_decl_array_incomplete_type))
569cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
570cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
571cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
572cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
573cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
574cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
575cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
577cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ 8.3.2p4: There shall be no ... arrays of references ...
578cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
579cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_references)
580cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
581cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
5821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
583cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
584e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Loc,  diag::err_illegal_decl_array_of_auto)
586e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson      << getPrintableNameForEntity(Entity);
587e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
588e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
5891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5906217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
591cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
592cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
593cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
594cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
595cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (T->isObjCInterfaceType()) {
596c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
597c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
598cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
600cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
601cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (ArraySize && !ArraySize->isTypeDependent() &&
602cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      !ArraySize->getType()->isIntegerType()) {
603cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
604cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
605cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    ArraySize->Destroy(Context);
606cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
607cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
608cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  llvm::APSInt ConstVal(32);
609cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
610f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
6117e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
612f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
613f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
614cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (ArraySize->isValueDependent()) {
6157e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
616cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
617cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor             (!T->isDependentType() && !T->isConstantSizeType())) {
618cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Per C99, a variable array is an array with either a non-constant
619cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // size or an element type that has a non-constant-size
6207e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
621cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
622cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
623cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
624cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (ConstVal.isSigned()) {
625cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      if (ConstVal.isNegative()) {
626cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        Diag(ArraySize->getLocStart(),
627cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor             diag::err_typecheck_negative_array_size)
628cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor          << ArraySize->getSourceRange();
629cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        return QualType();
630cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      } else if (ConstVal == 0) {
631cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        // GCC accepts zero sized static arrays.
632cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
633cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor          << ArraySize->getSourceRange();
634cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      }
6351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
63646a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
637cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
638cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
639cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!getLangOptions().C99) {
6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (ArraySize && !ArraySize->isTypeDependent() &&
6411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        !ArraySize->isValueDependent() &&
642cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        !ArraySize->isIntegerConstantExpr(Context))
643043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
644cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    else if (ASM != ArrayType::Normal || Quals != 0)
645043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      Diag(Loc,
646043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor           getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
647043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor                                     : diag::ext_c99_array_usage);
648cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
649cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
650cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
651cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
6529cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6539cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
6549cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
6559cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
6561eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
6579cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
6589cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6599cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Expr *Arg = (Expr *)ArraySize.get();
6609cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6619cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
6629cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
6631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
6649cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
6659cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
6669cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
6679cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
6689cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6699cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
6709cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
6719cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
6729cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
6739cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      << "ext_vector_type" << Arg->getSourceRange();
6749cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
6759cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
6789cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
6791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6819cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
6829cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
6839cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      << Arg->getSourceRange();
6849cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
6859cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
6861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6879cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!T->isDependentType())
6889cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return Context.getExtVectorType(T, vectorSize);
6891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
6901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
6929cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                AttrLoc);
6939cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
6941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
695724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
696724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
697724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
698724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
699724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
7002943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
701724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
702724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
703724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
704724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
705724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
706724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
707724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
708724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
709724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
710724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
711724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
712724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
713724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
714724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
715724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
716724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
717724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
718724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
719724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
720724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
721724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
722724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
723724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
724724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
7251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 QualType *ParamTypes,
726724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
727724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 bool Variadic, unsigned Quals,
728724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 SourceLocation Loc, DeclarationName Entity) {
729724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
730724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    Diag(Loc, diag::err_func_returning_array_function) << T;
731724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
732724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
7331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
734724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
735724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
7362dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    QualType ParamType = adjustParameterType(ParamTypes[Idx]);
7372dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
738724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
739724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
740724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
741cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
74254e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
743724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
744724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
745724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
746724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
747724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
7481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
749724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 Quals);
750724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
752949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
753949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
754949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
755949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
7560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR Qualifiers applied to the member pointer type
757949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
758949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
759949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
760949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
761949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
7621eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
7630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                      unsigned CVR, SourceLocation Loc,
764949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
7650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
7660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
767949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
768949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
769949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
770949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
771949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
772949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
773949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
774949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
775949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
776949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
777949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
778949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
779949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
780949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
781949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
782949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
7838d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
784949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
785949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
786949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
787949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
788949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
789949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
790949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
791949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
792949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
793949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
794949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
795949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // object or incomplete types shall not be restrict-qualified."
7960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
797949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
798949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << T;
799949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
800949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
801949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
8020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Quals.removeRestrict();
803949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
804949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
805949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
806949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
807949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
808949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
809949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
8100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return Context.getQualifiedType(
8110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall           Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
812949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
8131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8149a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
8159a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8169a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
8179a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
8199a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8209a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Loc The location of the entity whose type involves this
8219a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// block pointer type or, if there is no such entity, the location of the
8229a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type that will have block pointer type.
8239a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8249a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
8259a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
8269a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
8279a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
8289a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
8290953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
8301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
8319a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
8320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
8339a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
8349a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
8359a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
8361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
8380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
8399a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
8409a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
841e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios KyrtzidisQualType Sema::GetTypeFromParser(TypeTy *Ty, DeclaratorInfo **DInfo) {
842e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  QualType QT = QualType::getFromOpaquePtr(Ty);
843e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  DeclaratorInfo *DI = 0;
844e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
845e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
846e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    DI = LIT->getDeclaratorInfo();
847e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
8481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
849e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  if (DInfo) *DInfo = DI;
850e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
851e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
852e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
85398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
8548ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl/// declarator to Type instances.
855402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor///
856402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
857402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// owns the declaration of a type (e.g., the definition of a struct
858402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// type), then *OwnedDecl will receive the owned declaration.
859a1d5662d96465f0fddf8819d245da4d19b892effArgyrios KyrtzidisQualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
8608ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl                                    DeclaratorInfo **DInfo,
861402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor                                    TagDecl **OwnedDecl) {
862930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
863930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
864930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
865f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
866930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  switch (D.getKind()) {
867930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Abstract:
868930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Normal:
869db422dffb720ff41d0b60e228f45c685600ffa9eDouglas Gregor  case Declarator::DK_Operator:
8705db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  case Declarator::DK_TemplateId:
8718ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl    T = ConvertDeclSpecToType(D, *this);
8725db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
8735db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    if (!D.isInvalidType() && OwnedDecl && D.getDeclSpec().isTypeSpecOwned())
8745db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      *OwnedDecl = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
875930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
876930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
877930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Constructor:
878930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Destructor:
879930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Conversion:
880930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
881930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // "void" instead. Conversion operators will check their return
882930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // types separately.
883930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
884930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
885930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
886f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
887baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  if (T == Context.UndeducedAutoTy) {
888baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
8891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
890baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
891baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
892baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      assert(0 && "K&R type lists aren't allowed in C++");
893baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
894baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
895baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
896baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
897baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
898baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      switch (cast<TagDecl>(CurContext)->getTagKind()) {
899baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
900baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
901baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_union:  Error = 2; /* Union member */ break;
902baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_class:  Error = 3; /* Class member */ break;
9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
904baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
905baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
906baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 4; // Exception declaration
907baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
908baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
909baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 5; // Template parameter
910baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
911baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
912baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 6;  // Block literal
913baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
914baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
915baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
916baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
917baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
918baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TypeNameContext:
919baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
920baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
921baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
922baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
923baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
924baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
925baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      T = Context.IntTy;
926baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
927baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
928baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
9291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
930cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
931cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
932cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
933cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
9341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
93598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
93698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
93798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
9388ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
9398ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl    DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
9405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
9415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
9425618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
9439af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
9449af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
9459af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner        Diag(DeclType.Loc, diag::err_blocks_disable);
9461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
9489a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                Name);
9495618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
9505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
9516a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
9526a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
9536a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
9546a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
9556a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
9566a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
9576a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
95814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
959183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
96014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        T = Context.getObjCObjectPointerType(T,
96167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff                                         (ObjCProtocolDecl **)OIT->qual_begin(),
96267ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff                                         OIT->getNumProtocols());
96314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
96414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
965cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
9665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
9670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
9680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Qualifiers Quals;
9690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (DeclType.Ref.HasRestrict) Quals.addRestrict();
9700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
9716a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
9726a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
9736a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
9746a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
9756a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
9766a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
9776a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
9780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
979cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                             DeclType.Loc, Name);
9805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
9810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
9836a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
9846a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
9856a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
9866a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
9876a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
9886a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
9896a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
990fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
99194f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
9925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
9945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
9955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
9965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
9975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
9985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
999f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      if (ASM == ArrayType::Star &&
1000f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman          D.getContext() != Declarator::PrototypeContext) {
1001f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
1002f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
1003f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1004f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1005f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
1006f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
1007f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
10080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      T = BuildArrayType(T, ASM, ArraySize,
10090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                         Qualifiers::fromCVRMask(ATI.TypeQuals),
10107e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                         SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
10115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1013f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
10145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
10155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
10165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
10175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
10183cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
1019cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      // C99 6.7.5.3p1: The return type may not be a function or array type.
102068cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner      if (T->isArrayType() || T->isFunctionType()) {
1021d162584991885ab004a02573a73ce06422b921fcChris Lattner        Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
1022cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        T = Context.IntTy;
1023cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        D.setInvalidType(true);
1024cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      }
1025465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1026402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1027402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
1028402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
1029402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1030402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        if (Tag->isDefinition())
1031402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor          Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1032402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
1033402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
1034402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
10353cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
10363cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
10373cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      if (FTI.hasExceptionSpec &&
10383cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl          D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
10393cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl        Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
10403cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
1041eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      if (FTI.NumArgs == 0) {
1042c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        if (getLangOptions().CPlusPlus) {
1043c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1044c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // function takes no arguments.
1045465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          llvm::SmallVector<QualType, 4> Exceptions;
1046465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          Exceptions.reserve(FTI.NumExceptions);
10471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1048e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis            // FIXME: Preserve type source info.
1049e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis            QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1050ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            // Check that the type is valid for an exception spec, and drop it
1051ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            // if not.
1052ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1053ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl              Exceptions.push_back(ET);
1054ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          }
1055465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1056465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                      FTI.hasExceptionSpec,
1057465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                      FTI.hasAnyExceptionSpec,
1058ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl                                      Exceptions.size(), Exceptions.data());
1059965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor        } else if (FTI.isVariadic) {
1060965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // We allow a zero-parameter variadic function in C if the
1061965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // function is marked with the "overloadable"
1062965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // attribute. Scan for this attribute now.
1063965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
1064965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
1065965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
1066965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
1067965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
1068965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
1069965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
1070965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
1071965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
1072965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
1073965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1074965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
1075c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        } else {
1076c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // Simple void foo(), where the incoming T is the result type.
107772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          T = Context.getFunctionNoProtoType(T);
1078c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
1079eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      } else if (FTI.ArgInfo[0].Param == 0) {
10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
10811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
108254e14c4db764c0636160d26c5bbf491637c83a76John McCall        D.setInvalidType(true);
10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else {
10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
10871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1089b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner          ParmVarDecl *Param =
1090b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner            cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
10918123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
109278c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
10932dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
10942dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
1095beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor          assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
10962dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
109972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
11002dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
11055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
11062ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
11078123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
11082ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
11092ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
11114565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
11122ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
11138123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
11142ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
11152ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
11160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
11172ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
11181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11192ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
11202ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
11212ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
1122eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
1123eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
1124a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
1125183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
1126eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
1127eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
1128eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
11301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
113154e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
1133465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1134465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        llvm::SmallVector<QualType, 4> Exceptions;
1135465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        Exceptions.reserve(FTI.NumExceptions);
11361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1137e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis          // FIXME: Preserve type source info.
1138e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis          QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1139ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          // Check that the type is valid for an exception spec, and drop it if
1140ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          // not.
1141ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1142ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            Exceptions.push_back(ET);
1143ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl        }
1144465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1145beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
1146465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.isVariadic, FTI.TypeQuals,
1147465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.hasExceptionSpec,
1148465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.hasAnyExceptionSpec,
1149ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl                                    Exceptions.size(), Exceptions.data());
11505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
11525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1153f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
11544994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      // Verify that we're not building a pointer to pointer to function with
11554994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      // exception specification.
11564994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
11574994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
11584994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl        D.setInvalidType(true);
11594994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl        // Build the type anyway.
11604994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      }
1161f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
1162f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
1163949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (isDependentScopeSpecifier(DeclType.Mem.Scope())) {
11641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
1165949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
1166949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        assert(NNS->getAsType() && "Nested-name-specifier must name a type");
1167949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        ClsType = QualType(NNS->getAsType(), 0);
11681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else if (CXXRecordDecl *RD
1169949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                   = dyn_cast_or_null<CXXRecordDecl>(
1170949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                    computeDeclContext(DeclType.Mem.Scope()))) {
1171f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.getTagDeclType(RD);
1172f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
1173949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        Diag(DeclType.Mem.Scope().getBeginLoc(),
1174949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
1175949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1176949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
1177f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
1178f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1179f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1180949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
1181949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1182949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                   DeclType.Loc, D.getIdentifier());
1183949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
1184f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
1185949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
1186f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1187f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
1188f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
1189f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1190cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
1191cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
1192cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
1193cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
1194cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1195c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
1196c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    if (const AttributeList *AL = DeclType.getAttrs())
1197c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      ProcessTypeAttributeList(T, AL);
11985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1199971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1200971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
1201183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
1202778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
1203971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1204971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1205971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // for a nonstatic member function, the function type to which a pointer
1206971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // to member refers, or the top-level function type of a function typedef
1207971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // declaration.
1208971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    if (FnTy->getTypeQuals() != 0 &&
1209971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
1210584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor        ((D.getContext() != Declarator::MemberContext &&
1211584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor          (!D.getCXXScopeSpec().isSet() ||
1212f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor           !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1213f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor              ->isRecord())) ||
1214971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
1215971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      if (D.isFunctionDeclarator())
1216971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1217971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      else
1218971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(),
1219971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis             diag::err_invalid_qualified_typedef_function_type_use);
1220971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1221971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      // Strip the cv-quals from the type.
1222971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
12237fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                                  FnTy->getNumArgs(), FnTy->isVariadic(), 0);
1224971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
1225971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
12261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12270bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // If there were any type attributes applied to the decl itself (not the
12280bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // type, apply the type attribute to the type!)
12290bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  if (const AttributeList *Attrs = D.getAttributes())
1230c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(T, Attrs);
12314adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
123254e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (DInfo) {
123354e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (D.isInvalidType())
123454e14c4db764c0636160d26c5bbf491637c83a76John McCall      *DInfo = 0;
123554e14c4db764c0636160d26c5bbf491637c83a76John McCall    else
12368ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl      *DInfo = GetDeclaratorInfoForDeclarator(D, T);
123754e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
12384adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
12395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T;
12405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
124251bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
124351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
124451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
1245f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
124651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
124751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
1248f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
124951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
125051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
125151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
125251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
125351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
125451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
125551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
125651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
125754e14c4db764c0636160d26c5bbf491637c83a76John McCall
125854e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
125954e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
126054e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
126154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
126254e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
126354e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
126454e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
126554e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
126654e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
126754e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
126854e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
126954e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
127051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
127154e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
127251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
127351bd803fbdade51d674598ed45da3d54190a656cJohn McCall
127454e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
127554e14c4db764c0636160d26c5bbf491637c83a76John McCall
127654e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
127754e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
127854e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
127954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setHasProtocolsAsWritten(true);
128054e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
128154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
128254e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
128354e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
128454e14c4db764c0636160d26c5bbf491637c83a76John McCall
128554e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
128654e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
128754e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setHasProtocolsAsWritten(false);
128854e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
128954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
129054e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
129154e14c4db764c0636160d26c5bbf491637c83a76John McCall
129254e14c4db764c0636160d26c5bbf491637c83a76John McCall      // This might not have been written with an inner type.
129354e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
129454e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setHasBaseTypeAsWritten(false);
129554e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.getBaseTypeLoc().initialize(SourceLocation());
129654e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
129754e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setHasBaseTypeAsWritten(true);
129851bd803fbdade51d674598ed45da3d54190a656cJohn McCall        Visit(TL.getBaseTypeLoc());
129954e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
130051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
130151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
130251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
130351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.initialize(DS.getTypeSpecTypeLoc());
130451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
130551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
1306eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
130751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
130851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
1309f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
131051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
131151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
13124adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
131351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
131451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      llvm::llvm_unreachable("qualified type locs not expected here!");
131551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
13164adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
131751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
131851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
131951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
13204adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
132151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
132251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
132351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
13244adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
132551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
132651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
132751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
132854e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setHasBaseTypeAsWritten(true);
132954e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setHasProtocolsAsWritten(false);
133054e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setLAngleLoc(SourceLocation());
133154e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setRAngleLoc(SourceLocation());
13324adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
133351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
133451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
133551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
133651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: nested name specifier
13374adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
133851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
133951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
134054e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
134154e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
134251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
134351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
134451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
134551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
134651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
134751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
134851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
134951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
135051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
135151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
135251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
135351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
135451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
135551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
135651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
135751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLParenLoc(Chunk.Loc);
135851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRParenLoc(Chunk.EndLoc);
135951bd803fbdade51d674598ed45da3d54190a656cJohn McCall
136051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
136154e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
13624adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis        ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
136354e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
13644adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
136551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
13664adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
13671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
136851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
136951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      llvm::llvm_unreachable("unsupported TypeLoc kind in declarator!");
13704adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
137151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
137251bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
13734adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
137451bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \brief Create and instantiate a DeclaratorInfo with type source information.
137551bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
137651bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
137751bd803fbdade51d674598ed45da3d54190a656cJohn McCallDeclaratorInfo *
13788ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian RedlSema::GetDeclaratorInfoForDeclarator(Declarator &D, QualType T) {
137951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  DeclaratorInfo *DInfo = Context.CreateDeclaratorInfo(T);
138051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  UnqualTypeLoc CurrTL = DInfo->getTypeLoc().getUnqualifiedLoc();
138151bd803fbdade51d674598ed45da3d54190a656cJohn McCall
13828ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
138351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
138451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
13854adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
1386f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
138751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
13884adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
13894adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  return DInfo;
13904adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
13914adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
13921bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis/// \brief Create a LocInfoType to hold the given QualType and DeclaratorInfo.
13931bb8a45f7386a23871598d05141a07af03067925Argyrios KyrtzidisQualType Sema::CreateLocInfoType(QualType T, DeclaratorInfo *DInfo) {
13941bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
13951bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
13961bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
13971bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
13981bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  new (LocT) LocInfoType(T, DInfo);
13991bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
14001bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
14011bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  return QualType(LocT, 0);
14021bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
14031bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
14041bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
14051bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
140635d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis  assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
140735d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
140835d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
14091bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
14101bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
1411a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
1412360300ca2ee298d585d29baf006519507d968812Fariborz Jahanian/// declarator
1413b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerQualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1414b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
1415306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  QualType T = MDecl->getResultType();
1416306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  llvm::SmallVector<QualType, 16> ArgTys;
14171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14183560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian  // Add the first two invisible argument types for self and _cmd.
1419f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  if (MDecl->isInstanceMethod()) {
1420a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
14211f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    selfTy = Context.getPointerType(selfTy);
14221f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    ArgTys.push_back(selfTy);
142389951a86b594513c2a013532ed45d197413b1087Chris Lattner  } else
1424a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ArgTys.push_back(Context.getObjCIdType());
1425a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ArgTys.push_back(Context.getObjCSelType());
14261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
142789951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
142889951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = MDecl->param_end(); PI != E; ++PI) {
142989951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType ArgTy = (*PI)->getType();
1430306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    assert(!ArgTy.isNull() && "Couldn't parse type?");
1431beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor    ArgTy = adjustParameterType(ArgTy);
1432306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    ArgTys.push_back(ArgTy);
1433306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  }
1434306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
14357fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                              MDecl->isVariadic(), 0);
1436306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  return T;
1437306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian}
1438306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
14399e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
14409e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
14419e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// they point to and return true. If T1 and T2 aren't pointer types
14429e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// or pointer-to-member types, or if they are not similar at this
14439e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// level, returns false and leaves T1 and T2 unchanged. Top-level
14449e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// qualifiers on T1 and T2 are ignored. This function will typically
14459e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// be called in a loop that successively "unwraps" pointer and
14469e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// pointer-to-member types to compare them at each level.
1447ecb81f28cb279b7d8e84296445a4131fa80b69a9Chris Lattnerbool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
14486217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const PointerType *T1PtrType = T1->getAs<PointerType>(),
14496217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek                    *T2PtrType = T2->getAs<PointerType>();
145057373266011f73418381b736015d8d2bb0381176Douglas Gregor  if (T1PtrType && T2PtrType) {
145157373266011f73418381b736015d8d2bb0381176Douglas Gregor    T1 = T1PtrType->getPointeeType();
145257373266011f73418381b736015d8d2bb0381176Douglas Gregor    T2 = T2PtrType->getPointeeType();
145357373266011f73418381b736015d8d2bb0381176Douglas Gregor    return true;
145457373266011f73418381b736015d8d2bb0381176Douglas Gregor  }
145557373266011f73418381b736015d8d2bb0381176Douglas Gregor
14566217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
14576217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek                          *T2MPType = T2->getAs<MemberPointerType>();
145821593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl  if (T1MPType && T2MPType &&
145921593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T1MPType->getClass()) ==
146021593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T2MPType->getClass())) {
14614433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T1 = T1MPType->getPointeeType();
14624433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T2 = T2MPType->getPointeeType();
14634433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    return true;
14644433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  }
146557373266011f73418381b736015d8d2bb0381176Douglas Gregor  return false;
146657373266011f73418381b736015d8d2bb0381176Douglas Gregor}
146757373266011f73418381b736015d8d2bb0381176Douglas Gregor
1468cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlSema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
14695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
14705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
14715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
14721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1473a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis  DeclaratorInfo *DInfo = 0;
1474402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  TagDecl *OwnedTag = 0;
14758ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  QualType T = GetTypeForDeclarator(D, S, &DInfo, &OwnedTag);
14765153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
1477809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
14785912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
1479402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  if (getLangOptions().CPlusPlus) {
1480402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
14816d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
14826d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
1483402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // C++0x [dcl.type]p3:
1484402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   A type-specifier-seq shall not define a class or enumeration
1485402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   unless it appears in the type-id of an alias-declaration
1486402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   (7.1.3).
1487402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    if (OwnedTag && OwnedTag->isDefinition())
1488402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1489402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        << Context.getTypeDeclType(OwnedTag);
1490402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
1491402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
1492e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  if (DInfo)
1493e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    T = CreateLocInfoType(T, DInfo);
1494e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
14955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T.getAsOpaquePtr();
14965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1498c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1499c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1500c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
1501c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
1502c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
1503232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
1504232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1505c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
1506c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
15071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
1508c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
15090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1510232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
1511232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1512232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
1513232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
1514c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1515c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1516232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
15171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1518232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
1519545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
1520f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1521c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1522232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1523545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1524232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
1525c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
1526dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1527dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
1528c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1529232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1530232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
1531efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
1532efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
1533efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
1534efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1535efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
1536efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
1537efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
1538efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
1539efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
1540efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
15410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
1542efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
1543efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
15440953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
1545efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
1546efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
1547efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
15481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
1549f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
1550c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
1551c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1552d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1553d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// specified type.  The attribute contains 1 argument, weak or strong.
15541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleObjCGCTypeAttribute(QualType &Type,
15553b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner                                      const AttributeList &Attr, Sema &S) {
15560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
15575934e75d98d99374f72722a69c5eefe026f35c74Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
1558d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1559d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
15601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1561d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
15621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!Attr.getParameterName()) {
1563ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1564ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
1565ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    return;
1566ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
15670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
1568ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (Attr.getNumArgs() != 0) {
1569d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1570d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1571d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
15721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Attr.getParameterName()->isStr("weak"))
15730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
1574d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else if (Attr.getParameterName()->isStr("strong"))
15750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
1576d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
1577d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1578d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      << "objc_gc" << Attr.getParameterName();
1579d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1580d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
15811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15823b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  Type = S.Context.getObjCGCQualType(Type, GCAttr);
1583d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1584d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
15852455636163fdd18581d7fdae816433f886d88213Mike Stump/// HandleNoReturnTypeAttribute - Process the noreturn attribute on the
15862455636163fdd18581d7fdae816433f886d88213Mike Stump/// specified type.  The attribute contains 0 arguments.
15871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleNoReturnTypeAttribute(QualType &Type,
15882455636163fdd18581d7fdae816433f886d88213Mike Stump                                        const AttributeList &Attr, Sema &S) {
15892455636163fdd18581d7fdae816433f886d88213Mike Stump  if (Attr.getNumArgs() != 0)
15902455636163fdd18581d7fdae816433f886d88213Mike Stump    return;
15912455636163fdd18581d7fdae816433f886d88213Mike Stump
15922455636163fdd18581d7fdae816433f886d88213Mike Stump  // We only apply this to a pointer to function or a pointer to block.
15932455636163fdd18581d7fdae816433f886d88213Mike Stump  if (!Type->isFunctionPointerType()
15942455636163fdd18581d7fdae816433f886d88213Mike Stump      && !Type->isBlockPointerType()
15952455636163fdd18581d7fdae816433f886d88213Mike Stump      && !Type->isFunctionType())
15962455636163fdd18581d7fdae816433f886d88213Mike Stump    return;
15972455636163fdd18581d7fdae816433f886d88213Mike Stump
15982455636163fdd18581d7fdae816433f886d88213Mike Stump  Type = S.Context.getNoReturnType(Type);
15992455636163fdd18581d7fdae816433f886d88213Mike Stump}
16002455636163fdd18581d7fdae816433f886d88213Mike Stump
1601c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnervoid Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
1602c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
1603c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
1604c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
1605c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
1606c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  for (; AL; AL = AL->getNext()) {
1607c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // If this is an attribute we can handle, do so now, otherwise, add it to
1608c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // the LeftOverAttrs list for rechaining.
1609c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    switch (AL->getKind()) {
1610c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
1611c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
1612c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1613c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
1614d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    case AttributeList::AT_objc_gc:
1615d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      HandleObjCGCTypeAttribute(Result, *AL, *this);
1616d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
16172455636163fdd18581d7fdae816433f886d88213Mike Stump    case AttributeList::AT_noreturn:
16182455636163fdd18581d7fdae816433f886d88213Mike Stump      HandleNoReturnTypeAttribute(Result, *AL, *this);
16192455636163fdd18581d7fdae816433f886d88213Mike Stump      break;
1620c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
1621c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  }
1622232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
1623232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
16241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
16254ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
16264ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
16274ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
162886447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
162986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
163086447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
163186447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
163286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
16334ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
16344ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
16354ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
16364ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
16374ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
16384ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
16391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @param PD The partial diagnostic that will be printed out if T is not a
1640b790661a15d93941d2c33a0ea328254277b3d7e3Anders Carlsson/// complete type.
16414ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
16424ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
16434ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
164491a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
16458c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               const PartialDiagnostic &PD,
16468c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               std::pair<SourceLocation,
16478c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                                         PartialDiagnostic> Note) {
164891a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  unsigned diag = PD.getDiagID();
16491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1650573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
1651573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
1652690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
1653690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
1654690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
16551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
1656690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
1657690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
16584ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
16594ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
16604ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
16614ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
1662d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
1663d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // class template specialization, try to instantiate it.
16646217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *Record = T->getAs<RecordType>()) {
16652943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
1666d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
16672943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
16682943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        if (Loc.isValid())
16699cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall          ClassTemplateSpec->setPointOfInstantiation(Loc);
16702943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
1671d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
16725842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor                                                      /*Complain=*/diag != 0);
16732943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor      }
16741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
1675d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1676d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1677b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
1678b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        assert(MSInfo && "Missing member specialization information?");
1679357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
1680b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        if (MSInfo->getTemplateSpecializationKind()
1681b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor                                               != TSK_ExplicitSpecialization) {
1682b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor          MSInfo->setPointOfInstantiation(Loc);
1683f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
1684f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
1685f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
1686f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  /*Complain=*/diag != 0);
1687b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        }
1688d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
1689d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
1690d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
16912943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
16925842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor  if (diag == 0)
16935842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
16941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16954ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
169691a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  Diag(Loc, PD) << T;
16973c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
16988c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  // If we have a note, produce it.
16998c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  if (!Note.first.isInvalid())
17008c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson    Diag(Note.first, Note.second);
17018c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson
17024ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
17031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // type, produce
17044ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  const TagType *Tag = 0;
17056217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *Record = T->getAs<RecordType>())
17064ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Record;
1707183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  else if (const EnumType *Enum = T->getAs<EnumType>())
17084ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Enum;
17094ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
17104ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
17111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
17124ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
17134ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
17144ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
17154ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
17164ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
17174ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
1718e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
1719e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor/// \brief Retrieve a version of the type 'T' that is qualified by the
1720e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor/// nested-name-specifier contained in SS.
1721e6258936178b4c52b43b3b9dbec13552961cd645Douglas GregorQualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1722e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor  if (!SS.isSet() || SS.isInvalid() || T.isNull())
1723e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
17241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1725ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  NestedNameSpecifier *NNS
17263507369940bfb269551bfa1fec812481f60e3552Douglas Gregor    = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
1727ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  return Context.getQualifiedNameType(NNS, T);
1728e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
1729af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
1730af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders CarlssonQualType Sema::BuildTypeofExprType(Expr *E) {
1731af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
1732af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
1733af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
1734af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders CarlssonQualType Sema::BuildDecltypeType(Expr *E) {
1735af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  if (E->getType() == Context.OverloadTy) {
17361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(E->getLocStart(),
1737af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson         diag::err_cannot_determine_declared_type_of_overloaded_function);
1738af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    return QualType();
1739af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  }
1740af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getDecltypeType(E);
1741af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
1742