SemaType.cpp revision 152b4e4652baedfceba1cd8115515629225e713f
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
142d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
15658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie#include "clang/AST/ASTConsumer.h"
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
1736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor#include "clang/AST/ASTMutationListener.h"
18a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor#include "clang/AST/CXXInheritance.h"
19980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
202943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor#include "clang/AST/DeclTemplate.h"
2155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/Expr.h"
224adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
2351bd803fbdade51d674598ed45da3d54190a656cJohn McCall#include "clang/AST/TypeLocVisitor.h"
2455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/OpenCL.h"
2591a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
26d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis#include "clang/Basic/TargetInfo.h"
272792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall#include "clang/Lex/Preprocessor.h"
28bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman#include "clang/Parse/ParseDiagnostic.h"
2919510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
30f85e193739c953358c865005855253af4f68a497John McCall#include "clang/Sema/DelayedDiagnostic.h"
31d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor#include "clang/Sema/Lookup.h"
3255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/ScopeInfo.h"
3355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Template.h"
344994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl#include "llvm/ADT/SmallPtrSet.h"
358c952cd40ccec9d720931f27e7d722fed207d536Richard Smith#include "llvm/ADT/SmallString.h"
3687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor#include "llvm/Support/ErrorHandling.h"
37ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman#include "TypeLocBuilder.h"
38ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
41fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballmanenum TypeDiagSelector {
42fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  TDS_Function,
43fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  TDS_Pointer,
44fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  TDS_ObjCObjOrBlock
45fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman};
46fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman
475db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// isOmittedBlockReturnType - Return true if this declarator is missing a
4891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier/// return type because this is a omitted return type on a block literal.
498ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic bool isOmittedBlockReturnType(const Declarator &D) {
505db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getContext() != Declarator::BlockLiteralContext ||
518ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl      D.getDeclSpec().hasTypeSpecifier())
525db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return false;
5391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
545db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 0)
55a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^{ ... }
5691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
575db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 1 &&
585db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      D.getTypeObject(0).Kind == DeclaratorChunk::Function)
59a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^(int X, float Y) { ... }
6091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
615db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  return false;
625db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner}
635db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
642792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// diagnoseBadTypeAttribute - Diagnoses a type attribute which
652792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// doesn't apply to the given type.
662792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCallstatic void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr,
672792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall                                     QualType type) {
68fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  TypeDiagSelector WhichType;
69fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  bool useExpansionLoc = true;
702792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  switch (attr.getKind()) {
71fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  case AttributeList::AT_ObjCGC:        WhichType = TDS_Pointer; break;
72fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  case AttributeList::AT_ObjCOwnership: WhichType = TDS_ObjCObjOrBlock; break;
732792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  default:
742792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    // Assume everything else was a function attribute.
75fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman    WhichType = TDS_Function;
76fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman    useExpansionLoc = false;
772792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    break;
782792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  }
792792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
802792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  SourceLocation loc = attr.getLoc();
815f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef name = attr.getName()->getName();
822792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
832792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  // The GC attributes are usually written with macros;  special-case them.
84108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth  if (useExpansionLoc && loc.isMacroID() && attr.getParameterName()) {
85834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    if (attr.getParameterName()->isStr("strong")) {
86834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__strong")) name = "__strong";
87834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    } else if (attr.getParameterName()->isStr("weak")) {
88834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__weak")) name = "__weak";
892792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    }
902792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  }
912792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
92fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  S.Diag(loc, diag::warn_type_attribute_wrong_type) << name << WhichType
93fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman    << type;
942792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall}
952792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
96711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// objc_gc applies to Objective-C pointers or, otherwise, to the
97711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// smallest available pointer type (i.e. 'void*' in 'void**').
98711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
998e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ObjCGC: \
1008e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ObjCOwnership
101711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
102711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// Function type attributes.
103711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define FUNCTION_TYPE_ATTRS_CASELIST \
1048e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NoReturn: \
1058e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_CDecl: \
1068e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_FastCall: \
1078e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_StdCall: \
1088e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ThisCall: \
1098e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Pascal: \
1108e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Regparm: \
111263366f9241366f29ba65b703120f302490c39ffDerek Schuff    case AttributeList::AT_Pcs: \
11238980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    case AttributeList::AT_PnaclCall: \
113aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_IntelOclBicc
114aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
115aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman// Microsoft-specific type qualifiers.
116aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman#define MS_TYPE_ATTRS_CASELIST  \
117aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_Ptr32: \
118aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_Ptr64: \
119aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_SPtr: \
120aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_UPtr
121711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
122711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace {
123711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// An object which stores processing state for the entire
124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// GetTypeForDeclarator process.
125711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  class TypeProcessingState {
126711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &sema;
127711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
128711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The declarator being processed.
129711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &declarator;
130711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
131711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The index of the declarator chunk we're currently processing.
132711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// May be the total number of valid chunks, indicating the
133711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// DeclSpec.
134711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex;
135711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
136711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Whether there are non-trivial modifications to the decl spec.
137711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    bool trivial;
138711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1397ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    /// Whether we saved the attributes in the decl spec.
1407ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    bool hasSavedAttrs;
1417ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
142711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The original set of attributes on the DeclSpec.
1435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> savedAttrs;
144711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
145711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// A list of attributes to diagnose the uselessness of when the
146711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// processing is complete.
1475f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> ignoredTypeAttrs;
148711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
149711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  public:
150711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    TypeProcessingState(Sema &sema, Declarator &declarator)
151711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : sema(sema), declarator(declarator),
152711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        chunkIndex(declarator.getNumTypeObjects()),
1537ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        trivial(true), hasSavedAttrs(false) {}
154711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
155711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &getSema() const {
156711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return sema;
157711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
158711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
159711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &getDeclarator() const {
160711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator;
161711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
163b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    bool isProcessingDeclSpec() const {
164b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      return chunkIndex == declarator.getNumTypeObjects();
165b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
166b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned getCurrentChunkIndex() const {
168711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return chunkIndex;
169711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
170711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void setCurrentChunkIndex(unsigned idx) {
172711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(idx <= declarator.getNumTypeObjects());
173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      chunkIndex = idx;
174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
175711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
176711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&getCurrentAttrListRef() const {
177b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (isProcessingDeclSpec())
178711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        return getMutableDeclSpec().getAttributes().getListRef();
179711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator.getTypeObject(chunkIndex).getAttrListRef();
180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
181711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
182711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Save the current set of attributes on the DeclSpec.
183711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void saveDeclSpecAttrs() {
184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Don't try to save them multiple times.
1857ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (hasSavedAttrs) return;
186711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
187711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      DeclSpec &spec = getMutableDeclSpec();
188711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (AttributeList *attr = spec.getAttributes().getList(); attr;
189711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             attr = attr->getNext())
190711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs.push_back(attr);
191711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      trivial &= savedAttrs.empty();
1927ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      hasSavedAttrs = true;
193711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
194711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
195711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Record that we had nowhere to put the given type attribute.
196711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// We will diagnose such attributes later.
197711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void addIgnoredTypeAttr(AttributeList &attr) {
198711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      ignoredTypeAttrs.push_back(&attr);
199711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
201711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Diagnose all the ignored type attributes, given that the
202711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// declarator worked out to the given type.
203711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void diagnoseIgnoredTypeAttrs(QualType type) const {
2045f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      for (SmallVectorImpl<AttributeList*>::const_iterator
205711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
2062792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall           i != e; ++i)
2072792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall        diagnoseBadTypeAttribute(getSema(), **i, type);
208711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
209711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
210711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    ~TypeProcessingState() {
211711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (trivial) return;
212711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
213711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      restoreDeclSpecAttrs();
214711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
215711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
216711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  private:
217711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclSpec &getMutableDeclSpec() const {
218711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return const_cast<DeclSpec&>(declarator.getDeclSpec());
219711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
220711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
221711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void restoreDeclSpecAttrs() {
2227ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      assert(hasSavedAttrs);
2237ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
2247ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (savedAttrs.empty()) {
2257ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        getMutableDeclSpec().getAttributes().set(0);
2267ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        return;
2277ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      }
2287ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
229711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
230711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
231711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs[i]->setNext(savedAttrs[i+1]);
232711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      savedAttrs.back()->setNext(0);
233711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
234711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
235711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
236711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// Basically std::pair except that we really want to avoid an
237711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// implicit operator= for safety concerns.  It's also a minor
238711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// link-time optimization for this to be a private type.
239711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  struct AttrAndList {
240711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The attribute.
241711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &first;
242711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
243711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The head of the list the attribute is currently in.
244711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&second;
245711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
246711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttrAndList(AttributeList &attr, AttributeList *&head)
247711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : first(attr), second(head) {}
248711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
24904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
25004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
251711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace llvm {
252711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  template <> struct isPodLike<AttrAndList> {
253711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    static const bool value = true;
254711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
255711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
256711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
257711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
258711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  attr.setNext(head);
259711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  head = &attr;
260711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
261711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
262711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
263711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (head == &attr) {
264711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    head = attr.getNext();
265711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
26604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
267711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
268711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *cur = head;
269711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  while (true) {
270711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    assert(cur && cur->getNext() && "ran out of attrs?");
271711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (cur->getNext() == &attr) {
272711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      cur->setNext(attr.getNext());
273711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
274711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
275711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    cur = cur->getNext();
276711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
277711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
278711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
279711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void moveAttrFromListToList(AttributeList &attr,
280711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&fromList,
281711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&toList) {
282711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, fromList);
283711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrIntoList(attr, toList);
284711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
285711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
286f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith/// The location of a type attribute.
287f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smithenum TypeAttrLocation {
288f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is in the decl-specifier-seq.
289f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclSpec,
290f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is part of a DeclaratorChunk.
291f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclChunk,
292f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is immediately after the declaration's name.
293f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclName
294f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith};
295f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
296711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state,
297f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             QualType &type, TypeAttrLocation TAL,
298711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             AttributeList *attrs);
299711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
300711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
301711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
302711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type);
303711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
304aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballmanstatic bool handleMSPointerTypeQualifierAttr(TypeProcessingState &state,
305aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             AttributeList &attr,
306aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             QualType &type);
307aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
308711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
309711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr, QualType &type);
310711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
311b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
312f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr, QualType &type);
313f85e193739c953358c865005855253af4f68a497John McCall
314711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCPointerTypeAttr(TypeProcessingState &state,
315711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr, QualType &type) {
3168e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_ObjCGC)
317f85e193739c953358c865005855253af4f68a497John McCall    return handleObjCGCTypeAttr(state, attr, type);
3188e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  assert(attr.getKind() == AttributeList::AT_ObjCOwnership);
319b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  return handleObjCOwnershipTypeAttr(state, attr, type);
320711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
321711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
322b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// Given the index of a declarator chunk, check whether that chunk
323b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// directly specifies the return type of a function and, if so, find
324b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// an appropriate place for it.
325b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall///
326b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// \param i - a notional index which the search will start
327b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall///   immediately inside
328b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCallstatic DeclaratorChunk *maybeMovePastReturnType(Declarator &declarator,
329b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall                                                unsigned i) {
330b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  assert(i <= declarator.getNumTypeObjects());
331b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
332b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  DeclaratorChunk *result = 0;
333b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
334b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  // First, look inwards past parens for a function declarator.
335b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  for (; i != 0; --i) {
336b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    DeclaratorChunk &fnChunk = declarator.getTypeObject(i-1);
337b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    switch (fnChunk.Kind) {
338b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Paren:
339b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      continue;
340b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
341b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // If we find anything except a function, bail out.
342b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Pointer:
343b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::BlockPointer:
344b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Array:
345b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Reference:
346b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::MemberPointer:
347b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      return result;
348b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
349b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // If we do find a function declarator, scan inwards from that,
350b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // looking for a block-pointer declarator.
351b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Function:
352b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      for (--i; i != 0; --i) {
353b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        DeclaratorChunk &blockChunk = declarator.getTypeObject(i-1);
354b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        switch (blockChunk.Kind) {
355b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Paren:
356b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Pointer:
357b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Array:
358b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Function:
359b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Reference:
360b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::MemberPointer:
361b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          continue;
362b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::BlockPointer:
363b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          result = &blockChunk;
364b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          goto continue_outer;
365b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        }
366b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        llvm_unreachable("bad declarator chunk kind");
367b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      }
368b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
369b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      // If we run out of declarators doing that, we're done.
370b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      return result;
371b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
372b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    llvm_unreachable("bad declarator chunk kind");
373b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
374b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // Okay, reconsider from our new point.
375b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  continue_outer: ;
376b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  }
377b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
378b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  // Ran out of chunks, bail out.
379b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  return result;
380b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall}
381b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
382711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Given that an objc_gc attribute was written somewhere on a
383711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declaration *other* than on the declarator itself (for which, use
384711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
385711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// didn't apply in whatever position it was written in, try to move
386711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// it to a more appropriate position.
387711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeObjCPointerTypeAttr(TypeProcessingState &state,
388711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          AttributeList &attr,
389711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType type) {
390711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
391b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
392b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  // Move it to the outermost normal or block pointer declarator.
393711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
394711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
395711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
396711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
397b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::BlockPointer: {
398b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      // But don't move an ARC ownership attribute to the return type
399b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      // of a block.
400b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      DeclaratorChunk *destChunk = 0;
401b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (state.isProcessingDeclSpec() &&
402b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          attr.getKind() == AttributeList::AT_ObjCOwnership)
403b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        destChunk = maybeMovePastReturnType(declarator, i - 1);
404b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (!destChunk) destChunk = &chunk;
405b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
406711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
407b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall                             destChunk->getAttrListRef());
408711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
409b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
410711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
411711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
412711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
414711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
415b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // We may be starting at the return type of a block.
416b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Function:
417b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (state.isProcessingDeclSpec() &&
418b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          attr.getKind() == AttributeList::AT_ObjCOwnership) {
419b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        if (DeclaratorChunk *dest = maybeMovePastReturnType(declarator, i)) {
420b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
421b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall                                 dest->getAttrListRef());
422b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          return;
423b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        }
424b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      }
425b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      goto error;
426b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
427711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Don't walk through these.
428711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
429711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
430711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto error;
431711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
432711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
433711c52bb20d0c69063b52a99826fb7d2835501f1John McCall error:
4342792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
4352792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
436711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
437711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
438711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Distribute an objc_gc type attribute that was written on the
439711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator.
440711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
441711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
442711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            AttributeList &attr,
443711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            QualType &declSpecType) {
444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
445711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
446711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // objc_gc goes on the innermost pointer to something that's not a
447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // pointer.
448711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  unsigned innermost = -1U;
449711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  bool considerDeclSpec = true;
450711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
451711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
452711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
453711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
454711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
455711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      innermost = i;
456ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      continue;
457711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
458711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
460711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
461711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
462711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
463711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
465711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      considerDeclSpec = false;
466711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto done;
467711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
468711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
469711c52bb20d0c69063b52a99826fb7d2835501f1John McCall done:
470711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
471711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // That might actually be the decl spec if we weren't blocked by
472711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // anything in the declarator.
473711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (considerDeclSpec) {
4747ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    if (handleObjCPointerTypeAttr(state, attr, declSpecType)) {
4757ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // Splice the attribute into the decl spec.  Prevents the
4767ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // attribute from being applied multiple times and gives
4777ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // the source-location-filler something to work with.
4787ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      state.saveDeclSpecAttrs();
4797ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      moveAttrFromListToList(attr, declarator.getAttrListRef(),
4807ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall               declarator.getMutableDeclSpec().getAttributes().getListRef());
481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
4827ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    }
483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
484711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
485711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, if we found an appropriate chunk, splice the attribute
486711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // into it.
487711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (innermost != -1U) {
488711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, declarator.getAttrListRef(),
489711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                       declarator.getTypeObject(innermost).getAttrListRef());
490711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
491711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
492711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
493711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, diagnose when we're done building the type.
494711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
497711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
498711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written somewhere in a declaration
499711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// *other* than on the declarator itself or in the decl spec.  Given
500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// that it didn't apply in whatever position it was written in, try
501711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// to move it to a more appropriate position.
502711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeFunctionTypeAttr(TypeProcessingState &state,
503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType type) {
505711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
506711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
507711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to push the attribute from the return type of a function to
508711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the function itself.
509711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
510711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
511711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
512711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
513711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
514711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
520711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
521711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
523711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
524711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
525711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
52691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5272792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
528711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
529711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Try to distribute a function type attribute to the innermost
531711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// function chunk or type.  Returns true if the attribute was
532711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributed, false if no location was found.
533711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool
534711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
535711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr,
536711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList *&attrList,
537711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      QualType &declSpecType) {
538711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
539711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
540711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Put it on the innermost function chunk, if there is one.
541711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
542711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
543711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (chunk.Kind != DeclaratorChunk::Function) continue;
544711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
545711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
546711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
547711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
548711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5495b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  return handleFunctionTypeAttr(state, attr, declSpecType);
550711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
551711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
552711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written in the decl spec.  Try to
553711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
554711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
555711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
556711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
557711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType &declSpecType) {
558711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.saveDeclSpecAttrs();
559711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5605c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // C++11 attributes before the decl specifiers actually appertain to
5615c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // the declarators. Move them straight there. We don't support the
5625c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // 'put them wherever you like' semantics we allow for GNU attributes.
5635c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  if (attr.isCXX11Attribute()) {
5645c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
5655c52166525f8714c3e3a979b156ec23426947fd3Richard Smith                           state.getDeclarator().getAttrListRef());
5665c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    return;
5675c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  }
5685c52166525f8714c3e3a979b156ec23426947fd3Richard Smith
569711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
570711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
571711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            state.getCurrentAttrListRef(),
572711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
573711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
574711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
575711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
576711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
577711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
578711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
579711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
580711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written on the declarator.  Try to
581711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
582711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
583711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
584711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         AttributeList &attr,
585711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         QualType &declSpecType) {
586711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
587711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
588711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
589711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
590711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declarator.getAttrListRef(),
591711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
592711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
593711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
594711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
595711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
596711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
597711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
598711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
599711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
600711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// \brief Given that there are attributes written on the declarator
601711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// itself, try to distribute any type attributes to the appropriate
602711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator chunk.
603711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///
604711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// These are attributes like the following:
605711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f ATTR;
606711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int (f ATTR)();
607711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// but not necessarily this:
608711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f() ATTR;
609711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
610711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                              QualType &declSpecType) {
611711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Collect all the type attributes from the declarator itself.
612711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
613711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *attr = state.getDeclarator().getAttributes();
614711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
615711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
616711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr->getNext();
617711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
6185c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    // Do not distribute C++11 attributes. They have strict rules for what
6195c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    // they appertain to.
6205c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    if (attr->isCXX11Attribute())
6215c52166525f8714c3e3a979b156ec23426947fd3Richard Smith      continue;
6225c52166525f8714c3e3a979b156ec23426947fd3Richard Smith
623711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr->getKind()) {
624711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
625711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
626711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
627711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
6288e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
6294e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
630f85e193739c953358c865005855253af4f68a497John McCall        break;
631f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough
632f85e193739c953358c865005855253af4f68a497John McCall
633711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
634711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
635711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
636711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
637aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    MS_TYPE_ATTRS_CASELIST:
638aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      // Microsoft type attributes cannot go after the declarator-id.
639aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      continue;
640aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
641711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    default:
642711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
643711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
644711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attr = next));
645711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
646711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
647711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Add a synthetic '()' to a block-literal declarator if it is
648711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// required, given the return type.
649711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void maybeSynthesizeBlockSignature(TypeProcessingState &state,
650711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType declSpecType) {
651711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
652711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
653711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // First, check whether the declarator would produce a function,
654711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // i.e. whether the innermost semantic chunk is a function.
655711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.isFunctionDeclarator()) {
656711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // If so, make that declarator a prototyped declarator.
657711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.getFunctionTypeInfo().hasPrototype = true;
658711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
659711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
660711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
661da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // If there are any type objects, the type as written won't name a
662da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // function, regardless of the decl spec type.  This is because a
663da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // block signature declarator is always an abstract-declarator, and
664da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // abstract-declarators can't just be parentheses chunks.  Therefore
665da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // we need to build a function chunk unless there are no type
666da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // objects and the decl spec type is a function.
667711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
668711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
669711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
670da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // Note that there *are* cases with invalid declarators where
671da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // declarators consist solely of parentheses.  In general, these
672da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // occur only in failed efforts to make function declarators, so
673da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // faking up the function chunk is still the right thing to do.
674711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
675711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, we need to fake up a function declarator.
67696a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar  SourceLocation loc = declarator.getLocStart();
677711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
678711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // ...and *prepend* it to the declarator.
67959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara  SourceLocation NoLoc;
680711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
68159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*HasProto=*/true,
68259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*IsAmbiguous=*/false,
68359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*LParenLoc=*/NoLoc,
68459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ArgInfo=*/0,
68559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NumArgs=*/0,
68659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*EllipsisLoc=*/NoLoc,
68759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RParenLoc=*/NoLoc,
68859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*TypeQuals=*/0,
68959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RefQualifierIsLvalueRef=*/true,
69059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RefQualifierLoc=*/NoLoc,
69159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ConstQualifierLoc=*/NoLoc,
69259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*VolatileQualifierLoc=*/NoLoc,
69359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*MutableLoc=*/NoLoc,
69459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             EST_None,
69559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ESpecLoc=*/NoLoc,
69659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*Exceptions=*/0,
69759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ExceptionRanges=*/0,
69859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NumExceptions=*/0,
69959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NoexceptExpr=*/0,
70059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             loc, loc, declarator));
701711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
702711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // For consistency, make sure the state still has us as processing
703711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the decl spec.
704711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
705711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.setCurrentChunkIndex(declarator.getNumTypeObjects());
70604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
70704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
708930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
709930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
7101dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// \param state Specifies the declarator containing the declaration specifier
7111dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// to be converted, along with other associated processing state.
7125153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
7135153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
7148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType ConvertDeclSpecToType(TypeProcessingState &state) {
7155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
7165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
717711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
7188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
719711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
720711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const DeclSpec &DS = declarator.getDeclSpec();
721711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation DeclLoc = declarator.getIdentifierLoc();
7225db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
72396a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar    DeclLoc = DS.getLocStart();
72491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
725711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  ASTContext &Context = S.Context;
7261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7275db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
7285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
72996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
73096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
73196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
7325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
7335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
734fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
7355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
736fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
7375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
7385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
7395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
740fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
7415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
742958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
74364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
74464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
74564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
74664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
747711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
748f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
74964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
75064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
75164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
75264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
753711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
754f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
75564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
75664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
75764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
758f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
759f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
760f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
761f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
762f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
763f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
764f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
765f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
766f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
767f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
768d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
76962f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
770097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
771c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
77231ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                         (ObjCProtocolDecl*const*)PQ,
773c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         DS.getNumProtocolQualifiers());
774c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectPointerType(Result);
77562f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
77662f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
77791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
7785db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
7795db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
780f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // The declspec is always missing in a lambda expr context; it is either
781f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // specified with a trailing return type or inferred.
782f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    if (declarator.getContext() == Declarator::LambdaExprContext ||
783f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman        isOmittedBlockReturnType(declarator)) {
784152b4e4652baedfceba1cd8115515629225e713fManuel Klimek      Result = Context.DependentTy;
7855db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
7865db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
7871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
788d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
789d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
790d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
791d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
792d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
793d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
794d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
7954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().ImplicitInt) {
79635d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
79735d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
7983f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
799711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_declspec)
8003f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
80196a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        << FixItHint::CreateInsertion(DS.getLocStart(), "int");
8023f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
8034310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
804d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
805d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
806d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
807d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
80858eb37036b47bbe7433f72d92a2cb60848507707Richard Smith      if (S.getLangOpts().CPlusPlus) {
809711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_missing_type_specifier)
8103f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
8111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
812b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
813b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
814b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
815711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
816b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
817711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_type_specifier)
8183f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
819b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
820d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
8211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
8233cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
8245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
8255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
826fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
827fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
828fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
829311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
830311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
83191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
832e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
833e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
834e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
835e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
83680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
837e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
838e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
839e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
840e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
841311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
8425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
8435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
8445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
845fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
846fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
847fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
848311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
849311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
85091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
851e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
852e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
853e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
854e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
85580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
856e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
857e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
858e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
859e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
860311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
863958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
8643cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
8655a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith  case DeclSpec::TST_int128:
86684268904947ada7e251932a6f5b0f4364df7a2c7Richard Smith    if (!S.PP.getTargetInfo().hasInt128Type())
86784268904947ada7e251932a6f5b0f4364df7a2c7Richard Smith      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_int128_unsupported);
8685a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    if (DS.getTypeSpecSign() == DeclSpec::TSS_unsigned)
8695a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.UnsignedInt128Ty;
8705a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    else
8715a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.Int128Ty;
8725a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    break;
873aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case DeclSpec::TST_half: Result = Context.HalfTy; break;
874fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
875958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
876958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
877fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
878958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
879fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
88039d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne
8814e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
88239d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
88339d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      declarator.setInvalidType(true);
88439d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    }
885958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
886fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
8875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
8885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
8895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
890711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
8918f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
892711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
8938f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
89499dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
8955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
8965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
8976666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_struct:
8986666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_interface: {
899b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
9006e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
9016e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
9026e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
903711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
9046e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
9056e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
9066e24726524c2b51b31bb4b622aa678a46b024f42John McCall
907a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
9080daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc());
90991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
9105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
911a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
91291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
9135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
914a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
9152191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
9160daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    // In both C and C++, make an ElaboratedType.
9170daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    ElaboratedTypeKeyword Keyword
9180daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
9190daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
920958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
9211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
9221a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
9235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
9245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
9255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
926711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
92727940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    if (Result.isNull())
928711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
92927940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    else if (DeclSpec::ProtocolQualifierListTy PQ
93027940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall               = DS.getProtocolQualifiers()) {
931c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
932c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // Silently drop any existing protocol qualifiers.
933c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // TODO: determine whether that's the right thing to do.
934c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjT->getNumProtocols())
935c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = ObjT->getBaseType();
936c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
937c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (DS.getNumProtocolQualifiers())
938c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = Context.getObjCObjectType(Result,
93931ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                             (ObjCProtocolDecl*const*) PQ,
940c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             DS.getNumProtocolQualifiers());
941c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCIdType()) {
942ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
943c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
94431ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                           (ObjCProtocolDecl*const*) PQ,
945c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
946c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
947c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCClassType()) {
9484262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
949c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
95031ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                           (ObjCProtocolDecl*const*) PQ,
951c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
952c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
9533f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
954711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
9553f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
956711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
9573f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
958c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
9591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
961958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
9625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
963958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
964e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
965711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
966958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
967730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian    if (!Result->isDependentType())
968730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian      if (const TagType *TT = Result->getAs<TagType>())
969711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
970d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
971fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
972958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
973d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
974b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
975d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
976d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
977711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
9784b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
9794b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
980711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
9814b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
982958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
983d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
9846fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
985b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
9866fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
9876fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
988711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
989af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
990af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
991711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
992af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
9936fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
9946fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
995ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case DeclSpec::TST_underlyingType:
996db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    Result = S.GetTypeFromParser(DS.getRepAsType());
997db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    assert(!Result.isNull() && "Didn't get a type for __underlying_type?");
998ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    Result = S.BuildUnaryTransformType(Result,
999ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::EnumUnderlyingType,
1000ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       DS.getTypeSpecTypeLoc());
1001ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (Result.isNull()) {
1002ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Result = Context.IntTy;
1003ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      declarator.setInvalidType(true);
1004db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    }
100591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
1006db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
1007a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  case DeclSpec::TST_auto:
1008e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
1009152b4e4652baedfceba1cd8115515629225e713fManuel Klimek    Result = Context.getAutoType(QualType(), /*decltype(auto)*/false);
1010a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    break;
1011a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
1012a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  case DeclSpec::TST_decltype_auto:
1013152b4e4652baedfceba1cd8115515629225e713fManuel Klimek    Result = Context.getAutoType(QualType(), /*decltype(auto)*/true);
1014e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
10151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1016a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall  case DeclSpec::TST_unknown_anytype:
1017a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    Result = Context.UnknownAnyTy;
1018a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    break;
1019a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall
1020b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case DeclSpec::TST_atomic:
1021b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.GetTypeFromParser(DS.getRepAsType());
1022b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(!Result.isNull() && "Didn't get a type for _Atomic?");
1023b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc());
1024b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Result.isNull()) {
1025b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Result = Context.IntTy;
1026b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      declarator.setInvalidType(true);
1027b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
102891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
1029b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1030b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_t:
1031b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dTy;
1032b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1033b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1034b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_array_t:
1035b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dArrayTy;
1036b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1037b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1038b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_buffer_t:
1039b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dBufferTy;
1040b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1041b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1042b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_t:
1043b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dTy;
1044b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1045b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1046b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_array_t:
1047b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dArrayTy;
1048b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1049b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1050b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image3d_t:
1051b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage3dTy;
1052b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1053b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
105421f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei  case DeclSpec::TST_sampler_t:
105521f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    Result = Context.OCLSamplerTy;
105621f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    break;
105721f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei
1058e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei  case DeclSpec::TST_event_t:
1059e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    Result = Context.OCLEventTy;
1060e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    break;
1061e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei
1062809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
10635153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
1064711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
10655153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
10665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1068958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
1069f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
10704e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().Freestanding)
1071711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
1072fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
107382287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
107482287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
107582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
1076e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
1077788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
1078e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
1079788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
1080e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
1081e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
1082f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
10831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
108447423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
108547423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
1086711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
10871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1088711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
1089711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
1090711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
1091711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
1092711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1093711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
1094711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
1095711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
1096711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (AttributeList *attrs = DS.getAttributes().getList())
1097f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    processTypeAttrs(state, Result, TAL_DeclSpec, attrs);
10981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
109996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
110096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
110196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
110296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
110396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
110496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
110596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
11060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
11074cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        S.Diag(DS.getConstSpecLoc(), diag::warn_typecheck_function_qualifiers)
11084cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith          << Result << DS.getSourceRange();
11090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
11104cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        S.Diag(DS.getVolatileSpecLoc(), diag::warn_typecheck_function_qualifiers)
11114cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith          << Result << DS.getSourceRange();
11120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
11134cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        assert((TypeQuals & (DeclSpec::TQ_restrict | DeclSpec::TQ_atomic)) &&
11144cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith               "Has CVRA quals but not C, V, R, or A?");
11154cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // No diagnostic; we'll diagnose 'restrict' or '_Atomic' applied to a
11164cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // function type later, in BuildQualifiedType.
111796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
111896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
11191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1120f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
1121f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
1122f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
1123f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
1124f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
11251a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
11261a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
1127f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
11280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
11290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
11304cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      TypeQuals &= ~DeclSpec::TQ_atomic;
11311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
11321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1133bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // C90 6.5.3 constraints: "The same type qualifier shall not appear more
1134bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // than once in the same specifier-list or qualifier-list, either directly
1135bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // or via one or more typedefs."
113691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus
1137bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman        && TypeQuals & Result.getCVRQualifiers()) {
1138bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()) {
113991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec)
1140bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "const";
1141bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1142bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
1143bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()) {
114491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec)
1145bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "volatile";
1146bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1147bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
11484cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // C90 doesn't have restrict nor _Atomic, so it doesn't force us to
11494cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // produce a warning in this case.
1150bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    }
1151bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
11524cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    QualType Qualified = S.BuildQualifiedType(Result, DeclLoc, TypeQuals, &DS);
11534cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
11544cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // If adding qualifiers fails, just use the unqualified type.
11554cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (Qualified.isNull())
11564cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      declarator.setInvalidType(true);
11574cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    else
11584cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      Result = Qualified;
115996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
11600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1161f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
1162f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
1163f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
1164cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
1165cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
1166cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
11671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1168cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
1169cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1170cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
11712865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
117293d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith                                  Qualifiers Qs, const DeclSpec *DS) {
11732865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
11742865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
11752865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
11762865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
11772865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
11782865474261a608c7873b87ba4af110d17907896dJohn McCall
117993d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith    if (T->isAnyPointerType() || T->isReferenceType() ||
118093d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        T->isMemberPointerType()) {
118193d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      QualType EltTy;
118293d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      if (T->isObjCObjectPointerType())
118393d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = T;
118493d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>())
118593d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = PTy->getPointeeType();
118693d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      else
118793d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = T->getPointeeType();
118893d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith
118993d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      // If we have a pointer or reference, the pointee must have an object
119093d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      // incomplete type.
119193d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      if (!EltTy->isIncompleteOrObjectType()) {
11922865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
119393d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        ProblemTy = EltTy;
119491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
119593d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith    } else if (!T->isDependentType()) {
119693d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      DiagID = diag::err_typecheck_invalid_restrict_not_pointer;
11972865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
11982865474261a608c7873b87ba4af110d17907896dJohn McCall    }
11992865474261a608c7873b87ba4af110d17907896dJohn McCall
12002865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
120193d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << ProblemTy;
12022865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
12032865474261a608c7873b87ba4af110d17907896dJohn McCall    }
12042865474261a608c7873b87ba4af110d17907896dJohn McCall  }
12052865474261a608c7873b87ba4af110d17907896dJohn McCall
12062865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
12072865474261a608c7873b87ba4af110d17907896dJohn McCall}
12082865474261a608c7873b87ba4af110d17907896dJohn McCall
12094cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard SmithQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
12104cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith                                  unsigned CVRA, const DeclSpec *DS) {
12114cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // Convert from DeclSpec::TQ to Qualifiers::TQ by just dropping TQ_atomic.
12124cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  unsigned CVR = CVRA & ~DeclSpec::TQ_atomic;
12134cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
12144cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // C11 6.7.3/5:
12154cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   If the same qualifier appears more than once in the same
12164cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   specifier-qualifier-list, either directly or via one or more typedefs,
12174cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   the behavior is the same as if it appeared only once.
12184cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //
12194cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // It's not specified what happens when the _Atomic qualifier is applied to
12204cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // a type specified with the _Atomic specifier, but we assume that this
12214cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // should be treated as if the _Atomic qualifier appeared multiple times.
12224cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  if (CVRA & DeclSpec::TQ_atomic && !T->isAtomicType()) {
12234cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // C11 6.7.3/5:
12244cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   If other qualifiers appear along with the _Atomic qualifier in a
12254cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   specifier-qualifier-list, the resulting type is the so-qualified
12264cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   atomic type.
12274cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //
12284cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // Don't need to worry about array types here, since _Atomic can't be
12294cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // applied to such types.
12304cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    SplitQualType Split = T.getSplitUnqualifiedType();
12314cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    T = BuildAtomicType(QualType(Split.Ty, 0),
12324cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith                        DS ? DS->getAtomicSpecLoc() : Loc);
12334cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (T.isNull())
12344cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      return T;
12354cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    Split.Quals.addCVRQualifiers(CVR);
12364cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    return BuildQualifiedType(T, Loc, Split.Quals);
12374cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
12384cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
12394cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  return BuildQualifiedType(T, Loc, Qualifiers::fromCVRMask(CVR), DS);
12404cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith}
12414cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
1242075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
1243075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
1244075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
1245075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
1246075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
1247f85e193739c953358c865005855253af4f68a497John McCall/// Given that we're building a pointer or reference to the given
1248f85e193739c953358c865005855253af4f68a497John McCallstatic QualType inferARCLifetimeForPointee(Sema &S, QualType type,
1249f85e193739c953358c865005855253af4f68a497John McCall                                           SourceLocation loc,
1250f85e193739c953358c865005855253af4f68a497John McCall                                           bool isReference) {
1251f85e193739c953358c865005855253af4f68a497John McCall  // Bail out if retention is unrequired or already specified.
1252f85e193739c953358c865005855253af4f68a497John McCall  if (!type->isObjCLifetimeType() ||
1253f85e193739c953358c865005855253af4f68a497John McCall      type.getObjCLifetime() != Qualifiers::OCL_None)
1254f85e193739c953358c865005855253af4f68a497John McCall    return type;
1255f85e193739c953358c865005855253af4f68a497John McCall
1256f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None;
1257f85e193739c953358c865005855253af4f68a497John McCall
1258f85e193739c953358c865005855253af4f68a497John McCall  // If the object type is const-qualified, we can safely use
1259f85e193739c953358c865005855253af4f68a497John McCall  // __unsafe_unretained.  This is safe (because there are no read
1260f85e193739c953358c865005855253af4f68a497John McCall  // barriers), and it'll be safe to coerce anything but __weak* to
1261f85e193739c953358c865005855253af4f68a497John McCall  // the resulting type.
1262f85e193739c953358c865005855253af4f68a497John McCall  if (type.isConstQualified()) {
1263f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
1264f85e193739c953358c865005855253af4f68a497John McCall
1265f85e193739c953358c865005855253af4f68a497John McCall  // Otherwise, check whether the static type does not require
1266f85e193739c953358c865005855253af4f68a497John McCall  // retaining.  This currently only triggers for Class (possibly
1267f85e193739c953358c865005855253af4f68a497John McCall  // protocol-qualifed, and arrays thereof).
1268f85e193739c953358c865005855253af4f68a497John McCall  } else if (type->isObjCARCImplicitlyUnretainedType()) {
1269f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
12705b76f373d23cc3b292ecf523349aaaa388eea375Argyrios Kyrtzidis
1271ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // If we are in an unevaluated context, like sizeof, skip adding a
1272ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // qualification.
127371f55f771794674a410171dbf3cb5dbedf95d033David Blaikie  } else if (S.isUnevaluatedContext()) {
1274ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    return type;
1275f85e193739c953358c865005855253af4f68a497John McCall
1276e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // If that failed, give an error and recover using __strong.  __strong
1277e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // is the option most likely to prevent spurious second-order diagnostics,
1278e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // like when binding a reference to a field.
1279f85e193739c953358c865005855253af4f68a497John McCall  } else {
1280f85e193739c953358c865005855253af4f68a497John McCall    // These types can show up in private ivars in system headers, so
1281f85e193739c953358c865005855253af4f68a497John McCall    // we need this to not be an error in those cases.  Instead we
1282f85e193739c953358c865005855253af4f68a497John McCall    // want to delay.
1283f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1284ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.DelayedDiagnostics.add(
1285ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman          sema::DelayedDiagnostic::makeForbiddenType(loc,
1286ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman              diag::err_arc_indirect_no_ownership, type, isReference));
1287f85e193739c953358c865005855253af4f68a497John McCall    } else {
1288ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference;
1289f85e193739c953358c865005855253af4f68a497John McCall    }
1290e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall    implicitLifetime = Qualifiers::OCL_Strong;
1291f85e193739c953358c865005855253af4f68a497John McCall  }
1292f85e193739c953358c865005855253af4f68a497John McCall  assert(implicitLifetime && "didn't infer any lifetime!");
1293f85e193739c953358c865005855253af4f68a497John McCall
1294f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers qs;
1295f85e193739c953358c865005855253af4f68a497John McCall  qs.addObjCLifetime(implicitLifetime);
1296f85e193739c953358c865005855253af4f68a497John McCall  return S.Context.getQualifiedType(type, qs);
1297f85e193739c953358c865005855253af4f68a497John McCall}
1298f85e193739c953358c865005855253af4f68a497John McCall
1299cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
1300cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1301cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
1302cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1303cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1304cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
1305cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
1306cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1307cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
1308cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1309cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1310cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
1311cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
13122865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
1313cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
1314cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
1315cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
1316cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
1317ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1318cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1319cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1320cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1321c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
132292e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
1323f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build pointers to unqualified pointers.
13244e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1325f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false);
1326f85e193739c953358c865005855253af4f68a497John McCall
1327cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
13282865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
1329cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1330cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1331cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
1332cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1333cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
1334cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1335cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1336cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
1337cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
1338cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1339cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
1340cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1341cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1342cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
1343cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
134454e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
13452865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
134654e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
134791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  assert(Context.getCanonicalType(T) != Context.OverloadTy &&
13489625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
134991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
135069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // C++0x [dcl.ref]p6:
135191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   If a typedef (7.1.3), a type template-parameter (14.3.1), or a
135291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
135391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   type T, an attempt to create the type "lvalue reference to cv TR" creates
135491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   the type "lvalue reference to T", while an attempt to create the type
135569d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   "rvalue reference to cv TR" creates the type TR.
135654e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
135754e14c4db764c0636160d26c5bbf491637c83a76John McCall
135854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
135954e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
136054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
136154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
136254e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
136354e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
136454e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
136554e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
136654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
136754e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
136854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
136969d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // collapsing of references-to-references as described in C++0x.
137069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // DR 106 and 540 introduce reference-collapsing into C++98/03.
1371cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1372cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
137333a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
1374cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
1375cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
1376cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
1377cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1378cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1379cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1380f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build references to unqualified pointers.
13814e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1382f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true);
1383f85e193739c953358c865005855253af4f68a497John McCall
1384cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
13857c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
13862865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
13872865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
1388cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1389cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1390e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// Check whether the specified array size makes the array type a VLA.  If so,
1391e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// return true, if not, return the size of the array in SizeVal.
1392282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smithstatic bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal) {
1393282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode
1394282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // (like gnu99, but not c99) accept any evaluatable value as an extension.
1395ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  class VLADiagnoser : public Sema::VerifyICEDiagnoser {
1396ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  public:
1397ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    VLADiagnoser() : Sema::VerifyICEDiagnoser(true) {}
139891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1399ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) {
1400ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
140191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1402ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseFold(Sema &S, SourceLocation Loc, SourceRange SR) {
1403ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      S.Diag(Loc, diag::ext_vla_folded_to_constant) << SR;
1404ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
1405ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  } Diagnoser;
140691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1407ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  return S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser,
1408ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                           S.LangOpts.GNUMode).isInvalid();
1409e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner}
1410e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1411e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1412cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
1413cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1414cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
1415cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1416cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
14171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
14181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
1419cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1420efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Brackets The range from the opening '[' to the closing ']'.
1421cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1422cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
1423cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1424cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1425cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
1426cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
1427cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1428cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
14297e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
14300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
14317e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
14324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
1433138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
1434138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
143591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   type, the (possibly cv-qualified) type void, a function type or an
1436138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
1437138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
1438bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    // C++ [dcl.array]p3:
1439bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   When several "array of" specifications are adjacent, [...] only the
1440bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   first of the constant expressions that specify the bounds of the arrays
1441bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   may be omitted.
1442bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //
1443138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
1444138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
1445138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
1446138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
1447138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1448138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
144991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1450bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    if (T->isVoidType() || T->isIncompleteArrayType()) {
1451923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1452923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1453923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
145491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
145591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (RequireNonAbstractType(Brackets.getBegin(), T,
1456138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
1457138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
145891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1459923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
1460138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1461138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
1462923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
1463923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
1464923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1465923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
1466cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1467cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
1468cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
1469ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1470cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1471cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
14721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14736217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
1474cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
1475cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
1476cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
1477cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
1478c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
1479c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1480c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
1481cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
14821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1483806054db6653d29cb0d9692df3612cbcd03d0530John McCall  // Do placeholder conversions on the array size expression.
1484806054db6653d29cb0d9692df3612cbcd03d0530John McCall  if (ArraySize && ArraySize->hasPlaceholderType()) {
1485806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ExprResult Result = CheckPlaceholderExpr(ArraySize);
1486806054db6653d29cb0d9692df3612cbcd03d0530John McCall    if (Result.isInvalid()) return QualType();
1487806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ArraySize = Result.take();
1488806054db6653d29cb0d9692df3612cbcd03d0530John McCall  }
1489806054db6653d29cb0d9692df3612cbcd03d0530John McCall
14905e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
1491429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ArraySize && !ArraySize->isRValue()) {
1492429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult Result = DefaultLvalueConversion(ArraySize);
1493429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Result.isInvalid())
1494429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return QualType();
1495429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
1496429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ArraySize = Result.take();
1497429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
14985e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1499cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
1500282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // C++11 allows contextual conversions to such types.
150180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (!getLangOpts().CPlusPlus11 &&
1502282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      ArraySize && !ArraySize->isTypeDependent() &&
15031274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1504cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1505cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
1506cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1507cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1508282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
15092767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
1510cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
1511f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
15127e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
1513f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
1514f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
1515ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
15167e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
1517282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  } else if ((!T->isDependentType() && !T->isIncompleteType() &&
1518282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith              !T->isConstantSizeType()) ||
1519282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith             isArraySizeVLA(*this, ArraySize, ConstVal)) {
1520282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // Even in C++11, don't allow contextual conversions in the array bound
1521282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // of a VLA.
152280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (getLangOpts().CPlusPlus11 &&
1523282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1524282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1525282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        << ArraySize->getType() << ArraySize->getSourceRange();
1526282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      return QualType();
1527282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    }
1528282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
1529e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with an element type that has a non-constant-size is a VLA.
1530e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with a non-ICE size is a VLA.  We accept any expression
1531e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // that we can fold to a non-zero positive value as an extension.
15327e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1533cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
1534cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1535cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
1536923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
1537b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
1538b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1539b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1540b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
1541b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1542b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
1543923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1544923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1545923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
154602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
154702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
154891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      Diag(ArraySize->getLocStart(),
154902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
155002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
1551923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
155220cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne
155320cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      if (ASM == ArrayType::Static) {
155420cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        Diag(ArraySize->getLocStart(),
155520cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne             diag::warn_typecheck_zero_static_array_size)
155620cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne          << ArraySize->getSourceRange();
155720cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        ASM = ArrayType::Normal;
155820cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      }
155991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
1560152b4e4652baedfceba1cd8115515629225e713fManuel Klimek               !T->isIncompleteType()) {
156191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // Is the array too large?
15622767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
15632767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
1564ee2f8f2f7c4eacfa305a29fcd916d63c650ca847Richard Trieu      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) {
15652767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
15662767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
15672767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
1568ee2f8f2f7c4eacfa305a29fcd916d63c650ca847Richard Trieu        return QualType();
1569ee2f8f2f7c4eacfa305a29fcd916d63c650ca847Richard Trieu      }
15701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
157191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
157246a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
1573cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1574617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly
1575617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  // OpenCL v1.2 s6.9.d: variable length arrays are not supported.
1576617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  if (getLangOpts().OpenCL && T->isVariableArrayType()) {
1577617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    Diag(Loc, diag::err_opencl_vla);
1578617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    return QualType();
1579617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  }
1580af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
15814e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().C99) {
15820fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
15830fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
158439b0e269dc8895ecc0f92f08126d3082b2a837a8Richard Smith      // FIXME: C++1y allows this.
1585f85e193739c953358c865005855253af4f68a497John McCall      QualType BaseT = Context.getBaseElementType(T);
158691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      if (!T->isDependentType() &&
15873c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT.isPODType(Context) &&
15883c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT->isObjCLifetimeType()) {
15890fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
1590f85e193739c953358c865005855253af4f68a497John McCall          << BaseT;
15910fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
159291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
1593a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
1594a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
1595a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
1596a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
1597a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
15980fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
15990fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
160039b0e269dc8895ecc0f92f08126d3082b2a837a8Richard Smith        Diag(Loc, getLangOpts().CPlusPlus1y
160139b0e269dc8895ecc0f92f08126d3082b2a837a8Richard Smith                      ? diag::warn_cxx11_compat_array_of_runtime_bound
160239b0e269dc8895ecc0f92f08126d3082b2a837a8Richard Smith                      : diag::ext_vla);
16030fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
1604d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith      Diag(Loc,
16054e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie           getLangOpts().CPlusPlus? diag::err_c99_array_usage_cxx
1606d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith                                     : diag::ext_c99_array_usage) << ASM;
1607cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1608cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1609630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko  if (T->isVariableArrayType()) {
1610630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko    // Warn about VLAs for -Wvla.
1611630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko    Diag(Loc, diag::warn_vla_used);
1612630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko  }
1613630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko
1614cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
1615cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
16169cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
16179cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
16189cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
16199cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
16209ae2f076ca5ab1feb3ba95629099ec2319833701John McCallQualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
16219cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
16229cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
16239cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
16241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
16259cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
16269cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
16279cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
16289cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
16299cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
16309ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
16319cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
16329ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
16339f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      Diag(AttrLoc, diag::err_attribute_argument_type)
16349f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman        << "ext_vector_type" << AANT_ArgumentIntegerConstant
16359f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman        << ArraySize->getSourceRange();
16369cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
16379cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
16381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
16409cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
16411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
16421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16439cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
16449cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
16459ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      << ArraySize->getSourceRange();
16469cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
16479cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
16481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16491652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    if (VectorType::isVectorSizeTooLarge(vectorSize)) {
16501652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      Diag(AttrLoc, diag::err_attribute_size_too_large)
16511652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman        << ArraySize->getSourceRange();
16521652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      return QualType();
16531652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    }
16541652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman
16554ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    return Context.getExtVectorType(T, vectorSize);
16561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
16571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16589ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
16599cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
16601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1661ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedmanbool Sema::CheckFunctionReturnType(QualType T, SourceLocation Loc) {
1662724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
166391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    Diag(Loc, diag::err_func_returning_array_function)
166458408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
1665ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    return true;
1666724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1667aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1668aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // Functions cannot return half FP.
1669aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  if (T->isHalfType()) {
1670aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 <<
1671aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      FixItHint::CreateInsertion(Loc, "*");
1672ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    return true;
1673ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  }
1674ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
1675ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  // Methods cannot return interface types. All ObjC objects are
1676ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  // passed by reference.
1677ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  if (T->isObjCObjectType()) {
1678ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    Diag(Loc, diag::err_object_cannot_be_passed_returned_by_value) << 0 << T;
1679ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    return 0;
1680aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  }
1681aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1682ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  return false;
1683ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman}
1684ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
1685ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli FriedmanQualType Sema::BuildFunctionType(QualType T,
1686ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman                                 llvm::MutableArrayRef<QualType> ParamTypes,
1687ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
1688ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman                                 const FunctionProtoType::ExtProtoInfo &EPI) {
1689724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
1690ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
1691ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  Invalid |= CheckFunctionReturnType(T, Loc);
1692ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
1693bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  for (unsigned Idx = 0, Cnt = ParamTypes.size(); Idx < Cnt; ++Idx) {
1694aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    // FIXME: Loc is too inprecise here, should use proper locations for args.
1695c910d4cfa5042f2c9da1eb4e0b6ed59240c0eeeeReid Kleckner    QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]);
16962dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
1697724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
1698724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
1699aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    } else if (ParamType->isHalfType()) {
1700aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Disallow half FP arguments.
1701aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 <<
1702aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        FixItHint::CreateInsertion(Loc, "*");
1703aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Invalid = true;
1704724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
1705cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
170654e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
1707724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1708724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1709724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
1710724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1711724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1712bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  return Context.getFunctionType(T, ParamTypes, EPI);
1713724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
17141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1715949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
1716949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1717949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
1718949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
1719949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
1720949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
1721949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1722949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
1723949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
17241eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
17252865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
1726949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
1727949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
1728949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
1729949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
1730949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
1731949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1732949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
1733949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
1734949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1735949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
1736949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
1737949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
1738949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1739949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1740737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
1741949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
1742949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
17438d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
1744ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
1745949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1746949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1747949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1748949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
1749949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1750949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
1751949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1752949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1753949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1754949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
1755949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1756949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1757949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1758949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1759b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // C++ allows the class type in a member pointer to be an incomplete type.
1760b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // In the Microsoft ABI, the size of the member pointer can vary
1761b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // according to the class type, which means that we really need a
1762b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // complete type if possible, which means we need to instantiate templates.
1763b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  //
176484e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  // If template instantiation fails or the type is just incomplete, we have to
176584e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  // add an extra slot to the member pointer.  Yes, this does cause problems
176684e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  // when passing pointers between TUs that disagree about the size.
176784e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  if (Context.getTargetInfo().getCXXABI().isMicrosoft()) {
176884e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner    CXXRecordDecl *RD = Class->getAsCXXRecordDecl();
1769e93e2552e76ab704ec85919cc2c76f02b8b081eeReid Kleckner    if (RD && !RD->hasAttr<MSInheritanceAttr>()) {
177084e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // Lock in the inheritance model on the first use of a member pointer.
177184e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // Otherwise we may disagree about the size at different points in the TU.
177284e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // FIXME: MSVC picks a model on the first use that needs to know the size,
177384e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // rather than on the first mention of the type, e.g. typedefs.
1774e93e2552e76ab704ec85919cc2c76f02b8b081eeReid Kleckner      if (RequireCompleteType(Loc, Class, 0) && !RD->isBeingDefined()) {
177584e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        // We know it doesn't have an attribute and it's incomplete, so use the
177684e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        // unspecified inheritance model.  If we're in the record body, we can
177784e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        // figure out the inheritance model.
177884e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(),
177984e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner             E = RD->redecls_end(); I != E; ++I) {
178084e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner          I->addAttr(::new (Context) UnspecifiedInheritanceAttr(
178184e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner              RD->getSourceRange(), Context));
178284e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        }
178384e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      }
178484e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner    }
178584e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  }
1786679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos
17872865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
1788949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
17891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17909a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
17919a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
17929a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
17939a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
1794efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Loc The source location, used for diagnostics.
17959a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
17969a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
17979a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
17989a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
17999a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
18009a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
180191cbbbf506c892a26d4301e2b3ccd377b0938817Chad RosierQualType Sema::BuildBlockPointerType(QualType T,
18021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
18039a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
18040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
18059a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
18069a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
18079a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
18081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18092865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
18109a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
18119a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
1812b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1813b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
18143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
1815a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
18163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
18173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
18183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1819a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
1820f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
1821e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
1822a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
1823e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
18241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1825a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
1826e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
1827e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
1828e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
1829a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
1830a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
1831a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            unsigned chunkIndex);
1832a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis
1833f85e193739c953358c865005855253af4f68a497John McCall/// Given that this is the declaration of a parameter under ARC,
1834f85e193739c953358c865005855253af4f68a497John McCall/// attempt to infer attributes and such for pointer-to-whatever
1835f85e193739c953358c865005855253af4f68a497John McCall/// types.
1836f85e193739c953358c865005855253af4f68a497John McCallstatic void inferARCWriteback(TypeProcessingState &state,
1837f85e193739c953358c865005855253af4f68a497John McCall                              QualType &declSpecType) {
1838f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
1839f85e193739c953358c865005855253af4f68a497John McCall  Declarator &declarator = state.getDeclarator();
1840f85e193739c953358c865005855253af4f68a497John McCall
1841f85e193739c953358c865005855253af4f68a497John McCall  // TODO: should we care about decl qualifiers?
1842f85e193739c953358c865005855253af4f68a497John McCall
1843f85e193739c953358c865005855253af4f68a497John McCall  // Check whether the declarator has the expected form.  We walk
1844f85e193739c953358c865005855253af4f68a497John McCall  // from the inside out in order to make the block logic work.
1845f85e193739c953358c865005855253af4f68a497John McCall  unsigned outermostPointerIndex = 0;
1846f85e193739c953358c865005855253af4f68a497John McCall  bool isBlockPointer = false;
1847f85e193739c953358c865005855253af4f68a497John McCall  unsigned numPointers = 0;
1848f85e193739c953358c865005855253af4f68a497John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
1849f85e193739c953358c865005855253af4f68a497John McCall    unsigned chunkIndex = i;
1850f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex);
1851f85e193739c953358c865005855253af4f68a497John McCall    switch (chunk.Kind) {
1852f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Paren:
1853f85e193739c953358c865005855253af4f68a497John McCall      // Ignore parens.
1854f85e193739c953358c865005855253af4f68a497John McCall      break;
1855f85e193739c953358c865005855253af4f68a497John McCall
1856f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Reference:
1857f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Pointer:
1858f85e193739c953358c865005855253af4f68a497John McCall      // Count the number of pointers.  Treat references
1859f85e193739c953358c865005855253af4f68a497John McCall      // interchangeably as pointers; if they're mis-ordered, normal
1860f85e193739c953358c865005855253af4f68a497John McCall      // type building will discover that.
1861f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1862f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1863f85e193739c953358c865005855253af4f68a497John McCall      break;
1864f85e193739c953358c865005855253af4f68a497John McCall
1865f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::BlockPointer:
1866f85e193739c953358c865005855253af4f68a497John McCall      // If we have a pointer to block pointer, that's an acceptable
1867f85e193739c953358c865005855253af4f68a497John McCall      // indirect reference; anything else is not an application of
1868f85e193739c953358c865005855253af4f68a497John McCall      // the rules.
1869f85e193739c953358c865005855253af4f68a497John McCall      if (numPointers != 1) return;
1870f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1871f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1872f85e193739c953358c865005855253af4f68a497John McCall      isBlockPointer = true;
1873f85e193739c953358c865005855253af4f68a497John McCall
1874f85e193739c953358c865005855253af4f68a497John McCall      // We don't care about pointer structure in return values here.
1875f85e193739c953358c865005855253af4f68a497John McCall      goto done;
1876f85e193739c953358c865005855253af4f68a497John McCall
1877f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Array: // suppress if written (id[])?
1878f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Function:
1879f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::MemberPointer:
1880f85e193739c953358c865005855253af4f68a497John McCall      return;
1881f85e193739c953358c865005855253af4f68a497John McCall    }
1882f85e193739c953358c865005855253af4f68a497John McCall  }
1883f85e193739c953358c865005855253af4f68a497John McCall done:
1884f85e193739c953358c865005855253af4f68a497John McCall
1885f85e193739c953358c865005855253af4f68a497John McCall  // If we have *one* pointer, then we want to throw the qualifier on
1886f85e193739c953358c865005855253af4f68a497John McCall  // the declaration-specifiers, which means that it needs to be a
1887f85e193739c953358c865005855253af4f68a497John McCall  // retainable object type.
1888f85e193739c953358c865005855253af4f68a497John McCall  if (numPointers == 1) {
1889f85e193739c953358c865005855253af4f68a497John McCall    // If it's not a retainable object type, the rule doesn't apply.
1890f85e193739c953358c865005855253af4f68a497John McCall    if (!declSpecType->isObjCRetainableType()) return;
1891f85e193739c953358c865005855253af4f68a497John McCall
1892f85e193739c953358c865005855253af4f68a497John McCall    // If it already has lifetime, don't do anything.
1893f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType.getObjCLifetime()) return;
1894f85e193739c953358c865005855253af4f68a497John McCall
1895f85e193739c953358c865005855253af4f68a497John McCall    // Otherwise, modify the type in-place.
1896f85e193739c953358c865005855253af4f68a497John McCall    Qualifiers qs;
189791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1898f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType->isObjCARCImplicitlyUnretainedType())
1899f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
1900f85e193739c953358c865005855253af4f68a497John McCall    else
1901f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing);
1902f85e193739c953358c865005855253af4f68a497John McCall    declSpecType = S.Context.getQualifiedType(declSpecType, qs);
1903f85e193739c953358c865005855253af4f68a497John McCall
1904f85e193739c953358c865005855253af4f68a497John McCall  // If we have *two* pointers, then we want to throw the qualifier on
1905f85e193739c953358c865005855253af4f68a497John McCall  // the outermost pointer.
1906f85e193739c953358c865005855253af4f68a497John McCall  } else if (numPointers == 2) {
1907f85e193739c953358c865005855253af4f68a497John McCall    // If we don't have a block pointer, we need to check whether the
1908f85e193739c953358c865005855253af4f68a497John McCall    // declaration-specifiers gave us something that will turn into a
1909f85e193739c953358c865005855253af4f68a497John McCall    // retainable object pointer after we slap the first pointer on it.
1910f85e193739c953358c865005855253af4f68a497John McCall    if (!isBlockPointer && !declSpecType->isObjCObjectType())
1911f85e193739c953358c865005855253af4f68a497John McCall      return;
1912f85e193739c953358c865005855253af4f68a497John McCall
1913f85e193739c953358c865005855253af4f68a497John McCall    // Look for an explicit lifetime attribute there.
1914f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex);
19151c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis    if (chunk.Kind != DeclaratorChunk::Pointer &&
19161c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis        chunk.Kind != DeclaratorChunk::BlockPointer)
19171c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis      return;
1918f85e193739c953358c865005855253af4f68a497John McCall    for (const AttributeList *attr = chunk.getAttrs(); attr;
1919f85e193739c953358c865005855253af4f68a497John McCall           attr = attr->getNext())
19208e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt      if (attr->getKind() == AttributeList::AT_ObjCOwnership)
1921f85e193739c953358c865005855253af4f68a497John McCall        return;
1922f85e193739c953358c865005855253af4f68a497John McCall
1923a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis    transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing,
1924a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                          outermostPointerIndex);
1925f85e193739c953358c865005855253af4f68a497John McCall
1926f85e193739c953358c865005855253af4f68a497John McCall  // Any other number of pointers/references does not trigger the rule.
1927f85e193739c953358c865005855253af4f68a497John McCall  } else return;
1928f85e193739c953358c865005855253af4f68a497John McCall
1929f85e193739c953358c865005855253af4f68a497John McCall  // TODO: mark whether we did this inference?
1930f85e193739c953358c865005855253af4f68a497John McCall}
1931f85e193739c953358c865005855253af4f68a497John McCall
19328c952cd40ccec9d720931f27e7d722fed207d536Richard Smithstatic void diagnoseIgnoredQualifiers(
19338c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    Sema &S, unsigned Quals,
19348c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation FallbackLoc,
19358c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation ConstQualLoc = SourceLocation(),
19368c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation VolatileQualLoc = SourceLocation(),
19378c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation RestrictQualLoc = SourceLocation(),
19388c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation AtomicQualLoc = SourceLocation()) {
1939eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith  if (!Quals)
1940eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith    return;
1941eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith
19428c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  const SourceManager &SM = S.getSourceManager();
19438c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19448c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  struct Qual {
19458c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    unsigned Mask;
19468c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    const char *Name;
19478c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation Loc;
19488c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  } const QualKinds[4] = {
19498c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_const, "const", ConstQualLoc },
19508c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_volatile, "volatile", VolatileQualLoc },
19518c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_restrict, "restrict", RestrictQualLoc },
19528c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_atomic, "_Atomic", AtomicQualLoc }
19538c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  };
19548c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
195530d237556fdd29b5075c990da953116225b95d9dRobert Wilhelm  SmallString<32> QualStr;
1956d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  unsigned NumQuals = 0;
1957d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  SourceLocation Loc;
19588c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  FixItHint FixIts[4];
19598c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19608c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // Build a string naming the redundant qualifiers.
19618c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  for (unsigned I = 0; I != 4; ++I) {
19628c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    if (Quals & QualKinds[I].Mask) {
19638c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if (!QualStr.empty()) QualStr += ' ';
19648c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      QualStr += QualKinds[I].Name;
19658c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19668c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // If we have a location for the qualifier, offer a fixit.
19678c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      SourceLocation QualLoc = QualKinds[I].Loc;
19688c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if (!QualLoc.isInvalid()) {
19698c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        FixIts[NumQuals] = FixItHint::CreateRemoval(QualLoc);
19708c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        if (Loc.isInvalid() || SM.isBeforeInTranslationUnit(QualLoc, Loc))
19718c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          Loc = QualLoc;
19728c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      }
1973d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
19748c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      ++NumQuals;
19758c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
19768c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  }
1977d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
19788c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  S.Diag(Loc.isInvalid() ? FallbackLoc : Loc, diag::warn_qual_return_type)
19798c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    << QualStr << NumQuals << FixIts[0] << FixIts[1] << FixIts[2] << FixIts[3];
19808c952cd40ccec9d720931f27e7d722fed207d536Richard Smith}
1981a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg
19828c952cd40ccec9d720931f27e7d722fed207d536Richard Smith// Diagnose pointless type qualifiers on the return type of a function.
19838c952cd40ccec9d720931f27e7d722fed207d536Richard Smithstatic void diagnoseIgnoredFunctionQualifiers(Sema &S, QualType RetTy,
19848c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                              Declarator &D,
19858c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                              unsigned FunctionChunkIndex) {
19868c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  if (D.getTypeObject(FunctionChunkIndex).Fun.hasTrailingReturnType()) {
19878c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    // FIXME: TypeSourceInfo doesn't preserve location information for
19888c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    // qualifiers.
1989eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith    diagnoseIgnoredQualifiers(S, RetTy.getLocalCVRQualifiers(),
19908c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                              D.getIdentifierLoc());
19918c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    return;
1992d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
19938c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19948c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  for (unsigned OuterChunkIndex = FunctionChunkIndex + 1,
19958c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                End = D.getNumTypeObjects();
19968c952cd40ccec9d720931f27e7d722fed207d536Richard Smith       OuterChunkIndex != End; ++OuterChunkIndex) {
19978c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    DeclaratorChunk &OuterChunk = D.getTypeObject(OuterChunkIndex);
19988c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    switch (OuterChunk.Kind) {
19998c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Paren:
20008c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      continue;
20018c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
20028c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Pointer: {
20038c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      DeclaratorChunk::PointerTypeInfo &PTI = OuterChunk.Ptr;
20048c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      diagnoseIgnoredQualifiers(
20058c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          S, PTI.TypeQuals,
20068c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation(),
20078c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
20088c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
20098c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
20108c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.AtomicQualLoc));
20118c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      return;
20128c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
20138c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
20148c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Function:
20158c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::BlockPointer:
20168c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Reference:
20178c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Array:
20188c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::MemberPointer:
20198c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // FIXME: We can't currently provide an accurate source location and a
20208c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // fix-it hint for these.
2021eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith      unsigned AtomicQual = RetTy->isAtomicType() ? DeclSpec::TQ_atomic : 0;
20228c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      diagnoseIgnoredQualifiers(S, RetTy.getCVRQualifiers() | AtomicQual,
20238c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                D.getIdentifierLoc());
20248c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      return;
20258c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
20268c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
20278c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    llvm_unreachable("unknown declarator chunk kind");
20284cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
2029d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
20308c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // If the qualifiers come from a conversion function type, don't diagnose
20318c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // them -- they're not necessarily redundant, since such a conversion
20328c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // operator can be explicitly called as "x.operator const int()".
20338c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  if (D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId)
20348c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    return;
2035d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
20368c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // Just parens all the way out to the decl specifiers. Diagnose any qualifiers
20378c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // which are present there.
2038eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith  diagnoseIgnoredQualifiers(S, D.getDeclSpec().getTypeQualifiers(),
20398c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getIdentifierLoc(),
20408c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getConstSpecLoc(),
20418c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getVolatileSpecLoc(),
20428c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getRestrictSpecLoc(),
20438c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getAtomicSpecLoc());
2044d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth}
2045d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
20468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
20478cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                             TypeSourceInfo *&ReturnTypeInfo) {
20488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &SemaRef = state.getSema();
20498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
2050930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
20518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ReturnTypeInfo = 0;
2052711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
20538cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // The TagDecl owned by the DeclSpec.
20548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TagDecl *OwnedTagDecl = 0;
20558999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl
205660e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  bool ContainsPlaceholderType = false;
205760e141e1f87211ca831de6821003d80fe20a06f3Richard Smith
20583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
205998a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian  case UnqualifiedId::IK_ImplicitSelfParam:
20603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
20618999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  case UnqualifiedId::IK_Identifier:
20620486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
20633f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
20648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = ConvertDeclSpecToType(state);
206560e141e1f87211ca831de6821003d80fe20a06f3Richard Smith    ContainsPlaceholderType = D.getDeclSpec().containsPlaceholderType();
206691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2067591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
2068d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
206915987970eeaa1842c29ec8797affd1c1dea05585Abramo Bagnara      // Owned declaration is embedded in declarator.
2070d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl->setEmbeddedInDeclarator(true);
2071591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
2072930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
2073930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
20743f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
20750efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
20763f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
2077930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
207891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // "void" instead.
20798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = SemaRef.Context.VoidTy;
2080a78a640c5f59720f2c2b8034eca4fbf8525d9026Rafael Espindola    if (AttributeList *attrs = D.getDeclSpec().getAttributes().getList())
2081f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclSpec, attrs);
2082930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
208348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
208448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
208548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
208648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
208791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId,
20888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  &ReturnTypeInfo);
208960e141e1f87211ca831de6821003d80fe20a06f3Richard Smith    ContainsPlaceholderType = T->getContainedAutoType();
209048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
2091930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
2092dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
2093711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
2094711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
2095711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2096d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
2097d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // In C++11, a function declarator using 'auto' must have a trailing return
20988110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // type (this is checked later) and we can skip this. In other languages
20998110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // using auto, we need to check regardless.
210060e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  if (ContainsPlaceholderType &&
210180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      (!SemaRef.getLangOpts().CPlusPlus11 || !D.isFunctionDeclarator())) {
2102baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
21031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2104baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
2105baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
2106b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("K&R type lists aren't allowed in C++");
2107f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2108f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman      llvm_unreachable("Can't specify a type specifier in lambda grammar");
2109cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2110cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
2111baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
2112152b4e4652baedfceba1cd8115515629225e713fManuel Klimek      Error = 0; // Function prototype
2113baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2114baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
21157a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)
21167a614d8380297fcd2bc23986241905d97222948cRichard Smith        break;
21178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) {
2118eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie      case TTK_Enum: llvm_unreachable("unhandled tag kind");
2119465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
2120465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
2121465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
21226666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      case TTK_Interface: Error = 4; /* Interface member */ break;
21231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
2124baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2125baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
212617b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
21276666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 5; // Exception declaration
2128baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2129baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
21306666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 6; // Template parameter
2131baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2132baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
21336666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 7; // Block literal
213434b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
213534b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TemplateTypeArgContext:
21366666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 8; // Template type argument
213734b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
2138162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
21393e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
21406666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 10; // Type alias
2141162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      break;
21427796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
214360e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      if (!SemaRef.getLangOpts().CPlusPlus1y)
214460e141e1f87211ca831de6821003d80fe20a06f3Richard Smith        Error = 11; // Function return type
21457796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
214614f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    case Declarator::ConversionIdContext:
214760e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      if (!SemaRef.getLangOpts().CPlusPlus1y)
214860e141e1f87211ca831de6821003d80fe20a06f3Richard Smith        Error = 12; // conversion-type-id
214914f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      break;
215034b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TypeNameContext:
215114f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      Error = 13; // Generic
2152baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2153baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
2154baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
2155baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
2156baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
21570b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
2158baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2159baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
2160baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
2161ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith    if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
21626666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 9;
2163ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith
21648110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    // In Objective-C it is an error to use 'auto' on a function declarator.
21658110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    if (D.isFunctionDeclarator())
21666666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11;
21678110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith
2168d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.spec.auto]p2: 'auto' is always fine if the declarator
2169e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // contains a trailing return type. That is only legal at the outermost
2170e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // level. Check all declarator chunks (outermost first) anyway, to give
2171e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // better diagnostics.
217280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (SemaRef.getLangOpts().CPlusPlus11 && Error != -1) {
2173e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2174e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned chunkIndex = e - i - 1;
2175e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        state.setCurrentChunkIndex(chunkIndex);
2176e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2177e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (DeclType.Kind == DeclaratorChunk::Function) {
2178e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
217954655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (FTI.hasTrailingReturnType()) {
2180e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Error = -1;
2181e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            break;
2182e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          }
2183e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        }
2184e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2185e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    }
2186e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
218714f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    SourceRange AutoRange = D.getDeclSpec().getTypeSpecTypeLoc();
218814f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    if (D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId)
218914f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      AutoRange = D.getName().getSourceRange();
219014f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith
2191baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
219214f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      SemaRef.Diag(AutoRange.getBegin(), diag::err_auto_not_allowed)
219314f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith        << Error << AutoRange;
21948cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = SemaRef.Context.IntTy;
2195baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
21960aa86c0463a881be85fd34e04c7de3379997621dRichard Smith    } else
219714f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      SemaRef.Diag(AutoRange.getBegin(),
219814f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith                   diag::warn_cxx98_compat_auto_type_specifier)
219914f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith        << AutoRange;
2200baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
22018cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
22024e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().CPlusPlus &&
22035e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall      OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) {
22048cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // Check the contexts where C++ forbids the declaration of a new class
22058cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // or enumeration in a type-specifier-seq.
22068cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    switch (D.getContext()) {
22077796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
22087796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // Class and enumeration definitions are syntactically not allowed in
22097796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // trailing return types.
22107796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      llvm_unreachable("parser should not have allowed this");
22117796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
22128cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::FileContext:
22138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::MemberContext:
22148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockContext:
22158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ForContext:
22168cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockLiteralContext:
2217f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2218d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // C++11 [dcl.type]p3:
22198cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   A type-specifier-seq shall not define a class or enumeration unless
22208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   it appears in the type-id of an alias-declaration (7.1.3) that is not
22218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   the declaration of a template-declaration.
22228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasDeclContext:
22238cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasTemplateContext:
22258cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_alias_template)
22278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2228601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22298cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22308cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TypeNameContext:
223114f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    case Declarator::ConversionIdContext:
22328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateParamContext:
22338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXNewContext:
22348cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXCatchContext:
22358cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ObjCCatchContext:
22368cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateTypeArgContext:
22378cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22388cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_type_specifier)
22398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2240601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22428cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::PrototypeContext:
2243cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2244cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
22458cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::KNRTypeListContext:
22468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ [dcl.fct]p6:
22478cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   Types shall not be defined in return or parameter types.
22488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_param_type)
22508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2251601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22538cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ConditionContext:
22548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ 6.4p2:
22558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // The type-specifier-seq shall not contain typedef and shall not declare
22568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // a new class or enumeration.
22578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22588cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_condition);
2259601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22618cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    }
22628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  }
22638cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
22648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return T;
22658cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
22668cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2267a08c2fb74ef823c185619ecc532f8fced6a1982fBenjamin Kramerstatic std::string getFunctionQualifiersAsString(const FunctionProtoType *FnTy){
2268d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  std::string Quals =
2269d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
2270d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2271d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (FnTy->getRefQualifier()) {
2272d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_None:
2273d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2274d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2275d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_LValue:
2276d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2277d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2278d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += '&';
2279d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2280d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2281d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_RValue:
2282d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2283d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2284d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += "&&";
2285d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2286d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2287d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2288d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  return Quals;
2289d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2290d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2291d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// Check that the function type T, which has a cv-qualifier or a ref-qualifier,
2292d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// can be contained within the declarator chunk DeclType, and produce an
2293d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// appropriate diagnostic if not.
2294d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smithstatic void checkQualifiedFunction(Sema &S, QualType T,
2295d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                   DeclaratorChunk &DeclType) {
2296d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++98 [dcl.fct]p4 / C++11 [dcl.fct]p6: a function type with a
2297d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // cv-qualifier or a ref-qualifier can only appear at the topmost level
2298d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // of a type.
2299d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  int DiagKind = -1;
2300d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (DeclType.Kind) {
2301d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Paren:
2302d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::MemberPointer:
2303d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases are permitted.
2304d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2305d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Array:
2306d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Function:
2307d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases don't allow function types at all; no need to diagnose the
2308d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // qualifiers separately.
2309d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2310d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::BlockPointer:
2311d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 0;
2312d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2313d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Pointer:
2314d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 1;
2315d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2316d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Reference:
2317d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 2;
2318d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2319d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2320d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2321d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  assert(DiagKind != -1);
2322d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  S.Diag(DeclType.Loc, diag::err_compound_qualified_function_type)
2323d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << DiagKind << isa<FunctionType>(T.IgnoreParens()) << T
2324d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << getFunctionQualifiersAsString(T->castAs<FunctionProtoType>());
2325d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2326d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2327b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// Produce an approprioate diagnostic for an ambiguity between a function
2328b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// declarator and a C++ direct-initializer.
2329b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smithstatic void warnAboutAmbiguousFunction(Sema &S, Declarator &D,
2330b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith                                       DeclaratorChunk &DeclType, QualType RT) {
2331b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2332b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  assert(FTI.isAmbiguous && "no direct-initializer / function ambiguity");
2333b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2334b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // If the return type is void there is no ambiguity.
2335b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isVoidType())
2336b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2337b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2338b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a non-class type can have at most one argument.
2339b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!RT->isRecordType() && FTI.NumArgs > 1)
2340b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2341b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2342b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a reference must have exactly one argument.
2343b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isReferenceType() && FTI.NumArgs != 1)
2344b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2345b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2346b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Only warn if this declarator is declaring a function at block scope, and
2347b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // doesn't have a storage class (such as 'extern') specified.
2348b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!D.isFunctionDeclarator() ||
2349b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      D.getFunctionDefinitionKind() != FDK_Declaration ||
2350b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      !S.CurContext->isFunctionOrMethod() ||
2351d2615cc53b916e8aae45783ca7113b93de515ce3Rafael Espindola      D.getDeclSpec().getStorageClassSpec()
2352b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        != DeclSpec::SCS_unspecified)
2353b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2354b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2355b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Inside a condition, a direct initializer is not permitted. We allow one to
2356b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // be parsed in order to give better diagnostics in condition parsing.
2357b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (D.getContext() == Declarator::ConditionContext)
2358b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2359b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2360b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  SourceRange ParenRange(DeclType.Loc, DeclType.EndLoc);
2361b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2362d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  S.Diag(DeclType.Loc,
2363d64effc4e31044c05d6e4400150edb26e914983aRichard Smith         FTI.NumArgs ? diag::warn_parens_disambiguated_as_function_declaration
2364d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                     : diag::warn_empty_parens_are_function_decl)
2365d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    << ParenRange;
2366d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2367d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // If the declaration looks like:
2368d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   T var1,
2369d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   f();
2370d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // and name lookup finds a function named 'f', then the ',' was
2371d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // probably intended to be a ';'.
2372d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (!D.isFirstDeclarator() && D.getIdentifier()) {
2373d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Comma(D.getCommaLoc(), S.SourceMgr);
2374d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Name(D.getIdentifierLoc(), S.SourceMgr);
2375d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    if (Comma.getFileID() != Name.getFileID() ||
2376d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) {
2377d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      LookupResult Result(S, D.getIdentifier(), SourceLocation(),
2378d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                          Sema::LookupOrdinaryName);
2379d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      if (S.LookupName(Result, S.getCurScope()))
2380d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        S.Diag(D.getCommaLoc(), diag::note_empty_parens_function_call)
2381d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << FixItHint::CreateReplacement(D.getCommaLoc(), ";")
2382d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << D.getIdentifier();
2383d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    }
2384d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  }
2385b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2386d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (FTI.NumArgs > 0) {
2387d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration with parameters, eg. "T var(T());", suggest adding parens
2388d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // around the first parameter to turn the declaration into a variable
2389d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2390b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceRange Range = FTI.ArgInfo[0].Param->getSourceRange();
2391b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation B = Range.getBegin();
2392b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation E = S.PP.getLocForEndOfToken(Range.getEnd());
2393b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // FIXME: Maybe we should suggest adding braces instead of parens
2394b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // in C++11 for classes that don't have an initializer_list constructor.
2395b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    S.Diag(B, diag::note_additional_parens_for_variable_declaration)
2396b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(B, "(")
2397b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(E, ")");
2398d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  } else {
2399d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration without parameters, eg. "T var();", suggest replacing the
2400d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // parens with an initializer to turn the declaration into a variable
2401d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2402b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
2403d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2404b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // Empty parens mean value-initialization, and no parens mean
2405b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // default initialization. These are equivalent if the default
2406b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // constructor is user-provided or if zero-initialization is a
2407b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // no-op.
2408b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    if (RD && RD->hasDefinition() &&
2409b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        (RD->isEmpty() || RD->hasUserProvidedDefaultConstructor()))
2410b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor)
2411b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        << FixItHint::CreateRemoval(ParenRange);
2412b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    else {
2413b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      std::string Init = S.getFixItZeroInitializerForType(RT);
241480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      if (Init.empty() && S.LangOpts.CPlusPlus11)
2415b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        Init = "{}";
2416b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (!Init.empty())
2417b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        S.Diag(DeclType.Loc, diag::note_empty_parens_zero_initialize)
2418b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith          << FixItHint::CreateReplacement(ParenRange, Init);
2419b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    }
2420b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  }
2421b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith}
2422b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
24238cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
24248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                QualType declSpecType,
24258cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                TypeSourceInfo *TInfo) {
24268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
24278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = declSpecType;
24288cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
24298cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
24308cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ASTContext &Context = S.Context;
24314e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &LangOpts = S.getLangOpts();
24328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2433cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
2434cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
2435cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
2436cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
24371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2438162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  // Does this declaration declare a typedef-name?
2439162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  bool IsTypedefName =
2440162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef ||
24413e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasDeclContext ||
24423e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasTemplateContext;
2443162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2444d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // Does T refer to a function type with a cv-qualifier or a ref-qualifier?
2445d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  bool IsQualifiedFunction = T->isFunctionProtoType() &&
2446d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      (T->castAs<FunctionProtoType>()->getTypeQuals() != 0 ||
2447d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith       T->castAs<FunctionProtoType>()->getRefQualifier() != RQ_None);
2448d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2449a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  // If T is 'decltype(auto)', the only declarators we can have are parens
2450a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  // and at most one function declarator if this is a function declaration.
2451a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  if (const AutoType *AT = T->getAs<AutoType>()) {
2452a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    if (AT->isDecltypeAuto()) {
2453a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) {
2454a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        unsigned Index = E - I - 1;
2455a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        DeclaratorChunk &DeclChunk = D.getTypeObject(Index);
2456a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        unsigned DiagId = diag::err_decltype_auto_compound_type;
2457a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        unsigned DiagKind = 0;
2458a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        switch (DeclChunk.Kind) {
2459a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Paren:
2460a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          continue;
2461a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Function: {
2462a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          unsigned FnIndex;
2463a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          if (D.isFunctionDeclarationContext() &&
2464a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith              D.isFunctionDeclarator(FnIndex) && FnIndex == Index)
2465a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            continue;
2466a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagId = diag::err_decltype_auto_function_declarator_not_declaration;
2467a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2468a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        }
2469a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Pointer:
2470a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::BlockPointer:
2471a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::MemberPointer:
2472a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagKind = 0;
2473a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2474a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Reference:
2475a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagKind = 1;
2476a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2477a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Array:
2478a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagKind = 2;
2479a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2480a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        }
2481a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
2482a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        S.Diag(DeclChunk.Loc, DiagId) << DiagKind;
2483a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        D.setInvalidType(true);
2484a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        break;
2485a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      }
2486a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    }
2487a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  }
2488a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
248998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
249098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
249198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
24928ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2493711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
2494711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
2495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2496d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction) {
2497d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      checkQualifiedFunction(S, T, DeclType);
2498d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = DeclType.Kind == DeclaratorChunk::Paren;
2499d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    }
25005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
2501075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
25028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildParenType(T);
2503075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
25045618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
25059af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
25069af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
25078cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_blocks_disable);
25081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25098cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
25102865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
25118cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
25125618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
25135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
25146a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
25156a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
25168cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
25178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
25186a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
25196a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
25206a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
25218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.ObjC1 && T->getAs<ObjCObjectType>()) {
2522c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
25232865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
25248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
252514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
252614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
25278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildPointerType(T, DeclType.Loc, Name);
25282865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
25298cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
2530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
25315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
25320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
25336a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
25346a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
25358cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
25368cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
25376a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
25386a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
25396a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
25408cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
25412865474261a608c7873b87ba4af110d17907896dJohn McCall
25422865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
25432865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
25448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
25455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
25460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
25475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
25486a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
25496a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
25508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
25518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
25526a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
25536a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
25546a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
2555fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
255694f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
25575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
25585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
25595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
25605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
25615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
25625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
25635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
2564c05a94b7accd4035bf5d5897c434c445b22da855John McCall      if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
2565f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
2566f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
2567f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
25688cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
2569f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
2570f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
2571f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
25727f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
25737f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // C99 6.7.5.2p1: The optional type qualifiers and the keyword static
25747f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // shall appear only in a declaration of a function parameter with an
25757f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // array type, ...
25767f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      if (ASM == ArrayType::Static || ATI.TypeQuals) {
257799570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay        if (!(D.isPrototypeContext() ||
257899570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay              D.getContext() == Declarator::KNRTypeListContext)) {
25797f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          S.Diag(DeclType.Loc, diag::err_array_static_outside_prototype) <<
25807f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
25817f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Remove the 'static' and the type qualifiers.
25827f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          if (ASM == ArrayType::Static)
25837f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ASM = ArrayType::Normal;
25847f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          ATI.TypeQuals = 0;
25857f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          D.setInvalidType(true);
25867f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
25877f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
25887f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // C99 6.7.5.2p1: ... and then only in the outermost array type
25897f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // derivation.
25907f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        unsigned x = chunkIndex;
25917f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        while (x != 0) {
25927f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Walk outwards along the declarator chunks.
25937f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          x--;
25947f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          const DeclaratorChunk &DC = D.getTypeObject(x);
25957f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          switch (DC.Kind) {
25967f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Paren:
25977f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            continue;
25987f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Array:
25997f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Pointer:
26007f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Reference:
26017f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::MemberPointer:
26027f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            S.Diag(DeclType.Loc, diag::err_array_static_not_outermost) <<
26037f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
26047f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            if (ASM == ArrayType::Static)
26057f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              ASM = ArrayType::Normal;
26067f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ATI.TypeQuals = 0;
26077f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            D.setInvalidType(true);
26087f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
26097f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Function:
26107f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::BlockPointer:
26117f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            // These are invalid anyway, so just ignore.
26127f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
26137f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          }
26147f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
26157f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      }
2616152b4e4652baedfceba1cd8115515629225e713fManuel Klimek
2617152b4e4652baedfceba1cd8115515629225e713fManuel Klimek      if (const AutoType *AT = T->getContainedAutoType()) {
2618a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        // We've already diagnosed this for decltype(auto).
2619dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        if (!AT->isDecltypeAuto())
2620a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          S.Diag(DeclType.Loc, diag::err_illegal_decl_array_of_auto)
2621a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            << getPrintableNameForEntity(Name) << T;
2622dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        T = QualType();
2623dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        break;
2624a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      }
2625a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
26268ac2c66a1442985091c5ec2b33ce6d3df3bcb529Eli Friedman      T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
26278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                           SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
26285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
26295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2630f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
26315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
26325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
26335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
26345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2635d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = FTI.TypeQuals || FTI.hasRefQualifier();
26363cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
263734b41d939a1328f484511c6002ba2456db879a29Richard Smith      // Check for auto functions and trailing return type and adjust the
263834b41d939a1328f484511c6002ba2456db879a29Richard Smith      // return type accordingly.
263934b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!D.isInvalidType()) {
264034b41d939a1328f484511c6002ba2456db879a29Richard Smith        // trailing-return-type is only required if we're declaring a function,
264134b41d939a1328f484511c6002ba2456db879a29Richard Smith        // and not, for instance, a pointer to a function.
264234b41d939a1328f484511c6002ba2456db879a29Richard Smith        if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
264360e141e1f87211ca831de6821003d80fe20a06f3Richard Smith            !FTI.hasTrailingReturnType() && chunkIndex == 0 &&
264460e141e1f87211ca831de6821003d80fe20a06f3Richard Smith            !S.getLangOpts().CPlusPlus1y) {
26458cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
264634b41d939a1328f484511c6002ba2456db879a29Richard Smith               diag::err_auto_missing_trailing_return);
264734b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = Context.IntTy;
264834b41d939a1328f484511c6002ba2456db879a29Richard Smith          D.setInvalidType(true);
264954655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        } else if (FTI.hasTrailingReturnType()) {
2650e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          // T must be exactly 'auto' at this point. See CWG issue 681.
2651e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (isa<ParenType>(T)) {
26528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
2653e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith                 diag::err_trailing_return_in_parens)
2654e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith              << T << D.getDeclSpec().getSourceRange();
2655e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.setInvalidType(true);
2656f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman          } else if (D.getContext() != Declarator::LambdaExprContext &&
2657a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                     (T.hasQualifiers() || !isa<AutoType>(T) ||
2658a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                      cast<AutoType>(T)->isDecltypeAuto())) {
26598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
266034b41d939a1328f484511c6002ba2456db879a29Richard Smith                 diag::err_trailing_return_without_auto)
266134b41d939a1328f484511c6002ba2456db879a29Richard Smith              << T << D.getDeclSpec().getSourceRange();
266234b41d939a1328f484511c6002ba2456db879a29Richard Smith            D.setInvalidType(true);
266334b41d939a1328f484511c6002ba2456db879a29Richard Smith          }
266454655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          T = S.GetTypeFromParser(FTI.getTrailingReturnType(), &TInfo);
266554655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (T.isNull()) {
266654655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            // An error occurred parsing the trailing return type.
266754655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            T = Context.IntTy;
266854655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            D.setInvalidType(true);
266954655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          }
267034b41d939a1328f484511c6002ba2456db879a29Richard Smith        }
267134b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
267234b41d939a1328f484511c6002ba2456db879a29Richard Smith
2673e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // C99 6.7.5.3p1: The return type may not be a function or array type.
2674e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // For conversion functions, we'll diagnose this particular error later.
2675e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      if ((T->isArrayType() || T->isFunctionType()) &&
2676e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
2677e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned diagID = diag::err_func_returning_array_function;
2678e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // Last processing chunk in block context means this function chunk
2679e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // represents the block.
2680e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (chunkIndex == 0 &&
2681e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.getContext() == Declarator::BlockLiteralContext)
2682e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          diagID = diag::err_block_returning_array_function;
26838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
2684e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        T = Context.IntTy;
2685e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        D.setInvalidType(true);
2686e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2687e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2688aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Do not allow returning half FP value.
2689aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // FIXME: This really should be in BuildFunctionType.
2690aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      if (T->isHalfType()) {
269119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        if (S.getLangOpts().OpenCL) {
269219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          if (!S.getOpenCLOptions().cl_khr_fp16) {
269319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            S.Diag(D.getIdentifierLoc(), diag::err_opencl_half_return) << T;
269419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            D.setInvalidType(true);
269519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          }
269619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        } else {
269719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          S.Diag(D.getIdentifierLoc(),
269819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            diag::err_parameters_retval_cannot_have_fp16_type) << 1;
269919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          D.setInvalidType(true);
270019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        }
2701aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      }
2702aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
2703ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      // Methods cannot return interface types. All ObjC objects are
2704ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      // passed by reference.
2705ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      if (T->isObjCObjectType()) {
2706ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        SourceLocation DiagLoc, FixitLoc;
2707ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        if (TInfo) {
2708ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          DiagLoc = TInfo->getTypeLoc().getLocStart();
2709ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          FixitLoc = S.PP.getLocForEndOfToken(TInfo->getTypeLoc().getLocEnd());
2710ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        } else {
2711ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          DiagLoc = D.getDeclSpec().getTypeSpecTypeLoc();
2712ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          FixitLoc = S.PP.getLocForEndOfToken(D.getDeclSpec().getLocEnd());
2713ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        }
2714ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        S.Diag(DiagLoc, diag::err_object_cannot_be_passed_returned_by_value)
2715ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          << 0 << T
2716ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          << FixItHint::CreateInsertion(FixitLoc, "*");
2717ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
2718ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        T = Context.getObjCObjectPointerType(T);
2719ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        if (TInfo) {
2720ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TypeLocBuilder TLB;
2721ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TLB.pushFullCopy(TInfo->getTypeLoc());
2722ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          ObjCObjectPointerTypeLoc TLoc = TLB.push<ObjCObjectPointerTypeLoc>(T);
2723ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TLoc.setStarLoc(FixitLoc);
2724ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TInfo = TLB.getTypeSourceInfo(Context, T);
2725ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        }
2726ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
2727ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        D.setInvalidType(true);
2728ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      }
2729ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
27305291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
27315291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
27328c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if ((T.getCVRQualifiers() || T->isAtomicType()) &&
27338c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          !(S.getLangOpts().CPlusPlus &&
27348c952cd40ccec9d720931f27e7d722fed207d536Richard Smith            (T->isDependentType() || T->isRecordType())))
27358c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        diagnoseIgnoredFunctionQualifiers(S, T, D, chunkIndex);
2736d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
273702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // Objective-C ARC ownership qualifiers are ignored on the function
273802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // return type (by type canonicalization). Complain if this attribute
273902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // was written here.
274002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      if (T.getQualifiers().hasObjCLifetime()) {
274102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        SourceLocation AttrLoc;
274202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (chunkIndex + 1 < D.getNumTypeObjects()) {
274302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
274402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr = ReturnTypeChunk.getAttrs();
274502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
274602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
274702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
274802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
274902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
275002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
275102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
275202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isInvalid()) {
275302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr
275402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor                 = D.getDeclSpec().getAttributes().getList();
275502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
275602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
275702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
275802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
275902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
276002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
276102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
276202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
276302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isValid()) {
276402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // The ownership attributes are almost always written via
276502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // the predefined
276602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // __strong/__weak/__autoreleasing/__unsafe_unretained.
276702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          if (AttrLoc.isMacroID())
276802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            AttrLoc = S.SourceMgr.getImmediateExpansionRange(AttrLoc).first;
276902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
277002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          S.Diag(AttrLoc, diag::warn_arc_lifetime_result_type)
277102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            << T.getQualifiers().getObjCLifetime();
277202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
277302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      }
277402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
27758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
2776402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
2777402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
2778b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
27795e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall        if (Tag->isCompleteDefinition())
27808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
2781402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
2782402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
2783402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
27843cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
27853cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
2786162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (IsTypedefName && FTI.getExceptionSpecType())
27878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef)
27883e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          << (D.getContext() == Declarator::AliasDeclContext ||
27893e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith              D.getContext() == Declarator::AliasTemplateContext);
27903cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
2791b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // If we see "T var();" or "T var(T());" at block scope, it is probably
2792b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // an attempt to initialize a variable, not a function declaration.
2793b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (FTI.isAmbiguous)
2794b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        warnAboutAmbiguousFunction(S, D, DeclType, T);
2795b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
27968cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (!FTI.NumArgs && !FTI.isVariadic && !LangOpts.CPlusPlus) {
27972865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
27982865474261a608c7873b87ba4af110d17907896dJohn McCall        T = Context.getFunctionNoProtoType(T);
27992865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
28002865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
28012865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
28022865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
28038cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        if (!FTI.NumArgs && FTI.isVariadic && !LangOpts.CPlusPlus) {
2804965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
2805965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
2806965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
28078e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt            if (Attrs->getKind() == AttributeList::AT_Overloadable) {
2808965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
2809965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
2810965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
2811965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
2812965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
2813965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
28148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
2815c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
28162865474261a608c7873b87ba4af110d17907896dJohn McCall
28172865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
2818788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
2819788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
28208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
28212865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
2822d5668a2447c2afeea38815b51a80a5a6ac235599Argyrios Kyrtzidis          // Recover by creating a K&R-style function type.
2823d5668a2447c2afeea38815b51a80a5a6ac235599Argyrios Kyrtzidis          T = Context.getFunctionNoProtoType(T);
28242865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
28252865474261a608c7873b87ba4af110d17907896dJohn McCall        }
28262865474261a608c7873b87ba4af110d17907896dJohn McCall
2827e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
2828e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
282954655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        EPI.HasTrailingReturn = FTI.hasTrailingReturnType();
2830e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
2831c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
2832c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : FTI.RefQualifierIsLValueRef? RQ_LValue
2833c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : RQ_RValue;
283491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
28355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
28365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
28375f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 16> ArgTys;
28382865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
28391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<bool, 16> ConsumedArguments;
2841f85e193739c953358c865005855253af4f68a497John McCall        ConsumedArguments.reserve(FTI.NumArgs);
2842f85e193739c953358c865005855253af4f68a497John McCall        bool HasAnyConsumedArguments = false;
2843f85e193739c953358c865005855253af4f68a497John McCall
28445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
2845d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
2846c910d4cfa5042f2c9da1eb4e0b6ed59240c0eeeeReid Kleckner          QualType ArgTy = Param->getType();
284778c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
28482dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
28495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
28505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
285172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
28522dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
28535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
28545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
28555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
28565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
28578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(DeclType.Loc, diag::err_void_only_param);
28582ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
28598123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
28602ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
28612ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
28628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(FTI.ArgInfo[i].IdentLoc,
28634565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
28642ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
28658123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
28662ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
28672ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
28680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
28698cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                S.Diag(DeclType.Loc, diag::err_void_param_qualified);
28701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28712ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
28722ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
28732ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
2874aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov          } else if (ArgTy->isHalfType()) {
2875aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // Disallow half FP arguments.
2876aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // FIXME: This really should be in BuildFunctionType.
287719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            if (S.getLangOpts().OpenCL) {
287819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              if (!S.getOpenCLOptions().cl_khr_fp16) {
287919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                S.Diag(Param->getLocation(),
288019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                  diag::err_opencl_half_argument) << ArgTy;
288119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                D.setInvalidType();
28829dd74c5504c743c96ea3a1d691d6a75ec3a98147John McCall                Param->setInvalidDecl();
288319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              }
288419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            } else {
288519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              S.Diag(Param->getLocation(),
288619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                diag::err_parameters_retval_cannot_have_fp16_type) << 0;
288719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              D.setInvalidType();
288819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            }
2889eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
2890eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
2891a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
2892eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              Param->setKNRPromoted(true);
2893183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
2894eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              if (BTy->getKind() == BuiltinType::Float) {
2895eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
2896eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall                Param->setKNRPromoted(true);
2897eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              }
2898eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
28995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
290056a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
29018cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          if (LangOpts.ObjCAutoRefCount) {
2902f85e193739c953358c865005855253af4f68a497John McCall            bool Consumed = Param->hasAttr<NSConsumedAttr>();
2903f85e193739c953358c865005855253af4f68a497John McCall            ConsumedArguments.push_back(Consumed);
2904f85e193739c953358c865005855253af4f68a497John McCall            HasAnyConsumedArguments |= Consumed;
2905f85e193739c953358c865005855253af4f68a497John McCall          }
2906f85e193739c953358c865005855253af4f68a497John McCall
290754e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
29085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2909465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2910f85e193739c953358c865005855253af4f68a497John McCall        if (HasAnyConsumedArguments)
2911f85e193739c953358c865005855253af4f68a497John McCall          EPI.ConsumedArguments = ConsumedArguments.data();
2912f85e193739c953358c865005855253af4f68a497John McCall
29135f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 4> Exceptions;
291474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<ParsedType, 2> DynamicExceptions;
291574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<SourceRange, 2> DynamicExceptionRanges;
291674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        Expr *NoexceptExpr = 0;
291791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
29188b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        if (FTI.getExceptionSpecType() == EST_Dynamic) {
291974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // FIXME: It's rather inefficient to have to split into two vectors
292074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // here.
292174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          unsigned N = FTI.NumExceptions;
292274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptions.reserve(N);
292374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptionRanges.reserve(N);
292474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          for (unsigned I = 0; I != N; ++I) {
292574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptions.push_back(FTI.Exceptions[I].Ty);
292674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptionRanges.push_back(FTI.Exceptions[I].Range);
2927e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
29288b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
292974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          NoexceptExpr = FTI.NoexceptExpr;
293074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        }
293191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
293274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        S.checkExceptionSpecification(FTI.getExceptionSpecType(),
293374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptions,
293474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptionRanges,
293574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      NoexceptExpr,
293674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      Exceptions,
293774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      EPI);
293891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2939bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose        T = Context.getFunctionType(T, ArgTys, EPI);
29405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
294104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
29425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
29435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2944f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
2945f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
29467bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
2947f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
29487bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
2949edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
2950edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
29518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      } else if (S.isDependentScopeSpecifier(SS) ||
29528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                 dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) {
29531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
29547bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
295587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
295687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
295787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
29587bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
29594a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
296087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
296187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
296287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
296314aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor        case NestedNameSpecifier::NamespaceAlias:
296487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
29659f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
29667bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
296787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
296887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
296987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
297091ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Note: if the NNS has a prefix and ClsType is a nondependent
297191ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // TemplateSpecializationType, then the NNS prefix is NOT included
297291ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // in ClsType; hence we wrap ClsType into an ElaboratedType.
297391ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // NOTE: in particular, no wrap occurs if ClsType already is an
297491ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Elaborated, DependentName, or DependentTemplateSpecialization.
297591ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
29767bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
297787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
297887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
2979f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
29808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Mem.Scope().getBeginLoc(),
2981949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
2982949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
2983949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
2984f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
2985f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2986f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2987949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
29888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
2989949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
2990f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
2991949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
29922865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
29938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
2994f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2995f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
2996f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
2997f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2998cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
2999cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
3000cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
3001cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
3002cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
3003c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
3004711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
3005f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclChunk, attrs);
30065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3007971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
30088cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (LangOpts.CPlusPlus && T->isFunctionType()) {
3009183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
3010778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
3011971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
301291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // C++ 8.3.5p4:
3013708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
3014708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
3015708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
3016708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
3017683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    //
3018683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // Core issue 547 also allows cv-qualifiers on function types that are
3019683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // top-level template type arguments.
3020613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
3021613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
3022906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman      FreeFunction = ((D.getContext() != Declarator::MemberContext &&
3023906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman                       D.getContext() != Declarator::LambdaExprContext) ||
3024613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall                      D.getDeclSpec().isFriendSpecified());
3025613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
30268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec());
3027613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
3028613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
3029613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
3030d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.fct]p6 (w/DR1417):
3031d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // An attempt to specify a function type with a cv-qualifier-seq or a
3032d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // ref-qualifier (including by typedef-name) is ill-formed unless it is:
3033d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type for a non-static member function,
3034d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type to which a pointer to member refers,
3035d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the top-level function type of a function typedef declaration or
3036d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //    alias-declaration,
3037d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id in the default argument of a type-parameter, or
3038d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id of a template-argument for a type-parameter
3039d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction &&
3040d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !(!FreeFunction &&
3041d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) &&
3042d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !IsTypedefName &&
3043d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        D.getContext() != Declarator::TemplateTypeArgContext) {
304496a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      SourceLocation Loc = D.getLocStart();
3045d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      SourceRange RemovalRange;
3046d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      unsigned I;
3047d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      if (D.isFunctionDeclarator(I)) {
3048d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        SmallVector<SourceLocation, 4> RemovalLocs;
3049d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        const DeclaratorChunk &Chunk = D.getTypeObject(I);
3050d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        assert(Chunk.Kind == DeclaratorChunk::Function);
3051d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.hasRefQualifier())
3052d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getRefQualifierLoc());
3053d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Const)
3054d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getConstQualifierLoc());
3055d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Volatile)
3056d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getVolatileQualifierLoc());
3057d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        // FIXME: We do not track the location of the __restrict qualifier.
3058d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //if (Chunk.Fun.TypeQuals & Qualifiers::Restrict)
3059d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //  RemovalLocs.push_back(Chunk.Fun.getRestrictQualifierLoc());
3060d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (!RemovalLocs.empty()) {
3061d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          std::sort(RemovalLocs.begin(), RemovalLocs.end(),
3062aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                    BeforeThanCompare<SourceLocation>(S.getSourceManager()));
3063d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalRange = SourceRange(RemovalLocs.front(), RemovalLocs.back());
3064d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          Loc = RemovalLocs.front();
3065683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
3066683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      }
3067d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
3068d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      S.Diag(Loc, diag::err_invalid_qualified_function_type)
3069d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FreeFunction << D.isFunctionDeclarator() << T
3070d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << getFunctionQualifiersAsString(FnTy)
3071d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FixItHint::CreateRemoval(RemovalRange);
3072d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
3073d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // Strip the cv-qualifiers and ref-qualifiers from the type.
3074d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
3075d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.TypeQuals = 0;
3076d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.RefQualifier = RQ_None;
3077d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
30780567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner      T = Context.getFunctionType(FnTy->getResultType(), FnTy->getArgTypes(),
3079bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                  EPI);
3080e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      // Rebuild any parens around the identifier in the function type.
3081e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
3082e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        if (D.getTypeObject(i).Kind != DeclaratorChunk::Paren)
3083e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith          break;
3084e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        T = S.BuildParenType(T);
3085e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      }
3086971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
3087971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
30881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3089711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
3090711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull())
3091711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = D.getAttributes())
3092f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclName, attrs);
3093711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3094711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
3095711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
3096711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3097148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  // C++0x [dcl.constexpr]p9:
3098148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  //  A constexpr specifier used in an object declaration declares the object
309991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //  as const.
3100148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
3101737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
3102737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
3103737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
310491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // If there was an ellipsis in the declarator, the declaration declares a
3105a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
3106a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (D.hasEllipsis() && !T.isNull()) {
3107a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
310891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   A declarator-id or abstract-declarator containing an ellipsis shall
3109a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
3110a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
3111a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
3112a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
3113a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
311491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   [...] When it is part of a parameter-declaration-clause, the
311591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   parameter pack is a function parameter pack (14.5.3). The type T
3116a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
311791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   a template parameter pack; each template parameter pack in T is
3118a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
3119a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
3120a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
3121a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
3122a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
312391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(D.getEllipsisLoc(),
3124a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
3125a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
3126a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
3127a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
312866874fb18afbffb8b2ca05576851a64534be3352David Blaikie        T = Context.getPackExpansionType(T, None);
3129a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
3130a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
3131152b4e4652baedfceba1cd8115515629225e713fManuel Klimek
3132a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
3133a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
313491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   If a template-parameter is a [...] is a parameter-declaration that
3135a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
3136a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
3137a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
3138a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
3139a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
3140a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
3141a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
314266874fb18afbffb8b2ca05576851a64534be3352David Blaikie        T = Context.getPackExpansionType(T, None);
3143e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith      else
3144e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith        S.Diag(D.getEllipsisLoc(),
314580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith               LangOpts.CPlusPlus11
3146e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 ? diag::warn_cxx98_compat_variadic_templates
3147e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 : diag::ext_variadic_templates);
3148a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
314991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3150a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
3151a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
3152cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:  // FIXME: special diagnostic here?
3153cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:     // FIXME: special diagnostic here?
3154a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
31550b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
3156162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
31573e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
3158a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
3159a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
3160a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
3161a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
3162a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
316317b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
3164a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
3165f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
316614f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    case Declarator::ConversionIdContext:
31677796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
3168683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    case Declarator::TemplateTypeArgContext:
3169a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
3170a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
31718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      S.Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
3172a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
3173a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
3174a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
3175a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
3176e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
3177bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
3178bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
3179bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
3180bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
3181db7abf78dedc2ef6ccb42b3dac6ab330fe2ea469Argyrios Kyrtzidis
31828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return S.GetTypeSourceInfoForDeclarator(D, T, TInfo);
31838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
31848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
31858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// GetTypeForDeclarator - Convert the type for the specified
31868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// declarator to Type instances.
31878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis///
31888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// The result of this call will never be null, but the associated
31898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// type may be a null type if there's an unrecoverable error.
31908cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
31918cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // Determine the type of the declarator. Not all forms of declarator
31928cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // have a type.
31938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
31948cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeProcessingState state(*this, D);
31958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
31968cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
31978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
31988cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (T.isNull())
31998cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
32008cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
32014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (D.isPrototypeContext() && getLangOpts().ObjCAutoRefCount)
32028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    inferARCWriteback(state, T);
320391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
32048cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return GetFullTypeForDeclarator(state, T, ReturnTypeInfo);
32055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
32065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
320731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclSpec(Sema &S,
320831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           QualType &declSpecTy,
320931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           Qualifiers::ObjCLifetime ownership) {
321031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy->isObjCRetainableType() &&
321131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) {
321231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers qs;
321331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    qs.addObjCLifetime(ownership);
321431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    declSpecTy = S.Context.getQualifiedType(declSpecTy, qs);
321531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
321631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
321731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
321831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
321931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
322031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            unsigned chunkIndex) {
322131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
322231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
322331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
322431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Look for an explicit lifetime attribute.
322531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclaratorChunk &chunk = D.getTypeObject(chunkIndex);
322631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (const AttributeList *attr = chunk.getAttrs(); attr;
322731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis         attr = attr->getNext())
32288e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    if (attr->getKind() == AttributeList::AT_ObjCOwnership)
322931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
323031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
323131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  const char *attrStr = 0;
323231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  switch (ownership) {
32333026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
323431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break;
323531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Strong: attrStr = "strong"; break;
323631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Weak: attrStr = "weak"; break;
323731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break;
323831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
323931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
324031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // If there wasn't one, add one (with an invalid source location
324131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // so that we don't make an AttributedType for it).
324231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  AttributeList *attr = D.getAttributePool()
324331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    .create(&S.Context.Idents.get("objc_ownership"), SourceLocation(),
324431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            /*scope*/ 0, SourceLocation(),
324531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            &S.Context.Idents.get(attrStr), SourceLocation(),
324693f95f2a2cbb6bb3d17bfb5fc74ce1cccea751b6Sean Hunt            /*args*/ 0, 0, AttributeList::AS_GNU);
324731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  spliceAttrIntoList(*attr, chunk.getAttrListRef());
324831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
324931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // TODO: mark whether we did this inference?
325031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
325131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
325248d798ce32447607144db70a484cdb99c1180663Benjamin Kramer/// \brief Used for transferring ownership in casts resulting in l-values.
325331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnership(TypeProcessingState &state,
325431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 QualType &declSpecTy,
325531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 Qualifiers::ObjCLifetime ownership) {
325631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
325731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
325831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
325931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  int inner = -1;
32606ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis  bool hasIndirection = false;
326131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
326231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    DeclaratorChunk &chunk = D.getTypeObject(i);
326331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    switch (chunk.Kind) {
326431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Paren:
326531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      // Ignore parens.
326631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
326731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
326831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Array:
326931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Reference:
327031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Pointer:
32716ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
32726ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        hasIndirection = true;
327331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      inner = i;
327431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
327531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
327631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::BlockPointer:
32776ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
32786ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        transferARCOwnershipToDeclaratorChunk(state, ownership, i);
32796ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      return;
328031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
328131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Function:
328231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::MemberPointer:
328331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
328431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    }
328531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
328631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
328731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (inner == -1)
32886ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    return;
328931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
329091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  DeclaratorChunk &chunk = D.getTypeObject(inner);
329131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (chunk.Kind == DeclaratorChunk::Pointer) {
329231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (declSpecTy->isObjCRetainableType())
329331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
32946ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    if (declSpecTy->isObjCObjectType() && hasIndirection)
329531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclaratorChunk(state, ownership, inner);
329631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  } else {
329731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    assert(chunk.Kind == DeclaratorChunk::Array ||
329831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis           chunk.Kind == DeclaratorChunk::Reference);
329931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
330031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
330131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
330231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
330331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) {
330431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeProcessingState state(*this, D);
330531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
330631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
330731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
330831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy.isNull())
330931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
331031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
33114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount) {
331231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy);
331331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (ownership != Qualifiers::OCL_None)
331431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      transferARCOwnership(state, declSpecTy, ownership);
331531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
331631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
331731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo);
331831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
331931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
332014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall/// Map an AttributedType::Kind to an AttributeList::Kind.
332114aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
332214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  switch (kind) {
332314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_address_space:
33248e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_AddressSpace;
332514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_regparm:
33268e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Regparm;
332714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_vector_size:
33288e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_VectorSize;
332914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_vector_type:
33308e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonVectorType;
333114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_polyvector_type:
33328e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonPolyVectorType;
333314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_objc_gc:
33348e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCGC;
3335b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  case AttributedType::attr_objc_ownership:
33368e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCOwnership;
333714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_noreturn:
33388e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NoReturn;
333914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_cdecl:
33408e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_CDecl;
334114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_fastcall:
33428e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_FastCall;
334314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_stdcall:
33448e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_StdCall;
334514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_thiscall:
33468e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ThisCall;
334714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_pascal:
33488e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pascal;
3349414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case AttributedType::attr_pcs:
33505b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributedType::attr_pcs_vfp:
33518e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pcs;
3352263366f9241366f29ba65b703120f302490c39ffDerek Schuff  case AttributedType::attr_pnaclcall:
3353263366f9241366f29ba65b703120f302490c39ffDerek Schuff    return AttributeList::AT_PnaclCall;
335438980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei  case AttributedType::attr_inteloclbicc:
335538980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    return AttributeList::AT_IntelOclBicc;
3356aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_ptr32:
3357aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_Ptr32;
3358aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_ptr64:
3359aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_Ptr64;
3360aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_sptr:
3361aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_SPtr;
3362aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_uptr:
3363aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_UPtr;
336414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
336514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  llvm_unreachable("unexpected attribute kind!");
336614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
336714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
336814aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic void fillAttributedTypeLoc(AttributedTypeLoc TL,
336914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                  const AttributeList *attrs) {
337014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributedType::Kind kind = TL.getAttrKind();
337114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
337214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  assert(attrs && "no type attributes in the expected location!");
337314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributeList::Kind parsedKind = getAttrListKind(kind);
337414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  while (attrs->getKind() != parsedKind) {
337514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    attrs = attrs->getNext();
337614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    assert(attrs && "no matching attribute in expected location!");
337714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
337814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
337914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  TL.setAttrNameLoc(attrs->getLoc());
338014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrExprOperand())
338114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrExprOperand(attrs->getArg(0));
338214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  else if (TL.hasAttrEnumOperand())
338314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrEnumOperandLoc(attrs->getParameterLoc());
338414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
338514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // FIXME: preserve this information to here.
338614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrOperand())
338714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrOperandParensRange(SourceRange());
338814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
338914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
339051bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
339151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
3392c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    ASTContext &Context;
339351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
3394f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
339551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
339691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
3397c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      : Context(Context), DS(DS) {}
3398f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
339914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
340014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, DS.getAttributes().getList());
340114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      Visit(TL.getModifiedLoc());
340214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
340351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
340451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
340551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
340651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
340751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
340851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
340951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
341051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
34111de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // FIXME. We should have DS.getTypeSpecTypeEndLoc(). But, it requires
34121de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // addition field. What we have is good enough for dispay of location
34131de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // of 'fixit' on interface name.
34141de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      TL.setNameEndLoc(DS.getLocEnd());
3415c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
3416c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3417c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
3418c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
3419c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
3420c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.getBaseLoc().initialize(Context, SourceLocation());
3421c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
3422c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
3423c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
3424c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
342554e14c4db764c0636160d26c5bbf491637c83a76John McCall
3426c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
342754e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
342854e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
342954e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
343054e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
343154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
343254e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
343354e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
343454e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
343554e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
343654e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
343754e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
343854e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
343951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
344054e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
344154e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
3442c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
344351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
3444833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
3445a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
3446b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3447833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3448833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
3449833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
3450a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
34510daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara        TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
3452833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
3453833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
3454833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3455e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
3456e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
345739e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        ElaboratedTypeLoc ElabTL = OldTL.castAs<ElaboratedTypeLoc>();
345839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        TemplateSpecializationTypeLoc NamedTL = ElabTL.getNamedTypeLoc()
345939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            .castAs<TemplateSpecializationTypeLoc>();
3460e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
346144ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      } else {
346239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        TL.copy(OldTL.castAs<TemplateSpecializationTypeLoc>());
346344ee0a710c59d8e6793189f903bae21c16814324Eli Friedman        assert(TL.getRAngleLoc() == OldTL.castAs<TemplateSpecializationTypeLoc>().getRAngleLoc());
346444ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      }
346544ee0a710c59d8e6793189f903bae21c16814324Eli Friedman
3466833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
3467cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3468cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
3469cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3470cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3471cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3472cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3473cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
3474cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3475cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3476b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
3477cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
3478b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3479cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
3480cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3481ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3482ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      // FIXME: This holds only because we only have one unary transform.
3483ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType);
3484ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setKWLoc(DS.getTypeSpecTypeLoc());
3485ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setParensRange(DS.getTypeofParensRange());
3486ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getRepAsType());
3487ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TypeSourceInfo *TInfo = 0;
3488ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3489ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setUnderlyingTInfo(TInfo);
3490ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
3491ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3492ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
3493ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
3494ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
3495ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
3496ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
3497ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
3498ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
3499ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
3500ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
3501ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
3502ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
3503ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
3504ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
3505ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
3506e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3507e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
3508e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
3509253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
3510e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
3511b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3512e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
351339e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          TL.copy(TInfo->getTypeLoc().castAs<ElaboratedTypeLoc>());
3514e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
3515e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
3516e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
351738a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara      TL.setElaboratedKeywordLoc(Keyword != ETK_None
351838a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 ? DS.getTypeSpecTypeLoc()
351938a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 : SourceLocation());
3520e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
35219e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
3522e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
3523e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3524e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
352566581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
352666581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
352766581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
352866581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
352939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      TL.copy(TInfo->getTypeLoc().castAs<DependentNameTypeLoc>());
353033500955d731c73717af52088b7fc0e7a85681e7John McCall    }
353133500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
353233500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
353366581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
353466581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
353566581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
353666581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
353739e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      TL.copy(
353839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          TInfo->getTypeLoc().castAs<DependentTemplateSpecializationTypeLoc>());
35390daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    }
35400daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    void VisitTagTypeLoc(TagTypeLoc TL) {
35410daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
3542e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3543b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
35444cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // An AtomicTypeLoc can come from either an _Atomic(...) type specifier
35454cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // or an _Atomic qualifier.
35464cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      if (DS.getTypeSpecType() == DeclSpec::TST_atomic) {
35474cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setKWLoc(DS.getTypeSpecTypeLoc());
35484cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setParensRange(DS.getTypeofParensRange());
354991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
35504cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TypeSourceInfo *TInfo = 0;
35514cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
35524cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        assert(TInfo);
35534cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
35544cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      } else {
35554cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setKWLoc(DS.getAtomicSpecLoc());
35564cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // No parens, to indicate this was spelled as an _Atomic qualifier.
35574cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setParensRange(SourceRange());
35584cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        Visit(TL.getValueLoc());
35594cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      }
3560b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
3561e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
356251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
356351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
3564c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initialize(Context, DS.getTypeSpecTypeLoc());
356551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
356651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
3567eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
356851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
3569b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    ASTContext &Context;
357051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
3571f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
357251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
3573b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
3574b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      : Context(Context), Chunk(Chunk) {}
35754adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
357651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
35779f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
357851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
357912df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner    void VisitDecayedTypeLoc(DecayedTypeLoc TL) {
358012df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner      llvm_unreachable("decayed type locs not expected here!");
358112df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner    }
35824adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3583f85e193739c953358c865005855253af4f68a497John McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3584f85e193739c953358c865005855253af4f68a497John McCall      fillAttributedTypeLoc(TL, Chunk.getAttrs());
3585f85e193739c953358c865005855253af4f68a497John McCall    }
358651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
358751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
358851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
35894adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
359051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
359151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
359251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
35934adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
359451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
359551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
359651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
35974adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
359851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
359951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
3600b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const CXXScopeSpec& SS = Chunk.Mem.Scope();
3601b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
3602b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3603b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const Type* ClsTy = TL.getClass();
3604b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      QualType ClsQT = QualType(ClsTy, 0);
3605b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
3606b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Now copy source location info into the type loc component.
3607b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeLoc ClsTL = ClsTInfo->getTypeLoc();
3608b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
3609b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Identifier:
3610b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
3611b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        {
361239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          DependentNameTypeLoc DNTLoc = ClsTL.castAs<DependentNameTypeLoc>();
361338a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          DNTLoc.setElaboratedKeywordLoc(SourceLocation());
3614b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
3615b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
3616b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3617b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3618b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3619b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpec:
3620b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpecWithTemplate:
3621b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        if (isa<ElaboratedType>(ClsTy)) {
362239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          ElaboratedTypeLoc ETLoc = ClsTL.castAs<ElaboratedTypeLoc>();
362338a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          ETLoc.setElaboratedKeywordLoc(SourceLocation());
3624b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ETLoc.setQualifierLoc(NNSLoc.getPrefix());
3625b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
3626b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
3627b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        } else {
3628b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
3629b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3630b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3631b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3632b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Namespace:
3633b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::NamespaceAlias:
3634b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Global:
3635b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        llvm_unreachable("Nested-name-specifier must name a type");
3636b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      }
3637b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3638b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Finally fill in MemberPointerLocInfo fields.
363951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
3640b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TL.setClassTInfo(ClsTInfo);
36414adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
364251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
364351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
364454e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
364554e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
364651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
364751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
364851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
364951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
365051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
365151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
365251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
365351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
365451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
365551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
365651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
365751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
365851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
365951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
366051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
3661796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeBegin(Chunk.Loc);
3662796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeEnd(Chunk.EndLoc);
366351bd803fbdade51d674598ed45da3d54190a656cJohn McCall
366451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
366559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setLParenLoc(FTI.getLParenLoc());
366659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setRParenLoc(FTI.getRParenLoc());
366754e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
3668d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
366954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
36704adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
367151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
36724adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
3673075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
3674075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
3675075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
3676075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
3677075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
36781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
367951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
36809f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
36814adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
368251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
368351bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
36844adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
36854cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smithstatic void fillAtomicQualLoc(AtomicTypeLoc ATL, const DeclaratorChunk &Chunk) {
36864cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  SourceLocation Loc;
36874cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  switch (Chunk.Kind) {
36884cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Function:
36894cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Array:
36904cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Paren:
36914cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    llvm_unreachable("cannot be _Atomic qualified");
36924cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
36934cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Pointer:
36944cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    Loc = SourceLocation::getFromRawEncoding(Chunk.Ptr.AtomicQualLoc);
36954cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    break;
36964cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
36974cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::BlockPointer:
36984cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Reference:
36994cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::MemberPointer:
37004cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // FIXME: Provide a source location for the _Atomic keyword.
37014cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    break;
37024cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
37034cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
37044cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  ATL.setKWLoc(Loc);
37054cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  ATL.setParensRange(SourceRange());
37064cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith}
37074cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
3708a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
370951bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
371051bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
371105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
371205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
371305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
371405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
371505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
3716a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
371705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
371805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
3719a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
3720a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
372151bd803fbdade51d674598ed45da3d54190a656cJohn McCall
3722a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
3723a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
372439e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    CurrTL.castAs<PackExpansionTypeLoc>().setEllipsisLoc(D.getEllipsisLoc());
372591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
3726a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
372791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
37288ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
37294cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // An AtomicTypeLoc might be produced by an atomic qualifier in this
37304cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // declarator chunk.
37314cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (AtomicTypeLoc ATL = CurrTL.getAs<AtomicTypeLoc>()) {
37324cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      fillAtomicQualLoc(ATL, D.getTypeObject(i));
37334cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      CurrTL = ATL.getValueLoc().getUnqualifiedLoc();
37344cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    }
37354cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
373639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    while (AttributedTypeLoc TL = CurrTL.getAs<AttributedTypeLoc>()) {
373714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs());
373814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
373914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
374014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3741b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
374251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
37434adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
374491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3745b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
3746b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
3747b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
374805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
374905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
375005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
3751b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
3752c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
375305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
375491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3755a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
37564adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
37574adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3758a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
3759b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
37601bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
37611bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
37621bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
376391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
3764eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
3765a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
37661bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
37671bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
3768b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
37691bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
37701bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
37711bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
37721bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
3773b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*"
377435d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
377535d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
37761bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
37771bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
3778f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
37795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
37805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
37815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
37821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3783d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
3784bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
37855153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
3786809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
37875912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
3788e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall  // Make sure there are no unused decl attributes on the declarator.
3789cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // We don't want to do this for ObjC parameters because we're going
3790cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // to apply them to the actual parameter declaration.
37916b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  // Likewise, we don't want to do this for alias declarations, because
37926b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  // we are actually going to build a declaration from this eventually.
37936b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  if (D.getContext() != Declarator::ObjCParameterContext &&
37946b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith      D.getContext() != Declarator::AliasDeclContext &&
37956b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith      D.getContext() != Declarator::AliasTemplateContext)
3796cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    checkUnusedDeclAttributes(D);
3797e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
37984e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
3799402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
38006d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
3801402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
3802402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
3803b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
38045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
38055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3806e97179c675b341927807c718be215c8d1aab8acbDouglas GregorParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) {
3807e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  QualType T = Context.getObjCInstanceType();
3808e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc);
3809e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return CreateParsedType(T, TInfo);
3810e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
3811e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3812e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3813c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3814c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
3815c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3816232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3817232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
3818c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
3819c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
38201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
3821c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
38220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
3823232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
382429e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified by
382529e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // qualifiers for two or more different address spaces."
3826232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
3827c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
3828e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3829c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3830232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
38311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3832020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be
3833020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // qualified by an address-space qualifier."
3834020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  if (Type->isFunctionType()) {
3835020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type);
3836020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    Attr.setInvalid();
3837020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    return;
3838020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  }
3839020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne
3840232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
3841545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
3842baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3843baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
3844e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3845c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3846232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3847545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
3848232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
3849ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
3850ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
38519f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
38529f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
38539f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << ASArgExpr->getSourceRange();
3854e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3855c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3856232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3857232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3858efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
3859efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
3860efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
3861efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
3862efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
3863e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
3864efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
3865efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
3866efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
3867efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3868efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
38690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
3870efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
3871efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
38727348454025693dd20a411c2bcaabd4460cb87559Joerg Sonnenberger      << int(Qualifiers::MaxAddressSpace) << ASArgExpr->getSourceRange();
3873e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3874efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
3875efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3876efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
38771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
3878f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
3879c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
3880c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
3881d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// Does this type have a "direct" ownership qualifier?  That is,
3882d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// is it written like "__strong id", as opposed to something like
3883d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// "typeof(foo)", where that happens to be strong?
3884d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCallstatic bool hasDirectOwnershipQualifier(QualType type) {
3885d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Fast path: no qualifier at all.
3886d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  assert(type.getQualifiers().hasObjCLifetime());
3887d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3888d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  while (true) {
3889d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // __strong id
3890d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (const AttributedType *attr = dyn_cast<AttributedType>(type)) {
3891d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      if (attr->getAttrKind() == AttributedType::attr_objc_ownership)
3892d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        return true;
3893d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3894d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = attr->getModifiedType();
3895d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3896d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // X *__strong (...)
3897d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else if (const ParenType *paren = dyn_cast<ParenType>(type)) {
3898d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = paren->getInnerType();
389991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3900d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // That's it for things we want to complain about.  In particular,
3901d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // we do not want to look through typedefs, typeof(expr),
3902d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // typeof(type), or any other way that the type is somehow
3903d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // abstracted.
3904d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else {
390591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3906d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return false;
3907d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3908d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
3909d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall}
3910d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3911b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis/// handleObjCOwnershipTypeAttr - Process an objc_ownership
3912f85e193739c953358c865005855253af4f68a497John McCall/// attribute on the specified type.
3913f85e193739c953358c865005855253af4f68a497John McCall///
3914f85e193739c953358c865005855253af4f68a497John McCall/// Returns 'true' if the attribute was handled.
3915b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
3916f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr,
3917f85e193739c953358c865005855253af4f68a497John McCall                                       QualType &type) {
3918e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  bool NonObjCPointer = false;
3919e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3920dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith  if (!type->isDependentType() && !type->isUndeducedType()) {
3921e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    if (const PointerType *ptr = type->getAs<PointerType>()) {
3922e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      QualType pointee = ptr->getPointeeType();
3923e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      if (pointee->isObjCRetainableType() || pointee->isPointerType())
3924e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis        return false;
3925e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // It is important not to lose the source info that there was an attribute
3926e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // applied to non-objc pointer. We will create an attributed type but
3927e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // its type will be the same as the original type.
3928e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      NonObjCPointer = true;
3929e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    } else if (!type->isObjCRetainableType()) {
3930e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      return false;
3931e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3932b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
3933b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // Don't accept an ownership attribute in the declspec if it would
3934b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // just be the return type of a block pointer.
3935b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    if (state.isProcessingDeclSpec()) {
3936b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      Declarator &D = state.getDeclarator();
3937b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (maybeMovePastReturnType(D, D.getNumTypeObjects()))
3938b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        return false;
3939b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
3940e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3941f85e193739c953358c865005855253af4f68a497John McCall
3942f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
3943440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  SourceLocation AttrLoc = attr.getLoc();
3944440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isMacroID())
3945440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    AttrLoc = S.getSourceManager().getImmediateExpansionRange(AttrLoc).first;
3946f85e193739c953358c865005855253af4f68a497John McCall
3947f85e193739c953358c865005855253af4f68a497John McCall  if (!attr.getParameterName()) {
39483cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman    S.Diag(AttrLoc, diag::err_attribute_argument_type)
39493cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman      << attr.getName() << AANT_ArgumentString;
3950f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3951f85e193739c953358c865005855253af4f68a497John McCall    return true;
3952f85e193739c953358c865005855253af4f68a497John McCall  }
3953f85e193739c953358c865005855253af4f68a497John McCall
3954d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Consume lifetime attributes without further comment outside of
3955d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // ARC mode.
39564e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!S.getLangOpts().ObjCAutoRefCount)
3957d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    return true;
3958d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3959f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime lifetime;
3960f85e193739c953358c865005855253af4f68a497John McCall  if (attr.getParameterName()->isStr("none"))
3961f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_ExplicitNone;
3962f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("strong"))
3963f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Strong;
3964f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("weak"))
3965f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Weak;
3966f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("autoreleasing"))
3967f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Autoreleasing;
3968f85e193739c953358c865005855253af4f68a497John McCall  else {
3969440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_attribute_type_not_supported)
3970b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      << "objc_ownership" << attr.getParameterName();
3971f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3972f85e193739c953358c865005855253af4f68a497John McCall    return true;
3973f85e193739c953358c865005855253af4f68a497John McCall  }
3974f85e193739c953358c865005855253af4f68a497John McCall
3975d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  SplitQualType underlyingType = type.split();
3976d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3977d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Check for redundant/conflicting ownership qualifiers.
3978d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  if (Qualifiers::ObjCLifetime previousLifetime
3979d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        = type.getQualifiers().getObjCLifetime()) {
3980d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // If it's written directly, that's an error.
3981d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (hasDirectOwnershipQualifier(type)) {
3982d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant)
3983d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        << type;
3984d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return true;
3985d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3986d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3987d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // Otherwise, if the qualifiers actually conflict, pull sugar off
3988d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // until we reach a type that is directly qualified.
3989d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (previousLifetime != lifetime) {
3990d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // This should always terminate: the canonical type is
3991d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // qualified, so some bit of sugar must be hiding it.
3992d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      while (!underlyingType.Quals.hasObjCLifetime()) {
3993d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        underlyingType = underlyingType.getSingleStepDesugaredType();
3994d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      }
3995d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      underlyingType.Quals.removeObjCLifetime();
3996d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3997d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
3998d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3999d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  underlyingType.Quals.addObjCLifetime(lifetime);
4000f85e193739c953358c865005855253af4f68a497John McCall
4001e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (NonObjCPointer) {
4002e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    StringRef name = attr.getName()->getName();
4003e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    switch (lifetime) {
4004e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_None:
4005e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_ExplicitNone:
4006e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      break;
4007e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Strong: name = "__strong"; break;
4008e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Weak: name = "__weak"; break;
4009e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break;
4010e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
4011fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman    S.Diag(AttrLoc, diag::warn_type_attribute_wrong_type) << name
4012fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman      << TDS_ObjCObjOrBlock << type;
4013e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
4014e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
4015f85e193739c953358c865005855253af4f68a497John McCall  QualType origType = type;
4016e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!NonObjCPointer)
4017d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    type = S.Context.getQualifiedType(underlyingType);
4018f85e193739c953358c865005855253af4f68a497John McCall
4019f85e193739c953358c865005855253af4f68a497John McCall  // If we have a valid source location for the attribute, use an
4020f85e193739c953358c865005855253af4f68a497John McCall  // AttributedType instead.
4021440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isValid())
4022b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis    type = S.Context.getAttributedType(AttributedType::attr_objc_ownership,
4023f85e193739c953358c865005855253af4f68a497John McCall                                       origType, type);
4024f85e193739c953358c865005855253af4f68a497John McCall
40259f084a3166b684573ba49df28fc5792bc37d92e1John McCall  // Forbid __weak if the runtime doesn't support it.
4026f85e193739c953358c865005855253af4f68a497John McCall  if (lifetime == Qualifiers::OCL_Weak &&
40270a7dd788dbef975f35f273c7ab913f480f7edd60John McCall      !S.getLangOpts().ObjCARCWeak && !NonObjCPointer) {
4028f85e193739c953358c865005855253af4f68a497John McCall
4029f85e193739c953358c865005855253af4f68a497John McCall    // Actually, delay this until we know what we're parsing.
4030f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
4031f85e193739c953358c865005855253af4f68a497John McCall      S.DelayedDiagnostics.add(
4032440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis          sema::DelayedDiagnostic::makeForbiddenType(
4033440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis              S.getSourceManager().getExpansionLoc(AttrLoc),
4034f85e193739c953358c865005855253af4f68a497John McCall              diag::err_arc_weak_no_runtime, type, /*ignored*/ 0));
4035f85e193739c953358c865005855253af4f68a497John McCall    } else {
4036440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis      S.Diag(AttrLoc, diag::err_arc_weak_no_runtime);
4037f85e193739c953358c865005855253af4f68a497John McCall    }
4038f85e193739c953358c865005855253af4f68a497John McCall
4039f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
4040f85e193739c953358c865005855253af4f68a497John McCall    return true;
4041f85e193739c953358c865005855253af4f68a497John McCall  }
404291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
404391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // Forbid __weak for class objects marked as
4044742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  // objc_arc_weak_reference_unavailable
4045742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  if (lifetime == Qualifiers::OCL_Weak) {
4046b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    if (const ObjCObjectPointerType *ObjT =
4047b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          type->getAs<ObjCObjectPointerType>()) {
40484e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith      if (ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl()) {
40494e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        if (Class->isArcWeakrefUnavailable()) {
40504e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class);
40514e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(ObjT->getInterfaceDecl()->getLocation(),
40524e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith                   diag::note_class_declared);
40534e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        }
4054742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      }
4055742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    }
4056742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  }
405791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4058f85e193739c953358c865005855253af4f68a497John McCall  return true;
4059f85e193739c953358c865005855253af4f68a497John McCall}
4060f85e193739c953358c865005855253af4f68a497John McCall
4061711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
4062711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
4063711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
4064711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
4065711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
4066711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
4067711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
4068711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
4069711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4070711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
4071711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
4072711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
4073711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
4074711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
4075711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4076711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
4077711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
4078711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4079711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4080d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
40811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4082d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
4083711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!attr.getParameterName()) {
40843cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman    S.Diag(attr.getLoc(), diag::err_attribute_argument_type)
40853cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman      << attr.getName() << AANT_ArgumentString;
4086711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4087711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4088ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
40890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
4090711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getNumArgs() != 0) {
4091baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4092baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << attr.getName() << 1;
4093711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4094711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4095d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
4096711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getParameterName()->isStr("weak"))
40970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
4098711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  else if (attr.getParameterName()->isStr("strong"))
40990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
4100d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
4101711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
4102711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      << "objc_gc" << attr.getParameterName();
4103711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4104711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4105d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
41061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
410714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  QualType origType = type;
410814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  type = S.Context.getObjCGCQualType(origType, GCAttr);
410914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
411014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // Make an attributed type to preserve the source information.
411114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (attr.getLoc().isValid())
411214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
411314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                       origType, type);
411414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
4115711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
4116d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
4117d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
4118e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallnamespace {
4119e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// A helper class to unwrap a type down to a function for the
4120e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// purposes of applying attributes there.
4121e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///
4122e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// Use:
4123e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
4124e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   if (unwrapped.isFunctionType()) {
4125e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     const FunctionType *fn = unwrapped.get();
4126e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     // change fn somehow
4127e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     T = unwrapped.wrap(fn);
4128e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   }
4129e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  struct FunctionTypeUnwrapper {
4130e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    enum WrapKind {
4131e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Desugar,
4132e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Parens,
4133e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Pointer,
4134e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      BlockPointer,
4135e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Reference,
4136e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      MemberPointer
4137e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    };
4138e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4139e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType Original;
4140e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *Fn;
41415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<unsigned char /*WrapKind*/, 8> Stack;
4142e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4143e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
4144e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      while (true) {
4145e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const Type *Ty = T.getTypePtr();
4146e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<FunctionType>(Ty)) {
4147e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Fn = cast<FunctionType>(Ty);
4148e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return;
4149e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ParenType>(Ty)) {
4150e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ParenType>(Ty)->getInnerType();
4151e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Parens);
4152e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<PointerType>(Ty)) {
4153e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<PointerType>(Ty)->getPointeeType();
4154e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Pointer);
4155e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<BlockPointerType>(Ty)) {
4156e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<BlockPointerType>(Ty)->getPointeeType();
4157e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(BlockPointer);
4158e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<MemberPointerType>(Ty)) {
4159e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<MemberPointerType>(Ty)->getPointeeType();
4160e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(MemberPointer);
4161e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ReferenceType>(Ty)) {
4162e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ReferenceType>(Ty)->getPointeeType();
4163e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Reference);
4164e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else {
4165e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          const Type *DTy = Ty->getUnqualifiedDesugaredType();
4166e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          if (Ty == DTy) {
4167e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            Fn = 0;
4168e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            return;
4169e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          }
4170e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4171e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = QualType(DTy, 0);
4172e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Desugar);
4173e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        }
4174e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4175e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4176e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4177e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    bool isFunctionType() const { return (Fn != 0); }
4178e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *get() const { return Fn; }
4179e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4180e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(Sema &S, const FunctionType *New) {
4181e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // If T wasn't modified from the unwrapped type, do nothing.
4182e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (New == get()) return Original;
4183e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4184e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Fn = New;
4185e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return wrap(S.Context, Original, 0);
4186e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4187e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4188e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  private:
4189e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
4190e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size())
4191e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getQualifiedType(Fn, Old.getQualifiers());
4192e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4193e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // Build up the inner type, applying the qualifiers from the old
4194e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // type to the new type.
4195e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      SplitQualType SplitOld = Old.split();
4196e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4197e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // As a special case, tail-recurse if there are no qualifiers.
4198200fa53fd420aa8369586f569dbece04930ad6a3John McCall      if (SplitOld.Quals.empty())
4199200fa53fd420aa8369586f569dbece04930ad6a3John McCall        return wrap(C, SplitOld.Ty, I);
4200200fa53fd420aa8369586f569dbece04930ad6a3John McCall      return C.getQualifiedType(wrap(C, SplitOld.Ty, I), SplitOld.Quals);
4201e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4202e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4203e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
4204e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size()) return QualType(Fn, 0);
4205e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4206e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      switch (static_cast<WrapKind>(Stack[I++])) {
4207e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Desugar:
4208e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // This is the point at which we potentially lose source
4209e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // information.
4210e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
4211e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4212e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Parens: {
4213e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
4214e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getParenType(New);
4215e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4216e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4217e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Pointer: {
4218e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
4219e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getPointerType(New);
4220e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4221e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4222e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case BlockPointer: {
4223e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
4224e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getBlockPointerType(New);
4225e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4226e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4227e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case MemberPointer: {
4228e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
4229e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldMPT->getPointeeType(), I);
4230e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getMemberPointerType(New, OldMPT->getClass());
4231e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4232e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4233e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Reference: {
4234e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const ReferenceType *OldRef = cast<ReferenceType>(Old);
4235e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldRef->getPointeeType(), I);
4236e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<LValueReferenceType>(OldRef))
4237e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
4238e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        else
4239e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getRValueReferenceType(New);
4240e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4241e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4242e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4243e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      llvm_unreachable("unknown wrapping kind");
4244e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4245e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  };
4246e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
4247e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4248aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballmanstatic bool handleMSPointerTypeQualifierAttr(TypeProcessingState &State,
4249aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             AttributeList &Attr,
4250aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             QualType &Type) {
4251aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  Sema &S = State.getSema();
4252aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4253aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  AttributeList::Kind Kind = Attr.getKind();
4254aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  QualType Desugared = Type;
4255aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  const AttributedType *AT = dyn_cast<AttributedType>(Type);
4256aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  while (AT) {
4257aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    AttributedType::Kind CurAttrKind = AT->getAttrKind();
4258aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4259aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // You cannot specify duplicate type attributes, so if the attribute has
4260aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // already been applied, flag it.
4261aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    if (getAttrListKind(CurAttrKind) == Kind) {
4262aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute_exact)
4263aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        << Attr.getName();
4264aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      return true;
4265aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    }
4266aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4267aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // You cannot have both __sptr and __uptr on the same type, nor can you
4268aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // have __ptr32 and __ptr64.
4269aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    if ((CurAttrKind == AttributedType::attr_ptr32 &&
4270aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman         Kind == AttributeList::AT_Ptr64) ||
4271aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        (CurAttrKind == AttributedType::attr_ptr64 &&
4272aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman         Kind == AttributeList::AT_Ptr32)) {
4273aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
4274aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        << "'__ptr32'" << "'__ptr64'";
4275aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      return true;
4276aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    } else if ((CurAttrKind == AttributedType::attr_sptr &&
4277aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                Kind == AttributeList::AT_UPtr) ||
4278aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman               (CurAttrKind == AttributedType::attr_uptr &&
4279aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                Kind == AttributeList::AT_SPtr)) {
4280aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
4281aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        << "'__sptr'" << "'__uptr'";
4282aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      return true;
4283aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    }
4284aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4285aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    Desugared = AT->getEquivalentType();
4286aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    AT = dyn_cast<AttributedType>(Desugared);
4287aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  }
4288aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4289aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  // Pointer type qualifiers can only operate on pointer types, but not
4290aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  // pointer-to-member types.
4291aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  if (!isa<PointerType>(Desugared)) {
4292aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    S.Diag(Attr.getLoc(), Type->isMemberPointerType() ?
4293aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                          diag::err_attribute_no_member_pointers :
4294aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                          diag::err_attribute_pointers_only) << Attr.getName();
4295aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return true;
4296aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  }
4297aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4298aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  AttributedType::Kind TAK;
4299aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  switch (Kind) {
4300aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  default: llvm_unreachable("Unknown attribute kind");
4301aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_Ptr32: TAK = AttributedType::attr_ptr32; break;
4302aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_Ptr64: TAK = AttributedType::attr_ptr64; break;
4303aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_SPtr: TAK = AttributedType::attr_sptr; break;
4304aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_UPtr: TAK = AttributedType::attr_uptr; break;
4305aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  }
4306aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4307aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  Type = S.Context.getAttributedType(TAK, Type, Type);
4308aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  return false;
4309aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman}
4310aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
43115b92696c8f1f8ef943ad87397b95c031b5787305Reid Klecknerstatic AttributedType::Kind getCCTypeAttrKind(AttributeList &Attr) {
43125b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  assert(!Attr.isInvalid());
43135b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  switch (Attr.getKind()) {
43145b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  default:
43155b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    llvm_unreachable("not a calling convention attribute");
43165b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_CDecl:
43175b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_cdecl;
43185b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_FastCall:
43195b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_fastcall;
43205b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_StdCall:
43215b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_stdcall;
43225b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_ThisCall:
43235b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_thiscall;
43245b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_Pascal:
43255b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_pascal;
43265b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_Pcs: {
43275b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    // We know attr is valid so it can only have one of two strings args.
43285b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    StringLiteral *Str = cast<StringLiteral>(Attr.getArg(0));
43295b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return llvm::StringSwitch<AttributedType::Kind>(Str->getString())
43305b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner        .Case("aapcs", AttributedType::attr_pcs)
43315b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner        .Case("aapcs-vfp", AttributedType::attr_pcs_vfp);
43325b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  }
43335b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_PnaclCall:
43345b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_pnaclcall;
43355b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_IntelOclBicc:
43365b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_inteloclbicc;
43375b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  }
43385b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  llvm_unreachable("unexpected attribute kind!");
43395b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner}
43405b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner
4341711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
4342711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
4343711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
4344711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
4345711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
4346711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
4347e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4348711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
43492455636163fdd18581d7fdae816433f886d88213Mike Stump
43508e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NoReturn) {
4351711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
435204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
4353e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4354e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
4355711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
4356711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
4357425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
4358425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
4359711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
4360711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4361711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4362711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
4363425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
4364f85e193739c953358c865005855253af4f68a497John McCall  // ns_returns_retained is not always a type attribute, but if we got
4365f85e193739c953358c865005855253af4f68a497John McCall  // here, we're treating it as one right now.
43668e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NSReturnsRetained) {
43674e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(S.getLangOpts().ObjCAutoRefCount &&
4368f85e193739c953358c865005855253af4f68a497John McCall           "ns_returns_retained treated as type attribute in non-ARC");
4369f85e193739c953358c865005855253af4f68a497John McCall    if (attr.getNumArgs()) return true;
4370f85e193739c953358c865005855253af4f68a497John McCall
4371f85e193739c953358c865005855253af4f68a497John McCall    // Delay if this is not a function type.
4372f85e193739c953358c865005855253af4f68a497John McCall    if (!unwrapped.isFunctionType())
4373f85e193739c953358c865005855253af4f68a497John McCall      return false;
4374f85e193739c953358c865005855253af4f68a497John McCall
4375f85e193739c953358c865005855253af4f68a497John McCall    FunctionType::ExtInfo EI
4376f85e193739c953358c865005855253af4f68a497John McCall      = unwrapped.get()->getExtInfo().withProducesResult(true);
4377f85e193739c953358c865005855253af4f68a497John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4378f85e193739c953358c865005855253af4f68a497John McCall    return true;
4379f85e193739c953358c865005855253af4f68a497John McCall  }
4380f85e193739c953358c865005855253af4f68a497John McCall
43818e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_Regparm) {
4382711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
4383711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
4384711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
43851e030eb1194763b42c1752723be23b1515f48981John McCall
4386711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
4387711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
4388008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
43891e030eb1194763b42c1752723be23b1515f48981John McCall
4390ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Diagnose regparm with fastcall.
4391ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    const FunctionType *fn = unwrapped.get();
4392ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    CallingConv CC = fn->getCallConv();
4393ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (CC == CC_X86FastCall) {
4394ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
4395ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC)
4396ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm";
4397ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
4398ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
4399ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
4400ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
440191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    FunctionType::ExtInfo EI =
4402711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
4403711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4404711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4405425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
4406425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
440782bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  // Delay if the type didn't work out to a function.
440882bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  if (!unwrapped.isFunctionType()) return false;
440982bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman
441004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
4411711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
4412711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
4413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4414f82b4e85b1219295cad4b5851b035575bc293010John McCall
4415711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
4416711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
4417064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis  if (S.Context.getCanonicalCallConv(CC) ==
4418e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      S.Context.getCanonicalCallConv(CCOld)) {
4419ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
4420ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4421711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4422e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  }
442304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
44248e68f1c8a2919ea83c2053731d6011074f1062e1Roman Divacky  if (CCOld != (S.LangOpts.MRTD ? CC_X86StdCall : CC_Default)) {
442504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Should we diagnose reapplications of the same convention?
4426711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
442704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CC)
442804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CCOld);
4429711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4430711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
443104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
443204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
443304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
443404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
4435711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isa<FunctionNoProtoType>(fn)) {
4436711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_knr)
443704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
4438711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
4439711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
444004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
444104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
4442711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
444304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
4444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
444504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
4446711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
4447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
444804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
4449ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
4450ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Also diagnose fastcall with regparm.
4451a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    if (fn->getHasRegParm()) {
4452ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
4453ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm"
4454ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC);
4455ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
4456ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
4457ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
445804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
4459f82b4e85b1219295cad4b5851b035575bc293010John McCall
44605b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  // Modify the CC from the wrapped function type, wrap it all back, and then
44615b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  // wrap the whole thing in an AttributedType as written.  The modified type
44625b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  // might have a different CC if we ignored the attribute.
4463711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
44645b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  QualType Equivalent =
44655b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner      unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
44665b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  type = S.Context.getAttributedType(getCCTypeAttrKind(attr), type, Equivalent);
4467711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
4468f82b4e85b1219295cad4b5851b035575bc293010John McCall}
4469f82b4e85b1219295cad4b5851b035575bc293010John McCall
4470207f4d8543529221932af82836016a2ef066c917Peter Collingbourne/// Handle OpenCL image access qualifiers: read_only, write_only, read_write
4471207f4d8543529221932af82836016a2ef066c917Peter Collingbournestatic void HandleOpenCLImageAccessAttribute(QualType& CurType,
4472207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const AttributeList &Attr,
4473207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             Sema &S) {
4474207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  // Check the attribute arguments.
4475207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (Attr.getNumArgs() != 1) {
4476baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4477baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
4478207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4479207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4480207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4481207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
4482207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  llvm::APSInt arg(32);
4483207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4484207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      !sizeExpr->isIntegerConstantExpr(arg, S.Context)) {
44859f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
44869f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
44879f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << sizeExpr->getSourceRange();
4488207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4489207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4490207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4491207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  unsigned iarg = static_cast<unsigned>(arg.getZExtValue());
4492207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  switch (iarg) {
4493207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_only:
4494207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_write_only:
4495207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_write:
4496207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4497207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4498207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  default:
4499207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4500207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
4501207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << sizeExpr->getSourceRange();
4502207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4503207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4504207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4505207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
4506207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
45076e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
45086e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
45096e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
45106e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
45116e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
45126e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
45136e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
4514788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
4515788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
451656affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
45176e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
4518baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4519baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
4520e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
45216e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
45226e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
45236e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
45246e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
4525ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4526ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
45279f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
45289f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
45299f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << sizeExpr->getSourceRange();
4530e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
45316e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
45326e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
45336e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
45341652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman  if (!CurType->isBuiltinType() ||
45351652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
45366e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
4537e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
45386e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
45396e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
45406e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
45416e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
45426e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
45436e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
45446e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
45456e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
45466e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
45476e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4548e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
45496e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
45506e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
45511652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman  if (VectorType::isVectorSizeTooLarge(vectorSize / typeSize)) {
45521652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    S.Diag(Attr.getLoc(), diag::err_attribute_size_too_large)
45531652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      << sizeExpr->getSourceRange();
45541652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    Attr.setInvalid();
45551652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    return;
45561652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman  }
45576e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
45586e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
45596e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4560e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
45616e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
45626e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
45636e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
45646e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
45656e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
4566788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
4567e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
45686e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
45696e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
45704ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// \brief Process the OpenCL-like ext_vector_type attribute when it occurs on
45714ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// a type.
457291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierstatic void HandleExtVectorTypeAttr(QualType &CurType,
457391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                                    const AttributeList &Attr,
45744ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                    Sema &S) {
45754ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  Expr *sizeExpr;
457691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
45774ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Special case where the argument is a template id.
45784ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (Attr.getParameterName()) {
45794ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CXXScopeSpec SS;
4580e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    SourceLocation TemplateKWLoc;
45814ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    UnqualifiedId id;
45824ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
4583e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
4584e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    ExprResult Size = S.ActOnIdExpression(S.getCurScope(), SS, TemplateKWLoc,
4585e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          id, false, false);
45864ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Size.isInvalid())
45874ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
458891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
45894ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Size.get();
45904ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  } else {
45914ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    // check the attribute arguments.
45924ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Attr.getNumArgs() != 1) {
4593baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4594baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman        << Attr.getName() << 1;
45954ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
45964ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    }
45974ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Attr.getArg(0);
45984ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  }
459991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
46004ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Create the vector type.
46014ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  QualType T = S.BuildExtVectorType(CurType, sizeExpr, Attr.getLoc());
46024ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (!T.isNull())
46034ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CurType = T;
46044ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor}
46054ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
4606b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northoverstatic bool isPermittedNeonBaseType(QualType &Ty,
4607b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover                                    VectorType::VectorKind VecKind,
4608b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover                                    bool IsAArch64) {
4609b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  const BuiltinType *BTy = Ty->getAs<BuiltinType>();
4610b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  if (!BTy)
4611b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    return false;
4612b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
4613b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  if (VecKind == VectorType::NeonPolyVector) {
4614b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    if (IsAArch64) {
4615b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      // AArch64 polynomial vectors are unsigned
4616b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      return BTy->getKind() == BuiltinType::UChar ||
4617b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover             BTy->getKind() == BuiltinType::UShort;
4618b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    } else {
4619b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      // AArch32 polynomial vector are signed.
4620b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      return BTy->getKind() == BuiltinType::SChar ||
4621b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover             BTy->getKind() == BuiltinType::Short;
4622b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    }
4623b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  }
4624b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
4625b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  // Non-polynomial vector types: the usual suspects are allowed, as well as
4626b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  // float64_t on AArch64.
4627b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  if (IsAArch64 && BTy->getKind() == BuiltinType::Double)
4628b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    return true;
4629b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
4630b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  return BTy->getKind() == BuiltinType::SChar ||
4631b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::UChar ||
4632b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Short ||
4633b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::UShort ||
4634b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Int ||
4635b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::UInt ||
4636b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::LongLong ||
4637b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::ULongLong ||
4638b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Float ||
4639b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Half;
4640b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover}
4641b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
46424211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
46434211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
46444211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
46454211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
46464211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
46474211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
46484211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
46494211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
46504211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
46519f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman                                     VectorType::VectorKind VecKind) {
46524211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
46534211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
4654baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4655baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
46564211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
46574211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
46584211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
46594211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
46604211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
46614211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
46624211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
46634211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
46649f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
46659f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
46669f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << numEltsExpr->getSourceRange();
46674211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
46684211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
46694211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
46704211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
4671e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover  llvm::Triple::ArchType Arch =
4672b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover        S.Context.getTargetInfo().getTriple().getArch();
4673e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover  if (!isPermittedNeonBaseType(CurType, VecKind,
4674e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover                               Arch == llvm::Triple::aarch64)) {
4675e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
4676e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover    Attr.setInvalid();
4677e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover    return;
46784211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
4679e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover
46804211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
46814211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
46824211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
46834211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
46844211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
46854211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
46864211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
46874211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
46884211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
46894211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
46904211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
46914211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
46924211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
4693711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
4694f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             TypeAttrLocation TAL, AttributeList *attrs) {
4695c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
4696c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
4697c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
4698c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
4699711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4700711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
4701711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
4702711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
4703711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr.getNext();
4704711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4705e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
4706711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
4707e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
4708e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
4709cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    if (attr.isCXX11Attribute()) {
4710cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // [[gnu::...]] attributes are treated as declaration attributes, so may
4711cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // not appertain to a DeclaratorChunk, even if we handle them as type
4712cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // attributes.
4713cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.getScopeName() && attr.getScopeName()->isStr("gnu")) {
4714cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        if (TAL == TAL_DeclChunk) {
4715cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          state.getSema().Diag(attr.getLoc(),
4716cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                               diag::warn_cxx11_gnu_attribute_on_type)
4717cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith              << attr.getName();
4718cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          continue;
4719cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        }
4720cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      } else if (TAL != TAL_DeclChunk) {
4721cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // Otherwise, only consider type processing for a C++11 attribute if
4722cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // it's actually been applied to a type.
4723cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        continue;
4724cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      }
4725f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    }
4726f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
4727b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
4728b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
4729711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
4730cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    default:
4731cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // A C++11 attribute on a declarator chunk must appertain to a type.
4732d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk) {
4733cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(), diag::err_attribute_not_type_attr)
4734d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith          << attr.getName();
4735d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith        attr.setUsedAsTypeAttr();
4736d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      }
4737cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
4738cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
4739cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::UnknownAttribute:
4740cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk)
4741cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(),
4742cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                             diag::warn_unknown_attribute_ignored)
4743cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          << attr.getName();
4744cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
4745cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
4746cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::IgnoredAttribute:
4747cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
474804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
47498e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_MayAlias:
4750682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // FIXME: This attribute needs to actually be handled, but if we ignore
4751682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // it it breaks large amounts of Linux software.
4752682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      attr.setUsedAsTypeAttr();
4753682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      break;
47548e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_AddressSpace:
4755711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
4756e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4757c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
4758711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
4759711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
4760711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
4761e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4762d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
47638e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_VectorSize:
4764711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
4765e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4766f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
47678e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ExtVectorType:
4768a4fa9008988985f9cf01712a99ddd923aea278a0Richard Smith      HandleExtVectorTypeAttr(type, attr, state.getSema());
4769e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
47704ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      break;
47718e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonVectorType:
4772711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
47739f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman                               VectorType::NeonVector);
4774e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
47754211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
47768e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonPolyVectorType:
4777711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
47789f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman                               VectorType::NeonPolyVector);
4779e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
47804211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
47818e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_OpenCLImageAccess:
4782207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      HandleOpenCLImageAccessAttribute(type, attr, state.getSema());
4783e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4784207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      break;
4785207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
4786d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith    case AttributeList::AT_Win64:
4787d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      attr.setUsedAsTypeAttr();
4788d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      break;
4789aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    MS_TYPE_ATTRS_CASELIST:
4790aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      if (!handleMSPointerTypeQualifierAttr(state, attr, type))
4791aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        attr.setUsedAsTypeAttr();
4792aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      break;
4793d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith
47948e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
47954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
4796cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        break;
4797f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough into the function attrs
4798f85e193739c953358c865005855253af4f68a497John McCall
4799711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
4800e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4801e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
4802711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
4803711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
4804f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      if (TAL == TAL_DeclSpec)
4805711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
4806711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4807711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
4808711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
4809711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
48106e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
4811c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
4812711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attrs = next));
4813232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
4814232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
4815e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \brief Ensure that the type of the given expression is complete.
4816e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4817e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// This routine checks whether the expression \p E has a complete type. If the
4818e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// expression refers to an instantiable construct, that instantiation is
4819e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// performed as needed to complete its type. Furthermore
4820e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// Sema::RequireCompleteType is called for the expression's type (or in the
4821e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// case of a reference type, the referred-to type).
4822e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4823e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \param E The expression whose type is required to be complete.
4824d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// \param Diagnoser The object that will emit a diagnostic if the type is
4825d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// incomplete.
4826e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4827e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \returns \c true if the type of \p E is incomplete and diagnosed, \c false
4828e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// otherwise.
4829f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, TypeDiagnoser &Diagnoser){
4830e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  QualType T = E->getType();
4831e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4832e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Fast path the case where the type is already complete.
4833e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (!T->isIncompleteType())
4834e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    return false;
4835e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4836e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Incomplete array types may be completed by the initializer attached to
4837e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // their definitions. For static data members of class templates we need to
4838e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // instantiate the definition to get this initializer and complete the type.
4839e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (T->isIncompleteArrayType()) {
4840e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4841e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
4842e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        if (Var->isStaticDataMember() &&
4843e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth            Var->getInstantiatedFromStaticDataMember()) {
484491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
484536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
484636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          assert(MSInfo && "Missing member specialization information?");
484736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          if (MSInfo->getTemplateSpecializationKind()
484836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                != TSK_ExplicitSpecialization) {
484936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // If we don't already have a point of instantiation, this is it.
485036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (MSInfo->getPointOfInstantiation().isInvalid()) {
485136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              MSInfo->setPointOfInstantiation(E->getLocStart());
485291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
485391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier              // This is a modification of an existing AST node. Notify
485436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              // listeners.
485536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              if (ASTMutationListener *L = getASTMutationListener())
485636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                L->StaticDataMemberInstantiated(Var);
485736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
485891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
485936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            InstantiateStaticDataMemberDefinition(E->getExprLoc(), Var);
486091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
486136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // Update the type to the newly instantiated definition's type both
486236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // here and within the expression.
486336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (VarDecl *Def = Var->getDefinition()) {
486436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setDecl(Def);
486536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              T = Def->getType();
486636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setType(T);
486736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              E->setType(T);
486836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
4869f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor          }
487091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4871e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // We still go on to try to complete the type independently, as it
4872e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // may also require instantiations or diagnostics if it remains
4873e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // incomplete.
4874e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        }
4875e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      }
4876e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    }
4877e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  }
4878e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4879e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // FIXME: Are there other cases which require instantiating something other
4880e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // than the type to complete the type of an expression?
4881e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4882e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Look through reference types and complete the referred type.
4883e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
4884e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    T = Ref->getPointeeType();
4885e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4886d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(E->getExprLoc(), T, Diagnoser);
4887d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4888d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4889d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregornamespace {
4890f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  struct TypeDiagnoserDiag : Sema::TypeDiagnoser {
4891d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    unsigned DiagID;
489291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4893f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor    TypeDiagnoserDiag(unsigned DiagID)
4894f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor      : Sema::TypeDiagnoser(DiagID == 0), DiagID(DiagID) {}
489591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4896d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) {
4897d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      if (Suppressed) return;
4898d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      S.Diag(Loc, DiagID) << T;
4899d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    }
4900d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  };
4901d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4902d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4903d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, unsigned DiagID) {
4904f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4905d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteExprType(E, Diagnoser);
4906e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth}
4907e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
49081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
49094ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
49104ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
49114ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
491286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
491386447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
491486447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
491586447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
491686447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
49174ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
49184ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
49194ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
49204ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
49214ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
49224ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
49234ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
49244ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
492591a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
4926f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                               TypeDiagnoser &Diagnoser) {
4927658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  if (RequireCompleteTypeImpl(Loc, T, Diagnoser))
4928658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie    return true;
4929658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  if (const TagType *Tag = T->getAs<TagType>()) {
4930658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie    if (!Tag->getDecl()->isCompleteDefinitionRequired()) {
4931658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie      Tag->getDecl()->setCompleteDefinitionRequired();
4932658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie      Consumer.HandleTagDeclRequiredDefinition(Tag->getDecl());
4933658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie    }
4934658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  }
4935658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  return false;
4936658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie}
4937658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie
4938658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie/// \brief The implementation of RequireCompleteType
4939658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikiebool Sema::RequireCompleteTypeImpl(SourceLocation Loc, QualType T,
4940658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie                                   TypeDiagnoser &Diagnoser) {
4941573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
4942573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
4943690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
4944690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
4945690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
49461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
4947690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
4948690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
49494ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
4950d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  NamedDecl *Def = 0;
4951d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  if (!T->isIncompleteType(&Def)) {
4952d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    // If we know about the definition but it is not visible, complain.
4953b775100fea6d8955149897dae1adca50ca471d17Richard Smith    if (!Diagnoser.Suppressed && Def && !LookupResult::isVisible(*this, Def)) {
4954d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // Suppress this error outside of a SFINAE context if we've already
495591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // emitted the error once for this type. There's no usefulness in
4956d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // repeating the diagnostic.
4957d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // FIXME: Add a Fix-It that imports the corresponding module or includes
4958d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // the header.
4959ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Module *Owner = Def->getOwningModule();
4960ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Loc, diag::err_module_private_definition)
4961ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        << T << Owner->getFullModuleName();
4962ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Def->getLocation(), diag::note_previous_definition);
4963ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
4964ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      if (!isSFINAEContext()) {
4965ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        // Recover by implicitly importing this module.
4966ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        createImplicitModuleImport(Loc, Owner);
4967d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      }
4968d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    }
496991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
49704ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
4971d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
49724ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
4973bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const TagType *Tag = T->getAs<TagType>();
4974bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const ObjCInterfaceType *IFace = 0;
497591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4976bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  if (Tag) {
4977bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4978bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->isInvalidDecl())
4979bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
4980bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan
4981bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4982bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->hasExternalLexicalStorage()) {
4983bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(Tag->getDecl());
4984bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!Tag->isIncompleteType())
4985bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4986bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4987bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
4988bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  else if ((IFace = T->getAs<ObjCInterfaceType>())) {
4989bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4990bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->isInvalidDecl())
4991bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
499291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4993bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4994bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->hasExternalLexicalStorage()) {
4995bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(IFace->getDecl());
4996bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!IFace->isIncompleteType())
4997bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4998bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4999bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
500091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5001d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
5002923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
5003923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
5004923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
5005e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor  while (const ConstantArrayType *Array
5006e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor           = Context.getAsConstantArrayType(MaybeTemplate))
5007923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
5008923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
50092943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
5010d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
5011972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
5012972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
5013d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
5014d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                            /*Complain=*/!Diagnoser.Suppressed);
50151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
5016d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
5017564f4c5664f552becbd05407611a92754c40e641Richard Smith      CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass();
5018564f4c5664f552becbd05407611a92754c40e641Richard Smith      if (!Rec->isBeingDefined() && Pattern) {
5019564f4c5664f552becbd05407611a92754c40e641Richard Smith        MemberSpecializationInfo *MSI = Rec->getMemberSpecializationInfo();
5020564f4c5664f552becbd05407611a92754c40e641Richard Smith        assert(MSI && "Missing member specialization information?");
5021357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
5022564f4c5664f552becbd05407611a92754c40e641Richard Smith        if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
5023f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
5024f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
5025f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
5026d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                  /*Complain=*/!Diagnoser.Suppressed);
5027d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
5028d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
5029d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
50302943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
5031d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  if (Diagnoser.Suppressed)
50325842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
5033d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
50344ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
5035cac18add73d095eaab600aefe27ea7174aec4922Nico Weber  if (Ident___float128 &&
5036cac18add73d095eaab600aefe27ea7174aec4922Nico Weber      T == Context.getTypeDeclType(Context.getFloat128StubType())) {
5037cac18add73d095eaab600aefe27ea7174aec4922Nico Weber    Diag(Loc, diag::err_typecheck_decl_incomplete_type___float128);
5038cac18add73d095eaab600aefe27ea7174aec4922Nico Weber    return true;
5039cac18add73d095eaab600aefe27ea7174aec4922Nico Weber  }
5040cac18add73d095eaab600aefe27ea7174aec4922Nico Weber
5041d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
504291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
50434ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
504401620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
50454ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
50461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
50474ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
50484ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
5049b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor      << QualType(Tag, 0);
505091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5051b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  // If the Objective-C class was a forward declaration, produce a note.
5052b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  if (IFace && !IFace->getDecl()->isInvalidDecl())
5053b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor    Diag(IFace->getDecl()->getLocation(), diag::note_forward_class);
50544ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
50555d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain  // If we have external information that we can use to suggest a fix,
50565d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain  // produce a note.
50575d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain  if (ExternalSource)
50585d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain    ExternalSource->MaybeDiagnoseMissingCompleteType(Loc, T);
50595d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain
50604ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
50614ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
5062e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
5063fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
506491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                               unsigned DiagID) {
5065f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
5066d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(Loc, T, Diagnoser);
5067fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
5068fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
50696666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \brief Get diagnostic %select index for tag kind for
50706666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// literal type diagnostic message.
50716666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// WARNING: Indexes apply to particular diagnostics only!
50726666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos///
50736666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \returns diagnostic %select index.
5074f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matosstatic unsigned getLiteralDiagFromTagKind(TagTypeKind Tag) {
50756666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  switch (Tag) {
5076f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Struct: return 0;
5077f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Interface: return 1;
5078f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Class:  return 2;
5079f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  default: llvm_unreachable("Invalid tag kind for literal type diagnostic!");
50806666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  }
50816666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos}
50826666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos
50839f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @brief Ensure that the type T is a literal type.
50849f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
50859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// This routine checks whether the type @p T is a literal type. If @p T is an
50869f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// incomplete type, an attempt is made to complete it. If @p T is a literal
50879f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// type, or @p AllowIncompleteType is true and @p T is an incomplete type,
50889f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// returns false. Otherwise, this routine issues the diagnostic @p PD (giving
50899f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// it the type @p T), along with notes explaining why the type is not a
50909f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// literal type, and returns true.
50919f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
50929f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param Loc  The location in the source that the non-literal type
50939f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// diagnostic should refer to.
50949f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
50959f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param T  The type that this routine is examining for literalness.
50969f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
5097f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor/// @param Diagnoser Emits a diagnostic if T is not a literal type.
50989f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
50999f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @returns @c true if @p T is not a literal type and a diagnostic was emitted,
51009f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @c false otherwise.
51019f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithbool Sema::RequireLiteralType(SourceLocation Loc, QualType T,
5102f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                              TypeDiagnoser &Diagnoser) {
51039f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  assert(!T->isDependentType() && "type should not be dependent");
51049f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5105ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  QualType ElemType = Context.getBaseElementType(T);
5106ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  RequireCompleteType(Loc, ElemType, 0);
5107ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
5108a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  if (T->isLiteralType(Context))
51099f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return false;
51109f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5111f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  if (Diagnoser.Suppressed)
51129f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
51139f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5114f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
51159f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
51169f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (T->isVariableArrayType())
51179f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
51189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5119ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  const RecordType *RT = ElemType->getAs<RecordType>();
51209f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (!RT)
51219f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
51229f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
51239f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
51249f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5125c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // A partially-defined class type can't be a literal type, because a literal
5126c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // class type must have a trivial destructor (which can't be checked until
5127c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // the class definition is complete).
5128c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  if (!RD->isCompleteDefinition()) {
5129d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T);
5130ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman    return true;
5131c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  }
5132ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
51339f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // If the class has virtual base classes, then it's not an aggregate, and
513486c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // cannot have any constexpr constructors or a trivial default constructor,
513586c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // so is non-literal. This is better to diagnose than the resulting absence
513686c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // of constexpr constructors.
51379f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (RD->getNumVBases()) {
51389f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_virtual_base)
51396666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      << getLiteralDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases();
51409f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
51419f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           E = RD->vbases_end(); I != E; ++I)
514296a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      Diag(I->getLocStart(),
51439f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           diag::note_constexpr_virtual_base_here) << I->getSourceRange();
514486c3ae46250cdcc57778c27826060779a92f3815Richard Smith  } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor() &&
514586c3ae46250cdcc57778c27826060779a92f3815Richard Smith             !RD->hasTrivialDefaultConstructor()) {
51469f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD;
51479f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (RD->hasNonLiteralTypeFieldsOrBases()) {
51489f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
51499f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->bases_end(); I != E; ++I) {
5150a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith      if (!I->getType()->isLiteralType(Context)) {
515196a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        Diag(I->getLocStart(),
51529f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith             diag::note_non_literal_base_class)
51539f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith          << RD << I->getType() << I->getSourceRange();
51549f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
51559f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
51569f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
51579f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::field_iterator I = RD->field_begin(),
51589f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->field_end(); I != E; ++I) {
5159a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith      if (!I->getType()->isLiteralType(Context) ||
5160262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          I->getType().isVolatileQualified()) {
5161262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        Diag(I->getLocation(), diag::note_non_literal_field)
5162581deb3da481053c4993c7600f97acf7768caac5David Blaikie          << RD << *I << I->getType()
5163262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          << I->getType().isVolatileQualified();
51649f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
51659f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
51669f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
51679f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (!RD->hasTrivialDestructor()) {
51689f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // All fields and bases are of literal types, so have trivial destructors.
51699f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // If this class's destructor is non-trivial it must be user-declared.
51709f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    CXXDestructorDecl *Dtor = RD->getDestructor();
51719f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    assert(Dtor && "class has literal fields and bases but no dtor?");
51729f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    if (!Dtor)
51739f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      return true;
51749f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
51759f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(Dtor->getLocation(), Dtor->isUserProvided() ?
51769f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_user_provided_dtor :
51779f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_nontrivial_dtor) << RD;
5178ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    if (!Dtor->isUserProvided())
5179ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith      SpecialMemberIsTrivial(Dtor, CXXDestructor, /*Diagnose*/true);
51809f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  }
51819f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
51829f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return true;
51839f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
51849f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
518591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierbool Sema::RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID) {
5186f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
5187f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  return RequireLiteralType(Loc, T, Diagnoser);
5188f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor}
5189f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor
5190465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
5191465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
5192465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
5193465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
5194465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
5195e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
5196465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
5197e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
5198465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5199465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
5200465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
5201465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
5202465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
5203465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
5204465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
5205e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
5206af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
52072a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
5208fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
52092a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
52102a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
52112a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
52122b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
52132b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
5214aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
5215aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
52162b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
5217af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
5218af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
5219af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
5220f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// getDecltypeForExpr - Given an expr, will return the decltype for
5221f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// that expression, according to the rules in C++11
5222f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18.
5223f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregorstatic QualType getDecltypeForExpr(Sema &S, Expr *E) {
5224f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (E->isTypeDependent())
5225f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    return S.Context.DependentTy;
5226f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
52276d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
52286d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   The type denoted by decltype(e) is defined as follows:
52296d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //
52306d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //     - if e is an unparenthesized id-expression or an unparenthesized class
523191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       member access (5.2.5), decltype(e) is the type of the entity named
523291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       by e. If there is no such entity, or if e names a set of overloaded
52336d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       functions, the program is ill-formed;
523484dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  //
523584dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  // We apply the same rules for Objective-C ivar and property references.
5236f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
5237f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
5238f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return VD->getType();
523984dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
5240f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
5241f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return FD->getType();
524284dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCIvarRefExpr *IR = dyn_cast<ObjCIvarRefExpr>(E)) {
524384dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    return IR->getDecl()->getType();
524484dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCPropertyRefExpr *PR = dyn_cast<ObjCPropertyRefExpr>(E)) {
524584dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    if (PR->isExplicitProperty())
524684dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor      return PR->getExplicitProperty()->getType();
5247f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
524884dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor
5249f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  // C++11 [expr.lambda.prim]p18:
5250f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   Every occurrence of decltype((x)) where x is a possibly
5251f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   parenthesized id-expression that names an entity of automatic
5252f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   storage duration is treated as if x were transformed into an
5253f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   access to a corresponding data member of the closure type that
5254f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   would have been declared if x were an odr-use of the denoted
5255f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   entity.
5256f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  using namespace sema;
5257f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (S.getCurLambda()) {
5258f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (isa<ParenExpr>(E)) {
5259f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
5260f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
526168932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          QualType T = S.getCapturedDeclRefType(Var, DRE->getLocation());
526268932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          if (!T.isNull())
526368932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor            return S.Context.getLValueReferenceType(T);
5264f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        }
5265f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      }
5266f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    }
5267f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
5268f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
5269f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
52706d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
52716d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   [...]
52726d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  QualType T = E->getType();
52736d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  switch (E->getValueKind()) {
527491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the
52756d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
52766d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_XValue: T = S.Context.getRValueReferenceType(T); break;
527791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an lvalue, decltype(e) is T&, where T is the
52786d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
52796d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_LValue: T = S.Context.getLValueReferenceType(T); break;
52806d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //  - otherwise, decltype(e) is the type of e.
52816d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_RValue: break;
52826d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  }
528391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5284f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return T;
5285f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor}
5286f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
52872a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
5288fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
52892a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
52902a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
529191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5292f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return Context.getDecltypeType(E, getDecltypeForExpr(*this, E));
5293af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
5294ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
5295ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType Sema::BuildUnaryTransformType(QualType BaseType,
5296ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::UTTKind UKind,
5297ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       SourceLocation Loc) {
5298ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  switch (UKind) {
5299ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case UnaryTransformType::EnumUnderlyingType:
5300ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) {
5301ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Diag(Loc, diag::err_only_enums_have_underlying_types);
5302ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return QualType();
5303ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    } else {
5304ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      QualType Underlying = BaseType;
5305ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      if (!BaseType->isDependentType()) {
5306ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl();
5307ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        assert(ED && "EnumType has no EnumDecl");
5308ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        DiagnoseUseOfDecl(ED, Loc);
5309ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        Underlying = ED->getIntegerType();
5310ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      }
5311ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(!Underlying.isNull());
5312ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return Context.getUnaryTransformType(BaseType, Underlying,
5313ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                        UnaryTransformType::EnumUnderlyingType);
5314ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
5315ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  }
5316ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  llvm_unreachable("unknown unary transform type");
5317ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
5318b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5319b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) {
5320b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T->isDependentType()) {
53218327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // FIXME: It isn't entirely clear whether incomplete atomic types
53228327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // are allowed or not; for simplicity, ban them for the moment.
5323d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (RequireCompleteType(Loc, T, diag::err_atomic_specifier_bad_type, 0))
53248327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith      return QualType();
53258327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith
5326b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    int DisallowedKind = -1;
53278327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    if (T->isArrayType())
5328b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 1;
5329b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isFunctionType())
5330b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 2;
5331b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isReferenceType())
5332b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 3;
5333b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isAtomicType())
5334b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 4;
5335b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T.hasQualifiers())
5336b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 5;
5337b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (!T.isTriviallyCopyableType(Context))
5338b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      // Some other non-trivially-copyable type (probably a C++ class)
5339b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 6;
5340b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5341b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (DisallowedKind != -1) {
5342b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T;
5343b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return QualType();
5344b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
5345b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5346b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // FIXME: Do we need any handling for ARC here?
5347b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
5348b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5349b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Build the pointer type.
5350b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return Context.getAtomicType(T);
5351b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
5352