SemaType.cpp revision f244cd7e54753caf6edb76df430dea2f43bb82a8
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()) {
32d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  default: assert(0 && "Unknown TypeSpecType!");
3396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
3496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
3596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
38fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
40fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
44fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
46958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
4764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
4864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
4964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
5064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
51f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
52f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
5364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
5464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
5564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
5664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
57f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
58f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
5964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
6064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
6164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
62d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
6362f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
64097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
65ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner      Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
6662f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner                                              DS.getNumProtocolQualifiers());
6762f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
6862f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
6962f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner
70d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
71d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
72d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
73d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
74d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
75d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
76d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
77d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    if (getLangOptions().ImplicitInt) {
78d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
79d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner                                       DeclSpec::PQ_TypeSpecifier |
80d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner                                       DeclSpec::PQ_TypeQualifier)) == 0)
81d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner        Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec);
82d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    } else {
83d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
84d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
85d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
86d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
87930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      // FIXME: this should be a hard error in C++
88d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      if (!DS.hasTypeSpecifier())
89d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner        Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier);
90d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
91d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner
92d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // FALL THROUGH.
933cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
96fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
97fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
98fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
99fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_longlong:    Result = Context.LongLongTy; break;
1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
103fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
104fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
105fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
106fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_longlong:    Result =Context.UnsignedLongLongTy; break;
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
109958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1103cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
111fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
112958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
113958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
114fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
115958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
116fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
117958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
118fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
1195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
1205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
1215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(0 && "FIXME: GNU decimal extensions not supported yet!");
12399dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Decl *D = static_cast<Decl *>(DS.getTypeRep());
12899dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner    assert(D && "Didn't get a decl for a class/enum/union/struct?");
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
1332ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    Result = Context.getTypeDeclType(cast<TypeDecl>(D));
134958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1361a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
1401a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
1412ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
1421a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
1431a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
1441a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      // we have this "hack" for now...
1451a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
1461a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor        Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
1471a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor                                                       (ObjCProtocolDecl**)PQ,
1481a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor                                               DS.getNumProtocolQualifiers());
1491a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      else if (Result == Context.getObjCIdType())
150ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
151ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
152ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner                                                DS.getNumProtocolQualifiers());
153c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
155958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
157958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
158958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
159958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
160d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
161fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
162958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
163d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
164d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    Expr *E = static_cast<Expr *>(DS.getTypeRep());
165d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
166d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
167fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfExpr(E);
168958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
169d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
1705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
171958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
172958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
173f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
174f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor    if (getLangOptions().Freestanding)
175f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor      Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
176fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
177f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
178958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
179958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
180958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner         "FIXME: imaginary types not supported yet!");
181958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
18238d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // See if there are any attributes on the declspec that apply to the type (as
18338d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // opposed to the decl).
184fca0ddd42965e0b7ae821213486d4e0dd71fb439Chris Lattner  if (const AttributeList *AL = DS.getAttributes())
185c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(Result, AL);
186f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
18796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
18896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
18996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
19096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
19196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
19296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
19396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (TypeQuals & QualType::Restrict) {
194bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
195bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        QualType EltTy = PT->getPointeeType();
196bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner
197bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // If we have a pointer or reference, the pointee must have an object or
198bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
199bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
200bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          Diag(DS.getRestrictSpecLoc(),
201d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
202d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
203bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
204bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
205bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
20696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Diag(DS.getRestrictSpecLoc(),
207d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner             diag::err_typecheck_invalid_restrict_not_pointer)
208d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
209bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
21096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
21196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
21296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
21396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
21496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
21596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
21696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
21796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
21896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
21996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if (TypeQuals & QualType::Const)
22096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
22196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      else {
22296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        assert((TypeQuals & QualType::Volatile) &&
22396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner               "Has CV quals but not C or V?");
22496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
22596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
226d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, diag::warn_typecheck_function_qualifiers)
227d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
22896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
22996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
230f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
231f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
232f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
233f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
234f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
2351a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
2361a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
237f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
238f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Const;
239f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Volatile;
240f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    }
241f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor
24296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Result.getQualifiedType(TypeQuals);
24396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
244f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
245f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
246f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
24798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
24898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// declarator to Type instances. Skip the outermost Skip type
24998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// objects.
250cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlQualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
25198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  bool OmittedReturnType = false;
25298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
25398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  if (D.getContext() == Declarator::BlockLiteralContext
25498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && Skip == 0
25598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && !D.getDeclSpec().hasTypeSpecifier()
25698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && (D.getNumTypeObjects() == 0
25798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump          || (D.getNumTypeObjects() == 1
25898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump              && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
25998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    OmittedReturnType = true;
26098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
261b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner  // long long is a C99 feature.
262d1eb332181f02a7e0a6f9749265e6a0f55555cd4Chris Lattner  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
263b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner      D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
264b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner    Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
265930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
266930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
267930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
268930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
269930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  switch (D.getKind()) {
270930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Abstract:
271930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Normal:
27298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  case Declarator::DK_Operator: {
27398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    const DeclSpec& DS = D.getDeclSpec();
27498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    if (OmittedReturnType)
27598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // We default to a dependent type initially.  Can be modified by
27698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // the first return statement.
27798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      T = Context.DependentTy;
27898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    else
27998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      T = ConvertDeclSpecToType(DS);
280930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
28198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  }
282930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
283930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Constructor:
284930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Destructor:
285930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Conversion:
286930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
287930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // "void" instead. Conversion operators will check their return
288930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // types separately.
289930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
290930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
291930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
2924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
29398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
29498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
29598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
296cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
297cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
3005618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
3015618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      if (DeclType.Cls.TypeQuals)
3025618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
3035618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      if (!T.getTypePtr()->isFunctionType())
3045618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
3055618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      else
3065618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        T = Context.getBlockPointerType(T);
3075618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
30902c642e8f40562fcc9d440977f232c021b9d4353Chris Lattner      if (T->isReferenceType()) {
3105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C++ 8.3.2p4: There shall be no ... pointers to references ...
311d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
312d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner         << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
313e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
3145265af5b555f703be365dbc32c4e518fe38d4d9aChris Lattner        T = Context.IntTy;
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
3165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
31796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
31896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // object or incomplete types shall not be restrict-qualified."
31996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
320d805bec0fbb98aa10abbb41bfdcb2e2fab1bac96Chris Lattner          !T->isIncompleteOrObjectType()) {
321d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
322d162584991885ab004a02573a73ce06422b921fcChris Lattner          << T;
323f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        DeclType.Ptr.TypeQuals &= ~QualType::Restrict;
324f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
325f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
3265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Apply the pointer typequals to the pointer object.
3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
3285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
329f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    case DeclaratorChunk::Reference: {
330f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      // Whether we should suppress the creation of the reference.
331f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      bool SuppressReference = false;
332f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      if (T->isReferenceType()) {
333f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // C++ [dcl.ref]p4: There shall be no references to references.
334f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //
335f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // According to C++ DR 106, references to references are only
336f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // diagnosed when they are written directly (e.g., "int & &"),
337f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // but not when they happen via a typedef:
338f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //
339f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //   typedef int& intref;
340f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //   typedef intref& intref2;
341f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //
342f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // Parser::ParserDeclaratorInternal diagnoses the case where
343f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // references are written directly; here, we handle the
344f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // collapsing of references-to-references as described in C++
345f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // DR 106 and amended by C++ DR 540.
346f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        SuppressReference = true;
347f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      }
348f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor
349f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      // C++ [dcl.ref]p1:
350f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      //   A declarator that specifies the type “reference to cv void”
351f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      //   is ill-formed.
352f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      if (T->isVoidType()) {
353f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        Diag(DeclType.Loc, diag::err_reference_to_void);
354e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
355f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        T = Context.IntTy;
3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
35896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
35996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // object or incomplete types shall not be restrict-qualified."
36096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if (DeclType.Ref.HasRestrict &&
361d805bec0fbb98aa10abbb41bfdcb2e2fab1bac96Chris Lattner          !T->isIncompleteOrObjectType()) {
362d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
363d162584991885ab004a02573a73ce06422b921fcChris Lattner          << T;
36496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        DeclType.Ref.HasRestrict = false;
36596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
36696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
367f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      if (!SuppressReference)
368f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        T = Context.getReferenceType(T);
36996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
37096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Handle restrict on references.
37196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if (DeclType.Ref.HasRestrict)
37296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        T.addRestrict();
3735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
374f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    }
3755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
376fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
37794f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
3785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
3805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
3815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
3825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
3855265af5b555f703be365dbc32c4e518fe38d4d9aChris Lattner
3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // C99 6.7.5.2p1: If the element type is an incomplete or function type,
3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
3884ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor      if (DiagnoseIncompleteType(D.getIdentifierLoc(), T,
3894ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                                 diag::err_illegal_decl_array_incomplete_type)) {
390e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        T = Context.IntTy;
391e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
3925265af5b555f703be365dbc32c4e518fe38d4d9aChris Lattner      } else if (T->isFunctionType()) {
393d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions)
394d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
395e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        T = Context.getPointerType(T);
396e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
397a1d9fdea79ba7bbd71862b9f9f78f5f117331fc7Chris Lattner      } else if (const ReferenceType *RT = T->getAsReferenceType()) {
3985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C++ 8.3.2p4: There shall be no ... arrays of references ...
399d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references)
400d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
401bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        T = RT->getPointeeType();
402e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
40302c642e8f40562fcc9d440977f232c021b9d4353Chris Lattner      } else if (const RecordType *EltTy = T->getAsRecordType()) {
4045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // If the element type is a struct or union that contains a variadic
4050bfe54fdc83b7b4e37c40e652d86d15aa89885b2Douglas Gregor        // array, accept it as a GNU extension: C99 6.7.2.1p2.
4060bfe54fdc83b7b4e37c40e652d86d15aa89885b2Douglas Gregor        if (EltTy->getDecl()->hasFlexibleArrayMember())
4070bfe54fdc83b7b4e37c40e652d86d15aa89885b2Douglas Gregor          Diag(DeclType.Loc, diag::ext_flexible_array_in_array) << T;
40843477ca46792311640cf29b7cff731e29bebb146Chris Lattner      } else if (T->isObjCInterfaceType()) {
409d162584991885ab004a02573a73ce06422b921fcChris Lattner        Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T;
4105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
41143477ca46792311640cf29b7cff731e29bebb146Chris Lattner
41242471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff      // C99 6.7.5.2p1: The size expression shall have integer type.
41342471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff      if (ArraySize && !ArraySize->getType()->isIntegerType()) {
414d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
415d162584991885ab004a02573a73ce06422b921fcChris Lattner          << ArraySize->getType() << ArraySize->getSourceRange();
41642471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        D.setInvalidType(true);
417169a2664a64b57a815b5f0b39276a0891663921aTed Kremenek        ArraySize->Destroy(Context);
418fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner        ATI.NumElts = ArraySize = 0;
41942471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff      }
420c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff      llvm::APSInt ConstVal(32);
421c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      if (!ArraySize) {
422c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman        T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
423898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      } else if (ArraySize->isValueDependent()) {
424898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals);
42537148aabe7d153ce682b5715a820a11c0bbfcd59Eli Friedman      } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
426cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                 !T->isConstantSizeType()) {
42737148aabe7d153ce682b5715a820a11c0bbfcd59Eli Friedman        // Per C99, a variable array is an array with either a non-constant
42837148aabe7d153ce682b5715a820a11c0bbfcd59Eli Friedman        // size or an element type that has a non-constant-size
429c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff        T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
430c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      } else {
43142471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        // C99 6.7.5.2p1: If the expression is a constant expression, it shall
43242471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        // have a value greater than zero.
43342471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        if (ConstVal.isSigned()) {
43442471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff          if (ConstVal.isNegative()) {
435dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner            Diag(ArraySize->getLocStart(),
436dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                 diag::err_typecheck_negative_array_size)
437dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner              << ArraySize->getSourceRange();
43842471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff            D.setInvalidType(true);
43942471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff          } else if (ConstVal == 0) {
44042471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff            // GCC accepts zero sized static arrays.
441dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner            Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
442dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner              << ArraySize->getSourceRange();
44342471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff          }
44442471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        }
445c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff        T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
44642471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff      }
44794f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      // If this is not C99, extwarn about VLA's and C99 array size modifiers.
448a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner      if (!getLangOptions().C99) {
449a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner        if (ArraySize && !ArraySize->isValueDependent() &&
450a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner            !ArraySize->isIntegerConstantExpr(Context))
451a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner          Diag(D.getIdentifierLoc(), diag::ext_vla);
452a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner        else if (ASM != ArrayType::Normal || ATI.TypeQuals != 0)
453a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner          Diag(D.getIdentifierLoc(), diag::ext_c99_array_usage);
454a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner      }
4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
4565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
457f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
4585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
4595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
4615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
46268cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner
463cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      // C99 6.7.5.3p1: The return type may not be a function or array type.
46468cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner      if (T->isArrayType() || T->isFunctionType()) {
465d162584991885ab004a02573a73ce06422b921fcChris Lattner        Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
466cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        T = Context.IntTy;
467cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        D.setInvalidType(true);
468cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      }
469cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner
470eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      if (FTI.NumArgs == 0) {
471c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        if (getLangOptions().CPlusPlus) {
472c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
473c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // function takes no arguments.
474971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
475c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        } else {
476c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // Simple void foo(), where the incoming T is the result type.
477c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          T = Context.getFunctionTypeNoProto(T);
478c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
479eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      } else if (FTI.ArgInfo[0].Param == 0) {
4805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
481eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman        Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
4825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else {
4835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
4845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
4855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
4865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
4888123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
4898123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
49078c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
49108d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
49208d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
49308d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // This matches the conversion that is done in
494bff5f5cfd194c3cc096ba89360fb537814a9666aNate Begeman          // Sema::ActOnParamDeclarator(). Without this conversion, the
49508d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // argument type in the function prototype *will not* match the
49608d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // type in ParmVarDecl (which makes the code generator unhappy).
49708d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
49808d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // FIXME: We still apparently need the conversion in
499e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner          // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
50008d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // it should be driving off the type being created here.
50108d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
50208d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // FIXME: If a source translation tool needs to see the original type,
50308d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // then we need to consider storing both types somewhere...
50408d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
505e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner          if (ArgTy->isArrayType()) {
506e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner            ArgTy = Context.getArrayDecayedType(ArgTy);
507529bd02affa96a311dd9ab131f2ab4d833017fb7Chris Lattner          } else if (ArgTy->isFunctionType())
50808d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff            ArgTy = Context.getPointerType(ArgTy);
509e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
5105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
5115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
5125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // int(void) as a FunctionTypeProto with an empty argument list.
51308d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          else if (ArgTy->isVoidType()) {
5145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
5155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
5165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
5175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
5185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
5192ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
5208123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
5212ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
5222ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
5235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
5244565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
5252ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
5268123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
5272ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
5282ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
529f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner              if (ArgTy.getCVRQualifiers())
5302ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
5312ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner
5322ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
5332ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
5342ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
535eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
536eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
537eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              ArgTy = Context.IntTy;
538eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
539eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
540eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
541eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
5425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
5435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ArgTys.push_back(ArgTy);
5455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
5465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
547971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis                                    FTI.isVariadic, FTI.TypeQuals);
5485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
5495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
5505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
551f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
552f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
553f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      DeclContext *DC = static_cast<DeclContext*>(
554f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        DeclType.Mem.Scope().getScopeRep());
555f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
556f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // FIXME: Extend for dependent types when it's actually supported.
557f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // See ActOnCXXNestedNameSpecifier.
558f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
559f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.getTagDeclType(RD);
560f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
561f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        if (DC) {
562f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          Diag(DeclType.Mem.Scope().getBeginLoc(),
563f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl               diag::err_illegal_decl_mempointer_in_nonclass)
564f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl            << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
565f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl            << DeclType.Mem.Scope().getRange();
566f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        }
567f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
568f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.IntTy;
569f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
570f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
571f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
572f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      //   with reference type, or "cv void."
573f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (T->isReferenceType()) {
574f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
575f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
576f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
577f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
578f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
579f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (T->isVoidType()) {
580f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
581f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
582f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
583f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
584f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
585f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
586f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // object or incomplete types shall not be restrict-qualified."
587f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
588f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          !T->isIncompleteOrObjectType()) {
589f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
590f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << T;
591f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        DeclType.Mem.TypeQuals &= ~QualType::Restrict;
592f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
593f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
5944433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl      T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
5954433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl                    getQualifiedType(DeclType.Mem.TypeQuals);
596f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
597f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
598f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
599f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
600c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
601c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    if (const AttributeList *AL = DeclType.getAttrs())
602c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      ProcessTypeAttributeList(T, AL);
6035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
604971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
605971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
606971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    const FunctionTypeProto *FnTy = T->getAsFunctionTypeProto();
607971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    assert(FnTy && "Why oh why is there not a FunctionTypeProto here ?");
608971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
609971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
610971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // for a nonstatic member function, the function type to which a pointer
611971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // to member refers, or the top-level function type of a function typedef
612971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // declaration.
613971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    if (FnTy->getTypeQuals() != 0 &&
614971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
615584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor        ((D.getContext() != Declarator::MemberContext &&
616584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor          (!D.getCXXScopeSpec().isSet() ||
617584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor           !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
618bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor              ->isRecord())) ||
619971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
620971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      if (D.isFunctionDeclarator())
621971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
622971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      else
623971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(),
624971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis             diag::err_invalid_qualified_typedef_function_type_use);
625971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
626971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      // Strip the cv-quals from the type.
627971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
6287fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                                  FnTy->getNumArgs(), FnTy->isVariadic(), 0);
629971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
630971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
6315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6320bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // If there were any type attributes applied to the decl itself (not the
6330bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // type, apply the type attribute to the type!)
6340bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  if (const AttributeList *Attrs = D.getAttributes())
635c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(T, Attrs);
6360bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner
6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T;
6385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
640a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
641360300ca2ee298d585d29baf006519507d968812Fariborz Jahanian/// declarator
642a526c5c67e5a0473c340903ee542ce570119665fTed KremenekQualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
643a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
644306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  QualType T = MDecl->getResultType();
645306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  llvm::SmallVector<QualType, 16> ArgTys;
646306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
6473560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian  // Add the first two invisible argument types for self and _cmd.
648f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  if (MDecl->isInstanceMethod()) {
649a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
6501f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    selfTy = Context.getPointerType(selfTy);
6511f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    ArgTys.push_back(selfTy);
6521f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian  }
6533560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian  else
654a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ArgTys.push_back(Context.getObjCIdType());
655a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ArgTys.push_back(Context.getObjCSelType());
6563560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian
65758cce3b0dcbdcc95b7e713795834b4cb2c8a008aChris Lattner  for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
658306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    ParmVarDecl *PDecl = MDecl->getParamDecl(i);
659306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    QualType ArgTy = PDecl->getType();
660306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    assert(!ArgTy.isNull() && "Couldn't parse type?");
661306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
662306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    // This matches the conversion that is done in
663e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // Sema::ActOnParamDeclarator().
664e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    if (ArgTy->isArrayType())
665e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner      ArgTy = Context.getArrayDecayedType(ArgTy);
666306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    else if (ArgTy->isFunctionType())
667306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian      ArgTy = Context.getPointerType(ArgTy);
668306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    ArgTys.push_back(ArgTy);
669306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  }
670306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
6717fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                              MDecl->isVariadic(), 0);
672306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  return T;
673306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian}
674306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
6759e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
6769e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
6779e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// they point to and return true. If T1 and T2 aren't pointer types
6789e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// or pointer-to-member types, or if they are not similar at this
6799e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// level, returns false and leaves T1 and T2 unchanged. Top-level
6809e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// qualifiers on T1 and T2 are ignored. This function will typically
6819e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// be called in a loop that successively "unwraps" pointer and
6829e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// pointer-to-member types to compare them at each level.
68357373266011f73418381b736015d8d2bb0381176Douglas Gregorbool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2)
68457373266011f73418381b736015d8d2bb0381176Douglas Gregor{
68557373266011f73418381b736015d8d2bb0381176Douglas Gregor  const PointerType *T1PtrType = T1->getAsPointerType(),
68657373266011f73418381b736015d8d2bb0381176Douglas Gregor                    *T2PtrType = T2->getAsPointerType();
68757373266011f73418381b736015d8d2bb0381176Douglas Gregor  if (T1PtrType && T2PtrType) {
68857373266011f73418381b736015d8d2bb0381176Douglas Gregor    T1 = T1PtrType->getPointeeType();
68957373266011f73418381b736015d8d2bb0381176Douglas Gregor    T2 = T2PtrType->getPointeeType();
69057373266011f73418381b736015d8d2bb0381176Douglas Gregor    return true;
69157373266011f73418381b736015d8d2bb0381176Douglas Gregor  }
69257373266011f73418381b736015d8d2bb0381176Douglas Gregor
6934433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
6944433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl                          *T2MPType = T2->getAsMemberPointerType();
69521593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl  if (T1MPType && T2MPType &&
69621593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T1MPType->getClass()) ==
69721593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T2MPType->getClass())) {
6984433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T1 = T1MPType->getPointeeType();
6994433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T2 = T2MPType->getPointeeType();
7004433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    return true;
7014433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  }
70257373266011f73418381b736015d8d2bb0381176Douglas Gregor  return false;
70357373266011f73418381b736015d8d2bb0381176Douglas Gregor}
70457373266011f73418381b736015d8d2bb0381176Douglas Gregor
705cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlSema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
7065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
7075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
7085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
7095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
710cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  QualType T = GetTypeForDeclarator(D, S);
7115912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
7125912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff  assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
7135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7146d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor  // Check that there are no default arguments (C++ only).
7156d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor  if (getLangOptions().CPlusPlus)
7166d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
7176d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
7185912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff  // In this context, we *do not* check D.getInvalidType(). If the declarator
7195912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff  // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
7205912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff  // though it will not reflect the user specified type.
7215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T.getAsOpaquePtr();
7225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
724c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
725c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
726c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
727c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
728c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
729232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
730232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
731c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
732c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
733c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnerstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
734c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
735232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
736232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
737232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
738232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
739c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
740c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
741232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
742232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
743232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
744545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
745f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
746c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
747232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
748545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
749232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
750c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
751dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
752dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
753c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
754232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
755232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
756232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
757c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  Type = S.Context.getASQualType(Type, ASIdx);
758c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
759c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
760c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnervoid Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
761c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
762c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
763c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
764c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
765c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  for (; AL; AL = AL->getNext()) {
766c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // If this is an attribute we can handle, do so now, otherwise, add it to
767c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // the LeftOverAttrs list for rechaining.
768c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    switch (AL->getKind()) {
769c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
770c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
771c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      HandleAddressSpaceTypeAttribute(Result, *AL, *this);
772c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
773c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
774c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  }
775232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
776232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
7774ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @brief If the type T is incomplete and cannot be completed,
7784ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// produce a suitable diagnostic.
7794ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
7804ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
7814ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
7824ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// type, returns false. If @p T is incomplete, issues the diagnostic
7834ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @p diag (giving it the type @p T) and returns true.
7844ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
7854ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
7864ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
7874ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
7884ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
7894ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
7904ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param diag The diagnostic value (e.g.,
7914ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c diag::err_typecheck_decl_incomplete_type) that will be used
7924ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// for the error message if @p T is incomplete.
7934ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
7944ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range1  An optional range in the source code that will be a
7954ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
7964ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
7974ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range2  An optional range in the source code that will be a
7984ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
7994ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8004ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param PrintType If non-NULL, the type that should be printed
8014ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// instead of @p T. This parameter should be used when the type that
8024ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// we're checking for incompleteness isn't the type that should be
8034ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// displayed to the user, e.g., when T is a type and PrintType is a
8044ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// pointer to T.
8054ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8064ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
8074ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
8084ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8094ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @todo When Clang gets proper support for C++ templates, this
8104ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// routine will also be able perform template instantiation when @p T
8114ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// is a class template specialization.
8124ec339f43c0cae2678334850c90926bea10999c7Douglas Gregorbool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag,
8134ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                                  SourceRange Range1, SourceRange Range2,
8144ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                                  QualType PrintType) {
8154ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
8164ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
8174ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
8184ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
8194ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (PrintType.isNull())
8204ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    PrintType = T;
8214ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
8224ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
8234ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  Diag(Loc, diag) << PrintType << Range1 << Range2;
8243c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
8254ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
8264ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // type, produce
8274ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  const TagType *Tag = 0;
8284ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (const RecordType *Record = T->getAsRecordType())
8294ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Record;
8304ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  else if (const EnumType *Enum = T->getAsEnumType())
8314ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Enum;
8324ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
8334ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
8344ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Diag(Tag->getDecl()->getLocation(),
8354ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
8364ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
8374ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
8384ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
8394ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
8404ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
841