SemaType.cpp revision 89951a86b594513c2a013532ed45d197413b1087
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());
154c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
156958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
158958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
159958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
160958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
161d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
162fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
163958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
164d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
165d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    Expr *E = static_cast<Expr *>(DS.getTypeRep());
166d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
167d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
168fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfExpr(E);
169958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
170d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
171809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
172809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return QualType();
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
174958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
175958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
176f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
177f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor    if (getLangOptions().Freestanding)
178f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor      Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
179fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
180f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
181958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
182958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
183958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner         "FIXME: imaginary types not supported yet!");
184958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner
18538d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // See if there are any attributes on the declspec that apply to the type (as
18638d8b98803ac354dba15578d65ea99a83dead046Chris Lattner  // opposed to the decl).
187fca0ddd42965e0b7ae821213486d4e0dd71fb439Chris Lattner  if (const AttributeList *AL = DS.getAttributes())
188c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(Result, AL);
189f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
19096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
19196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
19296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
19396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
19496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
19596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
19696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (TypeQuals & QualType::Restrict) {
197bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
198bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        QualType EltTy = PT->getPointeeType();
199bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner
200bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // If we have a pointer or reference, the pointee must have an object or
201bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
202bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
203bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          Diag(DS.getRestrictSpecLoc(),
204d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
205d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
206bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner          TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
207bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
208bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
20996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Diag(DS.getRestrictSpecLoc(),
210d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner             diag::err_typecheck_invalid_restrict_not_pointer)
211d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
212bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
21396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
21496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
21596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
21696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
21796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
21896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
21996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
22096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
22196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
22296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if (TypeQuals & QualType::Const)
22396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
22496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      else {
22596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        assert((TypeQuals & QualType::Volatile) &&
22696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner               "Has CV quals but not C or V?");
22796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
22896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
229d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, diag::warn_typecheck_function_qualifiers)
230d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
23196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
23296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
233f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
234f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
235f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
236f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
237f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
2381a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
2391a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
240f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
241f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Const;
242f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      TypeQuals &= ~QualType::Volatile;
243f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    }
244f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor
24596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Result.getQualifiedType(TypeQuals);
24696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
247f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
248f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
249f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
25098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
25198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// declarator to Type instances. Skip the outermost Skip type
25298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// objects.
253cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlQualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
25498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  bool OmittedReturnType = false;
25598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
25698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  if (D.getContext() == Declarator::BlockLiteralContext
25798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && Skip == 0
25898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && !D.getDeclSpec().hasTypeSpecifier()
25998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      && (D.getNumTypeObjects() == 0
26098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump          || (D.getNumTypeObjects() == 1
26198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump              && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
26298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    OmittedReturnType = true;
26398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
264b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner  // long long is a C99 feature.
265d1eb332181f02a7e0a6f9749265e6a0f55555cd4Chris Lattner  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
266b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner      D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
267b23deda7333cfc2316c989d10745c145dfbea6d6Chris Lattner    Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
268930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
269930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
270930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
271930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
272930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  switch (D.getKind()) {
273930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Abstract:
274930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Normal:
27598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  case Declarator::DK_Operator: {
27698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    const DeclSpec& DS = D.getDeclSpec();
27798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    if (OmittedReturnType)
27898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // We default to a dependent type initially.  Can be modified by
27998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      // the first return statement.
28098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      T = Context.DependentTy;
281809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    else {
28298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      T = ConvertDeclSpecToType(DS);
283809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor      if (T.isNull())
284809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor        return T;
285809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    }
286930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
28798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  }
288930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
289930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Constructor:
290930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Destructor:
291930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  case Declarator::DK_Conversion:
292930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
293930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // "void" instead. Conversion operators will check their return
294930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // types separately.
295930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
296930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
297930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
2984c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
29998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
30098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
30198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
302cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
303cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl    DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
3045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
3065618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
3075618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      if (DeclType.Cls.TypeQuals)
3085618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
3095618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      if (!T.getTypePtr()->isFunctionType())
3105618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
3115618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      else
3125618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        T = Context.getBlockPointerType(T);
3135618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
31502c642e8f40562fcc9d440977f232c021b9d4353Chris Lattner      if (T->isReferenceType()) {
3165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C++ 8.3.2p4: There shall be no ... pointers to references ...
317d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
318d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner         << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
319e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
3205265af5b555f703be365dbc32c4e518fe38d4d9aChris Lattner        T = Context.IntTy;
3215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
3225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
32396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
32496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // object or incomplete types shall not be restrict-qualified."
32596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
326d805bec0fbb98aa10abbb41bfdcb2e2fab1bac96Chris Lattner          !T->isIncompleteOrObjectType()) {
327d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
328d162584991885ab004a02573a73ce06422b921fcChris Lattner          << T;
329f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        DeclType.Ptr.TypeQuals &= ~QualType::Restrict;
330f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
331f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
3325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Apply the pointer typequals to the pointer object.
3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
335f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    case DeclaratorChunk::Reference: {
336f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      // Whether we should suppress the creation of the reference.
337f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      bool SuppressReference = false;
338f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      if (T->isReferenceType()) {
339f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // C++ [dcl.ref]p4: There shall be no references to references.
340f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //
341f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // According to C++ DR 106, references to references are only
342f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // diagnosed when they are written directly (e.g., "int & &"),
343f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // but not when they happen via a typedef:
344f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //
345f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //   typedef int& intref;
346f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //   typedef intref& intref2;
347f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        //
348f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // Parser::ParserDeclaratorInternal diagnoses the case where
349f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // references are written directly; here, we handle the
350f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // collapsing of references-to-references as described in C++
351f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        // DR 106 and amended by C++ DR 540.
352f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        SuppressReference = true;
353f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      }
354f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor
355f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      // C++ [dcl.ref]p1:
356f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      //   A declarator that specifies the type “reference to cv void”
357f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      //   is ill-formed.
358f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      if (T->isVoidType()) {
359f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        Diag(DeclType.Loc, diag::err_reference_to_void);
360e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
361f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        T = Context.IntTy;
3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
36496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
36596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // object or incomplete types shall not be restrict-qualified."
36696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if (DeclType.Ref.HasRestrict &&
367d805bec0fbb98aa10abbb41bfdcb2e2fab1bac96Chris Lattner          !T->isIncompleteOrObjectType()) {
368d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
369d162584991885ab004a02573a73ce06422b921fcChris Lattner          << T;
37096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        DeclType.Ref.HasRestrict = false;
37196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
37296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
373f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor      if (!SuppressReference)
374f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        T = Context.getReferenceType(T);
37596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
37696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Handle restrict on references.
37796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      if (DeclType.Ref.HasRestrict)
37896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        T.addRestrict();
3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
380f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    }
3815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
382fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
38394f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
3855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
3885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
3895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
3905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
3915265af5b555f703be365dbc32c4e518fe38d4d9aChris Lattner
3925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // C99 6.7.5.2p1: If the element type is an incomplete or function type,
3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
3944ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor      if (DiagnoseIncompleteType(D.getIdentifierLoc(), T,
3954ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                                 diag::err_illegal_decl_array_incomplete_type)) {
396e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        T = Context.IntTy;
397e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
3985265af5b555f703be365dbc32c4e518fe38d4d9aChris Lattner      } else if (T->isFunctionType()) {
399d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions)
400d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
401e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        T = Context.getPointerType(T);
402e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
403a1d9fdea79ba7bbd71862b9f9f78f5f117331fc7Chris Lattner      } else if (const ReferenceType *RT = T->getAsReferenceType()) {
4045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C++ 8.3.2p4: There shall be no ... arrays of references ...
405d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references)
406d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
407bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        T = RT->getPointeeType();
408e1223f7246c2c297f7b62816fd8c6a0a14151977Steve Naroff        D.setInvalidType(true);
40902c642e8f40562fcc9d440977f232c021b9d4353Chris Lattner      } else if (const RecordType *EltTy = T->getAsRecordType()) {
4105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // If the element type is a struct or union that contains a variadic
4110bfe54fdc83b7b4e37c40e652d86d15aa89885b2Douglas Gregor        // array, accept it as a GNU extension: C99 6.7.2.1p2.
4120bfe54fdc83b7b4e37c40e652d86d15aa89885b2Douglas Gregor        if (EltTy->getDecl()->hasFlexibleArrayMember())
4130bfe54fdc83b7b4e37c40e652d86d15aa89885b2Douglas Gregor          Diag(DeclType.Loc, diag::ext_flexible_array_in_array) << T;
41443477ca46792311640cf29b7cff731e29bebb146Chris Lattner      } else if (T->isObjCInterfaceType()) {
415d162584991885ab004a02573a73ce06422b921fcChris Lattner        Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T;
4165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
41743477ca46792311640cf29b7cff731e29bebb146Chris Lattner
41842471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff      // C99 6.7.5.2p1: The size expression shall have integer type.
41942471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff      if (ArraySize && !ArraySize->getType()->isIntegerType()) {
420d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner        Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
421d162584991885ab004a02573a73ce06422b921fcChris Lattner          << ArraySize->getType() << ArraySize->getSourceRange();
42242471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        D.setInvalidType(true);
423169a2664a64b57a815b5f0b39276a0891663921aTed Kremenek        ArraySize->Destroy(Context);
424fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner        ATI.NumElts = ArraySize = 0;
42542471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff      }
426c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff      llvm::APSInt ConstVal(32);
427c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      if (!ArraySize) {
428c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman        T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
429898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor      } else if (ArraySize->isValueDependent()) {
430898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals);
43137148aabe7d153ce682b5715a820a11c0bbfcd59Eli Friedman      } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
432cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl                 !T->isConstantSizeType()) {
43337148aabe7d153ce682b5715a820a11c0bbfcd59Eli Friedman        // Per C99, a variable array is an array with either a non-constant
43437148aabe7d153ce682b5715a820a11c0bbfcd59Eli Friedman        // size or an element type that has a non-constant-size
435c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff        T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
436c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      } else {
43742471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        // C99 6.7.5.2p1: If the expression is a constant expression, it shall
43842471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        // have a value greater than zero.
43942471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        if (ConstVal.isSigned()) {
44042471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff          if (ConstVal.isNegative()) {
441dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner            Diag(ArraySize->getLocStart(),
442dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                 diag::err_typecheck_negative_array_size)
443dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner              << ArraySize->getSourceRange();
44442471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff            D.setInvalidType(true);
44542471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff          } else if (ConstVal == 0) {
44642471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff            // GCC accepts zero sized static arrays.
447dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner            Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
448dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner              << ArraySize->getSourceRange();
44942471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff          }
45042471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff        }
451c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff        T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
45242471f8bc6c1179a54941fac3c483ec1bd319436Steve Naroff      }
45394f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      // If this is not C99, extwarn about VLA's and C99 array size modifiers.
454a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner      if (!getLangOptions().C99) {
455a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner        if (ArraySize && !ArraySize->isValueDependent() &&
456a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner            !ArraySize->isIntegerConstantExpr(Context))
457a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner          Diag(D.getIdentifierLoc(), diag::ext_vla);
458a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner        else if (ASM != ArrayType::Normal || ATI.TypeQuals != 0)
459a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner          Diag(D.getIdentifierLoc(), diag::ext_c99_array_usage);
460a1fcbadf4f930c22bb171fb90ed886f5f359d010Chris Lattner      }
4615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
4625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
463f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
4645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
4655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
4665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
4675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
46868cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner
469cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      // C99 6.7.5.3p1: The return type may not be a function or array type.
47068cfd49ea35d9101da9e6d4d5642263c9e95f1a4Chris Lattner      if (T->isArrayType() || T->isFunctionType()) {
471d162584991885ab004a02573a73ce06422b921fcChris Lattner        Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
472cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        T = Context.IntTy;
473cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        D.setInvalidType(true);
474cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      }
475cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner
476eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      if (FTI.NumArgs == 0) {
477c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        if (getLangOptions().CPlusPlus) {
478c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
479c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // function takes no arguments.
480971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
481965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor        } else if (FTI.isVariadic) {
482965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // We allow a zero-parameter variadic function in C if the
483965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // function is marked with the "overloadable"
484965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          // attribute. Scan for this attribute now.
485965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
486965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
487965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
488965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
489965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
490965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
491965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
492965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
493965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
494965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
495965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
496965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
497c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        } else {
498c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          // Simple void foo(), where the incoming T is the result type.
499c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis          T = Context.getFunctionTypeNoProto(T);
500c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
501eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman      } else if (FTI.ArgInfo[0].Param == 0) {
5025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
503eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman        Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
5045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else {
5055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
5065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
5075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
5108123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
5118123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
51278c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
51308d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
51408d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
51508d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // This matches the conversion that is done in
516bff5f5cfd194c3cc096ba89360fb537814a9666aNate Begeman          // Sema::ActOnParamDeclarator(). Without this conversion, the
51708d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // argument type in the function prototype *will not* match the
51808d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // type in ParmVarDecl (which makes the code generator unhappy).
51908d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
52008d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // FIXME: We still apparently need the conversion in
521e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner          // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
52208d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // it should be driving off the type being created here.
52308d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
52408d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // FIXME: If a source translation tool needs to see the original type,
52508d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          // then we need to consider storing both types somewhere...
52608d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          //
527e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner          if (ArgTy->isArrayType()) {
528e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner            ArgTy = Context.getArrayDecayedType(ArgTy);
529529bd02affa96a311dd9ab131f2ab4d833017fb7Chris Lattner          } else if (ArgTy->isFunctionType())
53008d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff            ArgTy = Context.getPointerType(ArgTy);
531e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
5325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
5335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
5345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // int(void) as a FunctionTypeProto with an empty argument list.
53508d51393bfa55a0b7d99db2b2a59f1df7d3f4f68Steve Naroff          else if (ArgTy->isVoidType()) {
5365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
5375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
5385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
5395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
5405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
5412ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
5428123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
5432ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
5442ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
5455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
5464565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
5472ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
5488123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
5492ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
5502ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
551f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner              if (ArgTy.getCVRQualifiers())
5522ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
5532ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner
5542ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
5552ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
5562ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
557eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
558eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
559eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              ArgTy = Context.IntTy;
560eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
561eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
562eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
563eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
5655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          ArgTys.push_back(ArgTy);
5675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
5685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
569971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis                                    FTI.isVariadic, FTI.TypeQuals);
5705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
5715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
5725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
573f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
574f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
575f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      DeclContext *DC = static_cast<DeclContext*>(
576f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        DeclType.Mem.Scope().getScopeRep());
577f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
578f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // FIXME: Extend for dependent types when it's actually supported.
579f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // See ActOnCXXNestedNameSpecifier.
580f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
581f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.getTagDeclType(RD);
582f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
583f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        if (DC) {
584f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          Diag(DeclType.Mem.Scope().getBeginLoc(),
585f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl               diag::err_illegal_decl_mempointer_in_nonclass)
586f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl            << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
587f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl            << DeclType.Mem.Scope().getRange();
588f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        }
589f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
590f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        ClsType = Context.IntTy;
591f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
592f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
593f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
594f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      //   with reference type, or "cv void."
595f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (T->isReferenceType()) {
596f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
597f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
598f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
599f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
600f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
601f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if (T->isVoidType()) {
602f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
603f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
604f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
605f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
606f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
607f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
608f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // object or incomplete types shall not be restrict-qualified."
609f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
610f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          !T->isIncompleteOrObjectType()) {
611f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
612f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl          << T;
613f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        DeclType.Mem.TypeQuals &= ~QualType::Restrict;
614f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
615f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
6164433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl      T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
6174433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl                    getQualifiedType(DeclType.Mem.TypeQuals);
618f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
619f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
620f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
621f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
622c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
623c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    if (const AttributeList *AL = DeclType.getAttrs())
624c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      ProcessTypeAttributeList(T, AL);
6255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
626971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
627971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
628971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    const FunctionTypeProto *FnTy = T->getAsFunctionTypeProto();
629971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    assert(FnTy && "Why oh why is there not a FunctionTypeProto here ?");
630971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
631971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
632971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // for a nonstatic member function, the function type to which a pointer
633971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // to member refers, or the top-level function type of a function typedef
634971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    // declaration.
635971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    if (FnTy->getTypeQuals() != 0 &&
636971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
637584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor        ((D.getContext() != Declarator::MemberContext &&
638584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor          (!D.getCXXScopeSpec().isSet() ||
639584049d49d956add7bce5669e9823491f7d8de78Douglas Gregor           !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
640bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor              ->isRecord())) ||
641971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
642971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      if (D.isFunctionDeclarator())
643971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
644971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      else
645971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(),
646971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis             diag::err_invalid_qualified_typedef_function_type_use);
647971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
648971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      // Strip the cv-quals from the type.
649971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
6507fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                                  FnTy->getNumArgs(), FnTy->isVariadic(), 0);
651971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
652971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
6535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6540bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // If there were any type attributes applied to the decl itself (not the
6550bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  // type, apply the type attribute to the type!)
6560bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner  if (const AttributeList *Attrs = D.getAttributes())
657c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    ProcessTypeAttributeList(T, Attrs);
6580bf29ad534c381087e89efadbf7aff0579bf259fChris Lattner
6595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T;
6605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
662a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
663360300ca2ee298d585d29baf006519507d968812Fariborz Jahanian/// declarator
664a526c5c67e5a0473c340903ee542ce570119665fTed KremenekQualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
66589951a86b594513c2a013532ed45d197413b1087Chris Lattner  ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(static_cast<Decl *>(D));
666306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  QualType T = MDecl->getResultType();
667306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  llvm::SmallVector<QualType, 16> ArgTys;
668306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
6693560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian  // Add the first two invisible argument types for self and _cmd.
670f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  if (MDecl->isInstanceMethod()) {
671a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
6721f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    selfTy = Context.getPointerType(selfTy);
6731f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian    ArgTys.push_back(selfTy);
67489951a86b594513c2a013532ed45d197413b1087Chris Lattner  } else
675a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ArgTys.push_back(Context.getObjCIdType());
676a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ArgTys.push_back(Context.getObjCSelType());
6773560002bed2be6b428f1a909c489a8857b4417c7Fariborz Jahanian
67889951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
67989951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = MDecl->param_end(); PI != E; ++PI) {
68089951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType ArgTy = (*PI)->getType();
681306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    assert(!ArgTy.isNull() && "Couldn't parse type?");
682306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
683306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    // This matches the conversion that is done in
684e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // Sema::ActOnParamDeclarator().
685e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    if (ArgTy->isArrayType())
686e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner      ArgTy = Context.getArrayDecayedType(ArgTy);
687306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    else if (ArgTy->isFunctionType())
688306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian      ArgTy = Context.getPointerType(ArgTy);
689306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian    ArgTys.push_back(ArgTy);
690306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  }
691306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
6927fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                              MDecl->isVariadic(), 0);
693306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian  return T;
694306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian}
695306d68f5ef2eab86e0e86ebadd5a6eee1f752c0bFariborz Jahanian
6969e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
6979e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
6989e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// they point to and return true. If T1 and T2 aren't pointer types
6999e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// or pointer-to-member types, or if they are not similar at this
7009e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// level, returns false and leaves T1 and T2 unchanged. Top-level
7019e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// qualifiers on T1 and T2 are ignored. This function will typically
7029e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// be called in a loop that successively "unwraps" pointer and
7039e5e4aaf8b8835b552819d68d29b6d94115d8a0bSebastian Redl/// pointer-to-member types to compare them at each level.
704ecb81f28cb279b7d8e84296445a4131fa80b69a9Chris Lattnerbool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
70557373266011f73418381b736015d8d2bb0381176Douglas Gregor  const PointerType *T1PtrType = T1->getAsPointerType(),
70657373266011f73418381b736015d8d2bb0381176Douglas Gregor                    *T2PtrType = T2->getAsPointerType();
70757373266011f73418381b736015d8d2bb0381176Douglas Gregor  if (T1PtrType && T2PtrType) {
70857373266011f73418381b736015d8d2bb0381176Douglas Gregor    T1 = T1PtrType->getPointeeType();
70957373266011f73418381b736015d8d2bb0381176Douglas Gregor    T2 = T2PtrType->getPointeeType();
71057373266011f73418381b736015d8d2bb0381176Douglas Gregor    return true;
71157373266011f73418381b736015d8d2bb0381176Douglas Gregor  }
71257373266011f73418381b736015d8d2bb0381176Douglas Gregor
7134433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
7144433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl                          *T2MPType = T2->getAsMemberPointerType();
71521593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl  if (T1MPType && T2MPType &&
71621593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T1MPType->getClass()) ==
71721593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl      Context.getCanonicalType(T2MPType->getClass())) {
7184433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T1 = T1MPType->getPointeeType();
7194433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    T2 = T2MPType->getPointeeType();
7204433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl    return true;
7214433aafbc2591b82e4ea2fc39c723b21d2497f4dSebastian Redl  }
72257373266011f73418381b736015d8d2bb0381176Douglas Gregor  return false;
72357373266011f73418381b736015d8d2bb0381176Douglas Gregor}
72457373266011f73418381b736015d8d2bb0381176Douglas Gregor
725cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian RedlSema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
7265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
7275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
7285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
730cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  QualType T = GetTypeForDeclarator(D, S);
731809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (T.isNull())
732809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
7335912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
7346d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor  // Check that there are no default arguments (C++ only).
7356d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor  if (getLangOptions().CPlusPlus)
7366d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
7376d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
7385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return T.getAsOpaquePtr();
7395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
741c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
742c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
743c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
744c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
745c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
746232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
747232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
748c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
749c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
750c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnerstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
751c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
752232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
753232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
754232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
755232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
756c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
757c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
758232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
759232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
760232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
761545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
762f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
763c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
764232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
765545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
766232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
767c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
768dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
769dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
770c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
771232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
772232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
773232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
774f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
775c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
776c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
777d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
778d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian/// specified type.  The attribute contains 1 argument, weak or strong.
779d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanianstatic void HandleObjCGCTypeAttribute(QualType &Type,
7803b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner                                      const AttributeList &Attr, Sema &S) {
781d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (Type.getObjCGCAttr() != QualType::GCNone) {
7825934e75d98d99374f72722a69c5eefe026f35c74Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
783d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
784d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
785d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
786d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
787ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (!Attr.getParameterName()) {
788ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
789ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
790ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian    return;
791ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
7923b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  QualType::GCAttrTypes GCAttr;
793ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  if (Attr.getNumArgs() != 0) {
794d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
795d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
796d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
797d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (Attr.getParameterName()->isStr("weak"))
7983b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner    GCAttr = QualType::Weak;
799d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else if (Attr.getParameterName()->isStr("strong"))
8003b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner    GCAttr = QualType::Strong;
801d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
802d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
803d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      << "objc_gc" << Attr.getParameterName();
804d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return;
805d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
806d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
8073b6b83b8311ecdfa43cbb37ccc38c107d3b8d88bChris Lattner  Type = S.Context.getObjCGCQualType(Type, GCAttr);
808d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
809d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
810c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattnervoid Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
811c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
812c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
813c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
814c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
815c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  for (; AL; AL = AL->getNext()) {
816c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // If this is an attribute we can handle, do so now, otherwise, add it to
817c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // the LeftOverAttrs list for rechaining.
818c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    switch (AL->getKind()) {
819c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
820c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
821c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      HandleAddressSpaceTypeAttribute(Result, *AL, *this);
822c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
823d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    case AttributeList::AT_objc_gc:
824d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      HandleObjCGCTypeAttribute(Result, *AL, *this);
825d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
826c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
827c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  }
828232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
829232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
8304ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @brief If the type T is incomplete and cannot be completed,
8314ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// produce a suitable diagnostic.
8324ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8334ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
8344ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
8354ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// type, returns false. If @p T is incomplete, issues the diagnostic
8364ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @p diag (giving it the type @p T) and returns true.
8374ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8384ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
8394ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
8404ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8414ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
8424ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8434ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param diag The diagnostic value (e.g.,
8444ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c diag::err_typecheck_decl_incomplete_type) that will be used
8454ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// for the error message if @p T is incomplete.
8464ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8474ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range1  An optional range in the source code that will be a
8484ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
8494ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8504ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Range2  An optional range in the source code that will be a
8514ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// part of the "incomplete type" error message.
8524ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8534ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param PrintType If non-NULL, the type that should be printed
8544ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// instead of @p T. This parameter should be used when the type that
8554ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// we're checking for incompleteness isn't the type that should be
8564ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// displayed to the user, e.g., when T is a type and PrintType is a
8574ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// pointer to T.
8584ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8594ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
8604ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
8614ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
8624ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @todo When Clang gets proper support for C++ templates, this
8634ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// routine will also be able perform template instantiation when @p T
8644ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// is a class template specialization.
8654ec339f43c0cae2678334850c90926bea10999c7Douglas Gregorbool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag,
8664ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                                  SourceRange Range1, SourceRange Range2,
8674ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                                  QualType PrintType) {
8684ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
8694ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
8704ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
8714ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
8724ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (PrintType.isNull())
8734ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    PrintType = T;
8744ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
8754ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
8764ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  Diag(Loc, diag) << PrintType << Range1 << Range2;
8773c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
8784ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
8794ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // type, produce
8804ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  const TagType *Tag = 0;
8814ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (const RecordType *Record = T->getAsRecordType())
8824ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Record;
8834ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  else if (const EnumType *Enum = T->getAsEnumType())
8844ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Tag = Enum;
8854ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
8864ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
8874ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    Diag(Tag->getDecl()->getLocation(),
8884ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
8894ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
8904ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
8914ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
8924ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
8934ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
894