SemaType.cpp revision 4262a07621043c19292f5fd90b1e426d65cd366c
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) {
77d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
78d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner                                       DeclSpec::PQ_TypeSpecifier |
79d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner                                       DeclSpec::PQ_TypeQualifier)) == 0)
80d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner        Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec);
814310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
82d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
83d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
84d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
85d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
864310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
874310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      unsigned DK = getLangOptions().CPlusPlus && !getLangOptions().Microsoft?
884310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor          diag::err_missing_type_specifier
894310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor        : diag::ext_missing_type_specifier;
904310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      Diag(DS.getSourceRange().getBegin(), DK);
91d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
92d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner
93d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // FALL THROUGH.
943cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
97fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
98fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
99fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
100fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_longlong:    Result = Context.LongLongTy; break;
1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
104fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
105fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
106fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
107fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_longlong:    Result =Context.UnsignedLongLongTy; break;
1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
110958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1113cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
112fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
113958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
114958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
115fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
116958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
117fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
118958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
119fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
1205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
1215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(0 && "FIXME: GNU decimal extensions not supported yet!");
12499dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Decl *D = static_cast<Decl *>(DS.getTypeRep());
12999dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner    assert(D && "Didn't get a decl for a class/enum/union/struct?");
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
1342ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    Result = Context.getTypeDeclType(cast<TypeDecl>(D));
135958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1371a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
1411a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
1422ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
1431a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
1441a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
1451a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      // we have this "hack" for now...
1461a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
1471a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor        Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
1481a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor                                                       (ObjCProtocolDecl**)PQ,
1491a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor                                               DS.getNumProtocolQualifiers());
1501a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor      else if (Result == Context.getObjCIdType())
151ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
152ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
153ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner                                                DS.getNumProtocolQualifiers());
1544262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff      else if (Result == Context.getObjCClassType())
1554262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
1568dfb0c57ddb700b163afa89e3ab160f1de26753dSteve Naroff        Diag(DS.getSourceRange().getBegin(),
1574262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff             diag::err_qualified_class_unsupported) << DS.getSourceRange();
1584262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff      else
1594262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        Diag(DS.getSourceRange().getBegin(),
1604262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff             diag::err_invalid_protocol_qualifiers) << DS.getSourceRange();
161c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
163958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
165958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
166958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
167958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
168d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
169fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
170958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
171d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
172d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    Expr *E = static_cast<Expr *>(DS.getTypeRep());
173d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
174d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
175fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfExpr(E);
176958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
177d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
178809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
179809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return QualType();
1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
181958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
182958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
183f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
184f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor    if (getLangOptions().Freestanding)
185f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor      Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
186fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
187f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
188958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
189958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
190958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner         "FIXME: imaginary types not supported yet!");
191958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
19238d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // See if there are any attributes on the declspec that apply to the type (as
19338d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // opposed to the decl).
194fca0ddd42965e0b7ae821213486d4e0dd71fb439Chris Lattner  if (const AttributeList *AL = DS.getAttributes())
195c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(Result, AL);
196f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
19796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
19896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
19996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
20096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
20196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
20296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
20396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (TypeQuals & QualType::Restrict) {
204bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
205bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        QualType EltTy = PT->getPointeeType();
206bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner
207bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // If we have a pointer or reference, the pointee must have an object or
208bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
209bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
210bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          Diag(DS.getRestrictSpecLoc(),
211d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
212d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
213bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
214bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
215bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
21696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Diag(DS.getRestrictSpecLoc(),
217d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner             diag::err_typecheck_invalid_restrict_not_pointer)
218d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
219bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
22096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
22196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
22296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
22396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
22496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
22596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
22696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
22796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
22896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
22996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if (TypeQuals & QualType::Const)
23096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
23196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      else {
23296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        assert((TypeQuals & QualType::Volatile) &&
23396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner               "Has CV quals but not C or V?");
23496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
23596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
236d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, diag::warn_typecheck_function_qualifiers)
237d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
23896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
23996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
240f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
241f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
242f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
243f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
244f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
2451a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
2461a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
247f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
248f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Const;
249f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Volatile;
250f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    }
251f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor
25296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Result.getQualifiedType(TypeQuals);
25396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
254f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
255f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
256f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
25798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
25898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// declarator to Type instances. Skip the outermost Skip type
25998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// objects.
260cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlQualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
26198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  bool OmittedReturnType = false;
26298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
26398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  if (D.getContext() == Declarator::BlockLiteralContext
26498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && Skip == 0
26598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && !D.getDeclSpec().hasTypeSpecifier()
26698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && (D.getNumTypeObjects() == 0
26798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump          || (D.getNumTypeObjects() == 1
26898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump              && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
26998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    OmittedReturnType = true;
27098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
271b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner  // long long is a C99 feature.
272d1eb332181f02a7e0a6f9749265e6a0f55555cd4Chris Lattner  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
273b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner      D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
274b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner    Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
275930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
276930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
277930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
278930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
279930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  switch (D.getKind()) {
280930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Abstract:
281930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Normal:
28298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  case Declarator::DK_Operator: {
28398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    const DeclSpec& DS = D.getDeclSpec();
28498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    if (OmittedReturnType)
28598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // We default to a dependent type initially.  Can be modified by
28698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // the first return statement.
28798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      T = Context.DependentTy;
288809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    else {
28998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      T = ConvertDeclSpecToType(DS);
290809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor      if (T.isNull())
291809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor        return T;
292809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    }
293930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
29498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  }
295930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
296930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Constructor:
297930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Destructor:
298930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Conversion:
299930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
300930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // "void" instead. Conversion operators will check their return
301930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // types separately.
302930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
303930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
304930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
3054c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
30698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
30798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
30898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
309cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
310cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
3115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
3125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
3135618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
3145618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      if (DeclType.Cls.TypeQuals)
3155618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
3165618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      if (!T.getTypePtr()->isFunctionType())
3175618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
3185618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      else
3195618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        T = Context.getBlockPointerType(T);
3205618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
3215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
32202c642e8f40562fcc9d440977f232c021b9d4353Chris Lattner      if (T->isReferenceType()) {
3235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C++ 8.3.2p4: There shall be no ... pointers to references ...
324d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
325d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner         << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
326e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
3275265af5b555f703be365dbc32c4e518fe38d4d9aChris Lattner        T = Context.IntTy;
3285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
3295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
33096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
33196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // object or incomplete types shall not be restrict-qualified."
33296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
333d805bec0fbb98aa10abbb41bfdcb2e2fab1bac96Chris Lattner          !T->isIncompleteOrObjectType()) {
334d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
335d162584991885ab004a02573a73ce06422b921fcChris Lattner          << T;
336f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        DeclType.Ptr.TypeQuals &= ~QualType::Restrict;
337f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
338f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Apply the pointer typequals to the pointer object.
3405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
342f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    case DeclaratorChunk::Reference: {
343f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      // Whether we should suppress the creation of the reference.
344f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      bool SuppressReference = false;
345f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      if (T->isReferenceType()) {
346f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // C++ [dcl.ref]p4: There shall be no references to references.
347f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //
348f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // According to C++ DR 106, references to references are only
349f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // diagnosed when they are written directly (e.g., "int & &"),
350f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // but not when they happen via a typedef:
351f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //
352f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //   typedef int& intref;
353f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //   typedef intref& intref2;
354f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //
355f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // Parser::ParserDeclaratorInternal diagnoses the case where
356f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // references are written directly; here, we handle the
357f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // collapsing of references-to-references as described in C++
358f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // DR 106 and amended by C++ DR 540.
359f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        SuppressReference = true;
360f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      }
361f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor
362f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      // C++ [dcl.ref]p1:
363f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      //   A declarator that specifies the type “reference to cv void”
364f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      //   is ill-formed.
365f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      if (T->isVoidType()) {
366f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        Diag(DeclType.Loc, diag::err_reference_to_void);
367e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
368f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        T = Context.IntTy;
3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
3705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
37196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
37296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // object or incomplete types shall not be restrict-qualified."
37396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if (DeclType.Ref.HasRestrict &&
374d805bec0fbb98aa10abbb41bfdcb2e2fab1bac96Chris Lattner          !T->isIncompleteOrObjectType()) {
375d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
376d162584991885ab004a02573a73ce06422b921fcChris Lattner          << T;
37796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        DeclType.Ref.HasRestrict = false;
37896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
37996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
380f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      if (!SuppressReference)
381f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        T = Context.getReferenceType(T);
38296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
38396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Handle restrict on references.
38496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if (DeclType.Ref.HasRestrict)
38596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        T.addRestrict();
3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
387f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    }
3885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
389fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
39094f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
3915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
3925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
3945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
3965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
3975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
3985265af5b555f703be365dbc32c4e518fe38d4d9aChris Lattner
3995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // C99 6.7.5.2p1: If the element type is an incomplete or function type,
4005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
4014ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor      if (DiagnoseIncompleteType(D.getIdentifierLoc(), T,
4024ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                                 diag::err_illegal_decl_array_incomplete_type)) {
403e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        T = Context.IntTy;
404e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
4055265af5b555f703be365dbc32c4e518fe38d4d9aChris Lattner      } else if (T->isFunctionType()) {
406d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions)
407d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
408e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        T = Context.getPointerType(T);
409e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
410a1d9fdea79ba7bbd71862b9f9f78f5f117331fc7Chris Lattner      } else if (const ReferenceType *RT = T->getAsReferenceType()) {
4115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C++ 8.3.2p4: There shall be no ... arrays of references ...
412d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references)
413d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
414bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        T = RT->getPointeeType();
415e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
41602c642e8f40562fcc9d440977f232c021b9d4353Chris Lattner      } else if (const RecordType *EltTy = T->getAsRecordType()) {
4175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // If the element type is a struct or union that contains a variadic
4180bfe54fdc83b7b4e37c40e652d86d15aa89885b2Douglas Gregor        // array, accept it as a GNU extension: C99 6.7.2.1p2.
4190bfe54fdc83b7b4e37c40e652d86d15aa89885b2Douglas Gregor        if (EltTy->getDecl()->hasFlexibleArrayMember())
4200bfe54fdc83b7b4e37c40e652d86d15aa89885b2Douglas Gregor          Diag(DeclType.Loc, diag::ext_flexible_array_in_array) << T;
42143477ca46792311640cf29b7cff731e29bebb146Chris Lattner      } else if (T->isObjCInterfaceType()) {
422d162584991885ab004a02573a73ce06422b921fcChris Lattner        Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T;
4235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
42443477ca46792311640cf29b7cff731e29bebb146Chris Lattner
42542471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff      // C99 6.7.5.2p1: The size expression shall have integer type.
42642471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff      if (ArraySize && !ArraySize->getType()->isIntegerType()) {
427d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
428d162584991885ab004a02573a73ce06422b921fcChris Lattner          << ArraySize->getType() << ArraySize->getSourceRange();
42942471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        D.setInvalidType(true);
430169a2664a64b57a815b5f0b39276a0891663921aTed Kremenek        ArraySize->Destroy(Context);
431fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner        ATI.NumElts = ArraySize = 0;
43242471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff      }
4331ca4813ddae54deead43252fe2f2d79fa5b7ad48Eli Friedman      llvm::APSInt ConstVal(32);
434c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      if (!ArraySize) {
435c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman        T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
436898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      } else if (ArraySize->isValueDependent()) {
437898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals);
4381ca4813ddae54deead43252fe2f2d79fa5b7ad48Eli Friedman      } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
439cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                 !T->isConstantSizeType()) {
44037148aabe7d153ce682b5715a820a11c0bbfcd59Eli Friedman        // Per C99, a variable array is an array with either a non-constant
44137148aabe7d153ce682b5715a820a11c0bbfcd59Eli Friedman        // size or an element type that has a non-constant-size
442c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff        T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
443c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      } else {
44442471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        // C99 6.7.5.2p1: If the expression is a constant expression, it shall
44542471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        // have a value greater than zero.
44642471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        if (ConstVal.isSigned()) {
44742471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff          if (ConstVal.isNegative()) {
448dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner            Diag(ArraySize->getLocStart(),
449dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                 diag::err_typecheck_negative_array_size)
450dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner              << ArraySize->getSourceRange();
45142471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff            D.setInvalidType(true);
45242471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff          } else if (ConstVal == 0) {
45342471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff            // GCC accepts zero sized static arrays.
4541ca4813ddae54deead43252fe2f2d79fa5b7ad48Eli Friedman            Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
455dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner              << ArraySize->getSourceRange();
45642471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff          }
45742471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        }
458c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff        T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
45942471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff      }
46094f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      // If this is not C99, extwarn about VLA's and C99 array size modifiers.
461a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner      if (!getLangOptions().C99) {
462a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner        if (ArraySize && !ArraySize->isValueDependent() &&
463a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner            !ArraySize->isIntegerConstantExpr(Context))
464a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner          Diag(D.getIdentifierLoc(), diag::ext_vla);
465a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner        else if (ASM != ArrayType::Normal || ATI.TypeQuals != 0)
466a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner          Diag(D.getIdentifierLoc(), diag::ext_c99_array_usage);
467a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner      }
4685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
4695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
470f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
4715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
4725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
4735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
4745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
47568cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner
476cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      // C99 6.7.5.3p1: The return type may not be a function or array type.
47768cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner      if (T->isArrayType() || T->isFunctionType()) {
478d162584991885ab004a02573a73ce06422b921fcChris Lattner        Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
479cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        T = Context.IntTy;
480cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        D.setInvalidType(true);
481cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      }
482cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner
483eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      if (FTI.NumArgs == 0) {
484c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        if (getLangOptions().CPlusPlus) {
485c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
486c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // function takes no arguments.
487971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
488965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor        } else if (FTI.isVariadic) {
489965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // We allow a zero-parameter variadic function in C if the
490965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // function is marked with the "overloadable"
491965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // attribute. Scan for this attribute now.
492965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
493965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
494965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
495965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
496965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
497965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
498965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
499965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
500965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
501965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
502965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
503965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
504c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        } else {
505c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // Simple void foo(), where the incoming T is the result type.
506c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          T = Context.getFunctionTypeNoProto(T);
507c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
508eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      } else if (FTI.ArgInfo[0].Param == 0) {
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
510eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman        Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
5115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else {
5125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
5135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
5145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
5155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
5178123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
5188123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
51978c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
52008d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
52108d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
52208d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // This matches the conversion that is done in
523bff5f5cfd194c3cc096ba89360fb537814a9666aNate Begeman          // Sema::ActOnParamDeclarator(). Without this conversion, the
52408d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // argument type in the function prototype *will not* match the
52508d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // type in ParmVarDecl (which makes the code generator unhappy).
52608d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
52708d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // FIXME: We still apparently need the conversion in
528e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner          // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
52908d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // it should be driving off the type being created here.
53008d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
53108d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // FIXME: If a source translation tool needs to see the original type,
53208d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // then we need to consider storing both types somewhere...
53308d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
534e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner          if (ArgTy->isArrayType()) {
535e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner            ArgTy = Context.getArrayDecayedType(ArgTy);
536529bd02affa96a311dd9ab131f2ab4d833017fb7Chris Lattner          } else if (ArgTy->isFunctionType())
53708d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff            ArgTy = Context.getPointerType(ArgTy);
538e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
5395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
5405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
5415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // int(void) as a FunctionTypeProto with an empty argument list.
54208d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          else if (ArgTy->isVoidType()) {
5435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
5445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
5455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
5465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
5475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
5482ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
5498123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
5502ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
5512ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
5534565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
5542ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
5558123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
5562ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
5572ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
558f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner              if (ArgTy.getCVRQualifiers())
5592ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
5602ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner
5612ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
5622ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
5632ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
564eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
565eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
566eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              ArgTy = Context.IntTy;
567eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
568eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
569eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
570eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
5715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
5725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ArgTys.push_back(ArgTy);
5745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
5755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
576971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis                                    FTI.isVariadic, FTI.TypeQuals);
5775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
5785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
5795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
580f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
581f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
582f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      DeclContext *DC = static_cast<DeclContext*>(
583f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        DeclType.Mem.Scope().getScopeRep());
584f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
585f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // FIXME: Extend for dependent types when it's actually supported.
586f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // See ActOnCXXNestedNameSpecifier.
587f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
588f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.getTagDeclType(RD);
589f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
590f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        if (DC) {
591f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          Diag(DeclType.Mem.Scope().getBeginLoc(),
592f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl               diag::err_illegal_decl_mempointer_in_nonclass)
593f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl            << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
594f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl            << DeclType.Mem.Scope().getRange();
595f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        }
596f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
597f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.IntTy;
598f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
599f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
600f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
601f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      //   with reference type, or "cv void."
602f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (T->isReferenceType()) {
603f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
604f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
605f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
606f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
607f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
608f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (T->isVoidType()) {
609f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
610f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
611f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
612f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
613f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
614f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
615f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // object or incomplete types shall not be restrict-qualified."
616f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
617f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          !T->isIncompleteOrObjectType()) {
618f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
619f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << T;
620f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        DeclType.Mem.TypeQuals &= ~QualType::Restrict;
621f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
622f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
6234433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl      T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
6244433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl                    getQualifiedType(DeclType.Mem.TypeQuals);
625f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
626f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
627f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
628f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
629c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
630c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    if (const AttributeList *AL = DeclType.getAttrs())
631c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      ProcessTypeAttributeList(T, AL);
6325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
633971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
634971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
635971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    const FunctionTypeProto *FnTy = T->getAsFunctionTypeProto();
636971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    assert(FnTy && "Why oh why is there not a FunctionTypeProto here ?");
637971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
638971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
639971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // for a nonstatic member function, the function type to which a pointer
640971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // to member refers, or the top-level function type of a function typedef
641971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // declaration.
642971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    if (FnTy->getTypeQuals() != 0 &&
643971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
644584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor        ((D.getContext() != Declarator::MemberContext &&
645584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor          (!D.getCXXScopeSpec().isSet() ||
646584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor           !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
647bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor              ->isRecord())) ||
648971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
649971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      if (D.isFunctionDeclarator())
650971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
651971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      else
652971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(),
653971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis             diag::err_invalid_qualified_typedef_function_type_use);
654971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
655971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      // Strip the cv-quals from the type.
656971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
6577fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                                  FnTy->getNumArgs(), FnTy->isVariadic(), 0);
658971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
659971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
6605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6610bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // If there were any type attributes applied to the decl itself (not the
6620bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // type, apply the type attribute to the type!)
6630bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  if (const AttributeList *Attrs = D.getAttributes())
664c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(T, Attrs);
6650bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner
6665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T;
6675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
669a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
670360300ca2ee298d585d29baf006519507d968812Fariborz Jahanian/// declarator
671a526c5c67e5a0473c340903ee542ce570119665fTed KremenekQualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
67289951a86b594513c2a013532ed45d197413b1087Chris Lattner  ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(static_cast<Decl *>(D));
673306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  QualType T = MDecl->getResultType();
674306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  llvm::SmallVector<QualType, 16> ArgTys;
675306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
6763560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian  // Add the first two invisible argument types for self and _cmd.
677f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  if (MDecl->isInstanceMethod()) {
678a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
6791f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    selfTy = Context.getPointerType(selfTy);
6801f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    ArgTys.push_back(selfTy);
68189951a86b594513c2a013532ed45d197413b1087Chris Lattner  } else
682a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ArgTys.push_back(Context.getObjCIdType());
683a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ArgTys.push_back(Context.getObjCSelType());
6843560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian
68589951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
68689951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = MDecl->param_end(); PI != E; ++PI) {
68789951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType ArgTy = (*PI)->getType();
688306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    assert(!ArgTy.isNull() && "Couldn't parse type?");
689306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
690306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    // This matches the conversion that is done in
691e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // Sema::ActOnParamDeclarator().
692e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    if (ArgTy->isArrayType())
693e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner      ArgTy = Context.getArrayDecayedType(ArgTy);
694306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    else if (ArgTy->isFunctionType())
695306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian      ArgTy = Context.getPointerType(ArgTy);
696306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    ArgTys.push_back(ArgTy);
697306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  }
698306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
6997fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                              MDecl->isVariadic(), 0);
700306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  return T;
701306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian}
702306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
7039e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
7049e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
7059e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// they point to and return true. If T1 and T2 aren't pointer types
7069e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// or pointer-to-member types, or if they are not similar at this
7079e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// level, returns false and leaves T1 and T2 unchanged. Top-level
7089e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// qualifiers on T1 and T2 are ignored. This function will typically
7099e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// be called in a loop that successively "unwraps" pointer and
7109e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// pointer-to-member types to compare them at each level.
711ecb81f28cb279b7d8e84296445a4131fa80b69a9Chris Lattnerbool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
71257373266011f73418381b736015d8d2bb0381176Douglas Gregor  const PointerType *T1PtrType = T1->getAsPointerType(),
71357373266011f73418381b736015d8d2bb0381176Douglas Gregor                    *T2PtrType = T2->getAsPointerType();
71457373266011f73418381b736015d8d2bb0381176Douglas Gregor  if (T1PtrType && T2PtrType) {
71557373266011f73418381b736015d8d2bb0381176Douglas Gregor    T1 = T1PtrType->getPointeeType();
71657373266011f73418381b736015d8d2bb0381176Douglas Gregor    T2 = T2PtrType->getPointeeType();
71757373266011f73418381b736015d8d2bb0381176Douglas Gregor    return true;
71857373266011f73418381b736015d8d2bb0381176Douglas Gregor  }
71957373266011f73418381b736015d8d2bb0381176Douglas Gregor
7204433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
7214433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl                          *T2MPType = T2->getAsMemberPointerType();
72221593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl  if (T1MPType && T2MPType &&
72321593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T1MPType->getClass()) ==
72421593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T2MPType->getClass())) {
7254433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T1 = T1MPType->getPointeeType();
7264433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T2 = T2MPType->getPointeeType();
7274433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    return true;
7284433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  }
72957373266011f73418381b736015d8d2bb0381176Douglas Gregor  return false;
73057373266011f73418381b736015d8d2bb0381176Douglas Gregor}
73157373266011f73418381b736015d8d2bb0381176Douglas Gregor
732cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlSema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
7335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
7345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
7355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
7365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
737cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  QualType T = GetTypeForDeclarator(D, S);
738809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (T.isNull())
739809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
7405912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
7416d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor  // Check that there are no default arguments (C++ only).
7426d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor  if (getLangOptions().CPlusPlus)
7436d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
7446d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
7455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T.getAsOpaquePtr();
7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
748c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
749c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
750c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
751c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
752c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
753232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
754232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
755c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
756c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
757c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnerstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
758c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
759232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
760232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
761232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
762232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
763c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
764c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
765232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
766232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
767232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
768545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
769f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
770c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
771232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
772545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
773232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
774c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
775dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
776dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
777c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
778232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
779232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
780232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
781f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
782c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
783c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
784d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
785d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// specified type.  The attribute contains 1 argument, weak or strong.
786d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanianstatic void HandleObjCGCTypeAttribute(QualType &Type,
7873b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner                                      const AttributeList &Attr, Sema &S) {
788d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (Type.getObjCGCAttr() != QualType::GCNone) {
7895934e75d98d99374f72722a69c5eefe026f35c74Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
790d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
791d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
792d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
793d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
794ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (!Attr.getParameterName()) {
795ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
796ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
797ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    return;
798ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
7993b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  QualType::GCAttrTypes GCAttr;
800ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (Attr.getNumArgs() != 0) {
801d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
802d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
803d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
804d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (Attr.getParameterName()->isStr("weak"))
8053b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner    GCAttr = QualType::Weak;
806d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else if (Attr.getParameterName()->isStr("strong"))
8073b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner    GCAttr = QualType::Strong;
808d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
809d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
810d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      << "objc_gc" << Attr.getParameterName();
811d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
812d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
813d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
8143b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  Type = S.Context.getObjCGCQualType(Type, GCAttr);
815d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
816d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
817c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnervoid Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
818c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
819c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
820c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
821c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
822c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  for (; AL; AL = AL->getNext()) {
823c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // If this is an attribute we can handle, do so now, otherwise, add it to
824c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // the LeftOverAttrs list for rechaining.
825c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    switch (AL->getKind()) {
826c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
827c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
828c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      HandleAddressSpaceTypeAttribute(Result, *AL, *this);
829c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
830d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    case AttributeList::AT_objc_gc:
831d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      HandleObjCGCTypeAttribute(Result, *AL, *this);
832d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
833c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
834c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  }
835232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
836232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
8374ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @brief If the type T is incomplete and cannot be completed,
8384ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// produce a suitable diagnostic.
8394ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8404ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
8414ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
8424ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// type, returns false. If @p T is incomplete, issues the diagnostic
8434ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @p diag (giving it the type @p T) and returns true.
8444ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8454ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
8464ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
8474ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8484ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
8494ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8504ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param diag The diagnostic value (e.g.,
8514ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c diag::err_typecheck_decl_incomplete_type) that will be used
8524ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// for the error message if @p T is incomplete.
8534ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8544ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range1  An optional range in the source code that will be a
8554ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
8564ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8574ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range2  An optional range in the source code that will be a
8584ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
8594ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8604ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param PrintType If non-NULL, the type that should be printed
8614ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// instead of @p T. This parameter should be used when the type that
8624ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// we're checking for incompleteness isn't the type that should be
8634ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// displayed to the user, e.g., when T is a type and PrintType is a
8644ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// pointer to T.
8654ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8664ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
8674ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
8684ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8694ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @todo When Clang gets proper support for C++ templates, this
8704ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// routine will also be able perform template instantiation when @p T
8714ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// is a class template specialization.
8724ec339f43c0cae2678334850c90926bea10999c7Douglas Gregorbool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag,
8734ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                                  SourceRange Range1, SourceRange Range2,
8744ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                                  QualType PrintType) {
8754ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
8764ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
8774ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
8784ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
8794ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (PrintType.isNull())
8804ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    PrintType = T;
8814ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
8824ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
8834ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  Diag(Loc, diag) << PrintType << Range1 << Range2;
8843c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
8854ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
8864ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // type, produce
8874ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  const TagType *Tag = 0;
8884ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (const RecordType *Record = T->getAsRecordType())
8894ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Record;
8904ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  else if (const EnumType *Enum = T->getAsEnumType())
8914ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Enum;
8924ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
8934ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
8944ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Diag(Tag->getDecl()->getLocation(),
8954ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
8964ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
8974ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
8984ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
8994ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
9004ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
901