SemaType.cpp revision d1a32c328bce903fb1b17fc8147b646be818298e
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.
76541d0958281627882fbe2049fb887d741eabd3fe3Richard Smith    if (S.getLangOpts().CPlusPlus1y &&
76641d0958281627882fbe2049fb887d741eabd3fe3Richard Smith        declarator.getContext() == Declarator::LambdaExprContext) {
76741d0958281627882fbe2049fb887d741eabd3fe3Richard Smith      // In C++1y, a lambda's implicit return type is 'auto'.
76841d0958281627882fbe2049fb887d741eabd3fe3Richard Smith      Result = Context.getAutoDeductType();
76941d0958281627882fbe2049fb887d741eabd3fe3Richard Smith      break;
77041d0958281627882fbe2049fb887d741eabd3fe3Richard Smith    } else if (declarator.getContext() == Declarator::LambdaExprContext ||
77141d0958281627882fbe2049fb887d741eabd3fe3Richard Smith               isOmittedBlockReturnType(declarator)) {
772152b4e4652baedfceba1cd8115515629225e713fManuel Klimek      Result = Context.DependentTy;
7735db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
7745db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
7751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
776d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
777d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
778d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
779d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
780d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
781d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
782d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
7834e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().ImplicitInt) {
78435d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
78535d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
7863f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
787711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_declspec)
7883f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
78996a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        << FixItHint::CreateInsertion(DS.getLocStart(), "int");
7903f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
7914310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
792d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
793d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
794d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
795d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
79658eb37036b47bbe7433f72d92a2cb60848507707Richard Smith      if (S.getLangOpts().CPlusPlus) {
797711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_missing_type_specifier)
7983f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
7991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
800b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
801b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
802b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
803711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
804b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
805711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_type_specifier)
8063f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
807b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
808d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
8091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
8113cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
8125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
8135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
814fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
815fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
816fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
817311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
818311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
81991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
820e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
821e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
822e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
823e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
82480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
825e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
826e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
827e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
828e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
829311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
8305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
8315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
8325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
833fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
834fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
835fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
836311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
837311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
83891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
839e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
840e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
841e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
842e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
84380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
844e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
845e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
846e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
847e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
848311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
8495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
8505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
851958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
8523cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
8535a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith  case DeclSpec::TST_int128:
85484268904947ada7e251932a6f5b0f4364df7a2c7Richard Smith    if (!S.PP.getTargetInfo().hasInt128Type())
85584268904947ada7e251932a6f5b0f4364df7a2c7Richard Smith      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_int128_unsupported);
8565a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    if (DS.getTypeSpecSign() == DeclSpec::TSS_unsigned)
8575a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.UnsignedInt128Ty;
8585a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    else
8595a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.Int128Ty;
8605a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    break;
861aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case DeclSpec::TST_half: Result = Context.HalfTy; break;
862fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
863958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
864958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
865fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
866958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
867fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
86839d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne
8694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
87039d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
87139d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      declarator.setInvalidType(true);
87239d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    }
873958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
874fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
8775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
878711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
8798f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
880711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
8818f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
88299dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
8835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
8856666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_struct:
8866666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_interface: {
887b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
8886e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
8896e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
8906e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
891711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
8926e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
8936e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
8946e24726524c2b51b31bb4b622aa678a46b024f42John McCall
895a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
8960daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc());
89791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
8985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
899a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
90091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
9015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
902a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
9032191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
9040daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    // In both C and C++, make an ElaboratedType.
9050daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    ElaboratedTypeKeyword Keyword
9060daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
9070daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
908958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
9091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
9101a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
9115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
9125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
9135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
914711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
91527940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    if (Result.isNull())
916711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
91727940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    else if (DeclSpec::ProtocolQualifierListTy PQ
91827940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall               = DS.getProtocolQualifiers()) {
919c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
920c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // Silently drop any existing protocol qualifiers.
921c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // TODO: determine whether that's the right thing to do.
922c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjT->getNumProtocols())
923c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = ObjT->getBaseType();
924c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
925c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (DS.getNumProtocolQualifiers())
926c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = Context.getObjCObjectType(Result,
92731ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                             (ObjCProtocolDecl*const*) PQ,
928c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             DS.getNumProtocolQualifiers());
929c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCIdType()) {
930ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
931c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
93231ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                           (ObjCProtocolDecl*const*) PQ,
933c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
934c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
935c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCClassType()) {
9364262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
937c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
93831ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                           (ObjCProtocolDecl*const*) PQ,
939c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
940c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
9413f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
942711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
9433f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
944711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
9453f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
946c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
9471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
949958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
9505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
951958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
952e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
953711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
954958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
955730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian    if (!Result->isDependentType())
956730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian      if (const TagType *TT = Result->getAs<TagType>())
957711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
958d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
959fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
960958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
961d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
962b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
963d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
964d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
965711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
9664b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
9674b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
968711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
9694b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
970958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
971d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
9726fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
973b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
9746fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
9756fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
976711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
977af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
978af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
979711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
980af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
9816fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
9826fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
983ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case DeclSpec::TST_underlyingType:
984db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    Result = S.GetTypeFromParser(DS.getRepAsType());
985db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    assert(!Result.isNull() && "Didn't get a type for __underlying_type?");
986ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    Result = S.BuildUnaryTransformType(Result,
987ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::EnumUnderlyingType,
988ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       DS.getTypeSpecTypeLoc());
989ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (Result.isNull()) {
990ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Result = Context.IntTy;
991ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      declarator.setInvalidType(true);
992db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    }
99391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
994db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
995a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  case DeclSpec::TST_auto:
996e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
997fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    // If auto is mentioned in a lambda parameter context, convert it to a
998fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    // template parameter type immediately, with the appropriate depth and
999fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    // index, and update sema's state (LambdaScopeInfo) for the current lambda
1000fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    // being analyzed (which tracks the invented type template parameter).
1001fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    if (declarator.getContext() == Declarator::LambdaExprParameterContext) {
1002fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      sema::LambdaScopeInfo *LSI = S.getCurLambda();
1003fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      assert(LSI && "No LambdaScopeInfo on the stack!");
1004fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      const unsigned TemplateParameterDepth = LSI->AutoTemplateParameterDepth;
1005fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      const unsigned AutoParameterPosition = LSI->AutoTemplateParams.size();
1006fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      const bool IsParameterPack = declarator.hasEllipsis();
1007fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali
1008fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // Create a name for the invented template parameter type.
1009fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      std::string InventedTemplateParamName = "$auto-";
1010fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      llvm::raw_string_ostream ss(InventedTemplateParamName);
1011fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      ss << TemplateParameterDepth;
1012fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      ss << "-" << AutoParameterPosition;
1013fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      ss.flush();
1014fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali
1015fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      IdentifierInfo& TemplateParamII = Context.Idents.get(
1016fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali                                        InventedTemplateParamName.c_str());
1017fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // Turns out we must create the TemplateTypeParmDecl here to
1018fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // retrieve the corresponding template parameter type.
1019fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      TemplateTypeParmDecl *CorrespondingTemplateParam =
1020fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        TemplateTypeParmDecl::Create(Context,
1021fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        // Temporarily add to the TranslationUnit DeclContext.  When the
1022fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        // associated TemplateParameterList is attached to a template
1023fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        // declaration (such as FunctionTemplateDecl), the DeclContext
1024fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        // for each template parameter gets updated appropriately via
1025fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        // a call to AdoptTemplateParameterList.
1026fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        Context.getTranslationUnitDecl(),
1027fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        /*KeyLoc*/ SourceLocation(),
1028fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        /*NameLoc*/ declarator.getLocStart(),
1029fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        TemplateParameterDepth,
1030fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        AutoParameterPosition,  // our template param index
1031fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        /* Identifier*/ &TemplateParamII, false, IsParameterPack);
1032fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      LSI->AutoTemplateParams.push_back(CorrespondingTemplateParam);
1033fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // Replace the 'auto' in the function parameter with this invented
1034fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // template type parameter.
1035fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      Result = QualType(CorrespondingTemplateParam->getTypeForDecl(), 0);
1036fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    } else {
1037fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      Result = Context.getAutoType(QualType(), /*decltype(auto)*/false, false);
1038fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    }
1039a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    break;
1040a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
1041a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  case DeclSpec::TST_decltype_auto:
1042fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    Result = Context.getAutoType(QualType(),
1043fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali                                 /*decltype(auto)*/true,
1044fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali                                 /*IsDependent*/   false);
1045e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
10461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1047a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall  case DeclSpec::TST_unknown_anytype:
1048a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    Result = Context.UnknownAnyTy;
1049a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    break;
1050a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall
1051b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case DeclSpec::TST_atomic:
1052b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.GetTypeFromParser(DS.getRepAsType());
1053b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(!Result.isNull() && "Didn't get a type for _Atomic?");
1054b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc());
1055b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Result.isNull()) {
1056b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Result = Context.IntTy;
1057b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      declarator.setInvalidType(true);
1058b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
105991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
1060b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1061b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_t:
1062b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dTy;
1063b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1064b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1065b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_array_t:
1066b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dArrayTy;
1067b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1068b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1069b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_buffer_t:
1070b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dBufferTy;
1071b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1072b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1073b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_t:
1074b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dTy;
1075b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1076b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1077b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_array_t:
1078b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dArrayTy;
1079b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1080b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1081b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image3d_t:
1082b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage3dTy;
1083b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1084b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
108521f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei  case DeclSpec::TST_sampler_t:
108621f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    Result = Context.OCLSamplerTy;
108721f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    break;
108821f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei
1089e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei  case DeclSpec::TST_event_t:
1090e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    Result = Context.OCLEventTy;
1091e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    break;
1092e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei
1093809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
10945153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
1095711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
10965153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1099958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
1100f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
11014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().Freestanding)
1102711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
1103fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
110482287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
110582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
110682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
1107e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
1108788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
1109e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
1110788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
1111e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
1112e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
1113f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
11141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
111547423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
111647423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
1117711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
11181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1119711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
1120711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
1121711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
1122711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
1123711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
1125711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
1126711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
1127711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (AttributeList *attrs = DS.getAttributes().getList())
1128f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    processTypeAttrs(state, Result, TAL_DeclSpec, attrs);
11291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
113096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
113196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
113296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
113396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
113496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
113596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
113696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
11370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
11384cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        S.Diag(DS.getConstSpecLoc(), diag::warn_typecheck_function_qualifiers)
11394cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith          << Result << DS.getSourceRange();
11400953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
11414cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        S.Diag(DS.getVolatileSpecLoc(), diag::warn_typecheck_function_qualifiers)
11424cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith          << Result << DS.getSourceRange();
11430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
11444cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        assert((TypeQuals & (DeclSpec::TQ_restrict | DeclSpec::TQ_atomic)) &&
11454cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith               "Has CVRA quals but not C, V, R, or A?");
11464cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // No diagnostic; we'll diagnose 'restrict' or '_Atomic' applied to a
11474cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // function type later, in BuildQualifiedType.
114896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
114996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
11501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1151f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
1152f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
1153f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
1154f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
1155f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
11561a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
11571a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
1158f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
11590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
11600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
11614cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      TypeQuals &= ~DeclSpec::TQ_atomic;
11621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
11631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1164bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // C90 6.5.3 constraints: "The same type qualifier shall not appear more
1165bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // than once in the same specifier-list or qualifier-list, either directly
1166bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // or via one or more typedefs."
116791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus
1168bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman        && TypeQuals & Result.getCVRQualifiers()) {
1169bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()) {
117091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec)
1171bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "const";
1172bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1173bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
1174bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()) {
117591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec)
1176bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "volatile";
1177bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1178bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
11794cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // C90 doesn't have restrict nor _Atomic, so it doesn't force us to
11804cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // produce a warning in this case.
1181bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    }
1182bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
11834cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    QualType Qualified = S.BuildQualifiedType(Result, DeclLoc, TypeQuals, &DS);
11844cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
11854cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // If adding qualifiers fails, just use the unqualified type.
11864cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (Qualified.isNull())
11874cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      declarator.setInvalidType(true);
11884cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    else
11894cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      Result = Qualified;
119096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
11910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1192f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
1193f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
1194f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
1195cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
1196cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
1197cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1199cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
1200cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1201cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
12022865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
120393d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith                                  Qualifiers Qs, const DeclSpec *DS) {
12042865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
12052865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
12062865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
12072865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
12082865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
12092865474261a608c7873b87ba4af110d17907896dJohn McCall
121093d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith    if (T->isAnyPointerType() || T->isReferenceType() ||
121193d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        T->isMemberPointerType()) {
121293d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      QualType EltTy;
121393d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      if (T->isObjCObjectPointerType())
121493d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = T;
121593d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>())
121693d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = PTy->getPointeeType();
121793d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      else
121893d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = T->getPointeeType();
121993d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith
122093d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      // If we have a pointer or reference, the pointee must have an object
122193d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      // incomplete type.
122293d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      if (!EltTy->isIncompleteOrObjectType()) {
12232865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
122493d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        ProblemTy = EltTy;
122591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
122693d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith    } else if (!T->isDependentType()) {
122793d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      DiagID = diag::err_typecheck_invalid_restrict_not_pointer;
12282865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
12292865474261a608c7873b87ba4af110d17907896dJohn McCall    }
12302865474261a608c7873b87ba4af110d17907896dJohn McCall
12312865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
123293d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << ProblemTy;
12332865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
12342865474261a608c7873b87ba4af110d17907896dJohn McCall    }
12352865474261a608c7873b87ba4af110d17907896dJohn McCall  }
12362865474261a608c7873b87ba4af110d17907896dJohn McCall
12372865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
12382865474261a608c7873b87ba4af110d17907896dJohn McCall}
12392865474261a608c7873b87ba4af110d17907896dJohn McCall
12404cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard SmithQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
12414cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith                                  unsigned CVRA, const DeclSpec *DS) {
12424cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // Convert from DeclSpec::TQ to Qualifiers::TQ by just dropping TQ_atomic.
12434cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  unsigned CVR = CVRA & ~DeclSpec::TQ_atomic;
12444cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
12454cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // C11 6.7.3/5:
12464cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   If the same qualifier appears more than once in the same
12474cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   specifier-qualifier-list, either directly or via one or more typedefs,
12484cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   the behavior is the same as if it appeared only once.
12494cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //
12504cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // It's not specified what happens when the _Atomic qualifier is applied to
12514cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // a type specified with the _Atomic specifier, but we assume that this
12524cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // should be treated as if the _Atomic qualifier appeared multiple times.
12534cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  if (CVRA & DeclSpec::TQ_atomic && !T->isAtomicType()) {
12544cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // C11 6.7.3/5:
12554cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   If other qualifiers appear along with the _Atomic qualifier in a
12564cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   specifier-qualifier-list, the resulting type is the so-qualified
12574cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   atomic type.
12584cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //
12594cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // Don't need to worry about array types here, since _Atomic can't be
12604cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // applied to such types.
12614cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    SplitQualType Split = T.getSplitUnqualifiedType();
12624cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    T = BuildAtomicType(QualType(Split.Ty, 0),
12634cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith                        DS ? DS->getAtomicSpecLoc() : Loc);
12644cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (T.isNull())
12654cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      return T;
12664cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    Split.Quals.addCVRQualifiers(CVR);
12674cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    return BuildQualifiedType(T, Loc, Split.Quals);
12684cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
12694cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
12704cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  return BuildQualifiedType(T, Loc, Qualifiers::fromCVRMask(CVR), DS);
12714cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith}
12724cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
1273075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
1274075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
1275075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
1276075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
1277075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
1278f85e193739c953358c865005855253af4f68a497John McCall/// Given that we're building a pointer or reference to the given
1279f85e193739c953358c865005855253af4f68a497John McCallstatic QualType inferARCLifetimeForPointee(Sema &S, QualType type,
1280f85e193739c953358c865005855253af4f68a497John McCall                                           SourceLocation loc,
1281f85e193739c953358c865005855253af4f68a497John McCall                                           bool isReference) {
1282f85e193739c953358c865005855253af4f68a497John McCall  // Bail out if retention is unrequired or already specified.
1283f85e193739c953358c865005855253af4f68a497John McCall  if (!type->isObjCLifetimeType() ||
1284f85e193739c953358c865005855253af4f68a497John McCall      type.getObjCLifetime() != Qualifiers::OCL_None)
1285f85e193739c953358c865005855253af4f68a497John McCall    return type;
1286f85e193739c953358c865005855253af4f68a497John McCall
1287f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None;
1288f85e193739c953358c865005855253af4f68a497John McCall
1289f85e193739c953358c865005855253af4f68a497John McCall  // If the object type is const-qualified, we can safely use
1290f85e193739c953358c865005855253af4f68a497John McCall  // __unsafe_unretained.  This is safe (because there are no read
1291f85e193739c953358c865005855253af4f68a497John McCall  // barriers), and it'll be safe to coerce anything but __weak* to
1292f85e193739c953358c865005855253af4f68a497John McCall  // the resulting type.
1293f85e193739c953358c865005855253af4f68a497John McCall  if (type.isConstQualified()) {
1294f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
1295f85e193739c953358c865005855253af4f68a497John McCall
1296f85e193739c953358c865005855253af4f68a497John McCall  // Otherwise, check whether the static type does not require
1297f85e193739c953358c865005855253af4f68a497John McCall  // retaining.  This currently only triggers for Class (possibly
1298f85e193739c953358c865005855253af4f68a497John McCall  // protocol-qualifed, and arrays thereof).
1299f85e193739c953358c865005855253af4f68a497John McCall  } else if (type->isObjCARCImplicitlyUnretainedType()) {
1300f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
13015b76f373d23cc3b292ecf523349aaaa388eea375Argyrios Kyrtzidis
1302ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // If we are in an unevaluated context, like sizeof, skip adding a
1303ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // qualification.
130471f55f771794674a410171dbf3cb5dbedf95d033David Blaikie  } else if (S.isUnevaluatedContext()) {
1305ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    return type;
1306f85e193739c953358c865005855253af4f68a497John McCall
1307e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // If that failed, give an error and recover using __strong.  __strong
1308e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // is the option most likely to prevent spurious second-order diagnostics,
1309e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // like when binding a reference to a field.
1310f85e193739c953358c865005855253af4f68a497John McCall  } else {
1311f85e193739c953358c865005855253af4f68a497John McCall    // These types can show up in private ivars in system headers, so
1312f85e193739c953358c865005855253af4f68a497John McCall    // we need this to not be an error in those cases.  Instead we
1313f85e193739c953358c865005855253af4f68a497John McCall    // want to delay.
1314f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1315ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.DelayedDiagnostics.add(
1316ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman          sema::DelayedDiagnostic::makeForbiddenType(loc,
1317ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman              diag::err_arc_indirect_no_ownership, type, isReference));
1318f85e193739c953358c865005855253af4f68a497John McCall    } else {
1319ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference;
1320f85e193739c953358c865005855253af4f68a497John McCall    }
1321e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall    implicitLifetime = Qualifiers::OCL_Strong;
1322f85e193739c953358c865005855253af4f68a497John McCall  }
1323f85e193739c953358c865005855253af4f68a497John McCall  assert(implicitLifetime && "didn't infer any lifetime!");
1324f85e193739c953358c865005855253af4f68a497John McCall
1325f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers qs;
1326f85e193739c953358c865005855253af4f68a497John McCall  qs.addObjCLifetime(implicitLifetime);
1327f85e193739c953358c865005855253af4f68a497John McCall  return S.Context.getQualifiedType(type, qs);
1328f85e193739c953358c865005855253af4f68a497John McCall}
1329f85e193739c953358c865005855253af4f68a497John McCall
1330cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
1331cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1332cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
1333cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1334cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1335cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
1336cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
1337cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1338cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
1339cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1340cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1341cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
1342cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
13432865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
1344cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
1345cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
1346cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
1347cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
1348ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1349cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1350cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1351cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1352c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
135392e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
1354f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build pointers to unqualified pointers.
13554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1356f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false);
1357f85e193739c953358c865005855253af4f68a497John McCall
1358cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
13592865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
1360cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1361cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1362cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
1363cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1364cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
1365cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1366cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1367cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
1368cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
1369cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1370cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
1371cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1372cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1373cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
1374cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
137554e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
13762865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
137754e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
137891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  assert(Context.getCanonicalType(T) != Context.OverloadTy &&
13799625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
138091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
138169d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // C++0x [dcl.ref]p6:
138291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   If a typedef (7.1.3), a type template-parameter (14.3.1), or a
138391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
138491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   type T, an attempt to create the type "lvalue reference to cv TR" creates
138591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   the type "lvalue reference to T", while an attempt to create the type
138669d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   "rvalue reference to cv TR" creates the type TR.
138754e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
138854e14c4db764c0636160d26c5bbf491637c83a76John McCall
138954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
139054e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
139154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
139254e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
139354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
139454e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
139554e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
139654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
139754e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
139854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
139954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
140069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // collapsing of references-to-references as described in C++0x.
140169d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // DR 106 and 540 introduce reference-collapsing into C++98/03.
1402cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1403cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
140433a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
1405cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
1406cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
1407cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
1408cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1409cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1410cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1411f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build references to unqualified pointers.
14124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1413f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true);
1414f85e193739c953358c865005855253af4f68a497John McCall
1415cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
14167c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
14172865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
14182865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
1419cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1420cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1421e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// Check whether the specified array size makes the array type a VLA.  If so,
1422e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// return true, if not, return the size of the array in SizeVal.
1423282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smithstatic bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal) {
1424282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode
1425282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // (like gnu99, but not c99) accept any evaluatable value as an extension.
1426ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  class VLADiagnoser : public Sema::VerifyICEDiagnoser {
1427ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  public:
1428ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    VLADiagnoser() : Sema::VerifyICEDiagnoser(true) {}
142991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1430ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) {
1431ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
143291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1433ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseFold(Sema &S, SourceLocation Loc, SourceRange SR) {
1434ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      S.Diag(Loc, diag::ext_vla_folded_to_constant) << SR;
1435ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
1436ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  } Diagnoser;
143791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1438ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  return S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser,
1439ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                           S.LangOpts.GNUMode).isInvalid();
1440e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner}
1441e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1442e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1443cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
1444cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1445cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
1446cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1447cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
14481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
14491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
1450cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1451efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Brackets The range from the opening '[' to the closing ']'.
1452cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1453cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
1454cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1455cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1456cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
1457cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
1458cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1459cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
14607e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
14610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
14627e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
14634e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
1464138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
1465138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
146691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   type, the (possibly cv-qualified) type void, a function type or an
1467138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
1468138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
1469bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    // C++ [dcl.array]p3:
1470bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   When several "array of" specifications are adjacent, [...] only the
1471bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   first of the constant expressions that specify the bounds of the arrays
1472bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   may be omitted.
1473bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //
1474138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
1475138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
1476138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
1477138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
1478138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1479138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
148091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1481bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    if (T->isVoidType() || T->isIncompleteArrayType()) {
1482923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1483923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1484923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
148591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
148691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (RequireNonAbstractType(Brackets.getBegin(), T,
1487138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
1488138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
148991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1490923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
1491138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1492138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
1493923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
1494923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
1495923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1496923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
1497cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1498cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
1499cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
1500ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1501cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1502cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
15031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15046217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
1505cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
1506cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
1507cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
1508cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
1509c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
1510c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1511c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
1512cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
15131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1514806054db6653d29cb0d9692df3612cbcd03d0530John McCall  // Do placeholder conversions on the array size expression.
1515806054db6653d29cb0d9692df3612cbcd03d0530John McCall  if (ArraySize && ArraySize->hasPlaceholderType()) {
1516806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ExprResult Result = CheckPlaceholderExpr(ArraySize);
1517806054db6653d29cb0d9692df3612cbcd03d0530John McCall    if (Result.isInvalid()) return QualType();
1518806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ArraySize = Result.take();
1519806054db6653d29cb0d9692df3612cbcd03d0530John McCall  }
1520806054db6653d29cb0d9692df3612cbcd03d0530John McCall
15215e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
1522429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ArraySize && !ArraySize->isRValue()) {
1523429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult Result = DefaultLvalueConversion(ArraySize);
1524429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Result.isInvalid())
1525429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return QualType();
1526429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
1527429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ArraySize = Result.take();
1528429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
15295e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1530cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
1531282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // C++11 allows contextual conversions to such types.
153280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (!getLangOpts().CPlusPlus11 &&
1533282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      ArraySize && !ArraySize->isTypeDependent() &&
15341274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1535cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1536cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
1537cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1538cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1539282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
15402767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
1541cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
1542f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
15437e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
1544f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
1545f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
1546ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
15477e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
1548282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  } else if ((!T->isDependentType() && !T->isIncompleteType() &&
1549282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith              !T->isConstantSizeType()) ||
1550282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith             isArraySizeVLA(*this, ArraySize, ConstVal)) {
1551282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // Even in C++11, don't allow contextual conversions in the array bound
1552282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // of a VLA.
155380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (getLangOpts().CPlusPlus11 &&
1554282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1555282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1556282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        << ArraySize->getType() << ArraySize->getSourceRange();
1557282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      return QualType();
1558282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    }
1559282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
1560e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with an element type that has a non-constant-size is a VLA.
1561e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with a non-ICE size is a VLA.  We accept any expression
1562e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // that we can fold to a non-zero positive value as an extension.
15637e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1564cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
1565cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1566cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
1567923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
1568b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
1569b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1570b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1571b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
1572b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1573b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
1574923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1575923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1576923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
157702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
157802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
157991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      Diag(ArraySize->getLocStart(),
158002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
158102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
1582923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
158320cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne
158420cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      if (ASM == ArrayType::Static) {
158520cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        Diag(ArraySize->getLocStart(),
158620cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne             diag::warn_typecheck_zero_static_array_size)
158720cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne          << ArraySize->getSourceRange();
158820cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        ASM = ArrayType::Normal;
158920cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      }
159091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
1591fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali               !T->isIncompleteType() && !T->isUndeducedType()) {
159291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // Is the array too large?
15932767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
15942767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
1595ee2f8f2f7c4eacfa305a29fcd916d63c650ca847Richard Trieu      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) {
15962767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
15972767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
15982767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
1599ee2f8f2f7c4eacfa305a29fcd916d63c650ca847Richard Trieu        return QualType();
1600ee2f8f2f7c4eacfa305a29fcd916d63c650ca847Richard Trieu      }
16011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
160291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
160346a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
1604cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1605617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly
1606617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  // OpenCL v1.2 s6.9.d: variable length arrays are not supported.
1607617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  if (getLangOpts().OpenCL && T->isVariableArrayType()) {
1608617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    Diag(Loc, diag::err_opencl_vla);
1609617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    return QualType();
1610617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  }
1611af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
16124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().C99) {
16130fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
16140fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
1615f85e193739c953358c865005855253af4f68a497John McCall      QualType BaseT = Context.getBaseElementType(T);
161691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      if (!T->isDependentType() &&
16173c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT.isPODType(Context) &&
16183c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT->isObjCLifetimeType()) {
16190fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
1620f85e193739c953358c865005855253af4f68a497John McCall          << BaseT;
16210fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
162291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
1623a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
1624a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
1625a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
1626a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
1627a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
16280fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
16290fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
16308a3f9e46cb988d2c664395b21910091e3730ae82Richard Smith        Diag(Loc, diag::ext_vla);
16310fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
1632d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith      Diag(Loc,
16334e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie           getLangOpts().CPlusPlus? diag::err_c99_array_usage_cxx
1634d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith                                     : diag::ext_c99_array_usage) << ASM;
1635cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1636cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1637630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko  if (T->isVariableArrayType()) {
1638630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko    // Warn about VLAs for -Wvla.
1639630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko    Diag(Loc, diag::warn_vla_used);
1640630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko  }
1641630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko
1642cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
1643cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
16449cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
16459cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
16469cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
16479cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
16489ae2f076ca5ab1feb3ba95629099ec2319833701John McCallQualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
16499cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
16509cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
16519cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
16521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
16539cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
16549cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
16559cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
16569cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
16579cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
16589ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
16599cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
16609ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
16619f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      Diag(AttrLoc, diag::err_attribute_argument_type)
16629f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman        << "ext_vector_type" << AANT_ArgumentIntegerConstant
16639f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman        << ArraySize->getSourceRange();
16649cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
16659cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
16661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
16689cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
16691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
16701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16719cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
16729cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
16739ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      << ArraySize->getSourceRange();
16749cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
16759cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
16761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16771652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    if (VectorType::isVectorSizeTooLarge(vectorSize)) {
16781652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      Diag(AttrLoc, diag::err_attribute_size_too_large)
16791652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman        << ArraySize->getSourceRange();
16801652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      return QualType();
16811652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    }
16821652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman
16834ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    return Context.getExtVectorType(T, vectorSize);
16841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
16851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16869ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
16879cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
16881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1689ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedmanbool Sema::CheckFunctionReturnType(QualType T, SourceLocation Loc) {
1690724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
169191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    Diag(Loc, diag::err_func_returning_array_function)
169258408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
1693ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    return true;
1694724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1695aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1696aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // Functions cannot return half FP.
1697aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  if (T->isHalfType()) {
1698aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 <<
1699aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      FixItHint::CreateInsertion(Loc, "*");
1700ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    return true;
1701ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  }
1702ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
1703ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  // Methods cannot return interface types. All ObjC objects are
1704ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  // passed by reference.
1705ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  if (T->isObjCObjectType()) {
1706ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    Diag(Loc, diag::err_object_cannot_be_passed_returned_by_value) << 0 << T;
1707ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    return 0;
1708aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  }
1709aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1710ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  return false;
1711ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman}
1712ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
1713ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli FriedmanQualType Sema::BuildFunctionType(QualType T,
1714ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman                                 llvm::MutableArrayRef<QualType> ParamTypes,
1715ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
1716ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman                                 const FunctionProtoType::ExtProtoInfo &EPI) {
1717724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
1718ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
1719ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  Invalid |= CheckFunctionReturnType(T, Loc);
1720ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
1721bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  for (unsigned Idx = 0, Cnt = ParamTypes.size(); Idx < Cnt; ++Idx) {
1722aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    // FIXME: Loc is too inprecise here, should use proper locations for args.
1723c910d4cfa5042f2c9da1eb4e0b6ed59240c0eeeeReid Kleckner    QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]);
17242dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
1725724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
1726724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
1727aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    } else if (ParamType->isHalfType()) {
1728aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Disallow half FP arguments.
1729aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 <<
1730aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        FixItHint::CreateInsertion(Loc, "*");
1731aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Invalid = true;
1732724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
1733cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
173454e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
1735724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1736724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1737724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
1738724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1739724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1740bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  return Context.getFunctionType(T, ParamTypes, EPI);
1741724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
17421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1743949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
1744949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1745949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
1746949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
1747949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
1748949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
1749949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1750949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
1751949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
17521eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
17532865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
1754949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
1755949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
1756949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
1757949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
1758949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
1759949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1760949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
1761949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
1762949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1763949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
1764949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
1765949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
1766949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1767949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1768737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
1769949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
1770949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
17718d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
1772ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
1773949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1774949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1775949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1776949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
1777949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1778949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
1779949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1780949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1781949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1782949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
1783949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1784949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1785949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1786949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1787b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // C++ allows the class type in a member pointer to be an incomplete type.
1788b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // In the Microsoft ABI, the size of the member pointer can vary
1789b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // according to the class type, which means that we really need a
1790b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // complete type if possible, which means we need to instantiate templates.
1791b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  //
179284e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  // If template instantiation fails or the type is just incomplete, we have to
179384e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  // add an extra slot to the member pointer.  Yes, this does cause problems
179484e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  // when passing pointers between TUs that disagree about the size.
179584e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  if (Context.getTargetInfo().getCXXABI().isMicrosoft()) {
179684e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner    CXXRecordDecl *RD = Class->getAsCXXRecordDecl();
1797e93e2552e76ab704ec85919cc2c76f02b8b081eeReid Kleckner    if (RD && !RD->hasAttr<MSInheritanceAttr>()) {
179884e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // Lock in the inheritance model on the first use of a member pointer.
179984e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // Otherwise we may disagree about the size at different points in the TU.
180084e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // FIXME: MSVC picks a model on the first use that needs to know the size,
180184e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // rather than on the first mention of the type, e.g. typedefs.
1802e93e2552e76ab704ec85919cc2c76f02b8b081eeReid Kleckner      if (RequireCompleteType(Loc, Class, 0) && !RD->isBeingDefined()) {
180384e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        // We know it doesn't have an attribute and it's incomplete, so use the
180484e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        // unspecified inheritance model.  If we're in the record body, we can
180584e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        // figure out the inheritance model.
180684e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(),
180784e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner             E = RD->redecls_end(); I != E; ++I) {
180884e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner          I->addAttr(::new (Context) UnspecifiedInheritanceAttr(
180984e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner              RD->getSourceRange(), Context));
181084e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        }
181184e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      }
181284e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner    }
181384e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  }
1814679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos
1815ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  // FIXME: Adjust member function pointer calling conventions.
1816ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
18172865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
1818949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
18191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18209a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
18219a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
18229a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
18239a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
1824efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Loc The source location, used for diagnostics.
18259a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
18269a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
18279a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
18289a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
18299a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
18309a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
183191cbbbf506c892a26d4301e2b3ccd377b0938817Chad RosierQualType Sema::BuildBlockPointerType(QualType T,
18321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
18339a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
18340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
18359a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
18369a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
18379a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
18381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18392865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
18409a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
18419a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
1842b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1843b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
18443f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
1845a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
18463f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
18473f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
18483f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1849a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
1850f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
1851e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
1852a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
1853e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
18541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1855a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
1856e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
1857e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
1858e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
1859a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
1860a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
1861a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            unsigned chunkIndex);
1862a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis
1863f85e193739c953358c865005855253af4f68a497John McCall/// Given that this is the declaration of a parameter under ARC,
1864f85e193739c953358c865005855253af4f68a497John McCall/// attempt to infer attributes and such for pointer-to-whatever
1865f85e193739c953358c865005855253af4f68a497John McCall/// types.
1866f85e193739c953358c865005855253af4f68a497John McCallstatic void inferARCWriteback(TypeProcessingState &state,
1867f85e193739c953358c865005855253af4f68a497John McCall                              QualType &declSpecType) {
1868f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
1869f85e193739c953358c865005855253af4f68a497John McCall  Declarator &declarator = state.getDeclarator();
1870f85e193739c953358c865005855253af4f68a497John McCall
1871f85e193739c953358c865005855253af4f68a497John McCall  // TODO: should we care about decl qualifiers?
1872f85e193739c953358c865005855253af4f68a497John McCall
1873f85e193739c953358c865005855253af4f68a497John McCall  // Check whether the declarator has the expected form.  We walk
1874f85e193739c953358c865005855253af4f68a497John McCall  // from the inside out in order to make the block logic work.
1875f85e193739c953358c865005855253af4f68a497John McCall  unsigned outermostPointerIndex = 0;
1876f85e193739c953358c865005855253af4f68a497John McCall  bool isBlockPointer = false;
1877f85e193739c953358c865005855253af4f68a497John McCall  unsigned numPointers = 0;
1878f85e193739c953358c865005855253af4f68a497John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
1879f85e193739c953358c865005855253af4f68a497John McCall    unsigned chunkIndex = i;
1880f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex);
1881f85e193739c953358c865005855253af4f68a497John McCall    switch (chunk.Kind) {
1882f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Paren:
1883f85e193739c953358c865005855253af4f68a497John McCall      // Ignore parens.
1884f85e193739c953358c865005855253af4f68a497John McCall      break;
1885f85e193739c953358c865005855253af4f68a497John McCall
1886f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Reference:
1887f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Pointer:
1888f85e193739c953358c865005855253af4f68a497John McCall      // Count the number of pointers.  Treat references
1889f85e193739c953358c865005855253af4f68a497John McCall      // interchangeably as pointers; if they're mis-ordered, normal
1890f85e193739c953358c865005855253af4f68a497John McCall      // type building will discover that.
1891f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1892f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1893f85e193739c953358c865005855253af4f68a497John McCall      break;
1894f85e193739c953358c865005855253af4f68a497John McCall
1895f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::BlockPointer:
1896f85e193739c953358c865005855253af4f68a497John McCall      // If we have a pointer to block pointer, that's an acceptable
1897f85e193739c953358c865005855253af4f68a497John McCall      // indirect reference; anything else is not an application of
1898f85e193739c953358c865005855253af4f68a497John McCall      // the rules.
1899f85e193739c953358c865005855253af4f68a497John McCall      if (numPointers != 1) return;
1900f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1901f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1902f85e193739c953358c865005855253af4f68a497John McCall      isBlockPointer = true;
1903f85e193739c953358c865005855253af4f68a497John McCall
1904f85e193739c953358c865005855253af4f68a497John McCall      // We don't care about pointer structure in return values here.
1905f85e193739c953358c865005855253af4f68a497John McCall      goto done;
1906f85e193739c953358c865005855253af4f68a497John McCall
1907f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Array: // suppress if written (id[])?
1908f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Function:
1909f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::MemberPointer:
1910f85e193739c953358c865005855253af4f68a497John McCall      return;
1911f85e193739c953358c865005855253af4f68a497John McCall    }
1912f85e193739c953358c865005855253af4f68a497John McCall  }
1913f85e193739c953358c865005855253af4f68a497John McCall done:
1914f85e193739c953358c865005855253af4f68a497John McCall
1915f85e193739c953358c865005855253af4f68a497John McCall  // If we have *one* pointer, then we want to throw the qualifier on
1916f85e193739c953358c865005855253af4f68a497John McCall  // the declaration-specifiers, which means that it needs to be a
1917f85e193739c953358c865005855253af4f68a497John McCall  // retainable object type.
1918f85e193739c953358c865005855253af4f68a497John McCall  if (numPointers == 1) {
1919f85e193739c953358c865005855253af4f68a497John McCall    // If it's not a retainable object type, the rule doesn't apply.
1920f85e193739c953358c865005855253af4f68a497John McCall    if (!declSpecType->isObjCRetainableType()) return;
1921f85e193739c953358c865005855253af4f68a497John McCall
1922f85e193739c953358c865005855253af4f68a497John McCall    // If it already has lifetime, don't do anything.
1923f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType.getObjCLifetime()) return;
1924f85e193739c953358c865005855253af4f68a497John McCall
1925f85e193739c953358c865005855253af4f68a497John McCall    // Otherwise, modify the type in-place.
1926f85e193739c953358c865005855253af4f68a497John McCall    Qualifiers qs;
192791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1928f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType->isObjCARCImplicitlyUnretainedType())
1929f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
1930f85e193739c953358c865005855253af4f68a497John McCall    else
1931f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing);
1932f85e193739c953358c865005855253af4f68a497John McCall    declSpecType = S.Context.getQualifiedType(declSpecType, qs);
1933f85e193739c953358c865005855253af4f68a497John McCall
1934f85e193739c953358c865005855253af4f68a497John McCall  // If we have *two* pointers, then we want to throw the qualifier on
1935f85e193739c953358c865005855253af4f68a497John McCall  // the outermost pointer.
1936f85e193739c953358c865005855253af4f68a497John McCall  } else if (numPointers == 2) {
1937f85e193739c953358c865005855253af4f68a497John McCall    // If we don't have a block pointer, we need to check whether the
1938f85e193739c953358c865005855253af4f68a497John McCall    // declaration-specifiers gave us something that will turn into a
1939f85e193739c953358c865005855253af4f68a497John McCall    // retainable object pointer after we slap the first pointer on it.
1940f85e193739c953358c865005855253af4f68a497John McCall    if (!isBlockPointer && !declSpecType->isObjCObjectType())
1941f85e193739c953358c865005855253af4f68a497John McCall      return;
1942f85e193739c953358c865005855253af4f68a497John McCall
1943f85e193739c953358c865005855253af4f68a497John McCall    // Look for an explicit lifetime attribute there.
1944f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex);
19451c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis    if (chunk.Kind != DeclaratorChunk::Pointer &&
19461c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis        chunk.Kind != DeclaratorChunk::BlockPointer)
19471c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis      return;
1948f85e193739c953358c865005855253af4f68a497John McCall    for (const AttributeList *attr = chunk.getAttrs(); attr;
1949f85e193739c953358c865005855253af4f68a497John McCall           attr = attr->getNext())
19508e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt      if (attr->getKind() == AttributeList::AT_ObjCOwnership)
1951f85e193739c953358c865005855253af4f68a497John McCall        return;
1952f85e193739c953358c865005855253af4f68a497John McCall
1953a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis    transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing,
1954a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                          outermostPointerIndex);
1955f85e193739c953358c865005855253af4f68a497John McCall
1956f85e193739c953358c865005855253af4f68a497John McCall  // Any other number of pointers/references does not trigger the rule.
1957f85e193739c953358c865005855253af4f68a497John McCall  } else return;
1958f85e193739c953358c865005855253af4f68a497John McCall
1959f85e193739c953358c865005855253af4f68a497John McCall  // TODO: mark whether we did this inference?
1960f85e193739c953358c865005855253af4f68a497John McCall}
1961f85e193739c953358c865005855253af4f68a497John McCall
19628c952cd40ccec9d720931f27e7d722fed207d536Richard Smithstatic void diagnoseIgnoredQualifiers(
19638c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    Sema &S, unsigned Quals,
19648c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation FallbackLoc,
19658c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation ConstQualLoc = SourceLocation(),
19668c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation VolatileQualLoc = SourceLocation(),
19678c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation RestrictQualLoc = SourceLocation(),
19688c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation AtomicQualLoc = SourceLocation()) {
1969eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith  if (!Quals)
1970eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith    return;
1971eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith
19728c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  const SourceManager &SM = S.getSourceManager();
19738c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19748c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  struct Qual {
19758c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    unsigned Mask;
19768c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    const char *Name;
19778c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation Loc;
19788c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  } const QualKinds[4] = {
19798c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_const, "const", ConstQualLoc },
19808c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_volatile, "volatile", VolatileQualLoc },
19818c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_restrict, "restrict", RestrictQualLoc },
19828c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_atomic, "_Atomic", AtomicQualLoc }
19838c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  };
19848c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
198530d237556fdd29b5075c990da953116225b95d9dRobert Wilhelm  SmallString<32> QualStr;
1986d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  unsigned NumQuals = 0;
1987d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  SourceLocation Loc;
19888c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  FixItHint FixIts[4];
19898c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19908c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // Build a string naming the redundant qualifiers.
19918c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  for (unsigned I = 0; I != 4; ++I) {
19928c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    if (Quals & QualKinds[I].Mask) {
19938c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if (!QualStr.empty()) QualStr += ' ';
19948c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      QualStr += QualKinds[I].Name;
19958c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19968c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // If we have a location for the qualifier, offer a fixit.
19978c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      SourceLocation QualLoc = QualKinds[I].Loc;
19988c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if (!QualLoc.isInvalid()) {
19998c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        FixIts[NumQuals] = FixItHint::CreateRemoval(QualLoc);
20008c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        if (Loc.isInvalid() || SM.isBeforeInTranslationUnit(QualLoc, Loc))
20018c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          Loc = QualLoc;
20028c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      }
2003d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
20048c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      ++NumQuals;
20058c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
20068c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  }
2007d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
20088c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  S.Diag(Loc.isInvalid() ? FallbackLoc : Loc, diag::warn_qual_return_type)
20098c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    << QualStr << NumQuals << FixIts[0] << FixIts[1] << FixIts[2] << FixIts[3];
20108c952cd40ccec9d720931f27e7d722fed207d536Richard Smith}
2011a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg
20128c952cd40ccec9d720931f27e7d722fed207d536Richard Smith// Diagnose pointless type qualifiers on the return type of a function.
20138c952cd40ccec9d720931f27e7d722fed207d536Richard Smithstatic void diagnoseIgnoredFunctionQualifiers(Sema &S, QualType RetTy,
20148c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                              Declarator &D,
20158c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                              unsigned FunctionChunkIndex) {
20168c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  if (D.getTypeObject(FunctionChunkIndex).Fun.hasTrailingReturnType()) {
20178c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    // FIXME: TypeSourceInfo doesn't preserve location information for
20188c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    // qualifiers.
2019eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith    diagnoseIgnoredQualifiers(S, RetTy.getLocalCVRQualifiers(),
20208c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                              D.getIdentifierLoc());
20218c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    return;
2022d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
20238c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
20248c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  for (unsigned OuterChunkIndex = FunctionChunkIndex + 1,
20258c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                End = D.getNumTypeObjects();
20268c952cd40ccec9d720931f27e7d722fed207d536Richard Smith       OuterChunkIndex != End; ++OuterChunkIndex) {
20278c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    DeclaratorChunk &OuterChunk = D.getTypeObject(OuterChunkIndex);
20288c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    switch (OuterChunk.Kind) {
20298c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Paren:
20308c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      continue;
20318c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
20328c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Pointer: {
20338c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      DeclaratorChunk::PointerTypeInfo &PTI = OuterChunk.Ptr;
20348c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      diagnoseIgnoredQualifiers(
20358c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          S, PTI.TypeQuals,
20368c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation(),
20378c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
20388c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
20398c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
20408c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.AtomicQualLoc));
20418c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      return;
20428c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
20438c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
20448c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Function:
20458c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::BlockPointer:
20468c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Reference:
20478c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Array:
20488c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::MemberPointer:
20498c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // FIXME: We can't currently provide an accurate source location and a
20508c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // fix-it hint for these.
2051eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith      unsigned AtomicQual = RetTy->isAtomicType() ? DeclSpec::TQ_atomic : 0;
20528c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      diagnoseIgnoredQualifiers(S, RetTy.getCVRQualifiers() | AtomicQual,
20538c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                D.getIdentifierLoc());
20548c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      return;
20558c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
20568c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
20578c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    llvm_unreachable("unknown declarator chunk kind");
20584cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
2059d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
20608c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // If the qualifiers come from a conversion function type, don't diagnose
20618c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // them -- they're not necessarily redundant, since such a conversion
20628c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // operator can be explicitly called as "x.operator const int()".
20638c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  if (D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId)
20648c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    return;
2065d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
20668c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // Just parens all the way out to the decl specifiers. Diagnose any qualifiers
20678c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // which are present there.
2068eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith  diagnoseIgnoredQualifiers(S, D.getDeclSpec().getTypeQualifiers(),
20698c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getIdentifierLoc(),
20708c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getConstSpecLoc(),
20718c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getVolatileSpecLoc(),
20728c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getRestrictSpecLoc(),
20738c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getAtomicSpecLoc());
2074d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth}
2075d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
20768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
20778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                             TypeSourceInfo *&ReturnTypeInfo) {
20788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &SemaRef = state.getSema();
20798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
2080930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
20818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ReturnTypeInfo = 0;
2082711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
20838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // The TagDecl owned by the DeclSpec.
20848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TagDecl *OwnedTagDecl = 0;
20858999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl
208660e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  bool ContainsPlaceholderType = false;
208760e141e1f87211ca831de6821003d80fe20a06f3Richard Smith
20883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
208998a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian  case UnqualifiedId::IK_ImplicitSelfParam:
20903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
20918999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  case UnqualifiedId::IK_Identifier:
20920486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
20933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
20948cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = ConvertDeclSpecToType(state);
209560e141e1f87211ca831de6821003d80fe20a06f3Richard Smith    ContainsPlaceholderType = D.getDeclSpec().containsPlaceholderType();
209691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2097591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
2098d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
209915987970eeaa1842c29ec8797affd1c1dea05585Abramo Bagnara      // Owned declaration is embedded in declarator.
2100d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl->setEmbeddedInDeclarator(true);
2101591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
2102930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
2103930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
21043f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
21050efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
21063f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
2107930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
210891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // "void" instead.
21098cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = SemaRef.Context.VoidTy;
2110a78a640c5f59720f2c2b8034eca4fbf8525d9026Rafael Espindola    if (AttributeList *attrs = D.getDeclSpec().getAttributes().getList())
2111f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclSpec, attrs);
2112930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
211348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
211448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
211548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
211648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
211791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId,
21188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  &ReturnTypeInfo);
211960e141e1f87211ca831de6821003d80fe20a06f3Richard Smith    ContainsPlaceholderType = T->getContainedAutoType();
212048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
2121930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
2122dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
2123711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
2124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
2125711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2126d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
2127d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // In C++11, a function declarator using 'auto' must have a trailing return
21288110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // type (this is checked later) and we can skip this. In other languages
21298110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // using auto, we need to check regardless.
2130fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali  // C++14 In generic lambdas allow 'auto' in their parameters.
213160e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  if (ContainsPlaceholderType &&
213280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      (!SemaRef.getLangOpts().CPlusPlus11 || !D.isFunctionDeclarator())) {
2133baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
21341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2135baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
2136baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
2137b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("K&R type lists aren't allowed in C++");
2138f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2139f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman      llvm_unreachable("Can't specify a type specifier in lambda grammar");
2140cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2141cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
2142baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
2143fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      Error = 0;
2144fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      break;
2145fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    case Declarator::LambdaExprParameterContext:
2146fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      if (!(SemaRef.getLangOpts().CPlusPlus1y
2147fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali              && D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto))
2148fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        Error = 14;
2149baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2150baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
21517a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)
21527a614d8380297fcd2bc23986241905d97222948cRichard Smith        break;
21538cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) {
2154eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie      case TTK_Enum: llvm_unreachable("unhandled tag kind");
2155465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
2156465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
2157465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
21586666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      case TTK_Interface: Error = 4; /* Interface member */ break;
21591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
2160baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2161baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
216217b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
21636666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 5; // Exception declaration
2164baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2165baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
21666666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 6; // Template parameter
2167baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2168baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
21696666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 7; // Block literal
217034b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
217134b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TemplateTypeArgContext:
21726666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 8; // Template type argument
217334b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
2174162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
21753e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
21766666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 10; // Type alias
2177162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      break;
21787796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
217960e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      if (!SemaRef.getLangOpts().CPlusPlus1y)
218060e141e1f87211ca831de6821003d80fe20a06f3Richard Smith        Error = 11; // Function return type
21817796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
218214f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    case Declarator::ConversionIdContext:
218360e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      if (!SemaRef.getLangOpts().CPlusPlus1y)
218460e141e1f87211ca831de6821003d80fe20a06f3Richard Smith        Error = 12; // conversion-type-id
218514f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      break;
218634b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TypeNameContext:
218714f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      Error = 13; // Generic
2188baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2189baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
2190baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
2191baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
2192baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
21930b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
2194baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2195baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
2196baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
2197ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith    if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
21986666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 9;
2199ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith
22008110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    // In Objective-C it is an error to use 'auto' on a function declarator.
22018110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    if (D.isFunctionDeclarator())
22026666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11;
22038110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith
2204d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.spec.auto]p2: 'auto' is always fine if the declarator
2205e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // contains a trailing return type. That is only legal at the outermost
2206e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // level. Check all declarator chunks (outermost first) anyway, to give
2207e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // better diagnostics.
220880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (SemaRef.getLangOpts().CPlusPlus11 && Error != -1) {
2209e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2210e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned chunkIndex = e - i - 1;
2211e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        state.setCurrentChunkIndex(chunkIndex);
2212e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2213e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (DeclType.Kind == DeclaratorChunk::Function) {
2214e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
221554655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (FTI.hasTrailingReturnType()) {
2216e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Error = -1;
2217e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            break;
2218e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          }
2219e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        }
2220e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2221e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    }
2222e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
222314f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    SourceRange AutoRange = D.getDeclSpec().getTypeSpecTypeLoc();
222414f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    if (D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId)
222514f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      AutoRange = D.getName().getSourceRange();
222614f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith
2227baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
2228fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      const bool IsDeclTypeAuto =
2229fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali          D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_decltype_auto;
223014f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      SemaRef.Diag(AutoRange.getBegin(), diag::err_auto_not_allowed)
2231fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        << IsDeclTypeAuto << Error << AutoRange;
22328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = SemaRef.Context.IntTy;
2233baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
22340aa86c0463a881be85fd34e04c7de3379997621dRichard Smith    } else
223514f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      SemaRef.Diag(AutoRange.getBegin(),
223614f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith                   diag::warn_cxx98_compat_auto_type_specifier)
223714f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith        << AutoRange;
2238baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
22398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
22404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().CPlusPlus &&
22415e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall      OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) {
22428cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // Check the contexts where C++ forbids the declaration of a new class
22438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // or enumeration in a type-specifier-seq.
22448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    switch (D.getContext()) {
22457796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
22467796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // Class and enumeration definitions are syntactically not allowed in
22477796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // trailing return types.
22487796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      llvm_unreachable("parser should not have allowed this");
22497796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
22508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::FileContext:
22518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::MemberContext:
22528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockContext:
22538cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ForContext:
22548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockLiteralContext:
2255f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2256d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // C++11 [dcl.type]p3:
22578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   A type-specifier-seq shall not define a class or enumeration unless
22588cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   it appears in the type-id of an alias-declaration (7.1.3) that is not
22598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   the declaration of a template-declaration.
22608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasDeclContext:
22618cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasTemplateContext:
22638cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_alias_template)
22658cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2266601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22678cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22688cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TypeNameContext:
226914f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    case Declarator::ConversionIdContext:
22708cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateParamContext:
22718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXNewContext:
22728cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXCatchContext:
22738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ObjCCatchContext:
22748cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateTypeArgContext:
22758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_type_specifier)
22778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2278601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::PrototypeContext:
2281fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    case Declarator::LambdaExprParameterContext:
2282cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2283cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
22848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::KNRTypeListContext:
22858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ [dcl.fct]p6:
22868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   Types shall not be defined in return or parameter types.
22878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_param_type)
22898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2290601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22918cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22928cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ConditionContext:
22938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ 6.4p2:
22948cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // The type-specifier-seq shall not contain typedef and shall not declare
22958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // a new class or enumeration.
22968cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_condition);
2298601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22998cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
23008cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    }
23018cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  }
23028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
23038cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return T;
23048cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
23058cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2306a08c2fb74ef823c185619ecc532f8fced6a1982fBenjamin Kramerstatic std::string getFunctionQualifiersAsString(const FunctionProtoType *FnTy){
2307d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  std::string Quals =
2308d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
2309d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2310d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (FnTy->getRefQualifier()) {
2311d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_None:
2312d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2313d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2314d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_LValue:
2315d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2316d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2317d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += '&';
2318d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2319d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2320d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_RValue:
2321d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2322d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2323d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += "&&";
2324d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2325d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2326d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2327d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  return Quals;
2328d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2329d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2330d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// Check that the function type T, which has a cv-qualifier or a ref-qualifier,
2331d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// can be contained within the declarator chunk DeclType, and produce an
2332d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// appropriate diagnostic if not.
2333d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smithstatic void checkQualifiedFunction(Sema &S, QualType T,
2334d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                   DeclaratorChunk &DeclType) {
2335d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++98 [dcl.fct]p4 / C++11 [dcl.fct]p6: a function type with a
2336d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // cv-qualifier or a ref-qualifier can only appear at the topmost level
2337d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // of a type.
2338d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  int DiagKind = -1;
2339d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (DeclType.Kind) {
2340d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Paren:
2341d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::MemberPointer:
2342d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases are permitted.
2343d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2344d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Array:
2345d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Function:
2346d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases don't allow function types at all; no need to diagnose the
2347d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // qualifiers separately.
2348d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2349d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::BlockPointer:
2350d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 0;
2351d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2352d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Pointer:
2353d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 1;
2354d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2355d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Reference:
2356d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 2;
2357d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2358d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2359d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2360d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  assert(DiagKind != -1);
2361d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  S.Diag(DeclType.Loc, diag::err_compound_qualified_function_type)
2362d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << DiagKind << isa<FunctionType>(T.IgnoreParens()) << T
2363d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << getFunctionQualifiersAsString(T->castAs<FunctionProtoType>());
2364d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2365d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2366b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// Produce an approprioate diagnostic for an ambiguity between a function
2367b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// declarator and a C++ direct-initializer.
2368b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smithstatic void warnAboutAmbiguousFunction(Sema &S, Declarator &D,
2369b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith                                       DeclaratorChunk &DeclType, QualType RT) {
2370b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2371b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  assert(FTI.isAmbiguous && "no direct-initializer / function ambiguity");
2372b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2373b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // If the return type is void there is no ambiguity.
2374b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isVoidType())
2375b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2376b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2377b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a non-class type can have at most one argument.
2378b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!RT->isRecordType() && FTI.NumArgs > 1)
2379b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2380b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2381b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a reference must have exactly one argument.
2382b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isReferenceType() && FTI.NumArgs != 1)
2383b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2384b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2385b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Only warn if this declarator is declaring a function at block scope, and
2386b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // doesn't have a storage class (such as 'extern') specified.
2387b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!D.isFunctionDeclarator() ||
2388b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      D.getFunctionDefinitionKind() != FDK_Declaration ||
2389b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      !S.CurContext->isFunctionOrMethod() ||
2390d2615cc53b916e8aae45783ca7113b93de515ce3Rafael Espindola      D.getDeclSpec().getStorageClassSpec()
2391b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        != DeclSpec::SCS_unspecified)
2392b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2393b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2394b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Inside a condition, a direct initializer is not permitted. We allow one to
2395b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // be parsed in order to give better diagnostics in condition parsing.
2396b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (D.getContext() == Declarator::ConditionContext)
2397b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2398b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2399b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  SourceRange ParenRange(DeclType.Loc, DeclType.EndLoc);
2400b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2401d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  S.Diag(DeclType.Loc,
2402d64effc4e31044c05d6e4400150edb26e914983aRichard Smith         FTI.NumArgs ? diag::warn_parens_disambiguated_as_function_declaration
2403d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                     : diag::warn_empty_parens_are_function_decl)
2404d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    << ParenRange;
2405d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2406d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // If the declaration looks like:
2407d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   T var1,
2408d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   f();
2409d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // and name lookup finds a function named 'f', then the ',' was
2410d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // probably intended to be a ';'.
2411d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (!D.isFirstDeclarator() && D.getIdentifier()) {
2412d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Comma(D.getCommaLoc(), S.SourceMgr);
2413d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Name(D.getIdentifierLoc(), S.SourceMgr);
2414d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    if (Comma.getFileID() != Name.getFileID() ||
2415d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) {
2416d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      LookupResult Result(S, D.getIdentifier(), SourceLocation(),
2417d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                          Sema::LookupOrdinaryName);
2418d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      if (S.LookupName(Result, S.getCurScope()))
2419d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        S.Diag(D.getCommaLoc(), diag::note_empty_parens_function_call)
2420d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << FixItHint::CreateReplacement(D.getCommaLoc(), ";")
2421d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << D.getIdentifier();
2422d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    }
2423d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  }
2424b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2425d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (FTI.NumArgs > 0) {
2426d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration with parameters, eg. "T var(T());", suggest adding parens
2427d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // around the first parameter to turn the declaration into a variable
2428d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2429b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceRange Range = FTI.ArgInfo[0].Param->getSourceRange();
2430b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation B = Range.getBegin();
2431b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation E = S.PP.getLocForEndOfToken(Range.getEnd());
2432b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // FIXME: Maybe we should suggest adding braces instead of parens
2433b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // in C++11 for classes that don't have an initializer_list constructor.
2434b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    S.Diag(B, diag::note_additional_parens_for_variable_declaration)
2435b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(B, "(")
2436b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(E, ")");
2437d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  } else {
2438d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration without parameters, eg. "T var();", suggest replacing the
2439d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // parens with an initializer to turn the declaration into a variable
2440d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2441b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
2442d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2443b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // Empty parens mean value-initialization, and no parens mean
2444b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // default initialization. These are equivalent if the default
2445b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // constructor is user-provided or if zero-initialization is a
2446b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // no-op.
2447b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    if (RD && RD->hasDefinition() &&
2448b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        (RD->isEmpty() || RD->hasUserProvidedDefaultConstructor()))
2449b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor)
2450b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        << FixItHint::CreateRemoval(ParenRange);
2451b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    else {
24528adf837adc65b55a3f74643c02c1ee077dc26f06Richard Smith      std::string Init =
24538adf837adc65b55a3f74643c02c1ee077dc26f06Richard Smith          S.getFixItZeroInitializerForType(RT, ParenRange.getBegin());
245480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      if (Init.empty() && S.LangOpts.CPlusPlus11)
2455b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        Init = "{}";
2456b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (!Init.empty())
2457b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        S.Diag(DeclType.Loc, diag::note_empty_parens_zero_initialize)
2458b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith          << FixItHint::CreateReplacement(ParenRange, Init);
2459b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    }
2460b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  }
2461b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith}
2462b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2463ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner/// Helper for figuring out the default CC for a function declarator type.  If
2464ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner/// this is the outermost chunk, then we can determine the CC from the
2465ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner/// declarator context.  If not, then this could be either a member function
2466ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner/// type or normal function type.
2467ef072033876e295ec5d3402f8730a3ae358ad815Reid Klecknerstatic CallingConv
2468ef072033876e295ec5d3402f8730a3ae358ad815Reid KlecknergetCCForDeclaratorChunk(Sema &S, Declarator &D,
2469ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner                        const DeclaratorChunk::FunctionTypeInfo &FTI,
2470ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner                        unsigned ChunkIndex) {
2471ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  assert(D.getTypeObject(ChunkIndex).Kind == DeclaratorChunk::Function);
2472ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
2473ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  bool IsCXXInstanceMethod = false;
2474ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
2475ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  if (S.getLangOpts().CPlusPlus) {
2476ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // Look inwards through parentheses to see if this chunk will form a
2477ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // member pointer type or if we're the declarator.  Any type attributes
2478ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // between here and there will override the CC we choose here.
2479ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    unsigned I = ChunkIndex;
2480ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    bool FoundNonParen = false;
2481ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    while (I && !FoundNonParen) {
2482ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      --I;
2483ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      if (D.getTypeObject(I).Kind != DeclaratorChunk::Paren)
2484ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        FoundNonParen = true;
2485ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    }
2486ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
2487ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    if (FoundNonParen) {
2488ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      // If we're not the declarator, we're a regular function type unless we're
2489ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      // in a member pointer.
2490ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      IsCXXInstanceMethod =
2491ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner          D.getTypeObject(I).Kind == DeclaratorChunk::MemberPointer;
2492ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    } else {
2493ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      // We're the innermost decl chunk, so must be a function declarator.
2494ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      assert(D.isFunctionDeclarator());
2495ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
2496ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      // If we're inside a record, we're declaring a method, but it could be
2497d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner      // explicitly or implicitly static.
2498ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      IsCXXInstanceMethod =
2499d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner          D.isFirstDeclarationOfMember() &&
2500d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner          D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
2501d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner          !D.isStaticMember();
2502ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    }
2503ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  }
2504ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
2505ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  return S.Context.getDefaultCallingConvention(FTI.isVariadic,
2506ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner                                               IsCXXInstanceMethod);
2507ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner}
2508ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
25098cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
25108cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                QualType declSpecType,
25118cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                TypeSourceInfo *TInfo) {
25128cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
25138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = declSpecType;
25148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
25158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
25168cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ASTContext &Context = S.Context;
25174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &LangOpts = S.getLangOpts();
25188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2519cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
2520cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
2521cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
2522cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
25231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2524162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  // Does this declaration declare a typedef-name?
2525162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  bool IsTypedefName =
2526162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef ||
25273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasDeclContext ||
25283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasTemplateContext;
2529162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2530d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // Does T refer to a function type with a cv-qualifier or a ref-qualifier?
2531d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  bool IsQualifiedFunction = T->isFunctionProtoType() &&
2532d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      (T->castAs<FunctionProtoType>()->getTypeQuals() != 0 ||
2533d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith       T->castAs<FunctionProtoType>()->getRefQualifier() != RQ_None);
2534d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2535a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  // If T is 'decltype(auto)', the only declarators we can have are parens
2536a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  // and at most one function declarator if this is a function declaration.
2537a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  if (const AutoType *AT = T->getAs<AutoType>()) {
2538a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    if (AT->isDecltypeAuto()) {
2539a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) {
2540a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        unsigned Index = E - I - 1;
2541a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        DeclaratorChunk &DeclChunk = D.getTypeObject(Index);
2542a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        unsigned DiagId = diag::err_decltype_auto_compound_type;
2543a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        unsigned DiagKind = 0;
2544a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        switch (DeclChunk.Kind) {
2545a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Paren:
2546a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          continue;
2547a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Function: {
2548a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          unsigned FnIndex;
2549a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          if (D.isFunctionDeclarationContext() &&
2550a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith              D.isFunctionDeclarator(FnIndex) && FnIndex == Index)
2551a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            continue;
2552a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagId = diag::err_decltype_auto_function_declarator_not_declaration;
2553a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2554a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        }
2555a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Pointer:
2556a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::BlockPointer:
2557a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::MemberPointer:
2558a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagKind = 0;
2559a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2560a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Reference:
2561a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagKind = 1;
2562a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2563a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Array:
2564a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagKind = 2;
2565a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2566a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        }
2567a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
2568a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        S.Diag(DeclChunk.Loc, DiagId) << DiagKind;
2569a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        D.setInvalidType(true);
2570a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        break;
2571a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      }
2572a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    }
2573a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  }
2574a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
257598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
257698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
257798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
25788ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2579711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
2580711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
2581711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2582d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction) {
2583d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      checkQualifiedFunction(S, T, DeclType);
2584d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = DeclType.Kind == DeclaratorChunk::Paren;
2585d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    }
25865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
2587075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
25888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildParenType(T);
2589075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
25905618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
25919af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
25929af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
25938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_blocks_disable);
25941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
25962865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
25978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
25985618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
25995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
26006a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
26016a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
26028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
26038cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
26046a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
26056a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
26066a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
26078cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.ObjC1 && T->getAs<ObjCObjectType>()) {
2608c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
26092865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
26108cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
261114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
261214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
26138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildPointerType(T, DeclType.Loc, Name);
26142865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
26158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
2616711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
26175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
26180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
26196a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
26206a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
26218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
26228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
26236a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
26246a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
26256a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
26268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
26272865474261a608c7873b87ba4af110d17907896dJohn McCall
26282865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
26292865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
26308cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
26315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
26320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
26335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
26346a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
26356a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
26368cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
26378cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
26386a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
26396a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
26406a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
2641fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
264294f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
26435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
26445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
26455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
26465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
26475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
26485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
26495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
2650c05a94b7accd4035bf5d5897c434c445b22da855John McCall      if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
2651f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
2652f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
2653f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
26548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
2655f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
2656f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
2657f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
26587f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
26597f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // C99 6.7.5.2p1: The optional type qualifiers and the keyword static
26607f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // shall appear only in a declaration of a function parameter with an
26617f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // array type, ...
26627f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      if (ASM == ArrayType::Static || ATI.TypeQuals) {
266399570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay        if (!(D.isPrototypeContext() ||
266499570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay              D.getContext() == Declarator::KNRTypeListContext)) {
26657f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          S.Diag(DeclType.Loc, diag::err_array_static_outside_prototype) <<
26667f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
26677f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Remove the 'static' and the type qualifiers.
26687f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          if (ASM == ArrayType::Static)
26697f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ASM = ArrayType::Normal;
26707f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          ATI.TypeQuals = 0;
26717f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          D.setInvalidType(true);
26727f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
26737f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
26747f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // C99 6.7.5.2p1: ... and then only in the outermost array type
26757f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // derivation.
26767f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        unsigned x = chunkIndex;
26777f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        while (x != 0) {
26787f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Walk outwards along the declarator chunks.
26797f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          x--;
26807f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          const DeclaratorChunk &DC = D.getTypeObject(x);
26817f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          switch (DC.Kind) {
26827f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Paren:
26837f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            continue;
26847f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Array:
26857f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Pointer:
26867f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Reference:
26877f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::MemberPointer:
26887f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            S.Diag(DeclType.Loc, diag::err_array_static_not_outermost) <<
26897f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
26907f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            if (ASM == ArrayType::Static)
26917f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              ASM = ArrayType::Normal;
26927f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ATI.TypeQuals = 0;
26937f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            D.setInvalidType(true);
26947f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
26957f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Function:
26967f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::BlockPointer:
26977f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            // These are invalid anyway, so just ignore.
26987f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
26997f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          }
27007f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
27017f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      }
2702fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      const AutoType *AT = T->getContainedAutoType();
2703fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // Allow arrays of auto if we are a generic lambda parameter.
2704fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // i.e. [](auto (&array)[5]) { return array[0]; }; OK
2705fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      if (AT && D.getContext() != Declarator::LambdaExprParameterContext) {
2706a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        // We've already diagnosed this for decltype(auto).
2707dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        if (!AT->isDecltypeAuto())
2708a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          S.Diag(DeclType.Loc, diag::err_illegal_decl_array_of_auto)
2709a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            << getPrintableNameForEntity(Name) << T;
2710dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        T = QualType();
2711dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        break;
2712a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      }
2713a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
27148ac2c66a1442985091c5ec2b33ce6d3df3bcb529Eli Friedman      T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
27158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                           SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
27165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
27175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2718f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
27195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
27205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
27215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
27225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2723d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = FTI.TypeQuals || FTI.hasRefQualifier();
27243cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
272534b41d939a1328f484511c6002ba2456db879a29Richard Smith      // Check for auto functions and trailing return type and adjust the
272634b41d939a1328f484511c6002ba2456db879a29Richard Smith      // return type accordingly.
272734b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!D.isInvalidType()) {
272834b41d939a1328f484511c6002ba2456db879a29Richard Smith        // trailing-return-type is only required if we're declaring a function,
272934b41d939a1328f484511c6002ba2456db879a29Richard Smith        // and not, for instance, a pointer to a function.
273034b41d939a1328f484511c6002ba2456db879a29Richard Smith        if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
273160e141e1f87211ca831de6821003d80fe20a06f3Richard Smith            !FTI.hasTrailingReturnType() && chunkIndex == 0 &&
273260e141e1f87211ca831de6821003d80fe20a06f3Richard Smith            !S.getLangOpts().CPlusPlus1y) {
27338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
273434b41d939a1328f484511c6002ba2456db879a29Richard Smith               diag::err_auto_missing_trailing_return);
273534b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = Context.IntTy;
273634b41d939a1328f484511c6002ba2456db879a29Richard Smith          D.setInvalidType(true);
273754655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        } else if (FTI.hasTrailingReturnType()) {
2738e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          // T must be exactly 'auto' at this point. See CWG issue 681.
2739e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (isa<ParenType>(T)) {
27408cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
2741e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith                 diag::err_trailing_return_in_parens)
2742e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith              << T << D.getDeclSpec().getSourceRange();
2743e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.setInvalidType(true);
2744f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman          } else if (D.getContext() != Declarator::LambdaExprContext &&
2745a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                     (T.hasQualifiers() || !isa<AutoType>(T) ||
2746a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                      cast<AutoType>(T)->isDecltypeAuto())) {
27478cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
274834b41d939a1328f484511c6002ba2456db879a29Richard Smith                 diag::err_trailing_return_without_auto)
274934b41d939a1328f484511c6002ba2456db879a29Richard Smith              << T << D.getDeclSpec().getSourceRange();
275034b41d939a1328f484511c6002ba2456db879a29Richard Smith            D.setInvalidType(true);
275134b41d939a1328f484511c6002ba2456db879a29Richard Smith          }
275254655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          T = S.GetTypeFromParser(FTI.getTrailingReturnType(), &TInfo);
275354655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (T.isNull()) {
275454655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            // An error occurred parsing the trailing return type.
275554655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            T = Context.IntTy;
275654655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            D.setInvalidType(true);
275754655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          }
275834b41d939a1328f484511c6002ba2456db879a29Richard Smith        }
275934b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
276034b41d939a1328f484511c6002ba2456db879a29Richard Smith
2761e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // C99 6.7.5.3p1: The return type may not be a function or array type.
2762e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // For conversion functions, we'll diagnose this particular error later.
2763e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      if ((T->isArrayType() || T->isFunctionType()) &&
2764e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
2765e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned diagID = diag::err_func_returning_array_function;
2766e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // Last processing chunk in block context means this function chunk
2767e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // represents the block.
2768e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (chunkIndex == 0 &&
2769e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.getContext() == Declarator::BlockLiteralContext)
2770e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          diagID = diag::err_block_returning_array_function;
27718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
2772e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        T = Context.IntTy;
2773e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        D.setInvalidType(true);
2774e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2775e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2776aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Do not allow returning half FP value.
2777aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // FIXME: This really should be in BuildFunctionType.
2778aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      if (T->isHalfType()) {
277919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        if (S.getLangOpts().OpenCL) {
278019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          if (!S.getOpenCLOptions().cl_khr_fp16) {
278119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            S.Diag(D.getIdentifierLoc(), diag::err_opencl_half_return) << T;
278219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            D.setInvalidType(true);
278319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          }
278419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        } else {
278519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          S.Diag(D.getIdentifierLoc(),
278619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            diag::err_parameters_retval_cannot_have_fp16_type) << 1;
278719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          D.setInvalidType(true);
278819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        }
2789aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      }
2790aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
2791ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      // Methods cannot return interface types. All ObjC objects are
2792ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      // passed by reference.
2793ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      if (T->isObjCObjectType()) {
2794ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        SourceLocation DiagLoc, FixitLoc;
2795ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        if (TInfo) {
2796ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          DiagLoc = TInfo->getTypeLoc().getLocStart();
2797ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          FixitLoc = S.PP.getLocForEndOfToken(TInfo->getTypeLoc().getLocEnd());
2798ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        } else {
2799ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          DiagLoc = D.getDeclSpec().getTypeSpecTypeLoc();
2800ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          FixitLoc = S.PP.getLocForEndOfToken(D.getDeclSpec().getLocEnd());
2801ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        }
2802ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        S.Diag(DiagLoc, diag::err_object_cannot_be_passed_returned_by_value)
2803ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          << 0 << T
2804ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          << FixItHint::CreateInsertion(FixitLoc, "*");
2805ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
2806ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        T = Context.getObjCObjectPointerType(T);
2807ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        if (TInfo) {
2808ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TypeLocBuilder TLB;
2809ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TLB.pushFullCopy(TInfo->getTypeLoc());
2810ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          ObjCObjectPointerTypeLoc TLoc = TLB.push<ObjCObjectPointerTypeLoc>(T);
2811ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TLoc.setStarLoc(FixitLoc);
2812ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TInfo = TLB.getTypeSourceInfo(Context, T);
2813ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        }
2814ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
2815ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        D.setInvalidType(true);
2816ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      }
2817ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
28185291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
28195291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
28208c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if ((T.getCVRQualifiers() || T->isAtomicType()) &&
28218c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          !(S.getLangOpts().CPlusPlus &&
28228c952cd40ccec9d720931f27e7d722fed207d536Richard Smith            (T->isDependentType() || T->isRecordType())))
28238c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        diagnoseIgnoredFunctionQualifiers(S, T, D, chunkIndex);
2824d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
282502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // Objective-C ARC ownership qualifiers are ignored on the function
282602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // return type (by type canonicalization). Complain if this attribute
282702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // was written here.
282802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      if (T.getQualifiers().hasObjCLifetime()) {
282902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        SourceLocation AttrLoc;
283002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (chunkIndex + 1 < D.getNumTypeObjects()) {
283102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
283202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr = ReturnTypeChunk.getAttrs();
283302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
283402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
283502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
283602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
283702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
283802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
283902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
284002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isInvalid()) {
284102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr
284202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor                 = D.getDeclSpec().getAttributes().getList();
284302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
284402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
284502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
284602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
284702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
284802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
284902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
285002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
285102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isValid()) {
285202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // The ownership attributes are almost always written via
285302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // the predefined
285402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // __strong/__weak/__autoreleasing/__unsafe_unretained.
285502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          if (AttrLoc.isMacroID())
285602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            AttrLoc = S.SourceMgr.getImmediateExpansionRange(AttrLoc).first;
285702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
285802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          S.Diag(AttrLoc, diag::warn_arc_lifetime_result_type)
285902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            << T.getQualifiers().getObjCLifetime();
286002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
286102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      }
286202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
28638cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
2864402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
2865402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
2866b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
28675e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall        if (Tag->isCompleteDefinition())
28688cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
2869402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
2870402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
2871402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
28723cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
28733cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
2874162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (IsTypedefName && FTI.getExceptionSpecType())
28758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef)
28763e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          << (D.getContext() == Declarator::AliasDeclContext ||
28773e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith              D.getContext() == Declarator::AliasTemplateContext);
28783cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
2879b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // If we see "T var();" or "T var(T());" at block scope, it is probably
2880b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // an attempt to initialize a variable, not a function declaration.
2881b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (FTI.isAmbiguous)
2882b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        warnAboutAmbiguousFunction(S, D, DeclType, T);
2883b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2884ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      FunctionType::ExtInfo EI(getCCForDeclaratorChunk(S, D, FTI, chunkIndex));
2885ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
28868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (!FTI.NumArgs && !FTI.isVariadic && !LangOpts.CPlusPlus) {
28872865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
2888ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        T = Context.getFunctionNoProtoType(T, EI);
28892865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
28902865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
28912865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
28922865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
28938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        if (!FTI.NumArgs && FTI.isVariadic && !LangOpts.CPlusPlus) {
2894965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
2895965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
2896965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
28978e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt            if (Attrs->getKind() == AttributeList::AT_Overloadable) {
2898965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
2899965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
2900965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
2901965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
2902965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
2903965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
29048cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
2905c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
29062865474261a608c7873b87ba4af110d17907896dJohn McCall
29072865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
2908788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
2909788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
29108cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
29112865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
2912d5668a2447c2afeea38815b51a80a5a6ac235599Argyrios Kyrtzidis          // Recover by creating a K&R-style function type.
2913ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner          T = Context.getFunctionNoProtoType(T, EI);
29142865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
29152865474261a608c7873b87ba4af110d17907896dJohn McCall        }
29162865474261a608c7873b87ba4af110d17907896dJohn McCall
2917e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
2918ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        EPI.ExtInfo = EI;
2919e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
292054655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        EPI.HasTrailingReturn = FTI.hasTrailingReturnType();
2921e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
2922c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
2923c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : FTI.RefQualifierIsLValueRef? RQ_LValue
2924c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : RQ_RValue;
292591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
29265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
29275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
29285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 16> ArgTys;
29292865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
29301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29315f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<bool, 16> ConsumedArguments;
2932f85e193739c953358c865005855253af4f68a497John McCall        ConsumedArguments.reserve(FTI.NumArgs);
2933f85e193739c953358c865005855253af4f68a497John McCall        bool HasAnyConsumedArguments = false;
2934f85e193739c953358c865005855253af4f68a497John McCall
29355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
2936d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
2937c910d4cfa5042f2c9da1eb4e0b6ed59240c0eeeeReid Kleckner          QualType ArgTy = Param->getType();
293878c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
29392dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
29405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
29415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
294272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
29432dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
29445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
29455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
29465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
29475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
29488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(DeclType.Loc, diag::err_void_only_param);
29492ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
29508123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
29512ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
29522ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
29538cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(FTI.ArgInfo[i].IdentLoc,
29544565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
29552ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
29568123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
29572ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
29582ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
29590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
29608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                S.Diag(DeclType.Loc, diag::err_void_param_qualified);
29611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29622ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
29632ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
29642ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
2965aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov          } else if (ArgTy->isHalfType()) {
2966aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // Disallow half FP arguments.
2967aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // FIXME: This really should be in BuildFunctionType.
296819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            if (S.getLangOpts().OpenCL) {
296919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              if (!S.getOpenCLOptions().cl_khr_fp16) {
297019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                S.Diag(Param->getLocation(),
297119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                  diag::err_opencl_half_argument) << ArgTy;
297219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                D.setInvalidType();
29739dd74c5504c743c96ea3a1d691d6a75ec3a98147John McCall                Param->setInvalidDecl();
297419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              }
297519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            } else {
297619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              S.Diag(Param->getLocation(),
297719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                diag::err_parameters_retval_cannot_have_fp16_type) << 0;
297819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              D.setInvalidType();
297919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            }
2980eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
2981eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
2982a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
2983eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              Param->setKNRPromoted(true);
2984183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
2985eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              if (BTy->getKind() == BuiltinType::Float) {
2986eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
2987eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall                Param->setKNRPromoted(true);
2988eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              }
2989eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
29905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
299156a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
29928cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          if (LangOpts.ObjCAutoRefCount) {
2993f85e193739c953358c865005855253af4f68a497John McCall            bool Consumed = Param->hasAttr<NSConsumedAttr>();
2994f85e193739c953358c865005855253af4f68a497John McCall            ConsumedArguments.push_back(Consumed);
2995f85e193739c953358c865005855253af4f68a497John McCall            HasAnyConsumedArguments |= Consumed;
2996f85e193739c953358c865005855253af4f68a497John McCall          }
2997f85e193739c953358c865005855253af4f68a497John McCall
299854e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
29995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
3000465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
3001f85e193739c953358c865005855253af4f68a497John McCall        if (HasAnyConsumedArguments)
3002f85e193739c953358c865005855253af4f68a497John McCall          EPI.ConsumedArguments = ConsumedArguments.data();
3003f85e193739c953358c865005855253af4f68a497John McCall
30045f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 4> Exceptions;
300574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<ParsedType, 2> DynamicExceptions;
300674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<SourceRange, 2> DynamicExceptionRanges;
300774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        Expr *NoexceptExpr = 0;
300891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
30098b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        if (FTI.getExceptionSpecType() == EST_Dynamic) {
301074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // FIXME: It's rather inefficient to have to split into two vectors
301174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // here.
301274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          unsigned N = FTI.NumExceptions;
301374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptions.reserve(N);
301474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptionRanges.reserve(N);
301574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          for (unsigned I = 0; I != N; ++I) {
301674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptions.push_back(FTI.Exceptions[I].Ty);
301774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptionRanges.push_back(FTI.Exceptions[I].Range);
3018e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
30198b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
302074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          NoexceptExpr = FTI.NoexceptExpr;
302174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        }
302291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
302374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        S.checkExceptionSpecification(FTI.getExceptionSpecType(),
302474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptions,
302574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptionRanges,
302674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      NoexceptExpr,
302774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      Exceptions,
302874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      EPI);
302991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3030bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose        T = Context.getFunctionType(T, ArgTys, EPI);
30315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
303204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
30335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
30345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
3035f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
3036f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
30377bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
3038f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
30397bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
3040edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
3041edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
30428cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      } else if (S.isDependentScopeSpecifier(SS) ||
30438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                 dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) {
30441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
30457bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
304687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
304787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
304887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
30497bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
30504a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
305187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
305287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
305387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
305414aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor        case NestedNameSpecifier::NamespaceAlias:
305587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
30569f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
30577bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
305887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
305987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
306087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
306191ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Note: if the NNS has a prefix and ClsType is a nondependent
306291ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // TemplateSpecializationType, then the NNS prefix is NOT included
306391ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // in ClsType; hence we wrap ClsType into an ElaboratedType.
306491ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // NOTE: in particular, no wrap occurs if ClsType already is an
306591ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Elaborated, DependentName, or DependentTemplateSpecialization.
306691ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
30677bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
306887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
306987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
3070f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
30718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Mem.Scope().getBeginLoc(),
3072949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
3073949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
3074949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
3075f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
3076f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
3077f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
3078949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
30798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
3080949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
3081f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
3082949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
30832865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
30848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
3085f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
3086f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
3087f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
3088f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
3089cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
3090cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
3091cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
3092cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
3093cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
3094c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
3095711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
3096f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclChunk, attrs);
30975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3098971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
30998cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (LangOpts.CPlusPlus && T->isFunctionType()) {
3100183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
3101778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
3102971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
310391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // C++ 8.3.5p4:
3104708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
3105708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
3106708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
3107708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
3108683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    //
3109683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // Core issue 547 also allows cv-qualifiers on function types that are
3110683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // top-level template type arguments.
3111613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
3112613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
3113906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman      FreeFunction = ((D.getContext() != Declarator::MemberContext &&
3114906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman                       D.getContext() != Declarator::LambdaExprContext) ||
3115613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall                      D.getDeclSpec().isFriendSpecified());
3116613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
31178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec());
3118613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
3119613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
3120613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
3121d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.fct]p6 (w/DR1417):
3122d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // An attempt to specify a function type with a cv-qualifier-seq or a
3123d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // ref-qualifier (including by typedef-name) is ill-formed unless it is:
3124d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type for a non-static member function,
3125d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type to which a pointer to member refers,
3126d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the top-level function type of a function typedef declaration or
3127d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //    alias-declaration,
3128d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id in the default argument of a type-parameter, or
3129d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id of a template-argument for a type-parameter
3130d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction &&
3131d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !(!FreeFunction &&
3132d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) &&
3133d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !IsTypedefName &&
3134d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        D.getContext() != Declarator::TemplateTypeArgContext) {
313596a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      SourceLocation Loc = D.getLocStart();
3136d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      SourceRange RemovalRange;
3137d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      unsigned I;
3138d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      if (D.isFunctionDeclarator(I)) {
3139d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        SmallVector<SourceLocation, 4> RemovalLocs;
3140d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        const DeclaratorChunk &Chunk = D.getTypeObject(I);
3141d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        assert(Chunk.Kind == DeclaratorChunk::Function);
3142d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.hasRefQualifier())
3143d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getRefQualifierLoc());
3144d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Const)
3145d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getConstQualifierLoc());
3146d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Volatile)
3147d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getVolatileQualifierLoc());
3148d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        // FIXME: We do not track the location of the __restrict qualifier.
3149d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //if (Chunk.Fun.TypeQuals & Qualifiers::Restrict)
3150d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //  RemovalLocs.push_back(Chunk.Fun.getRestrictQualifierLoc());
3151d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (!RemovalLocs.empty()) {
3152d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          std::sort(RemovalLocs.begin(), RemovalLocs.end(),
3153aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                    BeforeThanCompare<SourceLocation>(S.getSourceManager()));
3154d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalRange = SourceRange(RemovalLocs.front(), RemovalLocs.back());
3155d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          Loc = RemovalLocs.front();
3156683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
3157683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      }
3158d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
3159d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      S.Diag(Loc, diag::err_invalid_qualified_function_type)
3160d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FreeFunction << D.isFunctionDeclarator() << T
3161d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << getFunctionQualifiersAsString(FnTy)
3162d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FixItHint::CreateRemoval(RemovalRange);
3163d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
3164d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // Strip the cv-qualifiers and ref-qualifiers from the type.
3165d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
3166d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.TypeQuals = 0;
3167d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.RefQualifier = RQ_None;
3168d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
31690567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner      T = Context.getFunctionType(FnTy->getResultType(), FnTy->getArgTypes(),
3170bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                  EPI);
3171e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      // Rebuild any parens around the identifier in the function type.
3172e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
3173e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        if (D.getTypeObject(i).Kind != DeclaratorChunk::Paren)
3174e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith          break;
3175e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        T = S.BuildParenType(T);
3176e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      }
3177971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
3178971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
31791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
3181711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull())
3182711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = D.getAttributes())
3183f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclName, attrs);
3184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3185711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
3186711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
3187711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3188148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  // C++0x [dcl.constexpr]p9:
3189148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  //  A constexpr specifier used in an object declaration declares the object
319091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //  as const.
3191148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
3192737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
3193737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
3194737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
319591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // If there was an ellipsis in the declarator, the declaration declares a
3196a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
3197a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (D.hasEllipsis() && !T.isNull()) {
3198a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
319991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   A declarator-id or abstract-declarator containing an ellipsis shall
3200a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
3201a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
3202a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
3203a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
3204fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    case Declarator::LambdaExprParameterContext:
3205a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
320691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   [...] When it is part of a parameter-declaration-clause, the
320791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   parameter pack is a function parameter pack (14.5.3). The type T
3208a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
320991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   a template parameter pack; each template parameter pack in T is
3210a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
3211a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
3212a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
3213a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
3214a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
321591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(D.getEllipsisLoc(),
3216a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
3217a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
3218a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
3219a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
322066874fb18afbffb8b2ca05576851a64534be3352David Blaikie        T = Context.getPackExpansionType(T, None);
3221a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
3222a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
3223a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
3224a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
322591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   If a template-parameter is a [...] is a parameter-declaration that
3226a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
3227a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
3228a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
3229a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
3230a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
3231a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
3232a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
323366874fb18afbffb8b2ca05576851a64534be3352David Blaikie        T = Context.getPackExpansionType(T, None);
3234e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith      else
3235e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith        S.Diag(D.getEllipsisLoc(),
323680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith               LangOpts.CPlusPlus11
3237e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 ? diag::warn_cxx98_compat_variadic_templates
3238e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 : diag::ext_variadic_templates);
3239a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
324091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3241a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
3242a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
3243cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:  // FIXME: special diagnostic here?
3244cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:     // FIXME: special diagnostic here?
3245a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
32460b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
3247162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
32483e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
3249a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
3250a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
3251a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
3252a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
3253a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
325417b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
3255a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
3256f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
325714f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    case Declarator::ConversionIdContext:
32587796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
3259683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    case Declarator::TemplateTypeArgContext:
3260a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
3261a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
32628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      S.Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
3263a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
3264a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
3265a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
3266a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
3267e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
3268bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
3269bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
3270bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
3271bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
3272db7abf78dedc2ef6ccb42b3dac6ab330fe2ea469Argyrios Kyrtzidis
32738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return S.GetTypeSourceInfoForDeclarator(D, T, TInfo);
32748cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
32758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
32768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// GetTypeForDeclarator - Convert the type for the specified
32778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// declarator to Type instances.
32788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis///
32798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// The result of this call will never be null, but the associated
32808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// type may be a null type if there's an unrecoverable error.
32818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
32828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // Determine the type of the declarator. Not all forms of declarator
32838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // have a type.
32848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
32858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeProcessingState state(*this, D);
32868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
32878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
32888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
32898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (T.isNull())
32908cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
32918cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
32924e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (D.isPrototypeContext() && getLangOpts().ObjCAutoRefCount)
32938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    inferARCWriteback(state, T);
329491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
32958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return GetFullTypeForDeclarator(state, T, ReturnTypeInfo);
32965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
32975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
329831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclSpec(Sema &S,
329931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           QualType &declSpecTy,
330031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           Qualifiers::ObjCLifetime ownership) {
330131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy->isObjCRetainableType() &&
330231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) {
330331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers qs;
330431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    qs.addObjCLifetime(ownership);
330531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    declSpecTy = S.Context.getQualifiedType(declSpecTy, qs);
330631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
330731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
330831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
330931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
331031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
331131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            unsigned chunkIndex) {
331231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
331331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
331431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
331531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Look for an explicit lifetime attribute.
331631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclaratorChunk &chunk = D.getTypeObject(chunkIndex);
331731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (const AttributeList *attr = chunk.getAttrs(); attr;
331831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis         attr = attr->getNext())
33198e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    if (attr->getKind() == AttributeList::AT_ObjCOwnership)
332031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
332131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
332231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  const char *attrStr = 0;
332331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  switch (ownership) {
33243026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
332531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break;
332631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Strong: attrStr = "strong"; break;
332731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Weak: attrStr = "weak"; break;
332831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break;
332931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
333031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
3331624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  IdentifierLoc *Arg = new (S.Context) IdentifierLoc;
3332624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Arg->Ident = &S.Context.Idents.get(attrStr);
3333624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Arg->Loc = SourceLocation();
3334624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
3335624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  ArgsUnion Args(Arg);
3336624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
333731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // If there wasn't one, add one (with an invalid source location
333831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // so that we don't make an AttributedType for it).
333931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  AttributeList *attr = D.getAttributePool()
334031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    .create(&S.Context.Idents.get("objc_ownership"), SourceLocation(),
334131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            /*scope*/ 0, SourceLocation(),
3342624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman            /*args*/ &Args, 1, AttributeList::AS_GNU);
334331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  spliceAttrIntoList(*attr, chunk.getAttrListRef());
334431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
334531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // TODO: mark whether we did this inference?
334631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
334731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
334848d798ce32447607144db70a484cdb99c1180663Benjamin Kramer/// \brief Used for transferring ownership in casts resulting in l-values.
334931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnership(TypeProcessingState &state,
335031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 QualType &declSpecTy,
335131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 Qualifiers::ObjCLifetime ownership) {
335231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
335331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
335431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
335531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  int inner = -1;
33566ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis  bool hasIndirection = false;
335731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
335831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    DeclaratorChunk &chunk = D.getTypeObject(i);
335931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    switch (chunk.Kind) {
336031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Paren:
336131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      // Ignore parens.
336231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
336331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
336431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Array:
336531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Reference:
336631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Pointer:
33676ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
33686ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        hasIndirection = true;
336931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      inner = i;
337031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
337131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
337231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::BlockPointer:
33736ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
33746ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        transferARCOwnershipToDeclaratorChunk(state, ownership, i);
33756ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      return;
337631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
337731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Function:
337831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::MemberPointer:
337931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
338031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    }
338131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
338231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
338331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (inner == -1)
33846ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    return;
338531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
338691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  DeclaratorChunk &chunk = D.getTypeObject(inner);
338731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (chunk.Kind == DeclaratorChunk::Pointer) {
338831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (declSpecTy->isObjCRetainableType())
338931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
33906ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    if (declSpecTy->isObjCObjectType() && hasIndirection)
339131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclaratorChunk(state, ownership, inner);
339231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  } else {
339331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    assert(chunk.Kind == DeclaratorChunk::Array ||
339431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis           chunk.Kind == DeclaratorChunk::Reference);
339531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
339631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
339731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
339831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
339931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) {
340031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeProcessingState state(*this, D);
340131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
340231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
340331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
340431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy.isNull())
340531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
340631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
34074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount) {
340831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy);
340931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (ownership != Qualifiers::OCL_None)
341031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      transferARCOwnership(state, declSpecTy, ownership);
341131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
341231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
341331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo);
341431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
341531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
341614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall/// Map an AttributedType::Kind to an AttributeList::Kind.
341714aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
341814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  switch (kind) {
341914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_address_space:
34208e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_AddressSpace;
342114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_regparm:
34228e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Regparm;
342314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_vector_size:
34248e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_VectorSize;
342514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_vector_type:
34268e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonVectorType;
342714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_polyvector_type:
34288e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonPolyVectorType;
342914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_objc_gc:
34308e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCGC;
3431b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  case AttributedType::attr_objc_ownership:
34328e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCOwnership;
343314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_noreturn:
34348e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NoReturn;
343514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_cdecl:
34368e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_CDecl;
343714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_fastcall:
34388e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_FastCall;
343914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_stdcall:
34408e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_StdCall;
344114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_thiscall:
34428e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ThisCall;
344314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_pascal:
34448e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pascal;
3445414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case AttributedType::attr_pcs:
34465b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributedType::attr_pcs_vfp:
34478e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pcs;
3448263366f9241366f29ba65b703120f302490c39ffDerek Schuff  case AttributedType::attr_pnaclcall:
3449263366f9241366f29ba65b703120f302490c39ffDerek Schuff    return AttributeList::AT_PnaclCall;
345038980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei  case AttributedType::attr_inteloclbicc:
345138980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    return AttributeList::AT_IntelOclBicc;
3452e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  case AttributedType::attr_ms_abi:
3453e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    return AttributeList::AT_MSABI;
3454e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  case AttributedType::attr_sysv_abi:
3455e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    return AttributeList::AT_SysVABI;
3456aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_ptr32:
3457aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_Ptr32;
3458aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_ptr64:
3459aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_Ptr64;
3460aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_sptr:
3461aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_SPtr;
3462aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_uptr:
3463aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_UPtr;
346414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
346514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  llvm_unreachable("unexpected attribute kind!");
346614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
346714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
346814aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic void fillAttributedTypeLoc(AttributedTypeLoc TL,
346914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                  const AttributeList *attrs) {
347014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributedType::Kind kind = TL.getAttrKind();
347114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
347214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  assert(attrs && "no type attributes in the expected location!");
347314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributeList::Kind parsedKind = getAttrListKind(kind);
347414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  while (attrs->getKind() != parsedKind) {
347514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    attrs = attrs->getNext();
347614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    assert(attrs && "no matching attribute in expected location!");
347714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
347814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
347914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  TL.setAttrNameLoc(attrs->getLoc());
3480624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (TL.hasAttrExprOperand() && attrs->isArgExpr(0))
3481624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    TL.setAttrExprOperand(attrs->getArgAsExpr(0));
3482624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (TL.hasAttrEnumOperand() && attrs->isArgIdent(0))
3483624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    TL.setAttrEnumOperandLoc(attrs->getArgAsIdent(0)->Loc);
348414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
348514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // FIXME: preserve this information to here.
348614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrOperand())
348714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrOperandParensRange(SourceRange());
348814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
348914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
349051bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
349151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
3492c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    ASTContext &Context;
349351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
3494f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
349551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
349691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
3497c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      : Context(Context), DS(DS) {}
3498f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
349914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
350014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, DS.getAttributes().getList());
350114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      Visit(TL.getModifiedLoc());
350214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
350351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
350451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
350551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
350651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
350751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
350851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
350951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
351051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
35111de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // FIXME. We should have DS.getTypeSpecTypeEndLoc(). But, it requires
35121de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // addition field. What we have is good enough for dispay of location
35131de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // of 'fixit' on interface name.
35141de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      TL.setNameEndLoc(DS.getLocEnd());
3515c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
3516c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3517c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
3518c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
3519c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
3520c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.getBaseLoc().initialize(Context, SourceLocation());
3521c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
3522c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
3523c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
3524c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
352554e14c4db764c0636160d26c5bbf491637c83a76John McCall
3526c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
352754e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
352854e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
352954e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
353054e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
353154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
353254e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
353354e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
353454e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
353554e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
353654e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
353754e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
353854e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
353951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
354054e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
354154e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
3542c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
354351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
3544833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
3545a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
3546b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3547833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3548833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
3549833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
3550a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
35510daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara        TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
3552833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
3553833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
3554833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3555e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
3556e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
355739e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        ElaboratedTypeLoc ElabTL = OldTL.castAs<ElaboratedTypeLoc>();
355839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        TemplateSpecializationTypeLoc NamedTL = ElabTL.getNamedTypeLoc()
355939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            .castAs<TemplateSpecializationTypeLoc>();
3560e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
356144ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      } else {
356239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        TL.copy(OldTL.castAs<TemplateSpecializationTypeLoc>());
356344ee0a710c59d8e6793189f903bae21c16814324Eli Friedman        assert(TL.getRAngleLoc() == OldTL.castAs<TemplateSpecializationTypeLoc>().getRAngleLoc());
356444ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      }
356544ee0a710c59d8e6793189f903bae21c16814324Eli Friedman
3566833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
3567cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3568cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
3569cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3570cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3571cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3572cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3573cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
3574cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3575cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3576b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
3577cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
3578b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3579cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
3580cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3581ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3582ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      // FIXME: This holds only because we only have one unary transform.
3583ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType);
3584ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setKWLoc(DS.getTypeSpecTypeLoc());
3585ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setParensRange(DS.getTypeofParensRange());
3586ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getRepAsType());
3587ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TypeSourceInfo *TInfo = 0;
3588ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3589ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setUnderlyingTInfo(TInfo);
3590ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
3591ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3592ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
3593ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
3594ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
3595ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
3596ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
3597ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
3598ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
3599ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
3600ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
3601ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
3602ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
3603ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
3604ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
3605ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
3606e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3607e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
3608e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
3609253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
3610e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
3611b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3612e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
361339e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          TL.copy(TInfo->getTypeLoc().castAs<ElaboratedTypeLoc>());
3614e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
3615e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
3616e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
361738a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara      TL.setElaboratedKeywordLoc(Keyword != ETK_None
361838a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 ? DS.getTypeSpecTypeLoc()
361938a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 : SourceLocation());
3620e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
36219e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
3622e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
3623e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3624e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
362566581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
362666581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
362766581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
362866581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
362939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      TL.copy(TInfo->getTypeLoc().castAs<DependentNameTypeLoc>());
363033500955d731c73717af52088b7fc0e7a85681e7John McCall    }
363133500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
363233500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
363366581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
363466581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
363566581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
363666581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
363739e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      TL.copy(
363839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          TInfo->getTypeLoc().castAs<DependentTemplateSpecializationTypeLoc>());
36390daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    }
36400daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    void VisitTagTypeLoc(TagTypeLoc TL) {
36410daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
3642e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3643b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
36444cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // An AtomicTypeLoc can come from either an _Atomic(...) type specifier
36454cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // or an _Atomic qualifier.
36464cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      if (DS.getTypeSpecType() == DeclSpec::TST_atomic) {
36474cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setKWLoc(DS.getTypeSpecTypeLoc());
36484cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setParensRange(DS.getTypeofParensRange());
364991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
36504cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TypeSourceInfo *TInfo = 0;
36514cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
36524cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        assert(TInfo);
36534cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
36544cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      } else {
36554cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setKWLoc(DS.getAtomicSpecLoc());
36564cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // No parens, to indicate this was spelled as an _Atomic qualifier.
36574cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setParensRange(SourceRange());
36584cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        Visit(TL.getValueLoc());
36594cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      }
3660b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
3661e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
366251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
366351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
3664c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initialize(Context, DS.getTypeSpecTypeLoc());
366551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
366651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
3667eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
366851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
3669b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    ASTContext &Context;
367051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
3671f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
367251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
3673b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
3674b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      : Context(Context), Chunk(Chunk) {}
36754adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
367651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
36779f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
367851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
367912df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner    void VisitDecayedTypeLoc(DecayedTypeLoc TL) {
368012df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner      llvm_unreachable("decayed type locs not expected here!");
368112df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner    }
36824adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3683f85e193739c953358c865005855253af4f68a497John McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3684f85e193739c953358c865005855253af4f68a497John McCall      fillAttributedTypeLoc(TL, Chunk.getAttrs());
3685f85e193739c953358c865005855253af4f68a497John McCall    }
368651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
368751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
368851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
36894adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
369051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
369151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
369251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
36934adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
369451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
369551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
369651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
36974adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
369851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
369951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
3700b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const CXXScopeSpec& SS = Chunk.Mem.Scope();
3701b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
3702b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3703b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const Type* ClsTy = TL.getClass();
3704b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      QualType ClsQT = QualType(ClsTy, 0);
3705b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
3706b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Now copy source location info into the type loc component.
3707b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeLoc ClsTL = ClsTInfo->getTypeLoc();
3708b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
3709b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Identifier:
3710b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
3711b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        {
371239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          DependentNameTypeLoc DNTLoc = ClsTL.castAs<DependentNameTypeLoc>();
371338a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          DNTLoc.setElaboratedKeywordLoc(SourceLocation());
3714b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
3715b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
3716b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3717b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3718b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3719b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpec:
3720b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpecWithTemplate:
3721b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        if (isa<ElaboratedType>(ClsTy)) {
372239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          ElaboratedTypeLoc ETLoc = ClsTL.castAs<ElaboratedTypeLoc>();
372338a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          ETLoc.setElaboratedKeywordLoc(SourceLocation());
3724b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ETLoc.setQualifierLoc(NNSLoc.getPrefix());
3725b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
3726b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
3727b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        } else {
3728b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
3729b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3730b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3731b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3732b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Namespace:
3733b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::NamespaceAlias:
3734b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Global:
3735b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        llvm_unreachable("Nested-name-specifier must name a type");
3736b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      }
3737b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3738b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Finally fill in MemberPointerLocInfo fields.
373951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
3740b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TL.setClassTInfo(ClsTInfo);
37414adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
374251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
374351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
374454e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
374554e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
374651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
374751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
374851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
374951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
375051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
375151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
375251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
375351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
375451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
375551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
375651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
375751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
375851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
375951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
376051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
3761796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeBegin(Chunk.Loc);
3762796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeEnd(Chunk.EndLoc);
376351bd803fbdade51d674598ed45da3d54190a656cJohn McCall
376451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
376559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setLParenLoc(FTI.getLParenLoc());
376659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setRParenLoc(FTI.getRParenLoc());
376754e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
3768d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
376954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
37704adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
377151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
37724adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
3773075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
3774075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
3775075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
3776075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
3777075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
37781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
377951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
37809f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
37814adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
378251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
378351bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
37844adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
37854cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smithstatic void fillAtomicQualLoc(AtomicTypeLoc ATL, const DeclaratorChunk &Chunk) {
37864cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  SourceLocation Loc;
37874cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  switch (Chunk.Kind) {
37884cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Function:
37894cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Array:
37904cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Paren:
37914cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    llvm_unreachable("cannot be _Atomic qualified");
37924cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
37934cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Pointer:
37944cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    Loc = SourceLocation::getFromRawEncoding(Chunk.Ptr.AtomicQualLoc);
37954cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    break;
37964cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
37974cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::BlockPointer:
37984cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Reference:
37994cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::MemberPointer:
38004cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // FIXME: Provide a source location for the _Atomic keyword.
38014cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    break;
38024cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
38034cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
38044cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  ATL.setKWLoc(Loc);
38054cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  ATL.setParensRange(SourceRange());
38064cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith}
38074cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
3808a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
380951bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
381051bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
381105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
381205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
381305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
381405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
381505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
3816a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
381705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
381805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
3819a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
3820a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
382151bd803fbdade51d674598ed45da3d54190a656cJohn McCall
3822a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
3823a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
382439e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    CurrTL.castAs<PackExpansionTypeLoc>().setEllipsisLoc(D.getEllipsisLoc());
382591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
3826a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
382791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
38288ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
38294cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // An AtomicTypeLoc might be produced by an atomic qualifier in this
38304cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // declarator chunk.
38314cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (AtomicTypeLoc ATL = CurrTL.getAs<AtomicTypeLoc>()) {
38324cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      fillAtomicQualLoc(ATL, D.getTypeObject(i));
38334cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      CurrTL = ATL.getValueLoc().getUnqualifiedLoc();
38344cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    }
38354cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
383639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    while (AttributedTypeLoc TL = CurrTL.getAs<AttributedTypeLoc>()) {
383714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs());
383814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
383914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
384014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3841b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
384251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
38434adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
384491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3845b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
3846b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
3847b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
384805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
384905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
385005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
3851b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
3852c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
385305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
385491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3855a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
38564adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
38574adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3858a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
3859b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
38601bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
38611bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
38621bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
386391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
3864eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
3865a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
38661bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
38671bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
3868b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
38691bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
38701bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
38711bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
38721bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
3873b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*"
387435d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
387535d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
38761bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
38771bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
3878f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
38795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
38805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
38815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
38821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3883d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
3884bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
38855153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
3886809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
38875912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
3888e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall  // Make sure there are no unused decl attributes on the declarator.
3889cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // We don't want to do this for ObjC parameters because we're going
3890cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // to apply them to the actual parameter declaration.
38916b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  // Likewise, we don't want to do this for alias declarations, because
38926b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  // we are actually going to build a declaration from this eventually.
38936b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  if (D.getContext() != Declarator::ObjCParameterContext &&
38946b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith      D.getContext() != Declarator::AliasDeclContext &&
38956b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith      D.getContext() != Declarator::AliasTemplateContext)
3896cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    checkUnusedDeclAttributes(D);
3897e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
38984e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
3899402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
39006d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
3901402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
3902402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
3903b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
39045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
39055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3906e97179c675b341927807c718be215c8d1aab8acbDouglas GregorParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) {
3907e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  QualType T = Context.getObjCInstanceType();
3908e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc);
3909e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return CreateParsedType(T, TInfo);
3910e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
3911e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3912e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3913c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3914c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
3915c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3916232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3917232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
3918c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
3919c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
39201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
3921c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
39220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
3923232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
392429e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified by
392529e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // qualifiers for two or more different address spaces."
3926232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
3927c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
3928e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3929c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3930232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
39311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3932020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be
3933020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // qualified by an address-space qualifier."
3934020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  if (Type->isFunctionType()) {
3935020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type);
3936020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    Attr.setInvalid();
3937020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    return;
3938020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  }
3939020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne
3940232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
3941545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
3942baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3943baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
3944e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3945c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3946232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3947624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3948232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
3949ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
3950ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
39519f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
39529f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
39539f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << ASArgExpr->getSourceRange();
3954e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3955c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3956232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3957232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3958efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
3959efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
3960efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
3961efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
3962efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
3963e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
3964efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
3965efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
3966efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
3967efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3968efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
39690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
3970efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
3971efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
39727348454025693dd20a411c2bcaabd4460cb87559Joerg Sonnenberger      << int(Qualifiers::MaxAddressSpace) << ASArgExpr->getSourceRange();
3973e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3974efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
3975efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3976efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
39771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
3978f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
3979c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
3980c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
3981d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// Does this type have a "direct" ownership qualifier?  That is,
3982d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// is it written like "__strong id", as opposed to something like
3983d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// "typeof(foo)", where that happens to be strong?
3984d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCallstatic bool hasDirectOwnershipQualifier(QualType type) {
3985d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Fast path: no qualifier at all.
3986d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  assert(type.getQualifiers().hasObjCLifetime());
3987d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3988d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  while (true) {
3989d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // __strong id
3990d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (const AttributedType *attr = dyn_cast<AttributedType>(type)) {
3991d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      if (attr->getAttrKind() == AttributedType::attr_objc_ownership)
3992d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        return true;
3993d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3994d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = attr->getModifiedType();
3995d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3996d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // X *__strong (...)
3997d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else if (const ParenType *paren = dyn_cast<ParenType>(type)) {
3998d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = paren->getInnerType();
399991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4000d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // That's it for things we want to complain about.  In particular,
4001d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // we do not want to look through typedefs, typeof(expr),
4002d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // typeof(type), or any other way that the type is somehow
4003d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // abstracted.
4004d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else {
400591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4006d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return false;
4007d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
4008d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
4009d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall}
4010d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
4011b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis/// handleObjCOwnershipTypeAttr - Process an objc_ownership
4012f85e193739c953358c865005855253af4f68a497John McCall/// attribute on the specified type.
4013f85e193739c953358c865005855253af4f68a497John McCall///
4014f85e193739c953358c865005855253af4f68a497John McCall/// Returns 'true' if the attribute was handled.
4015b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
4016f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr,
4017f85e193739c953358c865005855253af4f68a497John McCall                                       QualType &type) {
4018e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  bool NonObjCPointer = false;
4019e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
4020dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith  if (!type->isDependentType() && !type->isUndeducedType()) {
4021e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    if (const PointerType *ptr = type->getAs<PointerType>()) {
4022e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      QualType pointee = ptr->getPointeeType();
4023e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      if (pointee->isObjCRetainableType() || pointee->isPointerType())
4024e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis        return false;
4025e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // It is important not to lose the source info that there was an attribute
4026e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // applied to non-objc pointer. We will create an attributed type but
4027e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // its type will be the same as the original type.
4028e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      NonObjCPointer = true;
4029e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    } else if (!type->isObjCRetainableType()) {
4030e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      return false;
4031e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
4032b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
4033b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // Don't accept an ownership attribute in the declspec if it would
4034b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // just be the return type of a block pointer.
4035b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    if (state.isProcessingDeclSpec()) {
4036b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      Declarator &D = state.getDeclarator();
4037b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (maybeMovePastReturnType(D, D.getNumTypeObjects()))
4038b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        return false;
4039b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
4040e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
4041f85e193739c953358c865005855253af4f68a497John McCall
4042f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
4043440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  SourceLocation AttrLoc = attr.getLoc();
4044440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isMacroID())
4045440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    AttrLoc = S.getSourceManager().getImmediateExpansionRange(AttrLoc).first;
4046f85e193739c953358c865005855253af4f68a497John McCall
4047624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (!attr.isArgIdent(0)) {
40483cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman    S.Diag(AttrLoc, diag::err_attribute_argument_type)
40493cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman      << attr.getName() << AANT_ArgumentString;
4050f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
4051f85e193739c953358c865005855253af4f68a497John McCall    return true;
4052f85e193739c953358c865005855253af4f68a497John McCall  }
4053f85e193739c953358c865005855253af4f68a497John McCall
4054d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Consume lifetime attributes without further comment outside of
4055d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // ARC mode.
40564e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!S.getLangOpts().ObjCAutoRefCount)
4057d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    return true;
4058d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
4059624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  IdentifierInfo *II = attr.getArgAsIdent(0)->Ident;
4060f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime lifetime;
4061624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (II->isStr("none"))
4062f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_ExplicitNone;
4063624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (II->isStr("strong"))
4064f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Strong;
4065624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (II->isStr("weak"))
4066f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Weak;
4067624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (II->isStr("autoreleasing"))
4068f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Autoreleasing;
4069f85e193739c953358c865005855253af4f68a497John McCall  else {
4070440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_attribute_type_not_supported)
4071d068607c136298bec0891d750389a55bac9f5c98Aaron Ballman      << attr.getName() << II;
4072f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
4073f85e193739c953358c865005855253af4f68a497John McCall    return true;
4074f85e193739c953358c865005855253af4f68a497John McCall  }
4075f85e193739c953358c865005855253af4f68a497John McCall
4076d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  SplitQualType underlyingType = type.split();
4077d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
4078d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Check for redundant/conflicting ownership qualifiers.
4079d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  if (Qualifiers::ObjCLifetime previousLifetime
4080d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        = type.getQualifiers().getObjCLifetime()) {
4081d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // If it's written directly, that's an error.
4082d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (hasDirectOwnershipQualifier(type)) {
4083d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant)
4084d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        << type;
4085d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return true;
4086d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
4087d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
4088d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // Otherwise, if the qualifiers actually conflict, pull sugar off
4089d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // until we reach a type that is directly qualified.
4090d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (previousLifetime != lifetime) {
4091d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // This should always terminate: the canonical type is
4092d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // qualified, so some bit of sugar must be hiding it.
4093d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      while (!underlyingType.Quals.hasObjCLifetime()) {
4094d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        underlyingType = underlyingType.getSingleStepDesugaredType();
4095d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      }
4096d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      underlyingType.Quals.removeObjCLifetime();
4097d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
4098d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
4099d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
4100d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  underlyingType.Quals.addObjCLifetime(lifetime);
4101f85e193739c953358c865005855253af4f68a497John McCall
4102e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (NonObjCPointer) {
4103e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    StringRef name = attr.getName()->getName();
4104e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    switch (lifetime) {
4105e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_None:
4106e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_ExplicitNone:
4107e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      break;
4108e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Strong: name = "__strong"; break;
4109e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Weak: name = "__weak"; break;
4110e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break;
4111e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
4112fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman    S.Diag(AttrLoc, diag::warn_type_attribute_wrong_type) << name
4113fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman      << TDS_ObjCObjOrBlock << type;
4114e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
4115e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
4116f85e193739c953358c865005855253af4f68a497John McCall  QualType origType = type;
4117e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!NonObjCPointer)
4118d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    type = S.Context.getQualifiedType(underlyingType);
4119f85e193739c953358c865005855253af4f68a497John McCall
4120f85e193739c953358c865005855253af4f68a497John McCall  // If we have a valid source location for the attribute, use an
4121f85e193739c953358c865005855253af4f68a497John McCall  // AttributedType instead.
4122440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isValid())
4123b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis    type = S.Context.getAttributedType(AttributedType::attr_objc_ownership,
4124f85e193739c953358c865005855253af4f68a497John McCall                                       origType, type);
4125f85e193739c953358c865005855253af4f68a497John McCall
41269f084a3166b684573ba49df28fc5792bc37d92e1John McCall  // Forbid __weak if the runtime doesn't support it.
4127f85e193739c953358c865005855253af4f68a497John McCall  if (lifetime == Qualifiers::OCL_Weak &&
41280a7dd788dbef975f35f273c7ab913f480f7edd60John McCall      !S.getLangOpts().ObjCARCWeak && !NonObjCPointer) {
4129f85e193739c953358c865005855253af4f68a497John McCall
4130f85e193739c953358c865005855253af4f68a497John McCall    // Actually, delay this until we know what we're parsing.
4131f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
4132f85e193739c953358c865005855253af4f68a497John McCall      S.DelayedDiagnostics.add(
4133440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis          sema::DelayedDiagnostic::makeForbiddenType(
4134440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis              S.getSourceManager().getExpansionLoc(AttrLoc),
4135f85e193739c953358c865005855253af4f68a497John McCall              diag::err_arc_weak_no_runtime, type, /*ignored*/ 0));
4136f85e193739c953358c865005855253af4f68a497John McCall    } else {
4137440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis      S.Diag(AttrLoc, diag::err_arc_weak_no_runtime);
4138f85e193739c953358c865005855253af4f68a497John McCall    }
4139f85e193739c953358c865005855253af4f68a497John McCall
4140f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
4141f85e193739c953358c865005855253af4f68a497John McCall    return true;
4142f85e193739c953358c865005855253af4f68a497John McCall  }
414391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
414491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // Forbid __weak for class objects marked as
4145742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  // objc_arc_weak_reference_unavailable
4146742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  if (lifetime == Qualifiers::OCL_Weak) {
4147b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    if (const ObjCObjectPointerType *ObjT =
4148b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          type->getAs<ObjCObjectPointerType>()) {
41494e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith      if (ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl()) {
41504e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        if (Class->isArcWeakrefUnavailable()) {
41514e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class);
41524e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(ObjT->getInterfaceDecl()->getLocation(),
41534e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith                   diag::note_class_declared);
41544e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        }
4155742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      }
4156742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    }
4157742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  }
415891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4159f85e193739c953358c865005855253af4f68a497John McCall  return true;
4160f85e193739c953358c865005855253af4f68a497John McCall}
4161f85e193739c953358c865005855253af4f68a497John McCall
4162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
4163711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
4164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
4165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
4166711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
4167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
4168711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
4169711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
4170711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
4172711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
4173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
4174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
4175711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
4176711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4177711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
4178711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
4179711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4181d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
4182624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
4183d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
4184624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (!attr.isArgIdent(0)) {
41853cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman    S.Diag(attr.getLoc(), diag::err_attribute_argument_type)
41863cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman      << attr.getName() << AANT_ArgumentString;
4187711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4188711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4189ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
41900953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
4191624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (attr.getNumArgs() > 1) {
4192baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4193baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << attr.getName() << 1;
4194711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4195711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4196d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
4197624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
4198624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  IdentifierInfo *II = attr.getArgAsIdent(0)->Ident;
4199624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (II->isStr("weak"))
42000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
4201624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (II->isStr("strong"))
42020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
4203d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
4204711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
4205d068607c136298bec0891d750389a55bac9f5c98Aaron Ballman      << attr.getName() << II;
4206711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4207711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4208d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
42091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
421014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  QualType origType = type;
421114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  type = S.Context.getObjCGCQualType(origType, GCAttr);
421214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
421314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // Make an attributed type to preserve the source information.
421414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (attr.getLoc().isValid())
421514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
421614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                       origType, type);
421714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
4218711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
4219d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
4220d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
4221e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallnamespace {
4222e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// A helper class to unwrap a type down to a function for the
4223e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// purposes of applying attributes there.
4224e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///
4225e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// Use:
4226e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
4227e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   if (unwrapped.isFunctionType()) {
4228e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     const FunctionType *fn = unwrapped.get();
4229e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     // change fn somehow
4230e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     T = unwrapped.wrap(fn);
4231e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   }
4232e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  struct FunctionTypeUnwrapper {
4233e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    enum WrapKind {
4234e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Desugar,
4235e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Parens,
4236e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Pointer,
4237e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      BlockPointer,
4238e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Reference,
4239e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      MemberPointer
4240e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    };
4241e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4242e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType Original;
4243e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *Fn;
42445f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<unsigned char /*WrapKind*/, 8> Stack;
4245e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4246e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
4247e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      while (true) {
4248e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const Type *Ty = T.getTypePtr();
4249e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<FunctionType>(Ty)) {
4250e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Fn = cast<FunctionType>(Ty);
4251e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return;
4252e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ParenType>(Ty)) {
4253e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ParenType>(Ty)->getInnerType();
4254e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Parens);
4255e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<PointerType>(Ty)) {
4256e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<PointerType>(Ty)->getPointeeType();
4257e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Pointer);
4258e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<BlockPointerType>(Ty)) {
4259e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<BlockPointerType>(Ty)->getPointeeType();
4260e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(BlockPointer);
4261e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<MemberPointerType>(Ty)) {
4262e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<MemberPointerType>(Ty)->getPointeeType();
4263e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(MemberPointer);
4264e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ReferenceType>(Ty)) {
4265e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ReferenceType>(Ty)->getPointeeType();
4266e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Reference);
4267e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else {
4268e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          const Type *DTy = Ty->getUnqualifiedDesugaredType();
4269e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          if (Ty == DTy) {
4270e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            Fn = 0;
4271e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            return;
4272e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          }
4273e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4274e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = QualType(DTy, 0);
4275e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Desugar);
4276e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        }
4277e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4278e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4279e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4280e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    bool isFunctionType() const { return (Fn != 0); }
4281e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *get() const { return Fn; }
4282e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4283e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(Sema &S, const FunctionType *New) {
4284e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // If T wasn't modified from the unwrapped type, do nothing.
4285e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (New == get()) return Original;
4286e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4287e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Fn = New;
4288e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return wrap(S.Context, Original, 0);
4289e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4290e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4291e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  private:
4292e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
4293e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size())
4294e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getQualifiedType(Fn, Old.getQualifiers());
4295e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4296e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // Build up the inner type, applying the qualifiers from the old
4297e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // type to the new type.
4298e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      SplitQualType SplitOld = Old.split();
4299e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4300e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // As a special case, tail-recurse if there are no qualifiers.
4301200fa53fd420aa8369586f569dbece04930ad6a3John McCall      if (SplitOld.Quals.empty())
4302200fa53fd420aa8369586f569dbece04930ad6a3John McCall        return wrap(C, SplitOld.Ty, I);
4303200fa53fd420aa8369586f569dbece04930ad6a3John McCall      return C.getQualifiedType(wrap(C, SplitOld.Ty, I), SplitOld.Quals);
4304e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4305e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4306e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
4307e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size()) return QualType(Fn, 0);
4308e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4309e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      switch (static_cast<WrapKind>(Stack[I++])) {
4310e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Desugar:
4311e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // This is the point at which we potentially lose source
4312e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // information.
4313e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
4314e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4315e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Parens: {
4316e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
4317e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getParenType(New);
4318e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4319e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4320e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Pointer: {
4321e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
4322e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getPointerType(New);
4323e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4324e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4325e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case BlockPointer: {
4326e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
4327e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getBlockPointerType(New);
4328e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4329e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4330e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case MemberPointer: {
4331e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
4332e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldMPT->getPointeeType(), I);
4333e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getMemberPointerType(New, OldMPT->getClass());
4334e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4335e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4336e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Reference: {
4337e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const ReferenceType *OldRef = cast<ReferenceType>(Old);
4338e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldRef->getPointeeType(), I);
4339e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<LValueReferenceType>(OldRef))
4340e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
4341e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        else
4342e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getRValueReferenceType(New);
4343e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4344e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4345e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4346e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      llvm_unreachable("unknown wrapping kind");
4347e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4348e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  };
4349e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
4350e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4351aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballmanstatic bool handleMSPointerTypeQualifierAttr(TypeProcessingState &State,
4352aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             AttributeList &Attr,
4353aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             QualType &Type) {
4354aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  Sema &S = State.getSema();
4355aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4356aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  AttributeList::Kind Kind = Attr.getKind();
4357aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  QualType Desugared = Type;
4358aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  const AttributedType *AT = dyn_cast<AttributedType>(Type);
4359aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  while (AT) {
4360aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    AttributedType::Kind CurAttrKind = AT->getAttrKind();
4361aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4362aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // You cannot specify duplicate type attributes, so if the attribute has
4363aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // already been applied, flag it.
4364aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    if (getAttrListKind(CurAttrKind) == Kind) {
4365aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute_exact)
4366aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        << Attr.getName();
4367aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      return true;
4368aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    }
4369aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4370aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // You cannot have both __sptr and __uptr on the same type, nor can you
4371aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // have __ptr32 and __ptr64.
4372aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    if ((CurAttrKind == AttributedType::attr_ptr32 &&
4373aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman         Kind == AttributeList::AT_Ptr64) ||
4374aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        (CurAttrKind == AttributedType::attr_ptr64 &&
4375aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman         Kind == AttributeList::AT_Ptr32)) {
4376aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
4377aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        << "'__ptr32'" << "'__ptr64'";
4378aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      return true;
4379aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    } else if ((CurAttrKind == AttributedType::attr_sptr &&
4380aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                Kind == AttributeList::AT_UPtr) ||
4381aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman               (CurAttrKind == AttributedType::attr_uptr &&
4382aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                Kind == AttributeList::AT_SPtr)) {
4383aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
4384aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        << "'__sptr'" << "'__uptr'";
4385aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      return true;
4386aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    }
4387aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4388aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    Desugared = AT->getEquivalentType();
4389aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    AT = dyn_cast<AttributedType>(Desugared);
4390aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  }
4391aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4392aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  // Pointer type qualifiers can only operate on pointer types, but not
4393aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  // pointer-to-member types.
4394aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  if (!isa<PointerType>(Desugared)) {
4395aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    S.Diag(Attr.getLoc(), Type->isMemberPointerType() ?
4396aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                          diag::err_attribute_no_member_pointers :
4397aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                          diag::err_attribute_pointers_only) << Attr.getName();
4398aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return true;
4399aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  }
4400aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4401aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  AttributedType::Kind TAK;
4402aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  switch (Kind) {
4403aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  default: llvm_unreachable("Unknown attribute kind");
4404aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_Ptr32: TAK = AttributedType::attr_ptr32; break;
4405aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_Ptr64: TAK = AttributedType::attr_ptr64; break;
4406aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_SPtr: TAK = AttributedType::attr_sptr; break;
4407aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_UPtr: TAK = AttributedType::attr_uptr; break;
4408aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  }
4409aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
4410aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  Type = S.Context.getAttributedType(TAK, Type, Type);
4411aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  return false;
4412aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman}
4413aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
44145b92696c8f1f8ef943ad87397b95c031b5787305Reid Klecknerstatic AttributedType::Kind getCCTypeAttrKind(AttributeList &Attr) {
44155b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  assert(!Attr.isInvalid());
44165b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  switch (Attr.getKind()) {
44175b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  default:
44185b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    llvm_unreachable("not a calling convention attribute");
44195b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_CDecl:
44205b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_cdecl;
44215b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_FastCall:
44225b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_fastcall;
44235b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_StdCall:
44245b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_stdcall;
44255b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_ThisCall:
44265b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_thiscall;
44275b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_Pascal:
44285b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_pascal;
44295b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_Pcs: {
4430fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    // The attribute may have had a fixit applied where we treated an
4431fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    // identifier as a string literal.  The contents of the string are valid,
4432fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    // but the form may not be.
4433fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    StringRef Str;
4434fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    if (Attr.isArgExpr(0))
4435fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman      Str = cast<StringLiteral>(Attr.getArgAsExpr(0))->getString();
4436fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    else
4437fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman      Str = Attr.getArgAsIdent(0)->Ident->getName();
4438fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    return llvm::StringSwitch<AttributedType::Kind>(Str)
44395b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner        .Case("aapcs", AttributedType::attr_pcs)
44405b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner        .Case("aapcs-vfp", AttributedType::attr_pcs_vfp);
44415b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  }
44425b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_PnaclCall:
44435b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_pnaclcall;
44445b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_IntelOclBicc:
44455b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_inteloclbicc;
4446e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  case AttributeList::AT_MSABI:
4447e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    return AttributedType::attr_ms_abi;
4448e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  case AttributeList::AT_SysVABI:
4449e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    return AttributedType::attr_sysv_abi;
44505b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  }
44515b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  llvm_unreachable("unexpected attribute kind!");
44525b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner}
44535b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner
4454711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
4455711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
4456711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
4457711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
4458711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
4459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
4460e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4461711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
44622455636163fdd18581d7fdae816433f886d88213Mike Stump
44638e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NoReturn) {
4464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
446504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
4466e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4467e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
4468711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
4469711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
4470425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
4471425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
4472711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
4473711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4474711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4475711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
4476425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
4477f85e193739c953358c865005855253af4f68a497John McCall  // ns_returns_retained is not always a type attribute, but if we got
4478f85e193739c953358c865005855253af4f68a497John McCall  // here, we're treating it as one right now.
44798e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NSReturnsRetained) {
44804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(S.getLangOpts().ObjCAutoRefCount &&
4481f85e193739c953358c865005855253af4f68a497John McCall           "ns_returns_retained treated as type attribute in non-ARC");
4482f85e193739c953358c865005855253af4f68a497John McCall    if (attr.getNumArgs()) return true;
4483f85e193739c953358c865005855253af4f68a497John McCall
4484f85e193739c953358c865005855253af4f68a497John McCall    // Delay if this is not a function type.
4485f85e193739c953358c865005855253af4f68a497John McCall    if (!unwrapped.isFunctionType())
4486f85e193739c953358c865005855253af4f68a497John McCall      return false;
4487f85e193739c953358c865005855253af4f68a497John McCall
4488f85e193739c953358c865005855253af4f68a497John McCall    FunctionType::ExtInfo EI
4489f85e193739c953358c865005855253af4f68a497John McCall      = unwrapped.get()->getExtInfo().withProducesResult(true);
4490f85e193739c953358c865005855253af4f68a497John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4491f85e193739c953358c865005855253af4f68a497John McCall    return true;
4492f85e193739c953358c865005855253af4f68a497John McCall  }
4493f85e193739c953358c865005855253af4f68a497John McCall
44948e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_Regparm) {
4495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
4496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
4497711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
44981e030eb1194763b42c1752723be23b1515f48981John McCall
4499711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
4500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
4501008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
45021e030eb1194763b42c1752723be23b1515f48981John McCall
4503ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Diagnose regparm with fastcall.
4504ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    const FunctionType *fn = unwrapped.get();
4505ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    CallingConv CC = fn->getCallConv();
4506ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (CC == CC_X86FastCall) {
4507ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
4508ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC)
4509ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm";
4510ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
4511ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
4512ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
4513ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
451491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    FunctionType::ExtInfo EI =
4515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
4516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4518425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
4519425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
452082bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  // Delay if the type didn't work out to a function.
452182bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  if (!unwrapped.isFunctionType()) return false;
452282bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman
452304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
4524711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
4525711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
4526711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4527f82b4e85b1219295cad4b5851b035575bc293010John McCall
4528711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
4529711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
4530ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  AttributedType::Kind CCAttrKind = getCCTypeAttrKind(attr);
453104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
4532e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  if (CCOld != CC) {
4533ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // Error out on when there's already an attribute on the type
4534ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // and the CCs don't match.
4535ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    const AttributedType *AT = S.getCallingConvAttributedType(type);
4536ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    if (AT && AT->getAttrKind() != CCAttrKind) {
4537ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
4538ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        << FunctionType::getNameForCallConv(CC)
4539ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        << FunctionType::getNameForCallConv(CCOld);
4540ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      attr.setInvalid();
4541ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      return true;
4542ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    }
454304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
454404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
454504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
454604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
4547711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isa<FunctionNoProtoType>(fn)) {
4548711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_knr)
454904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
4550711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
4551711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
455204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
455304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
4554711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
455504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
4556711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
455704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
4558711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
4559711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
456004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
4561ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
4562ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Also diagnose fastcall with regparm.
4563a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    if (fn->getHasRegParm()) {
4564ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
4565ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm"
4566ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC);
4567ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
4568ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
4569ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
457004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
4571f82b4e85b1219295cad4b5851b035575bc293010John McCall
45725b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  // Modify the CC from the wrapped function type, wrap it all back, and then
45735b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  // wrap the whole thing in an AttributedType as written.  The modified type
45745b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  // might have a different CC if we ignored the attribute.
4575711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
45765b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  QualType Equivalent =
45775b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner      unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4578ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  type = S.Context.getAttributedType(CCAttrKind, type, Equivalent);
4579711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
4580f82b4e85b1219295cad4b5851b035575bc293010John McCall}
4581f82b4e85b1219295cad4b5851b035575bc293010John McCall
4582d1a32c328bce903fb1b17fc8147b646be818298eReid Klecknervoid Sema::adjustMemberFunctionCC(QualType &T, bool IsStatic) {
4583ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  const FunctionType *FT = T->castAs<FunctionType>();
4584ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  bool IsVariadic = (isa<FunctionProtoType>(FT) &&
4585ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner                     cast<FunctionProtoType>(FT)->isVariadic());
4586ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  CallingConv CC = FT->getCallConv();
4587d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner
4588d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner  // Only adjust types with the default convention.  For example, on Windows we
4589d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner  // should adjust a __cdecl type to __thiscall for instance methods, and a
4590d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner  // __thiscall type to __cdecl for static methods.
4591ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  CallingConv DefaultCC =
4592d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner      Context.getDefaultCallingConvention(IsVariadic, IsStatic);
4593ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  if (CC != DefaultCC)
4594ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    return;
4595ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
4596ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  // Check if there was an explicit attribute, but only look through parens.
4597ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  // The intent is to look for an attribute on the current declarator, but not
4598ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  // one that came from a typedef.
4599ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  QualType R = T.IgnoreParens();
4600ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  while (const AttributedType *AT = dyn_cast<AttributedType>(R)) {
4601ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    if (AT->isCallingConv())
4602ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      return;
4603ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    R = AT->getModifiedType().IgnoreParens();
4604ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  }
4605ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
4606ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  // FIXME: This loses sugar.  This should probably be fixed with an implicit
4607ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  // AttributedType node that adjusts the convention.
4608d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner  CC = Context.getDefaultCallingConvention(IsVariadic, !IsStatic);
4609ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  FT = Context.adjustFunctionType(FT, FT->getExtInfo().withCallingConv(CC));
4610ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  FunctionTypeUnwrapper Unwrapped(*this, T);
4611ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  T = Unwrapped.wrap(*this, FT);
4612ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner}
4613ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
4614207f4d8543529221932af82836016a2ef066c917Peter Collingbourne/// Handle OpenCL image access qualifiers: read_only, write_only, read_write
4615207f4d8543529221932af82836016a2ef066c917Peter Collingbournestatic void HandleOpenCLImageAccessAttribute(QualType& CurType,
4616207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const AttributeList &Attr,
4617207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             Sema &S) {
4618207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  // Check the attribute arguments.
4619207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (Attr.getNumArgs() != 1) {
4620baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4621baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
4622207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4623207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4624207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4625624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Expr *sizeExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4626207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  llvm::APSInt arg(32);
4627207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4628207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      !sizeExpr->isIntegerConstantExpr(arg, S.Context)) {
46299f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
46309f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
46319f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << sizeExpr->getSourceRange();
4632207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4633207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4634207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4635207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  unsigned iarg = static_cast<unsigned>(arg.getZExtValue());
4636207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  switch (iarg) {
4637207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_only:
4638207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_write_only:
4639207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_write:
4640207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4641207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4642207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  default:
4643207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4644207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
4645207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << sizeExpr->getSourceRange();
4646207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4647207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4648207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4649207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
4650207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
46516e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
46526e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
46536e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
46546e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
46556e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
46566e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
46576e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
4658788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
4659788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
466056affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
46616e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
4662baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4663baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
4664e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
46656e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
46666e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
4667624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Expr *sizeExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
46686e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
4669ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4670ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
46719f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
46729f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
46739f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << sizeExpr->getSourceRange();
4674e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
46756e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
46766e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
4677b445cb9662067b7c7586815937b07828ede9bb49Aaron Ballman  // The base type must be integer (not Boolean or enumeration) or float, and
4678b445cb9662067b7c7586815937b07828ede9bb49Aaron Ballman  // can't already be a vector.
4679b445cb9662067b7c7586815937b07828ede9bb49Aaron Ballman  if (!CurType->isBuiltinType() || CurType->isBooleanType() ||
46801652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
46816e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
4682e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
46836e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
46846e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
46856e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
46866e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
46876e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
46886e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
46896e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
46906e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
46916e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
46926e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4693e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
46946e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
46956e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
46961652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman  if (VectorType::isVectorSizeTooLarge(vectorSize / typeSize)) {
46971652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    S.Diag(Attr.getLoc(), diag::err_attribute_size_too_large)
46981652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      << sizeExpr->getSourceRange();
46991652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    Attr.setInvalid();
47001652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    return;
47011652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman  }
47026e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
47036e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
47046e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4705e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
47066e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
47076e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
47086e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
47096e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
47106e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
4711788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
4712e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
47136e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
47146e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
47154ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// \brief Process the OpenCL-like ext_vector_type attribute when it occurs on
47164ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// a type.
471791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierstatic void HandleExtVectorTypeAttr(QualType &CurType,
471891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                                    const AttributeList &Attr,
47194ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                    Sema &S) {
4720624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  // check the attribute arguments.
4721624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (Attr.getNumArgs() != 1) {
4722624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4723624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman      << Attr.getName() << 1;
4724624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    return;
4725624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  }
4726624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
47274ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  Expr *sizeExpr;
472891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
47294ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Special case where the argument is a template id.
4730624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (Attr.isArgIdent(0)) {
47314ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CXXScopeSpec SS;
4732e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    SourceLocation TemplateKWLoc;
47334ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    UnqualifiedId id;
4734624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    id.setIdentifier(Attr.getArgAsIdent(0)->Ident, Attr.getLoc());
4735e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
4736e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    ExprResult Size = S.ActOnIdExpression(S.getCurScope(), SS, TemplateKWLoc,
4737e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          id, false, false);
47384ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Size.isInvalid())
47394ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
474091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
47414ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Size.get();
47424ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  } else {
4743624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    sizeExpr = Attr.getArgAsExpr(0);
47444ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  }
474591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
47464ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Create the vector type.
47474ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  QualType T = S.BuildExtVectorType(CurType, sizeExpr, Attr.getLoc());
47484ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (!T.isNull())
47494ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CurType = T;
47504ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor}
47514ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
4752b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northoverstatic bool isPermittedNeonBaseType(QualType &Ty,
4753b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover                                    VectorType::VectorKind VecKind,
4754b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover                                    bool IsAArch64) {
4755b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  const BuiltinType *BTy = Ty->getAs<BuiltinType>();
4756b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  if (!BTy)
4757b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    return false;
4758b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
4759b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  if (VecKind == VectorType::NeonPolyVector) {
4760b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    if (IsAArch64) {
4761b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      // AArch64 polynomial vectors are unsigned
4762b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      return BTy->getKind() == BuiltinType::UChar ||
4763b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover             BTy->getKind() == BuiltinType::UShort;
4764b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    } else {
4765b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      // AArch32 polynomial vector are signed.
4766b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      return BTy->getKind() == BuiltinType::SChar ||
4767b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover             BTy->getKind() == BuiltinType::Short;
4768b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    }
4769b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  }
4770b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
4771b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  // Non-polynomial vector types: the usual suspects are allowed, as well as
4772b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  // float64_t on AArch64.
4773b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  if (IsAArch64 && BTy->getKind() == BuiltinType::Double)
4774b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    return true;
4775b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
4776b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  return BTy->getKind() == BuiltinType::SChar ||
4777b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::UChar ||
4778b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Short ||
4779b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::UShort ||
4780b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Int ||
4781b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::UInt ||
4782b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::LongLong ||
4783b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::ULongLong ||
4784b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Float ||
4785b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Half;
4786b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover}
4787b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
47884211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
47894211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
47904211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
47914211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
47924211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
47934211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
47944211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
47954211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
47964211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
47979f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman                                     VectorType::VectorKind VecKind) {
47982440fb1f91557912f8c43cb72201170254ae09f4Amara Emerson  // Target must have NEON
47992440fb1f91557912f8c43cb72201170254ae09f4Amara Emerson  if (!S.Context.getTargetInfo().hasFeature("neon")) {
48002440fb1f91557912f8c43cb72201170254ae09f4Amara Emerson    S.Diag(Attr.getLoc(), diag::err_attribute_unsupported) << Attr.getName();
48012440fb1f91557912f8c43cb72201170254ae09f4Amara Emerson    Attr.setInvalid();
48022440fb1f91557912f8c43cb72201170254ae09f4Amara Emerson    return;
48032440fb1f91557912f8c43cb72201170254ae09f4Amara Emerson  }
48044211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
48054211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
4806baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4807baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
48084211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
48094211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
48104211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
48114211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
4812624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
48134211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
48144211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
48154211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
48169f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
48179f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
48189f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << numEltsExpr->getSourceRange();
48194211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
48204211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
48214211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
48224211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
4823e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover  llvm::Triple::ArchType Arch =
4824b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover        S.Context.getTargetInfo().getTriple().getArch();
4825e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover  if (!isPermittedNeonBaseType(CurType, VecKind,
4826e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover                               Arch == llvm::Triple::aarch64)) {
4827e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
4828e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover    Attr.setInvalid();
4829e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover    return;
48304211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
4831e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover
48324211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
48334211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
48344211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
48354211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
48364211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
48374211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
48384211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
48394211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
48404211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
48414211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
48424211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
48434211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
48444211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
4845711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
4846f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             TypeAttrLocation TAL, AttributeList *attrs) {
4847c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
4848c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
4849c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
4850c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
4851711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4852711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
4853711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
4854711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
4855711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr.getNext();
4856711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4857e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
4858711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
4859e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
4860e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
4861cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    if (attr.isCXX11Attribute()) {
4862cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // [[gnu::...]] attributes are treated as declaration attributes, so may
4863cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // not appertain to a DeclaratorChunk, even if we handle them as type
4864cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // attributes.
4865cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.getScopeName() && attr.getScopeName()->isStr("gnu")) {
4866cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        if (TAL == TAL_DeclChunk) {
4867cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          state.getSema().Diag(attr.getLoc(),
4868cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                               diag::warn_cxx11_gnu_attribute_on_type)
4869cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith              << attr.getName();
4870cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          continue;
4871cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        }
4872cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      } else if (TAL != TAL_DeclChunk) {
4873cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // Otherwise, only consider type processing for a C++11 attribute if
4874cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // it's actually been applied to a type.
4875cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        continue;
4876cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      }
4877f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    }
4878f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
4879b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
4880b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
4881711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
4882cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    default:
4883cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // A C++11 attribute on a declarator chunk must appertain to a type.
4884d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk) {
4885cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(), diag::err_attribute_not_type_attr)
4886d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith          << attr.getName();
4887d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith        attr.setUsedAsTypeAttr();
4888d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      }
4889cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
4890cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
4891cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::UnknownAttribute:
4892cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk)
4893cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(),
4894cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                             diag::warn_unknown_attribute_ignored)
4895cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          << attr.getName();
4896cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
4897cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
4898cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::IgnoredAttribute:
4899cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
490004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
49018e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_MayAlias:
4902682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // FIXME: This attribute needs to actually be handled, but if we ignore
4903682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // it it breaks large amounts of Linux software.
4904682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      attr.setUsedAsTypeAttr();
4905682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      break;
49068e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_AddressSpace:
4907711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
4908e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4909c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
4910711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
4911711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
4912711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
4913e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4914d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
49158e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_VectorSize:
4916711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
4917e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4918f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
49198e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ExtVectorType:
4920a4fa9008988985f9cf01712a99ddd923aea278a0Richard Smith      HandleExtVectorTypeAttr(type, attr, state.getSema());
4921e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
49224ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      break;
49238e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonVectorType:
4924711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
49259f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman                               VectorType::NeonVector);
4926e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
49274211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
49288e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonPolyVectorType:
4929711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
49309f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman                               VectorType::NeonPolyVector);
4931e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
49324211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
49338e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_OpenCLImageAccess:
4934207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      HandleOpenCLImageAccessAttribute(type, attr, state.getSema());
4935e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4936207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      break;
4937207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
4938d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith    case AttributeList::AT_Win64:
4939d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      attr.setUsedAsTypeAttr();
4940d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      break;
4941aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    MS_TYPE_ATTRS_CASELIST:
4942aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      if (!handleMSPointerTypeQualifierAttr(state, attr, type))
4943aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        attr.setUsedAsTypeAttr();
4944aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      break;
4945d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith
49468e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
49474e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
4948cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        break;
4949f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough into the function attrs
4950f85e193739c953358c865005855253af4f68a497John McCall
4951711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
4952e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4953e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
4954711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
4955711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
4956f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      if (TAL == TAL_DeclSpec)
4957711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
4958711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4959711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
4960711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
4961711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
49626e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
4963c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
4964711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attrs = next));
4965232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
4966232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
4967e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \brief Ensure that the type of the given expression is complete.
4968e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4969e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// This routine checks whether the expression \p E has a complete type. If the
4970e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// expression refers to an instantiable construct, that instantiation is
4971e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// performed as needed to complete its type. Furthermore
4972e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// Sema::RequireCompleteType is called for the expression's type (or in the
4973e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// case of a reference type, the referred-to type).
4974e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4975e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \param E The expression whose type is required to be complete.
4976d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// \param Diagnoser The object that will emit a diagnostic if the type is
4977d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// incomplete.
4978e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4979e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \returns \c true if the type of \p E is incomplete and diagnosed, \c false
4980e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// otherwise.
4981f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, TypeDiagnoser &Diagnoser){
4982e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  QualType T = E->getType();
4983e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4984e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Fast path the case where the type is already complete.
4985e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (!T->isIncompleteType())
498663f25e6719e3920a04f66a80958b792ac268aeddRichard Smith    // FIXME: The definition might not be visible.
4987e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    return false;
4988e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4989e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Incomplete array types may be completed by the initializer attached to
49906aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith  // their definitions. For static data members of class templates and for
49916aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith  // variable templates, we need to instantiate the definition to get this
49926aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith  // initializer and complete the type.
4993e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (T->isIncompleteArrayType()) {
4994e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4995e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
49966aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith        if (isTemplateInstantiation(Var->getTemplateSpecializationKind())) {
49976aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith          SourceLocation PointOfInstantiation = E->getExprLoc();
499891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
49996aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith          if (MemberSpecializationInfo *MSInfo =
50006aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith                  Var->getMemberSpecializationInfo()) {
500136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // If we don't already have a point of instantiation, this is it.
500236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (MSInfo->getPointOfInstantiation().isInvalid()) {
50036aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith              MSInfo->setPointOfInstantiation(PointOfInstantiation);
500491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
500591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier              // This is a modification of an existing AST node. Notify
500636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              // listeners.
500736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              if (ASTMutationListener *L = getASTMutationListener())
500836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                L->StaticDataMemberInstantiated(Var);
500936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
50106aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith          } else {
50116aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith            VarTemplateSpecializationDecl *VarSpec =
50126aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith                cast<VarTemplateSpecializationDecl>(Var);
50136aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith            if (VarSpec->getPointOfInstantiation().isInvalid())
50146aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith              VarSpec->setPointOfInstantiation(PointOfInstantiation);
50156aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith          }
501691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
50176aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith          InstantiateVariableDefinition(PointOfInstantiation, Var);
501891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
50196aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith          // Update the type to the newly instantiated definition's type both
50206aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith          // here and within the expression.
50216aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith          if (VarDecl *Def = Var->getDefinition()) {
50226aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith            DRE->setDecl(Def);
50236aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith            T = Def->getType();
50246aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith            DRE->setType(T);
50256aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith            E->setType(T);
5026f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor          }
502791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5028e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // We still go on to try to complete the type independently, as it
5029e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // may also require instantiations or diagnostics if it remains
5030e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // incomplete.
5031e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        }
5032e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      }
5033e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    }
5034e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  }
5035e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
5036e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // FIXME: Are there other cases which require instantiating something other
5037e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // than the type to complete the type of an expression?
5038e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
5039e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Look through reference types and complete the referred type.
5040e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
5041e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    T = Ref->getPointeeType();
5042e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
5043d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(E->getExprLoc(), T, Diagnoser);
5044d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
5045d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
5046d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregornamespace {
5047f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  struct TypeDiagnoserDiag : Sema::TypeDiagnoser {
5048d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    unsigned DiagID;
504991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5050f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor    TypeDiagnoserDiag(unsigned DiagID)
5051f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor      : Sema::TypeDiagnoser(DiagID == 0), DiagID(DiagID) {}
505291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5053d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) {
5054d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      if (Suppressed) return;
5055d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      S.Diag(Loc, DiagID) << T;
5056d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    }
5057d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  };
5058d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
5059d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
5060d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, unsigned DiagID) {
5061f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
5062d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteExprType(E, Diagnoser);
5063e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth}
5064e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
50651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
50664ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
50674ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
50684ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
506986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
507086447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
507186447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
507286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
507386447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
50744ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
50754ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
50764ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
50774ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
50784ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
50794ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
50804ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
50814ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
508291a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
5083f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                               TypeDiagnoser &Diagnoser) {
5084658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  if (RequireCompleteTypeImpl(Loc, T, Diagnoser))
5085658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie    return true;
5086658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  if (const TagType *Tag = T->getAs<TagType>()) {
5087658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie    if (!Tag->getDecl()->isCompleteDefinitionRequired()) {
5088658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie      Tag->getDecl()->setCompleteDefinitionRequired();
5089658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie      Consumer.HandleTagDeclRequiredDefinition(Tag->getDecl());
5090658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie    }
5091658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  }
5092658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  return false;
5093658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie}
5094658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie
5095658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie/// \brief The implementation of RequireCompleteType
5096658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikiebool Sema::RequireCompleteTypeImpl(SourceLocation Loc, QualType T,
5097658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie                                   TypeDiagnoser &Diagnoser) {
5098573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
5099573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
5100690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
5101690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
5102690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
51031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
5104690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
5105690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
51064ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
5107d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  NamedDecl *Def = 0;
5108d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  if (!T->isIncompleteType(&Def)) {
5109d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    // If we know about the definition but it is not visible, complain.
5110b775100fea6d8955149897dae1adca50ca471d17Richard Smith    if (!Diagnoser.Suppressed && Def && !LookupResult::isVisible(*this, Def)) {
5111d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // Suppress this error outside of a SFINAE context if we've already
511291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // emitted the error once for this type. There's no usefulness in
5113d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // repeating the diagnostic.
5114d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // FIXME: Add a Fix-It that imports the corresponding module or includes
5115d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // the header.
5116ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Module *Owner = Def->getOwningModule();
5117ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Loc, diag::err_module_private_definition)
5118ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        << T << Owner->getFullModuleName();
5119ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Def->getLocation(), diag::note_previous_definition);
5120ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
5121ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      if (!isSFINAEContext()) {
5122ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        // Recover by implicitly importing this module.
5123ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        createImplicitModuleImport(Loc, Owner);
5124d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      }
5125d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    }
512691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
51274ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
5128d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
51294ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
5130b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // FIXME: If there's an unimported definition of this type in a module (for
5131b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // instance, because we forward declared it, then imported the definition),
5132b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // import that definition now.
5133b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // FIXME: What about other cases where an import extends a redeclaration
5134b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // chain for a declaration that can be accessed through a mechanism other
5135b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // than name lookup (eg, referenced in a template, or a variable whose type
5136b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // could be completed by the module)?
5137b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith
5138bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const TagType *Tag = T->getAs<TagType>();
5139bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const ObjCInterfaceType *IFace = 0;
514091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5141bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  if (Tag) {
5142bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
5143bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->isInvalidDecl())
5144bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
5145bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan
5146bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
5147bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->hasExternalLexicalStorage()) {
5148bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(Tag->getDecl());
5149bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!Tag->isIncompleteType())
5150bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
5151bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
5152bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
5153bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  else if ((IFace = T->getAs<ObjCInterfaceType>())) {
5154bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
5155bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->isInvalidDecl())
5156bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
515791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5158bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
5159bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->hasExternalLexicalStorage()) {
5160bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(IFace->getDecl());
5161bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!IFace->isIncompleteType())
5162bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
5163bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
5164bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
516591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5166d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
5167923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
5168923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
5169923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
5170e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor  while (const ConstantArrayType *Array
5171e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor           = Context.getAsConstantArrayType(MaybeTemplate))
5172923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
5173923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
51742943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
5175d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
5176972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
5177972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
5178d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
5179d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                            /*Complain=*/!Diagnoser.Suppressed);
51801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
5181d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
5182564f4c5664f552becbd05407611a92754c40e641Richard Smith      CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass();
5183564f4c5664f552becbd05407611a92754c40e641Richard Smith      if (!Rec->isBeingDefined() && Pattern) {
5184564f4c5664f552becbd05407611a92754c40e641Richard Smith        MemberSpecializationInfo *MSI = Rec->getMemberSpecializationInfo();
5185564f4c5664f552becbd05407611a92754c40e641Richard Smith        assert(MSI && "Missing member specialization information?");
5186357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
5187564f4c5664f552becbd05407611a92754c40e641Richard Smith        if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
5188f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
5189f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
5190f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
5191d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                  /*Complain=*/!Diagnoser.Suppressed);
5192d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
5193d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
5194d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
51952943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
5196d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  if (Diagnoser.Suppressed)
51975842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
5198d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
51994ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
5200cac18add73d095eaab600aefe27ea7174aec4922Nico Weber  if (Ident___float128 &&
5201cac18add73d095eaab600aefe27ea7174aec4922Nico Weber      T == Context.getTypeDeclType(Context.getFloat128StubType())) {
5202cac18add73d095eaab600aefe27ea7174aec4922Nico Weber    Diag(Loc, diag::err_typecheck_decl_incomplete_type___float128);
5203cac18add73d095eaab600aefe27ea7174aec4922Nico Weber    return true;
5204cac18add73d095eaab600aefe27ea7174aec4922Nico Weber  }
5205cac18add73d095eaab600aefe27ea7174aec4922Nico Weber
5206d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
520791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
52084ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
520901620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
52104ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
52111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
52124ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
52134ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
5214b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor      << QualType(Tag, 0);
521591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5216b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  // If the Objective-C class was a forward declaration, produce a note.
5217b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  if (IFace && !IFace->getDecl()->isInvalidDecl())
5218b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor    Diag(IFace->getDecl()->getLocation(), diag::note_forward_class);
52194ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
52205d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain  // If we have external information that we can use to suggest a fix,
52215d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain  // produce a note.
52225d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain  if (ExternalSource)
52235d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain    ExternalSource->MaybeDiagnoseMissingCompleteType(Loc, T);
52245d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain
52254ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
52264ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
5227e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
5228fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
522991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                               unsigned DiagID) {
5230f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
5231d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(Loc, T, Diagnoser);
5232fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
5233fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
52346666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \brief Get diagnostic %select index for tag kind for
52356666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// literal type diagnostic message.
52366666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// WARNING: Indexes apply to particular diagnostics only!
52376666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos///
52386666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \returns diagnostic %select index.
5239f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matosstatic unsigned getLiteralDiagFromTagKind(TagTypeKind Tag) {
52406666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  switch (Tag) {
5241f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Struct: return 0;
5242f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Interface: return 1;
5243f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Class:  return 2;
5244f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  default: llvm_unreachable("Invalid tag kind for literal type diagnostic!");
52456666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  }
52466666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos}
52476666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos
52489f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @brief Ensure that the type T is a literal type.
52499f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
52509f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// This routine checks whether the type @p T is a literal type. If @p T is an
52519f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// incomplete type, an attempt is made to complete it. If @p T is a literal
52529f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// type, or @p AllowIncompleteType is true and @p T is an incomplete type,
52539f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// returns false. Otherwise, this routine issues the diagnostic @p PD (giving
52549f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// it the type @p T), along with notes explaining why the type is not a
52559f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// literal type, and returns true.
52569f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
52579f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param Loc  The location in the source that the non-literal type
52589f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// diagnostic should refer to.
52599f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
52609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param T  The type that this routine is examining for literalness.
52619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
5262f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor/// @param Diagnoser Emits a diagnostic if T is not a literal type.
52639f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
52649f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @returns @c true if @p T is not a literal type and a diagnostic was emitted,
52659f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @c false otherwise.
52669f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithbool Sema::RequireLiteralType(SourceLocation Loc, QualType T,
5267f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                              TypeDiagnoser &Diagnoser) {
52689f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  assert(!T->isDependentType() && "type should not be dependent");
52699f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5270ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  QualType ElemType = Context.getBaseElementType(T);
5271ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  RequireCompleteType(Loc, ElemType, 0);
5272ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
5273a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  if (T->isLiteralType(Context))
52749f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return false;
52759f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5276f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  if (Diagnoser.Suppressed)
52779f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
52789f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5279f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
52809f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
52819f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (T->isVariableArrayType())
52829f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
52839f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5284ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  const RecordType *RT = ElemType->getAs<RecordType>();
52859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (!RT)
52869f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
52879f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
52889f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
52899f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
5290c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // A partially-defined class type can't be a literal type, because a literal
5291c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // class type must have a trivial destructor (which can't be checked until
5292c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // the class definition is complete).
5293c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  if (!RD->isCompleteDefinition()) {
5294d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T);
5295ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman    return true;
5296c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  }
5297ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
52989f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // If the class has virtual base classes, then it's not an aggregate, and
529986c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // cannot have any constexpr constructors or a trivial default constructor,
530086c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // so is non-literal. This is better to diagnose than the resulting absence
530186c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // of constexpr constructors.
53029f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (RD->getNumVBases()) {
53039f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_virtual_base)
53046666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      << getLiteralDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases();
53059f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
53069f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           E = RD->vbases_end(); I != E; ++I)
530796a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      Diag(I->getLocStart(),
53089f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           diag::note_constexpr_virtual_base_here) << I->getSourceRange();
530986c3ae46250cdcc57778c27826060779a92f3815Richard Smith  } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor() &&
531086c3ae46250cdcc57778c27826060779a92f3815Richard Smith             !RD->hasTrivialDefaultConstructor()) {
53119f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD;
53129f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (RD->hasNonLiteralTypeFieldsOrBases()) {
53139f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
53149f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->bases_end(); I != E; ++I) {
5315a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith      if (!I->getType()->isLiteralType(Context)) {
531696a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        Diag(I->getLocStart(),
53179f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith             diag::note_non_literal_base_class)
53189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith          << RD << I->getType() << I->getSourceRange();
53199f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
53209f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
53219f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
53229f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::field_iterator I = RD->field_begin(),
53239f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->field_end(); I != E; ++I) {
5324a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith      if (!I->getType()->isLiteralType(Context) ||
5325262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          I->getType().isVolatileQualified()) {
5326262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        Diag(I->getLocation(), diag::note_non_literal_field)
5327581deb3da481053c4993c7600f97acf7768caac5David Blaikie          << RD << *I << I->getType()
5328262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          << I->getType().isVolatileQualified();
53299f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
53309f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
53319f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
53329f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (!RD->hasTrivialDestructor()) {
53339f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // All fields and bases are of literal types, so have trivial destructors.
53349f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // If this class's destructor is non-trivial it must be user-declared.
53359f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    CXXDestructorDecl *Dtor = RD->getDestructor();
53369f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    assert(Dtor && "class has literal fields and bases but no dtor?");
53379f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    if (!Dtor)
53389f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      return true;
53399f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
53409f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(Dtor->getLocation(), Dtor->isUserProvided() ?
53419f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_user_provided_dtor :
53429f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_nontrivial_dtor) << RD;
5343ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    if (!Dtor->isUserProvided())
5344ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith      SpecialMemberIsTrivial(Dtor, CXXDestructor, /*Diagnose*/true);
53459f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  }
53469f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
53479f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return true;
53489f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
53499f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
535091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierbool Sema::RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID) {
5351f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
5352f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  return RequireLiteralType(Loc, T, Diagnoser);
5353f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor}
5354f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor
5355465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
5356465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
5357465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
5358465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
5359465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
5360e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
5361465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
5362e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
5363465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5364465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
5365465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
5366465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
5367465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
5368465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
5369465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
5370e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
5371af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
53722a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
5373fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
53742a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
53752a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
53762a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
53772b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
53782b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
5379aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
5380aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
53812b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
5382af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
5383af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
5384af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
5385f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// getDecltypeForExpr - Given an expr, will return the decltype for
5386f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// that expression, according to the rules in C++11
5387f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18.
5388f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregorstatic QualType getDecltypeForExpr(Sema &S, Expr *E) {
5389f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (E->isTypeDependent())
5390f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    return S.Context.DependentTy;
5391f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
53926d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
53936d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   The type denoted by decltype(e) is defined as follows:
53946d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //
53956d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //     - if e is an unparenthesized id-expression or an unparenthesized class
539691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       member access (5.2.5), decltype(e) is the type of the entity named
539791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       by e. If there is no such entity, or if e names a set of overloaded
53986d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       functions, the program is ill-formed;
539984dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  //
540084dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  // We apply the same rules for Objective-C ivar and property references.
5401f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
5402f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
5403f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return VD->getType();
540484dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
5405f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
5406f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return FD->getType();
540784dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCIvarRefExpr *IR = dyn_cast<ObjCIvarRefExpr>(E)) {
540884dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    return IR->getDecl()->getType();
540984dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCPropertyRefExpr *PR = dyn_cast<ObjCPropertyRefExpr>(E)) {
541084dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    if (PR->isExplicitProperty())
541184dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor      return PR->getExplicitProperty()->getType();
5412f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
541384dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor
5414f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  // C++11 [expr.lambda.prim]p18:
5415f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   Every occurrence of decltype((x)) where x is a possibly
5416f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   parenthesized id-expression that names an entity of automatic
5417f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   storage duration is treated as if x were transformed into an
5418f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   access to a corresponding data member of the closure type that
5419f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   would have been declared if x were an odr-use of the denoted
5420f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   entity.
5421f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  using namespace sema;
5422f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (S.getCurLambda()) {
5423f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (isa<ParenExpr>(E)) {
5424f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
5425f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
542668932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          QualType T = S.getCapturedDeclRefType(Var, DRE->getLocation());
542768932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          if (!T.isNull())
542868932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor            return S.Context.getLValueReferenceType(T);
5429f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        }
5430f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      }
5431f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    }
5432f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
5433f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
5434f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
54356d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
54366d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   [...]
54376d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  QualType T = E->getType();
54386d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  switch (E->getValueKind()) {
543991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the
54406d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
54416d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_XValue: T = S.Context.getRValueReferenceType(T); break;
544291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an lvalue, decltype(e) is T&, where T is the
54436d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
54446d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_LValue: T = S.Context.getLValueReferenceType(T); break;
54456d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //  - otherwise, decltype(e) is the type of e.
54466d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_RValue: break;
54476d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  }
544891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5449f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return T;
5450f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor}
5451f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
54522a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
5453fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
54542a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
54552a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
545691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5457f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return Context.getDecltypeType(E, getDecltypeForExpr(*this, E));
5458af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
5459ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
5460ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType Sema::BuildUnaryTransformType(QualType BaseType,
5461ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::UTTKind UKind,
5462ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       SourceLocation Loc) {
5463ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  switch (UKind) {
5464ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case UnaryTransformType::EnumUnderlyingType:
5465ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) {
5466ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Diag(Loc, diag::err_only_enums_have_underlying_types);
5467ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return QualType();
5468ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    } else {
5469ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      QualType Underlying = BaseType;
5470ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      if (!BaseType->isDependentType()) {
5471ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl();
5472ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        assert(ED && "EnumType has no EnumDecl");
5473ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        DiagnoseUseOfDecl(ED, Loc);
5474ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        Underlying = ED->getIntegerType();
5475ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      }
5476ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(!Underlying.isNull());
5477ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return Context.getUnaryTransformType(BaseType, Underlying,
5478ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                        UnaryTransformType::EnumUnderlyingType);
5479ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
5480ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  }
5481ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  llvm_unreachable("unknown unary transform type");
5482ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
5483b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5484b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) {
5485b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T->isDependentType()) {
54868327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // FIXME: It isn't entirely clear whether incomplete atomic types
54878327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // are allowed or not; for simplicity, ban them for the moment.
5488d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (RequireCompleteType(Loc, T, diag::err_atomic_specifier_bad_type, 0))
54898327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith      return QualType();
54908327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith
5491b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    int DisallowedKind = -1;
54928327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    if (T->isArrayType())
5493b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 1;
5494b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isFunctionType())
5495b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 2;
5496b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isReferenceType())
5497b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 3;
5498b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isAtomicType())
5499b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 4;
5500b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T.hasQualifiers())
5501b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 5;
5502b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (!T.isTriviallyCopyableType(Context))
5503b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      // Some other non-trivially-copyable type (probably a C++ class)
5504b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 6;
5505b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5506b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (DisallowedKind != -1) {
5507b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T;
5508b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return QualType();
5509b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
5510b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5511b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // FIXME: Do we need any handling for ARC here?
5512b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
5513b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5514b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Build the pointer type.
5515b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return Context.getAtomicType(T);
5516b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
5517