SemaType.cpp revision 9af5500f3f132f9a2f9abbe82113a7c7bb751472
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements type-related semantic analysis.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "Sema.h"
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
16980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
172943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor#include "clang/AST/DeclTemplate.h"
18e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
19e4858a65a93fb36c099d8dd2ea0a98e33e77687eDaniel Dunbar#include "clang/Parse/DeclSpec.h"
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
222dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// \brief Perform adjustment on the parameter type of a function.
232dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor///
242dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// This routine adjusts the given parameter type @p T to the actual
252dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
262dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
272dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorQualType Sema::adjustParameterType(QualType T) {
282dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // C99 6.7.5.3p7:
292dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T->isArrayType()) {
302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    // C99 6.7.5.3p7:
312dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   A declaration of a parameter as "array of type" shall be
322dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   adjusted to "qualified pointer to type", where the type
332dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   qualifiers (if any) are those specified within the [ and ] of
342dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   the array type derivation.
352dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getArrayDecayedType(T);
362dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  } else if (T->isFunctionType())
372dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    // C99 6.7.5.3p8:
382dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   A declaration of a parameter as "function returning type"
392dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   shall be adjusted to "pointer to function returning type", as
402dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //   in 6.3.2.1.
412dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getPointerType(T);
422dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
432dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return T;
442dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
452dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
46930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
47930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
48930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \param DS  the declaration specifiers
49930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \returns The type described by the declaration specifiers, or NULL
50930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// if there was an error.
51fca0ddd42965e0b7ae821213486d4e0dd71fb439Chris LattnerQualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) {
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
54958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  QualType Result;
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
5796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
5896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
5996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
62fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
64fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
68fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
70958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
7264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
7364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
7464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
75f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
76f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
7764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
7864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
7964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
8064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
81f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
82f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
8364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
8464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
8564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
86d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
8762f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
88097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
89ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner      Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
9062f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner                                              DS.getNumProtocolQualifiers());
9162f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
9262f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
9362f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner
94d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
95d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
96d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
97d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
98d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
99d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
100d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
101d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    if (getLangOptions().ImplicitInt) {
10235d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
10335d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
10435d276f443462249b436951c1c663820569e1768Chris Lattner      if (DS.isEmpty())
105173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner        Diag(DS.getSourceRange().getBegin(), diag::warn_missing_declspec)
106173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner        << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
107173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner                                                 "int");
1084310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
109d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
110d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
111d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
112d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
1134310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
1144310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      unsigned DK = getLangOptions().CPlusPlus && !getLangOptions().Microsoft?
1154310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor          diag::err_missing_type_specifier
11635d276f443462249b436951c1c663820569e1768Chris Lattner        : diag::warn_missing_type_specifier;
117173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner      Diag(DS.getSourceRange().getBegin(), DK)
118173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner        << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
119173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner                                                 "int");
120d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
121d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner
122d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // FALL THROUGH.
1233cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
126fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
127fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
128fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
129fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_longlong:    Result = Context.LongLongTy; break;
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
133fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
134fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
135fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
136fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_longlong:    Result =Context.UnsignedLongLongTy; break;
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
139958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1403cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
141fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
142958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
143958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
144fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
145958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
146fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
147958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
148fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(0 && "FIXME: GNU decimal extensions not supported yet!");
15399dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Decl *D = static_cast<Decl *>(DS.getTypeRep());
15899dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner    assert(D && "Didn't get a decl for a class/enum/union/struct?");
1595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
1632ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    Result = Context.getTypeDeclType(cast<TypeDecl>(D));
164958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1661a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
1685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
1701a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
1712ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
1721a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
1731a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
1741a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      // we have this "hack" for now...
1751a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
1761a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor        Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
1771a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor                                                       (ObjCProtocolDecl**)PQ,
1781a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor                                               DS.getNumProtocolQualifiers());
1791a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      else if (Result == Context.getObjCIdType())
180ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
181ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
182ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner                                                DS.getNumProtocolQualifiers());
1834262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff      else if (Result == Context.getObjCClassType())
1844262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
1858dfb0c57ddb700b163afa89e3ab160f1de26753dSteve Naroff        Diag(DS.getSourceRange().getBegin(),
1864262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff             diag::err_qualified_class_unsupported) << DS.getSourceRange();
1874262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff      else
1884262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        Diag(DS.getSourceRange().getBegin(),
1894262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff             diag::err_invalid_protocol_qualifiers) << DS.getSourceRange();
190c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
192958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
194958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
195958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
196958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
197d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
198fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
199958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
200d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
201d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    Expr *E = static_cast<Expr *>(DS.getTypeRep());
202d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
203d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
20472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    Result = Context.getTypeOfExprType(E);
205958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
206d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
207809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
208809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return QualType();
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
210958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
211958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
212f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
213f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor    if (getLangOptions().Freestanding)
214f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor      Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
215fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
216f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
217958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
218958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
219958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner         "FIXME: imaginary types not supported yet!");
220958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
22138d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // See if there are any attributes on the declspec that apply to the type (as
22238d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // opposed to the decl).
223fca0ddd42965e0b7ae821213486d4e0dd71fb439Chris Lattner  if (const AttributeList *AL = DS.getAttributes())
224c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(Result, AL);
225f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
22696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
22796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
22896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
22996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
23096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
23196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
23296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (TypeQuals & QualType::Restrict) {
233bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar      if (Result->isPointerType() || Result->isReferenceType()) {
234bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar        QualType EltTy = Result->isPointerType() ?
235bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar          Result->getAsPointerType()->getPointeeType() :
236bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar          Result->getAsReferenceType()->getPointeeType();
237bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner
238bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
239bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
240bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
241bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          Diag(DS.getRestrictSpecLoc(),
242d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
243d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
244bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
245bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
246bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
24796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Diag(DS.getRestrictSpecLoc(),
248d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner             diag::err_typecheck_invalid_restrict_not_pointer)
249d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
250bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
25196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
25296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
25396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
25496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
25596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
25696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
25796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
25896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
25996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
26096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if (TypeQuals & QualType::Const)
26196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
26296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      else {
26396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        assert((TypeQuals & QualType::Volatile) &&
26496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner               "Has CV quals but not C or V?");
26596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
26696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
267d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, diag::warn_typecheck_function_qualifiers)
268d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
26996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
27096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
271f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
272f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
273f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
274f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
275f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
2761a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
2771a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
278f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
279f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Const;
280f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Volatile;
281f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    }
282f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor
28396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Result.getQualifiedType(TypeQuals);
28496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
285f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
286f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
287f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
288cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
289cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
290cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
291cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
292cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
293cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
294cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
295cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
296cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
297cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
298cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
299cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the pointer type.
300cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
301cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
302cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
303cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
304cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
305cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
306cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
307cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
308cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
309cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
310cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildPointerType(QualType T, unsigned Quals,
311cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
312cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
313cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
314cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
315cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
316cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
317cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
318cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
319cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
320cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
321cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
322cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
323cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
324cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Quals &= ~QualType::Restrict;
325cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
326cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
327cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
328cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return Context.getPointerType(T).getQualifiedType(Quals);
329cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
330cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
331cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
332cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
333cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
334cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
335cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the reference type.
336cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
337cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
338cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
339cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
340cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
341cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
342cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
343cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
344cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
345cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
3467c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals,
347cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                  SourceLocation Loc, DeclarationName Entity) {
3487c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef) {
3497c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    if (const RValueReferenceType *R = T->getAsRValueReferenceType()) {
350dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
351dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      //   reference to a type T, and attempt to create the type "lvalue
352dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      //   reference to cv TD" creates the type "lvalue reference to T".
353dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      // We use the qualifiers (restrict or none) of the original reference,
354dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl      // not the new ones. This is consistent with GCC.
3557c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      return Context.getLValueReferenceType(R->getPointeeType()).
356dfe292dbebe84bc3a19dba83e9eef52d56492b0aSebastian Redl               getQualifiedType(T.getCVRQualifiers());
3577c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    }
3587c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
359cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
360cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ [dcl.ref]p4: There shall be no references to references.
361cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //
362cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // According to C++ DR 106, references to references are only
363cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // diagnosed when they are written directly (e.g., "int & &"),
364cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // but not when they happen via a typedef:
365cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //
366cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //   typedef int& intref;
367cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //   typedef intref& intref2;
368cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //
369cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Parser::ParserDeclaratorInternal diagnoses the case where
370cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // references are written directly; here, we handle the
371cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // collapsing of references-to-references as described in C++
372cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // DR 106 and amended by C++ DR 540.
373cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return T;
374cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
375cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
376cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
377cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   A declarator that specifies the type “reference to cv void”
378cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
379cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
380cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
381cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
382cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
383cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
384cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
385cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
386cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
387cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
388cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
389cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Quals &= ~QualType::Restrict;
390cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
391cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
392cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
393cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   [...] Cv-qualified references are ill-formed except when the
394cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   cv-qualifiers are introduced through the use of a typedef
395cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   (7.1.3) or of a template type argument (14.3), in which case
396cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   the cv-qualifiers are ignored.
397cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //
398cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // We diagnose extraneous cv-qualifiers for the non-typedef,
399cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // non-template type argument case within the parser. Here, we just
400cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // ignore any extraneous cv-qualifiers.
401cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  Quals &= ~QualType::Const;
402cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  Quals &= ~QualType::Volatile;
403cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
404cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
4057c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
4067c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return Context.getLValueReferenceType(T).getQualifiedType(Quals);
4077c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return Context.getRValueReferenceType(T).getQualifiedType(Quals);
408cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
409cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
410cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
411cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
412cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
413cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
414cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
415cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
416cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ArraySize Expression describing the size of the array.
417cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
418cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the array's
419cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// element type.
420cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
421cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
422cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// array type or, if there is no such entity, the location of the
423cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have array type.
424cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
425cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
426cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
427cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
428cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
429cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
430cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
431cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
432cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              SourceLocation Loc, DeclarationName Entity) {
433cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: If the element type is an incomplete or function type,
434cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
43586447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor  if (RequireCompleteType(Loc, T,
436cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                             diag::err_illegal_decl_array_incomplete_type))
437cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
438cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
439cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
440cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
441cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
442cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
443cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
444cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
445cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ 8.3.2p4: There shall be no ... arrays of references ...
446cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
447cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_references)
448cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
449cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
450cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
451cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
452cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (const RecordType *EltTy = T->getAsRecordType()) {
453cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
454cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
455cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
456cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
457cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (T->isObjCInterfaceType()) {
458cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::warn_objc_array_of_interfaces) << T;
459cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
460cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
461cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
462cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (ArraySize && !ArraySize->isTypeDependent() &&
463cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      !ArraySize->getType()->isIntegerType()) {
464cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
465cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
466cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    ArraySize->Destroy(Context);
467cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
468cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
469cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  llvm::APSInt ConstVal(32);
470cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
471cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    T = Context.getIncompleteArrayType(T, ASM, Quals);
472cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (ArraySize->isValueDependent()) {
473cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals);
474cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
475cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor             (!T->isDependentType() && !T->isConstantSizeType())) {
476cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Per C99, a variable array is an array with either a non-constant
477cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // size or an element type that has a non-constant-size
478cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals);
479cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
480cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
481cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
482cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (ConstVal.isSigned()) {
483cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      if (ConstVal.isNegative()) {
484cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        Diag(ArraySize->getLocStart(),
485cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor             diag::err_typecheck_negative_array_size)
486cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor          << ArraySize->getSourceRange();
487cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        return QualType();
488cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      } else if (ConstVal == 0) {
489cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        // GCC accepts zero sized static arrays.
490cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
491cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor          << ArraySize->getSourceRange();
492cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      }
493cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
494cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
495cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
496cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
497cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!getLangOptions().C99) {
498cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (ArraySize && !ArraySize->isTypeDependent() &&
499cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        !ArraySize->isValueDependent() &&
500cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        !ArraySize->isIntegerConstantExpr(Context))
501cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_vla);
502cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    else if (ASM != ArrayType::Normal || Quals != 0)
503cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_c99_array_usage);
504cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
505cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
506cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
507cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
508cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
509724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
510724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
511724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
512724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
513724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
5142943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
515724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
516724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
517724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
518724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
519724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
520724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
521724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
522724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
523724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
524724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
525724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
526724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
527724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
528724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
529724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
530724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
531724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
532724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
533724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
534724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
535724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
536724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
537724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
538724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
539724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 QualType *ParamTypes,
540724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
541724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 bool Variadic, unsigned Quals,
542724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 SourceLocation Loc, DeclarationName Entity) {
543724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
544724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    Diag(Loc, diag::err_func_returning_array_function) << T;
545724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
546724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
547724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
548724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
549724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
5502dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    QualType ParamType = adjustParameterType(ParamTypes[Idx]);
5512dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
552724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
553724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
554724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
555cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
556724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    ParamTypes[Idx] = ParamType;
557724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
558724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
559724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
560724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
561724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
562724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
563724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 Quals);
564724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
565724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
56698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
56798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// declarator to Type instances. Skip the outermost Skip type
56898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// objects.
569cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlQualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
57098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  bool OmittedReturnType = false;
57198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
57298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  if (D.getContext() == Declarator::BlockLiteralContext
57398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && Skip == 0
57498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && !D.getDeclSpec().hasTypeSpecifier()
57598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && (D.getNumTypeObjects() == 0
57698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump          || (D.getNumTypeObjects() == 1
57798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump              && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
57898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    OmittedReturnType = true;
57998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
580b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner  // long long is a C99 feature.
581d1eb332181f02a7e0a6f9749265e6a0f55555cd4Chris Lattner  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
582b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner      D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
583b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner    Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
584930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
585930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
586930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
587930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
588930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  switch (D.getKind()) {
589930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Abstract:
590930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Normal:
59198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  case Declarator::DK_Operator: {
59298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    const DeclSpec& DS = D.getDeclSpec();
59398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    if (OmittedReturnType)
59498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // We default to a dependent type initially.  Can be modified by
59598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // the first return statement.
59698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      T = Context.DependentTy;
597809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    else {
59898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      T = ConvertDeclSpecToType(DS);
599809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor      if (T.isNull())
600809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor        return T;
601809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    }
602930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
60398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  }
604930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
605930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Constructor:
606930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Destructor:
607930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Conversion:
608930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
609930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // "void" instead. Conversion operators will check their return
610930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // types separately.
611930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
612930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
613930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
6144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
615cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
616cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
617cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
618cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
619cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
62098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
62198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
62298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
623cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
624cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
6255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
6265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
6275618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
6289af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
6299af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
6309af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner        Diag(DeclType.Loc, diag::err_blocks_disable);
6319af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner
6325618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      if (DeclType.Cls.TypeQuals)
6335618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
6345618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      if (!T.getTypePtr()->isFunctionType())
6355618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
6365618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      else
6375618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        T = Context.getBlockPointerType(T);
6385618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
6395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
640cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
6415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
642cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    case DeclaratorChunk::Reference:
6437c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      T = BuildReferenceType(T, DeclType.Ref.LValueRef,
6447c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl                             DeclType.Ref.HasRestrict ? QualType::Restrict : 0,
645cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                             DeclType.Loc, Name);
6465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
6475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
648fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
64994f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
6505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
6515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
6525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
6535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
6545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
6555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
6565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
657cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, DeclType.Loc, Name);
6585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
6595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
660f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
6625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
6635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
6645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
66568cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner
666cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      // C99 6.7.5.3p1: The return type may not be a function or array type.
66768cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner      if (T->isArrayType() || T->isFunctionType()) {
668d162584991885ab004a02573a73ce06422b921fcChris Lattner        Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
669cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        T = Context.IntTy;
670cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        D.setInvalidType(true);
671cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      }
672cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner
673eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      if (FTI.NumArgs == 0) {
674c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        if (getLangOptions().CPlusPlus) {
675c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
676c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // function takes no arguments.
677971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
678965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor        } else if (FTI.isVariadic) {
679965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // We allow a zero-parameter variadic function in C if the
680965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // function is marked with the "overloadable"
681965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // attribute. Scan for this attribute now.
682965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
683965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
684965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
685965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
686965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
687965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
688965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
689965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
690965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
691965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
692965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
693965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
694c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        } else {
695c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // Simple void foo(), where the incoming T is the result type.
69672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          T = Context.getFunctionNoProtoType(T);
697c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
698eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      } else if (FTI.ArgInfo[0].Param == 0) {
6995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
700eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman        Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
7015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else {
7025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
7035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
7045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
7055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
7078123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
7088123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
70978c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
7102dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
7112dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
712beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor          assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
7132dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
7145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
7155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
71672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
7172dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
7185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
7195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
7205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
7215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
7225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
7232ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
7248123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
7252ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
7262ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
7275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
7284565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
7292ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
7308123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
7312ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
7322ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
733f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner              if (ArgTy.getCVRQualifiers())
7342ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
7352ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner
7362ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
7372ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
7382ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
739eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
740eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
741eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              ArgTy = Context.IntTy;
742eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
743eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
744eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
745eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ArgTys.push_back(ArgTy);
7495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
7505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
751971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis                                    FTI.isVariadic, FTI.TypeQuals);
7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
7535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
755f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
756f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
757e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor      DeclContext *DC = computeDeclContext(DeclType.Mem.Scope());
758f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
759f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // FIXME: Extend for dependent types when it's actually supported.
760f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // See ActOnCXXNestedNameSpecifier.
761f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
762f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.getTagDeclType(RD);
763f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
764f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        if (DC) {
765f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          Diag(DeclType.Mem.Scope().getBeginLoc(),
766f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl               diag::err_illegal_decl_mempointer_in_nonclass)
767f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl            << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
768f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl            << DeclType.Mem.Scope().getRange();
769f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        }
770f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
771f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.IntTy;
772f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
773f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
774f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
775f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      //   with reference type, or "cv void."
776f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (T->isReferenceType()) {
777f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
778f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
779f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
780f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
781f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
782f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (T->isVoidType()) {
783f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
784f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
785f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
786f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
787f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
788f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
789f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // object or incomplete types shall not be restrict-qualified."
790f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
791f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          !T->isIncompleteOrObjectType()) {
792f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
793f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << T;
794f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        DeclType.Mem.TypeQuals &= ~QualType::Restrict;
795f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
796f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
7974433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl      T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
7984433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl                    getQualifiedType(DeclType.Mem.TypeQuals);
799f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
800f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
801f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
802f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
803cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
804cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
805cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
806cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
807cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
808c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
809c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    if (const AttributeList *AL = DeclType.getAttrs())
810c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      ProcessTypeAttributeList(T, AL);
8115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
812971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
813971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
81472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
81572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
816971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
817971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
818971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // for a nonstatic member function, the function type to which a pointer
819971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // to member refers, or the top-level function type of a function typedef
820971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // declaration.
821971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    if (FnTy->getTypeQuals() != 0 &&
822971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
823584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor        ((D.getContext() != Declarator::MemberContext &&
824584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor          (!D.getCXXScopeSpec().isSet() ||
825e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor           !computeDeclContext(D.getCXXScopeSpec())->isRecord())) ||
826971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
827971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      if (D.isFunctionDeclarator())
828971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
829971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      else
830971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(),
831971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis             diag::err_invalid_qualified_typedef_function_type_use);
832971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
833971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      // Strip the cv-quals from the type.
834971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
8357fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                                  FnTy->getNumArgs(), FnTy->isVariadic(), 0);
836971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
837971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
8385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8390bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // If there were any type attributes applied to the decl itself (not the
8400bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // type, apply the type attribute to the type!)
8410bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  if (const AttributeList *Attrs = D.getAttributes())
842c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(T, Attrs);
8430bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner
8445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T;
8455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
847a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
848360300ca2ee298d585d29baf006519507d968812Fariborz Jahanian/// declarator
849a526c5c67e5a0473c340903ee542ce570119665fTed KremenekQualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
85089951a86b594513c2a013532ed45d197413b1087Chris Lattner  ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(static_cast<Decl *>(D));
851306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  QualType T = MDecl->getResultType();
852306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  llvm::SmallVector<QualType, 16> ArgTys;
853306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
8543560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian  // Add the first two invisible argument types for self and _cmd.
855f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  if (MDecl->isInstanceMethod()) {
856a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
8571f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    selfTy = Context.getPointerType(selfTy);
8581f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    ArgTys.push_back(selfTy);
85989951a86b594513c2a013532ed45d197413b1087Chris Lattner  } else
860a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ArgTys.push_back(Context.getObjCIdType());
861a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ArgTys.push_back(Context.getObjCSelType());
8623560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian
86389951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
86489951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = MDecl->param_end(); PI != E; ++PI) {
86589951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType ArgTy = (*PI)->getType();
866306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    assert(!ArgTy.isNull() && "Couldn't parse type?");
867beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor    ArgTy = adjustParameterType(ArgTy);
868306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    ArgTys.push_back(ArgTy);
869306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  }
870306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
8717fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                              MDecl->isVariadic(), 0);
872306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  return T;
873306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian}
874306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
8759e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
8769e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
8779e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// they point to and return true. If T1 and T2 aren't pointer types
8789e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// or pointer-to-member types, or if they are not similar at this
8799e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// level, returns false and leaves T1 and T2 unchanged. Top-level
8809e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// qualifiers on T1 and T2 are ignored. This function will typically
8819e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// be called in a loop that successively "unwraps" pointer and
8829e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// pointer-to-member types to compare them at each level.
883ecb81f28cb279b7d8e84296445a4131fa80b69a9Chris Lattnerbool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
88457373266011f73418381b736015d8d2bb0381176Douglas Gregor  const PointerType *T1PtrType = T1->getAsPointerType(),
88557373266011f73418381b736015d8d2bb0381176Douglas Gregor                    *T2PtrType = T2->getAsPointerType();
88657373266011f73418381b736015d8d2bb0381176Douglas Gregor  if (T1PtrType && T2PtrType) {
88757373266011f73418381b736015d8d2bb0381176Douglas Gregor    T1 = T1PtrType->getPointeeType();
88857373266011f73418381b736015d8d2bb0381176Douglas Gregor    T2 = T2PtrType->getPointeeType();
88957373266011f73418381b736015d8d2bb0381176Douglas Gregor    return true;
89057373266011f73418381b736015d8d2bb0381176Douglas Gregor  }
89157373266011f73418381b736015d8d2bb0381176Douglas Gregor
8924433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
8934433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl                          *T2MPType = T2->getAsMemberPointerType();
89421593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl  if (T1MPType && T2MPType &&
89521593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T1MPType->getClass()) ==
89621593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T2MPType->getClass())) {
8974433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T1 = T1MPType->getPointeeType();
8984433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T2 = T2MPType->getPointeeType();
8994433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    return true;
9004433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  }
90157373266011f73418381b736015d8d2bb0381176Douglas Gregor  return false;
90257373266011f73418381b736015d8d2bb0381176Douglas Gregor}
90357373266011f73418381b736015d8d2bb0381176Douglas Gregor
904cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlSema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
9055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
9065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
9075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
9085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
909cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  QualType T = GetTypeForDeclarator(D, S);
910809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (T.isNull())
911809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
9125912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
9136d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor  // Check that there are no default arguments (C++ only).
9146d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor  if (getLangOptions().CPlusPlus)
9156d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
9166d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
9175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T.getAsOpaquePtr();
9185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
920c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
921c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
922c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
923c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
924c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
925232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
926232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
927c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
928c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
929c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnerstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
930c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
931232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
932232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
933232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
934232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
935c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
936c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
937232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
938232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
939232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
940545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
941f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
942c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
943232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
944545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
945232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
946c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
947dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
948dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
949c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
950232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
951232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
952232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
953f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
954c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
955c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
956d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
957d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// specified type.  The attribute contains 1 argument, weak or strong.
958d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanianstatic void HandleObjCGCTypeAttribute(QualType &Type,
9593b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner                                      const AttributeList &Attr, Sema &S) {
960d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (Type.getObjCGCAttr() != QualType::GCNone) {
9615934e75d98d99374f72722a69c5eefe026f35c74Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
962d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
963d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
964d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
965d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
966ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (!Attr.getParameterName()) {
967ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
968ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
969ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    return;
970ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
9713b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  QualType::GCAttrTypes GCAttr;
972ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (Attr.getNumArgs() != 0) {
973d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
974d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
975d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
976d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (Attr.getParameterName()->isStr("weak"))
9773b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner    GCAttr = QualType::Weak;
978d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else if (Attr.getParameterName()->isStr("strong"))
9793b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner    GCAttr = QualType::Strong;
980d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
981d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
982d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      << "objc_gc" << Attr.getParameterName();
983d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
984d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
985d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
9863b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  Type = S.Context.getObjCGCQualType(Type, GCAttr);
987d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
988d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
989c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnervoid Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
990c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
991c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
992c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
993c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
994c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  for (; AL; AL = AL->getNext()) {
995c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // If this is an attribute we can handle, do so now, otherwise, add it to
996c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // the LeftOverAttrs list for rechaining.
997c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    switch (AL->getKind()) {
998c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
999c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
1000c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1001c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
1002d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    case AttributeList::AT_objc_gc:
1003d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      HandleObjCGCTypeAttribute(Result, *AL, *this);
1004d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
1005c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
1006c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  }
1007232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
1008232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
100986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// @brief Ensure that the type T is a complete type.
10104ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10114ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
10124ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
101386447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
101486447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
101586447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
101686447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
101786447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
10184ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10194ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
10204ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
10214ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10224ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
10234ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10244ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param diag The diagnostic value (e.g.,
10254ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c diag::err_typecheck_decl_incomplete_type) that will be used
10264ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// for the error message if @p T is incomplete.
10274ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10284ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range1  An optional range in the source code that will be a
10294ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
10304ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10314ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range2  An optional range in the source code that will be a
10324ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
10334ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10344ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param PrintType If non-NULL, the type that should be printed
10354ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// instead of @p T. This parameter should be used when the type that
10364ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// we're checking for incompleteness isn't the type that should be
10374ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// displayed to the user, e.g., when T is a type and PrintType is a
10384ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// pointer to T.
10394ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
10404ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
10414ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
104286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag,
10434ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                                  SourceRange Range1, SourceRange Range2,
10444ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                                  QualType PrintType) {
10454ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
10464ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
10474ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
10484ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
1049d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
1050d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // class template specialization, try to instantiate it.
1051d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (const RecordType *Record = T->getAsRecordType()) {
10522943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
1053d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
10542943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
10552943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        // Update the class template specialization's location to
10562943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        // refer to the point of instantiation.
10572943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        if (Loc.isValid())
10582943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor          ClassTemplateSpec->setLocation(Loc);
10592943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor        return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
10602943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor                                             /*ExplicitInstantiation=*/false);
10612943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor      }
1062d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    } else if (CXXRecordDecl *Rec
1063d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1064d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1065d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        // Find the class template specialization that surrounds this
1066d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        // member class.
1067d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        ClassTemplateSpecializationDecl *Spec = 0;
1068d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        for (DeclContext *Parent = Rec->getDeclContext();
1069d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor             Parent && !Spec; Parent = Parent->getParent())
1070d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
1071d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        assert(Spec && "Not a member of a class template specialization?");
1072d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor        return InstantiateClass(Loc, Rec, Pattern,
1073d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                                Spec->getTemplateArgs(),
1074d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                                Spec->getNumTemplateArgs());
1075d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
1076d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
1077d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
10782943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
10794ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (PrintType.isNull())
10804ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    PrintType = T;
10814ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
10824ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
10834ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  Diag(Loc, diag) << PrintType << Range1 << Range2;
10843c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
10854ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
10864ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // type, produce
10874ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  const TagType *Tag = 0;
10884ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (const RecordType *Record = T->getAsRecordType())
10894ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Record;
10904ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  else if (const EnumType *Enum = T->getAsEnumType())
10914ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Enum;
10924ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
10934ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
10944ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Diag(Tag->getDecl()->getLocation(),
10954ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
10964ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
10974ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
10984ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
10994ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
11004ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
1101e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
1102e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor/// \brief Retrieve a version of the type 'T' that is qualified by the
1103e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor/// nested-name-specifier contained in SS.
1104e6258936178b4c52b43b3b9dbec13552961cd645Douglas GregorQualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1105e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor  if (!SS.isSet() || SS.isInvalid() || T.isNull())
1106e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
1107e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
1108ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  NestedNameSpecifier *NNS
11093507369940bfb269551bfa1fec812481f60e3552Douglas Gregor    = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
1110ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  return Context.getQualifiedNameType(NNS, T);
1111e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
1112