SemaType.cpp revision b445cb9662067b7c7586815937b07828ede9bb49
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.
84624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  IdentifierInfo *II = attr.isArgIdent(0) ? attr.getArgAsIdent(0)->Ident : 0;
85624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (useExpansionLoc && loc.isMacroID() && II) {
86624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    if (II->isStr("strong")) {
87834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__strong")) name = "__strong";
88624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    } else if (II->isStr("weak")) {
89834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__weak")) name = "__weak";
902792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    }
912792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  }
922792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
93fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  S.Diag(loc, diag::warn_type_attribute_wrong_type) << name << WhichType
94fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman    << type;
952792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall}
962792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
97711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// objc_gc applies to Objective-C pointers or, otherwise, to the
98711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// smallest available pointer type (i.e. 'void*' in 'void**').
99711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
1008e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ObjCGC: \
1018e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ObjCOwnership
102711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
103711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// Function type attributes.
104711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define FUNCTION_TYPE_ATTRS_CASELIST \
1058e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NoReturn: \
1068e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_CDecl: \
1078e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_FastCall: \
1088e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_StdCall: \
1098e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ThisCall: \
1108e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Pascal: \
111e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    case AttributeList::AT_MSABI: \
112e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    case AttributeList::AT_SysVABI: \
1138e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Regparm: \
114263366f9241366f29ba65b703120f302490c39ffDerek Schuff    case AttributeList::AT_Pcs: \
11538980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    case AttributeList::AT_PnaclCall: \
116aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_IntelOclBicc
117aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
118aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman// Microsoft-specific type qualifiers.
119aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman#define MS_TYPE_ATTRS_CASELIST  \
120aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_Ptr32: \
121aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_Ptr64: \
122aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_SPtr: \
123aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_UPtr
124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
125711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace {
126711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// An object which stores processing state for the entire
127711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// GetTypeForDeclarator process.
128711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  class TypeProcessingState {
129711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &sema;
130711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
131711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The declarator being processed.
132711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &declarator;
133711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
134711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The index of the declarator chunk we're currently processing.
135711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// May be the total number of valid chunks, indicating the
136711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// DeclSpec.
137711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex;
138711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
139711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Whether there are non-trivial modifications to the decl spec.
140711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    bool trivial;
141711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1427ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    /// Whether we saved the attributes in the decl spec.
1437ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    bool hasSavedAttrs;
1447ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
145711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The original set of attributes on the DeclSpec.
1465f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> savedAttrs;
147711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
148711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// A list of attributes to diagnose the uselessness of when the
149711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// processing is complete.
1505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> ignoredTypeAttrs;
151711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
152711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  public:
153711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    TypeProcessingState(Sema &sema, Declarator &declarator)
154711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : sema(sema), declarator(declarator),
155711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        chunkIndex(declarator.getNumTypeObjects()),
1567ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        trivial(true), hasSavedAttrs(false) {}
157711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
158711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &getSema() const {
159711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return sema;
160711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
161711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &getDeclarator() const {
163711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator;
164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
166b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    bool isProcessingDeclSpec() const {
167b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      return chunkIndex == declarator.getNumTypeObjects();
168b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
169b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
170711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned getCurrentChunkIndex() const {
171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return chunkIndex;
172711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void setCurrentChunkIndex(unsigned idx) {
175711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(idx <= declarator.getNumTypeObjects());
176711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      chunkIndex = idx;
177711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
178711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
179711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&getCurrentAttrListRef() const {
180b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (isProcessingDeclSpec())
181711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        return getMutableDeclSpec().getAttributes().getListRef();
182711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator.getTypeObject(chunkIndex).getAttrListRef();
183711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
185711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Save the current set of attributes on the DeclSpec.
186711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void saveDeclSpecAttrs() {
187711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Don't try to save them multiple times.
1887ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (hasSavedAttrs) return;
189711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
190711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      DeclSpec &spec = getMutableDeclSpec();
191711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (AttributeList *attr = spec.getAttributes().getList(); attr;
192711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             attr = attr->getNext())
193711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs.push_back(attr);
194711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      trivial &= savedAttrs.empty();
1957ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      hasSavedAttrs = true;
196711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
197711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
198711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Record that we had nowhere to put the given type attribute.
199711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// We will diagnose such attributes later.
200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void addIgnoredTypeAttr(AttributeList &attr) {
201711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      ignoredTypeAttrs.push_back(&attr);
202711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
203711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
204711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Diagnose all the ignored type attributes, given that the
205711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// declarator worked out to the given type.
206711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void diagnoseIgnoredTypeAttrs(QualType type) const {
2075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      for (SmallVectorImpl<AttributeList*>::const_iterator
208711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
2092792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall           i != e; ++i)
2102792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall        diagnoseBadTypeAttribute(getSema(), **i, type);
211711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
212711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
213711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    ~TypeProcessingState() {
214711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (trivial) return;
215711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
216711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      restoreDeclSpecAttrs();
217711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
218711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
219711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  private:
220711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclSpec &getMutableDeclSpec() const {
221711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return const_cast<DeclSpec&>(declarator.getDeclSpec());
222711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
223711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
224711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void restoreDeclSpecAttrs() {
2257ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      assert(hasSavedAttrs);
2267ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
2277ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (savedAttrs.empty()) {
2287ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        getMutableDeclSpec().getAttributes().set(0);
2297ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        return;
2307ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      }
2317ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
232711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
233711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
234711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs[i]->setNext(savedAttrs[i+1]);
235711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      savedAttrs.back()->setNext(0);
236711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
237711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
238711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
239711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
240711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
241711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  attr.setNext(head);
242711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  head = &attr;
243711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
244711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
245711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
246711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (head == &attr) {
247711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    head = attr.getNext();
248711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
24904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
250711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
251711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *cur = head;
252711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  while (true) {
253711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    assert(cur && cur->getNext() && "ran out of attrs?");
254711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (cur->getNext() == &attr) {
255711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      cur->setNext(attr.getNext());
256711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
257711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
258711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    cur = cur->getNext();
259711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
260711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
261711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
262711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void moveAttrFromListToList(AttributeList &attr,
263711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&fromList,
264711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&toList) {
265711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, fromList);
266711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrIntoList(attr, toList);
267711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
268711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
269f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith/// The location of a type attribute.
270f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smithenum TypeAttrLocation {
271f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is in the decl-specifier-seq.
272f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclSpec,
273f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is part of a DeclaratorChunk.
274f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclChunk,
275f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is immediately after the declaration's name.
276f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclName
277f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith};
278f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
279711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state,
280f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             QualType &type, TypeAttrLocation TAL,
281711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             AttributeList *attrs);
282711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
283711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
284711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
285711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type);
286711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
287aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballmanstatic bool handleMSPointerTypeQualifierAttr(TypeProcessingState &state,
288aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             AttributeList &attr,
289aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             QualType &type);
290aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
291711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
292711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr, QualType &type);
293711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
294b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
295f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr, QualType &type);
296f85e193739c953358c865005855253af4f68a497John McCall
297711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCPointerTypeAttr(TypeProcessingState &state,
298711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr, QualType &type) {
2998e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_ObjCGC)
300f85e193739c953358c865005855253af4f68a497John McCall    return handleObjCGCTypeAttr(state, attr, type);
3018e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  assert(attr.getKind() == AttributeList::AT_ObjCOwnership);
302b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  return handleObjCOwnershipTypeAttr(state, attr, type);
303711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
304711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
305b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// Given the index of a declarator chunk, check whether that chunk
306b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// directly specifies the return type of a function and, if so, find
307b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// an appropriate place for it.
308b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall///
309b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// \param i - a notional index which the search will start
310b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall///   immediately inside
311b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCallstatic DeclaratorChunk *maybeMovePastReturnType(Declarator &declarator,
312b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall                                                unsigned i) {
313b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  assert(i <= declarator.getNumTypeObjects());
314b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
315b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  DeclaratorChunk *result = 0;
316b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
317b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  // First, look inwards past parens for a function declarator.
318b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  for (; i != 0; --i) {
319b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    DeclaratorChunk &fnChunk = declarator.getTypeObject(i-1);
320b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    switch (fnChunk.Kind) {
321b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Paren:
322b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      continue;
323b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
324b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // If we find anything except a function, bail out.
325b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Pointer:
326b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::BlockPointer:
327b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Array:
328b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Reference:
329b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::MemberPointer:
330b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      return result;
331b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
332b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // If we do find a function declarator, scan inwards from that,
333b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // looking for a block-pointer declarator.
334b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Function:
335b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      for (--i; i != 0; --i) {
336b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        DeclaratorChunk &blockChunk = declarator.getTypeObject(i-1);
337b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        switch (blockChunk.Kind) {
338b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Paren:
339b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Pointer:
340b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Array:
341b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Function:
342b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Reference:
343b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::MemberPointer:
344b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          continue;
345b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::BlockPointer:
346b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          result = &blockChunk;
347b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          goto continue_outer;
348b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        }
349b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        llvm_unreachable("bad declarator chunk kind");
350b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      }
351b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
352b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      // If we run out of declarators doing that, we're done.
353b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      return result;
354b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
355b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    llvm_unreachable("bad declarator chunk kind");
356b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
357b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // Okay, reconsider from our new point.
358b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  continue_outer: ;
359b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  }
360b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
361b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  // Ran out of chunks, bail out.
362b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  return result;
363b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall}
364b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
365711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Given that an objc_gc attribute was written somewhere on a
366711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declaration *other* than on the declarator itself (for which, use
367711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
368711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// didn't apply in whatever position it was written in, try to move
369711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// it to a more appropriate position.
370711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeObjCPointerTypeAttr(TypeProcessingState &state,
371711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          AttributeList &attr,
372711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType type) {
373711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
374b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
375b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  // Move it to the outermost normal or block pointer declarator.
376711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
377711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
378711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
379711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
380b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::BlockPointer: {
381b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      // But don't move an ARC ownership attribute to the return type
382b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      // of a block.
383b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      DeclaratorChunk *destChunk = 0;
384b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (state.isProcessingDeclSpec() &&
385b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          attr.getKind() == AttributeList::AT_ObjCOwnership)
386b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        destChunk = maybeMovePastReturnType(declarator, i - 1);
387b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (!destChunk) destChunk = &chunk;
388b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
389711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
390b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall                             destChunk->getAttrListRef());
391711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
392b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
393711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
394711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
395711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
396711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
397711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
398b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // We may be starting at the return type of a block.
399b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Function:
400b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (state.isProcessingDeclSpec() &&
401b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          attr.getKind() == AttributeList::AT_ObjCOwnership) {
402b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        if (DeclaratorChunk *dest = maybeMovePastReturnType(declarator, i)) {
403b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
404b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall                                 dest->getAttrListRef());
405b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          return;
406b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        }
407b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      }
408b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      goto error;
409b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
410711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Don't walk through these.
411711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
412711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto error;
414711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
415711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
416711c52bb20d0c69063b52a99826fb7d2835501f1John McCall error:
4172792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
4182792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
419711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
420711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
421711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Distribute an objc_gc type attribute that was written on the
422711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator.
423711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
424711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
425711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            AttributeList &attr,
426711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            QualType &declSpecType) {
427711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
428711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
429711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // objc_gc goes on the innermost pointer to something that's not a
430711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // pointer.
431711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  unsigned innermost = -1U;
432711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  bool considerDeclSpec = true;
433711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
434711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
435711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
436711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
437711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
438711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      innermost = i;
439ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      continue;
440711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
441711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
442711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
443711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
445711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
446711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
448711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      considerDeclSpec = false;
449711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto done;
450711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
451711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
452711c52bb20d0c69063b52a99826fb7d2835501f1John McCall done:
453711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
454711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // That might actually be the decl spec if we weren't blocked by
455711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // anything in the declarator.
456711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (considerDeclSpec) {
4577ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    if (handleObjCPointerTypeAttr(state, attr, declSpecType)) {
4587ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // Splice the attribute into the decl spec.  Prevents the
4597ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // attribute from being applied multiple times and gives
4607ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // the source-location-filler something to work with.
4617ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      state.saveDeclSpecAttrs();
4627ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      moveAttrFromListToList(attr, declarator.getAttrListRef(),
4637ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall               declarator.getMutableDeclSpec().getAttributes().getListRef());
464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
4657ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    }
466711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
467711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
468711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, if we found an appropriate chunk, splice the attribute
469711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // into it.
470711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (innermost != -1U) {
471711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, declarator.getAttrListRef(),
472711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                       declarator.getTypeObject(innermost).getAttrListRef());
473711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
474711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
475711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
476711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, diagnose when we're done building the type.
477711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
478711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
479711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
480711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written somewhere in a declaration
482711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// *other* than on the declarator itself or in the decl spec.  Given
483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// that it didn't apply in whatever position it was written in, try
484711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// to move it to a more appropriate position.
485711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeFunctionTypeAttr(TypeProcessingState &state,
486711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
487711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType type) {
488711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
489711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
490711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to push the attribute from the return type of a function to
491711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the function itself.
492711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
493711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
494711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
497711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
498711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
499711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
501711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
505711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
506711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
507711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
508711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
50991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5102792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
511711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
512711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
513711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Try to distribute a function type attribute to the innermost
514711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// function chunk or type.  Returns true if the attribute was
515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributed, false if no location was found.
516711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool
517711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr,
519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList *&attrList,
520711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      QualType &declSpecType) {
521711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
523711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Put it on the innermost function chunk, if there is one.
524711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
525711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
526711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (chunk.Kind != DeclaratorChunk::Function) continue;
527711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
528711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
529711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
531711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5325b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  return handleFunctionTypeAttr(state, attr, declSpecType);
533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
534711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
535711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written in the decl spec.  Try to
536711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
537711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
538711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
539711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
540711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType &declSpecType) {
541711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.saveDeclSpecAttrs();
542711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5435c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // C++11 attributes before the decl specifiers actually appertain to
5445c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // the declarators. Move them straight there. We don't support the
5455c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // 'put them wherever you like' semantics we allow for GNU attributes.
5465c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  if (attr.isCXX11Attribute()) {
5475c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
5485c52166525f8714c3e3a979b156ec23426947fd3Richard Smith                           state.getDeclarator().getAttrListRef());
5495c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    return;
5505c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  }
5515c52166525f8714c3e3a979b156ec23426947fd3Richard Smith
552711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
553711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
554711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            state.getCurrentAttrListRef(),
555711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
556711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
557711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
558711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
559711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
560711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
561711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
562711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
563711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written on the declarator.  Try to
564711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
565711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
566711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
567711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         AttributeList &attr,
568711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         QualType &declSpecType) {
569711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
570711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
571711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
572711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
573711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declarator.getAttrListRef(),
574711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
575711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
576711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
577711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
578711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
579711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
580711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
581711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
582711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
583711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// \brief Given that there are attributes written on the declarator
584711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// itself, try to distribute any type attributes to the appropriate
585711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator chunk.
586711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///
587711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// These are attributes like the following:
588711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f ATTR;
589711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int (f ATTR)();
590711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// but not necessarily this:
591711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f() ATTR;
592711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
593711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                              QualType &declSpecType) {
594711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Collect all the type attributes from the declarator itself.
595711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
596711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *attr = state.getDeclarator().getAttributes();
597711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
598711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
599711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr->getNext();
600711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
6015c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    // Do not distribute C++11 attributes. They have strict rules for what
6025c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    // they appertain to.
6035c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    if (attr->isCXX11Attribute())
6045c52166525f8714c3e3a979b156ec23426947fd3Richard Smith      continue;
6055c52166525f8714c3e3a979b156ec23426947fd3Richard Smith
606711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr->getKind()) {
607711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
608711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
609711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
610711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
6118e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
6124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
613f85e193739c953358c865005855253af4f68a497John McCall        break;
614f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough
615f85e193739c953358c865005855253af4f68a497John McCall
616711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
617711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
618711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
619711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
620aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    MS_TYPE_ATTRS_CASELIST:
621aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      // Microsoft type attributes cannot go after the declarator-id.
622aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      continue;
623aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
624711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    default:
625711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
626711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
627711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attr = next));
628711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
629711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
630711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Add a synthetic '()' to a block-literal declarator if it is
631711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// required, given the return type.
632711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void maybeSynthesizeBlockSignature(TypeProcessingState &state,
633711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType declSpecType) {
634711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
635711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
636711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // First, check whether the declarator would produce a function,
637711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // i.e. whether the innermost semantic chunk is a function.
638711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.isFunctionDeclarator()) {
639711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // If so, make that declarator a prototyped declarator.
640711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.getFunctionTypeInfo().hasPrototype = true;
641711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
642711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
643711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
644da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // If there are any type objects, the type as written won't name a
645da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // function, regardless of the decl spec type.  This is because a
646da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // block signature declarator is always an abstract-declarator, and
647da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // abstract-declarators can't just be parentheses chunks.  Therefore
648da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // we need to build a function chunk unless there are no type
649da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // objects and the decl spec type is a function.
650711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
651711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
652711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
653da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // Note that there *are* cases with invalid declarators where
654da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // declarators consist solely of parentheses.  In general, these
655da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // occur only in failed efforts to make function declarators, so
656da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // faking up the function chunk is still the right thing to do.
657711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
658711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, we need to fake up a function declarator.
65996a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar  SourceLocation loc = declarator.getLocStart();
660711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
661711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // ...and *prepend* it to the declarator.
66259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara  SourceLocation NoLoc;
663711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
66459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*HasProto=*/true,
66559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*IsAmbiguous=*/false,
66659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*LParenLoc=*/NoLoc,
66759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ArgInfo=*/0,
66859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NumArgs=*/0,
66959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*EllipsisLoc=*/NoLoc,
67059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RParenLoc=*/NoLoc,
67159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*TypeQuals=*/0,
67259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RefQualifierIsLvalueRef=*/true,
67359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RefQualifierLoc=*/NoLoc,
67459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ConstQualifierLoc=*/NoLoc,
67559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*VolatileQualifierLoc=*/NoLoc,
67659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*MutableLoc=*/NoLoc,
67759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             EST_None,
67859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ESpecLoc=*/NoLoc,
67959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*Exceptions=*/0,
68059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ExceptionRanges=*/0,
68159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NumExceptions=*/0,
68259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NoexceptExpr=*/0,
68359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             loc, loc, declarator));
684711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
685711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // For consistency, make sure the state still has us as processing
686711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the decl spec.
687711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
688711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.setCurrentChunkIndex(declarator.getNumTypeObjects());
68904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
69004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
691930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
692930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
6931dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// \param state Specifies the declarator containing the declaration specifier
6941dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// to be converted, along with other associated processing state.
6955153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
6965153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
6978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType ConvertDeclSpecToType(TypeProcessingState &state) {
6985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
6995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
700711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
7018cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
702711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
703711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const DeclSpec &DS = declarator.getDeclSpec();
704711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation DeclLoc = declarator.getIdentifierLoc();
7055db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
70696a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar    DeclLoc = DS.getLocStart();
70791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
708711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  ASTContext &Context = S.Context;
7091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7105db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
7115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
71296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
71396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
71496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
7155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
7165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
717fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
7185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
719fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
7205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
7215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
7225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
723fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
7245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
725958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
72664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
72764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
72864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
72964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
730711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
731f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
73264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
73364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
73464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
73564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
736711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
737f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
73864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
73964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
74064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
741f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
742f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
743f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
744f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
745f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
746f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
747f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
748f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
749f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
750f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
751d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
75262f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
753097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
754c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
75531ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                         (ObjCProtocolDecl*const*)PQ,
756c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         DS.getNumProtocolQualifiers());
757c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectPointerType(Result);
75862f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
75962f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
76091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
7615db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
7625db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
763f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // The declspec is always missing in a lambda expr context; it is either
764f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // specified with a trailing return type or inferred.
765f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    if (declarator.getContext() == Declarator::LambdaExprContext ||
766f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman        isOmittedBlockReturnType(declarator)) {
767152b4e4652baedfceba1cd8115515629225e713fManuel Klimek      Result = Context.DependentTy;
7685db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
7695db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
7701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
771d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
772d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
773d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
774d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
775d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
776d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
777d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
7784e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().ImplicitInt) {
77935d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
78035d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
7813f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
782711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_declspec)
7833f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
78496a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        << FixItHint::CreateInsertion(DS.getLocStart(), "int");
7853f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
7864310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
787d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
788d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
789d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
790d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
79158eb37036b47bbe7433f72d92a2cb60848507707Richard Smith      if (S.getLangOpts().CPlusPlus) {
792711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_missing_type_specifier)
7933f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
7941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
795b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
796b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
797b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
798711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
799b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
800711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_type_specifier)
8013f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
802b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
803d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
8041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
8063cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
8075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
8085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
809fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
810fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
811fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
812311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
813311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
81491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
815e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
816e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
817e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
818e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
81980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
820e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
821e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
822e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
823e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
824311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
8255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
8265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
8275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
828fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
829fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
830fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
831311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
832311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
83391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
834e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
835e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
836e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
837e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
83880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
839e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
840e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
841e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
842e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
843311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
8445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
8455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
846958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
8473cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
8485a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith  case DeclSpec::TST_int128:
84984268904947ada7e251932a6f5b0f4364df7a2c7Richard Smith    if (!S.PP.getTargetInfo().hasInt128Type())
85084268904947ada7e251932a6f5b0f4364df7a2c7Richard Smith      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_int128_unsupported);
8515a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    if (DS.getTypeSpecSign() == DeclSpec::TSS_unsigned)
8525a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.UnsignedInt128Ty;
8535a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    else
8545a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.Int128Ty;
8555a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    break;
856aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case DeclSpec::TST_half: Result = Context.HalfTy; break;
857fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
858958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
859958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
860fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
861958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
862fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
86339d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne
8644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
86539d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
86639d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      declarator.setInvalidType(true);
86739d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    }
868958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
869fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
8705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
8715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
8725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
873711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
8748f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
875711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
8768f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
87799dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
8785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
8806666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_struct:
8816666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_interface: {
882b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
8836e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
8846e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
8856e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
886711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
8876e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
8886e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
8896e24726524c2b51b31bb4b622aa678a46b024f42John McCall
890a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
8910daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc());
89291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
8935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
894a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
89591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
8965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
897a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
8982191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
8990daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    // In both C and C++, make an ElaboratedType.
9000daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    ElaboratedTypeKeyword Keyword
9010daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
9020daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
903958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
9041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
9051a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
9065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
9075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
9085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
909711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
91027940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    if (Result.isNull())
911711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
91227940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    else if (DeclSpec::ProtocolQualifierListTy PQ
91327940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall               = DS.getProtocolQualifiers()) {
914c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
915c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // Silently drop any existing protocol qualifiers.
916c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // TODO: determine whether that's the right thing to do.
917c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjT->getNumProtocols())
918c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = ObjT->getBaseType();
919c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
920c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (DS.getNumProtocolQualifiers())
921c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = Context.getObjCObjectType(Result,
92231ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                             (ObjCProtocolDecl*const*) PQ,
923c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             DS.getNumProtocolQualifiers());
924c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCIdType()) {
925ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
926c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
92731ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                           (ObjCProtocolDecl*const*) PQ,
928c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
929c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
930c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCClassType()) {
9314262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
932c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
93331ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                           (ObjCProtocolDecl*const*) PQ,
934c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
935c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
9363f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
937711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
9383f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
939711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
9403f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
941c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
9421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
944958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
9455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
946958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
947e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
948711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
949958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
950730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian    if (!Result->isDependentType())
951730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian      if (const TagType *TT = Result->getAs<TagType>())
952711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
953d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
954fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
955958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
956d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
957b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
958d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
959d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
960711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
9614b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
9624b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
963711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
9644b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
965958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
966d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
9676fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
968b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
9696fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
9706fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
971711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
972af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
973af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
974711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
975af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
9766fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
9776fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
978ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case DeclSpec::TST_underlyingType:
979db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    Result = S.GetTypeFromParser(DS.getRepAsType());
980db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    assert(!Result.isNull() && "Didn't get a type for __underlying_type?");
981ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    Result = S.BuildUnaryTransformType(Result,
982ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::EnumUnderlyingType,
983ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       DS.getTypeSpecTypeLoc());
984ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (Result.isNull()) {
985ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Result = Context.IntTy;
986ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      declarator.setInvalidType(true);
987db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    }
98891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
989db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
990a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  case DeclSpec::TST_auto:
991e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
992152b4e4652baedfceba1cd8115515629225e713fManuel Klimek    Result = Context.getAutoType(QualType(), /*decltype(auto)*/false);
993a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    break;
994a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
995a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  case DeclSpec::TST_decltype_auto:
996152b4e4652baedfceba1cd8115515629225e713fManuel Klimek    Result = Context.getAutoType(QualType(), /*decltype(auto)*/true);
997e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
9981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
999a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall  case DeclSpec::TST_unknown_anytype:
1000a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    Result = Context.UnknownAnyTy;
1001a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    break;
1002a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall
1003b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case DeclSpec::TST_atomic:
1004b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.GetTypeFromParser(DS.getRepAsType());
1005b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(!Result.isNull() && "Didn't get a type for _Atomic?");
1006b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc());
1007b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Result.isNull()) {
1008b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Result = Context.IntTy;
1009b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      declarator.setInvalidType(true);
1010b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
101191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
1012b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1013b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_t:
1014b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dTy;
1015b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1016b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1017b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_array_t:
1018b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dArrayTy;
1019b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1020b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1021b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_buffer_t:
1022b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dBufferTy;
1023b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1024b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1025b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_t:
1026b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dTy;
1027b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1028b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1029b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_array_t:
1030b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dArrayTy;
1031b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1032b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1033b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image3d_t:
1034b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage3dTy;
1035b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1036b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
103721f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei  case DeclSpec::TST_sampler_t:
103821f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    Result = Context.OCLSamplerTy;
103921f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    break;
104021f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei
1041e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei  case DeclSpec::TST_event_t:
1042e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    Result = Context.OCLEventTy;
1043e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    break;
1044e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei
1045809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
10465153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
1047711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
10485153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
10495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1051958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
1052f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
10534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().Freestanding)
1054711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
1055fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
105682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
105782287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
105882287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
1059e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
1060788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
1061e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
1062788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
1063e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
1064e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
1065f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
10661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
106747423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
106847423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
1069711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
10701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1071711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
1072711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
1073711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
1074711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
1075711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1076711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
1077711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
1078711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
1079711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (AttributeList *attrs = DS.getAttributes().getList())
1080f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    processTypeAttrs(state, Result, TAL_DeclSpec, attrs);
10811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
108296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
108396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
108496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
108596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
108696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
108796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
108896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
10890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
10904cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        S.Diag(DS.getConstSpecLoc(), diag::warn_typecheck_function_qualifiers)
10914cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith          << Result << DS.getSourceRange();
10920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
10934cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        S.Diag(DS.getVolatileSpecLoc(), diag::warn_typecheck_function_qualifiers)
10944cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith          << Result << DS.getSourceRange();
10950953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
10964cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        assert((TypeQuals & (DeclSpec::TQ_restrict | DeclSpec::TQ_atomic)) &&
10974cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith               "Has CVRA quals but not C, V, R, or A?");
10984cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // No diagnostic; we'll diagnose 'restrict' or '_Atomic' applied to a
10994cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // function type later, in BuildQualifiedType.
110096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
110196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
11021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1103f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
1104f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
1105f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
1106f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
1107f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
11081a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
11091a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
1110f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
11110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
11120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
11134cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      TypeQuals &= ~DeclSpec::TQ_atomic;
11141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
11151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1116bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // C90 6.5.3 constraints: "The same type qualifier shall not appear more
1117bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // than once in the same specifier-list or qualifier-list, either directly
1118bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // or via one or more typedefs."
111991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus
1120bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman        && TypeQuals & Result.getCVRQualifiers()) {
1121bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()) {
112291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec)
1123bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "const";
1124bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1125bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
1126bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()) {
112791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec)
1128bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "volatile";
1129bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1130bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
11314cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // C90 doesn't have restrict nor _Atomic, so it doesn't force us to
11324cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // produce a warning in this case.
1133bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    }
1134bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
11354cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    QualType Qualified = S.BuildQualifiedType(Result, DeclLoc, TypeQuals, &DS);
11364cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
11374cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // If adding qualifiers fails, just use the unqualified type.
11384cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (Qualified.isNull())
11394cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      declarator.setInvalidType(true);
11404cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    else
11414cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      Result = Qualified;
114296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
11430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1144f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
1145f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
1146f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
1147cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
1148cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
1149cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
11501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1151cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
1152cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1153cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
11542865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
115593d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith                                  Qualifiers Qs, const DeclSpec *DS) {
11562865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
11572865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
11582865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
11592865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
11602865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
11612865474261a608c7873b87ba4af110d17907896dJohn McCall
116293d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith    if (T->isAnyPointerType() || T->isReferenceType() ||
116393d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        T->isMemberPointerType()) {
116493d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      QualType EltTy;
116593d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      if (T->isObjCObjectPointerType())
116693d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = T;
116793d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>())
116893d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = PTy->getPointeeType();
116993d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      else
117093d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = T->getPointeeType();
117193d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith
117293d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      // If we have a pointer or reference, the pointee must have an object
117393d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      // incomplete type.
117493d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      if (!EltTy->isIncompleteOrObjectType()) {
11752865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
117693d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        ProblemTy = EltTy;
117791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
117893d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith    } else if (!T->isDependentType()) {
117993d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      DiagID = diag::err_typecheck_invalid_restrict_not_pointer;
11802865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
11812865474261a608c7873b87ba4af110d17907896dJohn McCall    }
11822865474261a608c7873b87ba4af110d17907896dJohn McCall
11832865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
118493d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << ProblemTy;
11852865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
11862865474261a608c7873b87ba4af110d17907896dJohn McCall    }
11872865474261a608c7873b87ba4af110d17907896dJohn McCall  }
11882865474261a608c7873b87ba4af110d17907896dJohn McCall
11892865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
11902865474261a608c7873b87ba4af110d17907896dJohn McCall}
11912865474261a608c7873b87ba4af110d17907896dJohn McCall
11924cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard SmithQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
11934cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith                                  unsigned CVRA, const DeclSpec *DS) {
11944cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // Convert from DeclSpec::TQ to Qualifiers::TQ by just dropping TQ_atomic.
11954cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  unsigned CVR = CVRA & ~DeclSpec::TQ_atomic;
11964cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
11974cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // C11 6.7.3/5:
11984cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   If the same qualifier appears more than once in the same
11994cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   specifier-qualifier-list, either directly or via one or more typedefs,
12004cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   the behavior is the same as if it appeared only once.
12014cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //
12024cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // It's not specified what happens when the _Atomic qualifier is applied to
12034cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // a type specified with the _Atomic specifier, but we assume that this
12044cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // should be treated as if the _Atomic qualifier appeared multiple times.
12054cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  if (CVRA & DeclSpec::TQ_atomic && !T->isAtomicType()) {
12064cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // C11 6.7.3/5:
12074cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   If other qualifiers appear along with the _Atomic qualifier in a
12084cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   specifier-qualifier-list, the resulting type is the so-qualified
12094cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   atomic type.
12104cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //
12114cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // Don't need to worry about array types here, since _Atomic can't be
12124cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // applied to such types.
12134cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    SplitQualType Split = T.getSplitUnqualifiedType();
12144cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    T = BuildAtomicType(QualType(Split.Ty, 0),
12154cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith                        DS ? DS->getAtomicSpecLoc() : Loc);
12164cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (T.isNull())
12174cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      return T;
12184cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    Split.Quals.addCVRQualifiers(CVR);
12194cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    return BuildQualifiedType(T, Loc, Split.Quals);
12204cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
12214cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
12224cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  return BuildQualifiedType(T, Loc, Qualifiers::fromCVRMask(CVR), DS);
12234cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith}
12244cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
1225075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
1226075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
1227075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
1228075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
1229075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
1230f85e193739c953358c865005855253af4f68a497John McCall/// Given that we're building a pointer or reference to the given
1231f85e193739c953358c865005855253af4f68a497John McCallstatic QualType inferARCLifetimeForPointee(Sema &S, QualType type,
1232f85e193739c953358c865005855253af4f68a497John McCall                                           SourceLocation loc,
1233f85e193739c953358c865005855253af4f68a497John McCall                                           bool isReference) {
1234f85e193739c953358c865005855253af4f68a497John McCall  // Bail out if retention is unrequired or already specified.
1235f85e193739c953358c865005855253af4f68a497John McCall  if (!type->isObjCLifetimeType() ||
1236f85e193739c953358c865005855253af4f68a497John McCall      type.getObjCLifetime() != Qualifiers::OCL_None)
1237f85e193739c953358c865005855253af4f68a497John McCall    return type;
1238f85e193739c953358c865005855253af4f68a497John McCall
1239f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None;
1240f85e193739c953358c865005855253af4f68a497John McCall
1241f85e193739c953358c865005855253af4f68a497John McCall  // If the object type is const-qualified, we can safely use
1242f85e193739c953358c865005855253af4f68a497John McCall  // __unsafe_unretained.  This is safe (because there are no read
1243f85e193739c953358c865005855253af4f68a497John McCall  // barriers), and it'll be safe to coerce anything but __weak* to
1244f85e193739c953358c865005855253af4f68a497John McCall  // the resulting type.
1245f85e193739c953358c865005855253af4f68a497John McCall  if (type.isConstQualified()) {
1246f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
1247f85e193739c953358c865005855253af4f68a497John McCall
1248f85e193739c953358c865005855253af4f68a497John McCall  // Otherwise, check whether the static type does not require
1249f85e193739c953358c865005855253af4f68a497John McCall  // retaining.  This currently only triggers for Class (possibly
1250f85e193739c953358c865005855253af4f68a497John McCall  // protocol-qualifed, and arrays thereof).
1251f85e193739c953358c865005855253af4f68a497John McCall  } else if (type->isObjCARCImplicitlyUnretainedType()) {
1252f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
12535b76f373d23cc3b292ecf523349aaaa388eea375Argyrios Kyrtzidis
1254ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // If we are in an unevaluated context, like sizeof, skip adding a
1255ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // qualification.
125671f55f771794674a410171dbf3cb5dbedf95d033David Blaikie  } else if (S.isUnevaluatedContext()) {
1257ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    return type;
1258f85e193739c953358c865005855253af4f68a497John McCall
1259e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // If that failed, give an error and recover using __strong.  __strong
1260e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // is the option most likely to prevent spurious second-order diagnostics,
1261e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // like when binding a reference to a field.
1262f85e193739c953358c865005855253af4f68a497John McCall  } else {
1263f85e193739c953358c865005855253af4f68a497John McCall    // These types can show up in private ivars in system headers, so
1264f85e193739c953358c865005855253af4f68a497John McCall    // we need this to not be an error in those cases.  Instead we
1265f85e193739c953358c865005855253af4f68a497John McCall    // want to delay.
1266f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1267ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.DelayedDiagnostics.add(
1268ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman          sema::DelayedDiagnostic::makeForbiddenType(loc,
1269ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman              diag::err_arc_indirect_no_ownership, type, isReference));
1270f85e193739c953358c865005855253af4f68a497John McCall    } else {
1271ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference;
1272f85e193739c953358c865005855253af4f68a497John McCall    }
1273e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall    implicitLifetime = Qualifiers::OCL_Strong;
1274f85e193739c953358c865005855253af4f68a497John McCall  }
1275f85e193739c953358c865005855253af4f68a497John McCall  assert(implicitLifetime && "didn't infer any lifetime!");
1276f85e193739c953358c865005855253af4f68a497John McCall
1277f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers qs;
1278f85e193739c953358c865005855253af4f68a497John McCall  qs.addObjCLifetime(implicitLifetime);
1279f85e193739c953358c865005855253af4f68a497John McCall  return S.Context.getQualifiedType(type, qs);
1280f85e193739c953358c865005855253af4f68a497John McCall}
1281f85e193739c953358c865005855253af4f68a497John McCall
1282cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
1283cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1284cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
1285cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1286cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1287cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
1288cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
1289cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1290cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
1291cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1292cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1293cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
1294cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
12952865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
1296cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
1297cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
1298cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
1299cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
1300ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1301cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1302cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1303cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1304c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
130592e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
1306f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build pointers to unqualified pointers.
13074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1308f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false);
1309f85e193739c953358c865005855253af4f68a497John McCall
1310cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
13112865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
1312cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1313cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1314cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
1315cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1316cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
1317cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1318cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1319cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
1320cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
1321cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1322cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
1323cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1324cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1325cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
1326cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
132754e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
13282865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
132954e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
133091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  assert(Context.getCanonicalType(T) != Context.OverloadTy &&
13319625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
133291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
133369d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // C++0x [dcl.ref]p6:
133491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   If a typedef (7.1.3), a type template-parameter (14.3.1), or a
133591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
133691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   type T, an attempt to create the type "lvalue reference to cv TR" creates
133791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   the type "lvalue reference to T", while an attempt to create the type
133869d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   "rvalue reference to cv TR" creates the type TR.
133954e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
134054e14c4db764c0636160d26c5bbf491637c83a76John McCall
134154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
134254e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
134354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
134454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
134554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
134654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
134754e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
134854e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
134954e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
135054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
135154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
135269d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // collapsing of references-to-references as described in C++0x.
135369d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // DR 106 and 540 introduce reference-collapsing into C++98/03.
1354cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1355cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
135633a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
1357cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
1358cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
1359cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
1360cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1361cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1362cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1363f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build references to unqualified pointers.
13644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1365f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true);
1366f85e193739c953358c865005855253af4f68a497John McCall
1367cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
13687c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
13692865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
13702865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
1371cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1372cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1373e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// Check whether the specified array size makes the array type a VLA.  If so,
1374e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// return true, if not, return the size of the array in SizeVal.
1375282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smithstatic bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal) {
1376282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode
1377282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // (like gnu99, but not c99) accept any evaluatable value as an extension.
1378ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  class VLADiagnoser : public Sema::VerifyICEDiagnoser {
1379ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  public:
1380ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    VLADiagnoser() : Sema::VerifyICEDiagnoser(true) {}
138191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1382ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) {
1383ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
138491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1385ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseFold(Sema &S, SourceLocation Loc, SourceRange SR) {
1386ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      S.Diag(Loc, diag::ext_vla_folded_to_constant) << SR;
1387ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
1388ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  } Diagnoser;
138991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1390ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  return S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser,
1391ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                           S.LangOpts.GNUMode).isInvalid();
1392e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner}
1393e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1394e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1395cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
1396cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1397cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
1398cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1399cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
14001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
14011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
1402cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1403efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Brackets The range from the opening '[' to the closing ']'.
1404cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1405cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
1406cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1407cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1408cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
1409cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
1410cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1411cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
14127e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
14130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
14147e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
14154e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
1416138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
1417138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
141891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   type, the (possibly cv-qualified) type void, a function type or an
1419138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
1420138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
1421bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    // C++ [dcl.array]p3:
1422bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   When several "array of" specifications are adjacent, [...] only the
1423bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   first of the constant expressions that specify the bounds of the arrays
1424bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   may be omitted.
1425bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //
1426138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
1427138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
1428138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
1429138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
1430138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1431138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
143291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1433bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    if (T->isVoidType() || T->isIncompleteArrayType()) {
1434923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1435923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1436923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
143791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
143891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (RequireNonAbstractType(Brackets.getBegin(), T,
1439138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
1440138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
144191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1442923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
1443138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1444138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
1445923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
1446923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
1447923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1448923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
1449cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1450cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
1451cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
1452ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1453cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1454cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
14551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14566217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
1457cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
1458cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
1459cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
1460cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
1461c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
1462c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1463c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
1464cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
14651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1466806054db6653d29cb0d9692df3612cbcd03d0530John McCall  // Do placeholder conversions on the array size expression.
1467806054db6653d29cb0d9692df3612cbcd03d0530John McCall  if (ArraySize && ArraySize->hasPlaceholderType()) {
1468806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ExprResult Result = CheckPlaceholderExpr(ArraySize);
1469806054db6653d29cb0d9692df3612cbcd03d0530John McCall    if (Result.isInvalid()) return QualType();
1470806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ArraySize = Result.take();
1471806054db6653d29cb0d9692df3612cbcd03d0530John McCall  }
1472806054db6653d29cb0d9692df3612cbcd03d0530John McCall
14735e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
1474429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ArraySize && !ArraySize->isRValue()) {
1475429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult Result = DefaultLvalueConversion(ArraySize);
1476429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Result.isInvalid())
1477429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return QualType();
1478429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
1479429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ArraySize = Result.take();
1480429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
14815e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1482cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
1483282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // C++11 allows contextual conversions to such types.
148480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (!getLangOpts().CPlusPlus11 &&
1485282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      ArraySize && !ArraySize->isTypeDependent() &&
14861274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1487cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1488cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
1489cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1490cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1491282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
14922767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
1493cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
1494f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
14957e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
1496f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
1497f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
1498ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
14997e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
1500282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  } else if ((!T->isDependentType() && !T->isIncompleteType() &&
1501282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith              !T->isConstantSizeType()) ||
1502282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith             isArraySizeVLA(*this, ArraySize, ConstVal)) {
1503282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // Even in C++11, don't allow contextual conversions in the array bound
1504282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // of a VLA.
150580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (getLangOpts().CPlusPlus11 &&
1506282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1507282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1508282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        << ArraySize->getType() << ArraySize->getSourceRange();
1509282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      return QualType();
1510282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    }
1511282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
1512e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with an element type that has a non-constant-size is a VLA.
1513e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with a non-ICE size is a VLA.  We accept any expression
1514e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // that we can fold to a non-zero positive value as an extension.
15157e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1516cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
1517cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1518cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
1519923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
1520b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
1521b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1522b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1523b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
1524b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1525b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
1526923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1527923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1528923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
152902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
153002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
153191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      Diag(ArraySize->getLocStart(),
153202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
153302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
1534923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
153520cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne
153620cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      if (ASM == ArrayType::Static) {
153720cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        Diag(ArraySize->getLocStart(),
153820cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne             diag::warn_typecheck_zero_static_array_size)
153920cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne          << ArraySize->getSourceRange();
154020cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        ASM = ArrayType::Normal;
154120cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      }
154291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
1543152b4e4652baedfceba1cd8115515629225e713fManuel Klimek               !T->isIncompleteType()) {
154491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // Is the array too large?
15452767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
15462767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
1547ee2f8f2f7c4eacfa305a29fcd916d63c650ca847Richard Trieu      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) {
15482767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
15492767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
15502767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
1551ee2f8f2f7c4eacfa305a29fcd916d63c650ca847Richard Trieu        return QualType();
1552ee2f8f2f7c4eacfa305a29fcd916d63c650ca847Richard Trieu      }
15531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
155491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
155546a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
1556cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1557617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly
1558617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  // OpenCL v1.2 s6.9.d: variable length arrays are not supported.
1559617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  if (getLangOpts().OpenCL && T->isVariableArrayType()) {
1560617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    Diag(Loc, diag::err_opencl_vla);
1561617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    return QualType();
1562617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  }
1563af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
15644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().C99) {
15650fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
15660fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
156739b0e269dc8895ecc0f92f08126d3082b2a837a8Richard Smith      // FIXME: C++1y allows this.
1568f85e193739c953358c865005855253af4f68a497John McCall      QualType BaseT = Context.getBaseElementType(T);
156991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      if (!T->isDependentType() &&
15703c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT.isPODType(Context) &&
15713c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT->isObjCLifetimeType()) {
15720fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
1573f85e193739c953358c865005855253af4f68a497John McCall          << BaseT;
15740fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
157591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
1576a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
1577a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
1578a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
1579a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
1580a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
15810fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
15820fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
158339b0e269dc8895ecc0f92f08126d3082b2a837a8Richard Smith        Diag(Loc, getLangOpts().CPlusPlus1y
158439b0e269dc8895ecc0f92f08126d3082b2a837a8Richard Smith                      ? diag::warn_cxx11_compat_array_of_runtime_bound
158539b0e269dc8895ecc0f92f08126d3082b2a837a8Richard Smith                      : diag::ext_vla);
15860fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
1587d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith      Diag(Loc,
15884e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie           getLangOpts().CPlusPlus? diag::err_c99_array_usage_cxx
1589d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith                                     : diag::ext_c99_array_usage) << ASM;
1590cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1591cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1592630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko  if (T->isVariableArrayType()) {
1593630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko    // Warn about VLAs for -Wvla.
1594630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko    Diag(Loc, diag::warn_vla_used);
1595630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko  }
1596630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko
1597cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
1598cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
15999cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
16009cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
16019cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
16029cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
16039ae2f076ca5ab1feb3ba95629099ec2319833701John McCallQualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
16049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
16059cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
16069cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
16071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
16089cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
16099cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
16109cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
16119cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
16129cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
16139ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
16149cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
16159ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
16169f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      Diag(AttrLoc, diag::err_attribute_argument_type)
16179f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman        << "ext_vector_type" << AANT_ArgumentIntegerConstant
16189f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman        << ArraySize->getSourceRange();
16199cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
16209cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
16211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
16239cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
16241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
16251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16269cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
16279cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
16289ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      << ArraySize->getSourceRange();
16299cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
16309cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
16311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16321652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    if (VectorType::isVectorSizeTooLarge(vectorSize)) {
16331652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      Diag(AttrLoc, diag::err_attribute_size_too_large)
16341652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman        << ArraySize->getSourceRange();
16351652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      return QualType();
16361652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    }
16371652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman
16384ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    return Context.getExtVectorType(T, vectorSize);
16391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
16401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16419ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
16429cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
16431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1644ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedmanbool Sema::CheckFunctionReturnType(QualType T, SourceLocation Loc) {
1645724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
164691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    Diag(Loc, diag::err_func_returning_array_function)
164758408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
1648ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    return true;
1649724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1650aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1651aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // Functions cannot return half FP.
1652aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  if (T->isHalfType()) {
1653aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 <<
1654aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      FixItHint::CreateInsertion(Loc, "*");
1655ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    return true;
1656ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  }
1657ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
1658ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  // Methods cannot return interface types. All ObjC objects are
1659ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  // passed by reference.
1660ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  if (T->isObjCObjectType()) {
1661ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    Diag(Loc, diag::err_object_cannot_be_passed_returned_by_value) << 0 << T;
1662ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    return 0;
1663aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  }
1664aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1665ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  return false;
1666ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman}
1667ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
1668ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli FriedmanQualType Sema::BuildFunctionType(QualType T,
1669ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman                                 llvm::MutableArrayRef<QualType> ParamTypes,
1670ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
1671ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman                                 const FunctionProtoType::ExtProtoInfo &EPI) {
1672724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
1673ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
1674ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  Invalid |= CheckFunctionReturnType(T, Loc);
1675ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
1676bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  for (unsigned Idx = 0, Cnt = ParamTypes.size(); Idx < Cnt; ++Idx) {
1677aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    // FIXME: Loc is too inprecise here, should use proper locations for args.
1678c910d4cfa5042f2c9da1eb4e0b6ed59240c0eeeeReid Kleckner    QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]);
16792dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
1680724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
1681724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
1682aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    } else if (ParamType->isHalfType()) {
1683aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Disallow half FP arguments.
1684aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 <<
1685aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        FixItHint::CreateInsertion(Loc, "*");
1686aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Invalid = true;
1687724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
1688cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
168954e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
1690724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1691724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1692724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
1693724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1694724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1695bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  return Context.getFunctionType(T, ParamTypes, EPI);
1696724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
16971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1698949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
1699949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1700949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
1701949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
1702949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
1703949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
1704949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1705949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
1706949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
17071eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
17082865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
1709949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
1710949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
1711949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
1712949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
1713949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
1714949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1715949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
1716949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
1717949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1718949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
1719949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
1720949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
1721949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1722949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1723737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
1724949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
1725949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
17268d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
1727ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
1728949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1729949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1730949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1731949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
1732949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1733949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
1734949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1735949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1736949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1737949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
1738949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1739949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1740949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1741949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1742b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // C++ allows the class type in a member pointer to be an incomplete type.
1743b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // In the Microsoft ABI, the size of the member pointer can vary
1744b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // according to the class type, which means that we really need a
1745b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // complete type if possible, which means we need to instantiate templates.
1746b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  //
174784e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  // If template instantiation fails or the type is just incomplete, we have to
174884e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  // add an extra slot to the member pointer.  Yes, this does cause problems
174984e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  // when passing pointers between TUs that disagree about the size.
175084e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  if (Context.getTargetInfo().getCXXABI().isMicrosoft()) {
175184e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner    CXXRecordDecl *RD = Class->getAsCXXRecordDecl();
1752e93e2552e76ab704ec85919cc2c76f02b8b081eeReid Kleckner    if (RD && !RD->hasAttr<MSInheritanceAttr>()) {
175384e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // Lock in the inheritance model on the first use of a member pointer.
175484e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // Otherwise we may disagree about the size at different points in the TU.
175584e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // FIXME: MSVC picks a model on the first use that needs to know the size,
175684e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // rather than on the first mention of the type, e.g. typedefs.
1757e93e2552e76ab704ec85919cc2c76f02b8b081eeReid Kleckner      if (RequireCompleteType(Loc, Class, 0) && !RD->isBeingDefined()) {
175884e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        // We know it doesn't have an attribute and it's incomplete, so use the
175984e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        // unspecified inheritance model.  If we're in the record body, we can
176084e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        // figure out the inheritance model.
176184e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(),
176284e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner             E = RD->redecls_end(); I != E; ++I) {
176384e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner          I->addAttr(::new (Context) UnspecifiedInheritanceAttr(
176484e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner              RD->getSourceRange(), Context));
176584e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        }
176684e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      }
176784e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner    }
176884e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  }
1769679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos
1770ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  // FIXME: Adjust member function pointer calling conventions.
1771ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
17722865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
1773949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
17741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17759a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
17769a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
17779a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
17789a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
1779efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Loc The source location, used for diagnostics.
17809a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
17819a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
17829a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
17839a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
17849a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
17859a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
178691cbbbf506c892a26d4301e2b3ccd377b0938817Chad RosierQualType Sema::BuildBlockPointerType(QualType T,
17871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
17889a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
17890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
17909a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
17919a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
17929a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
17931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17942865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
17959a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
17969a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
1797b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1798b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
17993f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
1800a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
18013f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
18023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
18033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1804a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
1805f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
1806e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
1807a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
1808e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
18091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1810a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
1811e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
1812e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
1813e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
1814a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
1815a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
1816a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            unsigned chunkIndex);
1817a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis
1818f85e193739c953358c865005855253af4f68a497John McCall/// Given that this is the declaration of a parameter under ARC,
1819f85e193739c953358c865005855253af4f68a497John McCall/// attempt to infer attributes and such for pointer-to-whatever
1820f85e193739c953358c865005855253af4f68a497John McCall/// types.
1821f85e193739c953358c865005855253af4f68a497John McCallstatic void inferARCWriteback(TypeProcessingState &state,
1822f85e193739c953358c865005855253af4f68a497John McCall                              QualType &declSpecType) {
1823f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
1824f85e193739c953358c865005855253af4f68a497John McCall  Declarator &declarator = state.getDeclarator();
1825f85e193739c953358c865005855253af4f68a497John McCall
1826f85e193739c953358c865005855253af4f68a497John McCall  // TODO: should we care about decl qualifiers?
1827f85e193739c953358c865005855253af4f68a497John McCall
1828f85e193739c953358c865005855253af4f68a497John McCall  // Check whether the declarator has the expected form.  We walk
1829f85e193739c953358c865005855253af4f68a497John McCall  // from the inside out in order to make the block logic work.
1830f85e193739c953358c865005855253af4f68a497John McCall  unsigned outermostPointerIndex = 0;
1831f85e193739c953358c865005855253af4f68a497John McCall  bool isBlockPointer = false;
1832f85e193739c953358c865005855253af4f68a497John McCall  unsigned numPointers = 0;
1833f85e193739c953358c865005855253af4f68a497John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
1834f85e193739c953358c865005855253af4f68a497John McCall    unsigned chunkIndex = i;
1835f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex);
1836f85e193739c953358c865005855253af4f68a497John McCall    switch (chunk.Kind) {
1837f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Paren:
1838f85e193739c953358c865005855253af4f68a497John McCall      // Ignore parens.
1839f85e193739c953358c865005855253af4f68a497John McCall      break;
1840f85e193739c953358c865005855253af4f68a497John McCall
1841f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Reference:
1842f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Pointer:
1843f85e193739c953358c865005855253af4f68a497John McCall      // Count the number of pointers.  Treat references
1844f85e193739c953358c865005855253af4f68a497John McCall      // interchangeably as pointers; if they're mis-ordered, normal
1845f85e193739c953358c865005855253af4f68a497John McCall      // type building will discover that.
1846f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1847f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1848f85e193739c953358c865005855253af4f68a497John McCall      break;
1849f85e193739c953358c865005855253af4f68a497John McCall
1850f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::BlockPointer:
1851f85e193739c953358c865005855253af4f68a497John McCall      // If we have a pointer to block pointer, that's an acceptable
1852f85e193739c953358c865005855253af4f68a497John McCall      // indirect reference; anything else is not an application of
1853f85e193739c953358c865005855253af4f68a497John McCall      // the rules.
1854f85e193739c953358c865005855253af4f68a497John McCall      if (numPointers != 1) return;
1855f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1856f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1857f85e193739c953358c865005855253af4f68a497John McCall      isBlockPointer = true;
1858f85e193739c953358c865005855253af4f68a497John McCall
1859f85e193739c953358c865005855253af4f68a497John McCall      // We don't care about pointer structure in return values here.
1860f85e193739c953358c865005855253af4f68a497John McCall      goto done;
1861f85e193739c953358c865005855253af4f68a497John McCall
1862f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Array: // suppress if written (id[])?
1863f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Function:
1864f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::MemberPointer:
1865f85e193739c953358c865005855253af4f68a497John McCall      return;
1866f85e193739c953358c865005855253af4f68a497John McCall    }
1867f85e193739c953358c865005855253af4f68a497John McCall  }
1868f85e193739c953358c865005855253af4f68a497John McCall done:
1869f85e193739c953358c865005855253af4f68a497John McCall
1870f85e193739c953358c865005855253af4f68a497John McCall  // If we have *one* pointer, then we want to throw the qualifier on
1871f85e193739c953358c865005855253af4f68a497John McCall  // the declaration-specifiers, which means that it needs to be a
1872f85e193739c953358c865005855253af4f68a497John McCall  // retainable object type.
1873f85e193739c953358c865005855253af4f68a497John McCall  if (numPointers == 1) {
1874f85e193739c953358c865005855253af4f68a497John McCall    // If it's not a retainable object type, the rule doesn't apply.
1875f85e193739c953358c865005855253af4f68a497John McCall    if (!declSpecType->isObjCRetainableType()) return;
1876f85e193739c953358c865005855253af4f68a497John McCall
1877f85e193739c953358c865005855253af4f68a497John McCall    // If it already has lifetime, don't do anything.
1878f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType.getObjCLifetime()) return;
1879f85e193739c953358c865005855253af4f68a497John McCall
1880f85e193739c953358c865005855253af4f68a497John McCall    // Otherwise, modify the type in-place.
1881f85e193739c953358c865005855253af4f68a497John McCall    Qualifiers qs;
188291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1883f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType->isObjCARCImplicitlyUnretainedType())
1884f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
1885f85e193739c953358c865005855253af4f68a497John McCall    else
1886f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing);
1887f85e193739c953358c865005855253af4f68a497John McCall    declSpecType = S.Context.getQualifiedType(declSpecType, qs);
1888f85e193739c953358c865005855253af4f68a497John McCall
1889f85e193739c953358c865005855253af4f68a497John McCall  // If we have *two* pointers, then we want to throw the qualifier on
1890f85e193739c953358c865005855253af4f68a497John McCall  // the outermost pointer.
1891f85e193739c953358c865005855253af4f68a497John McCall  } else if (numPointers == 2) {
1892f85e193739c953358c865005855253af4f68a497John McCall    // If we don't have a block pointer, we need to check whether the
1893f85e193739c953358c865005855253af4f68a497John McCall    // declaration-specifiers gave us something that will turn into a
1894f85e193739c953358c865005855253af4f68a497John McCall    // retainable object pointer after we slap the first pointer on it.
1895f85e193739c953358c865005855253af4f68a497John McCall    if (!isBlockPointer && !declSpecType->isObjCObjectType())
1896f85e193739c953358c865005855253af4f68a497John McCall      return;
1897f85e193739c953358c865005855253af4f68a497John McCall
1898f85e193739c953358c865005855253af4f68a497John McCall    // Look for an explicit lifetime attribute there.
1899f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex);
19001c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis    if (chunk.Kind != DeclaratorChunk::Pointer &&
19011c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis        chunk.Kind != DeclaratorChunk::BlockPointer)
19021c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis      return;
1903f85e193739c953358c865005855253af4f68a497John McCall    for (const AttributeList *attr = chunk.getAttrs(); attr;
1904f85e193739c953358c865005855253af4f68a497John McCall           attr = attr->getNext())
19058e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt      if (attr->getKind() == AttributeList::AT_ObjCOwnership)
1906f85e193739c953358c865005855253af4f68a497John McCall        return;
1907f85e193739c953358c865005855253af4f68a497John McCall
1908a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis    transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing,
1909a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                          outermostPointerIndex);
1910f85e193739c953358c865005855253af4f68a497John McCall
1911f85e193739c953358c865005855253af4f68a497John McCall  // Any other number of pointers/references does not trigger the rule.
1912f85e193739c953358c865005855253af4f68a497John McCall  } else return;
1913f85e193739c953358c865005855253af4f68a497John McCall
1914f85e193739c953358c865005855253af4f68a497John McCall  // TODO: mark whether we did this inference?
1915f85e193739c953358c865005855253af4f68a497John McCall}
1916f85e193739c953358c865005855253af4f68a497John McCall
19178c952cd40ccec9d720931f27e7d722fed207d536Richard Smithstatic void diagnoseIgnoredQualifiers(
19188c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    Sema &S, unsigned Quals,
19198c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation FallbackLoc,
19208c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation ConstQualLoc = SourceLocation(),
19218c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation VolatileQualLoc = SourceLocation(),
19228c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation RestrictQualLoc = SourceLocation(),
19238c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation AtomicQualLoc = SourceLocation()) {
1924eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith  if (!Quals)
1925eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith    return;
1926eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith
19278c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  const SourceManager &SM = S.getSourceManager();
19288c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19298c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  struct Qual {
19308c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    unsigned Mask;
19318c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    const char *Name;
19328c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation Loc;
19338c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  } const QualKinds[4] = {
19348c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_const, "const", ConstQualLoc },
19358c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_volatile, "volatile", VolatileQualLoc },
19368c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_restrict, "restrict", RestrictQualLoc },
19378c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_atomic, "_Atomic", AtomicQualLoc }
19388c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  };
19398c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
194030d237556fdd29b5075c990da953116225b95d9dRobert Wilhelm  SmallString<32> QualStr;
1941d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  unsigned NumQuals = 0;
1942d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  SourceLocation Loc;
19438c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  FixItHint FixIts[4];
19448c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19458c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // Build a string naming the redundant qualifiers.
19468c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  for (unsigned I = 0; I != 4; ++I) {
19478c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    if (Quals & QualKinds[I].Mask) {
19488c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if (!QualStr.empty()) QualStr += ' ';
19498c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      QualStr += QualKinds[I].Name;
19508c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19518c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // If we have a location for the qualifier, offer a fixit.
19528c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      SourceLocation QualLoc = QualKinds[I].Loc;
19538c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if (!QualLoc.isInvalid()) {
19548c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        FixIts[NumQuals] = FixItHint::CreateRemoval(QualLoc);
19558c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        if (Loc.isInvalid() || SM.isBeforeInTranslationUnit(QualLoc, Loc))
19568c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          Loc = QualLoc;
19578c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      }
1958d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
19598c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      ++NumQuals;
19608c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
19618c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  }
1962d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
19638c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  S.Diag(Loc.isInvalid() ? FallbackLoc : Loc, diag::warn_qual_return_type)
19648c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    << QualStr << NumQuals << FixIts[0] << FixIts[1] << FixIts[2] << FixIts[3];
19658c952cd40ccec9d720931f27e7d722fed207d536Richard Smith}
1966a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg
19678c952cd40ccec9d720931f27e7d722fed207d536Richard Smith// Diagnose pointless type qualifiers on the return type of a function.
19688c952cd40ccec9d720931f27e7d722fed207d536Richard Smithstatic void diagnoseIgnoredFunctionQualifiers(Sema &S, QualType RetTy,
19698c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                              Declarator &D,
19708c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                              unsigned FunctionChunkIndex) {
19718c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  if (D.getTypeObject(FunctionChunkIndex).Fun.hasTrailingReturnType()) {
19728c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    // FIXME: TypeSourceInfo doesn't preserve location information for
19738c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    // qualifiers.
1974eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith    diagnoseIgnoredQualifiers(S, RetTy.getLocalCVRQualifiers(),
19758c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                              D.getIdentifierLoc());
19768c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    return;
1977d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
19788c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19798c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  for (unsigned OuterChunkIndex = FunctionChunkIndex + 1,
19808c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                End = D.getNumTypeObjects();
19818c952cd40ccec9d720931f27e7d722fed207d536Richard Smith       OuterChunkIndex != End; ++OuterChunkIndex) {
19828c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    DeclaratorChunk &OuterChunk = D.getTypeObject(OuterChunkIndex);
19838c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    switch (OuterChunk.Kind) {
19848c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Paren:
19858c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      continue;
19868c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19878c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Pointer: {
19888c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      DeclaratorChunk::PointerTypeInfo &PTI = OuterChunk.Ptr;
19898c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      diagnoseIgnoredQualifiers(
19908c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          S, PTI.TypeQuals,
19918c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation(),
19928c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
19938c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
19948c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
19958c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.AtomicQualLoc));
19968c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      return;
19978c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
19988c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19998c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Function:
20008c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::BlockPointer:
20018c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Reference:
20028c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Array:
20038c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::MemberPointer:
20048c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // FIXME: We can't currently provide an accurate source location and a
20058c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // fix-it hint for these.
2006eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith      unsigned AtomicQual = RetTy->isAtomicType() ? DeclSpec::TQ_atomic : 0;
20078c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      diagnoseIgnoredQualifiers(S, RetTy.getCVRQualifiers() | AtomicQual,
20088c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                D.getIdentifierLoc());
20098c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      return;
20108c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
20118c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
20128c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    llvm_unreachable("unknown declarator chunk kind");
20134cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
2014d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
20158c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // If the qualifiers come from a conversion function type, don't diagnose
20168c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // them -- they're not necessarily redundant, since such a conversion
20178c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // operator can be explicitly called as "x.operator const int()".
20188c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  if (D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId)
20198c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    return;
2020d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
20218c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // Just parens all the way out to the decl specifiers. Diagnose any qualifiers
20228c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // which are present there.
2023eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith  diagnoseIgnoredQualifiers(S, D.getDeclSpec().getTypeQualifiers(),
20248c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getIdentifierLoc(),
20258c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getConstSpecLoc(),
20268c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getVolatileSpecLoc(),
20278c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getRestrictSpecLoc(),
20288c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getAtomicSpecLoc());
2029d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth}
2030d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
20318cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
20328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                             TypeSourceInfo *&ReturnTypeInfo) {
20338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &SemaRef = state.getSema();
20348cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
2035930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
20368cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ReturnTypeInfo = 0;
2037711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
20388cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // The TagDecl owned by the DeclSpec.
20398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TagDecl *OwnedTagDecl = 0;
20408999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl
204160e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  bool ContainsPlaceholderType = false;
204260e141e1f87211ca831de6821003d80fe20a06f3Richard Smith
20433f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
204498a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian  case UnqualifiedId::IK_ImplicitSelfParam:
20453f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
20468999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  case UnqualifiedId::IK_Identifier:
20470486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
20483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
20498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = ConvertDeclSpecToType(state);
205060e141e1f87211ca831de6821003d80fe20a06f3Richard Smith    ContainsPlaceholderType = D.getDeclSpec().containsPlaceholderType();
205191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2052591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
2053d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
205415987970eeaa1842c29ec8797affd1c1dea05585Abramo Bagnara      // Owned declaration is embedded in declarator.
2055d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl->setEmbeddedInDeclarator(true);
2056591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
2057930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
2058930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
20593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
20600efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
20613f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
2062930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
206391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // "void" instead.
20648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = SemaRef.Context.VoidTy;
2065a78a640c5f59720f2c2b8034eca4fbf8525d9026Rafael Espindola    if (AttributeList *attrs = D.getDeclSpec().getAttributes().getList())
2066f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclSpec, attrs);
2067930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
206848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
206948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
207048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
207148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
207291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId,
20738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  &ReturnTypeInfo);
207460e141e1f87211ca831de6821003d80fe20a06f3Richard Smith    ContainsPlaceholderType = T->getContainedAutoType();
207548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
2076930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
2077dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
2078711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
2079711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
2080711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2081d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
2082d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // In C++11, a function declarator using 'auto' must have a trailing return
20838110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // type (this is checked later) and we can skip this. In other languages
20848110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // using auto, we need to check regardless.
208560e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  if (ContainsPlaceholderType &&
208680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      (!SemaRef.getLangOpts().CPlusPlus11 || !D.isFunctionDeclarator())) {
2087baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
20881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2089baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
2090baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
2091b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("K&R type lists aren't allowed in C++");
2092f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2093f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman      llvm_unreachable("Can't specify a type specifier in lambda grammar");
2094cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2095cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
2096baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
2097152b4e4652baedfceba1cd8115515629225e713fManuel Klimek      Error = 0; // Function prototype
2098baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2099baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
21007a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)
21017a614d8380297fcd2bc23986241905d97222948cRichard Smith        break;
21028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) {
2103eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie      case TTK_Enum: llvm_unreachable("unhandled tag kind");
2104465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
2105465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
2106465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
21076666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      case TTK_Interface: Error = 4; /* Interface member */ break;
21081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
2109baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2110baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
211117b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
21126666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 5; // Exception declaration
2113baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2114baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
21156666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 6; // Template parameter
2116baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2117baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
21186666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 7; // Block literal
211934b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
212034b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TemplateTypeArgContext:
21216666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 8; // Template type argument
212234b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
2123162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
21243e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
21256666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 10; // Type alias
2126162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      break;
21277796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
212860e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      if (!SemaRef.getLangOpts().CPlusPlus1y)
212960e141e1f87211ca831de6821003d80fe20a06f3Richard Smith        Error = 11; // Function return type
21307796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
213114f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    case Declarator::ConversionIdContext:
213260e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      if (!SemaRef.getLangOpts().CPlusPlus1y)
213360e141e1f87211ca831de6821003d80fe20a06f3Richard Smith        Error = 12; // conversion-type-id
213414f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      break;
213534b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TypeNameContext:
213614f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      Error = 13; // Generic
2137baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2138baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
2139baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
2140baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
2141baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
21420b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
2143baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2144baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
2145baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
2146ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith    if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
21476666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 9;
2148ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith
21498110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    // In Objective-C it is an error to use 'auto' on a function declarator.
21508110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    if (D.isFunctionDeclarator())
21516666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11;
21528110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith
2153d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.spec.auto]p2: 'auto' is always fine if the declarator
2154e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // contains a trailing return type. That is only legal at the outermost
2155e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // level. Check all declarator chunks (outermost first) anyway, to give
2156e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // better diagnostics.
215780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (SemaRef.getLangOpts().CPlusPlus11 && Error != -1) {
2158e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2159e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned chunkIndex = e - i - 1;
2160e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        state.setCurrentChunkIndex(chunkIndex);
2161e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2162e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (DeclType.Kind == DeclaratorChunk::Function) {
2163e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
216454655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (FTI.hasTrailingReturnType()) {
2165e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Error = -1;
2166e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            break;
2167e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          }
2168e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        }
2169e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2170e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    }
2171e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
217214f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    SourceRange AutoRange = D.getDeclSpec().getTypeSpecTypeLoc();
217314f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    if (D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId)
217414f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      AutoRange = D.getName().getSourceRange();
217514f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith
2176baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
217714f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      SemaRef.Diag(AutoRange.getBegin(), diag::err_auto_not_allowed)
217814f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith        << Error << AutoRange;
21798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = SemaRef.Context.IntTy;
2180baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
21810aa86c0463a881be85fd34e04c7de3379997621dRichard Smith    } else
218214f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      SemaRef.Diag(AutoRange.getBegin(),
218314f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith                   diag::warn_cxx98_compat_auto_type_specifier)
218414f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith        << AutoRange;
2185baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
21868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
21874e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().CPlusPlus &&
21885e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall      OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) {
21898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // Check the contexts where C++ forbids the declaration of a new class
21908cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // or enumeration in a type-specifier-seq.
21918cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    switch (D.getContext()) {
21927796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
21937796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // Class and enumeration definitions are syntactically not allowed in
21947796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // trailing return types.
21957796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      llvm_unreachable("parser should not have allowed this");
21967796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
21978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::FileContext:
21988cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::MemberContext:
21998cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockContext:
22008cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ForContext:
22018cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockLiteralContext:
2202f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2203d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // C++11 [dcl.type]p3:
22048cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   A type-specifier-seq shall not define a class or enumeration unless
22058cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   it appears in the type-id of an alias-declaration (7.1.3) that is not
22068cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   the declaration of a template-declaration.
22078cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasDeclContext:
22088cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22098cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasTemplateContext:
22108cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22118cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_alias_template)
22128cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2213601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TypeNameContext:
221614f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    case Declarator::ConversionIdContext:
22178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateParamContext:
22188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXNewContext:
22198cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXCatchContext:
22208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ObjCCatchContext:
22218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateTypeArgContext:
22228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22238cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_type_specifier)
22248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2225601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::PrototypeContext:
2228cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2229cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
22308cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::KNRTypeListContext:
22318cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ [dcl.fct]p6:
22328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   Types shall not be defined in return or parameter types.
22338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22348cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_param_type)
22358cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2236601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22378cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22388cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ConditionContext:
22398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ 6.4p2:
22408cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // The type-specifier-seq shall not contain typedef and shall not declare
22418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // a new class or enumeration.
22428cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_condition);
2244601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22458cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    }
22478cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  }
22488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
22498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return T;
22508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
22518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2252a08c2fb74ef823c185619ecc532f8fced6a1982fBenjamin Kramerstatic std::string getFunctionQualifiersAsString(const FunctionProtoType *FnTy){
2253d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  std::string Quals =
2254d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
2255d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2256d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (FnTy->getRefQualifier()) {
2257d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_None:
2258d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2259d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2260d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_LValue:
2261d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2262d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2263d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += '&';
2264d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2265d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2266d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_RValue:
2267d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2268d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2269d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += "&&";
2270d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2271d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2272d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2273d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  return Quals;
2274d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2275d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2276d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// Check that the function type T, which has a cv-qualifier or a ref-qualifier,
2277d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// can be contained within the declarator chunk DeclType, and produce an
2278d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// appropriate diagnostic if not.
2279d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smithstatic void checkQualifiedFunction(Sema &S, QualType T,
2280d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                   DeclaratorChunk &DeclType) {
2281d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++98 [dcl.fct]p4 / C++11 [dcl.fct]p6: a function type with a
2282d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // cv-qualifier or a ref-qualifier can only appear at the topmost level
2283d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // of a type.
2284d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  int DiagKind = -1;
2285d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (DeclType.Kind) {
2286d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Paren:
2287d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::MemberPointer:
2288d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases are permitted.
2289d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2290d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Array:
2291d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Function:
2292d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases don't allow function types at all; no need to diagnose the
2293d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // qualifiers separately.
2294d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2295d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::BlockPointer:
2296d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 0;
2297d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2298d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Pointer:
2299d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 1;
2300d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2301d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Reference:
2302d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 2;
2303d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2304d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2305d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2306d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  assert(DiagKind != -1);
2307d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  S.Diag(DeclType.Loc, diag::err_compound_qualified_function_type)
2308d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << DiagKind << isa<FunctionType>(T.IgnoreParens()) << T
2309d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << getFunctionQualifiersAsString(T->castAs<FunctionProtoType>());
2310d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2311d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2312b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// Produce an approprioate diagnostic for an ambiguity between a function
2313b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// declarator and a C++ direct-initializer.
2314b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smithstatic void warnAboutAmbiguousFunction(Sema &S, Declarator &D,
2315b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith                                       DeclaratorChunk &DeclType, QualType RT) {
2316b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2317b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  assert(FTI.isAmbiguous && "no direct-initializer / function ambiguity");
2318b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2319b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // If the return type is void there is no ambiguity.
2320b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isVoidType())
2321b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2322b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2323b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a non-class type can have at most one argument.
2324b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!RT->isRecordType() && FTI.NumArgs > 1)
2325b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2326b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2327b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a reference must have exactly one argument.
2328b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isReferenceType() && FTI.NumArgs != 1)
2329b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2330b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2331b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Only warn if this declarator is declaring a function at block scope, and
2332b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // doesn't have a storage class (such as 'extern') specified.
2333b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!D.isFunctionDeclarator() ||
2334b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      D.getFunctionDefinitionKind() != FDK_Declaration ||
2335b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      !S.CurContext->isFunctionOrMethod() ||
2336d2615cc53b916e8aae45783ca7113b93de515ce3Rafael Espindola      D.getDeclSpec().getStorageClassSpec()
2337b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        != DeclSpec::SCS_unspecified)
2338b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2339b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2340b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Inside a condition, a direct initializer is not permitted. We allow one to
2341b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // be parsed in order to give better diagnostics in condition parsing.
2342b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (D.getContext() == Declarator::ConditionContext)
2343b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2344b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2345b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  SourceRange ParenRange(DeclType.Loc, DeclType.EndLoc);
2346b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2347d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  S.Diag(DeclType.Loc,
2348d64effc4e31044c05d6e4400150edb26e914983aRichard Smith         FTI.NumArgs ? diag::warn_parens_disambiguated_as_function_declaration
2349d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                     : diag::warn_empty_parens_are_function_decl)
2350d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    << ParenRange;
2351d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2352d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // If the declaration looks like:
2353d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   T var1,
2354d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   f();
2355d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // and name lookup finds a function named 'f', then the ',' was
2356d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // probably intended to be a ';'.
2357d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (!D.isFirstDeclarator() && D.getIdentifier()) {
2358d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Comma(D.getCommaLoc(), S.SourceMgr);
2359d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Name(D.getIdentifierLoc(), S.SourceMgr);
2360d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    if (Comma.getFileID() != Name.getFileID() ||
2361d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) {
2362d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      LookupResult Result(S, D.getIdentifier(), SourceLocation(),
2363d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                          Sema::LookupOrdinaryName);
2364d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      if (S.LookupName(Result, S.getCurScope()))
2365d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        S.Diag(D.getCommaLoc(), diag::note_empty_parens_function_call)
2366d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << FixItHint::CreateReplacement(D.getCommaLoc(), ";")
2367d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << D.getIdentifier();
2368d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    }
2369d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  }
2370b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2371d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (FTI.NumArgs > 0) {
2372d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration with parameters, eg. "T var(T());", suggest adding parens
2373d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // around the first parameter to turn the declaration into a variable
2374d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2375b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceRange Range = FTI.ArgInfo[0].Param->getSourceRange();
2376b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation B = Range.getBegin();
2377b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation E = S.PP.getLocForEndOfToken(Range.getEnd());
2378b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // FIXME: Maybe we should suggest adding braces instead of parens
2379b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // in C++11 for classes that don't have an initializer_list constructor.
2380b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    S.Diag(B, diag::note_additional_parens_for_variable_declaration)
2381b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(B, "(")
2382b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(E, ")");
2383d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  } else {
2384d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration without parameters, eg. "T var();", suggest replacing the
2385d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // parens with an initializer to turn the declaration into a variable
2386d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2387b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
2388d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2389b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // Empty parens mean value-initialization, and no parens mean
2390b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // default initialization. These are equivalent if the default
2391b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // constructor is user-provided or if zero-initialization is a
2392b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // no-op.
2393b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    if (RD && RD->hasDefinition() &&
2394b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        (RD->isEmpty() || RD->hasUserProvidedDefaultConstructor()))
2395b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor)
2396b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        << FixItHint::CreateRemoval(ParenRange);
2397b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    else {
2398b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      std::string Init = S.getFixItZeroInitializerForType(RT);
239980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      if (Init.empty() && S.LangOpts.CPlusPlus11)
2400b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        Init = "{}";
2401b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (!Init.empty())
2402b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        S.Diag(DeclType.Loc, diag::note_empty_parens_zero_initialize)
2403b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith          << FixItHint::CreateReplacement(ParenRange, Init);
2404b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    }
2405b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  }
2406b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith}
2407b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2408ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner/// Helper for figuring out the default CC for a function declarator type.  If
2409ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner/// this is the outermost chunk, then we can determine the CC from the
2410ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner/// declarator context.  If not, then this could be either a member function
2411ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner/// type or normal function type.
2412ef072033876e295ec5d3402f8730a3ae358ad815Reid Klecknerstatic CallingConv
2413ef072033876e295ec5d3402f8730a3ae358ad815Reid KlecknergetCCForDeclaratorChunk(Sema &S, Declarator &D,
2414ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner                        const DeclaratorChunk::FunctionTypeInfo &FTI,
2415ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner                        unsigned ChunkIndex) {
2416ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  assert(D.getTypeObject(ChunkIndex).Kind == DeclaratorChunk::Function);
2417ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
2418ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  bool IsCXXInstanceMethod = false;
2419ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
2420ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  if (S.getLangOpts().CPlusPlus) {
2421ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // Look inwards through parentheses to see if this chunk will form a
2422ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // member pointer type or if we're the declarator.  Any type attributes
2423ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // between here and there will override the CC we choose here.
2424ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    unsigned I = ChunkIndex;
2425ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    bool FoundNonParen = false;
2426ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    while (I && !FoundNonParen) {
2427ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      --I;
2428ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      if (D.getTypeObject(I).Kind != DeclaratorChunk::Paren)
2429ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        FoundNonParen = true;
2430ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    }
2431ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
2432ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    if (FoundNonParen) {
2433ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      // If we're not the declarator, we're a regular function type unless we're
2434ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      // in a member pointer.
2435ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      IsCXXInstanceMethod =
2436ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner          D.getTypeObject(I).Kind == DeclaratorChunk::MemberPointer;
2437ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    } else {
2438ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      // We're the innermost decl chunk, so must be a function declarator.
2439ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      assert(D.isFunctionDeclarator());
2440ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
2441ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      // If we're inside a record, we're declaring a method, but it could be
2442ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      // static.
2443ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      IsCXXInstanceMethod =
2444ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner          (D.getContext() == Declarator::MemberContext &&
2445ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner           D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
2446ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner           D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static &&
2447ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner           !D.getDeclSpec().isFriendSpecified());
2448ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    }
2449ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  }
2450ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
2451ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  return S.Context.getDefaultCallingConvention(FTI.isVariadic,
2452ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner                                               IsCXXInstanceMethod);
2453ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner}
2454ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
24558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
24568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                QualType declSpecType,
24578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                TypeSourceInfo *TInfo) {
24588cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
24598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = declSpecType;
24608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
24618cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
24628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ASTContext &Context = S.Context;
24634e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &LangOpts = S.getLangOpts();
24648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2465cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
2466cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
2467cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
2468cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
24691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2470162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  // Does this declaration declare a typedef-name?
2471162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  bool IsTypedefName =
2472162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef ||
24733e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasDeclContext ||
24743e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasTemplateContext;
2475162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2476d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // Does T refer to a function type with a cv-qualifier or a ref-qualifier?
2477d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  bool IsQualifiedFunction = T->isFunctionProtoType() &&
2478d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      (T->castAs<FunctionProtoType>()->getTypeQuals() != 0 ||
2479d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith       T->castAs<FunctionProtoType>()->getRefQualifier() != RQ_None);
2480d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2481a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  // If T is 'decltype(auto)', the only declarators we can have are parens
2482a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  // and at most one function declarator if this is a function declaration.
2483a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  if (const AutoType *AT = T->getAs<AutoType>()) {
2484a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    if (AT->isDecltypeAuto()) {
2485a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) {
2486a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        unsigned Index = E - I - 1;
2487a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        DeclaratorChunk &DeclChunk = D.getTypeObject(Index);
2488a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        unsigned DiagId = diag::err_decltype_auto_compound_type;
2489a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        unsigned DiagKind = 0;
2490a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        switch (DeclChunk.Kind) {
2491a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Paren:
2492a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          continue;
2493a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Function: {
2494a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          unsigned FnIndex;
2495a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          if (D.isFunctionDeclarationContext() &&
2496a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith              D.isFunctionDeclarator(FnIndex) && FnIndex == Index)
2497a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            continue;
2498a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagId = diag::err_decltype_auto_function_declarator_not_declaration;
2499a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2500a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        }
2501a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Pointer:
2502a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::BlockPointer:
2503a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::MemberPointer:
2504a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagKind = 0;
2505a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2506a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Reference:
2507a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagKind = 1;
2508a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2509a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Array:
2510a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagKind = 2;
2511a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2512a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        }
2513a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
2514a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        S.Diag(DeclChunk.Loc, DiagId) << DiagKind;
2515a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        D.setInvalidType(true);
2516a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        break;
2517a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      }
2518a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    }
2519a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  }
2520a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
252198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
252298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
252398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
25248ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2525711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
2526711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
2527711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2528d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction) {
2529d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      checkQualifiedFunction(S, T, DeclType);
2530d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = DeclType.Kind == DeclaratorChunk::Paren;
2531d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    }
25325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
2533075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
25348cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildParenType(T);
2535075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
25365618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
25379af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
25389af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
25398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_blocks_disable);
25401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
25422865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
25438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
25445618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
25455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
25466a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
25476a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
25488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
25498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
25506a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
25516a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
25526a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
25538cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.ObjC1 && T->getAs<ObjCObjectType>()) {
2554c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
25552865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
25568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
255714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
255814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
25598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildPointerType(T, DeclType.Loc, Name);
25602865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
25618cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
2562711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
25635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
25640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
25656a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
25666a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
25678cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
25688cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
25696a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
25706a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
25716a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
25728cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
25732865474261a608c7873b87ba4af110d17907896dJohn McCall
25742865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
25752865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
25768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
25775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
25780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
25795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
25806a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
25816a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
25828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
25838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
25846a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
25856a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
25866a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
2587fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
258894f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
25895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
25905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
25915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
25925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
25935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
25945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
25955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
2596c05a94b7accd4035bf5d5897c434c445b22da855John McCall      if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
2597f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
2598f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
2599f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
26008cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
2601f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
2602f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
2603f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
26047f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
26057f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // C99 6.7.5.2p1: The optional type qualifiers and the keyword static
26067f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // shall appear only in a declaration of a function parameter with an
26077f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // array type, ...
26087f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      if (ASM == ArrayType::Static || ATI.TypeQuals) {
260999570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay        if (!(D.isPrototypeContext() ||
261099570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay              D.getContext() == Declarator::KNRTypeListContext)) {
26117f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          S.Diag(DeclType.Loc, diag::err_array_static_outside_prototype) <<
26127f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
26137f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Remove the 'static' and the type qualifiers.
26147f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          if (ASM == ArrayType::Static)
26157f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ASM = ArrayType::Normal;
26167f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          ATI.TypeQuals = 0;
26177f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          D.setInvalidType(true);
26187f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
26197f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
26207f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // C99 6.7.5.2p1: ... and then only in the outermost array type
26217f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // derivation.
26227f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        unsigned x = chunkIndex;
26237f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        while (x != 0) {
26247f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Walk outwards along the declarator chunks.
26257f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          x--;
26267f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          const DeclaratorChunk &DC = D.getTypeObject(x);
26277f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          switch (DC.Kind) {
26287f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Paren:
26297f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            continue;
26307f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Array:
26317f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Pointer:
26327f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Reference:
26337f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::MemberPointer:
26347f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            S.Diag(DeclType.Loc, diag::err_array_static_not_outermost) <<
26357f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
26367f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            if (ASM == ArrayType::Static)
26377f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              ASM = ArrayType::Normal;
26387f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ATI.TypeQuals = 0;
26397f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            D.setInvalidType(true);
26407f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
26417f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Function:
26427f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::BlockPointer:
26437f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            // These are invalid anyway, so just ignore.
26447f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
26457f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          }
26467f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
26477f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      }
2648152b4e4652baedfceba1cd8115515629225e713fManuel Klimek
2649152b4e4652baedfceba1cd8115515629225e713fManuel Klimek      if (const AutoType *AT = T->getContainedAutoType()) {
2650a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        // We've already diagnosed this for decltype(auto).
2651dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        if (!AT->isDecltypeAuto())
2652a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          S.Diag(DeclType.Loc, diag::err_illegal_decl_array_of_auto)
2653a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            << getPrintableNameForEntity(Name) << T;
2654dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        T = QualType();
2655dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        break;
2656a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      }
2657a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
26588ac2c66a1442985091c5ec2b33ce6d3df3bcb529Eli Friedman      T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
26598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                           SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
26605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
26615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2662f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
26635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
26645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
26655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
26665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2667d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = FTI.TypeQuals || FTI.hasRefQualifier();
26683cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
266934b41d939a1328f484511c6002ba2456db879a29Richard Smith      // Check for auto functions and trailing return type and adjust the
267034b41d939a1328f484511c6002ba2456db879a29Richard Smith      // return type accordingly.
267134b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!D.isInvalidType()) {
267234b41d939a1328f484511c6002ba2456db879a29Richard Smith        // trailing-return-type is only required if we're declaring a function,
267334b41d939a1328f484511c6002ba2456db879a29Richard Smith        // and not, for instance, a pointer to a function.
267434b41d939a1328f484511c6002ba2456db879a29Richard Smith        if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
267560e141e1f87211ca831de6821003d80fe20a06f3Richard Smith            !FTI.hasTrailingReturnType() && chunkIndex == 0 &&
267660e141e1f87211ca831de6821003d80fe20a06f3Richard Smith            !S.getLangOpts().CPlusPlus1y) {
26778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
267834b41d939a1328f484511c6002ba2456db879a29Richard Smith               diag::err_auto_missing_trailing_return);
267934b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = Context.IntTy;
268034b41d939a1328f484511c6002ba2456db879a29Richard Smith          D.setInvalidType(true);
268154655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        } else if (FTI.hasTrailingReturnType()) {
2682e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          // T must be exactly 'auto' at this point. See CWG issue 681.
2683e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (isa<ParenType>(T)) {
26848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
2685e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith                 diag::err_trailing_return_in_parens)
2686e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith              << T << D.getDeclSpec().getSourceRange();
2687e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.setInvalidType(true);
2688f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman          } else if (D.getContext() != Declarator::LambdaExprContext &&
2689a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                     (T.hasQualifiers() || !isa<AutoType>(T) ||
2690a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                      cast<AutoType>(T)->isDecltypeAuto())) {
26918cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
269234b41d939a1328f484511c6002ba2456db879a29Richard Smith                 diag::err_trailing_return_without_auto)
269334b41d939a1328f484511c6002ba2456db879a29Richard Smith              << T << D.getDeclSpec().getSourceRange();
269434b41d939a1328f484511c6002ba2456db879a29Richard Smith            D.setInvalidType(true);
269534b41d939a1328f484511c6002ba2456db879a29Richard Smith          }
269654655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          T = S.GetTypeFromParser(FTI.getTrailingReturnType(), &TInfo);
269754655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (T.isNull()) {
269854655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            // An error occurred parsing the trailing return type.
269954655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            T = Context.IntTy;
270054655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            D.setInvalidType(true);
270154655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          }
270234b41d939a1328f484511c6002ba2456db879a29Richard Smith        }
270334b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
270434b41d939a1328f484511c6002ba2456db879a29Richard Smith
2705e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // C99 6.7.5.3p1: The return type may not be a function or array type.
2706e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // For conversion functions, we'll diagnose this particular error later.
2707e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      if ((T->isArrayType() || T->isFunctionType()) &&
2708e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
2709e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned diagID = diag::err_func_returning_array_function;
2710e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // Last processing chunk in block context means this function chunk
2711e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // represents the block.
2712e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (chunkIndex == 0 &&
2713e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.getContext() == Declarator::BlockLiteralContext)
2714e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          diagID = diag::err_block_returning_array_function;
27158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
2716e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        T = Context.IntTy;
2717e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        D.setInvalidType(true);
2718e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2719e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2720aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Do not allow returning half FP value.
2721aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // FIXME: This really should be in BuildFunctionType.
2722aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      if (T->isHalfType()) {
272319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        if (S.getLangOpts().OpenCL) {
272419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          if (!S.getOpenCLOptions().cl_khr_fp16) {
272519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            S.Diag(D.getIdentifierLoc(), diag::err_opencl_half_return) << T;
272619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            D.setInvalidType(true);
272719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          }
272819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        } else {
272919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          S.Diag(D.getIdentifierLoc(),
273019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            diag::err_parameters_retval_cannot_have_fp16_type) << 1;
273119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          D.setInvalidType(true);
273219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        }
2733aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      }
2734aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
2735ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      // Methods cannot return interface types. All ObjC objects are
2736ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      // passed by reference.
2737ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      if (T->isObjCObjectType()) {
2738ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        SourceLocation DiagLoc, FixitLoc;
2739ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        if (TInfo) {
2740ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          DiagLoc = TInfo->getTypeLoc().getLocStart();
2741ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          FixitLoc = S.PP.getLocForEndOfToken(TInfo->getTypeLoc().getLocEnd());
2742ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        } else {
2743ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          DiagLoc = D.getDeclSpec().getTypeSpecTypeLoc();
2744ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          FixitLoc = S.PP.getLocForEndOfToken(D.getDeclSpec().getLocEnd());
2745ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        }
2746ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        S.Diag(DiagLoc, diag::err_object_cannot_be_passed_returned_by_value)
2747ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          << 0 << T
2748ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          << FixItHint::CreateInsertion(FixitLoc, "*");
2749ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
2750ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        T = Context.getObjCObjectPointerType(T);
2751ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        if (TInfo) {
2752ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TypeLocBuilder TLB;
2753ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TLB.pushFullCopy(TInfo->getTypeLoc());
2754ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          ObjCObjectPointerTypeLoc TLoc = TLB.push<ObjCObjectPointerTypeLoc>(T);
2755ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TLoc.setStarLoc(FixitLoc);
2756ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TInfo = TLB.getTypeSourceInfo(Context, T);
2757ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        }
2758ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
2759ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        D.setInvalidType(true);
2760ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      }
2761ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
27625291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
27635291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
27648c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if ((T.getCVRQualifiers() || T->isAtomicType()) &&
27658c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          !(S.getLangOpts().CPlusPlus &&
27668c952cd40ccec9d720931f27e7d722fed207d536Richard Smith            (T->isDependentType() || T->isRecordType())))
27678c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        diagnoseIgnoredFunctionQualifiers(S, T, D, chunkIndex);
2768d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
276902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // Objective-C ARC ownership qualifiers are ignored on the function
277002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // return type (by type canonicalization). Complain if this attribute
277102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // was written here.
277202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      if (T.getQualifiers().hasObjCLifetime()) {
277302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        SourceLocation AttrLoc;
277402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (chunkIndex + 1 < D.getNumTypeObjects()) {
277502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
277602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr = ReturnTypeChunk.getAttrs();
277702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
277802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
277902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
278002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
278102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
278202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
278302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
278402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isInvalid()) {
278502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr
278602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor                 = D.getDeclSpec().getAttributes().getList();
278702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
278802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
278902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
279002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
279102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
279202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
279302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
279402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
279502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isValid()) {
279602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // The ownership attributes are almost always written via
279702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // the predefined
279802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // __strong/__weak/__autoreleasing/__unsafe_unretained.
279902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          if (AttrLoc.isMacroID())
280002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            AttrLoc = S.SourceMgr.getImmediateExpansionRange(AttrLoc).first;
280102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
280202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          S.Diag(AttrLoc, diag::warn_arc_lifetime_result_type)
280302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            << T.getQualifiers().getObjCLifetime();
280402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
280502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      }
280602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
28078cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
2808402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
2809402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
2810b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
28115e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall        if (Tag->isCompleteDefinition())
28128cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
2813402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
2814402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
2815402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
28163cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
28173cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
2818162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (IsTypedefName && FTI.getExceptionSpecType())
28198cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef)
28203e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          << (D.getContext() == Declarator::AliasDeclContext ||
28213e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith              D.getContext() == Declarator::AliasTemplateContext);
28223cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
2823b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // If we see "T var();" or "T var(T());" at block scope, it is probably
2824b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // an attempt to initialize a variable, not a function declaration.
2825b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (FTI.isAmbiguous)
2826b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        warnAboutAmbiguousFunction(S, D, DeclType, T);
2827b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2828ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      FunctionType::ExtInfo EI(getCCForDeclaratorChunk(S, D, FTI, chunkIndex));
2829ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
28308cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (!FTI.NumArgs && !FTI.isVariadic && !LangOpts.CPlusPlus) {
28312865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
2832ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        T = Context.getFunctionNoProtoType(T, EI);
28332865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
28342865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
28352865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
28362865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
28378cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        if (!FTI.NumArgs && FTI.isVariadic && !LangOpts.CPlusPlus) {
2838965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
2839965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
2840965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
28418e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt            if (Attrs->getKind() == AttributeList::AT_Overloadable) {
2842965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
2843965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
2844965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
2845965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
2846965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
2847965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
28488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
2849c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
28502865474261a608c7873b87ba4af110d17907896dJohn McCall
28512865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
2852788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
2853788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
28548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
28552865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
2856d5668a2447c2afeea38815b51a80a5a6ac235599Argyrios Kyrtzidis          // Recover by creating a K&R-style function type.
2857ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner          T = Context.getFunctionNoProtoType(T, EI);
28582865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
28592865474261a608c7873b87ba4af110d17907896dJohn McCall        }
28602865474261a608c7873b87ba4af110d17907896dJohn McCall
2861e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
2862ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        EPI.ExtInfo = EI;
2863e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
286454655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        EPI.HasTrailingReturn = FTI.hasTrailingReturnType();
2865e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
2866c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
2867c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : FTI.RefQualifierIsLValueRef? RQ_LValue
2868c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : RQ_RValue;
286991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
28705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
28715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
28725f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 16> ArgTys;
28732865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
28741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28755f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<bool, 16> ConsumedArguments;
2876f85e193739c953358c865005855253af4f68a497John McCall        ConsumedArguments.reserve(FTI.NumArgs);
2877f85e193739c953358c865005855253af4f68a497John McCall        bool HasAnyConsumedArguments = false;
2878f85e193739c953358c865005855253af4f68a497John McCall
28795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
2880d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
2881c910d4cfa5042f2c9da1eb4e0b6ed59240c0eeeeReid Kleckner          QualType ArgTy = Param->getType();
288278c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
28832dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
28845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
28855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
288672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
28872dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
28885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
28895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
28905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
28915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
28928cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(DeclType.Loc, diag::err_void_only_param);
28932ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
28948123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
28952ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
28962ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
28978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(FTI.ArgInfo[i].IdentLoc,
28984565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
28992ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
29008123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
29012ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
29022ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
29030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
29048cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                S.Diag(DeclType.Loc, diag::err_void_param_qualified);
29051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29062ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
29072ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
29082ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
2909aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov          } else if (ArgTy->isHalfType()) {
2910aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // Disallow half FP arguments.
2911aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // FIXME: This really should be in BuildFunctionType.
291219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            if (S.getLangOpts().OpenCL) {
291319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              if (!S.getOpenCLOptions().cl_khr_fp16) {
291419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                S.Diag(Param->getLocation(),
291519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                  diag::err_opencl_half_argument) << ArgTy;
291619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                D.setInvalidType();
29179dd74c5504c743c96ea3a1d691d6a75ec3a98147John McCall                Param->setInvalidDecl();
291819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              }
291919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            } else {
292019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              S.Diag(Param->getLocation(),
292119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                diag::err_parameters_retval_cannot_have_fp16_type) << 0;
292219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              D.setInvalidType();
292319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            }
2924eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
2925eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
2926a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
2927eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              Param->setKNRPromoted(true);
2928183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
2929eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              if (BTy->getKind() == BuiltinType::Float) {
2930eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
2931eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall                Param->setKNRPromoted(true);
2932eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              }
2933eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
29345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
293556a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
29368cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          if (LangOpts.ObjCAutoRefCount) {
2937f85e193739c953358c865005855253af4f68a497John McCall            bool Consumed = Param->hasAttr<NSConsumedAttr>();
2938f85e193739c953358c865005855253af4f68a497John McCall            ConsumedArguments.push_back(Consumed);
2939f85e193739c953358c865005855253af4f68a497John McCall            HasAnyConsumedArguments |= Consumed;
2940f85e193739c953358c865005855253af4f68a497John McCall          }
2941f85e193739c953358c865005855253af4f68a497John McCall
294254e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
29435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2944465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2945f85e193739c953358c865005855253af4f68a497John McCall        if (HasAnyConsumedArguments)
2946f85e193739c953358c865005855253af4f68a497John McCall          EPI.ConsumedArguments = ConsumedArguments.data();
2947f85e193739c953358c865005855253af4f68a497John McCall
29485f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 4> Exceptions;
294974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<ParsedType, 2> DynamicExceptions;
295074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<SourceRange, 2> DynamicExceptionRanges;
295174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        Expr *NoexceptExpr = 0;
295291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
29538b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        if (FTI.getExceptionSpecType() == EST_Dynamic) {
295474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // FIXME: It's rather inefficient to have to split into two vectors
295574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // here.
295674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          unsigned N = FTI.NumExceptions;
295774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptions.reserve(N);
295874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptionRanges.reserve(N);
295974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          for (unsigned I = 0; I != N; ++I) {
296074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptions.push_back(FTI.Exceptions[I].Ty);
296174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptionRanges.push_back(FTI.Exceptions[I].Range);
2962e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
29638b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
296474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          NoexceptExpr = FTI.NoexceptExpr;
296574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        }
296691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
296774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        S.checkExceptionSpecification(FTI.getExceptionSpecType(),
296874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptions,
296974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptionRanges,
297074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      NoexceptExpr,
297174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      Exceptions,
297274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      EPI);
297391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2974bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose        T = Context.getFunctionType(T, ArgTys, EPI);
29755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
297604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
29775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
29785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2979f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
2980f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
29817bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
2982f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
29837bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
2984edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
2985edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
29868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      } else if (S.isDependentScopeSpecifier(SS) ||
29878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                 dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) {
29881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
29897bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
299087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
299187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
299287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
29937bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
29944a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
299587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
299687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
299787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
299814aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor        case NestedNameSpecifier::NamespaceAlias:
299987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
30009f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
30017bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
300287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
300387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
300487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
300591ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Note: if the NNS has a prefix and ClsType is a nondependent
300691ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // TemplateSpecializationType, then the NNS prefix is NOT included
300791ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // in ClsType; hence we wrap ClsType into an ElaboratedType.
300891ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // NOTE: in particular, no wrap occurs if ClsType already is an
300991ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Elaborated, DependentName, or DependentTemplateSpecialization.
301091ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
30117bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
301287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
301387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
3014f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
30158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Mem.Scope().getBeginLoc(),
3016949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
3017949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
3018949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
3019f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
3020f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
3021f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
3022949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
30238cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
3024949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
3025f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
3026949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
30272865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
30288cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
3029f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
3030f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
3031f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
3032f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
3033cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
3034cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
3035cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
3036cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
3037cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
3038c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
3039711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
3040f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclChunk, attrs);
30415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3042971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
30438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (LangOpts.CPlusPlus && T->isFunctionType()) {
3044183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
3045778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
3046971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
304791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // C++ 8.3.5p4:
3048708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
3049708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
3050708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
3051708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
3052683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    //
3053683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // Core issue 547 also allows cv-qualifiers on function types that are
3054683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // top-level template type arguments.
3055613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
3056613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
3057906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman      FreeFunction = ((D.getContext() != Declarator::MemberContext &&
3058906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman                       D.getContext() != Declarator::LambdaExprContext) ||
3059613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall                      D.getDeclSpec().isFriendSpecified());
3060613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
30618cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec());
3062613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
3063613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
3064613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
3065d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.fct]p6 (w/DR1417):
3066d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // An attempt to specify a function type with a cv-qualifier-seq or a
3067d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // ref-qualifier (including by typedef-name) is ill-formed unless it is:
3068d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type for a non-static member function,
3069d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type to which a pointer to member refers,
3070d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the top-level function type of a function typedef declaration or
3071d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //    alias-declaration,
3072d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id in the default argument of a type-parameter, or
3073d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id of a template-argument for a type-parameter
3074d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction &&
3075d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !(!FreeFunction &&
3076d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) &&
3077d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !IsTypedefName &&
3078d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        D.getContext() != Declarator::TemplateTypeArgContext) {
307996a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      SourceLocation Loc = D.getLocStart();
3080d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      SourceRange RemovalRange;
3081d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      unsigned I;
3082d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      if (D.isFunctionDeclarator(I)) {
3083d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        SmallVector<SourceLocation, 4> RemovalLocs;
3084d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        const DeclaratorChunk &Chunk = D.getTypeObject(I);
3085d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        assert(Chunk.Kind == DeclaratorChunk::Function);
3086d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.hasRefQualifier())
3087d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getRefQualifierLoc());
3088d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Const)
3089d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getConstQualifierLoc());
3090d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Volatile)
3091d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getVolatileQualifierLoc());
3092d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        // FIXME: We do not track the location of the __restrict qualifier.
3093d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //if (Chunk.Fun.TypeQuals & Qualifiers::Restrict)
3094d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //  RemovalLocs.push_back(Chunk.Fun.getRestrictQualifierLoc());
3095d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (!RemovalLocs.empty()) {
3096d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          std::sort(RemovalLocs.begin(), RemovalLocs.end(),
3097aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                    BeforeThanCompare<SourceLocation>(S.getSourceManager()));
3098d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalRange = SourceRange(RemovalLocs.front(), RemovalLocs.back());
3099d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          Loc = RemovalLocs.front();
3100683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
3101683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      }
3102d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
3103d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      S.Diag(Loc, diag::err_invalid_qualified_function_type)
3104d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FreeFunction << D.isFunctionDeclarator() << T
3105d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << getFunctionQualifiersAsString(FnTy)
3106d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FixItHint::CreateRemoval(RemovalRange);
3107d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
3108d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // Strip the cv-qualifiers and ref-qualifiers from the type.
3109d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
3110d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.TypeQuals = 0;
3111d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.RefQualifier = RQ_None;
3112d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
31130567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner      T = Context.getFunctionType(FnTy->getResultType(), FnTy->getArgTypes(),
3114bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                  EPI);
3115e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      // Rebuild any parens around the identifier in the function type.
3116e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
3117e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        if (D.getTypeObject(i).Kind != DeclaratorChunk::Paren)
3118e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith          break;
3119e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        T = S.BuildParenType(T);
3120e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      }
3121971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
3122971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
31231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
3125711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull())
3126711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = D.getAttributes())
3127f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclName, attrs);
3128711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3129711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
3130711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
3131711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3132148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  // C++0x [dcl.constexpr]p9:
3133148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  //  A constexpr specifier used in an object declaration declares the object
313491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //  as const.
3135148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
3136737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
3137737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
3138737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
313991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // If there was an ellipsis in the declarator, the declaration declares a
3140a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
3141a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (D.hasEllipsis() && !T.isNull()) {
3142a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
314391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   A declarator-id or abstract-declarator containing an ellipsis shall
3144a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
3145a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
3146a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
3147a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
3148a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
314991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   [...] When it is part of a parameter-declaration-clause, the
315091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   parameter pack is a function parameter pack (14.5.3). The type T
3151a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
315291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   a template parameter pack; each template parameter pack in T is
3153a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
3154a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
3155a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
3156a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
3157a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
315891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(D.getEllipsisLoc(),
3159a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
3160a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
3161a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
3162a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
316366874fb18afbffb8b2ca05576851a64534be3352David Blaikie        T = Context.getPackExpansionType(T, None);
3164a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
3165a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
3166152b4e4652baedfceba1cd8115515629225e713fManuel Klimek
3167a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
3168a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
316991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   If a template-parameter is a [...] is a parameter-declaration that
3170a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
3171a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
3172a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
3173a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
3174a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
3175a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
3176a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
317766874fb18afbffb8b2ca05576851a64534be3352David Blaikie        T = Context.getPackExpansionType(T, None);
3178e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith      else
3179e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith        S.Diag(D.getEllipsisLoc(),
318080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith               LangOpts.CPlusPlus11
3181e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 ? diag::warn_cxx98_compat_variadic_templates
3182e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 : diag::ext_variadic_templates);
3183a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
318491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3185a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
3186a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
3187cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:  // FIXME: special diagnostic here?
3188cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:     // FIXME: special diagnostic here?
3189a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
31900b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
3191162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
31923e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
3193a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
3194a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
3195a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
3196a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
3197a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
319817b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
3199a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
3200f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
320114f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    case Declarator::ConversionIdContext:
32027796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
3203683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    case Declarator::TemplateTypeArgContext:
3204a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
3205a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
32068cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      S.Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
3207a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
3208a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
3209a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
3210a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
3211e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
3212bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
3213bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
3214bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
3215bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
3216db7abf78dedc2ef6ccb42b3dac6ab330fe2ea469Argyrios Kyrtzidis
32178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return S.GetTypeSourceInfoForDeclarator(D, T, TInfo);
32188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
32198cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
32208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// GetTypeForDeclarator - Convert the type for the specified
32218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// declarator to Type instances.
32228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis///
32238cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// The result of this call will never be null, but the associated
32248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// type may be a null type if there's an unrecoverable error.
32258cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
32268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // Determine the type of the declarator. Not all forms of declarator
32278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // have a type.
32288cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
32298cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeProcessingState state(*this, D);
32308cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
32318cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
32328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
32338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (T.isNull())
32348cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
32358cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
32364e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (D.isPrototypeContext() && getLangOpts().ObjCAutoRefCount)
32378cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    inferARCWriteback(state, T);
323891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
32398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return GetFullTypeForDeclarator(state, T, ReturnTypeInfo);
32405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
32415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
324231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclSpec(Sema &S,
324331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           QualType &declSpecTy,
324431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           Qualifiers::ObjCLifetime ownership) {
324531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy->isObjCRetainableType() &&
324631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) {
324731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers qs;
324831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    qs.addObjCLifetime(ownership);
324931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    declSpecTy = S.Context.getQualifiedType(declSpecTy, qs);
325031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
325131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
325231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
325331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
325431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
325531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            unsigned chunkIndex) {
325631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
325731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
325831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
325931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Look for an explicit lifetime attribute.
326031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclaratorChunk &chunk = D.getTypeObject(chunkIndex);
326131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (const AttributeList *attr = chunk.getAttrs(); attr;
326231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis         attr = attr->getNext())
32638e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    if (attr->getKind() == AttributeList::AT_ObjCOwnership)
326431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
326531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
326631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  const char *attrStr = 0;
326731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  switch (ownership) {
32683026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
326931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break;
327031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Strong: attrStr = "strong"; break;
327131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Weak: attrStr = "weak"; break;
327231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break;
327331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
327431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
3275624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  IdentifierLoc *Arg = new (S.Context) IdentifierLoc;
3276624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Arg->Ident = &S.Context.Idents.get(attrStr);
3277624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Arg->Loc = SourceLocation();
3278624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
3279624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  ArgsUnion Args(Arg);
3280624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
328131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // If there wasn't one, add one (with an invalid source location
328231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // so that we don't make an AttributedType for it).
328331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  AttributeList *attr = D.getAttributePool()
328431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    .create(&S.Context.Idents.get("objc_ownership"), SourceLocation(),
328531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            /*scope*/ 0, SourceLocation(),
3286624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman            /*args*/ &Args, 1, AttributeList::AS_GNU);
328731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  spliceAttrIntoList(*attr, chunk.getAttrListRef());
328831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
328931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // TODO: mark whether we did this inference?
329031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
329131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
329248d798ce32447607144db70a484cdb99c1180663Benjamin Kramer/// \brief Used for transferring ownership in casts resulting in l-values.
329331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnership(TypeProcessingState &state,
329431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 QualType &declSpecTy,
329531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 Qualifiers::ObjCLifetime ownership) {
329631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
329731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
329831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
329931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  int inner = -1;
33006ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis  bool hasIndirection = false;
330131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
330231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    DeclaratorChunk &chunk = D.getTypeObject(i);
330331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    switch (chunk.Kind) {
330431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Paren:
330531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      // Ignore parens.
330631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
330731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
330831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Array:
330931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Reference:
331031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Pointer:
33116ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
33126ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        hasIndirection = true;
331331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      inner = i;
331431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
331531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
331631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::BlockPointer:
33176ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
33186ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        transferARCOwnershipToDeclaratorChunk(state, ownership, i);
33196ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      return;
332031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
332131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Function:
332231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::MemberPointer:
332331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
332431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    }
332531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
332631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
332731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (inner == -1)
33286ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    return;
332931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
333091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  DeclaratorChunk &chunk = D.getTypeObject(inner);
333131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (chunk.Kind == DeclaratorChunk::Pointer) {
333231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (declSpecTy->isObjCRetainableType())
333331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
33346ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    if (declSpecTy->isObjCObjectType() && hasIndirection)
333531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclaratorChunk(state, ownership, inner);
333631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  } else {
333731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    assert(chunk.Kind == DeclaratorChunk::Array ||
333831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis           chunk.Kind == DeclaratorChunk::Reference);
333931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
334031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
334131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
334231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
334331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) {
334431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeProcessingState state(*this, D);
334531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
334631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
334731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
334831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy.isNull())
334931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
335031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
33514e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount) {
335231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy);
335331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (ownership != Qualifiers::OCL_None)
335431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      transferARCOwnership(state, declSpecTy, ownership);
335531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
335631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
335731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo);
335831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
335931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
336014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall/// Map an AttributedType::Kind to an AttributeList::Kind.
336114aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
336214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  switch (kind) {
336314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_address_space:
33648e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_AddressSpace;
336514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_regparm:
33668e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Regparm;
336714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_vector_size:
33688e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_VectorSize;
336914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_vector_type:
33708e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonVectorType;
337114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_polyvector_type:
33728e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonPolyVectorType;
337314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_objc_gc:
33748e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCGC;
3375b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  case AttributedType::attr_objc_ownership:
33768e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCOwnership;
337714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_noreturn:
33788e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NoReturn;
337914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_cdecl:
33808e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_CDecl;
338114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_fastcall:
33828e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_FastCall;
338314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_stdcall:
33848e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_StdCall;
338514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_thiscall:
33868e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ThisCall;
338714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_pascal:
33888e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pascal;
3389414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case AttributedType::attr_pcs:
33905b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributedType::attr_pcs_vfp:
33918e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pcs;
3392263366f9241366f29ba65b703120f302490c39ffDerek Schuff  case AttributedType::attr_pnaclcall:
3393263366f9241366f29ba65b703120f302490c39ffDerek Schuff    return AttributeList::AT_PnaclCall;
339438980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei  case AttributedType::attr_inteloclbicc:
339538980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    return AttributeList::AT_IntelOclBicc;
3396e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  case AttributedType::attr_ms_abi:
3397e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    return AttributeList::AT_MSABI;
3398e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  case AttributedType::attr_sysv_abi:
3399e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    return AttributeList::AT_SysVABI;
3400aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_ptr32:
3401aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_Ptr32;
3402aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_ptr64:
3403aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_Ptr64;
3404aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_sptr:
3405aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_SPtr;
3406aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_uptr:
3407aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_UPtr;
340814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
340914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  llvm_unreachable("unexpected attribute kind!");
341014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
341114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
341214aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic void fillAttributedTypeLoc(AttributedTypeLoc TL,
341314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                  const AttributeList *attrs) {
341414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributedType::Kind kind = TL.getAttrKind();
341514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
341614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  assert(attrs && "no type attributes in the expected location!");
341714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributeList::Kind parsedKind = getAttrListKind(kind);
341814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  while (attrs->getKind() != parsedKind) {
341914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    attrs = attrs->getNext();
342014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    assert(attrs && "no matching attribute in expected location!");
342114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
342214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
342314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  TL.setAttrNameLoc(attrs->getLoc());
3424624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (TL.hasAttrExprOperand() && attrs->isArgExpr(0))
3425624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    TL.setAttrExprOperand(attrs->getArgAsExpr(0));
3426624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (TL.hasAttrEnumOperand() && attrs->isArgIdent(0))
3427624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    TL.setAttrEnumOperandLoc(attrs->getArgAsIdent(0)->Loc);
342814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
342914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // FIXME: preserve this information to here.
343014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrOperand())
343114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrOperandParensRange(SourceRange());
343214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
343314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
343451bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
343551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
3436c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    ASTContext &Context;
343751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
3438f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
343951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
344091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
3441c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      : Context(Context), DS(DS) {}
3442f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
344314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
344414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, DS.getAttributes().getList());
344514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      Visit(TL.getModifiedLoc());
344614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
344751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
344851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
344951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
345051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
345151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
345251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
345351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
345451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
34551de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // FIXME. We should have DS.getTypeSpecTypeEndLoc(). But, it requires
34561de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // addition field. What we have is good enough for dispay of location
34571de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // of 'fixit' on interface name.
34581de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      TL.setNameEndLoc(DS.getLocEnd());
3459c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
3460c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3461c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
3462c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
3463c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
3464c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.getBaseLoc().initialize(Context, SourceLocation());
3465c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
3466c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
3467c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
3468c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
346954e14c4db764c0636160d26c5bbf491637c83a76John McCall
3470c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
347154e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
347254e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
347354e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
347454e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
347554e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
347654e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
347754e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
347854e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
347954e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
348054e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
348154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
348254e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
348351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
348454e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
348554e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
3486c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
348751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
3488833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
3489a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
3490b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3491833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3492833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
3493833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
3494a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
34950daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara        TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
3496833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
3497833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
3498833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3499e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
3500e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
350139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        ElaboratedTypeLoc ElabTL = OldTL.castAs<ElaboratedTypeLoc>();
350239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        TemplateSpecializationTypeLoc NamedTL = ElabTL.getNamedTypeLoc()
350339e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            .castAs<TemplateSpecializationTypeLoc>();
3504e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
350544ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      } else {
350639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        TL.copy(OldTL.castAs<TemplateSpecializationTypeLoc>());
350744ee0a710c59d8e6793189f903bae21c16814324Eli Friedman        assert(TL.getRAngleLoc() == OldTL.castAs<TemplateSpecializationTypeLoc>().getRAngleLoc());
350844ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      }
350944ee0a710c59d8e6793189f903bae21c16814324Eli Friedman
3510833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
3511cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3512cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
3513cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3514cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3515cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3516cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3517cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
3518cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3519cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3520b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
3521cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
3522b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3523cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
3524cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3525ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3526ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      // FIXME: This holds only because we only have one unary transform.
3527ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType);
3528ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setKWLoc(DS.getTypeSpecTypeLoc());
3529ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setParensRange(DS.getTypeofParensRange());
3530ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getRepAsType());
3531ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TypeSourceInfo *TInfo = 0;
3532ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3533ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setUnderlyingTInfo(TInfo);
3534ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
3535ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3536ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
3537ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
3538ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
3539ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
3540ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
3541ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
3542ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
3543ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
3544ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
3545ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
3546ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
3547ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
3548ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
3549ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
3550e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3551e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
3552e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
3553253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
3554e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
3555b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3556e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
355739e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          TL.copy(TInfo->getTypeLoc().castAs<ElaboratedTypeLoc>());
3558e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
3559e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
3560e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
356138a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara      TL.setElaboratedKeywordLoc(Keyword != ETK_None
356238a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 ? DS.getTypeSpecTypeLoc()
356338a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 : SourceLocation());
3564e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
35659e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
3566e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
3567e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3568e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
356966581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
357066581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
357166581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
357266581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
357339e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      TL.copy(TInfo->getTypeLoc().castAs<DependentNameTypeLoc>());
357433500955d731c73717af52088b7fc0e7a85681e7John McCall    }
357533500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
357633500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
357766581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
357866581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
357966581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
358066581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
358139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      TL.copy(
358239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          TInfo->getTypeLoc().castAs<DependentTemplateSpecializationTypeLoc>());
35830daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    }
35840daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    void VisitTagTypeLoc(TagTypeLoc TL) {
35850daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
3586e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3587b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
35884cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // An AtomicTypeLoc can come from either an _Atomic(...) type specifier
35894cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // or an _Atomic qualifier.
35904cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      if (DS.getTypeSpecType() == DeclSpec::TST_atomic) {
35914cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setKWLoc(DS.getTypeSpecTypeLoc());
35924cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setParensRange(DS.getTypeofParensRange());
359391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
35944cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TypeSourceInfo *TInfo = 0;
35954cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
35964cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        assert(TInfo);
35974cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
35984cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      } else {
35994cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setKWLoc(DS.getAtomicSpecLoc());
36004cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // No parens, to indicate this was spelled as an _Atomic qualifier.
36014cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setParensRange(SourceRange());
36024cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        Visit(TL.getValueLoc());
36034cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      }
3604b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
3605e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
360651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
360751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
3608c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initialize(Context, DS.getTypeSpecTypeLoc());
360951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
361051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
3611eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
361251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
3613b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    ASTContext &Context;
361451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
3615f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
361651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
3617b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
3618b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      : Context(Context), Chunk(Chunk) {}
36194adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
362051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
36219f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
362251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
362312df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner    void VisitDecayedTypeLoc(DecayedTypeLoc TL) {
362412df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner      llvm_unreachable("decayed type locs not expected here!");
362512df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner    }
36264adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3627f85e193739c953358c865005855253af4f68a497John McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3628f85e193739c953358c865005855253af4f68a497John McCall      fillAttributedTypeLoc(TL, Chunk.getAttrs());
3629f85e193739c953358c865005855253af4f68a497John McCall    }
363051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
363151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
363251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
36334adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
363451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
363551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
363651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
36374adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
363851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
363951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
364051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
36414adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
364251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
364351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
3644b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const CXXScopeSpec& SS = Chunk.Mem.Scope();
3645b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
3646b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3647b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const Type* ClsTy = TL.getClass();
3648b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      QualType ClsQT = QualType(ClsTy, 0);
3649b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
3650b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Now copy source location info into the type loc component.
3651b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeLoc ClsTL = ClsTInfo->getTypeLoc();
3652b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
3653b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Identifier:
3654b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
3655b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        {
365639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          DependentNameTypeLoc DNTLoc = ClsTL.castAs<DependentNameTypeLoc>();
365738a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          DNTLoc.setElaboratedKeywordLoc(SourceLocation());
3658b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
3659b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
3660b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3661b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3662b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3663b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpec:
3664b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpecWithTemplate:
3665b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        if (isa<ElaboratedType>(ClsTy)) {
366639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          ElaboratedTypeLoc ETLoc = ClsTL.castAs<ElaboratedTypeLoc>();
366738a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          ETLoc.setElaboratedKeywordLoc(SourceLocation());
3668b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ETLoc.setQualifierLoc(NNSLoc.getPrefix());
3669b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
3670b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
3671b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        } else {
3672b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
3673b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3674b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3675b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3676b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Namespace:
3677b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::NamespaceAlias:
3678b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Global:
3679b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        llvm_unreachable("Nested-name-specifier must name a type");
3680b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      }
3681b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3682b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Finally fill in MemberPointerLocInfo fields.
368351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
3684b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TL.setClassTInfo(ClsTInfo);
36854adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
368651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
368751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
368854e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
368954e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
369051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
369151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
369251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
369351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
369451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
369551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
369651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
369751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
369851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
369951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
370051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
370151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
370251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
370351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
370451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
3705796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeBegin(Chunk.Loc);
3706796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeEnd(Chunk.EndLoc);
370751bd803fbdade51d674598ed45da3d54190a656cJohn McCall
370851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
370959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setLParenLoc(FTI.getLParenLoc());
371059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setRParenLoc(FTI.getRParenLoc());
371154e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
3712d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
371354e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
37144adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
371551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
37164adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
3717075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
3718075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
3719075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
3720075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
3721075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
37221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
372351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
37249f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
37254adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
372651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
372751bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
37284adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
37294cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smithstatic void fillAtomicQualLoc(AtomicTypeLoc ATL, const DeclaratorChunk &Chunk) {
37304cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  SourceLocation Loc;
37314cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  switch (Chunk.Kind) {
37324cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Function:
37334cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Array:
37344cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Paren:
37354cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    llvm_unreachable("cannot be _Atomic qualified");
37364cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
37374cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Pointer:
37384cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    Loc = SourceLocation::getFromRawEncoding(Chunk.Ptr.AtomicQualLoc);
37394cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    break;
37404cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
37414cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::BlockPointer:
37424cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Reference:
37434cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::MemberPointer:
37444cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // FIXME: Provide a source location for the _Atomic keyword.
37454cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    break;
37464cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
37474cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
37484cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  ATL.setKWLoc(Loc);
37494cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  ATL.setParensRange(SourceRange());
37504cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith}
37514cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
3752a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
375351bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
375451bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
375505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
375605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
375705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
375805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
375905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
3760a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
376105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
376205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
3763a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
3764a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
376551bd803fbdade51d674598ed45da3d54190a656cJohn McCall
3766a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
3767a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
376839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    CurrTL.castAs<PackExpansionTypeLoc>().setEllipsisLoc(D.getEllipsisLoc());
376991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
3770a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
377191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
37728ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
37734cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // An AtomicTypeLoc might be produced by an atomic qualifier in this
37744cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // declarator chunk.
37754cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (AtomicTypeLoc ATL = CurrTL.getAs<AtomicTypeLoc>()) {
37764cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      fillAtomicQualLoc(ATL, D.getTypeObject(i));
37774cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      CurrTL = ATL.getValueLoc().getUnqualifiedLoc();
37784cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    }
37794cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
378039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    while (AttributedTypeLoc TL = CurrTL.getAs<AttributedTypeLoc>()) {
378114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs());
378214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
378314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
378414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3785b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
378651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
37874adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
378891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3789b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
3790b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
3791b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
379205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
379305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
379405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
3795b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
3796c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
379705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
379891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3799a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
38004adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
38014adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3802a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
3803b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
38041bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
38051bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
38061bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
380791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
3808eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
3809a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
38101bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
38111bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
3812b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
38131bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
38141bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
38151bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
38161bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
3817b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*"
381835d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
381935d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
38201bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
38211bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
3822f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
38235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
38245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
38255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
38261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3827d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
3828bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
38295153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
3830809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
38315912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
3832e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall  // Make sure there are no unused decl attributes on the declarator.
3833cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // We don't want to do this for ObjC parameters because we're going
3834cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // to apply them to the actual parameter declaration.
38356b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  // Likewise, we don't want to do this for alias declarations, because
38366b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  // we are actually going to build a declaration from this eventually.
38376b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  if (D.getContext() != Declarator::ObjCParameterContext &&
38386b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith      D.getContext() != Declarator::AliasDeclContext &&
38396b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith      D.getContext() != Declarator::AliasTemplateContext)
3840cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    checkUnusedDeclAttributes(D);
3841e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
38424e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
3843402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
38446d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
3845402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
3846402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
3847b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
38485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
38495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3850e97179c675b341927807c718be215c8d1aab8acbDouglas GregorParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) {
3851e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  QualType T = Context.getObjCInstanceType();
3852e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc);
3853e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return CreateParsedType(T, TInfo);
3854e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
3855e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3856e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3857c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3858c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
3859c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3860232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3861232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
3862c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
3863c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
38641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
3865c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
38660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
3867232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
386829e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified by
386929e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // qualifiers for two or more different address spaces."
3870232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
3871c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
3872e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3873c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3874232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
38751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3876020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be
3877020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // qualified by an address-space qualifier."
3878020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  if (Type->isFunctionType()) {
3879020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type);
3880020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    Attr.setInvalid();
3881020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    return;
3882020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  }
3883020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne
3884232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
3885545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
3886baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3887baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
3888e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3889c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3890232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3891624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3892232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
3893ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
3894ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
38959f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
38969f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
38979f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << ASArgExpr->getSourceRange();
3898e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3899c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3900232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3901232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3902efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
3903efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
3904efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
3905efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
3906efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
3907e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
3908efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
3909efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
3910efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
3911efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3912efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
39130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
3914efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
3915efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
39167348454025693dd20a411c2bcaabd4460cb87559Joerg Sonnenberger      << int(Qualifiers::MaxAddressSpace) << ASArgExpr->getSourceRange();
3917e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3918efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
3919efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3920efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
39211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
3922f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
3923c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
3924c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
3925d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// Does this type have a "direct" ownership qualifier?  That is,
3926d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// is it written like "__strong id", as opposed to something like
3927d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// "typeof(foo)", where that happens to be strong?
3928d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCallstatic bool hasDirectOwnershipQualifier(QualType type) {
3929d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Fast path: no qualifier at all.
3930d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  assert(type.getQualifiers().hasObjCLifetime());
3931d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3932d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  while (true) {
3933d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // __strong id
3934d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (const AttributedType *attr = dyn_cast<AttributedType>(type)) {
3935d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      if (attr->getAttrKind() == AttributedType::attr_objc_ownership)
3936d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        return true;
3937d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3938d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = attr->getModifiedType();
3939d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3940d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // X *__strong (...)
3941d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else if (const ParenType *paren = dyn_cast<ParenType>(type)) {
3942d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = paren->getInnerType();
394391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3944d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // That's it for things we want to complain about.  In particular,
3945d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // we do not want to look through typedefs, typeof(expr),
3946d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // typeof(type), or any other way that the type is somehow
3947d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // abstracted.
3948d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else {
394991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3950d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return false;
3951d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3952d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
3953d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall}
3954d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3955b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis/// handleObjCOwnershipTypeAttr - Process an objc_ownership
3956f85e193739c953358c865005855253af4f68a497John McCall/// attribute on the specified type.
3957f85e193739c953358c865005855253af4f68a497John McCall///
3958f85e193739c953358c865005855253af4f68a497John McCall/// Returns 'true' if the attribute was handled.
3959b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
3960f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr,
3961f85e193739c953358c865005855253af4f68a497John McCall                                       QualType &type) {
3962e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  bool NonObjCPointer = false;
3963e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3964dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith  if (!type->isDependentType() && !type->isUndeducedType()) {
3965e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    if (const PointerType *ptr = type->getAs<PointerType>()) {
3966e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      QualType pointee = ptr->getPointeeType();
3967e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      if (pointee->isObjCRetainableType() || pointee->isPointerType())
3968e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis        return false;
3969e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // It is important not to lose the source info that there was an attribute
3970e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // applied to non-objc pointer. We will create an attributed type but
3971e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // its type will be the same as the original type.
3972e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      NonObjCPointer = true;
3973e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    } else if (!type->isObjCRetainableType()) {
3974e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      return false;
3975e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3976b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
3977b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // Don't accept an ownership attribute in the declspec if it would
3978b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // just be the return type of a block pointer.
3979b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    if (state.isProcessingDeclSpec()) {
3980b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      Declarator &D = state.getDeclarator();
3981b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (maybeMovePastReturnType(D, D.getNumTypeObjects()))
3982b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        return false;
3983b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
3984e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3985f85e193739c953358c865005855253af4f68a497John McCall
3986f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
3987440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  SourceLocation AttrLoc = attr.getLoc();
3988440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isMacroID())
3989440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    AttrLoc = S.getSourceManager().getImmediateExpansionRange(AttrLoc).first;
3990f85e193739c953358c865005855253af4f68a497John McCall
3991624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (!attr.isArgIdent(0)) {
39923cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman    S.Diag(AttrLoc, diag::err_attribute_argument_type)
39933cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman      << attr.getName() << AANT_ArgumentString;
3994f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3995f85e193739c953358c865005855253af4f68a497John McCall    return true;
3996f85e193739c953358c865005855253af4f68a497John McCall  }
3997f85e193739c953358c865005855253af4f68a497John McCall
3998d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Consume lifetime attributes without further comment outside of
3999d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // ARC mode.
40004e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!S.getLangOpts().ObjCAutoRefCount)
4001d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    return true;
4002d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
4003624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  IdentifierInfo *II = attr.getArgAsIdent(0)->Ident;
4004f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime lifetime;
4005624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (II->isStr("none"))
4006f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_ExplicitNone;
4007624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (II->isStr("strong"))
4008f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Strong;
4009624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (II->isStr("weak"))
4010f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Weak;
4011624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (II->isStr("autoreleasing"))
4012f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Autoreleasing;
4013f85e193739c953358c865005855253af4f68a497John McCall  else {
4014440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_attribute_type_not_supported)
4015d068607c136298bec0891d750389a55bac9f5c98Aaron Ballman      << attr.getName() << II;
4016f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
4017f85e193739c953358c865005855253af4f68a497John McCall    return true;
4018f85e193739c953358c865005855253af4f68a497John McCall  }
4019f85e193739c953358c865005855253af4f68a497John McCall
4020d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  SplitQualType underlyingType = type.split();
4021d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
4022d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Check for redundant/conflicting ownership qualifiers.
4023d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  if (Qualifiers::ObjCLifetime previousLifetime
4024d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        = type.getQualifiers().getObjCLifetime()) {
4025d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // If it's written directly, that's an error.
4026d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (hasDirectOwnershipQualifier(type)) {
4027d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant)
4028d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        << type;
4029d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return true;
4030d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
4031d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
4032d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // Otherwise, if the qualifiers actually conflict, pull sugar off
4033d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // until we reach a type that is directly qualified.
4034d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (previousLifetime != lifetime) {
4035d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // This should always terminate: the canonical type is
4036d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // qualified, so some bit of sugar must be hiding it.
4037d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      while (!underlyingType.Quals.hasObjCLifetime()) {
4038d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        underlyingType = underlyingType.getSingleStepDesugaredType();
4039d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      }
4040d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      underlyingType.Quals.removeObjCLifetime();
4041d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
4042d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
4043d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
4044d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  underlyingType.Quals.addObjCLifetime(lifetime);
4045f85e193739c953358c865005855253af4f68a497John McCall
4046e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (NonObjCPointer) {
4047e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    StringRef name = attr.getName()->getName();
4048e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    switch (lifetime) {
4049e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_None:
4050e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_ExplicitNone:
4051e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      break;
4052e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Strong: name = "__strong"; break;
4053e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Weak: name = "__weak"; break;
4054e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break;
4055e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
4056fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman    S.Diag(AttrLoc, diag::warn_type_attribute_wrong_type) << name
4057fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman      << TDS_ObjCObjOrBlock << type;
4058e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
4059e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
4060f85e193739c953358c865005855253af4f68a497John McCall  QualType origType = type;
4061e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!NonObjCPointer)
4062d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    type = S.Context.getQualifiedType(underlyingType);
4063f85e193739c953358c865005855253af4f68a497John McCall
4064f85e193739c953358c865005855253af4f68a497John McCall  // If we have a valid source location for the attribute, use an
4065f85e193739c953358c865005855253af4f68a497John McCall  // AttributedType instead.
4066440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isValid())
4067b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis    type = S.Context.getAttributedType(AttributedType::attr_objc_ownership,
4068f85e193739c953358c865005855253af4f68a497John McCall                                       origType, type);
4069f85e193739c953358c865005855253af4f68a497John McCall
40709f084a3166b684573ba49df28fc5792bc37d92e1John McCall  // Forbid __weak if the runtime doesn't support it.
4071f85e193739c953358c865005855253af4f68a497John McCall  if (lifetime == Qualifiers::OCL_Weak &&
40720a7dd788dbef975f35f273c7ab913f480f7edd60John McCall      !S.getLangOpts().ObjCARCWeak && !NonObjCPointer) {
4073f85e193739c953358c865005855253af4f68a497John McCall
4074f85e193739c953358c865005855253af4f68a497John McCall    // Actually, delay this until we know what we're parsing.
4075f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
4076f85e193739c953358c865005855253af4f68a497John McCall      S.DelayedDiagnostics.add(
4077440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis          sema::DelayedDiagnostic::makeForbiddenType(
4078440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis              S.getSourceManager().getExpansionLoc(AttrLoc),
4079f85e193739c953358c865005855253af4f68a497John McCall              diag::err_arc_weak_no_runtime, type, /*ignored*/ 0));
4080f85e193739c953358c865005855253af4f68a497John McCall    } else {
4081440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis      S.Diag(AttrLoc, diag::err_arc_weak_no_runtime);
4082f85e193739c953358c865005855253af4f68a497John McCall    }
4083f85e193739c953358c865005855253af4f68a497John McCall
4084f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
4085f85e193739c953358c865005855253af4f68a497John McCall    return true;
4086f85e193739c953358c865005855253af4f68a497John McCall  }
408791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
408891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // Forbid __weak for class objects marked as
4089742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  // objc_arc_weak_reference_unavailable
4090742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  if (lifetime == Qualifiers::OCL_Weak) {
4091b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    if (const ObjCObjectPointerType *ObjT =
4092b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          type->getAs<ObjCObjectPointerType>()) {
40934e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith      if (ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl()) {
40944e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        if (Class->isArcWeakrefUnavailable()) {
40954e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class);
40964e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(ObjT->getInterfaceDecl()->getLocation(),
40974e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith                   diag::note_class_declared);
40984e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        }
4099742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      }
4100742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    }
4101742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  }
410291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4103f85e193739c953358c865005855253af4f68a497John McCall  return true;
4104f85e193739c953358c865005855253af4f68a497John McCall}
4105f85e193739c953358c865005855253af4f68a497John McCall
4106711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
4107711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
4108711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
4109711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
4110711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
4111711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
4112711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
4113711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
4114711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4115711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
4116711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
4117711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
4118711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
4119711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
4120711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4121711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
4122711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
4123711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4125d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
4126624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
4127d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
4128624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (!attr.isArgIdent(0)) {
41293cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman    S.Diag(attr.getLoc(), diag::err_attribute_argument_type)
41303cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman      << attr.getName() << AANT_ArgumentString;
4131711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4132711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4133ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
41340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
4135624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (attr.getNumArgs() > 1) {
4136baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4137baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << attr.getName() << 1;
4138711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4139711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4140d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
4141624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
4142624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  IdentifierInfo *II = attr.getArgAsIdent(0)->Ident;
4143624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (II->isStr("weak"))
41440953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
4145624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (II->isStr("strong"))
41460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
4147d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
4148711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
4149d068607c136298bec0891d750389a55bac9f5c98Aaron Ballman      << attr.getName() << II;
4150711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4151711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4152d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
41531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
415414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  QualType origType = type;
415514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  type = S.Context.getObjCGCQualType(origType, GCAttr);
415614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
415714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // Make an attributed type to preserve the source information.
415814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (attr.getLoc().isValid())
415914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
416014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                       origType, type);
416114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
4162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
4163d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
4164d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
4165e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallnamespace {
4166e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// A helper class to unwrap a type down to a function for the
4167e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// purposes of applying attributes there.
4168e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///
4169e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// Use:
4170e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
4171e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   if (unwrapped.isFunctionType()) {
4172e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     const FunctionType *fn = unwrapped.get();
4173e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     // change fn somehow
4174e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     T = unwrapped.wrap(fn);
4175e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   }
4176e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  struct FunctionTypeUnwrapper {
4177e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    enum WrapKind {
4178e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Desugar,
4179e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Parens,
4180e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Pointer,
4181e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      BlockPointer,
4182e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Reference,
4183e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      MemberPointer
4184e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    };
4185e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4186e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType Original;
4187e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *Fn;
41885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<unsigned char /*WrapKind*/, 8> Stack;
4189e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4190e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
4191e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      while (true) {
4192e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const Type *Ty = T.getTypePtr();
4193e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<FunctionType>(Ty)) {
4194e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Fn = cast<FunctionType>(Ty);
4195e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return;
4196e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ParenType>(Ty)) {
4197e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ParenType>(Ty)->getInnerType();
4198e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Parens);
4199e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<PointerType>(Ty)) {
4200e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<PointerType>(Ty)->getPointeeType();
4201e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Pointer);
4202e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<BlockPointerType>(Ty)) {
4203e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<BlockPointerType>(Ty)->getPointeeType();
4204e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(BlockPointer);
4205e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<MemberPointerType>(Ty)) {
4206e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<MemberPointerType>(Ty)->getPointeeType();
4207e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(MemberPointer);
4208e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ReferenceType>(Ty)) {
4209e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ReferenceType>(Ty)->getPointeeType();
4210e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Reference);
4211e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else {
4212e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          const Type *DTy = Ty->getUnqualifiedDesugaredType();
4213e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          if (Ty == DTy) {
4214e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            Fn = 0;
4215e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            return;
4216e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          }
4217e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4218e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = QualType(DTy, 0);
4219e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Desugar);
4220e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        }
4221e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4222e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4223e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4224e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    bool isFunctionType() const { return (Fn != 0); }
4225e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *get() const { return Fn; }
4226e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4227e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(Sema &S, const FunctionType *New) {
4228e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // If T wasn't modified from the unwrapped type, do nothing.
4229e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (New == get()) return Original;
4230e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4231e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Fn = New;
4232e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return wrap(S.Context, Original, 0);
4233e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4234e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4235e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  private:
4236e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
4237e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size())
4238e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getQualifiedType(Fn, Old.getQualifiers());
4239e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4240e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // Build up the inner type, applying the qualifiers from the old
4241e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // type to the new type.
4242e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      SplitQualType SplitOld = Old.split();
4243e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4244e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // As a special case, tail-recurse if there are no qualifiers.
4245200fa53fd420aa8369586f569dbece04930ad6a3John McCall      if (SplitOld.Quals.empty())
4246200fa53fd420aa8369586f569dbece04930ad6a3John McCall        return wrap(C, SplitOld.Ty, I);
4247200fa53fd420aa8369586f569dbece04930ad6a3John McCall      return C.getQualifiedType(wrap(C, SplitOld.Ty, I), SplitOld.Quals);
4248e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4249e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4250e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
4251e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size()) return QualType(Fn, 0);
4252e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4253e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      switch (static_cast<WrapKind>(Stack[I++])) {
4254e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Desugar:
4255e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // This is the point at which we potentially lose source
4256e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // information.
4257e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
4258e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4259e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Parens: {
4260e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
4261e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getParenType(New);
4262e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4263e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4264e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Pointer: {
4265e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
4266e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getPointerType(New);
4267e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4268e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4269e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case BlockPointer: {
4270e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
4271e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getBlockPointerType(New);
4272e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4273e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4274e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case MemberPointer: {
4275e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
4276e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldMPT->getPointeeType(), I);
4277e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getMemberPointerType(New, OldMPT->getClass());
4278e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4279e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4280e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Reference: {
4281e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const ReferenceType *OldRef = cast<ReferenceType>(Old);
4282e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldRef->getPointeeType(), I);
4283e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<LValueReferenceType>(OldRef))
4284e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
4285e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        else
4286e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getRValueReferenceType(New);
4287e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4288e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4289e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4290e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      llvm_unreachable("unknown wrapping kind");
4291e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4292e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  };
4293e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
4294e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4295aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballmanstatic bool handleMSPointerTypeQualifierAttr(TypeProcessingState &State,
4296aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             AttributeList &Attr,
4297aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             QualType &Type) {
4298aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  Sema &S = State.getSema();
4299aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4300aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  AttributeList::Kind Kind = Attr.getKind();
4301aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  QualType Desugared = Type;
4302aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  const AttributedType *AT = dyn_cast<AttributedType>(Type);
4303aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  while (AT) {
4304aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    AttributedType::Kind CurAttrKind = AT->getAttrKind();
4305aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4306aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // You cannot specify duplicate type attributes, so if the attribute has
4307aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // already been applied, flag it.
4308aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    if (getAttrListKind(CurAttrKind) == Kind) {
4309aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute_exact)
4310aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        << Attr.getName();
4311aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      return true;
4312aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    }
4313aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4314aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // You cannot have both __sptr and __uptr on the same type, nor can you
4315aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // have __ptr32 and __ptr64.
4316aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    if ((CurAttrKind == AttributedType::attr_ptr32 &&
4317aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman         Kind == AttributeList::AT_Ptr64) ||
4318aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        (CurAttrKind == AttributedType::attr_ptr64 &&
4319aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman         Kind == AttributeList::AT_Ptr32)) {
4320aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
4321aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        << "'__ptr32'" << "'__ptr64'";
4322aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      return true;
4323aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    } else if ((CurAttrKind == AttributedType::attr_sptr &&
4324aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                Kind == AttributeList::AT_UPtr) ||
4325aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman               (CurAttrKind == AttributedType::attr_uptr &&
4326aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                Kind == AttributeList::AT_SPtr)) {
4327aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
4328aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        << "'__sptr'" << "'__uptr'";
4329aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      return true;
4330aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    }
4331aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4332aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    Desugared = AT->getEquivalentType();
4333aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    AT = dyn_cast<AttributedType>(Desugared);
4334aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  }
4335aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4336aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  // Pointer type qualifiers can only operate on pointer types, but not
4337aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  // pointer-to-member types.
4338aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  if (!isa<PointerType>(Desugared)) {
4339aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    S.Diag(Attr.getLoc(), Type->isMemberPointerType() ?
4340aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                          diag::err_attribute_no_member_pointers :
4341aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                          diag::err_attribute_pointers_only) << Attr.getName();
4342aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return true;
4343aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  }
4344aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4345aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  AttributedType::Kind TAK;
4346aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  switch (Kind) {
4347aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  default: llvm_unreachable("Unknown attribute kind");
4348aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_Ptr32: TAK = AttributedType::attr_ptr32; break;
4349aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_Ptr64: TAK = AttributedType::attr_ptr64; break;
4350aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_SPtr: TAK = AttributedType::attr_sptr; break;
4351aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_UPtr: TAK = AttributedType::attr_uptr; break;
4352aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  }
4353aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4354aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  Type = S.Context.getAttributedType(TAK, Type, Type);
4355aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  return false;
4356aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman}
4357aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
43585b92696c8f1f8ef943ad87397b95c031b5787305Reid Klecknerstatic AttributedType::Kind getCCTypeAttrKind(AttributeList &Attr) {
43595b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  assert(!Attr.isInvalid());
43605b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  switch (Attr.getKind()) {
43615b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  default:
43625b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    llvm_unreachable("not a calling convention attribute");
43635b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_CDecl:
43645b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_cdecl;
43655b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_FastCall:
43665b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_fastcall;
43675b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_StdCall:
43685b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_stdcall;
43695b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_ThisCall:
43705b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_thiscall;
43715b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_Pascal:
43725b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_pascal;
43735b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_Pcs: {
4374fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    // The attribute may have had a fixit applied where we treated an
4375fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    // identifier as a string literal.  The contents of the string are valid,
4376fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    // but the form may not be.
4377fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    StringRef Str;
4378fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    if (Attr.isArgExpr(0))
4379fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman      Str = cast<StringLiteral>(Attr.getArgAsExpr(0))->getString();
4380fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    else
4381fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman      Str = Attr.getArgAsIdent(0)->Ident->getName();
4382fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    return llvm::StringSwitch<AttributedType::Kind>(Str)
43835b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner        .Case("aapcs", AttributedType::attr_pcs)
43845b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner        .Case("aapcs-vfp", AttributedType::attr_pcs_vfp);
43855b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  }
43865b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_PnaclCall:
43875b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_pnaclcall;
43885b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_IntelOclBicc:
43895b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_inteloclbicc;
4390e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  case AttributeList::AT_MSABI:
4391e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    return AttributedType::attr_ms_abi;
4392e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  case AttributeList::AT_SysVABI:
4393e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    return AttributedType::attr_sysv_abi;
43945b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  }
43955b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  llvm_unreachable("unexpected attribute kind!");
43965b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner}
43975b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner
4398711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
4399711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
4400711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
4401711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
4402711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
4403711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
4404e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4405711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
44062455636163fdd18581d7fdae816433f886d88213Mike Stump
44078e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NoReturn) {
4408711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
440904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
4410e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4411e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
4412711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
4413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
4414425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
4415425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
4416711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
4417711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4418711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4419711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
4420425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
4421f85e193739c953358c865005855253af4f68a497John McCall  // ns_returns_retained is not always a type attribute, but if we got
4422f85e193739c953358c865005855253af4f68a497John McCall  // here, we're treating it as one right now.
44238e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NSReturnsRetained) {
44244e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(S.getLangOpts().ObjCAutoRefCount &&
4425f85e193739c953358c865005855253af4f68a497John McCall           "ns_returns_retained treated as type attribute in non-ARC");
4426f85e193739c953358c865005855253af4f68a497John McCall    if (attr.getNumArgs()) return true;
4427f85e193739c953358c865005855253af4f68a497John McCall
4428f85e193739c953358c865005855253af4f68a497John McCall    // Delay if this is not a function type.
4429f85e193739c953358c865005855253af4f68a497John McCall    if (!unwrapped.isFunctionType())
4430f85e193739c953358c865005855253af4f68a497John McCall      return false;
4431f85e193739c953358c865005855253af4f68a497John McCall
4432f85e193739c953358c865005855253af4f68a497John McCall    FunctionType::ExtInfo EI
4433f85e193739c953358c865005855253af4f68a497John McCall      = unwrapped.get()->getExtInfo().withProducesResult(true);
4434f85e193739c953358c865005855253af4f68a497John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4435f85e193739c953358c865005855253af4f68a497John McCall    return true;
4436f85e193739c953358c865005855253af4f68a497John McCall  }
4437f85e193739c953358c865005855253af4f68a497John McCall
44388e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_Regparm) {
4439711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
4440711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
4441711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
44421e030eb1194763b42c1752723be23b1515f48981John McCall
4443711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
4444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
4445008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
44461e030eb1194763b42c1752723be23b1515f48981John McCall
4447ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Diagnose regparm with fastcall.
4448ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    const FunctionType *fn = unwrapped.get();
4449ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    CallingConv CC = fn->getCallConv();
4450ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (CC == CC_X86FastCall) {
4451ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
4452ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC)
4453ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm";
4454ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
4455ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
4456ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
4457ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
445891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    FunctionType::ExtInfo EI =
4459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
4460711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4461711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4462425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
4463425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
446482bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  // Delay if the type didn't work out to a function.
446582bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  if (!unwrapped.isFunctionType()) return false;
446682bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman
446704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
4468711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
4469711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
4470711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4471f82b4e85b1219295cad4b5851b035575bc293010John McCall
4472711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
4473711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
4474ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  AttributedType::Kind CCAttrKind = getCCTypeAttrKind(attr);
447504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
4476e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  if (CCOld != CC) {
4477ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // Error out on when there's already an attribute on the type
4478ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // and the CCs don't match.
4479ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    const AttributedType *AT = S.getCallingConvAttributedType(type);
4480ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    if (AT && AT->getAttrKind() != CCAttrKind) {
4481ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
4482ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        << FunctionType::getNameForCallConv(CC)
4483ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        << FunctionType::getNameForCallConv(CCOld);
4484ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      attr.setInvalid();
4485ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      return true;
4486ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    }
448704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
448804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
448904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
449004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
4491711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isa<FunctionNoProtoType>(fn)) {
4492711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_knr)
449304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
4494711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
4495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
449604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
449704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
4498711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
449904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
4500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
450104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
4502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
4503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
450404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
4505ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
4506ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Also diagnose fastcall with regparm.
4507a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    if (fn->getHasRegParm()) {
4508ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
4509ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm"
4510ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC);
4511ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
4512ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
4513ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
451404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
4515f82b4e85b1219295cad4b5851b035575bc293010John McCall
45165b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  // Modify the CC from the wrapped function type, wrap it all back, and then
45175b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  // wrap the whole thing in an AttributedType as written.  The modified type
45185b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  // might have a different CC if we ignored the attribute.
4519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
45205b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  QualType Equivalent =
45215b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner      unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4522ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  type = S.Context.getAttributedType(CCAttrKind, type, Equivalent);
4523711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
4524f82b4e85b1219295cad4b5851b035575bc293010John McCall}
4525f82b4e85b1219295cad4b5851b035575bc293010John McCall
4526ef072033876e295ec5d3402f8730a3ae358ad815Reid Klecknervoid Sema::adjustMemberFunctionCC(QualType &T) {
4527ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  const FunctionType *FT = T->castAs<FunctionType>();
4528ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  bool IsVariadic = (isa<FunctionProtoType>(FT) &&
4529ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner                     cast<FunctionProtoType>(FT)->isVariadic());
4530ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  CallingConv CC = FT->getCallConv();
4531ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  CallingConv DefaultCC =
4532ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      Context.getDefaultCallingConvention(IsVariadic, /*IsCXXMethod=*/false);
4533ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  if (CC != DefaultCC)
4534ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    return;
4535ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
4536ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  // Check if there was an explicit attribute, but only look through parens.
4537ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  // The intent is to look for an attribute on the current declarator, but not
4538ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  // one that came from a typedef.
4539ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  QualType R = T.IgnoreParens();
4540ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  while (const AttributedType *AT = dyn_cast<AttributedType>(R)) {
4541ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    if (AT->isCallingConv())
4542ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      return;
4543ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    R = AT->getModifiedType().IgnoreParens();
4544ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  }
4545ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
4546ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  // FIXME: This loses sugar.  This should probably be fixed with an implicit
4547ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  // AttributedType node that adjusts the convention.
4548ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  CC = Context.getDefaultCallingConvention(IsVariadic, /*IsCXXMethod=*/true);
4549ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  FT = Context.adjustFunctionType(FT, FT->getExtInfo().withCallingConv(CC));
4550ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  FunctionTypeUnwrapper Unwrapped(*this, T);
4551ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  T = Unwrapped.wrap(*this, FT);
4552ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner}
4553ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
4554207f4d8543529221932af82836016a2ef066c917Peter Collingbourne/// Handle OpenCL image access qualifiers: read_only, write_only, read_write
4555207f4d8543529221932af82836016a2ef066c917Peter Collingbournestatic void HandleOpenCLImageAccessAttribute(QualType& CurType,
4556207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const AttributeList &Attr,
4557207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             Sema &S) {
4558207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  // Check the attribute arguments.
4559207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (Attr.getNumArgs() != 1) {
4560baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4561baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
4562207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4563207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4564207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4565624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Expr *sizeExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4566207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  llvm::APSInt arg(32);
4567207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4568207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      !sizeExpr->isIntegerConstantExpr(arg, S.Context)) {
45699f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
45709f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
45719f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << sizeExpr->getSourceRange();
4572207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4573207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4574207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4575207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  unsigned iarg = static_cast<unsigned>(arg.getZExtValue());
4576207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  switch (iarg) {
4577207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_only:
4578207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_write_only:
4579207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_write:
4580207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4581207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4582207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  default:
4583207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4584207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
4585207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << sizeExpr->getSourceRange();
4586207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4587207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4588207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4589207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
4590207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
45916e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
45926e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
45936e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
45946e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
45956e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
45966e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
45976e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
4598788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
4599788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
460056affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
46016e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
4602baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4603baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
4604e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
46056e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
46066e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
4607624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Expr *sizeExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
46086e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
4609ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4610ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
46119f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
46129f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
46139f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << sizeExpr->getSourceRange();
4614e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
46156e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
46166e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
4617b445cb9662067b7c7586815937b07828ede9bb49Aaron Ballman  // The base type must be integer (not Boolean or enumeration) or float, and
4618b445cb9662067b7c7586815937b07828ede9bb49Aaron Ballman  // can't already be a vector.
4619b445cb9662067b7c7586815937b07828ede9bb49Aaron Ballman  if (!CurType->isBuiltinType() || CurType->isBooleanType() ||
46201652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
46216e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
4622e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
46236e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
46246e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
46256e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
46266e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
46276e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
46286e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
46296e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
46306e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
46316e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
46326e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4633e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
46346e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
46356e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
46361652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman  if (VectorType::isVectorSizeTooLarge(vectorSize / typeSize)) {
46371652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    S.Diag(Attr.getLoc(), diag::err_attribute_size_too_large)
46381652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      << sizeExpr->getSourceRange();
46391652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    Attr.setInvalid();
46401652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    return;
46411652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman  }
46426e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
46436e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
46446e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4645e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
46466e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
46476e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
46486e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
46496e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
46506e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
4651788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
4652e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
46536e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
46546e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
46554ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// \brief Process the OpenCL-like ext_vector_type attribute when it occurs on
46564ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// a type.
465791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierstatic void HandleExtVectorTypeAttr(QualType &CurType,
465891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                                    const AttributeList &Attr,
46594ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                    Sema &S) {
4660624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  // check the attribute arguments.
4661624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (Attr.getNumArgs() != 1) {
4662624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4663624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman      << Attr.getName() << 1;
4664624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    return;
4665624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  }
4666624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
46674ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  Expr *sizeExpr;
466891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
46694ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Special case where the argument is a template id.
4670624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (Attr.isArgIdent(0)) {
46714ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CXXScopeSpec SS;
4672e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    SourceLocation TemplateKWLoc;
46734ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    UnqualifiedId id;
4674624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    id.setIdentifier(Attr.getArgAsIdent(0)->Ident, Attr.getLoc());
4675e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
4676e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    ExprResult Size = S.ActOnIdExpression(S.getCurScope(), SS, TemplateKWLoc,
4677e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          id, false, false);
46784ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Size.isInvalid())
46794ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
468091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
46814ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Size.get();
46824ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  } else {
4683624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    sizeExpr = Attr.getArgAsExpr(0);
46844ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  }
468591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
46864ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Create the vector type.
46874ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  QualType T = S.BuildExtVectorType(CurType, sizeExpr, Attr.getLoc());
46884ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (!T.isNull())
46894ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CurType = T;
46904ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor}
46914ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
4692b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northoverstatic bool isPermittedNeonBaseType(QualType &Ty,
4693b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover                                    VectorType::VectorKind VecKind,
4694b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover                                    bool IsAArch64) {
4695b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  const BuiltinType *BTy = Ty->getAs<BuiltinType>();
4696b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  if (!BTy)
4697b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    return false;
4698b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
4699b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  if (VecKind == VectorType::NeonPolyVector) {
4700b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    if (IsAArch64) {
4701b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      // AArch64 polynomial vectors are unsigned
4702b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      return BTy->getKind() == BuiltinType::UChar ||
4703b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover             BTy->getKind() == BuiltinType::UShort;
4704b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    } else {
4705b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      // AArch32 polynomial vector are signed.
4706b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      return BTy->getKind() == BuiltinType::SChar ||
4707b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover             BTy->getKind() == BuiltinType::Short;
4708b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    }
4709b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  }
4710b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
4711b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  // Non-polynomial vector types: the usual suspects are allowed, as well as
4712b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  // float64_t on AArch64.
4713b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  if (IsAArch64 && BTy->getKind() == BuiltinType::Double)
4714b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    return true;
4715b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
4716b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  return BTy->getKind() == BuiltinType::SChar ||
4717b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::UChar ||
4718b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Short ||
4719b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::UShort ||
4720b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Int ||
4721b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::UInt ||
4722b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::LongLong ||
4723b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::ULongLong ||
4724b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Float ||
4725b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Half;
4726b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover}
4727b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
47284211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
47294211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
47304211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
47314211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
47324211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
47334211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
47344211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
47354211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
47364211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
47379f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman                                     VectorType::VectorKind VecKind) {
47384211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
47394211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
4740baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4741baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
47424211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
47434211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
47444211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
47454211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
4746624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
47474211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
47484211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
47494211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
47509f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
47519f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
47529f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << numEltsExpr->getSourceRange();
47534211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
47544211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
47554211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
47564211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
4757e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover  llvm::Triple::ArchType Arch =
4758b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover        S.Context.getTargetInfo().getTriple().getArch();
4759e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover  if (!isPermittedNeonBaseType(CurType, VecKind,
4760e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover                               Arch == llvm::Triple::aarch64)) {
4761e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
4762e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover    Attr.setInvalid();
4763e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover    return;
47644211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
4765e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover
47664211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
47674211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
47684211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
47694211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
47704211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
47714211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
47724211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
47734211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
47744211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
47754211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
47764211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
47774211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
47784211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
4779711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
4780f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             TypeAttrLocation TAL, AttributeList *attrs) {
4781c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
4782c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
4783c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
4784c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
4785711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4786711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
4787711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
4788711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
4789711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr.getNext();
4790711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4791e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
4792711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
4793e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
4794e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
4795cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    if (attr.isCXX11Attribute()) {
4796cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // [[gnu::...]] attributes are treated as declaration attributes, so may
4797cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // not appertain to a DeclaratorChunk, even if we handle them as type
4798cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // attributes.
4799cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.getScopeName() && attr.getScopeName()->isStr("gnu")) {
4800cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        if (TAL == TAL_DeclChunk) {
4801cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          state.getSema().Diag(attr.getLoc(),
4802cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                               diag::warn_cxx11_gnu_attribute_on_type)
4803cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith              << attr.getName();
4804cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          continue;
4805cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        }
4806cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      } else if (TAL != TAL_DeclChunk) {
4807cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // Otherwise, only consider type processing for a C++11 attribute if
4808cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // it's actually been applied to a type.
4809cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        continue;
4810cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      }
4811f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    }
4812f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
4813b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
4814b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
4815711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
4816cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    default:
4817cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // A C++11 attribute on a declarator chunk must appertain to a type.
4818d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk) {
4819cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(), diag::err_attribute_not_type_attr)
4820d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith          << attr.getName();
4821d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith        attr.setUsedAsTypeAttr();
4822d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      }
4823cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
4824cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
4825cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::UnknownAttribute:
4826cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk)
4827cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(),
4828cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                             diag::warn_unknown_attribute_ignored)
4829cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          << attr.getName();
4830cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
4831cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
4832cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::IgnoredAttribute:
4833cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
483404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
48358e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_MayAlias:
4836682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // FIXME: This attribute needs to actually be handled, but if we ignore
4837682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // it it breaks large amounts of Linux software.
4838682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      attr.setUsedAsTypeAttr();
4839682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      break;
48408e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_AddressSpace:
4841711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
4842e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4843c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
4844711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
4845711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
4846711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
4847e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4848d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
48498e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_VectorSize:
4850711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
4851e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4852f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
48538e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ExtVectorType:
4854a4fa9008988985f9cf01712a99ddd923aea278a0Richard Smith      HandleExtVectorTypeAttr(type, attr, state.getSema());
4855e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
48564ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      break;
48578e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonVectorType:
4858711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
48599f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman                               VectorType::NeonVector);
4860e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
48614211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
48628e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonPolyVectorType:
4863711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
48649f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman                               VectorType::NeonPolyVector);
4865e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
48664211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
48678e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_OpenCLImageAccess:
4868207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      HandleOpenCLImageAccessAttribute(type, attr, state.getSema());
4869e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4870207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      break;
4871207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
4872d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith    case AttributeList::AT_Win64:
4873d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      attr.setUsedAsTypeAttr();
4874d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      break;
4875aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    MS_TYPE_ATTRS_CASELIST:
4876aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      if (!handleMSPointerTypeQualifierAttr(state, attr, type))
4877aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        attr.setUsedAsTypeAttr();
4878aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      break;
4879d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith
48808e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
48814e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
4882cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        break;
4883f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough into the function attrs
4884f85e193739c953358c865005855253af4f68a497John McCall
4885711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
4886e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4887e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
4888711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
4889711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
4890f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      if (TAL == TAL_DeclSpec)
4891711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
4892711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4893711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
4894711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
4895711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
48966e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
4897c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
4898711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attrs = next));
4899232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
4900232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
4901e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \brief Ensure that the type of the given expression is complete.
4902e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4903e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// This routine checks whether the expression \p E has a complete type. If the
4904e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// expression refers to an instantiable construct, that instantiation is
4905e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// performed as needed to complete its type. Furthermore
4906e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// Sema::RequireCompleteType is called for the expression's type (or in the
4907e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// case of a reference type, the referred-to type).
4908e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4909e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \param E The expression whose type is required to be complete.
4910d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// \param Diagnoser The object that will emit a diagnostic if the type is
4911d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// incomplete.
4912e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4913e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \returns \c true if the type of \p E is incomplete and diagnosed, \c false
4914e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// otherwise.
4915f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, TypeDiagnoser &Diagnoser){
4916e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  QualType T = E->getType();
4917e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4918e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Fast path the case where the type is already complete.
4919e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (!T->isIncompleteType())
492063f25e6719e3920a04f66a80958b792ac268aeddRichard Smith    // FIXME: The definition might not be visible.
4921e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    return false;
4922e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4923e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Incomplete array types may be completed by the initializer attached to
4924e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // their definitions. For static data members of class templates we need to
4925e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // instantiate the definition to get this initializer and complete the type.
4926e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (T->isIncompleteArrayType()) {
4927e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4928e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
4929e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        if (Var->isStaticDataMember() &&
4930e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth            Var->getInstantiatedFromStaticDataMember()) {
493191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
493236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
493336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          assert(MSInfo && "Missing member specialization information?");
493436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          if (MSInfo->getTemplateSpecializationKind()
493536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                != TSK_ExplicitSpecialization) {
493636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // If we don't already have a point of instantiation, this is it.
493736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (MSInfo->getPointOfInstantiation().isInvalid()) {
493836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              MSInfo->setPointOfInstantiation(E->getLocStart());
493991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
494091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier              // This is a modification of an existing AST node. Notify
494136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              // listeners.
494236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              if (ASTMutationListener *L = getASTMutationListener())
494336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                L->StaticDataMemberInstantiated(Var);
494436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
494591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
494636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            InstantiateStaticDataMemberDefinition(E->getExprLoc(), Var);
494791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
494836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // Update the type to the newly instantiated definition's type both
494936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // here and within the expression.
495036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (VarDecl *Def = Var->getDefinition()) {
495136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setDecl(Def);
495236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              T = Def->getType();
495336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setType(T);
495436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              E->setType(T);
495536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
4956f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor          }
495791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4958e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // We still go on to try to complete the type independently, as it
4959e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // may also require instantiations or diagnostics if it remains
4960e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // incomplete.
4961e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        }
4962e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      }
4963e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    }
4964e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  }
4965e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4966e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // FIXME: Are there other cases which require instantiating something other
4967e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // than the type to complete the type of an expression?
4968e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4969e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Look through reference types and complete the referred type.
4970e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
4971e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    T = Ref->getPointeeType();
4972e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4973d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(E->getExprLoc(), T, Diagnoser);
4974d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4975d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4976d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregornamespace {
4977f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  struct TypeDiagnoserDiag : Sema::TypeDiagnoser {
4978d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    unsigned DiagID;
497991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4980f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor    TypeDiagnoserDiag(unsigned DiagID)
4981f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor      : Sema::TypeDiagnoser(DiagID == 0), DiagID(DiagID) {}
498291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4983d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) {
4984d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      if (Suppressed) return;
4985d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      S.Diag(Loc, DiagID) << T;
4986d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    }
4987d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  };
4988d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4989d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4990d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, unsigned DiagID) {
4991f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4992d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteExprType(E, Diagnoser);
4993e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth}
4994e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
49951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
49964ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
49974ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
49984ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
499986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
500086447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
500186447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
500286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
500386447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
50044ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
50054ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
50064ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
50074ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
50084ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
50094ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
50104ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
50114ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
501291a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
5013f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                               TypeDiagnoser &Diagnoser) {
5014658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  if (RequireCompleteTypeImpl(Loc, T, Diagnoser))
5015658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie    return true;
5016658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  if (const TagType *Tag = T->getAs<TagType>()) {
5017658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie    if (!Tag->getDecl()->isCompleteDefinitionRequired()) {
5018658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie      Tag->getDecl()->setCompleteDefinitionRequired();
5019658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie      Consumer.HandleTagDeclRequiredDefinition(Tag->getDecl());
5020658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie    }
5021658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  }
5022658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  return false;
5023658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie}
5024658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie
5025658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie/// \brief The implementation of RequireCompleteType
5026658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikiebool Sema::RequireCompleteTypeImpl(SourceLocation Loc, QualType T,
5027658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie                                   TypeDiagnoser &Diagnoser) {
5028573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
5029573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
5030690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
5031690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
5032690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
50331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
5034690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
5035690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
50364ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
5037d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  NamedDecl *Def = 0;
5038d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  if (!T->isIncompleteType(&Def)) {
5039d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    // If we know about the definition but it is not visible, complain.
5040b775100fea6d8955149897dae1adca50ca471d17Richard Smith    if (!Diagnoser.Suppressed && Def && !LookupResult::isVisible(*this, Def)) {
5041d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // Suppress this error outside of a SFINAE context if we've already
504291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // emitted the error once for this type. There's no usefulness in
5043d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // repeating the diagnostic.
5044d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // FIXME: Add a Fix-It that imports the corresponding module or includes
5045d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // the header.
5046ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Module *Owner = Def->getOwningModule();
5047ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Loc, diag::err_module_private_definition)
5048ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        << T << Owner->getFullModuleName();
5049ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Def->getLocation(), diag::note_previous_definition);
5050ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
5051ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      if (!isSFINAEContext()) {
5052ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        // Recover by implicitly importing this module.
5053ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        createImplicitModuleImport(Loc, Owner);
5054d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      }
5055d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    }
505691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
50574ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
5058d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
50594ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
5060b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // FIXME: If there's an unimported definition of this type in a module (for
5061b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // instance, because we forward declared it, then imported the definition),
5062b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // import that definition now.
5063b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // FIXME: What about other cases where an import extends a redeclaration
5064b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // chain for a declaration that can be accessed through a mechanism other
5065b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // than name lookup (eg, referenced in a template, or a variable whose type
5066b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // could be completed by the module)?
5067b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith
5068bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const TagType *Tag = T->getAs<TagType>();
5069bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const ObjCInterfaceType *IFace = 0;
507091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5071bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  if (Tag) {
5072bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
5073bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->isInvalidDecl())
5074bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
5075bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan
5076bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
5077bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->hasExternalLexicalStorage()) {
5078bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(Tag->getDecl());
5079bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!Tag->isIncompleteType())
5080bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
5081bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
5082bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
5083bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  else if ((IFace = T->getAs<ObjCInterfaceType>())) {
5084bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
5085bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->isInvalidDecl())
5086bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
508791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5088bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
5089bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->hasExternalLexicalStorage()) {
5090bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(IFace->getDecl());
5091bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!IFace->isIncompleteType())
5092bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
5093bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
5094bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
509591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5096d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
5097923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
5098923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
5099923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
5100e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor  while (const ConstantArrayType *Array
5101e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor           = Context.getAsConstantArrayType(MaybeTemplate))
5102923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
5103923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
51042943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
5105d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
5106972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
5107972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
5108d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
5109d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                            /*Complain=*/!Diagnoser.Suppressed);
51101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
5111d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
5112564f4c5664f552becbd05407611a92754c40e641Richard Smith      CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass();
5113564f4c5664f552becbd05407611a92754c40e641Richard Smith      if (!Rec->isBeingDefined() && Pattern) {
5114564f4c5664f552becbd05407611a92754c40e641Richard Smith        MemberSpecializationInfo *MSI = Rec->getMemberSpecializationInfo();
5115564f4c5664f552becbd05407611a92754c40e641Richard Smith        assert(MSI && "Missing member specialization information?");
5116357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
5117564f4c5664f552becbd05407611a92754c40e641Richard Smith        if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
5118f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
5119f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
5120f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
5121d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                  /*Complain=*/!Diagnoser.Suppressed);
5122d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
5123d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
5124d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
51252943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
5126d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  if (Diagnoser.Suppressed)
51275842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
5128d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
51294ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
5130cac18add73d095eaab600aefe27ea7174aec4922Nico Weber  if (Ident___float128 &&
5131cac18add73d095eaab600aefe27ea7174aec4922Nico Weber      T == Context.getTypeDeclType(Context.getFloat128StubType())) {
5132cac18add73d095eaab600aefe27ea7174aec4922Nico Weber    Diag(Loc, diag::err_typecheck_decl_incomplete_type___float128);
5133cac18add73d095eaab600aefe27ea7174aec4922Nico Weber    return true;
5134cac18add73d095eaab600aefe27ea7174aec4922Nico Weber  }
5135cac18add73d095eaab600aefe27ea7174aec4922Nico Weber
5136d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
513791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
51384ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
513901620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
51404ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
51411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
51424ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
51434ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
5144b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor      << QualType(Tag, 0);
514591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5146b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  // If the Objective-C class was a forward declaration, produce a note.
5147b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  if (IFace && !IFace->getDecl()->isInvalidDecl())
5148b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor    Diag(IFace->getDecl()->getLocation(), diag::note_forward_class);
51494ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
51505d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain  // If we have external information that we can use to suggest a fix,
51515d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain  // produce a note.
51525d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain  if (ExternalSource)
51535d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain    ExternalSource->MaybeDiagnoseMissingCompleteType(Loc, T);
51545d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain
51554ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
51564ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
5157e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
5158fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
515991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                               unsigned DiagID) {
5160f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
5161d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(Loc, T, Diagnoser);
5162fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
5163fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
51646666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \brief Get diagnostic %select index for tag kind for
51656666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// literal type diagnostic message.
51666666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// WARNING: Indexes apply to particular diagnostics only!
51676666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos///
51686666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \returns diagnostic %select index.
5169f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matosstatic unsigned getLiteralDiagFromTagKind(TagTypeKind Tag) {
51706666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  switch (Tag) {
5171f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Struct: return 0;
5172f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Interface: return 1;
5173f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Class:  return 2;
5174f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  default: llvm_unreachable("Invalid tag kind for literal type diagnostic!");
51756666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  }
51766666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos}
51776666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos
51789f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @brief Ensure that the type T is a literal type.
51799f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
51809f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// This routine checks whether the type @p T is a literal type. If @p T is an
51819f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// incomplete type, an attempt is made to complete it. If @p T is a literal
51829f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// type, or @p AllowIncompleteType is true and @p T is an incomplete type,
51839f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// returns false. Otherwise, this routine issues the diagnostic @p PD (giving
51849f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// it the type @p T), along with notes explaining why the type is not a
51859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// literal type, and returns true.
51869f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
51879f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param Loc  The location in the source that the non-literal type
51889f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// diagnostic should refer to.
51899f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
51909f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param T  The type that this routine is examining for literalness.
51919f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
5192f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor/// @param Diagnoser Emits a diagnostic if T is not a literal type.
51939f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
51949f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @returns @c true if @p T is not a literal type and a diagnostic was emitted,
51959f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @c false otherwise.
51969f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithbool Sema::RequireLiteralType(SourceLocation Loc, QualType T,
5197f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                              TypeDiagnoser &Diagnoser) {
51989f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  assert(!T->isDependentType() && "type should not be dependent");
51999f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5200ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  QualType ElemType = Context.getBaseElementType(T);
5201ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  RequireCompleteType(Loc, ElemType, 0);
5202ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
5203a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  if (T->isLiteralType(Context))
52049f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return false;
52059f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5206f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  if (Diagnoser.Suppressed)
52079f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
52089f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5209f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
52109f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
52119f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (T->isVariableArrayType())
52129f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
52139f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5214ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  const RecordType *RT = ElemType->getAs<RecordType>();
52159f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (!RT)
52169f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
52179f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
52189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
52199f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5220c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // A partially-defined class type can't be a literal type, because a literal
5221c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // class type must have a trivial destructor (which can't be checked until
5222c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // the class definition is complete).
5223c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  if (!RD->isCompleteDefinition()) {
5224d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T);
5225ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman    return true;
5226c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  }
5227ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
52289f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // If the class has virtual base classes, then it's not an aggregate, and
522986c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // cannot have any constexpr constructors or a trivial default constructor,
523086c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // so is non-literal. This is better to diagnose than the resulting absence
523186c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // of constexpr constructors.
52329f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (RD->getNumVBases()) {
52339f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_virtual_base)
52346666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      << getLiteralDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases();
52359f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
52369f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           E = RD->vbases_end(); I != E; ++I)
523796a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      Diag(I->getLocStart(),
52389f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           diag::note_constexpr_virtual_base_here) << I->getSourceRange();
523986c3ae46250cdcc57778c27826060779a92f3815Richard Smith  } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor() &&
524086c3ae46250cdcc57778c27826060779a92f3815Richard Smith             !RD->hasTrivialDefaultConstructor()) {
52419f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD;
52429f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (RD->hasNonLiteralTypeFieldsOrBases()) {
52439f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
52449f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->bases_end(); I != E; ++I) {
5245a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith      if (!I->getType()->isLiteralType(Context)) {
524696a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        Diag(I->getLocStart(),
52479f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith             diag::note_non_literal_base_class)
52489f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith          << RD << I->getType() << I->getSourceRange();
52499f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
52509f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
52519f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
52529f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::field_iterator I = RD->field_begin(),
52539f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->field_end(); I != E; ++I) {
5254a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith      if (!I->getType()->isLiteralType(Context) ||
5255262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          I->getType().isVolatileQualified()) {
5256262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        Diag(I->getLocation(), diag::note_non_literal_field)
5257581deb3da481053c4993c7600f97acf7768caac5David Blaikie          << RD << *I << I->getType()
5258262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          << I->getType().isVolatileQualified();
52599f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
52609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
52619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
52629f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (!RD->hasTrivialDestructor()) {
52639f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // All fields and bases are of literal types, so have trivial destructors.
52649f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // If this class's destructor is non-trivial it must be user-declared.
52659f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    CXXDestructorDecl *Dtor = RD->getDestructor();
52669f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    assert(Dtor && "class has literal fields and bases but no dtor?");
52679f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    if (!Dtor)
52689f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      return true;
52699f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
52709f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(Dtor->getLocation(), Dtor->isUserProvided() ?
52719f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_user_provided_dtor :
52729f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_nontrivial_dtor) << RD;
5273ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    if (!Dtor->isUserProvided())
5274ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith      SpecialMemberIsTrivial(Dtor, CXXDestructor, /*Diagnose*/true);
52759f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  }
52769f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
52779f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return true;
52789f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
52799f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
528091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierbool Sema::RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID) {
5281f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
5282f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  return RequireLiteralType(Loc, T, Diagnoser);
5283f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor}
5284f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor
5285465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
5286465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
5287465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
5288465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
5289465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
5290e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
5291465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
5292e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
5293465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5294465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
5295465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
5296465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
5297465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
5298465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
5299465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
5300e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
5301af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
53022a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
5303fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
53042a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
53052a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
53062a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
53072b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
53082b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
5309aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
5310aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
53112b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
5312af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
5313af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
5314af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
5315f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// getDecltypeForExpr - Given an expr, will return the decltype for
5316f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// that expression, according to the rules in C++11
5317f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18.
5318f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregorstatic QualType getDecltypeForExpr(Sema &S, Expr *E) {
5319f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (E->isTypeDependent())
5320f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    return S.Context.DependentTy;
5321f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
53226d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
53236d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   The type denoted by decltype(e) is defined as follows:
53246d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //
53256d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //     - if e is an unparenthesized id-expression or an unparenthesized class
532691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       member access (5.2.5), decltype(e) is the type of the entity named
532791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       by e. If there is no such entity, or if e names a set of overloaded
53286d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       functions, the program is ill-formed;
532984dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  //
533084dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  // We apply the same rules for Objective-C ivar and property references.
5331f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
5332f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
5333f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return VD->getType();
533484dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
5335f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
5336f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return FD->getType();
533784dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCIvarRefExpr *IR = dyn_cast<ObjCIvarRefExpr>(E)) {
533884dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    return IR->getDecl()->getType();
533984dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCPropertyRefExpr *PR = dyn_cast<ObjCPropertyRefExpr>(E)) {
534084dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    if (PR->isExplicitProperty())
534184dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor      return PR->getExplicitProperty()->getType();
5342f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
534384dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor
5344f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  // C++11 [expr.lambda.prim]p18:
5345f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   Every occurrence of decltype((x)) where x is a possibly
5346f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   parenthesized id-expression that names an entity of automatic
5347f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   storage duration is treated as if x were transformed into an
5348f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   access to a corresponding data member of the closure type that
5349f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   would have been declared if x were an odr-use of the denoted
5350f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   entity.
5351f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  using namespace sema;
5352f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (S.getCurLambda()) {
5353f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (isa<ParenExpr>(E)) {
5354f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
5355f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
535668932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          QualType T = S.getCapturedDeclRefType(Var, DRE->getLocation());
535768932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          if (!T.isNull())
535868932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor            return S.Context.getLValueReferenceType(T);
5359f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        }
5360f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      }
5361f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    }
5362f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
5363f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
5364f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
53656d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
53666d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   [...]
53676d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  QualType T = E->getType();
53686d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  switch (E->getValueKind()) {
536991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the
53706d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
53716d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_XValue: T = S.Context.getRValueReferenceType(T); break;
537291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an lvalue, decltype(e) is T&, where T is the
53736d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
53746d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_LValue: T = S.Context.getLValueReferenceType(T); break;
53756d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //  - otherwise, decltype(e) is the type of e.
53766d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_RValue: break;
53776d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  }
537891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5379f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return T;
5380f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor}
5381f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
53822a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
5383fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
53842a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
53852a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
538691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5387f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return Context.getDecltypeType(E, getDecltypeForExpr(*this, E));
5388af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
5389ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
5390ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType Sema::BuildUnaryTransformType(QualType BaseType,
5391ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::UTTKind UKind,
5392ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       SourceLocation Loc) {
5393ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  switch (UKind) {
5394ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case UnaryTransformType::EnumUnderlyingType:
5395ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) {
5396ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Diag(Loc, diag::err_only_enums_have_underlying_types);
5397ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return QualType();
5398ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    } else {
5399ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      QualType Underlying = BaseType;
5400ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      if (!BaseType->isDependentType()) {
5401ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl();
5402ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        assert(ED && "EnumType has no EnumDecl");
5403ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        DiagnoseUseOfDecl(ED, Loc);
5404ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        Underlying = ED->getIntegerType();
5405ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      }
5406ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(!Underlying.isNull());
5407ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return Context.getUnaryTransformType(BaseType, Underlying,
5408ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                        UnaryTransformType::EnumUnderlyingType);
5409ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
5410ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  }
5411ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  llvm_unreachable("unknown unary transform type");
5412ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
5413b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5414b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) {
5415b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T->isDependentType()) {
54168327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // FIXME: It isn't entirely clear whether incomplete atomic types
54178327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // are allowed or not; for simplicity, ban them for the moment.
5418d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (RequireCompleteType(Loc, T, diag::err_atomic_specifier_bad_type, 0))
54198327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith      return QualType();
54208327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith
5421b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    int DisallowedKind = -1;
54228327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    if (T->isArrayType())
5423b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 1;
5424b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isFunctionType())
5425b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 2;
5426b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isReferenceType())
5427b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 3;
5428b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isAtomicType())
5429b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 4;
5430b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T.hasQualifiers())
5431b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 5;
5432b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (!T.isTriviallyCopyableType(Context))
5433b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      // Some other non-trivially-copyable type (probably a C++ class)
5434b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 6;
5435b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5436b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (DisallowedKind != -1) {
5437b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T;
5438b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return QualType();
5439b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
5440b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5441b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // FIXME: Do we need any handling for ARC here?
5442b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
5443b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5444b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Build the pointer type.
5445b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return Context.getAtomicType(T);
5446b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
5447