SemaType.cpp revision 465226e23a3008bd68973513dda1f9e3cd27dbdd
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"
16980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
172943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor#include "clang/AST/DeclTemplate.h"
18e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
19e4858a65a93fb36c099d8dd2ea0a98e33e77687eDaniel Dunbar#include "clang/Parse/DeclSpec.h"
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
222dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// \brief Perform adjustment on the parameter type of a function.
232dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor///
242dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// This routine adjusts the given parameter type @p T to the actual
252dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
262dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
272dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorQualType Sema::adjustParameterType(QualType T) {
282dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // C99 6.7.5.3p7:
292dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T->isArrayType()) {
302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    // C99 6.7.5.3p7:
312dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   A declaration of a parameter as "array of type" shall be
322dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   adjusted to "qualified pointer to type", where the type
332dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   qualifiers (if any) are those specified within the [ and ] of
342dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   the array type derivation.
352dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getArrayDecayedType(T);
362dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  } else if (T->isFunctionType())
372dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    // C99 6.7.5.3p8:
382dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   A declaration of a parameter as "function returning type"
392dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   shall be adjusted to "pointer to function returning type", as
402dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   in 6.3.2.1.
412dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getPointerType(T);
422dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
432dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return T;
442dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
452dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
46930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
47930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
48930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \param DS  the declaration specifiers
493f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner/// \param DeclLoc The location of the declarator identifier or invalid if none.
505153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
515153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
523f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris LattnerQualType Sema::ConvertDeclSpecToType(const DeclSpec &DS,
53eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner                                     SourceLocation DeclLoc,
54eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner                                     bool &isInvalid) {
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
57958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  QualType Result;
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
6096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
6196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
6296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
65fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
67fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
71fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
73958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
7564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
7664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
7764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
78f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
79f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
8064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
8164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
8264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
8364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
84f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
85f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
8664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
8764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
8864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
89d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
9062f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
91097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
92ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner      Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
9362f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner                                              DS.getNumProtocolQualifiers());
9462f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
9562f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
9662f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner
97d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
98d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
99d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
100d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
101d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
102d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
103d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
104d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    if (getLangOptions().ImplicitInt) {
10535d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
10635d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
1073f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
1083f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        if (DeclLoc.isInvalid())
1093f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          DeclLoc = DS.getSourceRange().getBegin();
1103f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        Diag(DeclLoc, diag::warn_missing_declspec)
1113f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
112173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner        << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
113173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner                                                 "int");
1143f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
1154310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
116d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
117d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
118d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
119d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
1204310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
1213f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DeclLoc.isInvalid())
1223f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        DeclLoc = DS.getSourceRange().getBegin();
1233f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner
1243f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft)
1253f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        Diag(DeclLoc, diag::err_missing_type_specifier)
1263f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
1273f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      else
1283f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        Diag(DeclLoc, diag::warn_missing_type_specifier)
1293f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
130d17a9e5a29ef076e31d012d7254b526769c386f6Douglas Gregor
131390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump      // FIXME: If we could guarantee that the result would be well-formed, it
132390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump      // would be useful to have a code insertion hint here. However, after
133390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump      // emitting this warning/error, we often emit other errors.
134d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
135d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner
136d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // FALL THROUGH.
1373cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
140fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
141fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
142fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
143fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_longlong:    Result = Context.LongLongTy; break;
1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
1465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
147fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
148fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
149fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
150fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_longlong:    Result =Context.UnsignedLongLongTy; break;
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
153958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1543cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
155fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
156958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
157958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
158fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
159958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
160fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
161958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
162fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
1668f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
1678f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
1688f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    isInvalid = true;
1698f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
17099dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Decl *D = static_cast<Decl *>(DS.getTypeRep());
17599dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner    assert(D && "Didn't get a decl for a class/enum/union/struct?");
1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
1775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
1785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
1802ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    Result = Context.getTypeDeclType(cast<TypeDecl>(D));
1815153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner
1825153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    if (D->isInvalidDecl())
1835153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner      isInvalid = true;
184958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1861a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
1885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
1901a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
1912ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
1921a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
193390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump      // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so we have
194390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump      // this "hack" for now...
1951a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
1961a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor        Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
1971a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor                                                       (ObjCProtocolDecl**)PQ,
1981a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor                                               DS.getNumProtocolQualifiers());
1991a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      else if (Result == Context.getObjCIdType())
200ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
201ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
202ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner                                                DS.getNumProtocolQualifiers());
2033f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      else if (Result == Context.getObjCClassType()) {
2043f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        if (DeclLoc.isInvalid())
2053f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          DeclLoc = DS.getSourceRange().getBegin();
2064262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
2073f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        Diag(DeclLoc, diag::err_qualified_class_unsupported)
2083f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
2093f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
2103f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        if (DeclLoc.isInvalid())
2113f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          DeclLoc = DS.getSourceRange().getBegin();
2123f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
2133f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
214eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner        isInvalid = true;
2153f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
216c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
217eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner
218eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    // If this is a reference to an invalid typedef, propagate the invalidity.
219eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    if (TypedefType *TDT = dyn_cast<TypedefType>(Result))
220eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner      if (TDT->getDecl()->isInvalidDecl())
221eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner        isInvalid = true;
222eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner
2235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
224958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
226958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
227958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
228958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
229d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
230fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
231958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
232d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
233d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    Expr *E = static_cast<Expr *>(DS.getTypeRep());
234d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
235d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
23672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    Result = Context.getTypeOfExprType(E);
237958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
238d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
239809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
2405153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
2415153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    isInvalid = true;
2425153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
244958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
245958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
246f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
247f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor    if (getLangOptions().Freestanding)
248f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor      Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
249fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
250f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
251958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
252958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
253958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner         "FIXME: imaginary types not supported yet!");
254958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
25538d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // See if there are any attributes on the declspec that apply to the type (as
25638d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // opposed to the decl).
257fca0ddd42965e0b7ae821213486d4e0dd71fb439Chris Lattner  if (const AttributeList *AL = DS.getAttributes())
258c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(Result, AL);
259f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
26096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
26196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
26296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
26396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
26496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
26596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
26696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (TypeQuals & QualType::Restrict) {
267bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar      if (Result->isPointerType() || Result->isReferenceType()) {
268bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar        QualType EltTy = Result->isPointerType() ?
269bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar          Result->getAsPointerType()->getPointeeType() :
270bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar          Result->getAsReferenceType()->getPointeeType();
271bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner
272bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
273bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
274bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
275bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          Diag(DS.getRestrictSpecLoc(),
276d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
277d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
278bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
279bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
280bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
28196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Diag(DS.getRestrictSpecLoc(),
282d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner             diag::err_typecheck_invalid_restrict_not_pointer)
283d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
284bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
28596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
28696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
28796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
28896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
28996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
29096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
29196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
29296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
29396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
29496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if (TypeQuals & QualType::Const)
29596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
29696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      else {
29796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        assert((TypeQuals & QualType::Volatile) &&
29896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner               "Has CV quals but not C or V?");
29996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
30096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
301d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, diag::warn_typecheck_function_qualifiers)
302d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
30396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
30496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
305f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
306f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
307f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
308f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
309f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
3101a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
3111a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
312f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
313f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Const;
314f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Volatile;
315f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    }
316f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor
31796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Result.getQualifiedType(TypeQuals);
31896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
319f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
320f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
321f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
322cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
323cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
324cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
325cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
326cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
327cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
328cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
329cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
330cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
331cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
332cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
333cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the pointer type.
334cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
335cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
336cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
337cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
338cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
339cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
340cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
341cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
342cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
343cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
344cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildPointerType(QualType T, unsigned Quals,
345cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
346cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
347cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
348cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
349cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
350cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
351cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
352cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
353cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
354cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
355cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
356cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
357cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
358cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Quals &= ~QualType::Restrict;
359cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
360cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
361cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
362cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return Context.getPointerType(T).getQualifiedType(Quals);
363cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
364cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
365cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
366cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
367cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
368cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
369cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the reference type.
370cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
371cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
372cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
373cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
374cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
375cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
376cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
377cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
378cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
379cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
3807c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals,
381cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                  SourceLocation Loc, DeclarationName Entity) {
3827c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef) {
3837c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    if (const RValueReferenceType *R = T->getAsRValueReferenceType()) {
384dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
385dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      //   reference to a type T, and attempt to create the type "lvalue
386dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      //   reference to cv TD" creates the type "lvalue reference to T".
387dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      // We use the qualifiers (restrict or none) of the original reference,
388dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      // not the new ones. This is consistent with GCC.
3897c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      return Context.getLValueReferenceType(R->getPointeeType()).
390dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl               getQualifiedType(T.getCVRQualifiers());
3917c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    }
3927c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
393cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
394cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ [dcl.ref]p4: There shall be no references to references.
395cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //
396cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // According to C++ DR 106, references to references are only
397cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // diagnosed when they are written directly (e.g., "int & &"),
398cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // but not when they happen via a typedef:
399cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //
400cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //   typedef int& intref;
401cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //   typedef intref& intref2;
402cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //
403cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Parser::ParserDeclaratorInternal diagnoses the case where
404cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // references are written directly; here, we handle the
405cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // collapsing of references-to-references as described in C++
406cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // DR 106 and amended by C++ DR 540.
407cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return T;
408cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
409cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
410cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
411cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   A declarator that specifies the type “reference to cv void”
412cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
413cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
414cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
415cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
416cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
417cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
418cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
419cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
420cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
421cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
422cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
423cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Quals &= ~QualType::Restrict;
424cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
425cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
426cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
427cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   [...] Cv-qualified references are ill-formed except when the
428cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   cv-qualifiers are introduced through the use of a typedef
429cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   (7.1.3) or of a template type argument (14.3), in which case
430cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   the cv-qualifiers are ignored.
431cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //
432cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // We diagnose extraneous cv-qualifiers for the non-typedef,
433cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // non-template type argument case within the parser. Here, we just
434cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // ignore any extraneous cv-qualifiers.
435cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  Quals &= ~QualType::Const;
436cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  Quals &= ~QualType::Volatile;
437cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
438cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
4397c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
4407c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return Context.getLValueReferenceType(T).getQualifiedType(Quals);
4417c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return Context.getRValueReferenceType(T).getQualifiedType(Quals);
442cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
443cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
444cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
445cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
446cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
447cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
448cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
449cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
450cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ArraySize Expression describing the size of the array.
451cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
452cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the array's
453cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// element type.
454cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
455cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
456cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// array type or, if there is no such entity, the location of the
457cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have array type.
458cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
459cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
460cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
461cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
462cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
463cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
464cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
465cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
466cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              SourceLocation Loc, DeclarationName Entity) {
467cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: If the element type is an incomplete or function type,
468cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
46986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor  if (RequireCompleteType(Loc, T,
470cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                             diag::err_illegal_decl_array_incomplete_type))
471cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
472cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
473cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
474cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
475cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
476cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
477cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
478cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
479cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ 8.3.2p4: There shall be no ... arrays of references ...
480cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
481cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_references)
482cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
483cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
484cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
485cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
486cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (const RecordType *EltTy = T->getAsRecordType()) {
487cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
488cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
489cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
490cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
491cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (T->isObjCInterfaceType()) {
492c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
493c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
494cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
495cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
496cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
497cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (ArraySize && !ArraySize->isTypeDependent() &&
498cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      !ArraySize->getType()->isIntegerType()) {
499cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
500cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
501cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    ArraySize->Destroy(Context);
502cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
503cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
504cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  llvm::APSInt ConstVal(32);
505cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
506f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
507f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getVariableArrayType(T, 0, ASM, Quals);
508f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
509f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
510cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (ArraySize->isValueDependent()) {
511cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals);
512cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
513cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor             (!T->isDependentType() && !T->isConstantSizeType())) {
514cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Per C99, a variable array is an array with either a non-constant
515cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // size or an element type that has a non-constant-size
516cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals);
517cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
518cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
519cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
520cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (ConstVal.isSigned()) {
521cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      if (ConstVal.isNegative()) {
522cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        Diag(ArraySize->getLocStart(),
523cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor             diag::err_typecheck_negative_array_size)
524cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor          << ArraySize->getSourceRange();
525cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        return QualType();
526cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      } else if (ConstVal == 0) {
527cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        // GCC accepts zero sized static arrays.
528cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
529cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor          << ArraySize->getSourceRange();
530cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      }
531cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
532cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
533cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
534cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
535cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!getLangOptions().C99) {
536cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (ArraySize && !ArraySize->isTypeDependent() &&
537cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        !ArraySize->isValueDependent() &&
538cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        !ArraySize->isIntegerConstantExpr(Context))
539cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_vla);
540cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    else if (ASM != ArrayType::Normal || Quals != 0)
541cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_c99_array_usage);
542cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
543cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
544cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
545cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
546cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
547724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
548724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
549724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
550724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
551724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
5522943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
553724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
554724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
555724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
556724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
557724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
558724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
559724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
560724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
561724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
562724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
563724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
564724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
565724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
566724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
567724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
568724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
569724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
570724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
571724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
572724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
573724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
574724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
575724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
576724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
577724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 QualType *ParamTypes,
578724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
579724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 bool Variadic, unsigned Quals,
580724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 SourceLocation Loc, DeclarationName Entity) {
581724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
582724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    Diag(Loc, diag::err_func_returning_array_function) << T;
583724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
584724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
585724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
586724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
587724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
5882dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    QualType ParamType = adjustParameterType(ParamTypes[Idx]);
5892dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
590724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
591724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
592724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
593cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
594724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    ParamTypes[Idx] = ParamType;
595724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
596724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
597724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
598724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
599724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
600724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
601724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 Quals);
602724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
603724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
60498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
60598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// declarator to Type instances. Skip the outermost Skip type
60698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// objects.
607cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlQualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
60898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  bool OmittedReturnType = false;
60998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
61098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  if (D.getContext() == Declarator::BlockLiteralContext
61198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && Skip == 0
61298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && !D.getDeclSpec().hasTypeSpecifier()
61398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && (D.getNumTypeObjects() == 0
61498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump          || (D.getNumTypeObjects() == 1
61598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump              && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
61698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    OmittedReturnType = true;
61798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
618b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner  // long long is a C99 feature.
619d1eb332181f02a7e0a6f9749265e6a0f55555cd4Chris Lattner  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
620b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner      D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
621b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner    Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
622930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
623930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
624930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
625930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
626930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  switch (D.getKind()) {
627930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Abstract:
628930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Normal:
62998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  case Declarator::DK_Operator: {
6303f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner    const DeclSpec &DS = D.getDeclSpec();
6313f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner    if (OmittedReturnType) {
63298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // We default to a dependent type initially.  Can be modified by
63398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // the first return statement.
63498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      T = Context.DependentTy;
6353f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner    } else {
636eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner      bool isInvalid = false;
637eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner      T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid);
638eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner      if (isInvalid)
639eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner        D.setInvalidType(true);
640809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    }
641930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
64298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  }
643930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
644930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Constructor:
645930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Destructor:
646930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Conversion:
647930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
648930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // "void" instead. Conversion operators will check their return
649930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // types separately.
650930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
651930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
652930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
6534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
654cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
655cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
656cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
657cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
658cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
65998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
66098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
66198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
662cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
663cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
6645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
6655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
6665618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
6679af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
6689af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
6699af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner        Diag(DeclType.Loc, diag::err_blocks_disable);
6709af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner
6715618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      if (!T.getTypePtr()->isFunctionType())
6725618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
6735618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      else
6747bc8d964405ce3b0b95091cdb66a391e50275b3cMike Stump        T = (Context.getBlockPointerType(T)
6757bc8d964405ce3b0b95091cdb66a391e50275b3cMike Stump             .getQualifiedType(DeclType.Cls.TypeQuals));
6765618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
6775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
678cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
6795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
680cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    case DeclaratorChunk::Reference:
6817c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      T = BuildReferenceType(T, DeclType.Ref.LValueRef,
6827c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl                             DeclType.Ref.HasRestrict ? QualType::Restrict : 0,
683cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                             DeclType.Loc, Name);
6845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
6855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
686fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
68794f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
6885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
6895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
6905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
6915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
6925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
6935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
6945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
695f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      if (ASM == ArrayType::Star &&
696f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman          D.getContext() != Declarator::PrototypeContext) {
697f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
698f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
699f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
700f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
701f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
702f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
703f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
704cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, DeclType.Loc, Name);
7055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
7065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
707f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
7085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
7095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
7105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
7115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
71268cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner
713cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      // C99 6.7.5.3p1: The return type may not be a function or array type.
71468cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner      if (T->isArrayType() || T->isFunctionType()) {
715d162584991885ab004a02573a73ce06422b921fcChris Lattner        Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
716cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        T = Context.IntTy;
717cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        D.setInvalidType(true);
718cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      }
719465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
720eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      if (FTI.NumArgs == 0) {
721c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        if (getLangOptions().CPlusPlus) {
722c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
723c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // function takes no arguments.
724465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          llvm::SmallVector<QualType, 4> Exceptions;
725465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          Exceptions.reserve(FTI.NumExceptions);
726465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei)
727465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl            Exceptions.push_back(
728465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl              QualType::getFromOpaquePtr(FTI.Exceptions[ei]));
729465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
730465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                      FTI.hasExceptionSpec,
731465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                      FTI.hasAnyExceptionSpec,
732465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                      FTI.NumExceptions, Exceptions.data());
733965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor        } else if (FTI.isVariadic) {
734965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // We allow a zero-parameter variadic function in C if the
735965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // function is marked with the "overloadable"
736965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // attribute. Scan for this attribute now.
737965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
738965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
739965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
740965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
741965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
742965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
743965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
744965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
745965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
746965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
747965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
748965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
749c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        } else {
750c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // Simple void foo(), where the incoming T is the result type.
75172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          T = Context.getFunctionNoProtoType(T);
752c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
753eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      } else if (FTI.ArgInfo[0].Param == 0) {
7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
755eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman        Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
7565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else {
7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
7605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
762b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner          ParmVarDecl *Param =
763b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner            cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
7648123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
76578c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
7662dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
7672dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
768beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor          assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
7692dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
7705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
7715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
77272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
7732dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
7745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
7755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
7765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
7775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
7785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
7792ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
7808123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
7812ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
7822ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
7844565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
7852ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
7868123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
7872ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
7882ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
789f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner              if (ArgTy.getCVRQualifiers())
7902ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
7912ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner
7922ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
7932ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
7942ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
795eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
796eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
797eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              ArgTy = Context.IntTy;
798eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
799eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
800eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
801eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
8025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
8035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ArgTys.push_back(ArgTy);
8055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
806465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
807465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        llvm::SmallVector<QualType, 4> Exceptions;
808465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        Exceptions.reserve(FTI.NumExceptions);
809465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei)
810465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          Exceptions.push_back(QualType::getFromOpaquePtr(FTI.Exceptions[ei]));
811465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
812beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
813465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.isVariadic, FTI.TypeQuals,
814465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.hasExceptionSpec,
815465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.hasAnyExceptionSpec,
816465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.NumExceptions, Exceptions.data());
8175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
8185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
8195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
820f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
821f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
822e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor      DeclContext *DC = computeDeclContext(DeclType.Mem.Scope());
823f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
824f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // FIXME: Extend for dependent types when it's actually supported.
825f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // See ActOnCXXNestedNameSpecifier.
826f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
827f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.getTagDeclType(RD);
828f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
829f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        if (DC) {
830f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          Diag(DeclType.Mem.Scope().getBeginLoc(),
831f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl               diag::err_illegal_decl_mempointer_in_nonclass)
832f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl            << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
833f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl            << DeclType.Mem.Scope().getRange();
834f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        }
835f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
836f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.IntTy;
837f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
838f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
839f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
840f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      //   with reference type, or "cv void."
841f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (T->isReferenceType()) {
842f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
843f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
844f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
845f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
846f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
847f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (T->isVoidType()) {
848f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
849f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
850f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
851f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
852f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
853f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
854f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // object or incomplete types shall not be restrict-qualified."
855f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
856f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          !T->isIncompleteOrObjectType()) {
857f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
858f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << T;
859f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        DeclType.Mem.TypeQuals &= ~QualType::Restrict;
860f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
861f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
8624433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl      T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
8634433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl                    getQualifiedType(DeclType.Mem.TypeQuals);
864f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
865f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
866f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
867f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
868cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
869cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
870cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
871cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
872cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
873c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
874c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    if (const AttributeList *AL = DeclType.getAttrs())
875c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      ProcessTypeAttributeList(T, AL);
8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
877971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
878971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
87972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
88072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
881971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
882971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
883971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // for a nonstatic member function, the function type to which a pointer
884971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // to member refers, or the top-level function type of a function typedef
885971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // declaration.
886971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    if (FnTy->getTypeQuals() != 0 &&
887971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
888584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor        ((D.getContext() != Declarator::MemberContext &&
889584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor          (!D.getCXXScopeSpec().isSet() ||
890e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor           !computeDeclContext(D.getCXXScopeSpec())->isRecord())) ||
891971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
892971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      if (D.isFunctionDeclarator())
893971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
894971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      else
895971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(),
896971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis             diag::err_invalid_qualified_typedef_function_type_use);
897971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
898971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      // Strip the cv-quals from the type.
899971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
9007fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                                  FnTy->getNumArgs(), FnTy->isVariadic(), 0);
901971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
902971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
9035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9040bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // If there were any type attributes applied to the decl itself (not the
9050bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // type, apply the type attribute to the type!)
9060bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  if (const AttributeList *Attrs = D.getAttributes())
907c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(T, Attrs);
9080bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner
9095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T;
9105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
912a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
913360300ca2ee298d585d29baf006519507d968812Fariborz Jahanian/// declarator
914b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerQualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
915b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
916306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  QualType T = MDecl->getResultType();
917306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  llvm::SmallVector<QualType, 16> ArgTys;
918306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
9193560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian  // Add the first two invisible argument types for self and _cmd.
920f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  if (MDecl->isInstanceMethod()) {
921a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
9221f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    selfTy = Context.getPointerType(selfTy);
9231f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    ArgTys.push_back(selfTy);
92489951a86b594513c2a013532ed45d197413b1087Chris Lattner  } else
925a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ArgTys.push_back(Context.getObjCIdType());
926a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ArgTys.push_back(Context.getObjCSelType());
9273560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian
92889951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
92989951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = MDecl->param_end(); PI != E; ++PI) {
93089951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType ArgTy = (*PI)->getType();
931306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    assert(!ArgTy.isNull() && "Couldn't parse type?");
932beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor    ArgTy = adjustParameterType(ArgTy);
933306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    ArgTys.push_back(ArgTy);
934306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  }
935306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
9367fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                              MDecl->isVariadic(), 0);
937306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  return T;
938306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian}
939306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
9409e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
9419e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
9429e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// they point to and return true. If T1 and T2 aren't pointer types
9439e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// or pointer-to-member types, or if they are not similar at this
9449e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// level, returns false and leaves T1 and T2 unchanged. Top-level
9459e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// qualifiers on T1 and T2 are ignored. This function will typically
9469e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// be called in a loop that successively "unwraps" pointer and
9479e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// pointer-to-member types to compare them at each level.
948ecb81f28cb279b7d8e84296445a4131fa80b69a9Chris Lattnerbool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
94957373266011f73418381b736015d8d2bb0381176Douglas Gregor  const PointerType *T1PtrType = T1->getAsPointerType(),
95057373266011f73418381b736015d8d2bb0381176Douglas Gregor                    *T2PtrType = T2->getAsPointerType();
95157373266011f73418381b736015d8d2bb0381176Douglas Gregor  if (T1PtrType && T2PtrType) {
95257373266011f73418381b736015d8d2bb0381176Douglas Gregor    T1 = T1PtrType->getPointeeType();
95357373266011f73418381b736015d8d2bb0381176Douglas Gregor    T2 = T2PtrType->getPointeeType();
95457373266011f73418381b736015d8d2bb0381176Douglas Gregor    return true;
95557373266011f73418381b736015d8d2bb0381176Douglas Gregor  }
95657373266011f73418381b736015d8d2bb0381176Douglas Gregor
9574433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
9584433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl                          *T2MPType = T2->getAsMemberPointerType();
95921593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl  if (T1MPType && T2MPType &&
96021593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T1MPType->getClass()) ==
96121593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T2MPType->getClass())) {
9624433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T1 = T1MPType->getPointeeType();
9634433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T2 = T2MPType->getPointeeType();
9644433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    return true;
9654433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  }
96657373266011f73418381b736015d8d2bb0381176Douglas Gregor  return false;
96757373266011f73418381b736015d8d2bb0381176Douglas Gregor}
96857373266011f73418381b736015d8d2bb0381176Douglas Gregor
969cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlSema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
9705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
9715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
9735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
974cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  QualType T = GetTypeForDeclarator(D, S);
9755153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
976809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
9775912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
9786d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor  // Check that there are no default arguments (C++ only).
9796d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor  if (getLangOptions().CPlusPlus)
9806d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
9816d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T.getAsOpaquePtr();
9835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
985c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
986c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
987c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
988c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
989c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
990232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
991232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
992c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
993c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
994c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnerstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
995c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
996232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
997232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
998232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
999232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
1000c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1001c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1002232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1003232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
1004232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
1005545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
1006f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1007c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1008232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1009545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1010232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
1011c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
1012dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1013dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
1014c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1015232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1016232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
1017232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
1018f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
1019c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
1020c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1021d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1022d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// specified type.  The attribute contains 1 argument, weak or strong.
1023d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanianstatic void HandleObjCGCTypeAttribute(QualType &Type,
10243b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner                                      const AttributeList &Attr, Sema &S) {
1025d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (Type.getObjCGCAttr() != QualType::GCNone) {
10265934e75d98d99374f72722a69c5eefe026f35c74Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
1027d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1028d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
1029d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
1030d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
1031ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (!Attr.getParameterName()) {
1032ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1033ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
1034ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    return;
1035ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
10363b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  QualType::GCAttrTypes GCAttr;
1037ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (Attr.getNumArgs() != 0) {
1038d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1039d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1040d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
1041d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (Attr.getParameterName()->isStr("weak"))
10423b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner    GCAttr = QualType::Weak;
1043d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else if (Attr.getParameterName()->isStr("strong"))
10443b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner    GCAttr = QualType::Strong;
1045d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
1046d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1047d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      << "objc_gc" << Attr.getParameterName();
1048d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1049d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
1050d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
10513b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  Type = S.Context.getObjCGCQualType(Type, GCAttr);
1052d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1053d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
1054c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnervoid Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
1055c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
1056c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
1057c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
1058c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
1059c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  for (; AL; AL = AL->getNext()) {
1060c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // If this is an attribute we can handle, do so now, otherwise, add it to
1061c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // the LeftOverAttrs list for rechaining.
1062c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    switch (AL->getKind()) {
1063c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
1064c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
1065c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1066c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
1067d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    case AttributeList::AT_objc_gc:
1068d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      HandleObjCGCTypeAttribute(Result, *AL, *this);
1069d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
1070c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
1071c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  }
1072232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
1073232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
107486447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// @brief Ensure that the type T is a complete type.
10754ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10764ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
10774ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
107886447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
107986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
108086447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
108186447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
108286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
10834ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10844ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
10854ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
10864ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10874ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
10884ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10894ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param diag The diagnostic value (e.g.,
10904ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c diag::err_typecheck_decl_incomplete_type) that will be used
10914ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// for the error message if @p T is incomplete.
10924ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10934ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range1  An optional range in the source code that will be a
10944ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
10954ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10964ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range2  An optional range in the source code that will be a
10974ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
10984ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10994ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param PrintType If non-NULL, the type that should be printed
11004ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// instead of @p T. This parameter should be used when the type that
11014ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// we're checking for incompleteness isn't the type that should be
11024ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// displayed to the user, e.g., when T is a type and PrintType is a
11034ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// pointer to T.
11044ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
11054ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
11064ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
110786447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag,
11081efaa9594a81709a17658fd80ae7e783e1026407Chris Lattner                               SourceRange Range1, SourceRange Range2,
11091efaa9594a81709a17658fd80ae7e783e1026407Chris Lattner                               QualType PrintType) {
1110690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
1111690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
1112690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
1113690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //  assert(!T->isDependentType() &&
1114690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
1115690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
11164ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
11174ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
11184ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
11194ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
1120d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
1121d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // class template specialization, try to instantiate it.
1122d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (const RecordType *Record = T->getAsRecordType()) {
11232943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
1124d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
11252943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
11262943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        // Update the class template specialization's location to
11272943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        // refer to the point of instantiation.
11282943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        if (Loc.isValid())
11292943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor          ClassTemplateSpec->setLocation(Loc);
11302943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
11312943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor                                             /*ExplicitInstantiation=*/false);
11322943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor      }
1133d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    } else if (CXXRecordDecl *Rec
1134d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1135d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1136d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        // Find the class template specialization that surrounds this
1137d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        // member class.
1138d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        ClassTemplateSpecializationDecl *Spec = 0;
1139d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        for (DeclContext *Parent = Rec->getDeclContext();
1140d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor             Parent && !Spec; Parent = Parent->getParent())
1141d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
1142d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        assert(Spec && "Not a member of a class template specialization?");
114393dfdb1299ef740df854f4a745dc87e5e43f0c30Douglas Gregor        return InstantiateClass(Loc, Rec, Pattern, Spec->getTemplateArgs(),
114493dfdb1299ef740df854f4a745dc87e5e43f0c30Douglas Gregor                                /*ExplicitInstantiation=*/false);
1145d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
1146d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
1147d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
11482943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
11494ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (PrintType.isNull())
11504ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    PrintType = T;
11514ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
11524ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
11534ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  Diag(Loc, diag) << PrintType << Range1 << Range2;
11543c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
11554ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
11564ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // type, produce
11574ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  const TagType *Tag = 0;
11584ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (const RecordType *Record = T->getAsRecordType())
11594ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Record;
11604ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  else if (const EnumType *Enum = T->getAsEnumType())
11614ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Enum;
11624ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
11634ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
11644ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Diag(Tag->getDecl()->getLocation(),
11654ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
11664ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
11674ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
11684ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
11694ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
11704ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
1171e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
1172e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor/// \brief Retrieve a version of the type 'T' that is qualified by the
1173e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor/// nested-name-specifier contained in SS.
1174e6258936178b4c52b43b3b9dbec13552961cd645Douglas GregorQualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1175e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor  if (!SS.isSet() || SS.isInvalid() || T.isNull())
1176e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
1177e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
1178ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  NestedNameSpecifier *NNS
11793507369940bfb269551bfa1fec812481f60e3552Douglas Gregor    = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
1180ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  return Context.getQualifiedNameType(NNS, T);
1181e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
1182