SemaType.cpp revision cd281c3ded486ced5aad29dd7c3fa22b7514c3d8
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"
17e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
18e4858a65a93fb36c099d8dd2ea0a98e33e77687eDaniel Dunbar#include "clang/Parse/DeclSpec.h"
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
22930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
23930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \param DS  the declaration specifiers
24930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \returns The type described by the declaration specifiers, or NULL
25930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// if there was an error.
26fca0ddd42965e0b7ae821213486d4e0dd71fb439Chris LattnerQualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) {
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
29958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  QualType Result;
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
3296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
3396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
3496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
37fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
39fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
43fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
45958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
4664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
4764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
4864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
4964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
50f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
51f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
5264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
5364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
5464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
5564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
56f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
57f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
5864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
5964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
6064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
61d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
6262f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
63097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
64ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner      Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
6562f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner                                              DS.getNumProtocolQualifiers());
6662f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
6762f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
6862f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner
69d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
70d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
71d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
72d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
73d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
74d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
75d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
76d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    if (getLangOptions().ImplicitInt) {
7735d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
7835d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
7935d276f443462249b436951c1c663820569e1768Chris Lattner      if (DS.isEmpty())
80173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner        Diag(DS.getSourceRange().getBegin(), diag::warn_missing_declspec)
81173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner        << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
82173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner                                                 "int");
834310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
84d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
85d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
86d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
87d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
884310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
894310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      unsigned DK = getLangOptions().CPlusPlus && !getLangOptions().Microsoft?
904310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor          diag::err_missing_type_specifier
9135d276f443462249b436951c1c663820569e1768Chris Lattner        : diag::warn_missing_type_specifier;
92173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner      Diag(DS.getSourceRange().getBegin(), DK)
93173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner        << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
94173144affecc3f97b73b075c44752aff8cfcfc3aChris Lattner                                                 "int");
95d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
96d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner
97d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // FALL THROUGH.
983cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
101fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
102fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
103fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
104fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_longlong:    Result = Context.LongLongTy; break;
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
108fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
109fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
110fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
111fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_longlong:    Result =Context.UnsignedLongLongTy; break;
1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
114958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1153cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
116fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
117958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
118958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
119fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
120958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
121fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
122958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
123fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(0 && "FIXME: GNU decimal extensions not supported yet!");
12899dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Decl *D = static_cast<Decl *>(DS.getTypeRep());
13399dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner    assert(D && "Didn't get a decl for a class/enum/union/struct?");
1345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
1382ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    Result = Context.getTypeDeclType(cast<TypeDecl>(D));
139958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1411a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
1451a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
1462ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
1471a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
1481a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
1491a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      // we have this "hack" for now...
1501a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
1511a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor        Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
1521a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor                                                       (ObjCProtocolDecl**)PQ,
1531a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor                                               DS.getNumProtocolQualifiers());
1541a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      else if (Result == Context.getObjCIdType())
155ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
156ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
157ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner                                                DS.getNumProtocolQualifiers());
1584262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff      else if (Result == Context.getObjCClassType())
1594262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
1608dfb0c57ddb700b163afa89e3ab160f1de26753dSteve Naroff        Diag(DS.getSourceRange().getBegin(),
1614262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff             diag::err_qualified_class_unsupported) << DS.getSourceRange();
1624262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff      else
1634262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        Diag(DS.getSourceRange().getBegin(),
1644262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff             diag::err_invalid_protocol_qualifiers) << DS.getSourceRange();
165c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
167958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
169958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
170958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
171958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
172d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
173fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
174958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
175d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
176d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    Expr *E = static_cast<Expr *>(DS.getTypeRep());
177d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
178d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
17972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    Result = Context.getTypeOfExprType(E);
180958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
181d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
182809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
183809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return QualType();
1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
185958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
186958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
187f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
188f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor    if (getLangOptions().Freestanding)
189f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor      Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
190fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
191f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
192958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
193958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
194958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner         "FIXME: imaginary types not supported yet!");
195958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
19638d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // See if there are any attributes on the declspec that apply to the type (as
19738d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // opposed to the decl).
198fca0ddd42965e0b7ae821213486d4e0dd71fb439Chris Lattner  if (const AttributeList *AL = DS.getAttributes())
199c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(Result, AL);
200f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
20196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
20296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
20396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
20496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
20596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
20696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
20796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (TypeQuals & QualType::Restrict) {
208bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar      if (Result->isPointerType() || Result->isReferenceType()) {
209bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar        QualType EltTy = Result->isPointerType() ?
210bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar          Result->getAsPointerType()->getPointeeType() :
211bb71001d287fda144c4bcf096124d8e3667d6930Daniel Dunbar          Result->getAsReferenceType()->getPointeeType();
212bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner
213bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // If we have a pointer or reference, the pointee must have an object or
214bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
215bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
216bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          Diag(DS.getRestrictSpecLoc(),
217d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
218d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
219bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
220bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
221bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
22296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Diag(DS.getRestrictSpecLoc(),
223d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner             diag::err_typecheck_invalid_restrict_not_pointer)
224d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
225bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
22696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
22796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
22896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
22996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
23096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
23196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
23296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
23396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
23496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
23596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if (TypeQuals & QualType::Const)
23696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
23796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      else {
23896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        assert((TypeQuals & QualType::Volatile) &&
23996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner               "Has CV quals but not C or V?");
24096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
24196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
242d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, diag::warn_typecheck_function_qualifiers)
243d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
24496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
24596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
246f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
247f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
248f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
249f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
250f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
2511a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
2521a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
253f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
254f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Const;
255f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Volatile;
256f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    }
257f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor
25896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Result.getQualifiedType(TypeQuals);
25996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
260f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
261f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
262f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
263cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
264cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
265cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
266cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
267cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
268cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
269cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
270cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
271cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
272cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
273cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
274cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the pointer type.
275cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
276cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
277cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
278cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
279cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
280cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
281cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
282cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
283cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
284cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
285cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildPointerType(QualType T, unsigned Quals,
286cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
287cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
288cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
289cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
290cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
291cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
292cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
293cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
294cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
295cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
296cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
297cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
298cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
299cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Quals &= ~QualType::Restrict;
300cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
301cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
302cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
303cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return Context.getPointerType(T).getQualifiedType(Quals);
304cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
305cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
306cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
307cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
308cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
309cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
310cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the reference type.
311cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
312cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
313cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
314cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
315cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
316cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
317cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
318cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
319cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
320cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
321cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildReferenceType(QualType T, unsigned Quals,
322cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                  SourceLocation Loc, DeclarationName Entity) {
323cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
324cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ [dcl.ref]p4: There shall be no references to references.
325cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //
326cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // According to C++ DR 106, references to references are only
327cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // diagnosed when they are written directly (e.g., "int & &"),
328cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // but not when they happen via a typedef:
329cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //
330cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //   typedef int& intref;
331cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //   typedef intref& intref2;
332cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    //
333cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Parser::ParserDeclaratorInternal diagnoses the case where
334cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // references are written directly; here, we handle the
335cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // collapsing of references-to-references as described in C++
336cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // DR 106 and amended by C++ DR 540.
337cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return T;
338cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
339cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
340cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
341cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   A declarator that specifies the type “reference to cv void”
342cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
343cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
344cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
345cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
346cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
347cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
348cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
349cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // object or incomplete types shall not be restrict-qualified."
350cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
351cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
352cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << T;
353cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Quals &= ~QualType::Restrict;
354cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
355cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
356cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
357cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   [...] Cv-qualified references are ill-formed except when the
358cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   cv-qualifiers are introduced through the use of a typedef
359cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   (7.1.3) or of a template type argument (14.3), in which case
360cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   the cv-qualifiers are ignored.
361cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //
362cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // We diagnose extraneous cv-qualifiers for the non-typedef,
363cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // non-template type argument case within the parser. Here, we just
364cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // ignore any extraneous cv-qualifiers.
365cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  Quals &= ~QualType::Const;
366cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  Quals &= ~QualType::Volatile;
367cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
368cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
369cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return Context.getReferenceType(T).getQualifiedType(Quals);
370cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
371cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
372cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
373cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
374cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
375cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
376cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
377cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
378cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ArraySize Expression describing the size of the array.
379cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
380cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the array's
381cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// element type.
382cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
383cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
384cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// array type or, if there is no such entity, the location of the
385cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have array type.
386cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
387cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
388cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
389cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
390cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
391cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
392cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
393cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
394cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              SourceLocation Loc, DeclarationName Entity) {
395cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: If the element type is an incomplete or function type,
396cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
397cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (DiagnoseIncompleteType(Loc, T,
398cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                             diag::err_illegal_decl_array_incomplete_type))
399cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
400cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
401cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
402cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
403cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
404cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
405cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
406cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
407cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ 8.3.2p4: There shall be no ... arrays of references ...
408cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
409cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_references)
410cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << getPrintableNameForEntity(Entity);
411cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
412cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
413cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
414cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (const RecordType *EltTy = T->getAsRecordType()) {
415cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
416cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
417cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
418cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
419cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (T->isObjCInterfaceType()) {
420cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::warn_objc_array_of_interfaces) << T;
421cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
422cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
423cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
424cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (ArraySize && !ArraySize->isTypeDependent() &&
425cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      !ArraySize->getType()->isIntegerType()) {
426cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
427cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
428cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    ArraySize->Destroy(Context);
429cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
430cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
431cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  llvm::APSInt ConstVal(32);
432cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
433cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    T = Context.getIncompleteArrayType(T, ASM, Quals);
434cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (ArraySize->isValueDependent()) {
435cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals);
436cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
437cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor             (!T->isDependentType() && !T->isConstantSizeType())) {
438cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Per C99, a variable array is an array with either a non-constant
439cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // size or an element type that has a non-constant-size
440cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals);
441cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
442cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
443cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
444cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (ConstVal.isSigned()) {
445cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      if (ConstVal.isNegative()) {
446cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        Diag(ArraySize->getLocStart(),
447cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor             diag::err_typecheck_negative_array_size)
448cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor          << ArraySize->getSourceRange();
449cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        return QualType();
450cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      } else if (ConstVal == 0) {
451cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        // GCC accepts zero sized static arrays.
452cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
453cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor          << ArraySize->getSourceRange();
454cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      }
455cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
456cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
457cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
458cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
459cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!getLangOptions().C99) {
460cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (ArraySize && !ArraySize->isTypeDependent() &&
461cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        !ArraySize->isValueDependent() &&
462cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor        !ArraySize->isIntegerConstantExpr(Context))
463cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_vla);
464cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    else if (ASM != ArrayType::Normal || Quals != 0)
465cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_c99_array_usage);
466cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
467cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
468cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
469cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
470cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
471cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
47298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
47398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// declarator to Type instances. Skip the outermost Skip type
47498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// objects.
475cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlQualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
47698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  bool OmittedReturnType = false;
47798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
47898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  if (D.getContext() == Declarator::BlockLiteralContext
47998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && Skip == 0
48098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && !D.getDeclSpec().hasTypeSpecifier()
48198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && (D.getNumTypeObjects() == 0
48298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump          || (D.getNumTypeObjects() == 1
48398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump              && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
48498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    OmittedReturnType = true;
48598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
486b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner  // long long is a C99 feature.
487d1eb332181f02a7e0a6f9749265e6a0f55555cd4Chris Lattner  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
488b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner      D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
489b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner    Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
490930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
491930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
492930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
493930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
494930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  switch (D.getKind()) {
495930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Abstract:
496930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Normal:
49798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  case Declarator::DK_Operator: {
49898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    const DeclSpec& DS = D.getDeclSpec();
49998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    if (OmittedReturnType)
50098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // We default to a dependent type initially.  Can be modified by
50198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // the first return statement.
50298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      T = Context.DependentTy;
503809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    else {
50498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      T = ConvertDeclSpecToType(DS);
505809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor      if (T.isNull())
506809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor        return T;
507809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    }
508930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
50998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  }
510930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
511930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Constructor:
512930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Destructor:
513930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Conversion:
514930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
515930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // "void" instead. Conversion operators will check their return
516930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // types separately.
517930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
518930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
519930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
5204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
521cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
522cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
523cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
524cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
525cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
52698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
52798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
52898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
529cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
530cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
5325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
5335618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
5345618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      if (DeclType.Cls.TypeQuals)
5355618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
5365618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      if (!T.getTypePtr()->isFunctionType())
5375618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
5385618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      else
5395618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        T = Context.getBlockPointerType(T);
5405618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
5415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
542cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
5435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
544cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    case DeclaratorChunk::Reference:
545cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = BuildReferenceType(T,
546cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                             DeclType.Ref.HasRestrict? QualType::Restrict : 0,
547cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                             DeclType.Loc, Name);
5485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
5495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
550fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
55194f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
5535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
5545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
5555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
5565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
5575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
5585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
559cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, DeclType.Loc, Name);
5605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
5615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
562f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
5635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
5655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
5665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
56768cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner
568cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      // C99 6.7.5.3p1: The return type may not be a function or array type.
56968cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner      if (T->isArrayType() || T->isFunctionType()) {
570d162584991885ab004a02573a73ce06422b921fcChris Lattner        Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
571cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        T = Context.IntTy;
572cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        D.setInvalidType(true);
573cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      }
574cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner
575eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      if (FTI.NumArgs == 0) {
576c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        if (getLangOptions().CPlusPlus) {
577c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
578c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // function takes no arguments.
579971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
580965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor        } else if (FTI.isVariadic) {
581965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // We allow a zero-parameter variadic function in C if the
582965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // function is marked with the "overloadable"
583965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // attribute. Scan for this attribute now.
584965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
585965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
586965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
587965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
588965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
589965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
590965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
591965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
592965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
593965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
594965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
595965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
596c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        } else {
597c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // Simple void foo(), where the incoming T is the result type.
59872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          T = Context.getFunctionNoProtoType(T);
599c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
600eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      } else if (FTI.ArgInfo[0].Param == 0) {
6015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
602eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman        Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
6035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else {
6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
6055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
6065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
6075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
6098123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
6108123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
61178c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
61208d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
61308d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
61408d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // This matches the conversion that is done in
615bff5f5cfd194c3cc096ba89360fb537814a9666aNate Begeman          // Sema::ActOnParamDeclarator(). Without this conversion, the
61608d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // argument type in the function prototype *will not* match the
61708d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // type in ParmVarDecl (which makes the code generator unhappy).
61808d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
61908d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // FIXME: We still apparently need the conversion in
620e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner          // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
62108d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // it should be driving off the type being created here.
62208d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
62308d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // FIXME: If a source translation tool needs to see the original type,
62408d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // then we need to consider storing both types somewhere...
62508d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
626e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner          if (ArgTy->isArrayType()) {
627e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner            ArgTy = Context.getArrayDecayedType(ArgTy);
628529bd02affa96a311dd9ab131f2ab4d833017fb7Chris Lattner          } else if (ArgTy->isFunctionType())
62908d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff            ArgTy = Context.getPointerType(ArgTy);
630e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
6315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
6325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
63372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
63408d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          else if (ArgTy->isVoidType()) {
6355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
6365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
6385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
6395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
6402ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
6418123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
6422ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
6432ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
6445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
6454565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
6462ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
6478123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
6482ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
6492ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
650f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner              if (ArgTy.getCVRQualifiers())
6512ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
6522ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner
6532ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
6542ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
6552ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
656eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
657eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
658eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              ArgTy = Context.IntTy;
659eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
660eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
661eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
662eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
6635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
6645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ArgTys.push_back(ArgTy);
6665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
6675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
668971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis                                    FTI.isVariadic, FTI.TypeQuals);
6695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
6705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
6715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
672f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
673f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
674f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      DeclContext *DC = static_cast<DeclContext*>(
675f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        DeclType.Mem.Scope().getScopeRep());
676f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
677f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // FIXME: Extend for dependent types when it's actually supported.
678f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // See ActOnCXXNestedNameSpecifier.
679f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
680f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.getTagDeclType(RD);
681f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
682f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        if (DC) {
683f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          Diag(DeclType.Mem.Scope().getBeginLoc(),
684f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl               diag::err_illegal_decl_mempointer_in_nonclass)
685f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl            << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
686f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl            << DeclType.Mem.Scope().getRange();
687f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        }
688f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
689f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.IntTy;
690f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
691f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
692f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
693f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      //   with reference type, or "cv void."
694f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (T->isReferenceType()) {
695f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
696f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
697f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
698f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
699f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
700f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (T->isVoidType()) {
701f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
702f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
703f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
704f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
705f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
706f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
707f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // object or incomplete types shall not be restrict-qualified."
708f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
709f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          !T->isIncompleteOrObjectType()) {
710f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
711f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << T;
712f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        DeclType.Mem.TypeQuals &= ~QualType::Restrict;
713f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
714f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
7154433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl      T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
7164433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl                    getQualifiedType(DeclType.Mem.TypeQuals);
717f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
718f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
719f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
720f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
721cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
722cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
723cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
724cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
725cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
726c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
727c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    if (const AttributeList *AL = DeclType.getAttrs())
728c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      ProcessTypeAttributeList(T, AL);
7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
730971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
731971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
73272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
73372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
734971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
735971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
736971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // for a nonstatic member function, the function type to which a pointer
737971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // to member refers, or the top-level function type of a function typedef
738971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // declaration.
739971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    if (FnTy->getTypeQuals() != 0 &&
740971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
741584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor        ((D.getContext() != Declarator::MemberContext &&
742584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor          (!D.getCXXScopeSpec().isSet() ||
743584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor           !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
744bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor              ->isRecord())) ||
745971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
746971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      if (D.isFunctionDeclarator())
747971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
748971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      else
749971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(),
750971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis             diag::err_invalid_qualified_typedef_function_type_use);
751971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
752971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      // Strip the cv-quals from the type.
753971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
7547fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                                  FnTy->getNumArgs(), FnTy->isVariadic(), 0);
755971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
756971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7580bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // If there were any type attributes applied to the decl itself (not the
7590bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // type, apply the type attribute to the type!)
7600bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  if (const AttributeList *Attrs = D.getAttributes())
761c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(T, Attrs);
7620bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner
7635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T;
7645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
766a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
767360300ca2ee298d585d29baf006519507d968812Fariborz Jahanian/// declarator
768a526c5c67e5a0473c340903ee542ce570119665fTed KremenekQualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
76989951a86b594513c2a013532ed45d197413b1087Chris Lattner  ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(static_cast<Decl *>(D));
770306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  QualType T = MDecl->getResultType();
771306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  llvm::SmallVector<QualType, 16> ArgTys;
772306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
7733560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian  // Add the first two invisible argument types for self and _cmd.
774f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  if (MDecl->isInstanceMethod()) {
775a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
7761f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    selfTy = Context.getPointerType(selfTy);
7771f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    ArgTys.push_back(selfTy);
77889951a86b594513c2a013532ed45d197413b1087Chris Lattner  } else
779a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ArgTys.push_back(Context.getObjCIdType());
780a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ArgTys.push_back(Context.getObjCSelType());
7813560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian
78289951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
78389951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = MDecl->param_end(); PI != E; ++PI) {
78489951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType ArgTy = (*PI)->getType();
785306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    assert(!ArgTy.isNull() && "Couldn't parse type?");
786306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
787306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    // This matches the conversion that is done in
788e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // Sema::ActOnParamDeclarator().
789e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    if (ArgTy->isArrayType())
790e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner      ArgTy = Context.getArrayDecayedType(ArgTy);
791306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    else if (ArgTy->isFunctionType())
792306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian      ArgTy = Context.getPointerType(ArgTy);
793306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    ArgTys.push_back(ArgTy);
794306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  }
795306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
7967fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                              MDecl->isVariadic(), 0);
797306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  return T;
798306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian}
799306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
8009e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
8019e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
8029e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// they point to and return true. If T1 and T2 aren't pointer types
8039e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// or pointer-to-member types, or if they are not similar at this
8049e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// level, returns false and leaves T1 and T2 unchanged. Top-level
8059e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// qualifiers on T1 and T2 are ignored. This function will typically
8069e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// be called in a loop that successively "unwraps" pointer and
8079e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// pointer-to-member types to compare them at each level.
808ecb81f28cb279b7d8e84296445a4131fa80b69a9Chris Lattnerbool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
80957373266011f73418381b736015d8d2bb0381176Douglas Gregor  const PointerType *T1PtrType = T1->getAsPointerType(),
81057373266011f73418381b736015d8d2bb0381176Douglas Gregor                    *T2PtrType = T2->getAsPointerType();
81157373266011f73418381b736015d8d2bb0381176Douglas Gregor  if (T1PtrType && T2PtrType) {
81257373266011f73418381b736015d8d2bb0381176Douglas Gregor    T1 = T1PtrType->getPointeeType();
81357373266011f73418381b736015d8d2bb0381176Douglas Gregor    T2 = T2PtrType->getPointeeType();
81457373266011f73418381b736015d8d2bb0381176Douglas Gregor    return true;
81557373266011f73418381b736015d8d2bb0381176Douglas Gregor  }
81657373266011f73418381b736015d8d2bb0381176Douglas Gregor
8174433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
8184433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl                          *T2MPType = T2->getAsMemberPointerType();
81921593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl  if (T1MPType && T2MPType &&
82021593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T1MPType->getClass()) ==
82121593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T2MPType->getClass())) {
8224433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T1 = T1MPType->getPointeeType();
8234433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T2 = T2MPType->getPointeeType();
8244433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    return true;
8254433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  }
82657373266011f73418381b736015d8d2bb0381176Douglas Gregor  return false;
82757373266011f73418381b736015d8d2bb0381176Douglas Gregor}
82857373266011f73418381b736015d8d2bb0381176Douglas Gregor
829cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlSema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
8305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
8315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
8325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
8335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
834cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  QualType T = GetTypeForDeclarator(D, S);
835809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (T.isNull())
836809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
8375912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
8386d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor  // Check that there are no default arguments (C++ only).
8396d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor  if (getLangOptions().CPlusPlus)
8406d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
8416d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
8425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T.getAsOpaquePtr();
8435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
845c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
846c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
847c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
848c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
849c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
850232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
851232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
852c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
853c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
854c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnerstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
855c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
856232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
857232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
858232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
859232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
860c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
861c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
862232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
863232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
864232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
865545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
866f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
867c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
868232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
869545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
870232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
871c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
872dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
873dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
874c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
875232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
876232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
877232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
878f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
879c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
880c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
881d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
882d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// specified type.  The attribute contains 1 argument, weak or strong.
883d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanianstatic void HandleObjCGCTypeAttribute(QualType &Type,
8843b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner                                      const AttributeList &Attr, Sema &S) {
885d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (Type.getObjCGCAttr() != QualType::GCNone) {
8865934e75d98d99374f72722a69c5eefe026f35c74Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
887d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
888d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
889d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
890d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
891ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (!Attr.getParameterName()) {
892ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
893ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
894ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    return;
895ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
8963b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  QualType::GCAttrTypes GCAttr;
897ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (Attr.getNumArgs() != 0) {
898d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
899d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
900d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
901d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (Attr.getParameterName()->isStr("weak"))
9023b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner    GCAttr = QualType::Weak;
903d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else if (Attr.getParameterName()->isStr("strong"))
9043b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner    GCAttr = QualType::Strong;
905d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
906d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
907d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      << "objc_gc" << Attr.getParameterName();
908d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
909d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
910d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
9113b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  Type = S.Context.getObjCGCQualType(Type, GCAttr);
912d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
913d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
914c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnervoid Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
915c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
916c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
917c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
918c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
919c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  for (; AL; AL = AL->getNext()) {
920c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // If this is an attribute we can handle, do so now, otherwise, add it to
921c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // the LeftOverAttrs list for rechaining.
922c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    switch (AL->getKind()) {
923c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
924c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
925c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      HandleAddressSpaceTypeAttribute(Result, *AL, *this);
926c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
927d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    case AttributeList::AT_objc_gc:
928d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      HandleObjCGCTypeAttribute(Result, *AL, *this);
929d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
930c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
931c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  }
932232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
933232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
9344ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @brief If the type T is incomplete and cannot be completed,
9354ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// produce a suitable diagnostic.
9364ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
9374ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
9384ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
9394ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// type, returns false. If @p T is incomplete, issues the diagnostic
9404ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @p diag (giving it the type @p T) and returns true.
9414ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
9424ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
9434ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
9444ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
9454ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
9464ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
9474ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param diag The diagnostic value (e.g.,
9484ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c diag::err_typecheck_decl_incomplete_type) that will be used
9494ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// for the error message if @p T is incomplete.
9504ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
9514ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range1  An optional range in the source code that will be a
9524ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
9534ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
9544ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range2  An optional range in the source code that will be a
9554ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
9564ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
9574ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param PrintType If non-NULL, the type that should be printed
9584ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// instead of @p T. This parameter should be used when the type that
9594ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// we're checking for incompleteness isn't the type that should be
9604ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// displayed to the user, e.g., when T is a type and PrintType is a
9614ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// pointer to T.
9624ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
9634ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
9644ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
9654ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
9664ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @todo When Clang gets proper support for C++ templates, this
9674ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// routine will also be able perform template instantiation when @p T
9684ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// is a class template specialization.
9694ec339f43c0cae2678334850c90926bea10999c7Douglas Gregorbool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag,
9704ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                                  SourceRange Range1, SourceRange Range2,
9714ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                                  QualType PrintType) {
9724ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
9734ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
9744ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
9754ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
9764ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (PrintType.isNull())
9774ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    PrintType = T;
9784ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
9794ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
9804ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  Diag(Loc, diag) << PrintType << Range1 << Range2;
9813c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
9824ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
9834ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // type, produce
9844ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  const TagType *Tag = 0;
9854ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (const RecordType *Record = T->getAsRecordType())
9864ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Record;
9874ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  else if (const EnumType *Enum = T->getAsEnumType())
9884ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Enum;
9894ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
9904ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
9914ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Diag(Tag->getDecl()->getLocation(),
9924ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
9934ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
9944ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
9954ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
9964ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
9974ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
998