SemaType.cpp revision f59a56e180bf54528d7d1d5afa68fcc13300965a
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"
1523c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl#include "SemaInherit.h"
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
17980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
182943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor#include "clang/AST/DeclTemplate.h"
19e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
20e4858a65a93fb36c099d8dd2ea0a98e33e77687eDaniel Dunbar#include "clang/Parse/DeclSpec.h"
214994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl#include "llvm/ADT/SmallPtrSet.h"
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
242dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// \brief Perform adjustment on the parameter type of a function.
252dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor///
262dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// This routine adjusts the given parameter type @p T to the actual
272dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
282dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
292dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorQualType Sema::adjustParameterType(QualType T) {
302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // C99 6.7.5.3p7:
312dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T->isArrayType()) {
322dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    // C99 6.7.5.3p7:
332dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   A declaration of a parameter as "array of type" shall be
342dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   adjusted to "qualified pointer to type", where the type
352dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   qualifiers (if any) are those specified within the [ and ] of
362dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   the array type derivation.
372dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getArrayDecayedType(T);
382dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  } else if (T->isFunctionType())
392dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    // C99 6.7.5.3p8:
402dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   A declaration of a parameter as "function returning type"
412dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   shall be adjusted to "pointer to function returning type", as
422dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   in 6.3.2.1.
432dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getPointerType(T);
442dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
452dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return T;
462dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
48930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
49930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
50930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \param DS  the declaration specifiers
513f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner/// \param DeclLoc The location of the declarator identifier or invalid if none.
525153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
535153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
543f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris LattnerQualType Sema::ConvertDeclSpecToType(const DeclSpec &DS,
55eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner                                     SourceLocation DeclLoc,
56eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner                                     bool &isInvalid) {
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
59958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  QualType Result;
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
6296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
6396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
6496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
67fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
69fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
73fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
75958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
7764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
7864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
7964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
80f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
81f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
8264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
8364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
8464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
8564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
86f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
87f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
8864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
8964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
9064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
91f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
92f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
93f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
94f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
95f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
96f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
97f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
98f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
99f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
100f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
101d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
10262f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
103097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
10414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      Result = Context.getObjCObjectPointerType(QualType(),
10514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                                (ObjCProtocolDecl**)PQ,
106683087ffcf21d2a22cd2d0424b7f119168b47a8eSteve Naroff                                                DS.getNumProtocolQualifiers());
10762f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
10862f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
10962f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner
110d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
111d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
112d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
113d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
114d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
115d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
116d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
117d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    if (getLangOptions().ImplicitInt) {
11835d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
11935d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
1203f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
1213f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        if (DeclLoc.isInvalid())
1223f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          DeclLoc = DS.getSourceRange().getBegin();
123fcff57751247d534159e0b51177cad5cc3e18ae7Eli Friedman        Diag(DeclLoc, diag::ext_missing_declspec)
1243f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
125173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner        << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
126173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner                                                 "int");
1273f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
1284310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
129d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
130d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
131d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
132d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
1334310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
1343f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DeclLoc.isInvalid())
1353f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        DeclLoc = DS.getSourceRange().getBegin();
1363f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner
137b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft) {
1383f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        Diag(DeclLoc, diag::err_missing_type_specifier)
1393f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
140b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner
141b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
142b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
143b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
144b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        isInvalid = true;
145b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
146fcff57751247d534159e0b51177cad5cc3e18ae7Eli Friedman        Diag(DeclLoc, diag::ext_missing_type_specifier)
1473f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
148b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
149d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
150d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner
151d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // FALL THROUGH.
1523cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
155fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
156fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
157fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
158fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_longlong:    Result = Context.LongLongTy; break;
1595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
162fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
163fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
164fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
165fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_longlong:    Result =Context.UnsignedLongLongTy; break;
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
168958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1693cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
170fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
171958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
172958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
173fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
174958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
175fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
176958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
177fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
1785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
1818f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
1828f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
1838f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    isInvalid = true;
1848f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
18599dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
1885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Decl *D = static_cast<Decl *>(DS.getTypeRep());
19099dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner    assert(D && "Didn't get a decl for a class/enum/union/struct?");
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
1952ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    Result = Context.getTypeDeclType(cast<TypeDecl>(D));
1965153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner
1975153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    if (D->isInvalidDecl())
1985153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner      isInvalid = true;
199958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2011a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
2051a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
2062ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
2071a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
2081a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
20967ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // It would be nice if protocol qualifiers were only stored with the
21067ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // ObjCObjectPointerType. Unfortunately, this isn't possible due
21167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // to the following typedef idiom (which is uncommon, but allowed):
21267ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        //
21367ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // typedef Foo<P> T;
21467ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // static void func() {
21567ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        //   Foo<P> *yy;
21667ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        //   T *zz;
21767ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        // }
218c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff        Result = Context.getObjCInterfaceType(Interface->getDecl(),
219c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                              (ObjCProtocolDecl**)PQ,
220c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                              DS.getNumProtocolQualifiers());
22114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      else if (Result->isObjCIdType())
222ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
22314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        Result = Context.getObjCObjectPointerType(QualType(),
22414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                        (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
22514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      else if (Result->isObjCClassType()) {
2263f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        if (DeclLoc.isInvalid())
2273f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          DeclLoc = DS.getSourceRange().getBegin();
2284262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
2293f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        Diag(DeclLoc, diag::err_qualified_class_unsupported)
2303f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
2313f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
2323f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        if (DeclLoc.isInvalid())
2333f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          DeclLoc = DS.getSourceRange().getBegin();
2343f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner        Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
2353f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
236eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner        isInvalid = true;
2373f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
238c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
239eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner
240eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    // If this is a reference to an invalid typedef, propagate the invalidity.
241eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner    if (TypedefType *TDT = dyn_cast<TypedefType>(Result))
242eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner      if (TDT->getDecl()->isInvalidDecl())
243eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner        isInvalid = true;
244eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner
2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
246958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
248958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
249958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
250958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
251d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
252fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
253958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
254d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
255d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    Expr *E = static_cast<Expr *>(DS.getTypeRep());
256d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
257d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
25872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    Result = Context.getTypeOfExprType(E);
259958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
260d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
2616fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
2626fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    Expr *E = static_cast<Expr *>(DS.getTypeRep());
2636fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
2646fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
265af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    Result = BuildDecltypeType(E);
266af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
267af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
268af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      isInvalid = true;
269af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
2706fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
2716fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
272e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
273e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
274e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    Result = Context.UndeducedAutoTy;
275e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
276e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
2776fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson
278809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
2795153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
2805153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    isInvalid = true;
2815153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
2825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
283958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
284958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
285f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
286f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor    if (getLangOptions().Freestanding)
287f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor      Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
288fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
289f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
290958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
291958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
292958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner         "FIXME: imaginary types not supported yet!");
293958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
29438d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // See if there are any attributes on the declspec that apply to the type (as
29538d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // opposed to the decl).
296fca0ddd42965e0b7ae821213486d4e0dd71fb439Chris Lattner  if (const AttributeList *AL = DS.getAttributes())
297c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(Result, AL);
298f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
29996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
30096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
30196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
30296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
30396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
30496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
30596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (TypeQuals & QualType::Restrict) {
306bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar      if (Result->isPointerType() || Result->isReferenceType()) {
307bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar        QualType EltTy = Result->isPointerType() ?
30835366a67baa970c287c714c957cf78a4131cf60dTed Kremenek          Result->getAsPointerType()->getPointeeType() :
30935366a67baa970c287c714c957cf78a4131cf60dTed Kremenek          Result->getAsReferenceType()->getPointeeType();
310bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner
311bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
312bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
313bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
314bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          Diag(DS.getRestrictSpecLoc(),
315d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
316d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
317bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
318bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
319bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
32096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Diag(DS.getRestrictSpecLoc(),
321d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner             diag::err_typecheck_invalid_restrict_not_pointer)
322d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
323bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
32496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
32596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
32696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
32796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
32896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
32996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
33096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
33196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
33296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
33396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if (TypeQuals & QualType::Const)
33496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
33596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      else {
33696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        assert((TypeQuals & QualType::Volatile) &&
33796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner               "Has CV quals but not C or V?");
33896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
33996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
340d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, diag::warn_typecheck_function_qualifiers)
341d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
34296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
34396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
344f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
345f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
346f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
347f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
348f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
3491a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
3501a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
351f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
352f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Const;
353f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Volatile;
354f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    }
355f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor
35696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Result.getQualifiedType(TypeQuals);
35796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
358f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
359f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
360f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
361cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
362cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
363cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
364cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
365cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
366cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
367cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
368cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
369cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
370cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
371cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
372cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the pointer type.
373cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
374cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
375cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
376cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
377cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
378cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
379cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
380cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
381cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
382cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
383cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildPointerType(QualType T, unsigned Quals,
384cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
385cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
386cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
387cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
388cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
389cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
390cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
391cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
392cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
393cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
394cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
395cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
396cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
397cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Quals &= ~QualType::Restrict;
398cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
399cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
400cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
401cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return Context.getPointerType(T).getQualifiedType(Quals);
402cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
403cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
404cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
405cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
406cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
407cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
408cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the reference type.
409cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
410cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
411cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
412cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
413cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
414cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
415cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
416cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
417cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
418cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
4197c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals,
420cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                  SourceLocation Loc, DeclarationName Entity) {
4217c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef) {
42235366a67baa970c287c714c957cf78a4131cf60dTed Kremenek    if (const RValueReferenceType *R = T->getAsRValueReferenceType()) {
423dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
424dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      //   reference to a type T, and attempt to create the type "lvalue
425dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      //   reference to cv TD" creates the type "lvalue reference to T".
426dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      // We use the qualifiers (restrict or none) of the original reference,
427dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      // not the new ones. This is consistent with GCC.
4287c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      return Context.getLValueReferenceType(R->getPointeeType()).
429dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl               getQualifiedType(T.getCVRQualifiers());
4307c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    }
4317c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
432cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
433cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ [dcl.ref]p4: There shall be no references to references.
434cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //
435cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // According to C++ DR 106, references to references are only
436cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // diagnosed when they are written directly (e.g., "int & &"),
437cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // but not when they happen via a typedef:
438cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //
439cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //   typedef int& intref;
440cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //   typedef intref& intref2;
441cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //
442cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Parser::ParserDeclaratorInternal diagnoses the case where
443cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // references are written directly; here, we handle the
444cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // collapsing of references-to-references as described in C++
445cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // DR 106 and amended by C++ DR 540.
446cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return T;
447cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
448cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
449cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
450cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   A declarator that specifies the type “reference to cv void”
451cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
452cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
453cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
454cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
455cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
456cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
457cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
458cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
459cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
460cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
461cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
462cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Quals &= ~QualType::Restrict;
463cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
464cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
465cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
466cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   [...] Cv-qualified references are ill-formed except when the
467cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   cv-qualifiers are introduced through the use of a typedef
468cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   (7.1.3) or of a template type argument (14.3), in which case
469cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   the cv-qualifiers are ignored.
470cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //
471cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // We diagnose extraneous cv-qualifiers for the non-typedef,
472cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // non-template type argument case within the parser. Here, we just
473cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // ignore any extraneous cv-qualifiers.
474cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  Quals &= ~QualType::Const;
475cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  Quals &= ~QualType::Volatile;
476cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
477cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
4787c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
4797c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return Context.getLValueReferenceType(T).getQualifiedType(Quals);
4807c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return Context.getRValueReferenceType(T).getQualifiedType(Quals);
481cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
482cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
483cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
484cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
485cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
486cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
487cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
488cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
489cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ArraySize Expression describing the size of the array.
490cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
491cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the array's
492cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// element type.
493cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
494cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
495cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// array type or, if there is no such entity, the location of the
496cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have array type.
497cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
498cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
499cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
500cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
501cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
502cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
503cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
504cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
5057e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
5067e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
507cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: If the element type is an incomplete or function type,
508cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
50986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor  if (RequireCompleteType(Loc, T,
510cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                             diag::err_illegal_decl_array_incomplete_type))
511cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
512cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
513cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
514cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
515cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
516cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
517cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
518cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
519cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ 8.3.2p4: There shall be no ... arrays of references ...
520cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
521cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_references)
522cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
523cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
524cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
525cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
526e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
527e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    Diag(Loc,  diag::err_illegal_decl_array_of_auto)
528e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson      << getPrintableNameForEntity(Entity);
529e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
530e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
531e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson
53235366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  if (const RecordType *EltTy = T->getAsRecordType()) {
533cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
534cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
535cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
536cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
537cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (T->isObjCInterfaceType()) {
538c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
539c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
540cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
541cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
542cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
543cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (ArraySize && !ArraySize->isTypeDependent() &&
544cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      !ArraySize->getType()->isIntegerType()) {
545cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
546cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
547cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    ArraySize->Destroy(Context);
548cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
549cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
550cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  llvm::APSInt ConstVal(32);
551cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
552f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
5537e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
554f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
555f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
556cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (ArraySize->isValueDependent()) {
5577e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
558cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
559cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor             (!T->isDependentType() && !T->isConstantSizeType())) {
560cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Per C99, a variable array is an array with either a non-constant
561cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // size or an element type that has a non-constant-size
5627e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
563cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
564cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
565cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
566cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (ConstVal.isSigned()) {
567cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      if (ConstVal.isNegative()) {
568cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        Diag(ArraySize->getLocStart(),
569cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor             diag::err_typecheck_negative_array_size)
570cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor          << ArraySize->getSourceRange();
571cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        return QualType();
572cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      } else if (ConstVal == 0) {
573cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        // GCC accepts zero sized static arrays.
574cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
575cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor          << ArraySize->getSourceRange();
576cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      }
577cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
5787e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getConstantArrayWithExprType(T, ConstVal, ArraySize,
5797e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                             ASM, Quals, Brackets);
580cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
581cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
582cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!getLangOptions().C99) {
583cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (ArraySize && !ArraySize->isTypeDependent() &&
584cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        !ArraySize->isValueDependent() &&
585cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        !ArraySize->isIntegerConstantExpr(Context))
586cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_vla);
587cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    else if (ASM != ArrayType::Normal || Quals != 0)
588cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_c99_array_usage);
589cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
590cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
591cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
592cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
5939cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
5949cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
5959cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
5969cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
5979cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas GregorQualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
5989cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
5999cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6009cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Expr *Arg = (Expr *)ArraySize.get();
6019cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6029cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
6039cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
6049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  if (!T->isDependentType() &&
6059cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
6069cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
6079cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
6089cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
6099cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6109cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
6119cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
6129cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
6139cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
6149cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      << "ext_vector_type" << Arg->getSourceRange();
6159cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
6169cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
6179cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6189cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // unlike gcc's vector_size attribute, the size is specified as the
6199cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
6209cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
6219cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6229cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
6239cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
6249cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      << Arg->getSourceRange();
6259cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
6269cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
6279cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6289cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!T->isDependentType())
6299cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return Context.getExtVectorType(T, vectorSize);
6309cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
6319cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
6329cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
6339cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                AttrLoc);
6349cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
635cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
636724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
637724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
638724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
639724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
640724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
6412943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
642724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
643724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
644724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
645724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
646724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
647724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
648724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
649724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
650724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
651724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
652724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
653724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
654724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
655724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
656724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
657724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
658724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
659724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
660724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
661724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
662724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
663724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
664724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
665724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
666724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 QualType *ParamTypes,
667724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
668724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 bool Variadic, unsigned Quals,
669724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 SourceLocation Loc, DeclarationName Entity) {
670724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
671724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    Diag(Loc, diag::err_func_returning_array_function) << T;
672724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
673724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
674724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
675724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
676724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
6772dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    QualType ParamType = adjustParameterType(ParamTypes[Idx]);
6782dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
679724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
680724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
681724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
682cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
683724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    ParamTypes[Idx] = ParamType;
684724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
685724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
686724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
687724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
688724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
689724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
690724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 Quals);
691724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
692949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
693949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
694949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
695949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
696949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
697949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Quals Qualifiers applied to the member pointer type
698949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
699949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
700949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
701949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
702949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
703949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas GregorQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
704949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      unsigned Quals, SourceLocation Loc,
705949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
706949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
707949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
708949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
709949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
710949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
711949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
712949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
713949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
714949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
715949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
716949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
717949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
718949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
719949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
720949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
721949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
7228d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
723949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
724949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
725949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
726949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
727949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
728949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
729949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
730949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
731949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
732949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
733949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
734949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // object or incomplete types shall not be restrict-qualified."
735949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
736949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
737949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << T;
738949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
739949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
740949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
741949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Quals &= ~QualType::Restrict;
742949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
743949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
744949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
745949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
746949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
747949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
748949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
749949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  return Context.getMemberPointerType(T, Class.getTypePtr())
750949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor           .getQualifiedType(Quals);
751949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
7529a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
7539a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
7549a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
7559a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
7569a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
7579a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Quals The cvr-qualifiers to be applied to the block pointer type.
7589a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
7599a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Loc The location of the entity whose type involves this
7609a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// block pointer type or, if there is no such entity, the location of the
7619a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type that will have block pointer type.
7629a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
7639a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
7649a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
7659a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
7669a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
7679a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
7689a917e4fac79aba20fbd25983c78396475078918Anders CarlssonQualType Sema::BuildBlockPointerType(QualType T, unsigned Quals,
7699a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     SourceLocation Loc,
7709a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
7719a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  if (!T.getTypePtr()->isFunctionType()) {
7729a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
7739a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
7749a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
7759a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
7769a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  return Context.getBlockPointerType(T).getQualifiedType(Quals);
7779a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
7789a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
77998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
78098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// declarator to Type instances. Skip the outermost Skip type
78198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// objects.
782402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor///
783402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
784402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// owns the declaration of a type (e.g., the definition of a struct
785402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// type), then *OwnedDecl will receive the owned declaration.
786402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas GregorQualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip,
787402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor                                    TagDecl **OwnedDecl) {
78898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  bool OmittedReturnType = false;
78998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
79098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  if (D.getContext() == Declarator::BlockLiteralContext
79198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && Skip == 0
79298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && !D.getDeclSpec().hasTypeSpecifier()
79398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && (D.getNumTypeObjects() == 0
79498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump          || (D.getNumTypeObjects() == 1
79598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump              && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
79698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    OmittedReturnType = true;
79798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
798b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner  // long long is a C99 feature.
799d1eb332181f02a7e0a6f9749265e6a0f55555cd4Chris Lattner  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
800b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner      D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
801b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner    Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
802930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
803930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
804930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
805930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
806930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  switch (D.getKind()) {
807930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Abstract:
808930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Normal:
80998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  case Declarator::DK_Operator: {
8103f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner    const DeclSpec &DS = D.getDeclSpec();
8113f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner    if (OmittedReturnType) {
81298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // We default to a dependent type initially.  Can be modified by
81398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // the first return statement.
81498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      T = Context.DependentTy;
8153f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner    } else {
816eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner      bool isInvalid = false;
817eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner      T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid);
818eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner      if (isInvalid)
819eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner        D.setInvalidType(true);
820402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      else if (OwnedDecl && DS.isTypeSpecOwned())
821402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep());
822809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    }
823930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
82498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  }
825930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
826930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Constructor:
827930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Destructor:
828930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Conversion:
829930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
830930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // "void" instead. Conversion operators will check their return
831930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // types separately.
832930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
833930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
834930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
8354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
836baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  if (T == Context.UndeducedAutoTy) {
837baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
838baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
839baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
840baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
841baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      assert(0 && "K&R type lists aren't allowed in C++");
842baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
843baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
844baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
845baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
846baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
847baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      switch (cast<TagDecl>(CurContext)->getTagKind()) {
848baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
849baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
850baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_union:  Error = 2; /* Union member */ break;
851baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      case TagDecl::TK_class:  Error = 3; /* Class member */ break;
852baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      }
853baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
854baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
855baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 4; // Exception declaration
856baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
857baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
858baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 5; // Template parameter
859baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
860baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
861baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 6;  // Block literal
862baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
863baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
864baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
865baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
866baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
867baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TypeNameContext:
868baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
869baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
870baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
871baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
872baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
873baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
874baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      T = Context.IntTy;
875baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
876baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
877baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
878baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
879cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
880cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
881cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
882cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
883cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
88498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
88598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
88698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
887cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
888cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
8895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
8905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
8915618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
8929af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
8939af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
8949af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner        Diag(DeclType.Loc, diag::err_blocks_disable);
8959af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner
8969a917e4fac79aba20fbd25983c78396475078918Anders Carlsson      T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
8979a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                Name);
8985618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
8995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
9006a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
9016a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
9026a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
9036a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
9046a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
9056a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
9066a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
90714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
90867ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        const ObjCInterfaceType *OIT = T->getAsObjCInterfaceType();
90914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        T = Context.getObjCObjectPointerType(T,
91067ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff                                         (ObjCProtocolDecl **)OIT->qual_begin(),
91167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff                                         OIT->getNumProtocols());
91214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
91314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
914cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
916cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    case DeclaratorChunk::Reference:
9176a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
9186a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
9196a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
9206a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
9216a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
9226a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
9236a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
9247c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      T = BuildReferenceType(T, DeclType.Ref.LValueRef,
9257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl                             DeclType.Ref.HasRestrict ? QualType::Restrict : 0,
926cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                             DeclType.Loc, Name);
9275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
9285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
9296a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
9306a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
9316a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
9326a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
9336a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
9346a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
9356a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
936fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
93794f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
9385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
9395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
9405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
9415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
9425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
9435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
9445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
945f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      if (ASM == ArrayType::Star &&
946f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman          D.getContext() != Declarator::PrototypeContext) {
947f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
948f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
949f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
950f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
951f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
952f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
953f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
9547e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
9557e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                         SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
9565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
9575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
958f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
9595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
9605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
9615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
9625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
9633cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
964cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      // C99 6.7.5.3p1: The return type may not be a function or array type.
96568cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner      if (T->isArrayType() || T->isFunctionType()) {
966d162584991885ab004a02573a73ce06422b921fcChris Lattner        Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
967cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        T = Context.IntTy;
968cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        D.setInvalidType(true);
969cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      }
970465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
971402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
972402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
973402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
974402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
975402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        if (Tag->isDefinition())
976402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor          Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
977402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
978402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
979402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
9803cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
9813cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
9823cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      if (FTI.hasExceptionSpec &&
9833cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl          D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
9843cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl        Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
9853cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
986eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      if (FTI.NumArgs == 0) {
987c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        if (getLangOptions().CPlusPlus) {
988c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
989c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // function takes no arguments.
990465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          llvm::SmallVector<QualType, 4> Exceptions;
991465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          Exceptions.reserve(FTI.NumExceptions);
992ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
993ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty);
994ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            // Check that the type is valid for an exception spec, and drop it
995ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            // if not.
996ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
997ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl              Exceptions.push_back(ET);
998ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          }
999465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1000465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                      FTI.hasExceptionSpec,
1001465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                      FTI.hasAnyExceptionSpec,
1002ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl                                      Exceptions.size(), Exceptions.data());
1003965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor        } else if (FTI.isVariadic) {
1004965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // We allow a zero-parameter variadic function in C if the
1005965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // function is marked with the "overloadable"
1006965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // attribute. Scan for this attribute now.
1007965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
1008965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
1009965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
1010965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
1011965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
1012965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
1013965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
1014965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
1015965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
1016965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
1017965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1018965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
1019c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        } else {
1020c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // Simple void foo(), where the incoming T is the result type.
102172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          T = Context.getFunctionNoProtoType(T);
1022c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
1023eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      } else if (FTI.ArgInfo[0].Param == 0) {
10245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
1025eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman        Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
10265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else {
10275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
10285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
10295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
10305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1032b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner          ParmVarDecl *Param =
1033b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner            cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
10348123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
103578c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
10362dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
10372dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
1038beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor          assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
10392dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
10405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
10415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
104272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
10432dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
10445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
10465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
10475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
10485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
10492ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
10508123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
10512ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
10522ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
10535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
10544565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
10552ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
10568123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
10572ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
10582ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
1059f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner              if (ArgTy.getCVRQualifiers())
10602ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
10612ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner
10622ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
10632ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
10642ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
1065eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
1066eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
1067eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              ArgTy = Context.IntTy;
1068eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
1069eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
1070eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
1071eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
10735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ArgTys.push_back(ArgTy);
10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
1076465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1077465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        llvm::SmallVector<QualType, 4> Exceptions;
1078465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        Exceptions.reserve(FTI.NumExceptions);
1079ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl        for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1080ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty);
1081ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          // Check that the type is valid for an exception spec, and drop it if
1082ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          // not.
1083ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl          if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1084ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl            Exceptions.push_back(ET);
1085ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl        }
1086465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1087beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
1088465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.isVariadic, FTI.TypeQuals,
1089465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.hasExceptionSpec,
1090465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                    FTI.hasAnyExceptionSpec,
1091ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl                                    Exceptions.size(), Exceptions.data());
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1095f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
10964994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      // Verify that we're not building a pointer to pointer to function with
10974994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      // exception specification.
10984994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
10994994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
11004994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl        D.setInvalidType(true);
11014994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl        // Build the type anyway.
11024994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl      }
1103f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
1104f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
1105949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (isDependentScopeSpecifier(DeclType.Mem.Scope())) {
1106949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        NestedNameSpecifier *NNS
1107949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
1108949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        assert(NNS->getAsType() && "Nested-name-specifier must name a type");
1109949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        ClsType = QualType(NNS->getAsType(), 0);
1110949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      } else if (CXXRecordDecl *RD
1111949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                   = dyn_cast_or_null<CXXRecordDecl>(
1112949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                    computeDeclContext(DeclType.Mem.Scope()))) {
1113f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.getTagDeclType(RD);
1114f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
1115949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        Diag(DeclType.Mem.Scope().getBeginLoc(),
1116949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
1117949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1118949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
1119f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
1120f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1121f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1122949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
1123949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1124949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                   DeclType.Loc, D.getIdentifier());
1125949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
1126f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
1127949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
1128f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1129f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
1130f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
1131f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1132cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
1133cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
1134cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
1135cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
1136cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1137c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
1138c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    if (const AttributeList *AL = DeclType.getAttrs())
1139c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      ProcessTypeAttributeList(T, AL);
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1141971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1142971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
114372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
114472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
1145971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1146971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1147971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // for a nonstatic member function, the function type to which a pointer
1148971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // to member refers, or the top-level function type of a function typedef
1149971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // declaration.
1150971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    if (FnTy->getTypeQuals() != 0 &&
1151971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
1152584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor        ((D.getContext() != Declarator::MemberContext &&
1153584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor          (!D.getCXXScopeSpec().isSet() ||
1154f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor           !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1155f59a56e180bf54528d7d1d5afa68fcc13300965aDouglas Gregor              ->isRecord())) ||
1156971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
1157971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      if (D.isFunctionDeclarator())
1158971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1159971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      else
1160971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(),
1161971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis             diag::err_invalid_qualified_typedef_function_type_use);
1162971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1163971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      // Strip the cv-quals from the type.
1164971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
11657fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                                  FnTy->getNumArgs(), FnTy->isVariadic(), 0);
1166971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
1167971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11690bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // If there were any type attributes applied to the decl itself (not the
11700bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // type, apply the type attribute to the type!)
11710bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  if (const AttributeList *Attrs = D.getAttributes())
1172c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(T, Attrs);
11730bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner
11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T;
11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1177ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl/// CheckSpecifiedExceptionType - Check if the given type is valid in an
1178ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl/// exception specification. Incomplete types, or pointers to incomplete types
1179ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl/// other than void are not allowed.
1180ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redlbool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) {
1181ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl  // FIXME: This may not correctly work with the fix for core issue 437,
1182ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl  // where a class's own type is considered complete within its body.
1183ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl
1184ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl  // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1185ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl  //   an incomplete type.
1186ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl  if (T->isIncompleteType())
1187ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl    return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1188ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl      << Range << T << /*direct*/0;
1189ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl
1190ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl  // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1191ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl  //   an incomplete type a pointer or reference to an incomplete type, other
1192ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl  //   than (cv) void*.
1193ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl  int kind;
119435366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  if (const PointerType* IT = T->getAsPointerType()) {
1195ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl    T = IT->getPointeeType();
1196ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl    kind = 1;
119735366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  } else if (const ReferenceType* IT = T->getAsReferenceType()) {
1198ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl    T = IT->getPointeeType();
11993cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl    kind = 2;
1200ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl  } else
1201ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl    return false;
1202ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl
1203ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl  if (T->isIncompleteType() && !T->isVoidType())
1204ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl    return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1205ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl      << Range << T << /*indirect*/kind;
1206ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl
1207ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl  return false;
1208ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl}
1209ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl
12106a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
12116a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl/// to member to a function with an exception specification. This means that
12126a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl/// it is invalid to add another level of indirection.
12136a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redlbool Sema::CheckDistantExceptionSpec(QualType T) {
121435366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  if (const PointerType *PT = T->getAsPointerType())
12156a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl    T = PT->getPointeeType();
121635366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  else if (const MemberPointerType *PT = T->getAsMemberPointerType())
12176a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl    T = PT->getPointeeType();
12186a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl  else
12196a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl    return false;
12206a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl
12216a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl  const FunctionProtoType *FnT = T->getAsFunctionProtoType();
12226a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl  if (!FnT)
12236a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl    return false;
12246a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl
12256a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl  return FnT->hasExceptionSpec();
12266a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl}
12276a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl
12284994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
12294994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl/// exception specifications. Exception specifications are equivalent if
12304994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl/// they allow exactly the same set of exception types. It does not matter how
12314994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl/// that is achieved. See C++ [except.spec]p2.
12324994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redlbool Sema::CheckEquivalentExceptionSpec(
12334994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl    const FunctionProtoType *Old, SourceLocation OldLoc,
12344994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl    const FunctionProtoType *New, SourceLocation NewLoc) {
12354994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec();
12364994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec();
12374994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  if (OldAny && NewAny)
12384994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl    return false;
12394994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  if (OldAny || NewAny) {
12404994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl    Diag(NewLoc, diag::err_mismatched_exception_spec);
12414994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl    Diag(OldLoc, diag::note_previous_declaration);
12424994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl    return true;
12434994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  }
12444994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl
12454994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  bool Success = true;
12464994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  // Both have a definite exception spec. Collect the first set, then compare
12474994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  // to the second.
12484994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  llvm::SmallPtrSet<const Type*, 8> Types;
12494994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
12504994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl       E = Old->exception_end(); I != E; ++I)
12514994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl    Types.insert(Context.getCanonicalType(*I).getTypePtr());
12524994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl
12534994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  for (FunctionProtoType::exception_iterator I = New->exception_begin(),
12544994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl       E = New->exception_end(); I != E && Success; ++I)
12554994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl    Success = Types.erase(Context.getCanonicalType(*I).getTypePtr());
12564994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl
12574994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  Success = Success && Types.empty();
12584994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl
12594994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  if (Success) {
12604994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl    return false;
12614994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  }
12624994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  Diag(NewLoc, diag::err_mismatched_exception_spec);
12634994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  Diag(OldLoc, diag::note_previous_declaration);
12644994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl  return true;
12654994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl}
12664994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl
126723c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl/// CheckExceptionSpecSubset - Check whether the second function type's
126823c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl/// exception specification is a subset (or equivalent) of the first function
126923c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl/// type. This is used by override and pointer assignment checks.
127023c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redlbool Sema::CheckExceptionSpecSubset(unsigned DiagID, unsigned NoteID,
127123c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    const FunctionProtoType *Superset, SourceLocation SuperLoc,
127223c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    const FunctionProtoType *Subset, SourceLocation SubLoc)
127323c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl{
127423c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl  // FIXME: As usual, we could be more specific in our error messages, but
127523c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl  // that better waits until we've got types with source locations.
127623c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl
127723c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl  // If superset contains everything, we're done.
127823c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl  if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec())
127923c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    return false;
128023c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl
128123c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl  // It does not. If the subset contains everything, we've failed.
128223c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl  if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) {
128323c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    Diag(SubLoc, DiagID);
128423c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    Diag(SuperLoc, NoteID);
128523c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    return true;
128623c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl  }
128723c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl
128823c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl  // Neither contains everything. Do a proper comparison.
128923c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl  for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
129023c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl       SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
129123c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    // Take one type from the subset.
129223c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    QualType CanonicalSubT = Context.getCanonicalType(*SubI);
129323c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    bool SubIsPointer = false;
129435366a67baa970c287c714c957cf78a4131cf60dTed Kremenek    if (const ReferenceType *RefTy = CanonicalSubT->getAsReferenceType())
129523c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      CanonicalSubT = RefTy->getPointeeType();
129635366a67baa970c287c714c957cf78a4131cf60dTed Kremenek    if (const PointerType *PtrTy = CanonicalSubT->getAsPointerType()) {
129723c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      CanonicalSubT = PtrTy->getPointeeType();
129823c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      SubIsPointer = true;
129923c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    }
130023c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    bool SubIsClass = CanonicalSubT->isRecordType();
130123c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    CanonicalSubT.setCVRQualifiers(0);
130223c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl
1303726212f41bac77dc7f3352bc7047615fa0cd9e58Sebastian Redl    BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
130423c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl                    /*DetectVirtual=*/false);
130523c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl
130623c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    bool Contained = false;
130723c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    // Make sure it's in the superset.
130823c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    for (FunctionProtoType::exception_iterator SuperI =
130923c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl           Superset->exception_begin(), SuperE = Superset->exception_end();
131023c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl         SuperI != SuperE; ++SuperI) {
131123c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
131223c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      // SubT must be SuperT or derived from it, or pointer or reference to
131323c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      // such types.
131435366a67baa970c287c714c957cf78a4131cf60dTed Kremenek      if (const ReferenceType *RefTy = CanonicalSuperT->getAsReferenceType())
131523c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl        CanonicalSuperT = RefTy->getPointeeType();
131623c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      if (SubIsPointer) {
131735366a67baa970c287c714c957cf78a4131cf60dTed Kremenek        if (const PointerType *PtrTy = CanonicalSuperT->getAsPointerType())
131823c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl          CanonicalSuperT = PtrTy->getPointeeType();
131923c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl        else {
132023c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl          continue;
132123c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl        }
132223c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      }
132323c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      CanonicalSuperT.setCVRQualifiers(0);
132423c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      // If the types are the same, move on to the next type in the subset.
132523c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      if (CanonicalSubT == CanonicalSuperT) {
132623c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl        Contained = true;
132723c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl        break;
132823c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      }
132923c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl
133023c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      // Otherwise we need to check the inheritance.
133123c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      if (!SubIsClass || !CanonicalSuperT->isRecordType())
133223c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl        continue;
133323c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl
133423c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      Paths.clear();
133523c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
133623c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl        continue;
133723c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl
133823c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      if (Paths.isAmbiguous(CanonicalSuperT))
133923c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl        continue;
134023c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl
1341726212f41bac77dc7f3352bc7047615fa0cd9e58Sebastian Redl      if (FindInaccessibleBase(CanonicalSubT, CanonicalSuperT, Paths, true))
1342726212f41bac77dc7f3352bc7047615fa0cd9e58Sebastian Redl        continue;
134323c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl
134423c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      Contained = true;
134523c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      break;
134623c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    }
134723c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    if (!Contained) {
134823c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      Diag(SubLoc, DiagID);
134923c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      Diag(SuperLoc, NoteID);
135023c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl      return true;
135123c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl    }
135223c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl  }
135323c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl  // We've run the gauntlet.
135423c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl  return false;
135523c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl}
135623c7d061367dd2fc1631e867cffc3d6aae24e799Sebastian Redl
1357a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
1358360300ca2ee298d585d29baf006519507d968812Fariborz Jahanian/// declarator
1359b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerQualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1360b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
1361306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  QualType T = MDecl->getResultType();
1362306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  llvm::SmallVector<QualType, 16> ArgTys;
1363306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
13643560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian  // Add the first two invisible argument types for self and _cmd.
1365f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  if (MDecl->isInstanceMethod()) {
1366a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
13671f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    selfTy = Context.getPointerType(selfTy);
13681f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    ArgTys.push_back(selfTy);
136989951a86b594513c2a013532ed45d197413b1087Chris Lattner  } else
1370a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ArgTys.push_back(Context.getObjCIdType());
1371a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ArgTys.push_back(Context.getObjCSelType());
13723560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian
137389951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
137489951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = MDecl->param_end(); PI != E; ++PI) {
137589951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType ArgTy = (*PI)->getType();
1376306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    assert(!ArgTy.isNull() && "Couldn't parse type?");
1377beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor    ArgTy = adjustParameterType(ArgTy);
1378306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    ArgTys.push_back(ArgTy);
1379306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  }
1380306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
13817fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                              MDecl->isVariadic(), 0);
1382306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  return T;
1383306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian}
1384306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
13859e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
13869e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
13879e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// they point to and return true. If T1 and T2 aren't pointer types
13889e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// or pointer-to-member types, or if they are not similar at this
13899e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// level, returns false and leaves T1 and T2 unchanged. Top-level
13909e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// qualifiers on T1 and T2 are ignored. This function will typically
13919e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// be called in a loop that successively "unwraps" pointer and
13929e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// pointer-to-member types to compare them at each level.
1393ecb81f28cb279b7d8e84296445a4131fa80b69a9Chris Lattnerbool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
139435366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  const PointerType *T1PtrType = T1->getAsPointerType(),
139535366a67baa970c287c714c957cf78a4131cf60dTed Kremenek                    *T2PtrType = T2->getAsPointerType();
139657373266011f73418381b736015d8d2bb0381176Douglas Gregor  if (T1PtrType && T2PtrType) {
139757373266011f73418381b736015d8d2bb0381176Douglas Gregor    T1 = T1PtrType->getPointeeType();
139857373266011f73418381b736015d8d2bb0381176Douglas Gregor    T2 = T2PtrType->getPointeeType();
139957373266011f73418381b736015d8d2bb0381176Douglas Gregor    return true;
140057373266011f73418381b736015d8d2bb0381176Douglas Gregor  }
140157373266011f73418381b736015d8d2bb0381176Douglas Gregor
140235366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
140335366a67baa970c287c714c957cf78a4131cf60dTed Kremenek                          *T2MPType = T2->getAsMemberPointerType();
140421593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl  if (T1MPType && T2MPType &&
140521593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T1MPType->getClass()) ==
140621593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T2MPType->getClass())) {
14074433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T1 = T1MPType->getPointeeType();
14084433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T2 = T2MPType->getPointeeType();
14094433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    return true;
14104433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  }
141157373266011f73418381b736015d8d2bb0381176Douglas Gregor  return false;
141257373266011f73418381b736015d8d2bb0381176Douglas Gregor}
141357373266011f73418381b736015d8d2bb0381176Douglas Gregor
1414cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlSema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
14155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
14165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
14175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
14185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1419402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  TagDecl *OwnedTag = 0;
1420402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  QualType T = GetTypeForDeclarator(D, S, /*Skip=*/0, &OwnedTag);
14215153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
1422809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
14235912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
1424402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  if (getLangOptions().CPlusPlus) {
1425402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
14266d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
14276d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
1428402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // C++0x [dcl.type]p3:
1429402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   A type-specifier-seq shall not define a class or enumeration
1430402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   unless it appears in the type-id of an alias-declaration
1431402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   (7.1.3).
1432402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    if (OwnedTag && OwnedTag->isDefinition())
1433402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1434402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        << Context.getTypeDeclType(OwnedTag);
1435402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
1436402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
14375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T.getAsOpaquePtr();
14385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1440c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1441c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1442c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
1443c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
1444c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
1445232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
1446232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1447c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
1448c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
1449c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnerstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
1450c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
1451232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
1452232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1453232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
1454232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
1455c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1456c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1457232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1458232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
1459232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
1460545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
1461f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1462c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1463232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1464545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1465232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
1466c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
1467dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1468dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
1469c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
1470232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
1471232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
1472232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
1473f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
1474c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
1475c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
1476d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1477d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// specified type.  The attribute contains 1 argument, weak or strong.
1478d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanianstatic void HandleObjCGCTypeAttribute(QualType &Type,
14793b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner                                      const AttributeList &Attr, Sema &S) {
1480d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (Type.getObjCGCAttr() != QualType::GCNone) {
14815934e75d98d99374f72722a69c5eefe026f35c74Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
1482d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1483d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
1484d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
1485d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
1486ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (!Attr.getParameterName()) {
1487ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1488ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
1489ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    return;
1490ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
14913b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  QualType::GCAttrTypes GCAttr;
1492ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (Attr.getNumArgs() != 0) {
1493d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1494d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1495d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
1496d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (Attr.getParameterName()->isStr("weak"))
14973b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner    GCAttr = QualType::Weak;
1498d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else if (Attr.getParameterName()->isStr("strong"))
14993b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner    GCAttr = QualType::Strong;
1500d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
1501d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1502d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      << "objc_gc" << Attr.getParameterName();
1503d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
1504d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
1505d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
15063b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  Type = S.Context.getObjCGCQualType(Type, GCAttr);
1507d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1508d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
1509c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnervoid Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
1510c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
1511c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
1512c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
1513c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
1514c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  for (; AL; AL = AL->getNext()) {
1515c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // If this is an attribute we can handle, do so now, otherwise, add it to
1516c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // the LeftOverAttrs list for rechaining.
1517c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    switch (AL->getKind()) {
1518c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
1519c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
1520c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1521c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
1522d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    case AttributeList::AT_objc_gc:
1523d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      HandleObjCGCTypeAttribute(Result, *AL, *this);
1524d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
1525c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
1526c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  }
1527232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
1528232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
152986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// @brief Ensure that the type T is a complete type.
15304ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
15314ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
15324ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
153386447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
153486447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
153586447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
153686447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
153786447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
15384ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
15394ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
15404ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
15414ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
15424ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
15434ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
15444ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param diag The diagnostic value (e.g.,
15454ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c diag::err_typecheck_decl_incomplete_type) that will be used
15464ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// for the error message if @p T is incomplete.
15474ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
15484ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range1  An optional range in the source code that will be a
15494ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
15504ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
15514ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range2  An optional range in the source code that will be a
15524ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
15534ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
15544ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param PrintType If non-NULL, the type that should be printed
15554ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// instead of @p T. This parameter should be used when the type that
15564ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// we're checking for incompleteness isn't the type that should be
15574ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// displayed to the user, e.g., when T is a type and PrintType is a
15584ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// pointer to T.
15594ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
15604ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
15614ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
156286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag,
15631efaa9594a81709a17658fd80ae7e783e1026407Chris Lattner                               SourceRange Range1, SourceRange Range2,
15641efaa9594a81709a17658fd80ae7e783e1026407Chris Lattner                               QualType PrintType) {
1565690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
1566690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
1567690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
1568690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //  assert(!T->isDependentType() &&
1569690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
1570690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
15714ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
15724ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
15734ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
15744ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
1575d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
1576d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // class template specialization, try to instantiate it.
157735366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  if (const RecordType *Record = T->getAsRecordType()) {
15782943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
1579d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
15802943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
15812943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        // Update the class template specialization's location to
15822943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        // refer to the point of instantiation.
15832943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        if (Loc.isValid())
15842943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor          ClassTemplateSpec->setLocation(Loc);
15852943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
15862943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor                                             /*ExplicitInstantiation=*/false);
15872943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor      }
1588d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    } else if (CXXRecordDecl *Rec
1589d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1590d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1591d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        // Find the class template specialization that surrounds this
1592d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        // member class.
1593d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        ClassTemplateSpecializationDecl *Spec = 0;
1594d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        for (DeclContext *Parent = Rec->getDeclContext();
1595d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor             Parent && !Spec; Parent = Parent->getParent())
1596d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
1597d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        assert(Spec && "Not a member of a class template specialization?");
159893dfdb1299ef740df854f4a745dc87e5e43f0c30Douglas Gregor        return InstantiateClass(Loc, Rec, Pattern, Spec->getTemplateArgs(),
159993dfdb1299ef740df854f4a745dc87e5e43f0c30Douglas Gregor                                /*ExplicitInstantiation=*/false);
1600d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
1601d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
1602d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
16032943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
16044ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (PrintType.isNull())
16054ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    PrintType = T;
16064ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
16074ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
16084ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  Diag(Loc, diag) << PrintType << Range1 << Range2;
16093c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
16104ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
16114ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // type, produce
16124ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  const TagType *Tag = 0;
161335366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  if (const RecordType *Record = T->getAsRecordType())
16144ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Record;
16154ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  else if (const EnumType *Enum = T->getAsEnumType())
16164ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Enum;
16174ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
16184ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
16194ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Diag(Tag->getDecl()->getLocation(),
16204ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
16214ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
16224ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
16234ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
16244ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
16254ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
1626e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
1627e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor/// \brief Retrieve a version of the type 'T' that is qualified by the
1628e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor/// nested-name-specifier contained in SS.
1629e6258936178b4c52b43b3b9dbec13552961cd645Douglas GregorQualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1630e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor  if (!SS.isSet() || SS.isInvalid() || T.isNull())
1631e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
1632e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
1633ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  NestedNameSpecifier *NNS
16343507369940bfb269551bfa1fec812481f60e3552Douglas Gregor    = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
1635ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  return Context.getQualifiedNameType(NNS, T);
1636e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
1637af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
1638af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders CarlssonQualType Sema::BuildTypeofExprType(Expr *E) {
1639af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
1640af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
1641af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
1642af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders CarlssonQualType Sema::BuildDecltypeType(Expr *E) {
1643af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  if (E->getType() == Context.OverloadTy) {
1644af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    Diag(E->getLocStart(),
1645af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson         diag::err_cannot_determine_declared_type_of_overloaded_function);
1646af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    return QualType();
1647af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  }
1648af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getDecltypeType(E);
1649af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
1650