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
14651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "TypeLocBuilder.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"
2491a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
25d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis#include "clang/Basic/TargetInfo.h"
2687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar#include "clang/Lex/Preprocessor.h"
2719510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
28f85e193739c953358c865005855253af4f68a497John McCall#include "clang/Sema/DelayedDiagnostic.h"
29d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor#include "clang/Sema/Lookup.h"
3055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/ScopeInfo.h"
314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar#include "clang/Sema/SemaInternal.h"
3255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Template.h"
334994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl#include "llvm/ADT/SmallPtrSet.h"
348c952cd40ccec9d720931f27e7d722fed207d536Richard Smith#include "llvm/ADT/SmallString.h"
354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar#include "llvm/ADT/StringSwitch.h"
3687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor#include "llvm/Support/ErrorHandling.h"
37ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
40fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballmanenum TypeDiagSelector {
41fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  TDS_Function,
42fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  TDS_Pointer,
43fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  TDS_ObjCObjOrBlock
44fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman};
45fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman
465db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// isOmittedBlockReturnType - Return true if this declarator is missing a
4791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier/// return type because this is a omitted return type on a block literal.
488ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic bool isOmittedBlockReturnType(const Declarator &D) {
495db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getContext() != Declarator::BlockLiteralContext ||
508ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl      D.getDeclSpec().hasTypeSpecifier())
515db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return false;
5291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
535db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 0)
54a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^{ ... }
5591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
565db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 1 &&
575db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      D.getTypeObject(0).Kind == DeclaratorChunk::Function)
58a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^(int X, float Y) { ... }
5991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
605db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  return false;
615db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner}
625db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
632792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// diagnoseBadTypeAttribute - Diagnoses a type attribute which
642792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// doesn't apply to the given type.
652792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCallstatic void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr,
662792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall                                     QualType type) {
67fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  TypeDiagSelector WhichType;
68fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  bool useExpansionLoc = true;
692792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  switch (attr.getKind()) {
70fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  case AttributeList::AT_ObjCGC:        WhichType = TDS_Pointer; break;
71fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman  case AttributeList::AT_ObjCOwnership: WhichType = TDS_ObjCObjOrBlock; break;
722792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  default:
732792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    // Assume everything else was a function attribute.
74fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman    WhichType = TDS_Function;
75fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman    useExpansionLoc = false;
762792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    break;
772792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  }
782792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
792792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  SourceLocation loc = attr.getLoc();
805f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef name = attr.getName()->getName();
812792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
822792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  // The GC attributes are usually written with macros;  special-case them.
836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  IdentifierInfo *II = attr.isArgIdent(0) ? attr.getArgAsIdent(0)->Ident
846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                          : nullptr;
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
1034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar// Calling convention attributes.
1044967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar#define CALLING_CONV_ATTRS_CASELIST \
1058e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_CDecl: \
1068e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_FastCall: \
1078e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_StdCall: \
1088e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ThisCall: \
1098e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Pascal: \
1104967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case AttributeList::AT_SwiftCall: \
111176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    case AttributeList::AT_VectorCall: \
112e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    case AttributeList::AT_MSABI: \
113e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    case AttributeList::AT_SysVABI: \
114263366f9241366f29ba65b703120f302490c39ffDerek Schuff    case AttributeList::AT_Pcs: \
1154967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case AttributeList::AT_IntelOclBicc: \
1164967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case AttributeList::AT_PreserveMost: \
1174967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case AttributeList::AT_PreserveAll
1184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
1194967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar// Function type attributes.
1204967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar#define FUNCTION_TYPE_ATTRS_CASELIST \
1214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case AttributeList::AT_NoReturn: \
1224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case AttributeList::AT_Regparm: \
1234967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    CALLING_CONV_ATTRS_CASELIST
124aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
125aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman// Microsoft-specific type qualifiers.
126aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman#define MS_TYPE_ATTRS_CASELIST  \
127aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_Ptr32: \
128aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_Ptr64: \
129aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_SPtr: \
130aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    case AttributeList::AT_UPtr
131711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
13287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar// Nullability qualifiers.
13387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar#define NULLABILITY_TYPE_ATTRS_CASELIST         \
13487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case AttributeList::AT_TypeNonNull:         \
13587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case AttributeList::AT_TypeNullable:        \
13687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case AttributeList::AT_TypeNullUnspecified
13787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
138711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace {
139711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// An object which stores processing state for the entire
140711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// GetTypeForDeclarator process.
141711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  class TypeProcessingState {
142711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &sema;
143711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
144711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The declarator being processed.
145711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &declarator;
146711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
147711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The index of the declarator chunk we're currently processing.
148711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// May be the total number of valid chunks, indicating the
149711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// DeclSpec.
150711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex;
151711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
152711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Whether there are non-trivial modifications to the decl spec.
153711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    bool trivial;
154711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1557ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    /// Whether we saved the attributes in the decl spec.
1567ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    bool hasSavedAttrs;
1577ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
158711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The original set of attributes on the DeclSpec.
1595f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> savedAttrs;
160711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
161711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// A list of attributes to diagnose the uselessness of when the
162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// processing is complete.
1635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> ignoredTypeAttrs;
164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  public:
166711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    TypeProcessingState(Sema &sema, Declarator &declarator)
167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : sema(sema), declarator(declarator),
168711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        chunkIndex(declarator.getNumTypeObjects()),
1697ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        trivial(true), hasSavedAttrs(false) {}
170711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &getSema() const {
172711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return sema;
173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
175711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &getDeclarator() const {
176711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator;
177711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
178711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
179b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    bool isProcessingDeclSpec() const {
180b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      return chunkIndex == declarator.getNumTypeObjects();
181b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
182b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
183711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned getCurrentChunkIndex() const {
184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return chunkIndex;
185711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
186711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
187711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void setCurrentChunkIndex(unsigned idx) {
188711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(idx <= declarator.getNumTypeObjects());
189711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      chunkIndex = idx;
190711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
191711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
192711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&getCurrentAttrListRef() const {
193b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (isProcessingDeclSpec())
194711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        return getMutableDeclSpec().getAttributes().getListRef();
195711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator.getTypeObject(chunkIndex).getAttrListRef();
196711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
197711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
198711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Save the current set of attributes on the DeclSpec.
199711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void saveDeclSpecAttrs() {
200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Don't try to save them multiple times.
2017ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (hasSavedAttrs) return;
202711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
203711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      DeclSpec &spec = getMutableDeclSpec();
204711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (AttributeList *attr = spec.getAttributes().getList(); attr;
205711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             attr = attr->getNext())
206711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs.push_back(attr);
207711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      trivial &= savedAttrs.empty();
2087ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      hasSavedAttrs = true;
209711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
210711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
211711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Record that we had nowhere to put the given type attribute.
212711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// We will diagnose such attributes later.
213711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void addIgnoredTypeAttr(AttributeList &attr) {
214711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      ignoredTypeAttrs.push_back(&attr);
215711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
216711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
217711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Diagnose all the ignored type attributes, given that the
218711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// declarator worked out to the given type.
219711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void diagnoseIgnoredTypeAttrs(QualType type) const {
22087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      for (auto *Attr : ignoredTypeAttrs)
22187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        diagnoseBadTypeAttribute(getSema(), *Attr, type);
222711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
223711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
224711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    ~TypeProcessingState() {
225711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (trivial) return;
226711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
227711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      restoreDeclSpecAttrs();
228711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
229711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
230711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  private:
231711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclSpec &getMutableDeclSpec() const {
232711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return const_cast<DeclSpec&>(declarator.getDeclSpec());
233711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
234711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
235711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void restoreDeclSpecAttrs() {
2367ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      assert(hasSavedAttrs);
2377ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
2387ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (savedAttrs.empty()) {
2396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        getMutableDeclSpec().getAttributes().set(nullptr);
2407ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        return;
2417ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      }
2427ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
243711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
244711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
245711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs[i]->setNext(savedAttrs[i+1]);
2466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      savedAttrs.back()->setNext(nullptr);
247711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
248711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
2494967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} // end anonymous namespace
250711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
251711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
252711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  attr.setNext(head);
253711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  head = &attr;
254711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
255711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
256711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
257711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (head == &attr) {
258711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    head = attr.getNext();
259711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
26004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
261711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
262711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *cur = head;
263711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  while (true) {
264711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    assert(cur && cur->getNext() && "ran out of attrs?");
265711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (cur->getNext() == &attr) {
266711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      cur->setNext(attr.getNext());
267711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
268711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
269711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    cur = cur->getNext();
270711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
271711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
272711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
273711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void moveAttrFromListToList(AttributeList &attr,
274711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&fromList,
275711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&toList) {
276711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, fromList);
277711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrIntoList(attr, toList);
278711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
279711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
280f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith/// The location of a type attribute.
281f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smithenum TypeAttrLocation {
282f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is in the decl-specifier-seq.
283f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclSpec,
284f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is part of a DeclaratorChunk.
285f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclChunk,
286f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is immediately after the declaration's name.
287f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclName
288f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith};
289f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
290711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state,
291f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             QualType &type, TypeAttrLocation TAL,
292711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             AttributeList *attrs);
293711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
294711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
295711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
296711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type);
297711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
298aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballmanstatic bool handleMSPointerTypeQualifierAttr(TypeProcessingState &state,
299aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             AttributeList &attr,
300aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             QualType &type);
301aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
302711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
303711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr, QualType &type);
304711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
305b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
306f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr, QualType &type);
307f85e193739c953358c865005855253af4f68a497John McCall
308711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCPointerTypeAttr(TypeProcessingState &state,
309711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr, QualType &type) {
3108e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_ObjCGC)
311f85e193739c953358c865005855253af4f68a497John McCall    return handleObjCGCTypeAttr(state, attr, type);
3128e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  assert(attr.getKind() == AttributeList::AT_ObjCOwnership);
313b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  return handleObjCOwnershipTypeAttr(state, attr, type);
314711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
315711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
316b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// Given the index of a declarator chunk, check whether that chunk
317b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// directly specifies the return type of a function and, if so, find
318b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// an appropriate place for it.
319b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall///
320b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// \param i - a notional index which the search will start
321b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall///   immediately inside
32287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar///
32387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// \param onlyBlockPointers Whether we should only look into block
32487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// pointer types (vs. all pointer types).
325b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCallstatic DeclaratorChunk *maybeMovePastReturnType(Declarator &declarator,
32687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                                unsigned i,
32787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                                bool onlyBlockPointers) {
328b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  assert(i <= declarator.getNumTypeObjects());
329b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
3306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  DeclaratorChunk *result = nullptr;
331b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
332b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  // First, look inwards past parens for a function declarator.
333b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  for (; i != 0; --i) {
334b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    DeclaratorChunk &fnChunk = declarator.getTypeObject(i-1);
335b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    switch (fnChunk.Kind) {
336b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Paren:
337b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      continue;
338b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
339b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // If we find anything except a function, bail out.
340b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Pointer:
341b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::BlockPointer:
342b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Array:
343b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Reference:
344b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::MemberPointer:
3454967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case DeclaratorChunk::Pipe:
346b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      return result;
347b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
348b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // If we do find a function declarator, scan inwards from that,
34987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // looking for a (block-)pointer declarator.
350b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Function:
351b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      for (--i; i != 0; --i) {
35287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        DeclaratorChunk &ptrChunk = declarator.getTypeObject(i-1);
35387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        switch (ptrChunk.Kind) {
354b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Paren:
355b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Array:
356b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Function:
357b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Reference:
3584967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        case DeclaratorChunk::Pipe:
359b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          continue;
36087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
36187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        case DeclaratorChunk::MemberPointer:
36287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        case DeclaratorChunk::Pointer:
36387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          if (onlyBlockPointers)
36487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            continue;
36587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
36687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          // fallthrough
36787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
368b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::BlockPointer:
36987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          result = &ptrChunk;
370b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          goto continue_outer;
371b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        }
372b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        llvm_unreachable("bad declarator chunk kind");
373b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      }
374b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
375b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      // If we run out of declarators doing that, we're done.
376b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      return result;
377b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
378b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    llvm_unreachable("bad declarator chunk kind");
379b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
380b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // Okay, reconsider from our new point.
381b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  continue_outer: ;
382b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  }
383b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
384b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  // Ran out of chunks, bail out.
385b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  return result;
386b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall}
387b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
388711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Given that an objc_gc attribute was written somewhere on a
389711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declaration *other* than on the declarator itself (for which, use
390711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
391711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// didn't apply in whatever position it was written in, try to move
392711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// it to a more appropriate position.
393711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeObjCPointerTypeAttr(TypeProcessingState &state,
394711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          AttributeList &attr,
395711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType type) {
396711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
397b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
398b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  // Move it to the outermost normal or block pointer declarator.
399711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
400711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
401711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
402711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
403b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::BlockPointer: {
404b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      // But don't move an ARC ownership attribute to the return type
405b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      // of a block.
4066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      DeclaratorChunk *destChunk = nullptr;
407b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (state.isProcessingDeclSpec() &&
408b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          attr.getKind() == AttributeList::AT_ObjCOwnership)
40987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        destChunk = maybeMovePastReturnType(declarator, i - 1,
41087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                            /*onlyBlockPointers=*/true);
411b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (!destChunk) destChunk = &chunk;
412b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
414b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall                             destChunk->getAttrListRef());
415711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
416b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
417711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
418711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
419711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
420711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
421711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
422b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // We may be starting at the return type of a block.
423b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Function:
424b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (state.isProcessingDeclSpec() &&
425b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          attr.getKind() == AttributeList::AT_ObjCOwnership) {
42687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (DeclaratorChunk *dest = maybeMovePastReturnType(
42787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                      declarator, i,
42887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                      /*onlyBlockPointers=*/true)) {
429b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
430b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall                                 dest->getAttrListRef());
431b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          return;
432b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        }
433b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      }
434b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      goto error;
435b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
436711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Don't walk through these.
437711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
438711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
4394967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case DeclaratorChunk::Pipe:
440711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto error;
441711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
442711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
443711c52bb20d0c69063b52a99826fb7d2835501f1John McCall error:
4442792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
4452792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
446711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
448711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Distribute an objc_gc type attribute that was written on the
449711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator.
450711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
451711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
452711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            AttributeList &attr,
453711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            QualType &declSpecType) {
454711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
455711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
456711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // objc_gc goes on the innermost pointer to something that's not a
457711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // pointer.
458711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  unsigned innermost = -1U;
459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  bool considerDeclSpec = true;
460711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
461711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
462711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
463711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
465711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      innermost = i;
466ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      continue;
467711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
468711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
469711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
470711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
471711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
4724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case DeclaratorChunk::Pipe:
473711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
474711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
475711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
476711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      considerDeclSpec = false;
477711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto done;
478711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
479711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
480711c52bb20d0c69063b52a99826fb7d2835501f1John McCall done:
481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
482711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // That might actually be the decl spec if we weren't blocked by
483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // anything in the declarator.
484711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (considerDeclSpec) {
4857ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    if (handleObjCPointerTypeAttr(state, attr, declSpecType)) {
4867ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // Splice the attribute into the decl spec.  Prevents the
4877ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // attribute from being applied multiple times and gives
4887ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // the source-location-filler something to work with.
4897ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      state.saveDeclSpecAttrs();
4907ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      moveAttrFromListToList(attr, declarator.getAttrListRef(),
4917ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall               declarator.getMutableDeclSpec().getAttributes().getListRef());
492711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
4937ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    }
494711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, if we found an appropriate chunk, splice the attribute
497711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // into it.
498711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (innermost != -1U) {
499711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, declarator.getAttrListRef(),
500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                       declarator.getTypeObject(innermost).getAttrListRef());
501711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, diagnose when we're done building the type.
505711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
506711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
507711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
508711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
509711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written somewhere in a declaration
510711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// *other* than on the declarator itself or in the decl spec.  Given
511711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// that it didn't apply in whatever position it was written in, try
512711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// to move it to a more appropriate position.
513711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeFunctionTypeAttr(TypeProcessingState &state,
514711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType type) {
516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to push the attribute from the return type of a function to
519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the function itself.
520711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
521711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
523711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
524711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
525711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
526711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
527711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
528711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
529711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
531711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
532711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
5344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case DeclaratorChunk::Pipe:
535711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
536711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
537711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
53891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5392792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
540711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
541711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
542711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Try to distribute a function type attribute to the innermost
543711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// function chunk or type.  Returns true if the attribute was
544711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributed, false if no location was found.
545711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool
546711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
547711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr,
548711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList *&attrList,
549711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      QualType &declSpecType) {
550711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
551711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
552711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Put it on the innermost function chunk, if there is one.
553711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
554711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
555711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (chunk.Kind != DeclaratorChunk::Function) continue;
556711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
557711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
558711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
559711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
560711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5615b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  return handleFunctionTypeAttr(state, attr, declSpecType);
562711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
563711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
564711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written in the decl spec.  Try to
565711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
566711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
567711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
568711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
569711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType &declSpecType) {
570711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.saveDeclSpecAttrs();
571711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5725c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // C++11 attributes before the decl specifiers actually appertain to
5735c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // the declarators. Move them straight there. We don't support the
5745c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // 'put them wherever you like' semantics we allow for GNU attributes.
5755c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  if (attr.isCXX11Attribute()) {
5765c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
5775c52166525f8714c3e3a979b156ec23426947fd3Richard Smith                           state.getDeclarator().getAttrListRef());
5785c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    return;
5795c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  }
5805c52166525f8714c3e3a979b156ec23426947fd3Richard Smith
581711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
582711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
583711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            state.getCurrentAttrListRef(),
584711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
585711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
586711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
587711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
588711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
589711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
590711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
591711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
592711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written on the declarator.  Try to
593711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
594711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
595711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
596711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         AttributeList &attr,
597711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         QualType &declSpecType) {
598711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
599711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
600711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
601711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
602711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declarator.getAttrListRef(),
603711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
604711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
605711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
606711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
607711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
608711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
609711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
610711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
611711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
612711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// \brief Given that there are attributes written on the declarator
613711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// itself, try to distribute any type attributes to the appropriate
614711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator chunk.
615711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///
616711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// These are attributes like the following:
617711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f ATTR;
618711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int (f ATTR)();
619711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// but not necessarily this:
620711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f() ATTR;
621711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
622711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                              QualType &declSpecType) {
623711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Collect all the type attributes from the declarator itself.
624711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
625711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *attr = state.getDeclarator().getAttributes();
626711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
627711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
628711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr->getNext();
629711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
6305c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    // Do not distribute C++11 attributes. They have strict rules for what
6315c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    // they appertain to.
6325c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    if (attr->isCXX11Attribute())
6335c52166525f8714c3e3a979b156ec23426947fd3Richard Smith      continue;
6345c52166525f8714c3e3a979b156ec23426947fd3Richard Smith
635711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr->getKind()) {
636711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
637711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
638711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
639711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
6408e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
6414e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
642f85e193739c953358c865005855253af4f68a497John McCall        break;
643f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough
644f85e193739c953358c865005855253af4f68a497John McCall
645711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
646711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
647711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
648711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
649aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    MS_TYPE_ATTRS_CASELIST:
650aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      // Microsoft type attributes cannot go after the declarator-id.
651aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      continue;
652aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
65387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    NULLABILITY_TYPE_ATTRS_CASELIST:
65487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Nullability specifiers cannot go after the declarator-id.
65587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
65687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Objective-C __kindof does not get distributed.
65787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case AttributeList::AT_ObjCKindOf:
65887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      continue;
65987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
660711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    default:
661711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
662711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
663711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attr = next));
664711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
665711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
666711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Add a synthetic '()' to a block-literal declarator if it is
667711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// required, given the return type.
668711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void maybeSynthesizeBlockSignature(TypeProcessingState &state,
669711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType declSpecType) {
670711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
671711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
672711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // First, check whether the declarator would produce a function,
673711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // i.e. whether the innermost semantic chunk is a function.
674711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.isFunctionDeclarator()) {
675711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // If so, make that declarator a prototyped declarator.
676711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.getFunctionTypeInfo().hasPrototype = true;
677711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
678711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
679711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
680da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // If there are any type objects, the type as written won't name a
681da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // function, regardless of the decl spec type.  This is because a
682da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // block signature declarator is always an abstract-declarator, and
683da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // abstract-declarators can't just be parentheses chunks.  Therefore
684da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // we need to build a function chunk unless there are no type
685da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // objects and the decl spec type is a function.
686711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
687711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
688711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
689da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // Note that there *are* cases with invalid declarators where
690da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // declarators consist solely of parentheses.  In general, these
691da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // occur only in failed efforts to make function declarators, so
692da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // faking up the function chunk is still the right thing to do.
693711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
694711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, we need to fake up a function declarator.
69596a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar  SourceLocation loc = declarator.getLocStart();
696711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
697711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // ...and *prepend* it to the declarator.
69859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara  SourceLocation NoLoc;
699711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
700176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*HasProto=*/true,
701176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*IsAmbiguous=*/false,
702176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*LParenLoc=*/NoLoc,
703176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*ArgInfo=*/nullptr,
704176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*NumArgs=*/0,
705176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*EllipsisLoc=*/NoLoc,
706176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*RParenLoc=*/NoLoc,
707176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*TypeQuals=*/0,
708176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*RefQualifierIsLvalueRef=*/true,
709176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*RefQualifierLoc=*/NoLoc,
710176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*ConstQualifierLoc=*/NoLoc,
711176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*VolatileQualifierLoc=*/NoLoc,
712176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*RestrictQualifierLoc=*/NoLoc,
713176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*MutableLoc=*/NoLoc, EST_None,
71487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      /*ESpecRange=*/SourceRange(),
715176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*Exceptions=*/nullptr,
716176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*ExceptionRanges=*/nullptr,
717176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*NumExceptions=*/0,
718176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*NoexceptExpr=*/nullptr,
719176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      /*ExceptionSpecTokens=*/nullptr,
720176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      loc, loc, declarator));
721711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
722711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // For consistency, make sure the state still has us as processing
723711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the decl spec.
724711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
725711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.setCurrentChunkIndex(declarator.getNumTypeObjects());
72604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
72704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
728b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainarstatic void diagnoseAndRemoveTypeQualifiers(Sema &S, const DeclSpec &DS,
729b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                                            unsigned &TypeQuals,
730b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                                            QualType TypeSoFar,
731b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                                            unsigned RemoveTQs,
732b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                                            unsigned DiagID) {
733b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  // If this occurs outside a template instantiation, warn the user about
734b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  // it; they probably didn't mean to specify a redundant qualifier.
735b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  typedef std::pair<DeclSpec::TQ, SourceLocation> QualLoc;
736b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  for (QualLoc Qual : {QualLoc(DeclSpec::TQ_const, DS.getConstSpecLoc()),
7374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                       QualLoc(DeclSpec::TQ_restrict, DS.getRestrictSpecLoc()),
738b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                       QualLoc(DeclSpec::TQ_volatile, DS.getVolatileSpecLoc()),
739b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                       QualLoc(DeclSpec::TQ_atomic, DS.getAtomicSpecLoc())}) {
740b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    if (!(RemoveTQs & Qual.first))
741b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      continue;
742b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
743b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    if (S.ActiveTemplateInstantiations.empty()) {
744b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      if (TypeQuals & Qual.first)
745b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar        S.Diag(Qual.second, DiagID)
746b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          << DeclSpec::getSpecifierName(Qual.first) << TypeSoFar
747b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          << FixItHint::CreateRemoval(Qual.second);
748b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    }
749b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
750b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    TypeQuals &= ~Qual.first;
751b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  }
752b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar}
753b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
7544967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// Return true if this is omitted block return type. Also check type
7554967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// attributes and type qualifiers when returning true.
7564967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarstatic bool checkOmittedBlockReturnType(Sema &S, Declarator &declarator,
7574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                        QualType Result) {
7584967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if (!isOmittedBlockReturnType(declarator))
7594967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    return false;
7604967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
7614967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // Warn if we see type attributes for omitted return type on a block literal.
7624967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  AttributeList *&attrs =
7634967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      declarator.getMutableDeclSpec().getAttributes().getListRef();
7644967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  AttributeList *prev = nullptr;
7654967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  for (AttributeList *cur = attrs; cur; cur = cur->getNext()) {
7664967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    AttributeList &attr = *cur;
7674967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Skip attributes that were marked to be invalid or non-type
7684967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // attributes.
7694967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (attr.isInvalid() || !attr.isTypeAttr()) {
7704967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      prev = cur;
7714967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      continue;
7724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
7734967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    S.Diag(attr.getLoc(),
7744967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar           diag::warn_block_literal_attributes_on_omitted_return_type)
7754967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        << attr.getName();
7764967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Remove cur from the list.
7774967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (prev) {
7784967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      prev->setNext(cur->getNext());
7794967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      prev = cur;
7804967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    } else {
7814967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      attrs = cur->getNext();
7824967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
7834967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
7844967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
7854967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // Warn if we see type qualifiers for omitted return type on a block literal.
7864967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  const DeclSpec &DS = declarator.getDeclSpec();
7874967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  unsigned TypeQuals = DS.getTypeQualifiers();
7884967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  diagnoseAndRemoveTypeQualifiers(S, DS, TypeQuals, Result, (unsigned)-1,
7894967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      diag::warn_block_literal_qualifiers_on_omitted_return_type);
7904967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  declarator.getMutableDeclSpec().ClearTypeQualifiers();
7914967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
7924967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  return true;
7934967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar}
7944967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
79587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// Apply Objective-C type arguments to the given type.
79687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarstatic QualType applyObjCTypeArgs(Sema &S, SourceLocation loc, QualType type,
79787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                  ArrayRef<TypeSourceInfo *> typeArgs,
79887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                  SourceRange typeArgsRange,
79987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                  bool failOnError = false) {
80087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // We can only apply type arguments to an Objective-C class type.
80187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  const auto *objcObjectType = type->getAs<ObjCObjectType>();
80287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!objcObjectType || !objcObjectType->getInterface()) {
80387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    S.Diag(loc, diag::err_objc_type_args_non_class)
80487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << type
80587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << typeArgsRange;
80687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
80787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (failOnError)
80887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return QualType();
80987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return type;
81087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
81187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
81287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // The class type must be parameterized.
81387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  ObjCInterfaceDecl *objcClass = objcObjectType->getInterface();
81487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  ObjCTypeParamList *typeParams = objcClass->getTypeParamList();
81587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!typeParams) {
81687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    S.Diag(loc, diag::err_objc_type_args_non_parameterized_class)
81787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << objcClass->getDeclName()
81887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << FixItHint::CreateRemoval(typeArgsRange);
81987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
82087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (failOnError)
82187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return QualType();
82287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
82387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return type;
82487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
82587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
82687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // The type must not already be specialized.
82787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (objcObjectType->isSpecialized()) {
82887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    S.Diag(loc, diag::err_objc_type_args_specialized_class)
82987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << type
83087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << FixItHint::CreateRemoval(typeArgsRange);
83187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
83287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (failOnError)
83387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return QualType();
83487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
83587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return type;
83687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
83787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
83887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Check the type arguments.
83987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  SmallVector<QualType, 4> finalTypeArgs;
84087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  unsigned numTypeParams = typeParams->size();
84187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  bool anyPackExpansions = false;
84287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  for (unsigned i = 0, n = typeArgs.size(); i != n; ++i) {
84387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    TypeSourceInfo *typeArgInfo = typeArgs[i];
84487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    QualType typeArg = typeArgInfo->getType();
84587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
84687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Type arguments cannot have explicit qualifiers or nullability.
84787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // We ignore indirect sources of these, e.g. behind typedefs or
84887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // template arguments.
84987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (TypeLoc qual = typeArgInfo->getTypeLoc().findExplicitQualifierLoc()) {
85087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      bool diagnosed = false;
85187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      SourceRange rangeToRemove;
85287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (auto attr = qual.getAs<AttributedTypeLoc>()) {
85387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        rangeToRemove = attr.getLocalSourceRange();
85487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (attr.getTypePtr()->getImmediateNullability()) {
85587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          typeArg = attr.getTypePtr()->getModifiedType();
85687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          S.Diag(attr.getLocStart(),
85787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                 diag::err_objc_type_arg_explicit_nullability)
85887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            << typeArg << FixItHint::CreateRemoval(rangeToRemove);
85987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          diagnosed = true;
86087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        }
86187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
86287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
86387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (!diagnosed) {
86487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        S.Diag(qual.getLocStart(), diag::err_objc_type_arg_qualified)
86587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << typeArg << typeArg.getQualifiers().getAsString()
86687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << FixItHint::CreateRemoval(rangeToRemove);
86787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
86887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
86987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
87087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Remove qualifiers even if they're non-local.
87187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    typeArg = typeArg.getUnqualifiedType();
87287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
87387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    finalTypeArgs.push_back(typeArg);
87487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
87587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (typeArg->getAs<PackExpansionType>())
87687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      anyPackExpansions = true;
87787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
87887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Find the corresponding type parameter, if there is one.
87987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ObjCTypeParamDecl *typeParam = nullptr;
88087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (!anyPackExpansions) {
88187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (i < numTypeParams) {
88287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        typeParam = typeParams->begin()[i];
88387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      } else {
88487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // Too many arguments.
88587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        S.Diag(loc, diag::err_objc_type_args_wrong_arity)
88687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << false
88787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << objcClass->getDeclName()
88887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << (unsigned)typeArgs.size()
88987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << numTypeParams;
89087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        S.Diag(objcClass->getLocation(), diag::note_previous_decl)
89187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << objcClass;
89287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
89387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (failOnError)
89487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          return QualType();
89587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
89687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        return type;
89787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
89887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
89987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
90087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Objective-C object pointer types must be substitutable for the bounds.
90187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (const auto *typeArgObjC = typeArg->getAs<ObjCObjectPointerType>()) {
90287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // If we don't have a type parameter to match against, assume
90387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // everything is fine. There was a prior pack expansion that
90487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // means we won't be able to match anything.
90587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (!typeParam) {
90687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        assert(anyPackExpansions && "Too many arguments?");
90787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        continue;
90887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
90987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
91087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Retrieve the bound.
91187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      QualType bound = typeParam->getUnderlyingType();
91287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      const auto *boundObjC = bound->getAs<ObjCObjectPointerType>();
91387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
91487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Determine whether the type argument is substitutable for the bound.
91587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (typeArgObjC->isObjCIdType()) {
91687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // When the type argument is 'id', the only acceptable type
91787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // parameter bound is 'id'.
91887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (boundObjC->isObjCIdType())
91987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          continue;
92087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      } else if (S.Context.canAssignObjCInterfaces(boundObjC, typeArgObjC)) {
92187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // Otherwise, we follow the assignability rules.
92287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        continue;
92387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
92487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
92587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Diagnose the mismatch.
92687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      S.Diag(typeArgInfo->getTypeLoc().getLocStart(),
92787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             diag::err_objc_type_arg_does_not_match_bound)
92887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << typeArg << bound << typeParam->getDeclName();
92987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      S.Diag(typeParam->getLocation(), diag::note_objc_type_param_here)
93087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << typeParam->getDeclName();
93187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
93287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (failOnError)
93387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        return QualType();
93487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
93587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return type;
93687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
93787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
93887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Block pointer types are permitted for unqualified 'id' bounds.
93987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (typeArg->isBlockPointerType()) {
94087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // If we don't have a type parameter to match against, assume
94187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // everything is fine. There was a prior pack expansion that
94287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // means we won't be able to match anything.
94387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (!typeParam) {
94487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        assert(anyPackExpansions && "Too many arguments?");
94587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        continue;
94687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
94787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
94887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Retrieve the bound.
94987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      QualType bound = typeParam->getUnderlyingType();
95087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (bound->isBlockCompatibleObjCPointerType(S.Context))
95187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        continue;
95287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
95387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Diagnose the mismatch.
95487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      S.Diag(typeArgInfo->getTypeLoc().getLocStart(),
95587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             diag::err_objc_type_arg_does_not_match_bound)
95687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << typeArg << bound << typeParam->getDeclName();
95787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      S.Diag(typeParam->getLocation(), diag::note_objc_type_param_here)
95887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << typeParam->getDeclName();
95987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
96087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (failOnError)
96187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        return QualType();
96287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
96387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return type;
96487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
96587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
96687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Dependent types will be checked at instantiation time.
96787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (typeArg->isDependentType()) {
96887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      continue;
96987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
97087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
97187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Diagnose non-id-compatible type arguments.
97287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    S.Diag(typeArgInfo->getTypeLoc().getLocStart(),
97387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar           diag::err_objc_type_arg_not_id_compatible)
97487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << typeArg
97587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << typeArgInfo->getTypeLoc().getSourceRange();
97687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
97787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (failOnError)
97887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return QualType();
97987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
98087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return type;
98187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
98287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
98387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Make sure we didn't have the wrong number of arguments.
98487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!anyPackExpansions && finalTypeArgs.size() != numTypeParams) {
98587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    S.Diag(loc, diag::err_objc_type_args_wrong_arity)
98687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << (typeArgs.size() < typeParams->size())
98787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << objcClass->getDeclName()
98887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << (unsigned)finalTypeArgs.size()
98987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << (unsigned)numTypeParams;
99087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    S.Diag(objcClass->getLocation(), diag::note_previous_decl)
99187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << objcClass;
99287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
99387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (failOnError)
99487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return QualType();
99587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
99687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return type;
99787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
99887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
99987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Success. Form the specialized type.
100087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  return S.Context.getObjCObjectType(type, finalTypeArgs, { }, false);
100187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
100287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
100387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// Apply Objective-C protocol qualifiers to the given type.
100487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarstatic QualType applyObjCProtocolQualifiers(
100587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                  Sema &S, SourceLocation loc, SourceRange range, QualType type,
100687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                  ArrayRef<ObjCProtocolDecl *> protocols,
100787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                  const SourceLocation *protocolLocs,
100887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                  bool failOnError = false) {
100987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  ASTContext &ctx = S.Context;
101087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (const ObjCObjectType *objT = dyn_cast<ObjCObjectType>(type.getTypePtr())){
101187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // FIXME: Check for protocols to which the class type is already
101287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // known to conform.
101387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
101487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return ctx.getObjCObjectType(objT->getBaseType(),
101587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                 objT->getTypeArgsAsWritten(),
101687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                 protocols,
101787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                 objT->isKindOfTypeAsWritten());
101887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
101987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
102087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (type->isObjCObjectType()) {
102187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Silently overwrite any existing protocol qualifiers.
102287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // TODO: determine whether that's the right thing to do.
102387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
102487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // FIXME: Check for protocols to which the class type is already
102587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // known to conform.
102687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return ctx.getObjCObjectType(type, { }, protocols, false);
102787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
102887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
102987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // id<protocol-list>
103087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (type->isObjCIdType()) {
103187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    const ObjCObjectPointerType *objPtr = type->castAs<ObjCObjectPointerType>();
103287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    type = ctx.getObjCObjectType(ctx.ObjCBuiltinIdTy, { }, protocols,
103387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                 objPtr->isKindOfType());
103487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return ctx.getObjCObjectPointerType(type);
103587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
103687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
103787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Class<protocol-list>
103887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (type->isObjCClassType()) {
103987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    const ObjCObjectPointerType *objPtr = type->castAs<ObjCObjectPointerType>();
104087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    type = ctx.getObjCObjectType(ctx.ObjCBuiltinClassTy, { }, protocols,
104187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                 objPtr->isKindOfType());
104287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return ctx.getObjCObjectPointerType(type);
104387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
104487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
104587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  S.Diag(loc, diag::err_invalid_protocol_qualifiers)
104687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    << range;
104787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
104887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (failOnError)
104987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return QualType();
105087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
105187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  return type;
105287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
105387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
105487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga NainarQualType Sema::BuildObjCObjectType(QualType BaseType,
105587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   SourceLocation Loc,
105687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   SourceLocation TypeArgsLAngleLoc,
105787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   ArrayRef<TypeSourceInfo *> TypeArgs,
105887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   SourceLocation TypeArgsRAngleLoc,
105987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   SourceLocation ProtocolLAngleLoc,
106087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   ArrayRef<ObjCProtocolDecl *> Protocols,
106187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   ArrayRef<SourceLocation> ProtocolLocs,
106287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   SourceLocation ProtocolRAngleLoc,
106387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   bool FailOnError) {
106487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  QualType Result = BaseType;
106587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!TypeArgs.empty()) {
106687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    Result = applyObjCTypeArgs(*this, Loc, Result, TypeArgs,
106787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                               SourceRange(TypeArgsLAngleLoc,
106887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                           TypeArgsRAngleLoc),
106987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                               FailOnError);
107087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (FailOnError && Result.isNull())
107187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return QualType();
107287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
107387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
107487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!Protocols.empty()) {
107587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    Result = applyObjCProtocolQualifiers(*this, Loc,
107687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                         SourceRange(ProtocolLAngleLoc,
107787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                                     ProtocolRAngleLoc),
107887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                         Result, Protocols,
107987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                         ProtocolLocs.data(),
108087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                         FailOnError);
108187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (FailOnError && Result.isNull())
108287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return QualType();
108387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
108487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
108587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  return Result;
108687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
108787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
108887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga NainarTypeResult Sema::actOnObjCProtocolQualifierType(
108987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             SourceLocation lAngleLoc,
109087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             ArrayRef<Decl *> protocols,
109187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             ArrayRef<SourceLocation> protocolLocs,
109287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             SourceLocation rAngleLoc) {
109387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Form id<protocol-list>.
109487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  QualType Result = Context.getObjCObjectType(
109587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                      Context.ObjCBuiltinIdTy, { },
109687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                      llvm::makeArrayRef(
109787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                        (ObjCProtocolDecl * const *)protocols.data(),
109887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                        protocols.size()),
109987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                      false);
110087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  Result = Context.getObjCObjectPointerType(Result);
110187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
110287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  TypeSourceInfo *ResultTInfo = Context.CreateTypeSourceInfo(Result);
110387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  TypeLoc ResultTL = ResultTInfo->getTypeLoc();
110487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
110587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  auto ObjCObjectPointerTL = ResultTL.castAs<ObjCObjectPointerTypeLoc>();
110687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  ObjCObjectPointerTL.setStarLoc(SourceLocation()); // implicit
110787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
110887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  auto ObjCObjectTL = ObjCObjectPointerTL.getPointeeLoc()
110987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                        .castAs<ObjCObjectTypeLoc>();
111087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  ObjCObjectTL.setHasBaseTypeAsWritten(false);
111187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  ObjCObjectTL.getBaseLoc().initialize(Context, SourceLocation());
111287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
111387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // No type arguments.
111487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  ObjCObjectTL.setTypeArgsLAngleLoc(SourceLocation());
111587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  ObjCObjectTL.setTypeArgsRAngleLoc(SourceLocation());
111687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
111787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Fill in protocol qualifiers.
111887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  ObjCObjectTL.setProtocolLAngleLoc(lAngleLoc);
111987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  ObjCObjectTL.setProtocolRAngleLoc(rAngleLoc);
112087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  for (unsigned i = 0, n = protocols.size(); i != n; ++i)
112187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ObjCObjectTL.setProtocolLoc(i, protocolLocs[i]);
112287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
112387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // We're done. Return the completed type to the parser.
112487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  return CreateParsedType(Result, ResultTInfo);
112587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
112687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
112787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga NainarTypeResult Sema::actOnObjCTypeArgsAndProtocolQualifiers(
112887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             Scope *S,
112987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             SourceLocation Loc,
113087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             ParsedType BaseType,
113187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             SourceLocation TypeArgsLAngleLoc,
113287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             ArrayRef<ParsedType> TypeArgs,
113387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             SourceLocation TypeArgsRAngleLoc,
113487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             SourceLocation ProtocolLAngleLoc,
113587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             ArrayRef<Decl *> Protocols,
113687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             ArrayRef<SourceLocation> ProtocolLocs,
113787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             SourceLocation ProtocolRAngleLoc) {
113887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  TypeSourceInfo *BaseTypeInfo = nullptr;
113987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  QualType T = GetTypeFromParser(BaseType, &BaseTypeInfo);
114087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (T.isNull())
114187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return true;
114287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
114387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Handle missing type-source info.
114487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!BaseTypeInfo)
114587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    BaseTypeInfo = Context.getTrivialTypeSourceInfo(T, Loc);
114687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
114787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Extract type arguments.
114887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  SmallVector<TypeSourceInfo *, 4> ActualTypeArgInfos;
114987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  for (unsigned i = 0, n = TypeArgs.size(); i != n; ++i) {
115087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    TypeSourceInfo *TypeArgInfo = nullptr;
115187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    QualType TypeArg = GetTypeFromParser(TypeArgs[i], &TypeArgInfo);
115287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (TypeArg.isNull()) {
115387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      ActualTypeArgInfos.clear();
115487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
115587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
115687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
115787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    assert(TypeArgInfo && "No type source info?");
115887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ActualTypeArgInfos.push_back(TypeArgInfo);
115987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
116087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
116187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Build the object type.
116287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  QualType Result = BuildObjCObjectType(
116387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      T, BaseTypeInfo->getTypeLoc().getSourceRange().getBegin(),
116487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      TypeArgsLAngleLoc, ActualTypeArgInfos, TypeArgsRAngleLoc,
116587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      ProtocolLAngleLoc,
116687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      llvm::makeArrayRef((ObjCProtocolDecl * const *)Protocols.data(),
116787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                         Protocols.size()),
116887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      ProtocolLocs, ProtocolRAngleLoc,
116987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      /*FailOnError=*/false);
117087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
117187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (Result == T)
117287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return BaseType;
117387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
117487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Create source information for this type.
117587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  TypeSourceInfo *ResultTInfo = Context.CreateTypeSourceInfo(Result);
117687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  TypeLoc ResultTL = ResultTInfo->getTypeLoc();
117787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
117887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // For id<Proto1, Proto2> or Class<Proto1, Proto2>, we'll have an
117987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // object pointer type. Fill in source information for it.
118087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (auto ObjCObjectPointerTL = ResultTL.getAs<ObjCObjectPointerTypeLoc>()) {
118187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // The '*' is implicit.
118287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ObjCObjectPointerTL.setStarLoc(SourceLocation());
118387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ResultTL = ObjCObjectPointerTL.getPointeeLoc();
118487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
118587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
118687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  auto ObjCObjectTL = ResultTL.castAs<ObjCObjectTypeLoc>();
118787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
118887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Type argument information.
118987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (ObjCObjectTL.getNumTypeArgs() > 0) {
119087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    assert(ObjCObjectTL.getNumTypeArgs() == ActualTypeArgInfos.size());
119187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ObjCObjectTL.setTypeArgsLAngleLoc(TypeArgsLAngleLoc);
119287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ObjCObjectTL.setTypeArgsRAngleLoc(TypeArgsRAngleLoc);
119387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    for (unsigned i = 0, n = ActualTypeArgInfos.size(); i != n; ++i)
119487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      ObjCObjectTL.setTypeArgTInfo(i, ActualTypeArgInfos[i]);
119587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  } else {
119687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ObjCObjectTL.setTypeArgsLAngleLoc(SourceLocation());
119787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ObjCObjectTL.setTypeArgsRAngleLoc(SourceLocation());
119887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
119987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
120087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Protocol qualifier information.
120187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (ObjCObjectTL.getNumProtocols() > 0) {
120287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    assert(ObjCObjectTL.getNumProtocols() == Protocols.size());
120387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ObjCObjectTL.setProtocolLAngleLoc(ProtocolLAngleLoc);
120487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ObjCObjectTL.setProtocolRAngleLoc(ProtocolRAngleLoc);
120587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    for (unsigned i = 0, n = Protocols.size(); i != n; ++i)
120687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      ObjCObjectTL.setProtocolLoc(i, ProtocolLocs[i]);
120787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  } else {
120887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ObjCObjectTL.setProtocolLAngleLoc(SourceLocation());
120987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ObjCObjectTL.setProtocolRAngleLoc(SourceLocation());
121087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
121187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
121287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Base type.
121387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  ObjCObjectTL.setHasBaseTypeAsWritten(true);
121487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (ObjCObjectTL.getType() == T)
121587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ObjCObjectTL.getBaseLoc().initializeFullCopy(BaseTypeInfo->getTypeLoc());
121687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  else
121787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    ObjCObjectTL.getBaseLoc().initialize(Context, Loc);
121887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
121987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // We're done. Return the completed type to the parser.
122087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  return CreateParsedType(Result, ResultTInfo);
122187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
122287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
12234967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarstatic StringRef getImageAccessAttrStr(AttributeList *attrs) {
12244967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if (attrs) {
12254967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
12264967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    AttributeList *Next;
12274967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    do {
12284967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      AttributeList &Attr = *attrs;
12294967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      Next = Attr.getNext();
12304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (Attr.getKind() == AttributeList::AT_OpenCLAccess) {
12314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        return Attr.getName()->getName();
12324967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      }
12334967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    } while (Next);
12344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
12354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  return "";
12364967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar}
12374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
1238930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
1239930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
12401dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// \param state Specifies the declarator containing the declaration specifier
12411dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// to be converted, along with other associated processing state.
12425153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
12435153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
12448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType ConvertDeclSpecToType(TypeProcessingState &state) {
12455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
1247711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
12488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
1249711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
1250711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const DeclSpec &DS = declarator.getDeclSpec();
1251711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation DeclLoc = declarator.getIdentifierLoc();
12525db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
125396a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar    DeclLoc = DS.getLocStart();
125491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1255711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  ASTContext &Context = S.Context;
12561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12575db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
12585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
125996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
126096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
126196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
12625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
12635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
1264fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
12655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
1266fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
12675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
12685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
12695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
1270fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
12715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1272958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
127364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
127464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
127564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
127664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
1277711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
1278651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        << DS.getSpecifierName(DS.getTypeSpecType(),
1279651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                               Context.getPrintingPolicy());
128064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
128164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
128264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
128364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
1284711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
1285651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        << DS.getSpecifierName(DS.getTypeSpecType(),
1286651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                               Context.getPrintingPolicy());
128764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
128864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
128964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
1290f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
1291f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
1292f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
1293f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
1294f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
1295f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
1296f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
1297f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
1298f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
1299f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
1300d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
13015db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
13025db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
1303f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // The declspec is always missing in a lambda expr context; it is either
1304f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // specified with a trailing return type or inferred.
1305176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (S.getLangOpts().CPlusPlus14 &&
130641d0958281627882fbe2049fb887d741eabd3fe3Richard Smith        declarator.getContext() == Declarator::LambdaExprContext) {
130741d0958281627882fbe2049fb887d741eabd3fe3Richard Smith      // In C++1y, a lambda's implicit return type is 'auto'.
130841d0958281627882fbe2049fb887d741eabd3fe3Richard Smith      Result = Context.getAutoDeductType();
130941d0958281627882fbe2049fb887d741eabd3fe3Richard Smith      break;
131041d0958281627882fbe2049fb887d741eabd3fe3Richard Smith    } else if (declarator.getContext() == Declarator::LambdaExprContext ||
13114967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar               checkOmittedBlockReturnType(S, declarator,
13124967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                           Context.DependentTy)) {
1313152b4e4652baedfceba1cd8115515629225e713fManuel Klimek      Result = Context.DependentTy;
13145db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
13155db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
13161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1317d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
1318d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
1319d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
1320d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
1321d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
1322d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
1323d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
13244e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().ImplicitInt) {
132535d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
132635d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
13273f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
1328711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_declspec)
13293f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
133096a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        << FixItHint::CreateInsertion(DS.getLocStart(), "int");
13313f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
13324310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
1333d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
1334d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
1335d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
1336d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
133758eb37036b47bbe7433f72d92a2cb60848507707Richard Smith      if (S.getLangOpts().CPlusPlus) {
1338711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_missing_type_specifier)
13393f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
13401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1341b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
1342b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
1343b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
1344711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
13454967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      } else if (S.getLangOpts().OpenCLVersion >= 200 && DS.isTypeSpecPipe()){
13464967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        S.Diag(DeclLoc, diag::err_missing_actual_pipe_type)
13474967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          << DS.getSourceRange();
13484967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        declarator.setInvalidType(true);
1349b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
1350711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_type_specifier)
13513f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
1352b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
1353d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
13541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
13563cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
13575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
13585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
1359fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
1360fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
1361fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
1362311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
1363311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
136491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1365e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
1366e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
1367e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
1368e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
136980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
1370e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
1371e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
1372e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
1373e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
1374311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
13755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
13765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
13775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
1378fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
1379fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
1380fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
1381311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
1382311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
138391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1384e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
1385e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
1386e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
1387e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
138880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
1389e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
1390e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
1391e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
1392e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
1393311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
13945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
13955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1396958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
13973cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
13985a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith  case DeclSpec::TST_int128:
13996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!S.Context.getTargetInfo().hasInt128Type())
14004967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported)
14014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        << "__int128";
14025a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    if (DS.getTypeSpecSign() == DeclSpec::TSS_unsigned)
14035a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.UnsignedInt128Ty;
14045a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    else
14055a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.Int128Ty;
14065a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    break;
1407aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case DeclSpec::TST_half: Result = Context.HalfTy; break;
1408fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
1409958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
1410958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
1411fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
1412958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
1413fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
141439d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne
14150e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    if (S.getLangOpts().OpenCL &&
14160e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        !((S.getLangOpts().OpenCLVersion >= 120) ||
14170e2c34f92f00628d48968dfea096d36381f494cbStephen Hines          S.getOpenCLOptions().cl_khr_fp64)) {
14183ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_requires_extension)
14193ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar          << Result << "cl_khr_fp64";
142039d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      declarator.setInvalidType(true);
142139d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    }
1422958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
14234967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case DeclSpec::TST_float128:
14244967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (!S.Context.getTargetInfo().hasFloat128Type())
14254967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported)
14264967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        << "__float128";
14274967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    Result = Context.Float128Ty;
14284967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    break;
1429fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
14304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    break;
14315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
14335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
1434711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
14358f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
1436711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
14378f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
143899dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
14395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
14405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
14416666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_struct:
14426666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_interface: {
1443b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
14446e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
14456e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
14466e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
1447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
14486e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
14496e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
14506e24726524c2b51b31bb4b622aa678a46b024f42John McCall
1451a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
14520daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc());
145391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
14545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
1455a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
145691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
14575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
1458a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
14592191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
14600daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    // In both C and C++, make an ElaboratedType.
14610daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    ElaboratedTypeKeyword Keyword
14620daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
14630daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
1464958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
14651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
14661a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
14675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
14685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
14695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
1470711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
147187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (Result.isNull()) {
1472711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
14733ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    } else if (S.getLangOpts().OpenCL) {
147487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (Result->getAs<AtomicType>()) {
147587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        StringRef TypeName = Result.getBaseTypeIdentifier()->getName();
147687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        bool NoExtTypes =
147787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            llvm::StringSwitch<bool>(TypeName)
147887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                .Cases("atomic_int", "atomic_uint", "atomic_float",
147987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                       "atomic_flag", true)
148087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                .Default(false);
14813ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar        if (!S.getOpenCLOptions().cl_khr_int64_base_atomics && !NoExtTypes) {
14823ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar          S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_requires_extension)
14833ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar              << Result << "cl_khr_int64_base_atomics";
14843ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar          declarator.setInvalidType(true);
14853ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar        }
14863ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar        if (!S.getOpenCLOptions().cl_khr_int64_extended_atomics &&
14873ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar            !NoExtTypes) {
14883ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar          S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_requires_extension)
14893ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar              << Result << "cl_khr_int64_extended_atomics";
14903ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar          declarator.setInvalidType(true);
14913ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar        }
149287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (!S.getOpenCLOptions().cl_khr_fp64 &&
149387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            !TypeName.compare("atomic_double")) {
14943ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar          S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_requires_extension)
14953ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar              << Result << "cl_khr_fp64";
14963ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar          declarator.setInvalidType(true);
14973ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar        }
149887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      } else if (!S.getOpenCLOptions().cl_khr_gl_msaa_sharing &&
14994967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                 (Result->isOCLImage2dArrayMSAADepthROType() ||
15004967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                  Result->isOCLImage2dArrayMSAADepthWOType() ||
15014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                  Result->isOCLImage2dArrayMSAADepthRWType() ||
15024967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                  Result->isOCLImage2dArrayMSAAROType() ||
15034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                  Result->isOCLImage2dArrayMSAARWType() ||
15044967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                  Result->isOCLImage2dArrayMSAAWOType() ||
15054967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                  Result->isOCLImage2dMSAADepthROType() ||
15064967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                  Result->isOCLImage2dMSAADepthRWType() ||
15074967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                  Result->isOCLImage2dMSAADepthWOType() ||
15084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                  Result->isOCLImage2dMSAAROType() ||
15094967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                  Result->isOCLImage2dMSAARWType() ||
15104967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                  Result->isOCLImage2dMSAAWOType())) {
151187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_requires_extension)
151287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            << Result << "cl_khr_gl_msaa_sharing";
151387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        declarator.setInvalidType(true);
15143ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar      }
1515c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
15161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
1518958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1520958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
1521e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
1522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
1523958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
1524730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian    if (!Result->isDependentType())
1525730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian      if (const TagType *TT = Result->getAs<TagType>())
1526711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
1527d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
1528fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
1529958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1530d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
1531b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
1532d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
1533d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
1534711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
15354b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
15364b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
1537711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
15384b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
1539958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
1540d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
15416fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
1542b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
15436fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
15446fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
1545711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
1546af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
1547af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
1548711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
1549af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
15506fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
15516fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
1552ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case DeclSpec::TST_underlyingType:
1553db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    Result = S.GetTypeFromParser(DS.getRepAsType());
1554db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    assert(!Result.isNull() && "Didn't get a type for __underlying_type?");
1555ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    Result = S.BuildUnaryTransformType(Result,
1556ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::EnumUnderlyingType,
1557ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       DS.getTypeSpecTypeLoc());
1558ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (Result.isNull()) {
1559ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Result = Context.IntTy;
1560ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      declarator.setInvalidType(true);
1561db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    }
156291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
1563db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
1564a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  case DeclSpec::TST_auto:
1565e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
1566fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    // If auto is mentioned in a lambda parameter context, convert it to a
1567fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    // template parameter type immediately, with the appropriate depth and
1568fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    // index, and update sema's state (LambdaScopeInfo) for the current lambda
1569fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    // being analyzed (which tracks the invented type template parameter).
1570fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    if (declarator.getContext() == Declarator::LambdaExprParameterContext) {
1571fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      sema::LambdaScopeInfo *LSI = S.getCurLambda();
1572fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      assert(LSI && "No LambdaScopeInfo on the stack!");
1573fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      const unsigned TemplateParameterDepth = LSI->AutoTemplateParameterDepth;
1574fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      const unsigned AutoParameterPosition = LSI->AutoTemplateParams.size();
1575fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      const bool IsParameterPack = declarator.hasEllipsis();
15760e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1577fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // Turns out we must create the TemplateTypeParmDecl here to
1578fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // retrieve the corresponding template parameter type.
1579fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      TemplateTypeParmDecl *CorrespondingTemplateParam =
1580fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        TemplateTypeParmDecl::Create(Context,
1581fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        // Temporarily add to the TranslationUnit DeclContext.  When the
1582fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        // associated TemplateParameterList is attached to a template
1583fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        // declaration (such as FunctionTemplateDecl), the DeclContext
1584fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        // for each template parameter gets updated appropriately via
1585fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        // a call to AdoptTemplateParameterList.
1586fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        Context.getTranslationUnitDecl(),
1587fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        /*KeyLoc*/ SourceLocation(),
1588fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        /*NameLoc*/ declarator.getLocStart(),
1589fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        TemplateParameterDepth,
1590fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali        AutoParameterPosition,  // our template param index
15910e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        /* Identifier*/ nullptr, false, IsParameterPack);
1592fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      LSI->AutoTemplateParams.push_back(CorrespondingTemplateParam);
1593fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // Replace the 'auto' in the function parameter with this invented
1594fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // template type parameter.
1595176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      Result = QualType(CorrespondingTemplateParam->getTypeForDecl(), 0);
1596fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    } else {
159787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Result = Context.getAutoType(QualType(), AutoTypeKeyword::Auto, false);
1598fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    }
1599a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    break;
1600a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
160187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case DeclSpec::TST_auto_type:
160287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    Result = Context.getAutoType(QualType(), AutoTypeKeyword::GNUAutoType, false);
160387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    break;
160487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
1605a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  case DeclSpec::TST_decltype_auto:
160687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    Result = Context.getAutoType(QualType(), AutoTypeKeyword::DecltypeAuto,
160787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                 /*IsDependent*/ false);
1608e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
16091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1610a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall  case DeclSpec::TST_unknown_anytype:
1611a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    Result = Context.UnknownAnyTy;
1612a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    break;
1613a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall
1614b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case DeclSpec::TST_atomic:
1615b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.GetTypeFromParser(DS.getRepAsType());
1616b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(!Result.isNull() && "Didn't get a type for _Atomic?");
1617b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc());
1618b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Result.isNull()) {
1619b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Result = Context.IntTy;
1620b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      declarator.setInvalidType(true);
1621b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
162291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
1623b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
16244967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar#define GENERIC_IMAGE_TYPE(ImgType, Id) \
16254967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case DeclSpec::TST_##ImgType##_t: \
16264967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    Result = llvm::StringSwitch<QualType>( \
16274967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                 getImageAccessAttrStr(DS.getAttributes().getList())) \
16284967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                 .Cases("write_only", "__write_only", Context.Id##WOTy) \
16294967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                 .Cases("read_write", "__read_write", Context.Id##RWTy) \
16304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                 .Default(Context.Id##ROTy); \
16314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    break;
16324967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar#include "clang/Basic/OpenCLImageTypes.def"
16334967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
1634809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
16355153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
1636711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
16375153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
16385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1640958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
1641f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
16424e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().Freestanding)
1643711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
1644fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
164582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
164682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
164782287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
1648e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
1649788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
1650e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
1651788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
1652e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
1653e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
1654f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
16551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
165647423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
165747423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
1658711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
16591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1660711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
1661711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
1662711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
1663711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
1664711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1665711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
1666711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
1667711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
16684967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // pipe attributes will be handled later ( at GetFullTypeForDeclarator )
16694967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if (!DS.isTypeSpecPipe())
16704967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      processTypeAttrs(state, Result, TAL_DeclSpec, DS.getAttributes().getList());
16711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
167296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
167396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
1674b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    // Warn about CV qualifiers on function types.
1675b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    // C99 6.7.3p8:
1676b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    //   If the specification of a function type includes any type qualifiers,
1677b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    //   the behavior is undefined.
1678b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    // C++11 [dcl.fct]p7:
1679b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    //   The effect of a cv-qualifier-seq in a function declarator is not the
1680b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    //   same as adding cv-qualification on top of the function type. In the
1681b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    //   latter case, the cv-qualifiers are ignored.
1682b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    if (TypeQuals && Result->isFunctionType()) {
1683b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      diagnoseAndRemoveTypeQualifiers(
1684b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          S, DS, TypeQuals, Result, DeclSpec::TQ_const | DeclSpec::TQ_volatile,
1685b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          S.getLangOpts().CPlusPlus
1686b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar              ? diag::warn_typecheck_function_qualifiers_ignored
1687b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar              : diag::warn_typecheck_function_qualifiers_unspecified);
1688b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      // No diagnostic for 'restrict' or '_Atomic' applied to a
1689b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      // function type; we'll diagnose those later, in BuildQualifiedType.
169096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
16911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1692651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // C++11 [dcl.ref]p1:
1693f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
1694651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    //   cv-qualifiers are introduced through the use of a typedef-name
1695651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    //   or decltype-specifier, in which case the cv-qualifiers are ignored.
1696651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    //
1697651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // There don't appear to be any other contexts in which a cv-qualified
1698651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // reference type could be formed, so the 'ill-formed' clause here appears
1699651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // to never happen.
1700b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    if (TypeQuals && Result->isReferenceType()) {
1701b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      diagnoseAndRemoveTypeQualifiers(
1702b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          S, DS, TypeQuals, Result,
1703b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          DeclSpec::TQ_const | DeclSpec::TQ_volatile | DeclSpec::TQ_atomic,
1704b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          diag::warn_typecheck_reference_qualifiers);
17051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
17061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1707bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // C90 6.5.3 constraints: "The same type qualifier shall not appear more
1708bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // than once in the same specifier-list or qualifier-list, either directly
1709bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // or via one or more typedefs."
171091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus
1711bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman        && TypeQuals & Result.getCVRQualifiers()) {
1712bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()) {
171391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec)
1714bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "const";
1715bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1716bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
1717bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()) {
171891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec)
1719bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "volatile";
1720bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1721bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
17224cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // C90 doesn't have restrict nor _Atomic, so it doesn't force us to
17234cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // produce a warning in this case.
1724bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    }
1725bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
17264cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    QualType Qualified = S.BuildQualifiedType(Result, DeclLoc, TypeQuals, &DS);
17274cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
17284cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // If adding qualifiers fails, just use the unqualified type.
17294cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (Qualified.isNull())
17304cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      declarator.setInvalidType(true);
17314cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    else
17324cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      Result = Qualified;
173396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
17340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
17350e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  assert(!Result.isNull() && "This function should not return a null type");
1736f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
1737f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
1738f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
1739cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
1740cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
1741cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
17421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1743cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
1744cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1745cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
17462865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
174793d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith                                  Qualifiers Qs, const DeclSpec *DS) {
17480e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (T.isNull())
17490e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return QualType();
17500e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
17512865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
17522865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
17532865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
17542865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
17552865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
17562865474261a608c7873b87ba4af110d17907896dJohn McCall
175793d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith    if (T->isAnyPointerType() || T->isReferenceType() ||
175893d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        T->isMemberPointerType()) {
175993d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      QualType EltTy;
176093d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      if (T->isObjCObjectPointerType())
176193d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = T;
176293d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>())
176393d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = PTy->getPointeeType();
176493d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      else
176593d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = T->getPointeeType();
176693d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith
176793d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      // If we have a pointer or reference, the pointee must have an object
176893d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      // incomplete type.
176993d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      if (!EltTy->isIncompleteOrObjectType()) {
17702865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
177193d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        ProblemTy = EltTy;
177291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
177393d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith    } else if (!T->isDependentType()) {
177493d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      DiagID = diag::err_typecheck_invalid_restrict_not_pointer;
17752865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
17762865474261a608c7873b87ba4af110d17907896dJohn McCall    }
17772865474261a608c7873b87ba4af110d17907896dJohn McCall
17782865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
177993d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << ProblemTy;
17802865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
17812865474261a608c7873b87ba4af110d17907896dJohn McCall    }
17822865474261a608c7873b87ba4af110d17907896dJohn McCall  }
17832865474261a608c7873b87ba4af110d17907896dJohn McCall
17842865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
17852865474261a608c7873b87ba4af110d17907896dJohn McCall}
17862865474261a608c7873b87ba4af110d17907896dJohn McCall
17874cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard SmithQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
17884967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                  unsigned CVRAU, const DeclSpec *DS) {
17890e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (T.isNull())
17900e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return QualType();
17910e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
17924967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // Convert from DeclSpec::TQ to Qualifiers::TQ by just dropping TQ_atomic and
17934967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // TQ_unaligned;
17944967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  unsigned CVR = CVRAU & ~(DeclSpec::TQ_atomic | DeclSpec::TQ_unaligned);
17954cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
17964cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // C11 6.7.3/5:
17974cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   If the same qualifier appears more than once in the same
17984cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   specifier-qualifier-list, either directly or via one or more typedefs,
17994cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   the behavior is the same as if it appeared only once.
18004cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //
18014cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // It's not specified what happens when the _Atomic qualifier is applied to
18024cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // a type specified with the _Atomic specifier, but we assume that this
18034cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // should be treated as if the _Atomic qualifier appeared multiple times.
18044967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if (CVRAU & DeclSpec::TQ_atomic && !T->isAtomicType()) {
18054cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // C11 6.7.3/5:
18064cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   If other qualifiers appear along with the _Atomic qualifier in a
18074cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   specifier-qualifier-list, the resulting type is the so-qualified
18084cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   atomic type.
18094cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //
18104cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // Don't need to worry about array types here, since _Atomic can't be
18114cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // applied to such types.
18124cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    SplitQualType Split = T.getSplitUnqualifiedType();
18134cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    T = BuildAtomicType(QualType(Split.Ty, 0),
18144cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith                        DS ? DS->getAtomicSpecLoc() : Loc);
18154cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (T.isNull())
18164cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      return T;
18174cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    Split.Quals.addCVRQualifiers(CVR);
18184cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    return BuildQualifiedType(T, Loc, Split.Quals);
18194cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
18204cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
18214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  Qualifiers Q = Qualifiers::fromCVRMask(CVR);
18224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  Q.setUnaligned(CVRAU & DeclSpec::TQ_unaligned);
18234967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  return BuildQualifiedType(T, Loc, Q, DS);
18244cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith}
18254cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
1826075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
1827075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
1828075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
1829075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
1830075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
1831f85e193739c953358c865005855253af4f68a497John McCall/// Given that we're building a pointer or reference to the given
1832f85e193739c953358c865005855253af4f68a497John McCallstatic QualType inferARCLifetimeForPointee(Sema &S, QualType type,
1833f85e193739c953358c865005855253af4f68a497John McCall                                           SourceLocation loc,
1834f85e193739c953358c865005855253af4f68a497John McCall                                           bool isReference) {
1835f85e193739c953358c865005855253af4f68a497John McCall  // Bail out if retention is unrequired or already specified.
1836f85e193739c953358c865005855253af4f68a497John McCall  if (!type->isObjCLifetimeType() ||
1837f85e193739c953358c865005855253af4f68a497John McCall      type.getObjCLifetime() != Qualifiers::OCL_None)
1838f85e193739c953358c865005855253af4f68a497John McCall    return type;
1839f85e193739c953358c865005855253af4f68a497John McCall
1840f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None;
1841f85e193739c953358c865005855253af4f68a497John McCall
1842f85e193739c953358c865005855253af4f68a497John McCall  // If the object type is const-qualified, we can safely use
1843f85e193739c953358c865005855253af4f68a497John McCall  // __unsafe_unretained.  This is safe (because there are no read
1844f85e193739c953358c865005855253af4f68a497John McCall  // barriers), and it'll be safe to coerce anything but __weak* to
1845f85e193739c953358c865005855253af4f68a497John McCall  // the resulting type.
1846f85e193739c953358c865005855253af4f68a497John McCall  if (type.isConstQualified()) {
1847f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
1848f85e193739c953358c865005855253af4f68a497John McCall
1849f85e193739c953358c865005855253af4f68a497John McCall  // Otherwise, check whether the static type does not require
1850f85e193739c953358c865005855253af4f68a497John McCall  // retaining.  This currently only triggers for Class (possibly
1851f85e193739c953358c865005855253af4f68a497John McCall  // protocol-qualifed, and arrays thereof).
1852f85e193739c953358c865005855253af4f68a497John McCall  } else if (type->isObjCARCImplicitlyUnretainedType()) {
1853f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
18545b76f373d23cc3b292ecf523349aaaa388eea375Argyrios Kyrtzidis
1855ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // If we are in an unevaluated context, like sizeof, skip adding a
1856ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // qualification.
185771f55f771794674a410171dbf3cb5dbedf95d033David Blaikie  } else if (S.isUnevaluatedContext()) {
1858ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    return type;
1859f85e193739c953358c865005855253af4f68a497John McCall
1860e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // If that failed, give an error and recover using __strong.  __strong
1861e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // is the option most likely to prevent spurious second-order diagnostics,
1862e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // like when binding a reference to a field.
1863f85e193739c953358c865005855253af4f68a497John McCall  } else {
1864f85e193739c953358c865005855253af4f68a497John McCall    // These types can show up in private ivars in system headers, so
1865f85e193739c953358c865005855253af4f68a497John McCall    // we need this to not be an error in those cases.  Instead we
1866f85e193739c953358c865005855253af4f68a497John McCall    // want to delay.
1867f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1868ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.DelayedDiagnostics.add(
1869ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman          sema::DelayedDiagnostic::makeForbiddenType(loc,
1870ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman              diag::err_arc_indirect_no_ownership, type, isReference));
1871f85e193739c953358c865005855253af4f68a497John McCall    } else {
1872ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference;
1873f85e193739c953358c865005855253af4f68a497John McCall    }
1874e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall    implicitLifetime = Qualifiers::OCL_Strong;
1875f85e193739c953358c865005855253af4f68a497John McCall  }
1876f85e193739c953358c865005855253af4f68a497John McCall  assert(implicitLifetime && "didn't infer any lifetime!");
1877f85e193739c953358c865005855253af4f68a497John McCall
1878f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers qs;
1879f85e193739c953358c865005855253af4f68a497John McCall  qs.addObjCLifetime(implicitLifetime);
1880f85e193739c953358c865005855253af4f68a497John McCall  return S.Context.getQualifiedType(type, qs);
1881f85e193739c953358c865005855253af4f68a497John McCall}
1882f85e193739c953358c865005855253af4f68a497John McCall
18836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstatic std::string getFunctionQualifiersAsString(const FunctionProtoType *FnTy){
18846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  std::string Quals =
18856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
18866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
18876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  switch (FnTy->getRefQualifier()) {
18886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  case RQ_None:
18896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    break;
18906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
18916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  case RQ_LValue:
18926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!Quals.empty())
18936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Quals += ' ';
18946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Quals += '&';
18956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    break;
18966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
18976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  case RQ_RValue:
18986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!Quals.empty())
18996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Quals += ' ';
19006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Quals += "&&";
19016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    break;
19026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
19036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
19046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return Quals;
19056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
19066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
19076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesnamespace {
19086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// Kinds of declarator that cannot contain a qualified function type.
19096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///
19106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// C++98 [dcl.fct]p4 / C++11 [dcl.fct]p6:
19116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///     a function type with a cv-qualifier or a ref-qualifier can only appear
19126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///     at the topmost level of a type.
19136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///
19146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// Parens and member pointers are permitted. We don't diagnose array and
19156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// function declarators, because they don't allow function types at all.
19166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///
19176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// The values of this enum are used in diagnostics.
19186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesenum QualifiedFunctionKind { QFK_BlockPointer, QFK_Pointer, QFK_Reference };
19194967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} // end anonymous namespace
19206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
19216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// Check whether the type T is a qualified function type, and if it is,
19226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// diagnose that it cannot be contained within the given kind of declarator.
19236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstatic bool checkQualifiedFunction(Sema &S, QualType T, SourceLocation Loc,
19246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                   QualifiedFunctionKind QFK) {
19256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // Does T refer to a function type with a cv-qualifier or a ref-qualifier?
19266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const FunctionProtoType *FPT = T->getAs<FunctionProtoType>();
19276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!FPT || (FPT->getTypeQuals() == 0 && FPT->getRefQualifier() == RQ_None))
19286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return false;
19296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
19306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  S.Diag(Loc, diag::err_compound_qualified_function_type)
19316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    << QFK << isa<FunctionType>(T.IgnoreParens()) << T
19326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    << getFunctionQualifiersAsString(FPT);
19336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return true;
19346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
19356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
1936cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
1937cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1938cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
1939cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1940cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1941cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
1942cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
1943cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1944cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
1945cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1946cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1947cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
1948cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
19492865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
1950cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
1951cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
1952cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
1953cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
1954ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1955cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1956cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1957cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
19586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (checkQualifiedFunction(*this, T, Loc, QFK_Pointer))
19596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return QualType();
19606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
1961c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
196292e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
1963f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build pointers to unqualified pointers.
19644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1965f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false);
1966f85e193739c953358c865005855253af4f68a497John McCall
1967cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
19682865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
1969cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1970cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1971cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
1972cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1973cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
1974cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1975cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1976cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
1977cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
1978cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1979cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
1980cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1981cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1982cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
1983cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
198454e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
19852865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
198654e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
198791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  assert(Context.getCanonicalType(T) != Context.OverloadTy &&
19889625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
198991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
199069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // C++0x [dcl.ref]p6:
199191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   If a typedef (7.1.3), a type template-parameter (14.3.1), or a
199291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
199391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   type T, an attempt to create the type "lvalue reference to cv TR" creates
199491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   the type "lvalue reference to T", while an attempt to create the type
199569d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   "rvalue reference to cv TR" creates the type TR.
199654e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
199754e14c4db764c0636160d26c5bbf491637c83a76John McCall
199854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
199954e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
200054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
200154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
200254e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
200354e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
200454e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
200554e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
200654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
200754e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
200854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
200969d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // collapsing of references-to-references as described in C++0x.
201069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // DR 106 and 540 introduce reference-collapsing into C++98/03.
2011cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
2012cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
201333a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
2014cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
2015cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
2016cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
2017cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
2018cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
2019cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
20206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (checkQualifiedFunction(*this, T, Loc, QFK_Reference))
20216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return QualType();
20226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
2023f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build references to unqualified pointers.
20244e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
2025f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true);
2026f85e193739c953358c865005855253af4f68a497John McCall
2027cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
20287c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
20292865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
20302865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
2031cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
2032cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
20334967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// \brief Build a Pipe type.
20344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar///
20354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// \param T The type to which we'll be building a Pipe.
20364967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar///
20374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// \param Loc We do not use it for now.
20384967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar///
20394967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// \returns A suitable pipe type, if there are no errors. Otherwise, returns a
20404967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// NULL type.
20414967a710c84587c654b56c828382219c3937dacbPirama Arumuga NainarQualType Sema::BuildPipeType(QualType T, SourceLocation Loc) {
20424967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
20434967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
20444967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // Build the pipe type.
20454967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  return Context.getPipeType(T);
20464967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar}
20474967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
2048e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// Check whether the specified array size makes the array type a VLA.  If so,
2049e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// return true, if not, return the size of the array in SizeVal.
2050282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smithstatic bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal) {
2051282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode
2052282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // (like gnu99, but not c99) accept any evaluatable value as an extension.
2053ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  class VLADiagnoser : public Sema::VerifyICEDiagnoser {
2054ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  public:
2055ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    VLADiagnoser() : Sema::VerifyICEDiagnoser(true) {}
205691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2057651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) override {
2058ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
205991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2060651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    void diagnoseFold(Sema &S, SourceLocation Loc, SourceRange SR) override {
2061ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      S.Diag(Loc, diag::ext_vla_folded_to_constant) << SR;
2062ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
2063ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  } Diagnoser;
206491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2065ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  return S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser,
20664967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                           S.LangOpts.GNUMode ||
20674967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                           S.LangOpts.OpenCL).isInvalid();
2068e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner}
2069e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
2070cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
2071cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
2072cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
2073cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
2074cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
20751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
20761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
2077cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
2078efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Brackets The range from the opening '[' to the closing ']'.
2079cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
2080cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
2081cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
2082cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
2083cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
2084cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
2085cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
2086cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
20877e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
20880953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
20897e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
20904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
2091138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
2092138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
209391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   type, the (possibly cv-qualified) type void, a function type or an
2094138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
2095138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
2096bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    // C++ [dcl.array]p3:
2097bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   When several "array of" specifications are adjacent, [...] only the
2098bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   first of the constant expressions that specify the bounds of the arrays
2099bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   may be omitted.
2100bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //
2101138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
2102138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
2103138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
2104138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
2105138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
2106138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
210791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2108bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    if (T->isVoidType() || T->isIncompleteArrayType()) {
2109923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
2110923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
2111923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
211291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
211391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (RequireNonAbstractType(Brackets.getBegin(), T,
2114138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
2115138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
211691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2117651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // Mentioning a member pointer type for an array type causes us to lock in
2118651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // an inheritance model, even if it's inside an unused typedef.
2119651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (Context.getTargetInfo().getCXXABI().isMicrosoft())
2120651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
2121651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        if (!MPTy->getClass()->isDependentType())
212287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          (void)isCompleteType(Loc, T);
2123651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2124923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
2125138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
2126138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
2127923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
2128923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
2129923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
2130923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
2131cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
2132cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
2133cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
2134ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
2135cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
2136cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
21371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21386217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
2139cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
2140cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
2141cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
2142cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
2143c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
2144c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
2145c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
2146cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
21471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2148806054db6653d29cb0d9692df3612cbcd03d0530John McCall  // Do placeholder conversions on the array size expression.
2149806054db6653d29cb0d9692df3612cbcd03d0530John McCall  if (ArraySize && ArraySize->hasPlaceholderType()) {
2150806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ExprResult Result = CheckPlaceholderExpr(ArraySize);
2151806054db6653d29cb0d9692df3612cbcd03d0530John McCall    if (Result.isInvalid()) return QualType();
2152c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    ArraySize = Result.get();
2153806054db6653d29cb0d9692df3612cbcd03d0530John McCall  }
2154806054db6653d29cb0d9692df3612cbcd03d0530John McCall
21555e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
2156429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ArraySize && !ArraySize->isRValue()) {
2157429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult Result = DefaultLvalueConversion(ArraySize);
2158429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Result.isInvalid())
2159429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return QualType();
2160429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
2161c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    ArraySize = Result.get();
2162429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
21635e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
2164cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
2165282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // C++11 allows contextual conversions to such types.
216680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (!getLangOpts().CPlusPlus11 &&
2167282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      ArraySize && !ArraySize->isTypeDependent() &&
21681274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
2169cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
2170cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
2171cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
2172cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
2173282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
21742767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
2175cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
2176f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
21776bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      T = Context.getVariableArrayType(T, nullptr, ASM, Quals, Brackets);
2178f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
2179f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
2180ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
21817e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
2182282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  } else if ((!T->isDependentType() && !T->isIncompleteType() &&
2183282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith              !T->isConstantSizeType()) ||
2184282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith             isArraySizeVLA(*this, ArraySize, ConstVal)) {
2185282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // Even in C++11, don't allow contextual conversions in the array bound
2186282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // of a VLA.
218780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (getLangOpts().CPlusPlus11 &&
2188282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
2189282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
2190282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        << ArraySize->getType() << ArraySize->getSourceRange();
2191282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      return QualType();
2192282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    }
2193282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
2194e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with an element type that has a non-constant-size is a VLA.
2195e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with a non-ICE size is a VLA.  We accept any expression
2196e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // that we can fold to a non-zero positive value as an extension.
21977e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
2198cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
2199cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
2200cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
2201923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
2202b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
2203b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
2204b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
2205b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
2206b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
2207b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
2208923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
2209923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
2210923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
221102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
221202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
221391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      Diag(ArraySize->getLocStart(),
221402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
221502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
2216923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
221720cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne
221820cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      if (ASM == ArrayType::Static) {
221920cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        Diag(ArraySize->getLocStart(),
222020cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne             diag::warn_typecheck_zero_static_array_size)
222120cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne          << ArraySize->getSourceRange();
222220cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        ASM = ArrayType::Normal;
222320cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      }
222491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
2225fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali               !T->isIncompleteType() && !T->isUndeducedType()) {
222691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // Is the array too large?
22272767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
22282767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
2229ee2f8f2f7c4eacfa305a29fcd916d63c650ca847Richard Trieu      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) {
22302767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
22312767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
22322767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
2233ee2f8f2f7c4eacfa305a29fcd916d63c650ca847Richard Trieu        return QualType();
2234ee2f8f2f7c4eacfa305a29fcd916d63c650ca847Richard Trieu      }
22351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
223691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
223746a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
2238cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
2239617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly
2240617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  // OpenCL v1.2 s6.9.d: variable length arrays are not supported.
2241617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  if (getLangOpts().OpenCL && T->isVariableArrayType()) {
2242617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    Diag(Loc, diag::err_opencl_vla);
2243617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    return QualType();
2244617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  }
2245af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
22464e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().C99) {
22470fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
2248a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
22494967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (isSFINAEContext()) {
2250a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
2251a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
2252a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
22530fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
22540fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
22558a3f9e46cb988d2c664395b21910091e3730ae82Richard Smith        Diag(Loc, diag::ext_vla);
22560fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
2257d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith      Diag(Loc,
22584e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie           getLangOpts().CPlusPlus? diag::err_c99_array_usage_cxx
225987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                  : diag::ext_c99_array_usage) << ASM;
2260cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
2261cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
2262630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko  if (T->isVariableArrayType()) {
2263630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko    // Warn about VLAs for -Wvla.
2264630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko    Diag(Loc, diag::warn_vla_used);
2265630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko  }
2266630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko
22674967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // OpenCL v2.0 s6.12.5 - Arrays of blocks are not supported.
22684967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // OpenCL v2.0 s6.16.13.1 - Arrays of pipe type are not supported.
22694967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // OpenCL v2.0 s6.9.b - Arrays of image/sampler type are not supported.
22704967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if (getLangOpts().OpenCL) {
22714967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    const QualType ArrType = Context.getBaseElementType(T);
22724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (ArrType->isBlockPointerType() || ArrType->isPipeType() ||
22734967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        ArrType->isSamplerT() || ArrType->isImageType()) {
22744967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      Diag(Loc, diag::err_opencl_invalid_type_array) << ArrType;
22754967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      return QualType();
22764967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
22774967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
22784967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
2279cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
2280cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
22819cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
22829cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
22839cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
22849cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
22859ae2f076ca5ab1feb3ba95629099ec2319833701John McCallQualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
22869cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
22874967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // Unlike gcc's vector_size attribute, we do not allow vectors to be defined
22889cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
22894967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  //
22904967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // Additionally, OpenCL prohibits vectors of booleans (they're considered a
22914967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // reserved data type under OpenCL v2.0 s6.1.4), we don't support selects
22924967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // on bitvectors, and we have no well-defined ABI for bitvectors, so vectors
22934967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // of bool aren't allowed.
22944967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if ((!T->isDependentType() && !T->isIntegerType() &&
22954967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar       !T->isRealFloatingType()) ||
22964967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      T->isBooleanType()) {
22979cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
22989cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
22999cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
23009cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
23019ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
23029cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
23039ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
23049f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      Diag(AttrLoc, diag::err_attribute_argument_type)
23059f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman        << "ext_vector_type" << AANT_ArgumentIntegerConstant
23069f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman        << ArraySize->getSourceRange();
23079cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
23089cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
23091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23104967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Unlike gcc's vector_size attribute, the size is specified as the
23119cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
23121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
23131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23149cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
23159cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
23169ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      << ArraySize->getSourceRange();
23179cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
23189cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
23191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23201652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    if (VectorType::isVectorSizeTooLarge(vectorSize)) {
23211652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      Diag(AttrLoc, diag::err_attribute_size_too_large)
23221652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman        << ArraySize->getSourceRange();
23231652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      return QualType();
23241652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    }
23251652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman
23264ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    return Context.getExtVectorType(T, vectorSize);
23271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
23281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23299ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
23309cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
23311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2332ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedmanbool Sema::CheckFunctionReturnType(QualType T, SourceLocation Loc) {
2333724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
233491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    Diag(Loc, diag::err_func_returning_array_function)
233558408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
2336ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    return true;
2337724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
2338aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
2339aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // Functions cannot return half FP.
2340176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (T->isHalfType() && !getLangOpts().HalfArgsAndReturns) {
2341aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 <<
2342aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      FixItHint::CreateInsertion(Loc, "*");
2343ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    return true;
2344ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  }
2345ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
2346ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  // Methods cannot return interface types. All ObjC objects are
2347ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  // passed by reference.
2348ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  if (T->isObjCObjectType()) {
2349ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    Diag(Loc, diag::err_object_cannot_be_passed_returned_by_value) << 0 << T;
2350ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman    return 0;
2351aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  }
2352aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
2353ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  return false;
2354ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman}
2355ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
23564967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// Check the extended parameter information.  Most of the necessary
23574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// checking should occur when applying the parameter attribute; the
23584967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// only other checks required are positional restrictions.
23594967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarstatic void checkExtParameterInfos(Sema &S, ArrayRef<QualType> paramTypes,
23604967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                    const FunctionProtoType::ExtProtoInfo &EPI,
23614967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                    llvm::function_ref<SourceLocation(unsigned)> getParamLoc) {
23624967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  assert(EPI.ExtParameterInfos && "shouldn't get here without param infos");
23634967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
23644967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  bool hasCheckedSwiftCall = false;
23654967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  auto checkForSwiftCC = [&](unsigned paramIndex) {
23664967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Only do this once.
23674967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (hasCheckedSwiftCall) return;
23684967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    hasCheckedSwiftCall = true;
23694967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (EPI.ExtInfo.getCC() == CC_Swift) return;
23704967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    S.Diag(getParamLoc(paramIndex), diag::err_swift_param_attr_not_swiftcall)
23714967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      << getParameterABISpelling(EPI.ExtParameterInfos[paramIndex].getABI());
23724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  };
23734967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
23744967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  for (size_t paramIndex = 0, numParams = paramTypes.size();
23754967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          paramIndex != numParams; ++paramIndex) {
23764967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    switch (EPI.ExtParameterInfos[paramIndex].getABI()) {
23774967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // Nothing interesting to check for orindary-ABI parameters.
23784967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case ParameterABI::Ordinary:
23794967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      continue;
23804967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
23814967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // swift_indirect_result parameters must be a prefix of the function
23824967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // arguments.
23834967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case ParameterABI::SwiftIndirectResult:
23844967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      checkForSwiftCC(paramIndex);
23854967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (paramIndex != 0 &&
23864967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          EPI.ExtParameterInfos[paramIndex - 1].getABI()
23874967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar            != ParameterABI::SwiftIndirectResult) {
23884967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        S.Diag(getParamLoc(paramIndex),
23894967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar               diag::err_swift_indirect_result_not_first);
23904967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      }
23914967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      continue;
23924967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
23934967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // swift_context parameters must be the last parameter except for
23944967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // a possible swift_error parameter.
23954967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case ParameterABI::SwiftContext:
23964967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      checkForSwiftCC(paramIndex);
23974967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (!(paramIndex == numParams - 1 ||
23984967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar            (paramIndex == numParams - 2 &&
23994967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar             EPI.ExtParameterInfos[numParams - 1].getABI()
24004967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar               == ParameterABI::SwiftErrorResult))) {
24014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        S.Diag(getParamLoc(paramIndex),
24024967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar               diag::err_swift_context_not_before_swift_error_result);
24034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      }
24044967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      continue;
24054967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
24064967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // swift_error parameters must be the last parameter.
24074967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case ParameterABI::SwiftErrorResult:
24084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      checkForSwiftCC(paramIndex);
24094967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (paramIndex != numParams - 1) {
24104967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        S.Diag(getParamLoc(paramIndex),
24114967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar               diag::err_swift_error_result_not_last);
24124967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      } else if (paramIndex == 0 ||
24134967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                 EPI.ExtParameterInfos[paramIndex - 1].getABI()
24144967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                   != ParameterABI::SwiftContext) {
24154967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        S.Diag(getParamLoc(paramIndex),
24164967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar               diag::err_swift_error_result_not_after_swift_context);
24174967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      }
24184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      continue;
24194967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
24204967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    llvm_unreachable("bad ABI kind");
24214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
24224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar}
24234967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
2424ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli FriedmanQualType Sema::BuildFunctionType(QualType T,
2425c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                 MutableArrayRef<QualType> ParamTypes,
2426ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
2427ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman                                 const FunctionProtoType::ExtProtoInfo &EPI) {
2428724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
2429ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
2430ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman  Invalid |= CheckFunctionReturnType(T, Loc);
2431ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
2432bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  for (unsigned Idx = 0, Cnt = ParamTypes.size(); Idx < Cnt; ++Idx) {
2433aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    // FIXME: Loc is too inprecise here, should use proper locations for args.
2434c910d4cfa5042f2c9da1eb4e0b6ed59240c0eeeeReid Kleckner    QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]);
24352dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
2436724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
2437724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
2438176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    } else if (ParamType->isHalfType() && !getLangOpts().HalfArgsAndReturns) {
2439aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Disallow half FP arguments.
2440aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 <<
2441aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        FixItHint::CreateInsertion(Loc, "*");
2442aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Invalid = true;
2443724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
2444cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
244554e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
2446724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
2447724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
24484967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if (EPI.ExtParameterInfos) {
24494967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    checkExtParameterInfos(*this, ParamTypes, EPI,
24504967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                           [=](unsigned i) { return Loc; });
24514967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
24524967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
2453724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
2454724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
2455724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
2456bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  return Context.getFunctionType(T, ParamTypes, EPI);
2457724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
24581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2459949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
2460949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
2461949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
2462949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
2463949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
2464949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
2465949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
2466949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
2467949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
24681eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
24692865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
2470949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
2471949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
2472949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
2473949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
2474949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
24750e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return QualType();
2476949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
2477949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
2478737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
2479949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
2480949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
24818d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
2482651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      << getPrintableNameForEntity(Entity) << T;
2483949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
2484949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
2485949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
2486949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
2487949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
2488651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      << getPrintableNameForEntity(Entity);
2489949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
2490949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
2491949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
2492949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
2493949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
2494949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
2495949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
2496949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
2497651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Adjust the default free function calling convention to the default method
2498651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // calling convention.
249987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  bool IsCtorOrDtor =
250087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      (Entity.getNameKind() == DeclarationName::CXXConstructorName) ||
250187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      (Entity.getNameKind() == DeclarationName::CXXDestructorName);
2502651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (T->isFunctionType())
250387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    adjustMemberFunctionCC(T, /*IsStatic=*/false, IsCtorOrDtor, Loc);
2504ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
25052865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
2506949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
25071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25089a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
25099a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
25109a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
25119a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
2512efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Loc The source location, used for diagnostics.
25139a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
25149a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
25159a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
25169a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
25179a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
25189a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
251991cbbbf506c892a26d4301e2b3ccd377b0938817Chad RosierQualType Sema::BuildBlockPointerType(QualType T,
25201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
25219a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
25220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
25239a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
25249a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
25259a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
25261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (checkQualifiedFunction(*this, T, Loc, QFK_BlockPointer))
25286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return QualType();
25296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
25302865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
25319a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
25329a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
2533b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
2534b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
25353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
25366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (TInfo) *TInfo = nullptr;
25373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
25383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
25393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
25406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  TypeSourceInfo *DI = nullptr;
2541f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
2542e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
2543a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
2544e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
25451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2546a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
2547e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
2548e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
2549e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
2550a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
2551a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
2552a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            unsigned chunkIndex);
2553a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis
2554f85e193739c953358c865005855253af4f68a497John McCall/// Given that this is the declaration of a parameter under ARC,
2555f85e193739c953358c865005855253af4f68a497John McCall/// attempt to infer attributes and such for pointer-to-whatever
2556f85e193739c953358c865005855253af4f68a497John McCall/// types.
2557f85e193739c953358c865005855253af4f68a497John McCallstatic void inferARCWriteback(TypeProcessingState &state,
2558f85e193739c953358c865005855253af4f68a497John McCall                              QualType &declSpecType) {
2559f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
2560f85e193739c953358c865005855253af4f68a497John McCall  Declarator &declarator = state.getDeclarator();
2561f85e193739c953358c865005855253af4f68a497John McCall
2562f85e193739c953358c865005855253af4f68a497John McCall  // TODO: should we care about decl qualifiers?
2563f85e193739c953358c865005855253af4f68a497John McCall
2564f85e193739c953358c865005855253af4f68a497John McCall  // Check whether the declarator has the expected form.  We walk
2565f85e193739c953358c865005855253af4f68a497John McCall  // from the inside out in order to make the block logic work.
2566f85e193739c953358c865005855253af4f68a497John McCall  unsigned outermostPointerIndex = 0;
2567f85e193739c953358c865005855253af4f68a497John McCall  bool isBlockPointer = false;
2568f85e193739c953358c865005855253af4f68a497John McCall  unsigned numPointers = 0;
2569f85e193739c953358c865005855253af4f68a497John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
2570f85e193739c953358c865005855253af4f68a497John McCall    unsigned chunkIndex = i;
2571f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex);
2572f85e193739c953358c865005855253af4f68a497John McCall    switch (chunk.Kind) {
2573f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Paren:
2574f85e193739c953358c865005855253af4f68a497John McCall      // Ignore parens.
2575f85e193739c953358c865005855253af4f68a497John McCall      break;
2576f85e193739c953358c865005855253af4f68a497John McCall
2577f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Reference:
2578f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Pointer:
2579f85e193739c953358c865005855253af4f68a497John McCall      // Count the number of pointers.  Treat references
2580f85e193739c953358c865005855253af4f68a497John McCall      // interchangeably as pointers; if they're mis-ordered, normal
2581f85e193739c953358c865005855253af4f68a497John McCall      // type building will discover that.
2582f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
2583f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
2584f85e193739c953358c865005855253af4f68a497John McCall      break;
2585f85e193739c953358c865005855253af4f68a497John McCall
2586f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::BlockPointer:
2587f85e193739c953358c865005855253af4f68a497John McCall      // If we have a pointer to block pointer, that's an acceptable
2588f85e193739c953358c865005855253af4f68a497John McCall      // indirect reference; anything else is not an application of
2589f85e193739c953358c865005855253af4f68a497John McCall      // the rules.
2590f85e193739c953358c865005855253af4f68a497John McCall      if (numPointers != 1) return;
2591f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
2592f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
2593f85e193739c953358c865005855253af4f68a497John McCall      isBlockPointer = true;
2594f85e193739c953358c865005855253af4f68a497John McCall
2595f85e193739c953358c865005855253af4f68a497John McCall      // We don't care about pointer structure in return values here.
2596f85e193739c953358c865005855253af4f68a497John McCall      goto done;
2597f85e193739c953358c865005855253af4f68a497John McCall
2598f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Array: // suppress if written (id[])?
2599f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Function:
2600f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::MemberPointer:
26014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case DeclaratorChunk::Pipe:
2602f85e193739c953358c865005855253af4f68a497John McCall      return;
2603f85e193739c953358c865005855253af4f68a497John McCall    }
2604f85e193739c953358c865005855253af4f68a497John McCall  }
2605f85e193739c953358c865005855253af4f68a497John McCall done:
2606f85e193739c953358c865005855253af4f68a497John McCall
2607f85e193739c953358c865005855253af4f68a497John McCall  // If we have *one* pointer, then we want to throw the qualifier on
2608f85e193739c953358c865005855253af4f68a497John McCall  // the declaration-specifiers, which means that it needs to be a
2609f85e193739c953358c865005855253af4f68a497John McCall  // retainable object type.
2610f85e193739c953358c865005855253af4f68a497John McCall  if (numPointers == 1) {
2611f85e193739c953358c865005855253af4f68a497John McCall    // If it's not a retainable object type, the rule doesn't apply.
2612f85e193739c953358c865005855253af4f68a497John McCall    if (!declSpecType->isObjCRetainableType()) return;
2613f85e193739c953358c865005855253af4f68a497John McCall
2614f85e193739c953358c865005855253af4f68a497John McCall    // If it already has lifetime, don't do anything.
2615f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType.getObjCLifetime()) return;
2616f85e193739c953358c865005855253af4f68a497John McCall
2617f85e193739c953358c865005855253af4f68a497John McCall    // Otherwise, modify the type in-place.
2618f85e193739c953358c865005855253af4f68a497John McCall    Qualifiers qs;
261991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2620f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType->isObjCARCImplicitlyUnretainedType())
2621f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
2622f85e193739c953358c865005855253af4f68a497John McCall    else
2623f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing);
2624f85e193739c953358c865005855253af4f68a497John McCall    declSpecType = S.Context.getQualifiedType(declSpecType, qs);
2625f85e193739c953358c865005855253af4f68a497John McCall
2626f85e193739c953358c865005855253af4f68a497John McCall  // If we have *two* pointers, then we want to throw the qualifier on
2627f85e193739c953358c865005855253af4f68a497John McCall  // the outermost pointer.
2628f85e193739c953358c865005855253af4f68a497John McCall  } else if (numPointers == 2) {
2629f85e193739c953358c865005855253af4f68a497John McCall    // If we don't have a block pointer, we need to check whether the
2630f85e193739c953358c865005855253af4f68a497John McCall    // declaration-specifiers gave us something that will turn into a
2631f85e193739c953358c865005855253af4f68a497John McCall    // retainable object pointer after we slap the first pointer on it.
2632f85e193739c953358c865005855253af4f68a497John McCall    if (!isBlockPointer && !declSpecType->isObjCObjectType())
2633f85e193739c953358c865005855253af4f68a497John McCall      return;
2634f85e193739c953358c865005855253af4f68a497John McCall
2635f85e193739c953358c865005855253af4f68a497John McCall    // Look for an explicit lifetime attribute there.
2636f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex);
26371c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis    if (chunk.Kind != DeclaratorChunk::Pointer &&
26381c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis        chunk.Kind != DeclaratorChunk::BlockPointer)
26391c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis      return;
2640f85e193739c953358c865005855253af4f68a497John McCall    for (const AttributeList *attr = chunk.getAttrs(); attr;
2641f85e193739c953358c865005855253af4f68a497John McCall           attr = attr->getNext())
26428e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt      if (attr->getKind() == AttributeList::AT_ObjCOwnership)
2643f85e193739c953358c865005855253af4f68a497John McCall        return;
2644f85e193739c953358c865005855253af4f68a497John McCall
2645a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis    transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing,
2646a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                          outermostPointerIndex);
2647f85e193739c953358c865005855253af4f68a497John McCall
2648f85e193739c953358c865005855253af4f68a497John McCall  // Any other number of pointers/references does not trigger the rule.
2649f85e193739c953358c865005855253af4f68a497John McCall  } else return;
2650f85e193739c953358c865005855253af4f68a497John McCall
2651f85e193739c953358c865005855253af4f68a497John McCall  // TODO: mark whether we did this inference?
2652f85e193739c953358c865005855253af4f68a497John McCall}
2653f85e193739c953358c865005855253af4f68a497John McCall
2654c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hinesvoid Sema::diagnoseIgnoredQualifiers(unsigned DiagID, unsigned Quals,
2655c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                     SourceLocation FallbackLoc,
2656c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                     SourceLocation ConstQualLoc,
2657c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                     SourceLocation VolatileQualLoc,
2658c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                     SourceLocation RestrictQualLoc,
26594967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                     SourceLocation AtomicQualLoc,
26604967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                     SourceLocation UnalignedQualLoc) {
2661eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith  if (!Quals)
2662eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith    return;
2663eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith
26648c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  struct Qual {
26658c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    const char *Name;
266687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    unsigned Mask;
26678c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation Loc;
26684967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  } const QualKinds[5] = {
266987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    { "const", DeclSpec::TQ_const, ConstQualLoc },
267087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    { "volatile", DeclSpec::TQ_volatile, VolatileQualLoc },
267187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    { "restrict", DeclSpec::TQ_restrict, RestrictQualLoc },
26724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    { "__unaligned", DeclSpec::TQ_unaligned, UnalignedQualLoc },
267387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    { "_Atomic", DeclSpec::TQ_atomic, AtomicQualLoc }
26748c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  };
26758c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
267630d237556fdd29b5075c990da953116225b95d9dRobert Wilhelm  SmallString<32> QualStr;
2677d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  unsigned NumQuals = 0;
2678d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  SourceLocation Loc;
26794967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  FixItHint FixIts[5];
26808c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
26818c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // Build a string naming the redundant qualifiers.
26824967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  for (auto &E : QualKinds) {
26834967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (Quals & E.Mask) {
26848c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if (!QualStr.empty()) QualStr += ' ';
26854967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      QualStr += E.Name;
26868c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
26878c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // If we have a location for the qualifier, offer a fixit.
26884967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      SourceLocation QualLoc = E.Loc;
268987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (QualLoc.isValid()) {
26908c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        FixIts[NumQuals] = FixItHint::CreateRemoval(QualLoc);
2691c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        if (Loc.isInvalid() ||
2692c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines            getSourceManager().isBeforeInTranslationUnit(QualLoc, Loc))
26938c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          Loc = QualLoc;
26948c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      }
2695d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
26968c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      ++NumQuals;
26978c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
26988c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  }
2699d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2700c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  Diag(Loc.isInvalid() ? FallbackLoc : Loc, DiagID)
27018c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    << QualStr << NumQuals << FixIts[0] << FixIts[1] << FixIts[2] << FixIts[3];
27028c952cd40ccec9d720931f27e7d722fed207d536Richard Smith}
2703a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg
27048c952cd40ccec9d720931f27e7d722fed207d536Richard Smith// Diagnose pointless type qualifiers on the return type of a function.
2705c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hinesstatic void diagnoseRedundantReturnTypeQualifiers(Sema &S, QualType RetTy,
2706c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                                  Declarator &D,
2707c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                                  unsigned FunctionChunkIndex) {
27088c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  if (D.getTypeObject(FunctionChunkIndex).Fun.hasTrailingReturnType()) {
27098c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    // FIXME: TypeSourceInfo doesn't preserve location information for
27108c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    // qualifiers.
2711c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    S.diagnoseIgnoredQualifiers(diag::warn_qual_return_type,
2712c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                RetTy.getLocalCVRQualifiers(),
2713c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                D.getIdentifierLoc());
27148c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    return;
2715d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
27168c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
27178c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  for (unsigned OuterChunkIndex = FunctionChunkIndex + 1,
27188c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                End = D.getNumTypeObjects();
27198c952cd40ccec9d720931f27e7d722fed207d536Richard Smith       OuterChunkIndex != End; ++OuterChunkIndex) {
27208c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    DeclaratorChunk &OuterChunk = D.getTypeObject(OuterChunkIndex);
27218c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    switch (OuterChunk.Kind) {
27228c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Paren:
27238c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      continue;
27248c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
27258c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Pointer: {
27268c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      DeclaratorChunk::PointerTypeInfo &PTI = OuterChunk.Ptr;
2727c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      S.diagnoseIgnoredQualifiers(
2728c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines          diag::warn_qual_return_type,
2729c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines          PTI.TypeQuals,
27308c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation(),
27318c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
27328c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
27338c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
27344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          SourceLocation::getFromRawEncoding(PTI.AtomicQualLoc),
27354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          SourceLocation::getFromRawEncoding(PTI.UnalignedQualLoc));
27368c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      return;
27378c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
27388c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
27398c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Function:
27408c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::BlockPointer:
27418c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Reference:
27428c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Array:
27438c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::MemberPointer:
27444967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case DeclaratorChunk::Pipe:
27458c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // FIXME: We can't currently provide an accurate source location and a
27468c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // fix-it hint for these.
2747eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith      unsigned AtomicQual = RetTy->isAtomicType() ? DeclSpec::TQ_atomic : 0;
2748c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      S.diagnoseIgnoredQualifiers(diag::warn_qual_return_type,
2749c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                  RetTy.getCVRQualifiers() | AtomicQual,
2750c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                  D.getIdentifierLoc());
27518c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      return;
27528c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
27538c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
27548c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    llvm_unreachable("unknown declarator chunk kind");
27554cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
2756d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
27578c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // If the qualifiers come from a conversion function type, don't diagnose
27588c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // them -- they're not necessarily redundant, since such a conversion
27598c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // operator can be explicitly called as "x.operator const int()".
27608c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  if (D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId)
27618c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    return;
2762d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
27638c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // Just parens all the way out to the decl specifiers. Diagnose any qualifiers
27648c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // which are present there.
2765c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  S.diagnoseIgnoredQualifiers(diag::warn_qual_return_type,
2766c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                              D.getDeclSpec().getTypeQualifiers(),
2767c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                              D.getIdentifierLoc(),
2768c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                              D.getDeclSpec().getConstSpecLoc(),
2769c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                              D.getDeclSpec().getVolatileSpecLoc(),
2770c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                              D.getDeclSpec().getRestrictSpecLoc(),
27714967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                              D.getDeclSpec().getAtomicSpecLoc(),
27724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                              D.getDeclSpec().getUnalignedSpecLoc());
2773d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth}
2774d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
27758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
27768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                             TypeSourceInfo *&ReturnTypeInfo) {
27778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &SemaRef = state.getSema();
27788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
2779930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
27806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  ReturnTypeInfo = nullptr;
2781711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
27828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // The TagDecl owned by the DeclSpec.
27836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  TagDecl *OwnedTagDecl = nullptr;
27848999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl
27853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
278698a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian  case UnqualifiedId::IK_ImplicitSelfParam:
27873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
27888999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  case UnqualifiedId::IK_Identifier:
27890486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
27903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
27918cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = ConvertDeclSpecToType(state);
279291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2793591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
2794d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
279515987970eeaa1842c29ec8797affd1c1dea05585Abramo Bagnara      // Owned declaration is embedded in declarator.
2796d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl->setEmbeddedInDeclarator(true);
2797591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
2798930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
2799930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
28003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
28010efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
28023f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
2803930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
280491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // "void" instead.
28058cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = SemaRef.Context.VoidTy;
280687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    processTypeAttrs(state, T, TAL_DeclSpec,
280787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                     D.getDeclSpec().getAttributes().getList());
2808930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
280948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
281048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
281148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
281248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
281391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId,
28148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  &ReturnTypeInfo);
281548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
2816930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
2817dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
2818711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
2819711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
2820711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2821d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
282287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (D.getDeclSpec().containsPlaceholderType()) {
2823baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
28241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2825baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
2826f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2827f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman      llvm_unreachable("Can't specify a type specifier in lambda grammar");
2828cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2829cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
2830baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
2831fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      Error = 0;
2832fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      break;
2833fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    case Declarator::LambdaExprParameterContext:
283487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // In C++14, generic lambdas allow 'auto' in their parameters.
2835176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      if (!(SemaRef.getLangOpts().CPlusPlus14
2836fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali              && D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto))
283787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        Error = 16;
2838baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
283987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::MemberContext: {
284087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
284187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          D.isFunctionDeclarator())
28427a614d8380297fcd2bc23986241905d97222948cRichard Smith        break;
284387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      bool Cxx = SemaRef.getLangOpts().CPlusPlus;
28448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) {
2845eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie      case TTK_Enum: llvm_unreachable("unhandled tag kind");
284687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case TTK_Struct: Error = Cxx ? 1 : 2; /* Struct member */ break;
284787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case TTK_Union:  Error = Cxx ? 3 : 4; /* Union member */ break;
284887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case TTK_Class:  Error = 5; /* Class member */ break;
284987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case TTK_Interface: Error = 6; /* Interface member */ break;
28501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
2851baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
285287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
2853baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
285417b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
285587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Error = 7; // Exception declaration
2856baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2857baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
285887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Error = 8; // Template parameter
2859baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2860baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
286187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Error = 9; // Block literal
286234b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
286334b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TemplateTypeArgContext:
286487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Error = 10; // Template type argument
286534b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
2866162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
28673e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
286887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Error = 12; // Type alias
2869162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      break;
28707796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
287187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (!SemaRef.getLangOpts().CPlusPlus14 ||
287287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto_type)
287387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        Error = 13; // Function return type
28747796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
287514f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    case Declarator::ConversionIdContext:
287687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (!SemaRef.getLangOpts().CPlusPlus14 ||
287787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto_type)
287887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        Error = 14; // conversion-type-id
287914f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      break;
288034b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TypeNameContext:
288187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Error = 15; // Generic
2882baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2883baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
2884baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
2885baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
28864967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case Declarator::InitStmtContext:
2887baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
288887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
28890b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
289087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto_type)
289187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        Error = 17; // 'new' type
289287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
289387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::KNRTypeListContext:
289487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Error = 18; // K&R function parameter
2895baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2896baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
2897baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
2898ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith    if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
28996666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11;
29008110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith
290187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // In Objective-C it is an error to use 'auto' on a function declarator
290287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // (and everywhere for '__auto_type').
290387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (D.isFunctionDeclarator() &&
290487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        (!SemaRef.getLangOpts().CPlusPlus11 ||
290587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar         D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto_type))
290687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Error = 13;
290787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
290887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    bool HaveTrailing = false;
290987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
2910d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.spec.auto]p2: 'auto' is always fine if the declarator
2911e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // contains a trailing return type. That is only legal at the outermost
2912e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // level. Check all declarator chunks (outermost first) anyway, to give
2913e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // better diagnostics.
291487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // We don't support '__auto_type' with trailing return types.
291587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (SemaRef.getLangOpts().CPlusPlus11 &&
291687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        D.getDeclSpec().getTypeSpecType() != DeclSpec::TST_auto_type) {
2917e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2918e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned chunkIndex = e - i - 1;
2919e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        state.setCurrentChunkIndex(chunkIndex);
2920e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2921e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (DeclType.Kind == DeclaratorChunk::Function) {
2922e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
292354655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (FTI.hasTrailingReturnType()) {
292487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            HaveTrailing = true;
2925e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Error = -1;
2926e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            break;
2927e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          }
2928e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        }
2929e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2930e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    }
2931e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
293214f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    SourceRange AutoRange = D.getDeclSpec().getTypeSpecTypeLoc();
293314f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    if (D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId)
293414f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      AutoRange = D.getName().getSourceRange();
293514f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith
2936baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
293787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      unsigned Keyword;
293887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      switch (D.getDeclSpec().getTypeSpecType()) {
293987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case DeclSpec::TST_auto: Keyword = 0; break;
294087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case DeclSpec::TST_decltype_auto: Keyword = 1; break;
294187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case DeclSpec::TST_auto_type: Keyword = 2; break;
294287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      default: llvm_unreachable("unknown auto TypeSpecType");
294387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
294414f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      SemaRef.Diag(AutoRange.getBegin(), diag::err_auto_not_allowed)
294587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << Keyword << Error << AutoRange;
29468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = SemaRef.Context.IntTy;
2947baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
294887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    } else if (!HaveTrailing) {
294987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // If there was a trailing return type, we already got
295087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // warn_cxx98_compat_trailing_return_type in the parser.
295114f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith      SemaRef.Diag(AutoRange.getBegin(),
295214f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith                   diag::warn_cxx98_compat_auto_type_specifier)
295314f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith        << AutoRange;
295487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
2955baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
29568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
29574e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().CPlusPlus &&
29585e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall      OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) {
29598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // Check the contexts where C++ forbids the declaration of a new class
29608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // or enumeration in a type-specifier-seq.
296187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    unsigned DiagID = 0;
29628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    switch (D.getContext()) {
29637796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
29647796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // Class and enumeration definitions are syntactically not allowed in
29657796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // trailing return types.
29667796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      llvm_unreachable("parser should not have allowed this");
29677796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
29688cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::FileContext:
29698cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::MemberContext:
29708cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockContext:
29718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ForContext:
29724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case Declarator::InitStmtContext:
29738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockLiteralContext:
2974f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2975d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // C++11 [dcl.type]p3:
29768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   A type-specifier-seq shall not define a class or enumeration unless
29778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   it appears in the type-id of an alias-declaration (7.1.3) that is not
29788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   the declaration of a template-declaration.
29798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasDeclContext:
29808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
29818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasTemplateContext:
298287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      DiagID = diag::err_type_defined_in_alias_template;
29838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
29848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TypeNameContext:
298514f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    case Declarator::ConversionIdContext:
29868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateParamContext:
29878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXNewContext:
29888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXCatchContext:
29898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ObjCCatchContext:
29908cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateTypeArgContext:
299187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      DiagID = diag::err_type_defined_in_type_specifier;
29928cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
29938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::PrototypeContext:
2994fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    case Declarator::LambdaExprParameterContext:
2995cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2996cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
29978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::KNRTypeListContext:
29988cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ [dcl.fct]p6:
29998cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   Types shall not be defined in return or parameter types.
300087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      DiagID = diag::err_type_defined_in_param_type;
30018cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
30028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ConditionContext:
30038cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ 6.4p2:
30048cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // The type-specifier-seq shall not contain typedef and shall not declare
30058cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // a new class or enumeration.
300687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      DiagID = diag::err_type_defined_in_condition;
30078cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
30088cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    }
300987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
301087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (DiagID != 0) {
301187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      SemaRef.Diag(OwnedTagDecl->getLocation(), DiagID)
301287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
301387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      D.setInvalidType(true);
301487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
30158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  }
30168cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
30170e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  assert(!T.isNull() && "This function should not return a null type");
30188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return T;
30198cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
30208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
30216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// Produce an appropriate diagnostic for an ambiguity between a function
3022b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// declarator and a C++ direct-initializer.
3023b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smithstatic void warnAboutAmbiguousFunction(Sema &S, Declarator &D,
3024b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith                                       DeclaratorChunk &DeclType, QualType RT) {
3025b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
3026b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  assert(FTI.isAmbiguous && "no direct-initializer / function ambiguity");
3027b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
3028b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // If the return type is void there is no ambiguity.
3029b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isVoidType())
3030b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
3031b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
3032b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a non-class type can have at most one argument.
3033651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (!RT->isRecordType() && FTI.NumParams > 1)
3034b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
3035b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
3036b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a reference must have exactly one argument.
3037651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (RT->isReferenceType() && FTI.NumParams != 1)
3038b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
3039b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
3040b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Only warn if this declarator is declaring a function at block scope, and
3041b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // doesn't have a storage class (such as 'extern') specified.
3042b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!D.isFunctionDeclarator() ||
3043b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      D.getFunctionDefinitionKind() != FDK_Declaration ||
3044b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      !S.CurContext->isFunctionOrMethod() ||
3045d2615cc53b916e8aae45783ca7113b93de515ce3Rafael Espindola      D.getDeclSpec().getStorageClassSpec()
3046b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        != DeclSpec::SCS_unspecified)
3047b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
3048b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
3049b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Inside a condition, a direct initializer is not permitted. We allow one to
3050b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // be parsed in order to give better diagnostics in condition parsing.
3051b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (D.getContext() == Declarator::ConditionContext)
3052b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
3053b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
3054b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  SourceRange ParenRange(DeclType.Loc, DeclType.EndLoc);
3055b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
3056d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  S.Diag(DeclType.Loc,
3057651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines         FTI.NumParams ? diag::warn_parens_disambiguated_as_function_declaration
3058651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                       : diag::warn_empty_parens_are_function_decl)
3059651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      << ParenRange;
3060d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
3061d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // If the declaration looks like:
3062d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   T var1,
3063d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   f();
3064d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // and name lookup finds a function named 'f', then the ',' was
3065d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // probably intended to be a ';'.
3066d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (!D.isFirstDeclarator() && D.getIdentifier()) {
3067d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Comma(D.getCommaLoc(), S.SourceMgr);
3068d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Name(D.getIdentifierLoc(), S.SourceMgr);
3069d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    if (Comma.getFileID() != Name.getFileID() ||
3070d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) {
3071d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      LookupResult Result(S, D.getIdentifier(), SourceLocation(),
3072d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                          Sema::LookupOrdinaryName);
3073d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      if (S.LookupName(Result, S.getCurScope()))
3074d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        S.Diag(D.getCommaLoc(), diag::note_empty_parens_function_call)
3075d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << FixItHint::CreateReplacement(D.getCommaLoc(), ";")
3076d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << D.getIdentifier();
3077d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    }
3078d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  }
3079b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
3080651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (FTI.NumParams > 0) {
3081c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    // For a declaration with parameters, eg. "T var(T());", suggest adding
3082c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    // parens around the first parameter to turn the declaration into a
3083c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    // variable declaration.
3084651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    SourceRange Range = FTI.Params[0].Param->getSourceRange();
3085b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation B = Range.getBegin();
30866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    SourceLocation E = S.getLocForEndOfToken(Range.getEnd());
3087b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // FIXME: Maybe we should suggest adding braces instead of parens
3088b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // in C++11 for classes that don't have an initializer_list constructor.
3089b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    S.Diag(B, diag::note_additional_parens_for_variable_declaration)
3090b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(B, "(")
3091b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(E, ")");
3092d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  } else {
3093c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    // For a declaration without parameters, eg. "T var();", suggest replacing
3094c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    // the parens with an initializer to turn the declaration into a variable
3095d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
3096b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
3097d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
3098b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // Empty parens mean value-initialization, and no parens mean
3099b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // default initialization. These are equivalent if the default
3100b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // constructor is user-provided or if zero-initialization is a
3101b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // no-op.
3102b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    if (RD && RD->hasDefinition() &&
3103b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        (RD->isEmpty() || RD->hasUserProvidedDefaultConstructor()))
3104b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor)
3105b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        << FixItHint::CreateRemoval(ParenRange);
3106b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    else {
31078adf837adc65b55a3f74643c02c1ee077dc26f06Richard Smith      std::string Init =
31088adf837adc65b55a3f74643c02c1ee077dc26f06Richard Smith          S.getFixItZeroInitializerForType(RT, ParenRange.getBegin());
310980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      if (Init.empty() && S.LangOpts.CPlusPlus11)
3110b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        Init = "{}";
3111b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (!Init.empty())
3112b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        S.Diag(DeclType.Loc, diag::note_empty_parens_zero_initialize)
3113b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith          << FixItHint::CreateReplacement(ParenRange, Init);
3114b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    }
3115b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  }
3116b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith}
3117b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
3118ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner/// Helper for figuring out the default CC for a function declarator type.  If
3119ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner/// this is the outermost chunk, then we can determine the CC from the
3120ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner/// declarator context.  If not, then this could be either a member function
3121ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner/// type or normal function type.
3122ef072033876e295ec5d3402f8730a3ae358ad815Reid Klecknerstatic CallingConv
3123ef072033876e295ec5d3402f8730a3ae358ad815Reid KlecknergetCCForDeclaratorChunk(Sema &S, Declarator &D,
3124ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner                        const DeclaratorChunk::FunctionTypeInfo &FTI,
3125ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner                        unsigned ChunkIndex) {
3126ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  assert(D.getTypeObject(ChunkIndex).Kind == DeclaratorChunk::Function);
3127ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
31284967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // Check for an explicit CC attribute.
31294967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  for (auto Attr = FTI.AttrList; Attr; Attr = Attr->getNext()) {
31304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    switch (Attr->getKind()) {
31314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    CALLING_CONV_ATTRS_CASELIST: {
31324967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      // Ignore attributes that don't validate or can't apply to the
31334967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      // function type.  We'll diagnose the failure to apply them in
31344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      // handleFunctionTypeAttr.
31354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      CallingConv CC;
31364967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (!S.CheckCallingConvAttr(*Attr, CC) &&
31374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          (!FTI.isVariadic || supportsVariadicCall(CC))) {
31384967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        return CC;
31394967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      }
31404967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      break;
31414967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
31424967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
31434967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    default:
31444967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      break;
31454967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
31464967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
31474967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
3148ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  bool IsCXXInstanceMethod = false;
3149ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
3150ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  if (S.getLangOpts().CPlusPlus) {
3151ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // Look inwards through parentheses to see if this chunk will form a
3152ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // member pointer type or if we're the declarator.  Any type attributes
3153ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // between here and there will override the CC we choose here.
3154ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    unsigned I = ChunkIndex;
3155ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    bool FoundNonParen = false;
3156ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    while (I && !FoundNonParen) {
3157ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      --I;
3158ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      if (D.getTypeObject(I).Kind != DeclaratorChunk::Paren)
3159ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        FoundNonParen = true;
3160ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    }
3161ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
3162ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    if (FoundNonParen) {
3163ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      // If we're not the declarator, we're a regular function type unless we're
3164ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      // in a member pointer.
3165ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      IsCXXInstanceMethod =
3166ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner          D.getTypeObject(I).Kind == DeclaratorChunk::MemberPointer;
316758878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar    } else if (D.getContext() == Declarator::LambdaExprContext) {
316858878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar      // This can only be a call operator for a lambda, which is an instance
316958878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar      // method.
317058878f85ab89b13e9eea4af3ccf055e42c557bc8Pirama Arumuga Nainar      IsCXXInstanceMethod = true;
3171ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    } else {
3172ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      // We're the innermost decl chunk, so must be a function declarator.
3173ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      assert(D.isFunctionDeclarator());
3174ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
3175ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      // If we're inside a record, we're declaring a method, but it could be
3176d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner      // explicitly or implicitly static.
3177ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      IsCXXInstanceMethod =
3178d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner          D.isFirstDeclarationOfMember() &&
3179d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner          D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
3180d1a32c328bce903fb1b17fc8147b646be818298eReid Kleckner          !D.isStaticMember();
3181ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    }
3182ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  }
3183ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
31840e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  CallingConv CC = S.Context.getDefaultCallingConvention(FTI.isVariadic,
31850e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                                         IsCXXInstanceMethod);
31860e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
31874967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // Attribute AT_OpenCLKernel affects the calling convention for SPIR
31884967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // and AMDGPU targets, hence it cannot be treated as a calling
31890e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // convention attribute. This is the simplest place to infer
31904967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // calling convention for OpenCL kernels.
31914967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if (S.getLangOpts().OpenCL) {
31920e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    for (const AttributeList *Attr = D.getDeclSpec().getAttributes().getList();
31930e2c34f92f00628d48968dfea096d36381f494cbStephen Hines         Attr; Attr = Attr->getNext()) {
31940e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (Attr->getKind() == AttributeList::AT_OpenCLKernel) {
31954967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        llvm::Triple::ArchType arch = S.Context.getTargetInfo().getTriple().getArch();
31964967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        if (arch == llvm::Triple::spir || arch == llvm::Triple::spir64 ||
31974967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar            arch == llvm::Triple::amdgcn) {
31984967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          CC = CC_OpenCLKernel;
31994967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        }
32000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        break;
32010e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      }
32020e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    }
32030e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
32040e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
32050e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  return CC;
3206ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner}
3207ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
320887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarnamespace {
320987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  /// A simple notion of pointer kinds, which matches up with the various
321087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  /// pointer declarators.
321187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  enum class SimplePointerKind {
321287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    Pointer,
321387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    BlockPointer,
321487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    MemberPointer,
321587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  };
32164967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} // end anonymous namespace
321787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
321887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga NainarIdentifierInfo *Sema::getNullabilityKeyword(NullabilityKind nullability) {
321987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  switch (nullability) {
322087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case NullabilityKind::NonNull:
322187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (!Ident__Nonnull)
322287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Ident__Nonnull = PP.getIdentifierInfo("_Nonnull");
322387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return Ident__Nonnull;
322487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
322587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case NullabilityKind::Nullable:
322687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (!Ident__Nullable)
322787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Ident__Nullable = PP.getIdentifierInfo("_Nullable");
322887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return Ident__Nullable;
322987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
323087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case NullabilityKind::Unspecified:
323187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (!Ident__Null_unspecified)
323287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Ident__Null_unspecified = PP.getIdentifierInfo("_Null_unspecified");
323387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return Ident__Null_unspecified;
323487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
323587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  llvm_unreachable("Unknown nullability kind.");
323687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
323787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
323887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// Retrieve the identifier "NSError".
323987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga NainarIdentifierInfo *Sema::getNSErrorIdent() {
324087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!Ident_NSError)
324187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    Ident_NSError = PP.getIdentifierInfo("NSError");
324287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
324387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  return Ident_NSError;
324487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
324587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
324687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// Check whether there is a nullability attribute of any kind in the given
324787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// attribute list.
324887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarstatic bool hasNullabilityAttr(const AttributeList *attrs) {
324987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  for (const AttributeList *attr = attrs; attr;
325087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar       attr = attr->getNext()) {
325187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (attr->getKind() == AttributeList::AT_TypeNonNull ||
325287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        attr->getKind() == AttributeList::AT_TypeNullable ||
325387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        attr->getKind() == AttributeList::AT_TypeNullUnspecified)
325487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return true;
325587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
325687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
325787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  return false;
325887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
325987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
326087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarnamespace {
326187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  /// Describes the kind of a pointer a declarator describes.
326287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  enum class PointerDeclaratorKind {
326387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Not a pointer.
326487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    NonPointer,
326587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Single-level pointer.
326687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    SingleLevelPointer,
326787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Multi-level pointer (of any pointer kind).
326887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    MultiLevelPointer,
326987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // CFFooRef*
327087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    MaybePointerToCFRef,
327187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // CFErrorRef*
327287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    CFErrorRefPointer,
327387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // NSError**
327487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    NSErrorPointerPointer,
327587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  };
32764967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} // end anonymous namespace
327787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
327887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// Classify the given declarator, whose type-specified is \c type, based on
327987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// what kind of pointer it refers to.
328087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar///
328187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// This is used to determine the default nullability.
328287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarstatic PointerDeclaratorKind classifyPointerDeclarator(Sema &S,
328387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                                       QualType type,
328487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                                       Declarator &declarator) {
328587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  unsigned numNormalPointers = 0;
328687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
328787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // For any dependent type, we consider it a non-pointer.
328887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (type->isDependentType())
328987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return PointerDeclaratorKind::NonPointer;
329087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
329187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Look through the declarator chunks to identify pointers.
329287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  for (unsigned i = 0, n = declarator.getNumTypeObjects(); i != n; ++i) {
329387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    DeclaratorChunk &chunk = declarator.getTypeObject(i);
329487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    switch (chunk.Kind) {
329587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::Array:
329687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::Function:
32974967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case DeclaratorChunk::Pipe:
329887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
329987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
330087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::BlockPointer:
330187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::MemberPointer:
330287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer
330387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   : PointerDeclaratorKind::SingleLevelPointer;
330487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
330587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::Paren:
330687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::Reference:
330787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      continue;
330887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
330987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::Pointer:
331087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      ++numNormalPointers;
331187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (numNormalPointers > 2)
331287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        return PointerDeclaratorKind::MultiLevelPointer;
331387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      continue;
331487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
331587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
331687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
331787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Then, dig into the type specifier itself.
331887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  unsigned numTypeSpecifierPointers = 0;
331987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  do {
332087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Decompose normal pointers.
332187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (auto ptrType = type->getAs<PointerType>()) {
332287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      ++numNormalPointers;
332387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
332487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (numNormalPointers > 2)
332587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        return PointerDeclaratorKind::MultiLevelPointer;
332687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
332787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      type = ptrType->getPointeeType();
332887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      ++numTypeSpecifierPointers;
332987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      continue;
333087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
333187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
333287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Decompose block pointers.
333387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (type->getAs<BlockPointerType>()) {
333487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer
333587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   : PointerDeclaratorKind::SingleLevelPointer;
333687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
333787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
333887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Decompose member pointers.
333987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (type->getAs<MemberPointerType>()) {
334087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer
334187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   : PointerDeclaratorKind::SingleLevelPointer;
334287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
334387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
334487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Look at Objective-C object pointers.
334587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (auto objcObjectPtr = type->getAs<ObjCObjectPointerType>()) {
334687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      ++numNormalPointers;
334787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      ++numTypeSpecifierPointers;
334887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
334987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // If this is NSError**, report that.
335087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (auto objcClassDecl = objcObjectPtr->getInterfaceDecl()) {
335187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (objcClassDecl->getIdentifier() == S.getNSErrorIdent() &&
335287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            numNormalPointers == 2 && numTypeSpecifierPointers < 2) {
335387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          return PointerDeclaratorKind::NSErrorPointerPointer;
335487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        }
335587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
335687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
335787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
335887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
335987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
336087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Look at Objective-C class types.
336187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (auto objcClass = type->getAs<ObjCInterfaceType>()) {
336287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (objcClass->getInterface()->getIdentifier() == S.getNSErrorIdent()) {
336387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (numNormalPointers == 2 && numTypeSpecifierPointers < 2)
336487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          return PointerDeclaratorKind::NSErrorPointerPointer;;
336587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
336687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
336787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
336887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
336987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
337087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // If at this point we haven't seen a pointer, we won't see one.
337187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (numNormalPointers == 0)
337287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return PointerDeclaratorKind::NonPointer;
337387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
337487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (auto recordType = type->getAs<RecordType>()) {
337587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      RecordDecl *recordDecl = recordType->getDecl();
337687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
337787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      bool isCFError = false;
337887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (S.CFError) {
337987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // If we already know about CFError, test it directly.
338087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        isCFError = (S.CFError == recordDecl);
338187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      } else {
338287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // Check whether this is CFError, which we identify based on its bridge
338387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // to NSError.
338487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (recordDecl->getTagKind() == TTK_Struct && numNormalPointers > 0) {
338587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          if (auto bridgeAttr = recordDecl->getAttr<ObjCBridgeAttr>()) {
338687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            if (bridgeAttr->getBridgedType() == S.getNSErrorIdent()) {
338787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              S.CFError = recordDecl;
338887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              isCFError = true;
338987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            }
339087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          }
339187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        }
339287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
339387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
339487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // If this is CFErrorRef*, report it as such.
339587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (isCFError && numNormalPointers == 2 && numTypeSpecifierPointers < 2) {
339687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        return PointerDeclaratorKind::CFErrorRefPointer;
339787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
339887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
339987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
340087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
340187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    break;
340287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  } while (true);
340387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
340487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  switch (numNormalPointers) {
340587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case 0:
340687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return PointerDeclaratorKind::NonPointer;
340787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
340887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case 1:
340987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return PointerDeclaratorKind::SingleLevelPointer;
341087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
341187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case 2:
341287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return PointerDeclaratorKind::MaybePointerToCFRef;
341387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
341487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  default:
341587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return PointerDeclaratorKind::MultiLevelPointer;
341687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
341787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
341887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
341987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarstatic FileID getNullabilityCompletenessCheckFileID(Sema &S,
342087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                                    SourceLocation loc) {
342187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // If we're anywhere in a function, method, or closure context, don't perform
342287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // completeness checks.
342387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  for (DeclContext *ctx = S.CurContext; ctx; ctx = ctx->getParent()) {
342487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (ctx->isFunctionOrMethod())
342587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return FileID();
342687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
342787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (ctx->isFileContext())
342887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
342987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
343087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
343187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // We only care about the expansion location.
343287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  loc = S.SourceMgr.getExpansionLoc(loc);
343387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  FileID file = S.SourceMgr.getFileID(loc);
343487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (file.isInvalid())
343587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return FileID();
343687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
343787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Retrieve file information.
343887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  bool invalid = false;
343987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  const SrcMgr::SLocEntry &sloc = S.SourceMgr.getSLocEntry(file, &invalid);
344087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (invalid || !sloc.isFile())
344187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return FileID();
344287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
344387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // We don't want to perform completeness checks on the main file or in
344487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // system headers.
344587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  const SrcMgr::FileInfo &fileInfo = sloc.getFile();
344687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (fileInfo.getIncludeLoc().isInvalid())
344787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return FileID();
344887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (fileInfo.getFileCharacteristic() != SrcMgr::C_User &&
344987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      S.Diags.getSuppressSystemWarnings()) {
345087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return FileID();
345187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
345287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
345387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  return file;
345487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
345587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
345687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// Check for consistent use of nullability.
345787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarstatic void checkNullabilityConsistency(TypeProcessingState &state,
345887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                        SimplePointerKind pointerKind,
345987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                        SourceLocation pointerLoc) {
346087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  Sema &S = state.getSema();
346187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
346287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Determine which file we're performing consistency checking for.
346387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  FileID file = getNullabilityCompletenessCheckFileID(S, pointerLoc);
346487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (file.isInvalid())
346587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return;
346687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
346787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // If we haven't seen any type nullability in this file, we won't warn now
346887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // about anything.
346987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  FileNullability &fileNullability = S.NullabilityMap[file];
347087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!fileNullability.SawTypeNullability) {
347187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // If this is the first pointer declarator in the file, record it.
347287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (fileNullability.PointerLoc.isInvalid() &&
347387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        !S.Context.getDiagnostics().isIgnored(diag::warn_nullability_missing,
347487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                              pointerLoc)) {
347587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      fileNullability.PointerLoc = pointerLoc;
347687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      fileNullability.PointerKind = static_cast<unsigned>(pointerKind);
347787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
347887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
347987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return;
348087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
348187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
348287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Complain about missing nullability.
348387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  S.Diag(pointerLoc, diag::warn_nullability_missing)
348487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    << static_cast<unsigned>(pointerKind);
348587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
348687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
34878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
34888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                QualType declSpecType,
34898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                TypeSourceInfo *TInfo) {
34900e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // The TypeSourceInfo that this function returns will not be a null type.
34910e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // If there is an error, this function will fill in a dummy type as fallback.
34928cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = declSpecType;
34938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
34948cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
34958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ASTContext &Context = S.Context;
34964e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &LangOpts = S.getLangOpts();
34978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
3498cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
3499cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
3500cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
3501cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
35021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
350387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Does this declaration declare a typedef-name?
350487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  bool IsTypedefName =
350587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef ||
350687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    D.getContext() == Declarator::AliasDeclContext ||
350787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    D.getContext() == Declarator::AliasTemplateContext;
350887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
350987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Does T refer to a function type with a cv-qualifier or a ref-qualifier?
351087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  bool IsQualifiedFunction = T->isFunctionProtoType() &&
351187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      (T->castAs<FunctionProtoType>()->getTypeQuals() != 0 ||
351287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar       T->castAs<FunctionProtoType>()->getRefQualifier() != RQ_None);
351387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
351487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // If T is 'decltype(auto)', the only declarators we can have are parens
351587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // and at most one function declarator if this is a function declaration.
351687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (const AutoType *AT = T->getAs<AutoType>()) {
351787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (AT->isDecltypeAuto()) {
351887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) {
351987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        unsigned Index = E - I - 1;
352087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        DeclaratorChunk &DeclChunk = D.getTypeObject(Index);
352187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        unsigned DiagId = diag::err_decltype_auto_compound_type;
352287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        unsigned DiagKind = 0;
352387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        switch (DeclChunk.Kind) {
352487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        case DeclaratorChunk::Paren:
352587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          continue;
352687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        case DeclaratorChunk::Function: {
352787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          unsigned FnIndex;
352887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          if (D.isFunctionDeclarationContext() &&
352987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              D.isFunctionDeclarator(FnIndex) && FnIndex == Index)
353087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            continue;
353187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          DiagId = diag::err_decltype_auto_function_declarator_not_declaration;
353287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          break;
353387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        }
353487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        case DeclaratorChunk::Pointer:
353587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        case DeclaratorChunk::BlockPointer:
353687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        case DeclaratorChunk::MemberPointer:
353787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          DiagKind = 0;
353887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          break;
353987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        case DeclaratorChunk::Reference:
354087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          DiagKind = 1;
354187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          break;
354287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        case DeclaratorChunk::Array:
354387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          DiagKind = 2;
354487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          break;
35454967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        case DeclaratorChunk::Pipe:
35464967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          break;
354787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        }
354887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
354987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        S.Diag(DeclChunk.Loc, DiagId) << DiagKind;
355087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        D.setInvalidType(true);
355187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        break;
355287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
355387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
355487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
355587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
355687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Determine whether we should infer _Nonnull on pointer types.
355787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  Optional<NullabilityKind> inferNullability;
355887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  bool inferNullabilityCS = false;
355987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  bool inferNullabilityInnerOnly = false;
356087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  bool inferNullabilityInnerOnlyComplete = false;
356187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
356287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Are we in an assume-nonnull region?
356387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  bool inAssumeNonNullRegion = false;
356487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (S.PP.getPragmaAssumeNonNullLoc().isValid()) {
356587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    inAssumeNonNullRegion = true;
356687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Determine which file we saw the assume-nonnull region in.
356787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    FileID file = getNullabilityCompletenessCheckFileID(
356887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                    S, S.PP.getPragmaAssumeNonNullLoc());
356987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (file.isValid()) {
357087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      FileNullability &fileNullability = S.NullabilityMap[file];
357187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
357287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // If we haven't seen any type nullability before, now we have.
357387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (!fileNullability.SawTypeNullability) {
357487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (fileNullability.PointerLoc.isValid()) {
357587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          S.Diag(fileNullability.PointerLoc, diag::warn_nullability_missing)
357687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            << static_cast<unsigned>(fileNullability.PointerKind);
357787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        }
357887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
357987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        fileNullability.SawTypeNullability = true;
358087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
358187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
358287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
358387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
358487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Whether to complain about missing nullability specifiers or not.
358587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  enum {
358687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    /// Never complain.
358787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    CAMN_No,
358887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    /// Complain on the inner pointers (but not the outermost
358987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    /// pointer).
359087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    CAMN_InnerPointers,
359187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    /// Complain about any pointers that don't have nullability
359287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    /// specified or inferred.
359387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    CAMN_Yes
359487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  } complainAboutMissingNullability = CAMN_No;
359587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  unsigned NumPointersRemaining = 0;
359687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
359787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (IsTypedefName) {
359887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // For typedefs, we do not infer any nullability (the default),
359987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // and we only complain about missing nullability specifiers on
360087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // inner pointers.
360187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    complainAboutMissingNullability = CAMN_InnerPointers;
360287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
360387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (T->canHaveNullability() && !T->getNullability(S.Context)) {
360487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      ++NumPointersRemaining;
360587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
360687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
360787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    for (unsigned i = 0, n = D.getNumTypeObjects(); i != n; ++i) {
360887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      DeclaratorChunk &chunk = D.getTypeObject(i);
360987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      switch (chunk.Kind) {
361087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case DeclaratorChunk::Array:
361187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case DeclaratorChunk::Function:
36124967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      case DeclaratorChunk::Pipe:
361387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        break;
361487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
361587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case DeclaratorChunk::BlockPointer:
361687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case DeclaratorChunk::MemberPointer:
361787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        ++NumPointersRemaining;
361887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        break;
361987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
362087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case DeclaratorChunk::Paren:
362187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case DeclaratorChunk::Reference:
362287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        continue;
362387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
362487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case DeclaratorChunk::Pointer:
362587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        ++NumPointersRemaining;
362687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        continue;
362787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
362887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
362987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  } else {
363087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    bool isFunctionOrMethod = false;
363187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    switch (auto context = state.getDeclarator().getContext()) {
363287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::ObjCParameterContext:
363387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::ObjCResultContext:
363487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::PrototypeContext:
363587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::TrailingReturnContext:
363687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      isFunctionOrMethod = true;
363787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // fallthrough
363887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
363987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::MemberContext:
364087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (state.getDeclarator().isObjCIvar() && !isFunctionOrMethod) {
364187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        complainAboutMissingNullability = CAMN_No;
364287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        break;
364387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
364487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
364587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Weak properties are inferred to be nullable.
364687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (state.getDeclarator().isObjCWeakProperty() && inAssumeNonNullRegion) {
364787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        inferNullability = NullabilityKind::Nullable;
364887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        break;
364987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
365087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
365187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // fallthrough
365287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
365387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::FileContext:
365487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::KNRTypeListContext:
365587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      complainAboutMissingNullability = CAMN_Yes;
365687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
365787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Nullability inference depends on the type and declarator.
365887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      switch (classifyPointerDeclarator(S, T, D)) {
365987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case PointerDeclaratorKind::NonPointer:
366087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case PointerDeclaratorKind::MultiLevelPointer:
366187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // Cannot infer nullability.
366287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        break;
366387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
366487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case PointerDeclaratorKind::SingleLevelPointer:
366587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // Infer _Nonnull if we are in an assumes-nonnull region.
366687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (inAssumeNonNullRegion) {
366787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          inferNullability = NullabilityKind::NonNull;
366887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          inferNullabilityCS = (context == Declarator::ObjCParameterContext ||
366987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                context == Declarator::ObjCResultContext);
367087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        }
367187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        break;
367287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
367387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case PointerDeclaratorKind::CFErrorRefPointer:
367487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case PointerDeclaratorKind::NSErrorPointerPointer:
367587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // Within a function or method signature, infer _Nullable at both
367687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // levels.
367787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (isFunctionOrMethod && inAssumeNonNullRegion)
367887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          inferNullability = NullabilityKind::Nullable;
367987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        break;
368087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
368187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case PointerDeclaratorKind::MaybePointerToCFRef:
368287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (isFunctionOrMethod) {
368387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          // On pointer-to-pointer parameters marked cf_returns_retained or
368487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          // cf_returns_not_retained, if the outer pointer is explicit then
368587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          // infer the inner pointer as _Nullable.
368687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          auto hasCFReturnsAttr = [](const AttributeList *NextAttr) -> bool {
368787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            while (NextAttr) {
368887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              if (NextAttr->getKind() == AttributeList::AT_CFReturnsRetained ||
368987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                  NextAttr->getKind() == AttributeList::AT_CFReturnsNotRetained)
369087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                return true;
369187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              NextAttr = NextAttr->getNext();
369287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            }
369387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            return false;
369487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          };
369587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          if (const auto *InnermostChunk = D.getInnermostNonParenChunk()) {
369687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            if (hasCFReturnsAttr(D.getAttributes()) ||
369787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                hasCFReturnsAttr(InnermostChunk->getAttrs()) ||
369887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                hasCFReturnsAttr(D.getDeclSpec().getAttributes().getList())) {
369987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              inferNullability = NullabilityKind::Nullable;
370087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              inferNullabilityInnerOnly = true;
370187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            }
370287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          }
370387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        }
370487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        break;
370587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
370687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
370787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
370887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::ConversionIdContext:
370987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      complainAboutMissingNullability = CAMN_Yes;
371087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
371187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
371287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::AliasDeclContext:
371387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::AliasTemplateContext:
371487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::BlockContext:
371587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::BlockLiteralContext:
371687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::ConditionContext:
371787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::CXXCatchContext:
371887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::CXXNewContext:
371987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::ForContext:
37204967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case Declarator::InitStmtContext:
372187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::LambdaExprContext:
372287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::LambdaExprParameterContext:
372387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::ObjCCatchContext:
372487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::TemplateParamContext:
372587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::TemplateTypeArgContext:
372687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case Declarator::TypeNameContext:
372787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Don't infer in these contexts.
372887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
372987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
373087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
373187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
373287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Local function that checks the nullability for a given pointer declarator.
373387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Returns true if _Nonnull was inferred.
373487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  auto inferPointerNullability = [&](SimplePointerKind pointerKind,
373587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                     SourceLocation pointerLoc,
373687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                     AttributeList *&attrs) -> AttributeList * {
373787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // We've seen a pointer.
373887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (NumPointersRemaining > 0)
373987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      --NumPointersRemaining;
374087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
374187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // If a nullability attribute is present, there's nothing to do.
374287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (hasNullabilityAttr(attrs))
374387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return nullptr;
374487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
374587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // If we're supposed to infer nullability, do so now.
374687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (inferNullability && !inferNullabilityInnerOnlyComplete) {
374787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      AttributeList::Syntax syntax
374887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        = inferNullabilityCS ? AttributeList::AS_ContextSensitiveKeyword
374987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                             : AttributeList::AS_Keyword;
375087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      AttributeList *nullabilityAttr = state.getDeclarator().getAttributePool()
375187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                         .create(
375287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                           S.getNullabilityKeyword(
375387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                             *inferNullability),
375487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                           SourceRange(pointerLoc),
375587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                           nullptr, SourceLocation(),
375687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                           nullptr, 0, syntax);
375787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
375887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      spliceAttrIntoList(*nullabilityAttr, attrs);
375987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
376087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (inferNullabilityCS) {
376187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        state.getDeclarator().getMutableDeclSpec().getObjCQualifiers()
376287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          ->setObjCDeclQualifier(ObjCDeclSpec::DQ_CSNullability);
376387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
3764162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
376587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (inferNullabilityInnerOnly)
376687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        inferNullabilityInnerOnlyComplete = true;
376787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return nullabilityAttr;
376887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
3769d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
377087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // If we're supposed to complain about missing nullability, do so
377187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // now if it's truly missing.
377287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    switch (complainAboutMissingNullability) {
377387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case CAMN_No:
377487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
3775a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
377687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case CAMN_InnerPointers:
377787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (NumPointersRemaining == 0)
3778a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        break;
377987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Fallthrough.
378087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
378187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case CAMN_Yes:
378287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      checkNullabilityConsistency(state, pointerKind, pointerLoc);
378387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
378487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return nullptr;
378587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  };
378687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
378787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // If the type itself could have nullability but does not, infer pointer
378887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // nullability and perform consistency checking.
378987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (T->canHaveNullability() && S.ActiveTemplateInstantiations.empty() &&
379087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      !T->getNullability(S.Context)) {
379187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    SimplePointerKind pointerKind = SimplePointerKind::Pointer;
379287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (T->isBlockPointerType())
379387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      pointerKind = SimplePointerKind::BlockPointer;
379487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    else if (T->isMemberPointerType())
379587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      pointerKind = SimplePointerKind::MemberPointer;
379687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
379787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (auto *attr = inferPointerNullability(
379887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                       pointerKind, D.getDeclSpec().getTypeSpecTypeLoc(),
379987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                       D.getMutableDeclSpec().getAttributes().getListRef())) {
380087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      T = Context.getAttributedType(
380187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            AttributedType::getNullabilityAttrKind(*inferNullability), T, T);
380287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      attr->setUsedAsTypeAttr();
3803a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    }
3804a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  }
3805a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
380698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
380798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
380898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
38098ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
3810711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
3811711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
3812711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
38136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    IsQualifiedFunction &= DeclType.Kind == DeclaratorChunk::Paren;
38145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
3815075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
38168cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildParenType(T);
3817075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
38185618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
38199af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
38209af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
38214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        S.Diag(DeclType.Loc, diag::err_blocks_disable) << LangOpts.OpenCL;
38221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
382387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Handle pointer nullability.
382487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      inferPointerNullability(SimplePointerKind::BlockPointer,
382587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                              DeclType.Loc, DeclType.getAttrListRef());
382687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
38278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
38284967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (DeclType.Cls.TypeQuals || LangOpts.OpenCL) {
38294967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        // OpenCL v2.0, s6.12.5 - Block variable declarations are implicitly
38304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        // qualified with const.
38314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        if (LangOpts.OpenCL)
38324967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          DeclType.Cls.TypeQuals |= DeclSpec::TQ_const;
38338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
38344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      }
38355618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
38365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
38376a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
38386a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
38398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
38408cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
38416a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
38426a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
38436a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
384487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
384587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Handle pointer nullability
384687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      inferPointerNullability(SimplePointerKind::Pointer, DeclType.Loc,
384787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                              DeclType.getAttrListRef());
384887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
38498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.ObjC1 && T->getAs<ObjCObjectType>()) {
3850c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
38512865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
38528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
385314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
385414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
38554967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
38564967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      // OpenCL v2.0 s6.9b - Pointer to image/sampler cannot be used.
38574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      // OpenCL v2.0 s6.13.16.1 - Pointer to pipe cannot be used.
38584967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      // OpenCL v2.0 s6.12.5 - Pointers to Blocks are not allowed.
38594967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (LangOpts.OpenCL) {
38604967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        if (T->isImageType() || T->isSamplerT() || T->isPipeType() ||
38614967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar            T->isBlockPointerType()) {
38624967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          S.Diag(D.getIdentifierLoc(), diag::err_opencl_pointer_to_type) << T;
38634967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          D.setInvalidType(true);
38644967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        }
38654967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      }
38664967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
38678cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildPointerType(T, DeclType.Loc, Name);
38682865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
38698cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
38705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
38710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
38726a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
38736a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
38748cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
38758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
38766a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
38776a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
38786a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
38798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
38802865474261a608c7873b87ba4af110d17907896dJohn McCall
38812865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
38828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
38835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
38840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
38855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
38866a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
38876a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
38888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
38898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
38906a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
38916a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
38926a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
3893fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
389494f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
38955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
38965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
38975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
38985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
38995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
39005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
39015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
3902c05a94b7accd4035bf5d5897c434c445b22da855John McCall      if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
3903f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
3904f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
3905f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
39068cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
3907f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
3908f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
3909f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
39107f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
39117f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // C99 6.7.5.2p1: The optional type qualifiers and the keyword static
39127f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // shall appear only in a declaration of a function parameter with an
39137f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // array type, ...
39147f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      if (ASM == ArrayType::Static || ATI.TypeQuals) {
391599570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay        if (!(D.isPrototypeContext() ||
391699570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay              D.getContext() == Declarator::KNRTypeListContext)) {
39177f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          S.Diag(DeclType.Loc, diag::err_array_static_outside_prototype) <<
39187f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
39197f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Remove the 'static' and the type qualifiers.
39207f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          if (ASM == ArrayType::Static)
39217f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ASM = ArrayType::Normal;
39227f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          ATI.TypeQuals = 0;
39237f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          D.setInvalidType(true);
39247f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
39257f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
39267f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // C99 6.7.5.2p1: ... and then only in the outermost array type
39277f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // derivation.
39287f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        unsigned x = chunkIndex;
39297f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        while (x != 0) {
39307f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Walk outwards along the declarator chunks.
39317f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          x--;
39327f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          const DeclaratorChunk &DC = D.getTypeObject(x);
39337f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          switch (DC.Kind) {
39347f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Paren:
39357f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            continue;
39367f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Array:
39377f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Pointer:
39387f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Reference:
39397f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::MemberPointer:
39407f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            S.Diag(DeclType.Loc, diag::err_array_static_not_outermost) <<
39417f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
39427f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            if (ASM == ArrayType::Static)
39437f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              ASM = ArrayType::Normal;
39447f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ATI.TypeQuals = 0;
39457f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            D.setInvalidType(true);
39467f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
39477f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Function:
39487f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::BlockPointer:
39494967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          case DeclaratorChunk::Pipe:
39507f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            // These are invalid anyway, so just ignore.
39517f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
39527f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          }
39537f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
39547f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      }
3955fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      const AutoType *AT = T->getContainedAutoType();
3956fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // Allow arrays of auto if we are a generic lambda parameter.
3957fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      // i.e. [](auto (&array)[5]) { return array[0]; }; OK
3958fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      if (AT && D.getContext() != Declarator::LambdaExprParameterContext) {
3959a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        // We've already diagnosed this for decltype(auto).
3960dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        if (!AT->isDecltypeAuto())
3961a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          S.Diag(DeclType.Loc, diag::err_illegal_decl_array_of_auto)
3962a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            << getPrintableNameForEntity(Name) << T;
3963dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        T = QualType();
3964dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        break;
3965a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      }
3966a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
39678ac2c66a1442985091c5ec2b33ce6d3df3bcb529Eli Friedman      T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
39688cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                           SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
39695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
39705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
3971f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
39725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
39735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
39745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
39755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
3976d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = FTI.TypeQuals || FTI.hasRefQualifier();
39773cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
397834b41d939a1328f484511c6002ba2456db879a29Richard Smith      // Check for auto functions and trailing return type and adjust the
397934b41d939a1328f484511c6002ba2456db879a29Richard Smith      // return type accordingly.
398034b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!D.isInvalidType()) {
398134b41d939a1328f484511c6002ba2456db879a29Richard Smith        // trailing-return-type is only required if we're declaring a function,
398234b41d939a1328f484511c6002ba2456db879a29Richard Smith        // and not, for instance, a pointer to a function.
3983651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        if (D.getDeclSpec().containsPlaceholderType() &&
398460e141e1f87211ca831de6821003d80fe20a06f3Richard Smith            !FTI.hasTrailingReturnType() && chunkIndex == 0 &&
3985176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines            !S.getLangOpts().CPlusPlus14) {
39868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
3987651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto
3988651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                     ? diag::err_auto_missing_trailing_return
3989651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                     : diag::err_deduced_return_type);
399034b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = Context.IntTy;
399134b41d939a1328f484511c6002ba2456db879a29Richard Smith          D.setInvalidType(true);
399254655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        } else if (FTI.hasTrailingReturnType()) {
3993e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          // T must be exactly 'auto' at this point. See CWG issue 681.
3994e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (isa<ParenType>(T)) {
39958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
3996e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith                 diag::err_trailing_return_in_parens)
3997e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith              << T << D.getDeclSpec().getSourceRange();
3998e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.setInvalidType(true);
3999f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman          } else if (D.getContext() != Declarator::LambdaExprContext &&
4000a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                     (T.hasQualifiers() || !isa<AutoType>(T) ||
400187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                      cast<AutoType>(T)->getKeyword() != AutoTypeKeyword::Auto)) {
40028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
400334b41d939a1328f484511c6002ba2456db879a29Richard Smith                 diag::err_trailing_return_without_auto)
400434b41d939a1328f484511c6002ba2456db879a29Richard Smith              << T << D.getDeclSpec().getSourceRange();
400534b41d939a1328f484511c6002ba2456db879a29Richard Smith            D.setInvalidType(true);
400634b41d939a1328f484511c6002ba2456db879a29Richard Smith          }
400754655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          T = S.GetTypeFromParser(FTI.getTrailingReturnType(), &TInfo);
400854655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (T.isNull()) {
400954655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            // An error occurred parsing the trailing return type.
401054655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            T = Context.IntTy;
401154655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            D.setInvalidType(true);
401254655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          }
401334b41d939a1328f484511c6002ba2456db879a29Richard Smith        }
401434b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
401534b41d939a1328f484511c6002ba2456db879a29Richard Smith
4016e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // C99 6.7.5.3p1: The return type may not be a function or array type.
4017e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // For conversion functions, we'll diagnose this particular error later.
4018e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      if ((T->isArrayType() || T->isFunctionType()) &&
4019e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
4020e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned diagID = diag::err_func_returning_array_function;
4021e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // Last processing chunk in block context means this function chunk
4022e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // represents the block.
4023e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (chunkIndex == 0 &&
4024e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.getContext() == Declarator::BlockLiteralContext)
4025e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          diagID = diag::err_block_returning_array_function;
40268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
4027e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        T = Context.IntTy;
4028e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        D.setInvalidType(true);
4029e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
4030e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
4031aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Do not allow returning half FP value.
4032aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // FIXME: This really should be in BuildFunctionType.
4033aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      if (T->isHalfType()) {
403419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        if (S.getLangOpts().OpenCL) {
403519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          if (!S.getOpenCLOptions().cl_khr_fp16) {
40364967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar            S.Diag(D.getIdentifierLoc(), diag::err_opencl_invalid_return)
40374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                << T << 0 /*pointer hint*/;
403819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            D.setInvalidType(true);
403919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          }
4040176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        } else if (!S.getLangOpts().HalfArgsAndReturns) {
404119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          S.Diag(D.getIdentifierLoc(),
404219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            diag::err_parameters_retval_cannot_have_fp16_type) << 1;
404319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          D.setInvalidType(true);
404419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        }
4045aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      }
4046aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
40474967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        // OpenCL v2.0 s6.12.5 - A block cannot be the return value of a
40484967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        // function.
40494967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (LangOpts.OpenCL && (T->isBlockPointerType() || T->isImageType() ||
40504967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                              T->isSamplerT() || T->isPipeType())) {
40514967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        S.Diag(D.getIdentifierLoc(), diag::err_opencl_invalid_return)
40524967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar            << T << 1 /*hint off*/;
40534967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        D.setInvalidType(true);
40544967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      }
40554967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
4056ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      // Methods cannot return interface types. All ObjC objects are
4057ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      // passed by reference.
4058ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      if (T->isObjCObjectType()) {
4059ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        SourceLocation DiagLoc, FixitLoc;
4060ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        if (TInfo) {
4061ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          DiagLoc = TInfo->getTypeLoc().getLocStart();
40626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          FixitLoc = S.getLocForEndOfToken(TInfo->getTypeLoc().getLocEnd());
4063ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        } else {
4064ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          DiagLoc = D.getDeclSpec().getTypeSpecTypeLoc();
40656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          FixitLoc = S.getLocForEndOfToken(D.getDeclSpec().getLocEnd());
4066ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        }
4067ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        S.Diag(DiagLoc, diag::err_object_cannot_be_passed_returned_by_value)
4068ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          << 0 << T
4069ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          << FixItHint::CreateInsertion(FixitLoc, "*");
4070ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
4071ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        T = Context.getObjCObjectPointerType(T);
4072ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        if (TInfo) {
4073ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TypeLocBuilder TLB;
4074ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TLB.pushFullCopy(TInfo->getTypeLoc());
4075ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          ObjCObjectPointerTypeLoc TLoc = TLB.push<ObjCObjectPointerTypeLoc>(T);
4076ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TLoc.setStarLoc(FixitLoc);
4077ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman          TInfo = TLB.getTypeSourceInfo(Context, T);
4078ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        }
4079ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
4080ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman        D.setInvalidType(true);
4081ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman      }
4082ddb5a3926d715ab4354ca36117679e3f4d5d3e21Eli Friedman
40835291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
40845291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
40858c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if ((T.getCVRQualifiers() || T->isAtomicType()) &&
40868c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          !(S.getLangOpts().CPlusPlus &&
40870e2c34f92f00628d48968dfea096d36381f494cbStephen Hines            (T->isDependentType() || T->isRecordType()))) {
4088b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar        if (T->isVoidType() && !S.getLangOpts().CPlusPlus &&
4089b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar            D.getFunctionDefinitionKind() == FDK_Definition) {
4090b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          // [6.9.1/3] qualified void return is invalid on a C
4091b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          // function definition.  Apparently ok on declarations and
4092b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          // in C++ though (!)
4093b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          S.Diag(DeclType.Loc, diag::err_func_returning_qualified_void) << T;
4094b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar        } else
4095b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar          diagnoseRedundantReturnTypeQualifiers(S, T, D, chunkIndex);
40960e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      }
4097d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
409802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // Objective-C ARC ownership qualifiers are ignored on the function
409902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // return type (by type canonicalization). Complain if this attribute
410002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // was written here.
410102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      if (T.getQualifiers().hasObjCLifetime()) {
410202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        SourceLocation AttrLoc;
410302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (chunkIndex + 1 < D.getNumTypeObjects()) {
410402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
410502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr = ReturnTypeChunk.getAttrs();
410602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
410702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
410802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
410902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
411002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
411102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
411202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
411302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isInvalid()) {
411402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr
411502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor                 = D.getDeclSpec().getAttributes().getList();
411602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
411702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
411802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
411902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
412002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
412102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
412202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
412302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
412402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isValid()) {
412502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // The ownership attributes are almost always written via
412602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // the predefined
412702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // __strong/__weak/__autoreleasing/__unsafe_unretained.
412802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          if (AttrLoc.isMacroID())
412902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            AttrLoc = S.SourceMgr.getImmediateExpansionRange(AttrLoc).first;
413002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
413102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          S.Diag(AttrLoc, diag::warn_arc_lifetime_result_type)
413202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            << T.getQualifiers().getObjCLifetime();
413302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
413402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      }
413502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
4136f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling      if (LangOpts.CPlusPlus && D.getDeclSpec().hasTagDefinition()) {
4137402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
4138402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
4139b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
4140f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling        S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
4141f0cc19f43d5e05dbd22d00faca8c093b7005be3fBill Wendling          << Context.getTypeDeclType(Tag);
4142402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
4143402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
41443cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
41453cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
4146162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (IsTypedefName && FTI.getExceptionSpecType())
414787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        S.Diag(FTI.getExceptionSpecLocBeg(),
414887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar               diag::err_exception_spec_in_typedef)
414987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            << (D.getContext() == Declarator::AliasDeclContext ||
415087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                D.getContext() == Declarator::AliasTemplateContext);
41513cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
4152b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // If we see "T var();" or "T var(T());" at block scope, it is probably
4153b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // an attempt to initialize a variable, not a function declaration.
4154b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (FTI.isAmbiguous)
4155b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        warnAboutAmbiguousFunction(S, D, DeclType, T);
4156b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
4157ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      FunctionType::ExtInfo EI(getCCForDeclaratorChunk(S, D, FTI, chunkIndex));
4158ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
4159651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (!FTI.NumParams && !FTI.isVariadic && !LangOpts.CPlusPlus) {
41602865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
4161ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        T = Context.getFunctionNoProtoType(T, EI);
41622865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
41632865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
41642865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
41652865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
4166651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        if (!FTI.NumParams && FTI.isVariadic && !LangOpts.CPlusPlus) {
4167965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
4168965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
4169965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
41708e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt            if (Attrs->getKind() == AttributeList::AT_Overloadable) {
4171965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
4172965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
4173965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
4174965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
4175965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
4176965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
41776bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_param);
4178c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
41792865474261a608c7873b87ba4af110d17907896dJohn McCall
41806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (FTI.NumParams && FTI.Params[0].Param == nullptr) {
4181788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
4182788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
4183651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          S.Diag(FTI.Params[0].IdentLoc,
4184651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                 diag::err_ident_list_in_fn_declaration);
41852865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
4186d5668a2447c2afeea38815b51a80a5a6ac235599Argyrios Kyrtzidis          // Recover by creating a K&R-style function type.
4187ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner          T = Context.getFunctionNoProtoType(T, EI);
41882865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
41892865474261a608c7873b87ba4af110d17907896dJohn McCall        }
41902865474261a608c7873b87ba4af110d17907896dJohn McCall
4191e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
4192ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        EPI.ExtInfo = EI;
4193e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
419454655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        EPI.HasTrailingReturn = FTI.hasTrailingReturnType();
4195e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
4196c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
4197c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : FTI.RefQualifierIsLValueRef? RQ_LValue
4198c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : RQ_RValue;
419991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
42006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        // Otherwise, we have a function with a parameter list that is
42015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
42026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        SmallVector<QualType, 16> ParamTys;
42036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        ParamTys.reserve(FTI.NumParams);
42041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42054967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        SmallVector<FunctionProtoType::ExtParameterInfo, 16>
42064967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          ExtParameterInfos(FTI.NumParams);
42074967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        bool HasAnyInterestingExtParameterInfos = false;
4208f85e193739c953358c865005855253af4f68a497John McCall
4209651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        for (unsigned i = 0, e = FTI.NumParams; i != e; ++i) {
4210651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
42116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          QualType ParamTy = Param->getType();
42126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          assert(!ParamTy.isNull() && "Couldn't parse type?");
42132dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
42146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          // Look for 'void'.  void is allowed only as a single parameter to a
42155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
42166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          // int(void) as a FunctionProtoType with an empty parameter list.
42176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          if (ParamTy->isVoidType()) {
42185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
42195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
42206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            // have parameters of incomplete type.
4221651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines            if (FTI.NumParams != 1 || FTI.isVariadic) {
42228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(DeclType.Loc, diag::err_void_only_param);
42236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              ParamTy = Context.IntTy;
42246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              Param->setType(ParamTy);
4225651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines            } else if (FTI.Params[i].Ident) {
42262ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
4227651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines              S.Diag(FTI.Params[i].IdentLoc, diag::err_param_with_void_type);
42286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              ParamTy = Context.IntTy;
42296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              Param->setType(ParamTy);
42302ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
42312ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
42326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              if (ParamTy.hasQualifiers())
42338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                S.Diag(DeclType.Loc, diag::err_void_param_qualified);
42341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              // Do not add 'void' to the list.
42362ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
42372ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
42386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          } else if (ParamTy->isHalfType()) {
42396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            // Disallow half FP parameters.
4240aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // FIXME: This really should be in BuildFunctionType.
424119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            if (S.getLangOpts().OpenCL) {
424219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              if (!S.getOpenCLOptions().cl_khr_fp16) {
424319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                S.Diag(Param->getLocation(),
42446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                  diag::err_opencl_half_param) << ParamTy;
424519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                D.setInvalidType();
42469dd74c5504c743c96ea3a1d691d6a75ec3a98147John McCall                Param->setInvalidDecl();
424719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              }
4248176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines            } else if (!S.getLangOpts().HalfArgsAndReturns) {
424919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              S.Diag(Param->getLocation(),
425019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                diag::err_parameters_retval_cannot_have_fp16_type) << 0;
425119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              D.setInvalidType();
425219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            }
4253eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
42546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            if (ParamTy->isPromotableIntegerType()) {
42556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              ParamTy = Context.getPromotedIntegerType(ParamTy);
4256eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              Param->setKNRPromoted(true);
42576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            } else if (const BuiltinType* BTy = ParamTy->getAs<BuiltinType>()) {
4258eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              if (BTy->getKind() == BuiltinType::Float) {
42596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                ParamTy = Context.DoubleTy;
4260eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall                Param->setKNRPromoted(true);
4261eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              }
4262eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
42635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
426456a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
42654967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          if (LangOpts.ObjCAutoRefCount && Param->hasAttr<NSConsumedAttr>()) {
42664967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar            ExtParameterInfos[i] = ExtParameterInfos[i].withIsConsumed(true);
42674967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar            HasAnyInterestingExtParameterInfos = true;
42684967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          }
42694967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
42704967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          if (auto attr = Param->getAttr<ParameterABIAttr>()) {
42714967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar            ExtParameterInfos[i] =
42724967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar              ExtParameterInfos[i].withABI(attr->getABI());
42734967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar            HasAnyInterestingExtParameterInfos = true;
4274f85e193739c953358c865005855253af4f68a497John McCall          }
4275f85e193739c953358c865005855253af4f68a497John McCall
42766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          ParamTys.push_back(ParamTy);
42775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
4278465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
42794967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        if (HasAnyInterestingExtParameterInfos) {
42804967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          EPI.ExtParameterInfos = ExtParameterInfos.data();
42814967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          checkExtParameterInfos(S, ParamTys, EPI,
42824967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar              [&](unsigned i) { return FTI.Params[i].Param->getLocation(); });
42834967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        }
4284f85e193739c953358c865005855253af4f68a497John McCall
42855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 4> Exceptions;
428674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<ParsedType, 2> DynamicExceptions;
428774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<SourceRange, 2> DynamicExceptionRanges;
42886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        Expr *NoexceptExpr = nullptr;
428991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
42908b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        if (FTI.getExceptionSpecType() == EST_Dynamic) {
429174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // FIXME: It's rather inefficient to have to split into two vectors
429274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // here.
429374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          unsigned N = FTI.NumExceptions;
429474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptions.reserve(N);
429574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptionRanges.reserve(N);
429674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          for (unsigned I = 0; I != N; ++I) {
429774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptions.push_back(FTI.Exceptions[I].Ty);
429874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptionRanges.push_back(FTI.Exceptions[I].Range);
4299e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
43008b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
430174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          NoexceptExpr = FTI.NoexceptExpr;
430274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        }
430391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4304176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        S.checkExceptionSpecification(D.isFunctionDeclarationContext(),
4305176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                      FTI.getExceptionSpecType(),
430674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptions,
430774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptionRanges,
430874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      NoexceptExpr,
430974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      Exceptions,
4310176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                      EPI.ExceptionSpec);
431191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
43126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        T = Context.getFunctionType(T, ParamTys, EPI);
43135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
43145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
43155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
43164967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case DeclaratorChunk::MemberPointer: {
4317f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
43187bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
4319f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
432087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
432187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Handle pointer nullability.
432287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      inferPointerNullability(SimplePointerKind::MemberPointer,
432387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                              DeclType.Loc, DeclType.getAttrListRef());
432487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
43257bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
4326edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
4327edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
43288cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      } else if (S.isDependentScopeSpecifier(SS) ||
43298cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                 dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) {
4330651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        NestedNameSpecifier *NNS = SS.getScopeRep();
433187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
433287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
433387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
43347bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
43354a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
433687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
433787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
433887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
433914aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor        case NestedNameSpecifier::NamespaceAlias:
434087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
4341176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        case NestedNameSpecifier::Super:
43429f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
43437bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
434487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
434587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
434687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
434791ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Note: if the NNS has a prefix and ClsType is a nondependent
434891ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // TemplateSpecializationType, then the NNS prefix is NOT included
434991ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // in ClsType; hence we wrap ClsType into an ElaboratedType.
435091ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // NOTE: in particular, no wrap occurs if ClsType already is an
435191ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Elaborated, DependentName, or DependentTemplateSpecialization.
435291ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
43537bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
435487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
435587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
4356f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
43578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Mem.Scope().getBeginLoc(),
4358949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
4359949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
4360949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
4361f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
4362f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
4363f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
4364949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
43650e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc,
43660e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                     D.getIdentifier());
4367949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
4368f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
4369949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
43702865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
43718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
4372f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
4373f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
4374f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
4375f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
43764967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case DeclaratorChunk::Pipe: {
43774967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      T = S.BuildPipeType(T, DeclType.Loc );
43784967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      break;
43794967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
43804967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
43814967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
4382cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
4383cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
4384cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
4385cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
4386cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
4387c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
438887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    processTypeAttrs(state, T, TAL_DeclChunk,
438987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                     const_cast<AttributeList *>(DeclType.getAttrs()));
43905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4391971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
43920e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  assert(!T.isNull() && "T must not be null after this point");
43930e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
43948cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (LangOpts.CPlusPlus && T->isFunctionType()) {
4395183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
4396778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
4397971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
439891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // C++ 8.3.5p4:
4399708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
4400708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
4401708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
4402708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
4403683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    //
4404683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // Core issue 547 also allows cv-qualifiers on function types that are
4405683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // top-level template type arguments.
4406613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
4407613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
4408579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      FreeFunction = ((D.getContext() != Declarator::MemberContext &&
4409579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling                       D.getContext() != Declarator::LambdaExprContext) ||
4410579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling                      D.getDeclSpec().isFriendSpecified());
4411613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
44128cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec());
4413613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
4414613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
4415613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
4416d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.fct]p6 (w/DR1417):
4417d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // An attempt to specify a function type with a cv-qualifier-seq or a
4418d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // ref-qualifier (including by typedef-name) is ill-formed unless it is:
4419d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type for a non-static member function,
4420d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type to which a pointer to member refers,
4421d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the top-level function type of a function typedef declaration or
4422d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //    alias-declaration,
4423d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id in the default argument of a type-parameter, or
4424d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id of a template-argument for a type-parameter
44256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    //
44266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    // FIXME: Checking this here is insufficient. We accept-invalid on:
44276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    //
44286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    //   template<typename T> struct S { void f(T); };
44296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    //   S<int() const> s;
44306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    //
44316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    // ... for instance.
4432d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction &&
4433d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !(!FreeFunction &&
4434d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) &&
4435d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !IsTypedefName &&
4436d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        D.getContext() != Declarator::TemplateTypeArgContext) {
443796a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      SourceLocation Loc = D.getLocStart();
4438d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      SourceRange RemovalRange;
4439d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      unsigned I;
4440d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      if (D.isFunctionDeclarator(I)) {
4441d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        SmallVector<SourceLocation, 4> RemovalLocs;
4442d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        const DeclaratorChunk &Chunk = D.getTypeObject(I);
4443d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        assert(Chunk.Kind == DeclaratorChunk::Function);
4444d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.hasRefQualifier())
4445d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getRefQualifierLoc());
4446d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Const)
4447d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getConstQualifierLoc());
4448d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Volatile)
4449d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getVolatileQualifierLoc());
4450176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        if (Chunk.Fun.TypeQuals & Qualifiers::Restrict)
4451176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines          RemovalLocs.push_back(Chunk.Fun.getRestrictQualifierLoc());
4452d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (!RemovalLocs.empty()) {
4453d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          std::sort(RemovalLocs.begin(), RemovalLocs.end(),
4454aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                    BeforeThanCompare<SourceLocation>(S.getSourceManager()));
4455d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalRange = SourceRange(RemovalLocs.front(), RemovalLocs.back());
4456d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          Loc = RemovalLocs.front();
4457683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
4458683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      }
4459d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
4460d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      S.Diag(Loc, diag::err_invalid_qualified_function_type)
4461d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FreeFunction << D.isFunctionDeclarator() << T
4462d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << getFunctionQualifiersAsString(FnTy)
4463d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FixItHint::CreateRemoval(RemovalRange);
4464d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
4465d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // Strip the cv-qualifiers and ref-qualifiers from the type.
4466d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
4467d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.TypeQuals = 0;
4468d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.RefQualifier = RQ_None;
4469d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
4470651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      T = Context.getFunctionType(FnTy->getReturnType(), FnTy->getParamTypes(),
4471bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                  EPI);
4472e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      // Rebuild any parens around the identifier in the function type.
4473e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
4474e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        if (D.getTypeObject(i).Kind != DeclaratorChunk::Paren)
4475e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith          break;
4476e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        T = S.BuildParenType(T);
4477e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      }
4478971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
4479971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
44801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
448287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  processTypeAttrs(state, T, TAL_DeclName, D.getAttributes());
4483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4484711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
44850e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  state.diagnoseIgnoredTypeAttrs(T);
4486711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4487148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  // C++0x [dcl.constexpr]p9:
4488148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  //  A constexpr specifier used in an object declaration declares the object
448991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //  as const.
4490148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
4491737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
4492737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
4493737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
449491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // If there was an ellipsis in the declarator, the declaration declares a
4495a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
44960e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (D.hasEllipsis()) {
4497a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
449891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   A declarator-id or abstract-declarator containing an ellipsis shall
4499a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
4500a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
4501a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
4502a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
4503fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    case Declarator::LambdaExprParameterContext:
4504a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
450591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   [...] When it is part of a parameter-declaration-clause, the
450691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   parameter pack is a function parameter pack (14.5.3). The type T
4507a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
450891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   a template parameter pack; each template parameter pack in T is
4509a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
4510a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
4511a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
4512a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
4513a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
451491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(D.getEllipsisLoc(),
4515a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
4516a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
4517a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
4518a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
451966874fb18afbffb8b2ca05576851a64534be3352David Blaikie        T = Context.getPackExpansionType(T, None);
4520a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
4521a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
4522a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
4523a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
452491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   If a template-parameter is a [...] is a parameter-declaration that
4525a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
4526a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
4527a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
4528a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
4529a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
4530a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
4531a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
453266874fb18afbffb8b2ca05576851a64534be3352David Blaikie        T = Context.getPackExpansionType(T, None);
4533e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith      else
4534e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith        S.Diag(D.getEllipsisLoc(),
453580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith               LangOpts.CPlusPlus11
4536e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 ? diag::warn_cxx98_compat_variadic_templates
4537e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 : diag::ext_variadic_templates);
4538a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
453991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4540a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
4541a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
4542cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:  // FIXME: special diagnostic here?
4543cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:     // FIXME: special diagnostic here?
4544a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
45450b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
4546162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
45473e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
4548a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
4549a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
4550a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
45514967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case Declarator::InitStmtContext:
4552a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
4553a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
455417b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
4555a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
4556f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
455714f78f4a11df4c06667e2cbb87eeb179e4cb46feRichard Smith    case Declarator::ConversionIdContext:
45587796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
4559683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    case Declarator::TemplateTypeArgContext:
4560a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
4561a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
45620e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      S.Diag(D.getEllipsisLoc(),
45630e2c34f92f00628d48968dfea096d36381f494cbStephen Hines             diag::err_ellipsis_in_declarator_not_parameter);
4564a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
4565a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
4566a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
4567a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
4568e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
45690e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  assert(!T.isNull() && "T must not be null at the end of this function");
45700e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (D.isInvalidType())
4571bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
4572db7abf78dedc2ef6ccb42b3dac6ab330fe2ea469Argyrios Kyrtzidis
45738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return S.GetTypeSourceInfoForDeclarator(D, T, TInfo);
45748cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
45758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
45768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// GetTypeForDeclarator - Convert the type for the specified
45778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// declarator to Type instances.
45788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis///
45798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// The result of this call will never be null, but the associated
45808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// type may be a null type if there's an unrecoverable error.
45818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
45828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // Determine the type of the declarator. Not all forms of declarator
45838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // have a type.
45848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
45858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeProcessingState state(*this, D);
45868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
45876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  TypeSourceInfo *ReturnTypeInfo = nullptr;
45888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
45898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
45904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (D.isPrototypeContext() && getLangOpts().ObjCAutoRefCount)
45918cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    inferARCWriteback(state, T);
459291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
45938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return GetFullTypeForDeclarator(state, T, ReturnTypeInfo);
45945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
45955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
459631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclSpec(Sema &S,
459731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           QualType &declSpecTy,
459831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           Qualifiers::ObjCLifetime ownership) {
459931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy->isObjCRetainableType() &&
460031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) {
460131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers qs;
460231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    qs.addObjCLifetime(ownership);
460331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    declSpecTy = S.Context.getQualifiedType(declSpecTy, qs);
460431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
460531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
460631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
460731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
460831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
460931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            unsigned chunkIndex) {
461031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
461131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
461231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
461331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Look for an explicit lifetime attribute.
461431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclaratorChunk &chunk = D.getTypeObject(chunkIndex);
461531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (const AttributeList *attr = chunk.getAttrs(); attr;
461631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis         attr = attr->getNext())
46178e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    if (attr->getKind() == AttributeList::AT_ObjCOwnership)
461831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
461931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
46206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const char *attrStr = nullptr;
462131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  switch (ownership) {
46223026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
462331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break;
462431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Strong: attrStr = "strong"; break;
462531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Weak: attrStr = "weak"; break;
462631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break;
462731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
462831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
4629624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  IdentifierLoc *Arg = new (S.Context) IdentifierLoc;
4630624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Arg->Ident = &S.Context.Idents.get(attrStr);
4631624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Arg->Loc = SourceLocation();
4632624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
4633624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  ArgsUnion Args(Arg);
4634624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
463531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // If there wasn't one, add one (with an invalid source location
463631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // so that we don't make an AttributedType for it).
463731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  AttributeList *attr = D.getAttributePool()
463831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    .create(&S.Context.Idents.get("objc_ownership"), SourceLocation(),
46396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            /*scope*/ nullptr, SourceLocation(),
4640624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman            /*args*/ &Args, 1, AttributeList::AS_GNU);
464131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  spliceAttrIntoList(*attr, chunk.getAttrListRef());
464231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
464331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // TODO: mark whether we did this inference?
464431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
464531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
464648d798ce32447607144db70a484cdb99c1180663Benjamin Kramer/// \brief Used for transferring ownership in casts resulting in l-values.
464731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnership(TypeProcessingState &state,
464831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 QualType &declSpecTy,
464931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 Qualifiers::ObjCLifetime ownership) {
465031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
465131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
465231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
465331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  int inner = -1;
46546ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis  bool hasIndirection = false;
465531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
465631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    DeclaratorChunk &chunk = D.getTypeObject(i);
465731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    switch (chunk.Kind) {
465831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Paren:
465931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      // Ignore parens.
466031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
466131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
466231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Array:
466331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Reference:
466431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Pointer:
46656ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
46666ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        hasIndirection = true;
466731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      inner = i;
466831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
466931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
467031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::BlockPointer:
46716ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
46726ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        transferARCOwnershipToDeclaratorChunk(state, ownership, i);
46736ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      return;
467431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
467531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Function:
467631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::MemberPointer:
46774967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case DeclaratorChunk::Pipe:
467831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
467931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    }
468031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
468131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
468231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (inner == -1)
46836ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    return;
468431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
468591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  DeclaratorChunk &chunk = D.getTypeObject(inner);
468631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (chunk.Kind == DeclaratorChunk::Pointer) {
468731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (declSpecTy->isObjCRetainableType())
468831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
46896ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    if (declSpecTy->isObjCObjectType() && hasIndirection)
469031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclaratorChunk(state, ownership, inner);
469131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  } else {
469231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    assert(chunk.Kind == DeclaratorChunk::Array ||
469331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis           chunk.Kind == DeclaratorChunk::Reference);
469431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
469531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
469631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
469731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
469831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) {
469931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeProcessingState state(*this, D);
470031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
47016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  TypeSourceInfo *ReturnTypeInfo = nullptr;
470231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
470331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
470487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (getLangOpts().ObjC1) {
470531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy);
470631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (ownership != Qualifiers::OCL_None)
470731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      transferARCOwnership(state, declSpecTy, ownership);
470831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
470931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
471031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo);
471131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
471231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
471314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall/// Map an AttributedType::Kind to an AttributeList::Kind.
471414aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
471514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  switch (kind) {
471614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_address_space:
47178e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_AddressSpace;
471814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_regparm:
47198e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Regparm;
472014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_vector_size:
47218e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_VectorSize;
472214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_vector_type:
47238e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonVectorType;
472414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_polyvector_type:
47258e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonPolyVectorType;
472614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_objc_gc:
47278e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCGC;
4728b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  case AttributedType::attr_objc_ownership:
472987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case AttributedType::attr_objc_inert_unsafe_unretained:
47308e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCOwnership;
473114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_noreturn:
47328e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NoReturn;
473314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_cdecl:
47348e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_CDecl;
473514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_fastcall:
47368e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_FastCall;
473714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_stdcall:
47388e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_StdCall;
473914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_thiscall:
47408e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ThisCall;
474114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_pascal:
47428e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pascal;
47434967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case AttributedType::attr_swiftcall:
47444967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    return AttributeList::AT_SwiftCall;
4745176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case AttributedType::attr_vectorcall:
4746176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return AttributeList::AT_VectorCall;
4747414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case AttributedType::attr_pcs:
47485b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributedType::attr_pcs_vfp:
47498e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pcs;
475038980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei  case AttributedType::attr_inteloclbicc:
475138980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    return AttributeList::AT_IntelOclBicc;
4752e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  case AttributedType::attr_ms_abi:
4753e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    return AttributeList::AT_MSABI;
4754e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  case AttributedType::attr_sysv_abi:
4755e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    return AttributeList::AT_SysVABI;
47564967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case AttributedType::attr_preserve_most:
47574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    return AttributeList::AT_PreserveMost;
47584967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case AttributedType::attr_preserve_all:
47594967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    return AttributeList::AT_PreserveAll;
4760aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_ptr32:
4761aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_Ptr32;
4762aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_ptr64:
4763aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_Ptr64;
4764aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_sptr:
4765aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_SPtr;
4766aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributedType::attr_uptr:
4767aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return AttributeList::AT_UPtr;
476887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case AttributedType::attr_nonnull:
476987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return AttributeList::AT_TypeNonNull;
477087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case AttributedType::attr_nullable:
477187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return AttributeList::AT_TypeNullable;
477287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case AttributedType::attr_null_unspecified:
477387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return AttributeList::AT_TypeNullUnspecified;
477487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case AttributedType::attr_objc_kindof:
477587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return AttributeList::AT_ObjCKindOf;
477614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
477714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  llvm_unreachable("unexpected attribute kind!");
477814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
477914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
478014aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic void fillAttributedTypeLoc(AttributedTypeLoc TL,
4781b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                                  const AttributeList *attrs,
4782b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                                  const AttributeList *DeclAttrs = nullptr) {
4783b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  // DeclAttrs and attrs cannot be both empty.
4784b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  assert((attrs || DeclAttrs) &&
4785b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar         "no type attributes in the expected location!");
4786b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
4787b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  AttributeList::Kind parsedKind = getAttrListKind(TL.getAttrKind());
4788b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  // Try to search for an attribute of matching kind in attrs list.
4789b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  while (attrs && attrs->getKind() != parsedKind)
479014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    attrs = attrs->getNext();
4791b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  if (!attrs) {
4792b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    // No matching type attribute in attrs list found.
4793b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    // Try searching through C++11 attributes in the declarator attribute list.
4794b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    while (DeclAttrs && (!DeclAttrs->isCXX11Attribute() ||
4795b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                         DeclAttrs->getKind() != parsedKind))
4796b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      DeclAttrs = DeclAttrs->getNext();
4797b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    attrs = DeclAttrs;
479814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
479914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
4800b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  assert(attrs && "no matching type attribute in expected location!");
4801b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
480214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  TL.setAttrNameLoc(attrs->getLoc());
4803651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (TL.hasAttrExprOperand()) {
4804651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    assert(attrs->isArgExpr(0) && "mismatched attribute operand kind");
4805624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    TL.setAttrExprOperand(attrs->getArgAsExpr(0));
4806651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  } else if (TL.hasAttrEnumOperand()) {
4807651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    assert((attrs->isArgIdent(0) || attrs->isArgExpr(0)) &&
4808651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines           "unexpected attribute operand kind");
4809651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (attrs->isArgIdent(0))
4810651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      TL.setAttrEnumOperandLoc(attrs->getArgAsIdent(0)->Loc);
4811651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    else
4812651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      TL.setAttrEnumOperandLoc(attrs->getArgAsExpr(0)->getExprLoc());
4813651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
481414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
481514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // FIXME: preserve this information to here.
481614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrOperand())
481714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrOperandParensRange(SourceRange());
481814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
481914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
482051bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
482151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
4822c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    ASTContext &Context;
482351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
4824f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
482551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
482691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
4827c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      : Context(Context), DS(DS) {}
4828f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
482914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
483014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, DS.getAttributes().getList());
483114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      Visit(TL.getModifiedLoc());
483214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
483351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
483451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
483551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
483651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
483751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
483851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
483951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
484051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
48411de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // FIXME. We should have DS.getTypeSpecTypeEndLoc(). But, it requires
48421de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // addition field. What we have is good enough for dispay of location
48431de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // of 'fixit' on interface name.
48441de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      TL.setNameEndLoc(DS.getLocEnd());
4845c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
4846c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
484787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      TypeSourceInfo *RepTInfo = nullptr;
484887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Sema::GetTypeFromParser(DS.getRepAsType(), &RepTInfo);
484987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      TL.copy(RepTInfo->getTypeLoc());
485051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
485154e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
485287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      TypeSourceInfo *RepTInfo = nullptr;
485387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Sema::GetTypeFromParser(DS.getRepAsType(), &RepTInfo);
485487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      TL.copy(RepTInfo->getTypeLoc());
485551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
4856833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
48576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TypeSourceInfo *TInfo = nullptr;
4858b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
4859833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
4860833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
4861833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
4862a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
48630daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara        TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
4864833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
4865833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
4866833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
4867e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
4868e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
486939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        ElaboratedTypeLoc ElabTL = OldTL.castAs<ElaboratedTypeLoc>();
487039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        TemplateSpecializationTypeLoc NamedTL = ElabTL.getNamedTypeLoc()
487139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            .castAs<TemplateSpecializationTypeLoc>();
4872e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
487344ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      } else {
487439e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        TL.copy(OldTL.castAs<TemplateSpecializationTypeLoc>());
487544ee0a710c59d8e6793189f903bae21c16814324Eli Friedman        assert(TL.getRAngleLoc() == OldTL.castAs<TemplateSpecializationTypeLoc>().getRAngleLoc());
487644ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      }
487744ee0a710c59d8e6793189f903bae21c16814324Eli Friedman
4878833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
4879cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
4880cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
4881cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
4882cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
4883cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
4884cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
4885cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
4886cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
4887cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
4888b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
48896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TypeSourceInfo *TInfo = nullptr;
4890b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
4891cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
4892cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
4893ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4894ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      // FIXME: This holds only because we only have one unary transform.
4895ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType);
4896ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setKWLoc(DS.getTypeSpecTypeLoc());
4897ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setParensRange(DS.getTypeofParensRange());
4898ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getRepAsType());
48996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TypeSourceInfo *TInfo = nullptr;
4900ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
4901ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setUnderlyingTInfo(TInfo);
4902ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
4903ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4904ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
4905ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
4906ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
4907ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
4908ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
4909ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
4910ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
4911ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
4912ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
4913ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
4914ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
4915ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
4916ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
4917ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
4918e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
4919e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
4920e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
4921253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
49226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        TypeSourceInfo *TInfo = nullptr;
4923b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
4924e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
492539e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          TL.copy(TInfo->getTypeLoc().castAs<ElaboratedTypeLoc>());
4926e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
4927e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
4928e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
492938a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara      TL.setElaboratedKeywordLoc(Keyword != ETK_None
493038a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 ? DS.getTypeSpecTypeLoc()
493138a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 : SourceLocation());
4932e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
49339e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
4934e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
4935e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
4936e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
493766581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
49386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TypeSourceInfo *TInfo = nullptr;
493966581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
494066581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
494139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      TL.copy(TInfo->getTypeLoc().castAs<DependentNameTypeLoc>());
494233500955d731c73717af52088b7fc0e7a85681e7John McCall    }
494333500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
494433500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
494566581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
49466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TypeSourceInfo *TInfo = nullptr;
494766581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
494866581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
494939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      TL.copy(
495039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          TInfo->getTypeLoc().castAs<DependentTemplateSpecializationTypeLoc>());
49510daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    }
49520daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    void VisitTagTypeLoc(TagTypeLoc TL) {
49530daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
4954e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
4955b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
49564cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // An AtomicTypeLoc can come from either an _Atomic(...) type specifier
49574cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // or an _Atomic qualifier.
49584cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      if (DS.getTypeSpecType() == DeclSpec::TST_atomic) {
49594cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setKWLoc(DS.getTypeSpecTypeLoc());
49604cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setParensRange(DS.getTypeofParensRange());
496191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
49626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        TypeSourceInfo *TInfo = nullptr;
49634cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
49644cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        assert(TInfo);
49654cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
49664cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      } else {
49674cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setKWLoc(DS.getAtomicSpecLoc());
49684cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // No parens, to indicate this was spelled as an _Atomic qualifier.
49694cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setParensRange(SourceRange());
49704cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        Visit(TL.getValueLoc());
49714cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      }
4972b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
4973e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
49744967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    void VisitPipeTypeLoc(PipeTypeLoc TL) {
49754967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      TL.setKWLoc(DS.getTypeSpecTypeLoc());
49764967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
49774967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      TypeSourceInfo *TInfo = nullptr;
49784967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
49794967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
49804967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
49814967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
498251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
498351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
4984c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initialize(Context, DS.getTypeSpecTypeLoc());
498551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
498651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
4987eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
498851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
4989b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    ASTContext &Context;
499051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
4991f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
499251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
4993b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
4994b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      : Context(Context), Chunk(Chunk) {}
49954adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
499651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
49979f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
499851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
499912df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner    void VisitDecayedTypeLoc(DecayedTypeLoc TL) {
500012df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner      llvm_unreachable("decayed type locs not expected here!");
500112df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner    }
50024adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
5003f85e193739c953358c865005855253af4f68a497John McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5004f85e193739c953358c865005855253af4f68a497John McCall      fillAttributedTypeLoc(TL, Chunk.getAttrs());
5005f85e193739c953358c865005855253af4f68a497John McCall    }
5006651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    void VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5007651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // nothing
5008651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
500951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
501051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
501151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
50124adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
501351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
501451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
501551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
50164adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
501751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
501851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
501951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
50204adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
502151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
502251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
5023b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const CXXScopeSpec& SS = Chunk.Mem.Scope();
5024b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
5025b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
5026b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const Type* ClsTy = TL.getClass();
5027b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      QualType ClsQT = QualType(ClsTy, 0);
5028b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
5029b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Now copy source location info into the type loc component.
5030b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeLoc ClsTL = ClsTInfo->getTypeLoc();
5031b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
5032b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Identifier:
5033b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
5034b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        {
503539e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          DependentNameTypeLoc DNTLoc = ClsTL.castAs<DependentNameTypeLoc>();
503638a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          DNTLoc.setElaboratedKeywordLoc(SourceLocation());
5037b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
5038b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
5039b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
5040b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
5041b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
5042b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpec:
5043b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpecWithTemplate:
5044b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        if (isa<ElaboratedType>(ClsTy)) {
504539e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          ElaboratedTypeLoc ETLoc = ClsTL.castAs<ElaboratedTypeLoc>();
504638a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          ETLoc.setElaboratedKeywordLoc(SourceLocation());
5047b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ETLoc.setQualifierLoc(NNSLoc.getPrefix());
5048b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
5049b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
5050b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        } else {
5051b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
5052b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
5053b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
5054b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
5055b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Namespace:
5056b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::NamespaceAlias:
5057b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Global:
5058176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      case NestedNameSpecifier::Super:
5059b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        llvm_unreachable("Nested-name-specifier must name a type");
5060b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      }
5061b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
5062b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Finally fill in MemberPointerLocInfo fields.
506351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
5064b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TL.setClassTInfo(ClsTInfo);
50654adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
506651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
506751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
506854e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
506954e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
507051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
507151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
507251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
507351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
507451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
507551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
507651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
507751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
507851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
507951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
508051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
508151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
508251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
508351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
508451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
5085796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeBegin(Chunk.Loc);
5086796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeEnd(Chunk.EndLoc);
508751bd803fbdade51d674598ed45da3d54190a656cJohn McCall
508851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
508959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setLParenLoc(FTI.getLParenLoc());
509059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setRParenLoc(FTI.getRParenLoc());
5091651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      for (unsigned i = 0, e = TL.getNumParams(), tpi = 0; i != e; ++i) {
5092651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
5093651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        TL.setParam(tpi++, Param);
50944adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
509551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
50964adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
5097075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
5098075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
5099075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
5100075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
5101075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
51024967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    void VisitPipeTypeLoc(PipeTypeLoc TL) {
51034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      assert(Chunk.Kind == DeclaratorChunk::Pipe);
51044967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      TL.setKWLoc(Chunk.Loc);
51054967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
51061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
510751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
51089f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
51094adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
511051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
51114967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} // end anonymous namespace
51124adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
51134cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smithstatic void fillAtomicQualLoc(AtomicTypeLoc ATL, const DeclaratorChunk &Chunk) {
51144cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  SourceLocation Loc;
51154cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  switch (Chunk.Kind) {
51164cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Function:
51174cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Array:
51184cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Paren:
51194967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case DeclaratorChunk::Pipe:
51204cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    llvm_unreachable("cannot be _Atomic qualified");
51214cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
51224cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Pointer:
51234cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    Loc = SourceLocation::getFromRawEncoding(Chunk.Ptr.AtomicQualLoc);
51244cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    break;
51254cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
51264cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::BlockPointer:
51274cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Reference:
51284cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::MemberPointer:
51294cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // FIXME: Provide a source location for the _Atomic keyword.
51304cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    break;
51314cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
51324cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
51334cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  ATL.setKWLoc(Loc);
51344cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  ATL.setParensRange(SourceRange());
51354cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith}
51364cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
5137a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
513851bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
513951bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
514005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
514105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
514205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
514305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
514405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
5145a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
514605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
514705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
5148a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
5149a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
5150b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  const AttributeList *DeclAttrs = D.getAttributes();
515151bd803fbdade51d674598ed45da3d54190a656cJohn McCall
5152a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
5153a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
515439e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    CurrTL.castAs<PackExpansionTypeLoc>().setEllipsisLoc(D.getEllipsisLoc());
515591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
5156a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
515791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
51588ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
51594cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // An AtomicTypeLoc might be produced by an atomic qualifier in this
51604cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // declarator chunk.
51614cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (AtomicTypeLoc ATL = CurrTL.getAs<AtomicTypeLoc>()) {
51624cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      fillAtomicQualLoc(ATL, D.getTypeObject(i));
51634cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      CurrTL = ATL.getValueLoc().getUnqualifiedLoc();
51644cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    }
51654cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
516639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    while (AttributedTypeLoc TL = CurrTL.getAs<AttributedTypeLoc>()) {
5167b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs(), DeclAttrs);
516814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
516914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
517014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
5171651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // FIXME: Ordering here?
5172651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    while (AdjustedTypeLoc TL = CurrTL.getAs<AdjustedTypeLoc>())
5173651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
5174651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
5175b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
517651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
51774adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
517891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5179b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
5180b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
5181b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
518205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
518305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
518405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
5185b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
5186c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
518705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
518891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5189a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
51904adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
51914adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
5192a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
5193b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
51941bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
51951bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
51961bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
519791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
5198eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
5199a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
52001bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
52011bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
5202b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
52031bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
52041bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
52051bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
52061bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
5207b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*"
520835d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
520935d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
52101bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
52111bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
5212f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
52135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
52145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
52156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  assert(D.getIdentifier() == nullptr &&
52166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines         "Type name should have no identifier!");
52171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5218d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
5219bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
52205153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
5221809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
52225912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
5223e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall  // Make sure there are no unused decl attributes on the declarator.
5224cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // We don't want to do this for ObjC parameters because we're going
5225cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // to apply them to the actual parameter declaration.
52266b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  // Likewise, we don't want to do this for alias declarations, because
52276b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  // we are actually going to build a declaration from this eventually.
52286b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  if (D.getContext() != Declarator::ObjCParameterContext &&
52296b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith      D.getContext() != Declarator::AliasDeclContext &&
52306b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith      D.getContext() != Declarator::AliasTemplateContext)
5231cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    checkUnusedDeclAttributes(D);
5232e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
52334e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
5234402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
52356d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
5236402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
5237402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
5238b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
52395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
52405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5241e97179c675b341927807c718be215c8d1aab8acbDouglas GregorParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) {
5242e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  QualType T = Context.getObjCInstanceType();
5243e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc);
5244e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return CreateParsedType(T, TInfo);
5245e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
5246e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
5247c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
5248c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
5249c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
5250232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
5251232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
5252c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
5253c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
52541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
5255c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
52560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
5257232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
525829e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified by
525929e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // qualifiers for two or more different address spaces."
5260232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
5261c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
5262e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
5263c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
5264232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
52651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5266020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be
5267020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // qualified by an address-space qualifier."
5268020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  if (Type->isFunctionType()) {
5269020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type);
5270020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    Attr.setInvalid();
5271020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    return;
5272020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  }
5273020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne
5274651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  unsigned ASIdx;
5275651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (Attr.getKind() == AttributeList::AT_AddressSpace) {
5276651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // Check the attribute arguments.
5277651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (Attr.getNumArgs() != 1) {
5278651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
5279651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        << Attr.getName() << 1;
5280651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Attr.setInvalid();
5281651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return;
5282651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
5283651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Expr *ASArgExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
5284651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::APSInt addrSpace(32);
5285651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
5286651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
5287651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
5288651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        << Attr.getName() << AANT_ArgumentIntegerConstant
5289efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
5290e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
5291efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
5292efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
5293efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
5294651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // Bounds checking.
5295651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (addrSpace.isSigned()) {
5296651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (addrSpace.isNegative()) {
5297651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
5298651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          << ASArgExpr->getSourceRange();
5299651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        Attr.setInvalid();
5300651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        return;
5301651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      }
5302651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      addrSpace.setIsSigned(false);
5303651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
5304651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::APSInt max(addrSpace.getBitWidth());
5305651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    max = Qualifiers::MaxAddressSpace;
5306651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (addrSpace > max) {
5307651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
5308651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        << int(Qualifiers::MaxAddressSpace) << ASArgExpr->getSourceRange();
5309651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Attr.setInvalid();
5310651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return;
5311651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
5312651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
5313651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  } else {
5314651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // The keyword-based type attributes imply which address space to use.
5315651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    switch (Attr.getKind()) {
5316651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case AttributeList::AT_OpenCLGlobalAddressSpace:
5317651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ASIdx = LangAS::opencl_global; break;
5318651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case AttributeList::AT_OpenCLLocalAddressSpace:
5319651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ASIdx = LangAS::opencl_local; break;
5320651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case AttributeList::AT_OpenCLConstantAddressSpace:
5321651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ASIdx = LangAS::opencl_constant; break;
53220e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    case AttributeList::AT_OpenCLGenericAddressSpace:
53230e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      ASIdx = LangAS::opencl_generic; break;
5324651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    default:
5325651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      assert(Attr.getKind() == AttributeList::AT_OpenCLPrivateAddressSpace);
5326651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ASIdx = 0; break;
5327651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
5328651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
5329651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
5330f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
5331c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
5332c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
5333d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// Does this type have a "direct" ownership qualifier?  That is,
5334d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// is it written like "__strong id", as opposed to something like
5335d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// "typeof(foo)", where that happens to be strong?
5336d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCallstatic bool hasDirectOwnershipQualifier(QualType type) {
5337d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Fast path: no qualifier at all.
5338d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  assert(type.getQualifiers().hasObjCLifetime());
5339d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
5340d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  while (true) {
5341d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // __strong id
5342d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (const AttributedType *attr = dyn_cast<AttributedType>(type)) {
5343d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      if (attr->getAttrKind() == AttributedType::attr_objc_ownership)
5344d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        return true;
5345d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
5346d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = attr->getModifiedType();
5347d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
5348d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // X *__strong (...)
5349d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else if (const ParenType *paren = dyn_cast<ParenType>(type)) {
5350d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = paren->getInnerType();
535191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5352d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // That's it for things we want to complain about.  In particular,
5353d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // we do not want to look through typedefs, typeof(expr),
5354d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // typeof(type), or any other way that the type is somehow
5355d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // abstracted.
5356d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else {
535791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5358d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return false;
5359d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
5360d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
5361d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall}
5362d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
5363b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis/// handleObjCOwnershipTypeAttr - Process an objc_ownership
5364f85e193739c953358c865005855253af4f68a497John McCall/// attribute on the specified type.
5365f85e193739c953358c865005855253af4f68a497John McCall///
5366f85e193739c953358c865005855253af4f68a497John McCall/// Returns 'true' if the attribute was handled.
5367b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
5368f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr,
5369f85e193739c953358c865005855253af4f68a497John McCall                                       QualType &type) {
5370e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  bool NonObjCPointer = false;
5371e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
5372dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith  if (!type->isDependentType() && !type->isUndeducedType()) {
5373e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    if (const PointerType *ptr = type->getAs<PointerType>()) {
5374e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      QualType pointee = ptr->getPointeeType();
5375e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      if (pointee->isObjCRetainableType() || pointee->isPointerType())
5376e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis        return false;
5377e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // It is important not to lose the source info that there was an attribute
5378e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // applied to non-objc pointer. We will create an attributed type but
5379e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // its type will be the same as the original type.
5380e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      NonObjCPointer = true;
5381e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    } else if (!type->isObjCRetainableType()) {
5382e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      return false;
5383e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
5384b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
5385b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // Don't accept an ownership attribute in the declspec if it would
5386b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // just be the return type of a block pointer.
5387b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    if (state.isProcessingDeclSpec()) {
5388b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      Declarator &D = state.getDeclarator();
538987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (maybeMovePastReturnType(D, D.getNumTypeObjects(),
539087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                  /*onlyBlockPointers=*/true))
5391b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        return false;
5392b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
5393e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
5394f85e193739c953358c865005855253af4f68a497John McCall
5395f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
5396440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  SourceLocation AttrLoc = attr.getLoc();
5397440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isMacroID())
5398440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    AttrLoc = S.getSourceManager().getImmediateExpansionRange(AttrLoc).first;
5399f85e193739c953358c865005855253af4f68a497John McCall
5400624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (!attr.isArgIdent(0)) {
54013cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman    S.Diag(AttrLoc, diag::err_attribute_argument_type)
54023cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman      << attr.getName() << AANT_ArgumentString;
5403f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
5404f85e193739c953358c865005855253af4f68a497John McCall    return true;
5405f85e193739c953358c865005855253af4f68a497John McCall  }
5406f85e193739c953358c865005855253af4f68a497John McCall
5407624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  IdentifierInfo *II = attr.getArgAsIdent(0)->Ident;
5408f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime lifetime;
5409624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (II->isStr("none"))
5410f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_ExplicitNone;
5411624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (II->isStr("strong"))
5412f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Strong;
5413624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (II->isStr("weak"))
5414f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Weak;
5415624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (II->isStr("autoreleasing"))
5416f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Autoreleasing;
5417f85e193739c953358c865005855253af4f68a497John McCall  else {
5418440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_attribute_type_not_supported)
5419d068607c136298bec0891d750389a55bac9f5c98Aaron Ballman      << attr.getName() << II;
5420f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
5421f85e193739c953358c865005855253af4f68a497John McCall    return true;
5422f85e193739c953358c865005855253af4f68a497John McCall  }
5423f85e193739c953358c865005855253af4f68a497John McCall
542487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Just ignore lifetime attributes other than __weak and __unsafe_unretained
542587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // outside of ARC mode.
542687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!S.getLangOpts().ObjCAutoRefCount &&
542787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      lifetime != Qualifiers::OCL_Weak &&
542887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      lifetime != Qualifiers::OCL_ExplicitNone) {
542987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return true;
543087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
543187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
5432d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  SplitQualType underlyingType = type.split();
5433d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
5434d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Check for redundant/conflicting ownership qualifiers.
5435d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  if (Qualifiers::ObjCLifetime previousLifetime
5436d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        = type.getQualifiers().getObjCLifetime()) {
5437d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // If it's written directly, that's an error.
5438d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (hasDirectOwnershipQualifier(type)) {
5439d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant)
5440d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        << type;
5441d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return true;
5442d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
5443d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
5444d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // Otherwise, if the qualifiers actually conflict, pull sugar off
54454967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    // and remove the ObjCLifetime qualifiers.
5446d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (previousLifetime != lifetime) {
54474967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      // It's possible to have multiple local ObjCLifetime qualifiers. We
54484967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      // can't stop after we reach a type that is directly qualified.
54494967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      const Type *prevTy = nullptr;
54504967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      while (!prevTy || prevTy != underlyingType.Ty) {
54514967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        prevTy = underlyingType.Ty;
5452d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        underlyingType = underlyingType.getSingleStepDesugaredType();
5453d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      }
5454d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      underlyingType.Quals.removeObjCLifetime();
5455d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
5456d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
5457d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
5458d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  underlyingType.Quals.addObjCLifetime(lifetime);
5459f85e193739c953358c865005855253af4f68a497John McCall
5460e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (NonObjCPointer) {
5461e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    StringRef name = attr.getName()->getName();
5462e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    switch (lifetime) {
5463e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_None:
5464e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_ExplicitNone:
5465e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      break;
5466e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Strong: name = "__strong"; break;
5467e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Weak: name = "__weak"; break;
5468e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break;
5469e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
5470fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman    S.Diag(AttrLoc, diag::warn_type_attribute_wrong_type) << name
5471fe6dec6efaf860ed3eb20ee13267f35129f7747aAaron Ballman      << TDS_ObjCObjOrBlock << type;
5472e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
5473e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
547487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Don't actually add the __unsafe_unretained qualifier in non-ARC files,
547587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // because having both 'T' and '__unsafe_unretained T' exist in the type
547687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // system causes unfortunate widespread consistency problems.  (For example,
547787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // they're not considered compatible types, and we mangle them identicially
547887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // as template arguments.)  These problems are all individually fixable,
547987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // but it's easier to just not add the qualifier and instead sniff it out
548087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // in specific places using isObjCInertUnsafeUnretainedType().
548187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  //
548287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Doing this does means we miss some trivial consistency checks that
548387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // would've triggered in ARC, but that's better than trying to solve all
548487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // the coexistence problems with __unsafe_unretained.
548587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!S.getLangOpts().ObjCAutoRefCount &&
548687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      lifetime == Qualifiers::OCL_ExplicitNone) {
548787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    type = S.Context.getAttributedType(
548887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                             AttributedType::attr_objc_inert_unsafe_unretained,
548987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                       type, type);
549087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return true;
549187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
549287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
5493f85e193739c953358c865005855253af4f68a497John McCall  QualType origType = type;
5494e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!NonObjCPointer)
5495d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    type = S.Context.getQualifiedType(underlyingType);
5496f85e193739c953358c865005855253af4f68a497John McCall
5497f85e193739c953358c865005855253af4f68a497John McCall  // If we have a valid source location for the attribute, use an
5498f85e193739c953358c865005855253af4f68a497John McCall  // AttributedType instead.
5499440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isValid())
5500b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis    type = S.Context.getAttributedType(AttributedType::attr_objc_ownership,
5501f85e193739c953358c865005855253af4f68a497John McCall                                       origType, type);
5502f85e193739c953358c865005855253af4f68a497John McCall
550387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  auto diagnoseOrDelay = [](Sema &S, SourceLocation loc,
550487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                            unsigned diagnostic, QualType type) {
5505f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
5506f85e193739c953358c865005855253af4f68a497John McCall      S.DelayedDiagnostics.add(
5507440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis          sema::DelayedDiagnostic::makeForbiddenType(
550887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              S.getSourceManager().getExpansionLoc(loc),
550987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              diagnostic, type, /*ignored*/ 0));
5510f85e193739c953358c865005855253af4f68a497John McCall    } else {
551187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      S.Diag(loc, diagnostic);
5512f85e193739c953358c865005855253af4f68a497John McCall    }
551387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  };
551487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
551587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Sometimes, __weak isn't allowed.
551687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (lifetime == Qualifiers::OCL_Weak &&
551787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      !S.getLangOpts().ObjCWeak && !NonObjCPointer) {
551887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
551987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Use a specialized diagnostic if the runtime just doesn't support them.
552087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    unsigned diagnostic =
552187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      (S.getLangOpts().ObjCWeakRuntime ? diag::err_arc_weak_disabled
552287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                       : diag::err_arc_weak_no_runtime);
552387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
552487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // In any case, delay the diagnostic until we know what we're parsing.
552587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    diagnoseOrDelay(S, AttrLoc, diagnostic, type);
5526f85e193739c953358c865005855253af4f68a497John McCall
5527f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
5528f85e193739c953358c865005855253af4f68a497John McCall    return true;
5529f85e193739c953358c865005855253af4f68a497John McCall  }
553091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
553191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // Forbid __weak for class objects marked as
5532742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  // objc_arc_weak_reference_unavailable
5533742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  if (lifetime == Qualifiers::OCL_Weak) {
5534b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    if (const ObjCObjectPointerType *ObjT =
5535b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          type->getAs<ObjCObjectPointerType>()) {
55364e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith      if (ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl()) {
55374e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        if (Class->isArcWeakrefUnavailable()) {
553887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class);
553987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          S.Diag(ObjT->getInterfaceDecl()->getLocation(),
554087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                  diag::note_class_declared);
55414e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        }
5542742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      }
5543742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    }
5544742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  }
554591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5546f85e193739c953358c865005855253af4f68a497John McCall  return true;
5547f85e193739c953358c865005855253af4f68a497John McCall}
5548f85e193739c953358c865005855253af4f68a497John McCall
5549711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
5550711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
5551711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
5552711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
5553711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
5554711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
5555711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
5556711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
5557711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5558711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
5559711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
5560711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
5561711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
5562711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
5563711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5564711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
5565711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
5566711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
5567711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
5568d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
5569624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
5570d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
5571624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (!attr.isArgIdent(0)) {
55723cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman    S.Diag(attr.getLoc(), diag::err_attribute_argument_type)
55733cd6feb87a62fb52c31cbc83655d76ace020513fAaron Ballman      << attr.getName() << AANT_ArgumentString;
5574711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
5575711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
5576ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
55770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
5578624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (attr.getNumArgs() > 1) {
5579baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
5580baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << attr.getName() << 1;
5581711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
5582711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
5583d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
5584624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
5585624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  IdentifierInfo *II = attr.getArgAsIdent(0)->Ident;
5586624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (II->isStr("weak"))
55870953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
5588624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  else if (II->isStr("strong"))
55890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
5590d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
5591711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
5592d068607c136298bec0891d750389a55bac9f5c98Aaron Ballman      << attr.getName() << II;
5593711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
5594711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
5595d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
55961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
559714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  QualType origType = type;
559814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  type = S.Context.getObjCGCQualType(origType, GCAttr);
559914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
560014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // Make an attributed type to preserve the source information.
560114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (attr.getLoc().isValid())
560214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
560314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                       origType, type);
560414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
5605711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
5606d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
5607d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
5608579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendlingnamespace {
5609579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling  /// A helper class to unwrap a type down to a function for the
5610579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling  /// purposes of applying attributes there.
5611579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling  ///
5612579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling  /// Use:
5613579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
5614579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling  ///   if (unwrapped.isFunctionType()) {
5615579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling  ///     const FunctionType *fn = unwrapped.get();
5616579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling  ///     // change fn somehow
5617579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling  ///     T = unwrapped.wrap(fn);
5618579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling  ///   }
5619579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling  struct FunctionTypeUnwrapper {
5620579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    enum WrapKind {
5621579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      Desugar,
56224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      Attributed,
5623579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      Parens,
5624579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      Pointer,
5625579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      BlockPointer,
5626579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      Reference,
5627579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      MemberPointer
5628579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    };
5629579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5630579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    QualType Original;
5631579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    const FunctionType *Fn;
5632579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    SmallVector<unsigned char /*WrapKind*/, 8> Stack;
5633579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5634579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
5635579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      while (true) {
5636579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        const Type *Ty = T.getTypePtr();
5637579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        if (isa<FunctionType>(Ty)) {
5638579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          Fn = cast<FunctionType>(Ty);
5639579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          return;
5640579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        } else if (isa<ParenType>(Ty)) {
5641579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          T = cast<ParenType>(Ty)->getInnerType();
5642579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          Stack.push_back(Parens);
5643579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        } else if (isa<PointerType>(Ty)) {
5644579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          T = cast<PointerType>(Ty)->getPointeeType();
5645579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          Stack.push_back(Pointer);
5646579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        } else if (isa<BlockPointerType>(Ty)) {
5647579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          T = cast<BlockPointerType>(Ty)->getPointeeType();
5648579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          Stack.push_back(BlockPointer);
5649579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        } else if (isa<MemberPointerType>(Ty)) {
5650579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          T = cast<MemberPointerType>(Ty)->getPointeeType();
5651579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          Stack.push_back(MemberPointer);
5652579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        } else if (isa<ReferenceType>(Ty)) {
5653579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          T = cast<ReferenceType>(Ty)->getPointeeType();
5654579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          Stack.push_back(Reference);
56554967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        } else if (isa<AttributedType>(Ty)) {
56564967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          T = cast<AttributedType>(Ty)->getEquivalentType();
56574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar          Stack.push_back(Attributed);
5658579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        } else {
5659579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          const Type *DTy = Ty->getUnqualifiedDesugaredType();
5660579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          if (Ty == DTy) {
56616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            Fn = nullptr;
5662579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling            return;
5663579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          }
5664579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5665579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          T = QualType(DTy, 0);
5666579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          Stack.push_back(Desugar);
5667579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        }
5668579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      }
5669579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    }
5670579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
56716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    bool isFunctionType() const { return (Fn != nullptr); }
5672579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    const FunctionType *get() const { return Fn; }
5673579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5674579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    QualType wrap(Sema &S, const FunctionType *New) {
5675579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      // If T wasn't modified from the unwrapped type, do nothing.
5676579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      if (New == get()) return Original;
5677579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5678579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      Fn = New;
5679579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      return wrap(S.Context, Original, 0);
5680579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    }
5681579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5682579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling  private:
5683579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
5684579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      if (I == Stack.size())
5685579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        return C.getQualifiedType(Fn, Old.getQualifiers());
5686579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5687579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      // Build up the inner type, applying the qualifiers from the old
5688579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      // type to the new type.
5689579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      SplitQualType SplitOld = Old.split();
5690579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5691579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      // As a special case, tail-recurse if there are no qualifiers.
5692579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      if (SplitOld.Quals.empty())
5693579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        return wrap(C, SplitOld.Ty, I);
5694579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      return C.getQualifiedType(wrap(C, SplitOld.Ty, I), SplitOld.Quals);
5695579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    }
5696579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5697579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
5698579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      if (I == Stack.size()) return QualType(Fn, 0);
5699579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5700579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      switch (static_cast<WrapKind>(Stack[I++])) {
5701579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      case Desugar:
5702579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        // This is the point at which we potentially lose source
5703579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        // information.
5704579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
5705579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
57064967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      case Attributed:
57074967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        return wrap(C, cast<AttributedType>(Old)->getEquivalentType(), I);
57084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
5709579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      case Parens: {
5710579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
5711579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        return C.getParenType(New);
5712579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      }
5713579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5714579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      case Pointer: {
5715579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
5716579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        return C.getPointerType(New);
5717579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      }
5718579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5719579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      case BlockPointer: {
5720579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
5721579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        return C.getBlockPointerType(New);
5722579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      }
5723579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5724579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      case MemberPointer: {
5725579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
5726579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        QualType New = wrap(C, OldMPT->getPointeeType(), I);
5727579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        return C.getMemberPointerType(New, OldMPT->getClass());
5728579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      }
5729579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5730579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      case Reference: {
5731579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        const ReferenceType *OldRef = cast<ReferenceType>(Old);
5732579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        QualType New = wrap(C, OldRef->getPointeeType(), I);
5733579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        if (isa<LValueReferenceType>(OldRef))
5734579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
5735579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling        else
5736579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling          return C.getRValueReferenceType(New);
5737579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      }
5738579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      }
5739579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5740579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling      llvm_unreachable("unknown wrapping kind");
5741579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling    }
5742579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling  };
57434967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar} // end anonymous namespace
5744579d45ff5f92c4f5e31213e31490acdc5bcc5567Bill Wendling
5745aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballmanstatic bool handleMSPointerTypeQualifierAttr(TypeProcessingState &State,
5746aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             AttributeList &Attr,
5747aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                                             QualType &Type) {
5748aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  Sema &S = State.getSema();
5749aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
5750aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  AttributeList::Kind Kind = Attr.getKind();
5751aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  QualType Desugared = Type;
5752aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  const AttributedType *AT = dyn_cast<AttributedType>(Type);
5753aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  while (AT) {
5754aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    AttributedType::Kind CurAttrKind = AT->getAttrKind();
5755aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
5756aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // You cannot specify duplicate type attributes, so if the attribute has
5757aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // already been applied, flag it.
5758aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    if (getAttrListKind(CurAttrKind) == Kind) {
5759aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute_exact)
5760aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        << Attr.getName();
5761aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      return true;
5762aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    }
5763aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
5764aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // You cannot have both __sptr and __uptr on the same type, nor can you
5765aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    // have __ptr32 and __ptr64.
5766aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    if ((CurAttrKind == AttributedType::attr_ptr32 &&
5767aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman         Kind == AttributeList::AT_Ptr64) ||
5768aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        (CurAttrKind == AttributedType::attr_ptr64 &&
5769aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman         Kind == AttributeList::AT_Ptr32)) {
5770aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
5771aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        << "'__ptr32'" << "'__ptr64'";
5772aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      return true;
5773aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    } else if ((CurAttrKind == AttributedType::attr_sptr &&
5774aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                Kind == AttributeList::AT_UPtr) ||
5775aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman               (CurAttrKind == AttributedType::attr_uptr &&
5776aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman                Kind == AttributeList::AT_SPtr)) {
5777aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
5778aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        << "'__sptr'" << "'__uptr'";
5779aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      return true;
5780aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    }
5781aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
5782aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    Desugared = AT->getEquivalentType();
5783aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    AT = dyn_cast<AttributedType>(Desugared);
5784aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  }
5785aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
5786aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  // Pointer type qualifiers can only operate on pointer types, but not
5787aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  // pointer-to-member types.
5788aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  if (!isa<PointerType>(Desugared)) {
578987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (Type->isMemberPointerType())
579087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      S.Diag(Attr.getLoc(), diag::err_attribute_no_member_pointers)
579187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << Attr.getName();
579287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    else
579387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
579487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << Attr.getName() << 0;
5795aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    return true;
5796aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  }
5797aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
5798aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  AttributedType::Kind TAK;
5799aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  switch (Kind) {
5800aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  default: llvm_unreachable("Unknown attribute kind");
5801aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_Ptr32: TAK = AttributedType::attr_ptr32; break;
5802aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_Ptr64: TAK = AttributedType::attr_ptr64; break;
5803aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_SPtr: TAK = AttributedType::attr_sptr; break;
5804aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  case AttributeList::AT_UPtr: TAK = AttributedType::attr_uptr; break;
5805aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  }
5806aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
5807aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  Type = S.Context.getAttributedType(TAK, Type, Type);
5808aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman  return false;
5809aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman}
5810aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman
581187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarbool Sema::checkNullabilityTypeSpecifier(QualType &type,
581287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                         NullabilityKind nullability,
581387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                         SourceLocation nullabilityLoc,
581487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                         bool isContextSensitive) {
581587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // We saw a nullability type specifier. If this is the first one for
581687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // this file, note that.
581787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  FileID file = getNullabilityCompletenessCheckFileID(*this, nullabilityLoc);
581887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!file.isInvalid()) {
581987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    FileNullability &fileNullability = NullabilityMap[file];
582087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (!fileNullability.SawTypeNullability) {
582187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // If we have already seen a pointer declarator without a nullability
582287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // annotation, complain about it.
582387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (fileNullability.PointerLoc.isValid()) {
582487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        Diag(fileNullability.PointerLoc, diag::warn_nullability_missing)
582587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << static_cast<unsigned>(fileNullability.PointerKind);
582687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
582787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
582887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      fileNullability.SawTypeNullability = true;
582987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
583087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
583187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
583287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Check for existing nullability attributes on the type.
583387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  QualType desugared = type;
583487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  while (auto attributed = dyn_cast<AttributedType>(desugared.getTypePtr())) {
583587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Check whether there is already a null
583687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (auto existingNullability = attributed->getImmediateNullability()) {
583787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Duplicated nullability.
583887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (nullability == *existingNullability) {
583987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        Diag(nullabilityLoc, diag::warn_nullability_duplicate)
584087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << DiagNullabilityKind(nullability, isContextSensitive)
584187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << FixItHint::CreateRemoval(nullabilityLoc);
584287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
584387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        break;
584487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
584587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
584687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Conflicting nullability.
584787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Diag(nullabilityLoc, diag::err_nullability_conflicting)
584887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << DiagNullabilityKind(nullability, isContextSensitive)
584987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << DiagNullabilityKind(*existingNullability, false);
585087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return true;
585187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
585287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
585387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    desugared = attributed->getModifiedType();
585487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
585587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
585687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // If there is already a different nullability specifier, complain.
585787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // This (unlike the code above) looks through typedefs that might
585887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // have nullability specifiers on them, which means we cannot
585987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // provide a useful Fix-It.
586087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (auto existingNullability = desugared->getNullability(Context)) {
586187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (nullability != *existingNullability) {
586287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Diag(nullabilityLoc, diag::err_nullability_conflicting)
586387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << DiagNullabilityKind(nullability, isContextSensitive)
586487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << DiagNullabilityKind(*existingNullability, false);
586587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
586687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Try to find the typedef with the existing nullability specifier.
586787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (auto typedefType = desugared->getAs<TypedefType>()) {
586887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        TypedefNameDecl *typedefDecl = typedefType->getDecl();
586987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        QualType underlyingType = typedefDecl->getUnderlyingType();
587087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (auto typedefNullability
587187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              = AttributedType::stripOuterNullability(underlyingType)) {
587287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          if (*typedefNullability == *existingNullability) {
587387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            Diag(typedefDecl->getLocation(), diag::note_nullability_here)
587487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              << DiagNullabilityKind(*existingNullability, false);
587587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          }
587687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        }
587787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
587887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
587987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return true;
588087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
588187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
588287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
588387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // If this definitely isn't a pointer type, reject the specifier.
588487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!desugared->canHaveNullability()) {
588587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    Diag(nullabilityLoc, diag::err_nullability_nonpointer)
588687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << DiagNullabilityKind(nullability, isContextSensitive) << type;
588787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return true;
588887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
588987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
589087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // For the context-sensitive keywords/Objective-C property
589187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // attributes, require that the type be a single-level pointer.
589287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (isContextSensitive) {
589387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Make sure that the pointee isn't itself a pointer type.
589487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    QualType pointeeType = desugared->getPointeeType();
589587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (pointeeType->isAnyPointerType() ||
589687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        pointeeType->isObjCObjectPointerType() ||
589787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        pointeeType->isMemberPointerType()) {
589887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Diag(nullabilityLoc, diag::err_nullability_cs_multilevel)
589987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << DiagNullabilityKind(nullability, true)
590087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << type;
590187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Diag(nullabilityLoc, diag::note_nullability_type_specifier)
590287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << DiagNullabilityKind(nullability, false)
590387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << type
590487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        << FixItHint::CreateReplacement(nullabilityLoc,
590587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                        getNullabilitySpelling(nullability));
590687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return true;
590787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
590887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
590987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
591087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Form the attributed type.
591187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  type = Context.getAttributedType(
591287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar           AttributedType::getNullabilityAttrKind(nullability), type, type);
591387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  return false;
591487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
591587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
591687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarbool Sema::checkObjCKindOfType(QualType &type, SourceLocation loc) {
591787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Find out if it's an Objective-C object or object pointer type;
591887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  const ObjCObjectPointerType *ptrType = type->getAs<ObjCObjectPointerType>();
591987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  const ObjCObjectType *objType = ptrType ? ptrType->getObjectType()
592087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                          : type->getAs<ObjCObjectType>();
592187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
592287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // If not, we can't apply __kindof.
592387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!objType) {
592487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // FIXME: Handle dependent types that aren't yet object types.
592587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    Diag(loc, diag::err_objc_kindof_nonobject)
592687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << type;
592787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return true;
592887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
592987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
593087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Rebuild the "equivalent" type, which pushes __kindof down into
593187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // the object type.
59324967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // There is no need to apply kindof on an unqualified id type.
59334967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  QualType equivType = Context.getObjCObjectType(
59344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      objType->getBaseType(), objType->getTypeArgsAsWritten(),
59354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      objType->getProtocols(),
59364967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      /*isKindOf=*/objType->isObjCUnqualifiedId() ? false : true);
593787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
593887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // If we started with an object pointer type, rebuild it.
593987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (ptrType) {
594087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    equivType = Context.getObjCObjectPointerType(equivType);
594187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (auto nullability = type->getNullability(Context)) {
594287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      auto attrKind = AttributedType::getNullabilityAttrKind(*nullability);
594387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      equivType = Context.getAttributedType(attrKind, equivType, equivType);
594487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
594587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
594687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
594787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Build the attributed type to record where __kindof occurred.
594887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  type = Context.getAttributedType(AttributedType::attr_objc_kindof,
594987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   type,
595087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   equivType);
595187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
595287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  return false;
595387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
595487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
595587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// Map a nullability attribute kind to a nullability kind.
595687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarstatic NullabilityKind mapNullabilityAttrKind(AttributeList::Kind kind) {
595787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  switch (kind) {
595887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case AttributeList::AT_TypeNonNull:
595987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return NullabilityKind::NonNull;
596087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
596187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case AttributeList::AT_TypeNullable:
596287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return NullabilityKind::Nullable;
596387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
596487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  case AttributeList::AT_TypeNullUnspecified:
596587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return NullabilityKind::Unspecified;
596687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
596787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  default:
596887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    llvm_unreachable("not a nullability attribute kind");
596987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
597087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
597187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
597287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// Distribute a nullability type attribute that cannot be applied to
597387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// the type specifier to a pointer, block pointer, or member pointer
597487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// declarator, complaining if necessary.
597587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar///
597687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// \returns true if the nullability annotation was distributed, false
597787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// otherwise.
597887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarstatic bool distributeNullabilityTypeAttr(TypeProcessingState &state,
597987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                          QualType type,
598087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                          AttributeList &attr) {
598187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  Declarator &declarator = state.getDeclarator();
598287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
598387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  /// Attempt to move the attribute to the specified chunk.
598487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  auto moveToChunk = [&](DeclaratorChunk &chunk, bool inFunction) -> bool {
598587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // If there is already a nullability attribute there, don't add
598687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // one.
598787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (hasNullabilityAttr(chunk.getAttrListRef()))
598887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return false;
598987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
599087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Complain about the nullability qualifier being in the wrong
599187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // place.
599287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    enum {
599387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      PK_Pointer,
599487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      PK_BlockPointer,
599587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      PK_MemberPointer,
599687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      PK_FunctionPointer,
599787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      PK_MemberFunctionPointer,
599887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    } pointerKind
599987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      = chunk.Kind == DeclaratorChunk::Pointer ? (inFunction ? PK_FunctionPointer
600087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                                             : PK_Pointer)
600187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        : chunk.Kind == DeclaratorChunk::BlockPointer ? PK_BlockPointer
600287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        : inFunction? PK_MemberFunctionPointer : PK_MemberPointer;
600387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
600487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    auto diag = state.getSema().Diag(attr.getLoc(),
600587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                     diag::warn_nullability_declspec)
600687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << DiagNullabilityKind(mapNullabilityAttrKind(attr.getKind()),
600787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                             attr.isContextSensitiveKeywordAttribute())
600887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << type
600987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      << static_cast<unsigned>(pointerKind);
601087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
601187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // FIXME: MemberPointer chunks don't carry the location of the *.
601287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (chunk.Kind != DeclaratorChunk::MemberPointer) {
601387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      diag << FixItHint::CreateRemoval(attr.getLoc())
601487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar           << FixItHint::CreateInsertion(
601587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                state.getSema().getPreprocessor()
601687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                  .getLocForEndOfToken(chunk.Loc),
601787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                " " + attr.getName()->getName().str() + " ");
601887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
601987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
602087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
602187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                           chunk.getAttrListRef());
602287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return true;
602387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  };
602487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
602587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Move it to the outermost pointer, member pointer, or block
602687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // pointer declarator.
602787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
602887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
602987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    switch (chunk.Kind) {
603087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::Pointer:
603187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::BlockPointer:
603287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::MemberPointer:
603387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return moveToChunk(chunk, false);
603487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
603587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::Paren:
603687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::Array:
603787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      continue;
603887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
603987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::Function:
604087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Try to move past the return type to a function/block/member
604187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // function pointer.
604287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (DeclaratorChunk *dest = maybeMovePastReturnType(
604387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                    declarator, i,
604487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                    /*onlyBlockPointers=*/false)) {
604587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        return moveToChunk(*dest, true);
604687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
604787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
604887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return false;
604987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
605087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Don't walk through these.
605187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case DeclaratorChunk::Reference:
60524967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case DeclaratorChunk::Pipe:
605387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return false;
605487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
605587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
605687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
605787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  return false;
605887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
605987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
60605b92696c8f1f8ef943ad87397b95c031b5787305Reid Klecknerstatic AttributedType::Kind getCCTypeAttrKind(AttributeList &Attr) {
60615b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  assert(!Attr.isInvalid());
60625b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  switch (Attr.getKind()) {
60635b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  default:
60645b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    llvm_unreachable("not a calling convention attribute");
60655b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_CDecl:
60665b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_cdecl;
60675b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_FastCall:
60685b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_fastcall;
60695b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_StdCall:
60705b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_stdcall;
60715b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_ThisCall:
60725b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_thiscall;
60735b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_Pascal:
60745b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_pascal;
60754967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case AttributeList::AT_SwiftCall:
60764967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    return AttributedType::attr_swiftcall;
6077176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  case AttributeList::AT_VectorCall:
6078176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return AttributedType::attr_vectorcall;
60795b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_Pcs: {
6080fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    // The attribute may have had a fixit applied where we treated an
6081fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    // identifier as a string literal.  The contents of the string are valid,
6082fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    // but the form may not be.
6083fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    StringRef Str;
6084fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    if (Attr.isArgExpr(0))
6085fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman      Str = cast<StringLiteral>(Attr.getArgAsExpr(0))->getString();
6086fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    else
6087fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman      Str = Attr.getArgAsIdent(0)->Ident->getName();
6088fbf6f5c8ac0a3feb9a5add5f9221a21f68ca487aAaron Ballman    return llvm::StringSwitch<AttributedType::Kind>(Str)
60895b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner        .Case("aapcs", AttributedType::attr_pcs)
60905b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner        .Case("aapcs-vfp", AttributedType::attr_pcs_vfp);
60915b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  }
60925b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  case AttributeList::AT_IntelOclBicc:
60935b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner    return AttributedType::attr_inteloclbicc;
6094e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  case AttributeList::AT_MSABI:
6095e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    return AttributedType::attr_ms_abi;
6096e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  case AttributeList::AT_SysVABI:
6097e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis    return AttributedType::attr_sysv_abi;
60984967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case AttributeList::AT_PreserveMost:
60994967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    return AttributedType::attr_preserve_most;
61004967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  case AttributeList::AT_PreserveAll:
61014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    return AttributedType::attr_preserve_all;
61025b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  }
61035b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  llvm_unreachable("unexpected attribute kind!");
61045b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner}
61055b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner
6106711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
6107711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
6108711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
6109711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
6110711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
6111711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
6112e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
6113711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
61142455636163fdd18581d7fdae816433f886d88213Mike Stump
61158e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NoReturn) {
6116711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
611704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
6118e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
6119e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
6120711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
6121711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
6122425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
6123425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
6124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
6125711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
6126711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
6127711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
6128425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
6129f85e193739c953358c865005855253af4f68a497John McCall  // ns_returns_retained is not always a type attribute, but if we got
6130f85e193739c953358c865005855253af4f68a497John McCall  // here, we're treating it as one right now.
61318e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NSReturnsRetained) {
61324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(S.getLangOpts().ObjCAutoRefCount &&
6133f85e193739c953358c865005855253af4f68a497John McCall           "ns_returns_retained treated as type attribute in non-ARC");
6134f85e193739c953358c865005855253af4f68a497John McCall    if (attr.getNumArgs()) return true;
6135f85e193739c953358c865005855253af4f68a497John McCall
6136f85e193739c953358c865005855253af4f68a497John McCall    // Delay if this is not a function type.
6137f85e193739c953358c865005855253af4f68a497John McCall    if (!unwrapped.isFunctionType())
6138f85e193739c953358c865005855253af4f68a497John McCall      return false;
6139f85e193739c953358c865005855253af4f68a497John McCall
6140f85e193739c953358c865005855253af4f68a497John McCall    FunctionType::ExtInfo EI
6141f85e193739c953358c865005855253af4f68a497John McCall      = unwrapped.get()->getExtInfo().withProducesResult(true);
6142f85e193739c953358c865005855253af4f68a497John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
6143f85e193739c953358c865005855253af4f68a497John McCall    return true;
6144f85e193739c953358c865005855253af4f68a497John McCall  }
6145f85e193739c953358c865005855253af4f68a497John McCall
61468e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_Regparm) {
6147711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
6148711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
6149711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
61501e030eb1194763b42c1752723be23b1515f48981John McCall
6151711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
6152711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
6153008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
61541e030eb1194763b42c1752723be23b1515f48981John McCall
6155ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Diagnose regparm with fastcall.
6156ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    const FunctionType *fn = unwrapped.get();
6157ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    CallingConv CC = fn->getCallConv();
6158ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (CC == CC_X86FastCall) {
6159ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
6160ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC)
6161ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm";
6162ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
6163ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
6164ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
6165ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
616691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    FunctionType::ExtInfo EI =
6167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
6168711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
6169711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
6170425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
6171425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
617282bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  // Delay if the type didn't work out to a function.
617382bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  if (!unwrapped.isFunctionType()) return false;
617482bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman
617504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
6176711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
6177711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
6178711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
6179f82b4e85b1219295cad4b5851b035575bc293010John McCall
6180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
6181711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
6182ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  AttributedType::Kind CCAttrKind = getCCTypeAttrKind(attr);
618304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
6184e8519c31a6ef853b627d557702ac1890f18ce2c9Charles Davis  if (CCOld != CC) {
6185ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // Error out on when there's already an attribute on the type
6186ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    // and the CCs don't match.
6187ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    const AttributedType *AT = S.getCallingConvAttributedType(type);
6188ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    if (AT && AT->getAttrKind() != CCAttrKind) {
6189ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
6190ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        << FunctionType::getNameForCallConv(CC)
6191ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner        << FunctionType::getNameForCallConv(CCOld);
6192ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      attr.setInvalid();
6193ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner      return true;
6194ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner    }
619504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
619604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
61974967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // Diagnose use of variadic functions with calling conventions that
61984967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // don't support them (e.g. because they're callee-cleanup).
61994967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // We delay warning about this on unprototyped function declarations
62004967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // until after redeclaration checking, just in case we pick up a
62014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // prototype that way.  And apparently we also "delay" warning about
62024967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // unprototyped function types in general, despite not necessarily having
62034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // much ability to diagnose it later.
6204176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (!supportsVariadicCall(CC)) {
620545d3950e373412f395413c81a0310e8090508608Hans Wennborg    const FunctionProtoType *FnP = dyn_cast<FunctionProtoType>(fn);
620645d3950e373412f395413c81a0310e8090508608Hans Wennborg    if (FnP && FnP->isVariadic()) {
620745d3950e373412f395413c81a0310e8090508608Hans Wennborg      unsigned DiagID = diag::err_cconv_varargs;
62084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
620945d3950e373412f395413c81a0310e8090508608Hans Wennborg      // stdcall and fastcall are ignored with a warning for GCC and MS
621045d3950e373412f395413c81a0310e8090508608Hans Wennborg      // compatibility.
62114967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      bool IsInvalid = true;
62124967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (CC == CC_X86StdCall || CC == CC_X86FastCall) {
621345d3950e373412f395413c81a0310e8090508608Hans Wennborg        DiagID = diag::warn_cconv_varargs;
62144967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        IsInvalid = false;
62154967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      }
621645d3950e373412f395413c81a0310e8090508608Hans Wennborg
621745d3950e373412f395413c81a0310e8090508608Hans Wennborg      S.Diag(attr.getLoc(), DiagID) << FunctionType::getNameForCallConv(CC);
62184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      if (IsInvalid) attr.setInvalid();
6219711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
622004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
622145d3950e373412f395413c81a0310e8090508608Hans Wennborg  }
622204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
6223176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  // Also diagnose fastcall with regparm.
6224176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  if (CC == CC_X86FastCall && fn->getHasRegParm()) {
6225176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
6226176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        << "regparm" << FunctionType::getNameForCallConv(CC_X86FastCall);
6227176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    attr.setInvalid();
6228176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return true;
622904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
6230f82b4e85b1219295cad4b5851b035575bc293010John McCall
62315b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  // Modify the CC from the wrapped function type, wrap it all back, and then
62325b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  // wrap the whole thing in an AttributedType as written.  The modified type
62335b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner  // might have a different CC if we ignored the attribute.
62344967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  QualType Equivalent;
62354967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if (CCOld == CC) {
62364967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    Equivalent = type;
62374967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  } else {
62384967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    auto EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
62394967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    Equivalent =
62405b92696c8f1f8ef943ad87397b95c031b5787305Reid Kleckner      unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
62414967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
6242ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  type = S.Context.getAttributedType(CCAttrKind, type, Equivalent);
6243711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
6244f82b4e85b1219295cad4b5851b035575bc293010John McCall}
6245f82b4e85b1219295cad4b5851b035575bc293010John McCall
6246651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesbool Sema::hasExplicitCallingConv(QualType &T) {
6247651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType R = T.IgnoreParens();
6248651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  while (const AttributedType *AT = dyn_cast<AttributedType>(R)) {
6249651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (AT->isCallingConv())
6250651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return true;
6251651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    R = AT->getModifiedType().IgnoreParens();
6252651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
6253651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return false;
6254651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
6255651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
625687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarvoid Sema::adjustMemberFunctionCC(QualType &T, bool IsStatic, bool IsCtorOrDtor,
625787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                  SourceLocation Loc) {
6258651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  FunctionTypeUnwrapper Unwrapped(*this, T);
6259651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const FunctionType *FT = Unwrapped.get();
6260ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner  bool IsVariadic = (isa<FunctionProtoType>(FT) &&
6261ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner                     cast<FunctionProtoType>(FT)->isVariadic());
6262651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallingConv CurCC = FT->getCallConv();
6263651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallingConv ToCC = Context.getDefaultCallingConvention(IsVariadic, !IsStatic);
6264ef072033876e295ec5d3402f8730a3ae358ad815Reid Kleckner
626587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (CurCC == ToCC)
6266207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
6267651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
626887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // MS compiler ignores explicit calling convention attributes on structors. We
626987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // should do the same.
627087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (Context.getTargetInfo().getCXXABI().isMicrosoft() && IsCtorOrDtor) {
627187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Issue a warning on ignored calling convention -- except of __stdcall.
627287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Again, this is what MS compiler does.
627387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (CurCC != CC_X86StdCall)
627487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      Diag(Loc, diag::warn_cconv_structors)
627587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << FunctionType::getNameForCallConv(CurCC);
627687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Default adjustment.
627787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  } else {
627887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // Only adjust types with the default convention.  For example, on Windows
627987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // we should adjust a __cdecl type to __thiscall for instance methods, and a
628087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    // __thiscall type to __cdecl for static methods.
628187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    CallingConv DefaultCC =
628287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        Context.getDefaultCallingConvention(IsVariadic, IsStatic);
628387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
628487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (CurCC != DefaultCC || DefaultCC == ToCC)
628587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return;
628687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
628787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (hasExplicitCallingConv(T))
628887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return;
628987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
629087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
6291651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  FT = Context.adjustFunctionType(FT, FT->getExtInfo().withCallingConv(ToCC));
6292651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType Wrapped = Unwrapped.wrap(*this, FT);
6293651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  T = Context.getAdjustedType(T, Wrapped);
6294207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
6295207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
62966e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
62976e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
62986e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
62996e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
63006e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
63016e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
63026e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
6303788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
6304788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
630556affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
63066e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
6307baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
6308baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
6309e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
63106e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
63116e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
6312624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Expr *sizeExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
63136e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
6314ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
6315ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
63169f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
63179f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
63189f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << sizeExpr->getSourceRange();
6319e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
63206e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
63216e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
6322b445cb9662067b7c7586815937b07828ede9bb49Aaron Ballman  // The base type must be integer (not Boolean or enumeration) or float, and
6323b445cb9662067b7c7586815937b07828ede9bb49Aaron Ballman  // can't already be a vector.
6324b445cb9662067b7c7586815937b07828ede9bb49Aaron Ballman  if (!CurType->isBuiltinType() || CurType->isBooleanType() ||
63251652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
63266e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
6327e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
63286e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
63296e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
63306e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
63316e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
63326e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
63336e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
63346e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
63356e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
63366e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
63376e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
6338e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
63396e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
63406e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
63411652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman  if (VectorType::isVectorSizeTooLarge(vectorSize / typeSize)) {
63421652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    S.Diag(Attr.getLoc(), diag::err_attribute_size_too_large)
63431652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman      << sizeExpr->getSourceRange();
63441652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    Attr.setInvalid();
63451652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman    return;
63461652ed1cd2cb63e0d0cb74c67a40d9dc5cab6b89Eli Friedman  }
63476e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
63486e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
63496e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
6350e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
63516e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
63526e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
63536e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
63546e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
63556e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
6356788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
6357e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
63586e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
63596e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
63604ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// \brief Process the OpenCL-like ext_vector_type attribute when it occurs on
63614ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// a type.
636291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierstatic void HandleExtVectorTypeAttr(QualType &CurType,
636391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                                    const AttributeList &Attr,
63644ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                    Sema &S) {
6365624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  // check the attribute arguments.
6366624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (Attr.getNumArgs() != 1) {
6367624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
6368624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman      << Attr.getName() << 1;
6369624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    return;
6370624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  }
6371624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman
63724ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  Expr *sizeExpr;
637391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
63744ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Special case where the argument is a template id.
6375624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  if (Attr.isArgIdent(0)) {
63764ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CXXScopeSpec SS;
6377e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    SourceLocation TemplateKWLoc;
63784ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    UnqualifiedId id;
6379624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    id.setIdentifier(Attr.getArgAsIdent(0)->Ident, Attr.getLoc());
6380e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
6381e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    ExprResult Size = S.ActOnIdExpression(S.getCurScope(), SS, TemplateKWLoc,
6382e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          id, false, false);
63834ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Size.isInvalid())
63844ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
638591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
63864ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Size.get();
63874ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  } else {
6388624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman    sizeExpr = Attr.getArgAsExpr(0);
63894ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  }
639091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
63914ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Create the vector type.
63924ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  QualType T = S.BuildExtVectorType(CurType, sizeExpr, Attr.getLoc());
63934ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (!T.isNull())
63944ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CurType = T;
63954ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor}
63964ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
6397b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northoverstatic bool isPermittedNeonBaseType(QualType &Ty,
6398651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                    VectorType::VectorKind VecKind, Sema &S) {
6399b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  const BuiltinType *BTy = Ty->getAs<BuiltinType>();
6400b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  if (!BTy)
6401b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    return false;
6402b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
6403651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Triple Triple = S.Context.getTargetInfo().getTriple();
6404651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
6405651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Signed poly is mathematically wrong, but has been baked into some ABIs by
6406651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // now.
6407651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool IsPolyUnsigned = Triple.getArch() == llvm::Triple::aarch64 ||
6408176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                        Triple.getArch() == llvm::Triple::aarch64_be;
6409b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  if (VecKind == VectorType::NeonPolyVector) {
6410651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (IsPolyUnsigned) {
6411624bb5e59dbcc24efeee7dff12c9b48d2b5077e9Kevin Qin      // AArch64 polynomial vectors are unsigned and support poly64.
6412b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      return BTy->getKind() == BuiltinType::UChar ||
6413624bb5e59dbcc24efeee7dff12c9b48d2b5077e9Kevin Qin             BTy->getKind() == BuiltinType::UShort ||
6414651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines             BTy->getKind() == BuiltinType::ULong ||
6415624bb5e59dbcc24efeee7dff12c9b48d2b5077e9Kevin Qin             BTy->getKind() == BuiltinType::ULongLong;
6416b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    } else {
6417b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      // AArch32 polynomial vector are signed.
6418b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover      return BTy->getKind() == BuiltinType::SChar ||
6419b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover             BTy->getKind() == BuiltinType::Short;
6420b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    }
6421b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  }
6422b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
6423b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  // Non-polynomial vector types: the usual suspects are allowed, as well as
6424b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  // float64_t on AArch64.
6425651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool Is64Bit = Triple.getArch() == llvm::Triple::aarch64 ||
6426176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                 Triple.getArch() == llvm::Triple::aarch64_be;
6427651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
6428651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (Is64Bit && BTy->getKind() == BuiltinType::Double)
6429b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover    return true;
6430b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
6431b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover  return BTy->getKind() == BuiltinType::SChar ||
6432b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::UChar ||
6433b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Short ||
6434b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::UShort ||
6435b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Int ||
6436b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::UInt ||
6437651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines         BTy->getKind() == BuiltinType::Long ||
6438651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines         BTy->getKind() == BuiltinType::ULong ||
6439b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::LongLong ||
6440b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::ULongLong ||
6441b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Float ||
6442b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover         BTy->getKind() == BuiltinType::Half;
6443b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover}
6444b793f0d3448a15277cd6b6cc4ba558ded39a8084Tim Northover
64454211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
64464211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
64474211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
64484211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
64494211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
64504211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
64514211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
64524211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
64534211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
64549f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman                                     VectorType::VectorKind VecKind) {
64552440fb1f91557912f8c43cb72201170254ae09f4Amara Emerson  // Target must have NEON
64562440fb1f91557912f8c43cb72201170254ae09f4Amara Emerson  if (!S.Context.getTargetInfo().hasFeature("neon")) {
64572440fb1f91557912f8c43cb72201170254ae09f4Amara Emerson    S.Diag(Attr.getLoc(), diag::err_attribute_unsupported) << Attr.getName();
64582440fb1f91557912f8c43cb72201170254ae09f4Amara Emerson    Attr.setInvalid();
64592440fb1f91557912f8c43cb72201170254ae09f4Amara Emerson    return;
64602440fb1f91557912f8c43cb72201170254ae09f4Amara Emerson  }
64614211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
64624211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
6463baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
6464baec77865b3ce64bb942dddc5dc2fada84ce5099Aaron Ballman      << Attr.getName() << 1;
64654211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
64664211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
64674211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
64684211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
6469624421f98d8fcb8ed8ebc406da41217682159aa8Aaron Ballman  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
64704211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
64714211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
64724211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
64739f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman    S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
64749f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << Attr.getName() << AANT_ArgumentIntegerConstant
64759f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman      << numEltsExpr->getSourceRange();
64764211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
64774211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
64784211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
64794211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
6480651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (!isPermittedNeonBaseType(CurType, VecKind, S)) {
6481e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
6482e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover    Attr.setInvalid();
6483e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover    return;
64844211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
6485e2d20c9a539cdfa75db6382eb5a327a73e5e0e1bTim Northover
64864211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
64874211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
64884211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
64894211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
64904211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
64914211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
64924211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
64934211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
64944211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
64954211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
64964211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
64974211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
64984211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
64994967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar/// Handle OpenCL Access Qualifier Attribute.
65004967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainarstatic void HandleOpenCLAccessAttr(QualType &CurType, const AttributeList &Attr,
65014967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar                                   Sema &S) {
65024967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // OpenCL v2.0 s6.6 - Access qualifier can be used only for image and pipe type.
65034967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if (!(CurType->isImageType() || CurType->isPipeType())) {
65044967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    S.Diag(Attr.getLoc(), diag::err_opencl_invalid_access_qualifier);
65054967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    Attr.setInvalid();
65064967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    return;
65074967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
65084967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
65094967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if (const TypedefType* TypedefTy = CurType->getAs<TypedefType>()) {
65104967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    QualType PointeeTy = TypedefTy->desugar();
65114967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    S.Diag(Attr.getLoc(), diag::err_opencl_multiple_access_qualifiers);
65124967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
65134967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    std::string PrevAccessQual;
65144967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    switch (cast<BuiltinType>(PointeeTy.getTypePtr())->getKind()) {
65154967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
65164967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case BuiltinType::Id:                                          \
65174967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      PrevAccessQual = #Access;                                    \
65184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      break;
65194967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      #include "clang/Basic/OpenCLImageTypes.def"
65204967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    default:
65214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      assert(0 && "Unable to find corresponding image type.");
65224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    }
65234967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
65244967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    S.Diag(TypedefTy->getDecl()->getLocStart(),
65254967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar       diag::note_opencl_typedef_access_qualifier) << PrevAccessQual;
65264967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  }
65274967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar}
65284967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
6529711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
6530f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             TypeAttrLocation TAL, AttributeList *attrs) {
6531c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
6532c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
6533c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
6534c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
6535711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
653687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  bool hasOpenCLAddressSpace = false;
653787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  while (attrs) {
6538711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
653987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    attrs = attr.getNext(); // reset to the next here due to early loop continue
654087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                            // stmts
6541711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
6542e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
6543711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
6544e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
6545e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
6546cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    if (attr.isCXX11Attribute()) {
6547cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // [[gnu::...]] attributes are treated as declaration attributes, so may
6548cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // not appertain to a DeclaratorChunk, even if we handle them as type
6549cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // attributes.
6550cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.getScopeName() && attr.getScopeName()->isStr("gnu")) {
6551cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        if (TAL == TAL_DeclChunk) {
6552cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          state.getSema().Diag(attr.getLoc(),
6553cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                               diag::warn_cxx11_gnu_attribute_on_type)
6554cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith              << attr.getName();
6555cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          continue;
6556cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        }
6557cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      } else if (TAL != TAL_DeclChunk) {
6558cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // Otherwise, only consider type processing for a C++11 attribute if
6559cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // it's actually been applied to a type.
6560cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        continue;
6561cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      }
6562f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    }
6563f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
6564b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
6565b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
6566711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
6567cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    default:
6568cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // A C++11 attribute on a declarator chunk must appertain to a type.
6569d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk) {
6570cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(), diag::err_attribute_not_type_attr)
6571d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith          << attr.getName();
6572d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith        attr.setUsedAsTypeAttr();
6573d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      }
6574cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
6575cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
6576cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::UnknownAttribute:
6577cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk)
6578cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(),
6579cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                             diag::warn_unknown_attribute_ignored)
6580cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          << attr.getName();
6581cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
6582cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
6583cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::IgnoredAttribute:
6584cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
658504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
65868e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_MayAlias:
6587682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // FIXME: This attribute needs to actually be handled, but if we ignore
6588682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // it it breaks large amounts of Linux software.
6589682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      attr.setUsedAsTypeAttr();
6590682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      break;
6591651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case AttributeList::AT_OpenCLPrivateAddressSpace:
6592651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case AttributeList::AT_OpenCLGlobalAddressSpace:
6593651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case AttributeList::AT_OpenCLLocalAddressSpace:
6594651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case AttributeList::AT_OpenCLConstantAddressSpace:
65950e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    case AttributeList::AT_OpenCLGenericAddressSpace:
65968e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_AddressSpace:
6597711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
6598e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
659987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      hasOpenCLAddressSpace = true;
6600c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
6601711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
6602711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
6603711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
6604e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
6605d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
66068e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_VectorSize:
6607711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
6608e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
6609f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
66108e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ExtVectorType:
6611a4fa9008988985f9cf01712a99ddd923aea278a0Richard Smith      HandleExtVectorTypeAttr(type, attr, state.getSema());
6612e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
66134ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      break;
66148e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonVectorType:
6615711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
66169f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman                               VectorType::NeonVector);
6617e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
66184211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
66198e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonPolyVectorType:
6620711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
66219f939f75c64770c746d78579f75a49f9c657e426Aaron Ballman                               VectorType::NeonPolyVector);
6622e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
66234211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
66244967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    case AttributeList::AT_OpenCLAccess:
66254967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      HandleOpenCLAccessAttr(type, attr, state.getSema());
6626e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
6627207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      break;
6628207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
6629aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman    MS_TYPE_ATTRS_CASELIST:
6630aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      if (!handleMSPointerTypeQualifierAttr(state, attr, type))
6631aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman        attr.setUsedAsTypeAttr();
6632aa9df09729fb8aee3e645549e95fcb413306a7aaAaron Ballman      break;
6633d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith
663487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
663587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    NULLABILITY_TYPE_ATTRS_CASELIST:
663687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Either add nullability here or try to distribute it.  We
663787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // don't want to distribute the nullability specifier past any
663887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // dependent type, because that complicates the user model.
663987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (type->canHaveNullability() || type->isDependentType() ||
664087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          !distributeNullabilityTypeAttr(state, type, attr)) {
664187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (state.getSema().checkNullabilityTypeSpecifier(
664287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              type,
664387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              mapNullabilityAttrKind(attr.getKind()),
664487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              attr.getLoc(),
664587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              attr.isContextSensitiveKeywordAttribute())) {
664687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          attr.setInvalid();
664787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        }
664887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
664987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        attr.setUsedAsTypeAttr();
665087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
665187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
665287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
665387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    case AttributeList::AT_ObjCKindOf:
665487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // '__kindof' must be part of the decl-specifiers.
665587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      switch (TAL) {
665687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case TAL_DeclSpec:
665787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        break;
665887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
665987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case TAL_DeclChunk:
666087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      case TAL_DeclName:
666187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        state.getSema().Diag(attr.getLoc(),
666287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                             diag::err_objc_kindof_wrong_position)
666387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << FixItHint::CreateRemoval(attr.getLoc())
666487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          << FixItHint::CreateInsertion(
666587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar               state.getDeclarator().getDeclSpec().getLocStart(), "__kindof ");
666687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        break;
666787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
666887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
666987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Apply it regardless.
667087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (state.getSema().checkObjCKindOfType(type, attr.getLoc()))
667187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        attr.setInvalid();
667287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      attr.setUsedAsTypeAttr();
667387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      break;
667487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
66758e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
66764e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
6677cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        break;
6678f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough into the function attrs
6679f85e193739c953358c865005855253af4f68a497John McCall
6680711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
6681e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
6682e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
6683711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
6684711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
6685f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      if (TAL == TAL_DeclSpec)
6686711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
6687711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
6688711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
6689711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
6690711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
66916e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
6692c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
669387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
669487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
669587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // If address space is not set, OpenCL 2.0 defines non private default
669687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // address spaces for some cases:
669787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // OpenCL 2.0, section 6.5:
669887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // The address space for a variable at program scope or a static variable
669987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // inside a function can either be __global or __constant, but defaults to
670087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // __global if not specified.
670187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // (...)
670287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // Pointers that are declared without pointing to a named address space point
670387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // to the generic address space.
670487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (state.getSema().getLangOpts().OpenCLVersion >= 200 &&
670587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      !hasOpenCLAddressSpace && type.getAddressSpace() == 0 &&
670687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      (TAL == TAL_DeclSpec || TAL == TAL_DeclChunk)) {
670787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    Declarator &D = state.getDeclarator();
670887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (state.getCurrentChunkIndex() > 0 &&
670987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        D.getTypeObject(state.getCurrentChunkIndex() - 1).Kind ==
671087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            DeclaratorChunk::Pointer) {
671187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      type = state.getSema().Context.getAddrSpaceQualType(
671287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          type, LangAS::opencl_generic);
671387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    } else if (state.getCurrentChunkIndex() == 0 &&
671487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar               D.getContext() == Declarator::FileContext &&
671587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar               !D.isFunctionDeclarator() && !D.isFunctionDefinition() &&
671687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar               D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
671787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar               !type->isSamplerT())
671887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      type = state.getSema().Context.getAddrSpaceQualType(
671987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          type, LangAS::opencl_global);
672087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    else if (state.getCurrentChunkIndex() == 0 &&
672187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             D.getContext() == Declarator::BlockContext &&
672287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar             D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)
672387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      type = state.getSema().Context.getAddrSpaceQualType(
672487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          type, LangAS::opencl_global);
672587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
672687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar}
672787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
672887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarvoid Sema::completeExprArrayBound(Expr *E) {
672987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
673087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
673187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (isTemplateInstantiation(Var->getTemplateSpecializationKind())) {
673287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        SourceLocation PointOfInstantiation = E->getExprLoc();
673387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
673487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (MemberSpecializationInfo *MSInfo =
673587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                Var->getMemberSpecializationInfo()) {
673687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          // If we don't already have a point of instantiation, this is it.
673787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          if (MSInfo->getPointOfInstantiation().isInvalid()) {
673887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            MSInfo->setPointOfInstantiation(PointOfInstantiation);
673987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
674087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            // This is a modification of an existing AST node. Notify
674187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            // listeners.
674287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            if (ASTMutationListener *L = getASTMutationListener())
674387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              L->StaticDataMemberInstantiated(Var);
674487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          }
674587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        } else {
674687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          VarTemplateSpecializationDecl *VarSpec =
674787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar              cast<VarTemplateSpecializationDecl>(Var);
674887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          if (VarSpec->getPointOfInstantiation().isInvalid())
674987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            VarSpec->setPointOfInstantiation(PointOfInstantiation);
675087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        }
675187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
675287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        InstantiateVariableDefinition(PointOfInstantiation, Var);
675387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
675487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // Update the type to the newly instantiated definition's type both
675587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // here and within the expression.
675687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (VarDecl *Def = Var->getDefinition()) {
675787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          DRE->setDecl(Def);
675887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          QualType T = Def->getType();
675987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          DRE->setType(T);
676087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          // FIXME: Update the type on all intervening expressions.
676187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          E->setType(T);
676287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        }
676387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
676487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // We still go on to try to complete the type independently, as it
676587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // may also require instantiations or diagnostics if it remains
676687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        // incomplete.
676787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
676887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
676987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
6770232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
6771232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
6772e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \brief Ensure that the type of the given expression is complete.
6773e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
6774e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// This routine checks whether the expression \p E has a complete type. If the
6775e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// expression refers to an instantiable construct, that instantiation is
6776e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// performed as needed to complete its type. Furthermore
6777e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// Sema::RequireCompleteType is called for the expression's type (or in the
6778e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// case of a reference type, the referred-to type).
6779e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
6780e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \param E The expression whose type is required to be complete.
6781d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// \param Diagnoser The object that will emit a diagnostic if the type is
6782d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// incomplete.
6783e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
6784e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \returns \c true if the type of \p E is incomplete and diagnosed, \c false
6785e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// otherwise.
678687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarbool Sema::RequireCompleteExprType(Expr *E, TypeDiagnoser &Diagnoser) {
6787e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  QualType T = E->getType();
6788e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
6789e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Incomplete array types may be completed by the initializer attached to
67906aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith  // their definitions. For static data members of class templates and for
67916aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith  // variable templates, we need to instantiate the definition to get this
67926aa7df9df93bcf2d6399f6e535ef74c132db40ecRichard Smith  // initializer and complete the type.
6793e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (T->isIncompleteArrayType()) {
679487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    completeExprArrayBound(E);
679587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    T = E->getType();
6796e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  }
6797e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
6798e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // FIXME: Are there other cases which require instantiating something other
6799e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // than the type to complete the type of an expression?
6800e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
6801d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(E->getExprLoc(), T, Diagnoser);
6802d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
6803d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
6804d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, unsigned DiagID) {
680587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  BoundTypeDiagnoser<> Diagnoser(DiagID);
6806d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteExprType(E, Diagnoser);
6807e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth}
6808e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
68091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
68104ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
68114ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
68124ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
681386447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
681486447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
681586447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
681686447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
681786447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
68184ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
68194ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
68204ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
68214ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
68224ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
68234ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
68244ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
68254ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
682691a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
6827f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                               TypeDiagnoser &Diagnoser) {
682887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (RequireCompleteTypeImpl(Loc, T, &Diagnoser))
6829658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie    return true;
6830658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  if (const TagType *Tag = T->getAs<TagType>()) {
6831658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie    if (!Tag->getDecl()->isCompleteDefinitionRequired()) {
6832658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie      Tag->getDecl()->setCompleteDefinitionRequired();
6833658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie      Consumer.HandleTagDeclRequiredDefinition(Tag->getDecl());
6834658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie    }
6835658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  }
6836658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie  return false;
6837658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie}
6838658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie
68396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \brief Determine whether there is any declaration of \p D that was ever a
68406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        definition (perhaps before module merging) and is currently visible.
68416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param D The definition of the entity.
68426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// \param Suggested Filled in with the declaration that should be made visible
68436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///        in order to provide a definition of this entity.
684487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar/// \param OnlyNeedComplete If \c true, we only need the type to be complete,
684587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar///        not defined. This only matters for enums with a fixed underlying
684687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar///        type, since in all other cases, a type is complete if and only if it
684787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar///        is defined.
684887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainarbool Sema::hasVisibleDefinition(NamedDecl *D, NamedDecl **Suggested,
684987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                bool OnlyNeedComplete) {
68506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // Easy case: if we don't have modules, all declarations are visible.
6851b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  if (!getLangOpts().Modules && !getLangOpts().ModulesLocalVisibility)
68526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return true;
68536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
68546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // If this definition was instantiated from a template, map back to the
68556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // pattern from which it was instantiated.
6856b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  if (isa<TagDecl>(D) && cast<TagDecl>(D)->isBeingDefined()) {
6857b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    // We're in the middle of defining it; this definition should be treated
6858b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    // as visible.
6859b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    return true;
6860b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  } else if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
6861176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (auto *Pattern = RD->getTemplateInstantiationPattern())
6862176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      RD = Pattern;
68636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    D = RD->getDefinition();
68646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  } else if (auto *ED = dyn_cast<EnumDecl>(D)) {
68654967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    if (auto *Pattern = ED->getTemplateInstantiationPattern())
68664967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      ED = Pattern;
686787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (OnlyNeedComplete && ED->isFixed()) {
686887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // If the enum has a fixed underlying type, and we're only looking for a
686987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // complete type (not a definition), any visible declaration of it will
687087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // do.
6871c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      *Suggested = nullptr;
68726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      for (auto *Redecl : ED->redecls()) {
687387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (isVisible(Redecl))
68746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          return true;
68756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (Redecl->isThisDeclarationADefinition() ||
68766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            (Redecl->isCanonicalDecl() && !*Suggested))
68776bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          *Suggested = Redecl;
68786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      }
68796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return false;
68806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    }
68816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    D = ED->getDefinition();
68826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
68836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  assert(D && "missing definition for pattern of instantiated definition");
68846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
68856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  *Suggested = D;
688687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (isVisible(D))
688787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return true;
688887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
688987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // The external source may have additional definitions of this type that are
689087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // visible, so complete the redeclaration chain now and ask again.
689187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (auto *Source = Context.getExternalSource()) {
689287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    Source->CompleteRedeclChain(D);
689387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    return isVisible(D);
689487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
689587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
689687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  return false;
68976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
68986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
6899c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines/// Locks in the inheritance model for the given class and all of its bases.
6900c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hinesstatic void assignInheritanceModel(Sema &S, CXXRecordDecl *RD) {
6901c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  RD = RD->getMostRecentDecl();
6902c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  if (!RD->hasAttr<MSInheritanceAttr>()) {
6903c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    MSInheritanceAttr::Spelling IM;
6904c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
6905c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    switch (S.MSPointerToMemberRepresentationMethod) {
6906c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    case LangOptions::PPTMK_BestCase:
6907c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      IM = RD->calculateInheritanceModel();
6908c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      break;
6909c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    case LangOptions::PPTMK_FullGeneralitySingleInheritance:
6910c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      IM = MSInheritanceAttr::Keyword_single_inheritance;
6911c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      break;
6912c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    case LangOptions::PPTMK_FullGeneralityMultipleInheritance:
6913c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      IM = MSInheritanceAttr::Keyword_multiple_inheritance;
6914c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      break;
6915c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    case LangOptions::PPTMK_FullGeneralityVirtualInheritance:
6916c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      IM = MSInheritanceAttr::Keyword_unspecified_inheritance;
6917c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines      break;
6918c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    }
6919c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
6920c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    RD->addAttr(MSInheritanceAttr::CreateImplicit(
6921c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        S.getASTContext(), IM,
6922c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        /*BestCase=*/S.MSPointerToMemberRepresentationMethod ==
6923c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines            LangOptions::PPTMK_BestCase,
6924c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        S.ImplicitMSInheritanceAttrLoc.isValid()
6925c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines            ? S.ImplicitMSInheritanceAttrLoc
6926c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines            : RD->getSourceRange()));
69274967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    S.Consumer.AssignInheritanceModel(RD);
6928c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  }
6929c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines}
6930c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
6931658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie/// \brief The implementation of RequireCompleteType
6932658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikiebool Sema::RequireCompleteTypeImpl(SourceLocation Loc, QualType T,
693387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                   TypeDiagnoser *Diagnoser) {
6934573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
6935573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
6936690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
6937690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
6938690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
69391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
6940690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
6941690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
694287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // We lock in the inheritance model once somebody has asked us to ensure
694387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // that a pointer-to-member type is complete.
694487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (Context.getTargetInfo().getCXXABI().isMicrosoft()) {
694587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>()) {
694687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (!MPTy->getClass()->isDependentType()) {
694787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        (void)isCompleteType(Loc, QualType(MPTy->getClass(), 0));
694887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        assignInheritanceModel(*this, MPTy->getMostRecentCXXRecordDecl());
694987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
695087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
695187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  }
695287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
69536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  NamedDecl *Def = nullptr;
69544967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  bool Incomplete = T->isIncompleteType(&Def);
69554967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
69564967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // Check that any necessary explicit specializations are visible. For an
69574967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // enum, we just need the declaration, so don't check this.
69584967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if (Def && !isa<EnumDecl>(Def))
69594967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar    checkSpecializationVisibility(Loc, Def);
69604967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
69614967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // If we have a complete type, we're done.
69624967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if (!Incomplete) {
6963d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    // If we know about the definition but it is not visible, complain.
6964c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    NamedDecl *SuggestedDef = nullptr;
696587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (Def &&
696687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        !hasVisibleDefinition(Def, &SuggestedDef, /*OnlyNeedComplete*/true)) {
696787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // If the user is going to see an error here, recover by making the
696887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // definition visible.
696987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      bool TreatAsComplete = Diagnoser && !isSFINAEContext();
697087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (Diagnoser)
69714967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar        diagnoseMissingImport(Loc, SuggestedDef, MissingImportKind::Definition,
697287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                              /*Recover*/TreatAsComplete);
697387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      return !TreatAsComplete;
6974651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
6975651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
69764ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
6977d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
69784ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
69796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const TagType *Tag = T->getAs<TagType>();
69806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const ObjCInterfaceType *IFace = T->getAs<ObjCInterfaceType>();
69816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
69826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // If there's an unimported definition of this type in a module (for
6983b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // instance, because we forward declared it, then imported the definition),
6984b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // import that definition now.
69856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  //
6986b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // FIXME: What about other cases where an import extends a redeclaration
6987b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // chain for a declaration that can be accessed through a mechanism other
6988b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // than name lookup (eg, referenced in a template, or a variable whose type
6989b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  // could be completed by the module)?
699087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  //
699187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // FIXME: Should we map through to the base array element type before
699287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  // checking for a tag type?
69936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (Tag || IFace) {
69946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    NamedDecl *D =
69956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        Tag ? static_cast<NamedDecl *>(Tag->getDecl()) : IFace->getDecl();
6996b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith
6997bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
69986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (D->isInvalidDecl())
6999bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
7000bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan
7001bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
70026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (auto *Source = Context.getExternalSource()) {
70036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (Tag)
70046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        Source->CompleteType(Tag->getDecl());
70056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      else
70066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        Source->CompleteType(IFace->getDecl());
700791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
70086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // If the external source completed the type, go through the motions
70096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // again to ensure we're allowed to use the completed type.
70106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (!T->isIncompleteType())
70116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return RequireCompleteTypeImpl(Loc, T, Diagnoser);
7012bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
7013bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
701491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
7015d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
7016923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
7017923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
7018923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
7019e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor  while (const ConstantArrayType *Array
7020e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor           = Context.getAsConstantArrayType(MaybeTemplate))
7021923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
7022923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
702387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    bool Instantiated = false;
702487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    bool Diagnosed = false;
70252943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
7026d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
702787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
702887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        Diagnosed = InstantiateClassTemplateSpecialization(
702987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            Loc, ClassTemplateSpec, TSK_ImplicitInstantiation,
703087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            /*Complain=*/Diagnoser);
703187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        Instantiated = true;
703287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      }
70331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
7034d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
7035564f4c5664f552becbd05407611a92754c40e641Richard Smith      CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass();
7036564f4c5664f552becbd05407611a92754c40e641Richard Smith      if (!Rec->isBeingDefined() && Pattern) {
7037564f4c5664f552becbd05407611a92754c40e641Richard Smith        MemberSpecializationInfo *MSI = Rec->getMemberSpecializationInfo();
7038564f4c5664f552becbd05407611a92754c40e641Richard Smith        assert(MSI && "Missing member specialization information?");
7039357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
704087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        if (MSI->getTemplateSpecializationKind() !=
704187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar            TSK_ExplicitSpecialization) {
704287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          Diagnosed = InstantiateClass(Loc, Rec, Pattern,
704387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                       getTemplateInstantiationArgs(Rec),
704487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                       TSK_ImplicitInstantiation,
704587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                                       /*Complain=*/Diagnoser);
704687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar          Instantiated = true;
704787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        }
7048d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
7049d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
705087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
705187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    if (Instantiated) {
705287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // Instantiate* might have already complained that the template is not
705387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // defined, if we asked it to.
705487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (Diagnoser && Diagnosed)
705587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        return true;
705687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // If we instantiated a definition, check that it's usable, even if
705787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // instantiation produced an error, so that repeated calls to this
705887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      // function give consistent answers.
705987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      if (!T->isIncompleteType())
706087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar        return RequireCompleteTypeImpl(Loc, T, Diagnoser);
706187d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    }
7062d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
70632943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
70644967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // FIXME: If we didn't instantiate a definition because of an explicit
70654967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  // specialization declaration, check that it's visible.
7066d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
70674967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  if (!Diagnoser)
7068cac18add73d095eaab600aefe27ea7174aec4922Nico Weber    return true;
7069cac18add73d095eaab600aefe27ea7174aec4922Nico Weber
707087d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  Diagnoser->diagnose(*this, Loc, T);
707191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
70724ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
707301620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
70744ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
70751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
70764ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
70774ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
7078b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor      << QualType(Tag, 0);
707991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
7080b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  // If the Objective-C class was a forward declaration, produce a note.
7081b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  if (IFace && !IFace->getDecl()->isInvalidDecl())
7082b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor    Diag(IFace->getDecl()->getLocation(), diag::note_forward_class);
70834ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
70845d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain  // If we have external information that we can use to suggest a fix,
70855d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain  // produce a note.
70865d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain  if (ExternalSource)
70875d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain    ExternalSource->MaybeDiagnoseMissingCompleteType(Loc, T);
70885d937b3fe7832f8ffa0a258d1b037c64708e97c1Kaelyn Uhrain
70894ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
70904ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
7091e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
7092fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
709391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                               unsigned DiagID) {
709487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  BoundTypeDiagnoser<> Diagnoser(DiagID);
7095d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(Loc, T, Diagnoser);
7096fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
7097fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
70986666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \brief Get diagnostic %select index for tag kind for
70996666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// literal type diagnostic message.
71006666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// WARNING: Indexes apply to particular diagnostics only!
71016666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos///
71026666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \returns diagnostic %select index.
7103f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matosstatic unsigned getLiteralDiagFromTagKind(TagTypeKind Tag) {
71046666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  switch (Tag) {
7105f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Struct: return 0;
7106f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Interface: return 1;
7107f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Class:  return 2;
7108f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  default: llvm_unreachable("Invalid tag kind for literal type diagnostic!");
71096666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  }
71106666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos}
71116666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos
71129f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @brief Ensure that the type T is a literal type.
71139f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
71149f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// This routine checks whether the type @p T is a literal type. If @p T is an
71159f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// incomplete type, an attempt is made to complete it. If @p T is a literal
71169f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// type, or @p AllowIncompleteType is true and @p T is an incomplete type,
71179f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// returns false. Otherwise, this routine issues the diagnostic @p PD (giving
71189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// it the type @p T), along with notes explaining why the type is not a
71199f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// literal type, and returns true.
71209f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
71219f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param Loc  The location in the source that the non-literal type
71229f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// diagnostic should refer to.
71239f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
71249f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param T  The type that this routine is examining for literalness.
71259f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
7126f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor/// @param Diagnoser Emits a diagnostic if T is not a literal type.
71279f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
71289f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @returns @c true if @p T is not a literal type and a diagnostic was emitted,
71299f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @c false otherwise.
71309f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithbool Sema::RequireLiteralType(SourceLocation Loc, QualType T,
7131f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                              TypeDiagnoser &Diagnoser) {
71329f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  assert(!T->isDependentType() && "type should not be dependent");
71339f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
7134ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  QualType ElemType = Context.getBaseElementType(T);
713587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if ((isCompleteType(Loc, ElemType) || ElemType->isVoidType()) &&
713687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar      T->isLiteralType(Context))
71379f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return false;
71389f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
7139f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
71409f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
71419f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (T->isVariableArrayType())
71429f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
71439f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
7144ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  const RecordType *RT = ElemType->getAs<RecordType>();
71459f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (!RT)
71469f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
71479f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
71489f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
71499f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
7150c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // A partially-defined class type can't be a literal type, because a literal
7151c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // class type must have a trivial destructor (which can't be checked until
7152c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // the class definition is complete).
715387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T))
7154ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman    return true;
7155ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
71569f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // If the class has virtual base classes, then it's not an aggregate, and
715786c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // cannot have any constexpr constructors or a trivial default constructor,
715886c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // so is non-literal. This is better to diagnose than the resulting absence
715986c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // of constexpr constructors.
71609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (RD->getNumVBases()) {
71619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_virtual_base)
71626666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      << getLiteralDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases();
7163651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    for (const auto &I : RD->vbases())
7164651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag(I.getLocStart(), diag::note_constexpr_virtual_base_here)
7165651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          << I.getSourceRange();
716686c3ae46250cdcc57778c27826060779a92f3815Richard Smith  } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor() &&
716786c3ae46250cdcc57778c27826060779a92f3815Richard Smith             !RD->hasTrivialDefaultConstructor()) {
71689f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD;
71699f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (RD->hasNonLiteralTypeFieldsOrBases()) {
7170651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    for (const auto &I : RD->bases()) {
7171651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (!I.getType()->isLiteralType(Context)) {
7172651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        Diag(I.getLocStart(),
71739f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith             diag::note_non_literal_base_class)
7174651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          << RD << I.getType() << I.getSourceRange();
71759f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
71769f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
71779f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
7178651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    for (const auto *I : RD->fields()) {
7179a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith      if (!I->getType()->isLiteralType(Context) ||
7180262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          I->getType().isVolatileQualified()) {
7181262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        Diag(I->getLocation(), diag::note_non_literal_field)
7182651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          << RD << I << I->getType()
7183262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          << I->getType().isVolatileQualified();
71849f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
71859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
71869f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
71879f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (!RD->hasTrivialDestructor()) {
71889f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // All fields and bases are of literal types, so have trivial destructors.
71899f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // If this class's destructor is non-trivial it must be user-declared.
71909f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    CXXDestructorDecl *Dtor = RD->getDestructor();
71919f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    assert(Dtor && "class has literal fields and bases but no dtor?");
71929f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    if (!Dtor)
71939f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      return true;
71949f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
71959f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(Dtor->getLocation(), Dtor->isUserProvided() ?
71969f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_user_provided_dtor :
71979f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_nontrivial_dtor) << RD;
7198ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    if (!Dtor->isUserProvided())
7199ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith      SpecialMemberIsTrivial(Dtor, CXXDestructor, /*Diagnose*/true);
72009f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  }
72019f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
72029f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return true;
72039f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
72049f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
720591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierbool Sema::RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID) {
720687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  BoundTypeDiagnoser<> Diagnoser(DiagID);
7207f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  return RequireLiteralType(Loc, T, Diagnoser);
7208f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor}
7209f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor
7210465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
7211465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
7212465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
7213465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
7214465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
7215e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
7216465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
7217e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
7218651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    NNS = SS.getScopeRep();
7219465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
7220465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
7221465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
72226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    NNS = nullptr;
7223465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
7224465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
7225e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
7226af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
72272a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
7228fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
72292a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
7230c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  E = ER.get();
72312a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
723287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  if (!getLangOpts().CPlusPlus && E->refersToBitField())
723387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar    Diag(E->getExprLoc(), diag::err_sizeof_alignof_typeof_bitfield) << 2;
723487d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar
72352b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
72362b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
7237aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
7238aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
72392b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
7240af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
7241af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
7242af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
7243f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// getDecltypeForExpr - Given an expr, will return the decltype for
7244f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// that expression, according to the rules in C++11
7245f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18.
7246f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregorstatic QualType getDecltypeForExpr(Sema &S, Expr *E) {
7247f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (E->isTypeDependent())
7248f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    return S.Context.DependentTy;
7249f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
72506d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
72516d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   The type denoted by decltype(e) is defined as follows:
72526d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //
72536d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //     - if e is an unparenthesized id-expression or an unparenthesized class
725491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       member access (5.2.5), decltype(e) is the type of the entity named
725591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       by e. If there is no such entity, or if e names a set of overloaded
72566d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       functions, the program is ill-formed;
725784dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  //
725884dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  // We apply the same rules for Objective-C ivar and property references.
7259f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
7260f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
7261f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return VD->getType();
726284dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
7263f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
7264f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return FD->getType();
726584dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCIvarRefExpr *IR = dyn_cast<ObjCIvarRefExpr>(E)) {
726684dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    return IR->getDecl()->getType();
726784dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCPropertyRefExpr *PR = dyn_cast<ObjCPropertyRefExpr>(E)) {
726884dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    if (PR->isExplicitProperty())
726984dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor      return PR->getExplicitProperty()->getType();
7270176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  } else if (auto *PE = dyn_cast<PredefinedExpr>(E)) {
7271176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return PE->getType();
7272f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
727384dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor
7274f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  // C++11 [expr.lambda.prim]p18:
7275f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   Every occurrence of decltype((x)) where x is a possibly
7276f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   parenthesized id-expression that names an entity of automatic
7277f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   storage duration is treated as if x were transformed into an
7278f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   access to a corresponding data member of the closure type that
7279f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   would have been declared if x were an odr-use of the denoted
7280f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   entity.
7281f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  using namespace sema;
7282f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (S.getCurLambda()) {
7283f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (isa<ParenExpr>(E)) {
7284f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
7285f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
728668932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          QualType T = S.getCapturedDeclRefType(Var, DRE->getLocation());
728768932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          if (!T.isNull())
728868932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor            return S.Context.getLValueReferenceType(T);
7289f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        }
7290f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      }
7291f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    }
7292f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
7293f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
7294f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
72956d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
72966d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   [...]
72976d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  QualType T = E->getType();
72986d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  switch (E->getValueKind()) {
729991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the
73006d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
73016d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_XValue: T = S.Context.getRValueReferenceType(T); break;
730291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an lvalue, decltype(e) is T&, where T is the
73036d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
73046d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_LValue: T = S.Context.getLValueReferenceType(T); break;
73056d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //  - otherwise, decltype(e) is the type of e.
73066d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_RValue: break;
73076d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  }
730891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
7309f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return T;
7310f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor}
7311f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
73120e2c34f92f00628d48968dfea096d36381f494cbStephen HinesQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc,
73130e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                 bool AsUnevaluated) {
7314fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
73152a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
7316c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  E = ER.get();
731791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
73180e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (AsUnevaluated && ActiveTemplateInstantiations.empty() &&
73190e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      E->HasSideEffects(Context, false)) {
73200e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // The expression operand for decltype is in an unevaluated expression
73210e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    // context, so side effects could result in unintended consequences.
73220e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    Diag(E->getExprLoc(), diag::warn_side_effects_unevaluated_context);
73230e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
73240e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
7325f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return Context.getDecltypeType(E, getDecltypeForExpr(*this, E));
7326af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
7327ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
7328ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType Sema::BuildUnaryTransformType(QualType BaseType,
7329ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::UTTKind UKind,
7330ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       SourceLocation Loc) {
7331ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  switch (UKind) {
7332ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case UnaryTransformType::EnumUnderlyingType:
7333ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) {
7334ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Diag(Loc, diag::err_only_enums_have_underlying_types);
7335ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return QualType();
7336ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    } else {
7337ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      QualType Underlying = BaseType;
7338ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      if (!BaseType->isDependentType()) {
7339c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        // The enum could be incomplete if we're parsing its definition or
7340c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        // recovering from an error.
7341c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        NamedDecl *FwdDecl = nullptr;
7342c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        if (BaseType->isIncompleteType(&FwdDecl)) {
7343c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines          Diag(Loc, diag::err_underlying_type_of_incomplete_enum) << BaseType;
7344c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines          Diag(FwdDecl->getLocation(), diag::note_forward_declaration) << FwdDecl;
7345c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines          return QualType();
7346c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        }
7347c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
7348ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl();
7349ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        assert(ED && "EnumType has no EnumDecl");
7350c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
7351ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        DiagnoseUseOfDecl(ED, Loc);
7352c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
7353ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        Underlying = ED->getIntegerType();
7354c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines        assert(!Underlying.isNull());
7355ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      }
7356ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return Context.getUnaryTransformType(BaseType, Underlying,
7357ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                        UnaryTransformType::EnumUnderlyingType);
7358ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
7359ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  }
7360ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  llvm_unreachable("unknown unary transform type");
7361ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
7362b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
7363b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) {
7364b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T->isDependentType()) {
73658327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // FIXME: It isn't entirely clear whether incomplete atomic types
73668327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // are allowed or not; for simplicity, ban them for the moment.
7367d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (RequireCompleteType(Loc, T, diag::err_atomic_specifier_bad_type, 0))
73688327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith      return QualType();
73698327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith
7370b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    int DisallowedKind = -1;
73718327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    if (T->isArrayType())
7372b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 1;
7373b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isFunctionType())
7374b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 2;
7375b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isReferenceType())
7376b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 3;
7377b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isAtomicType())
7378b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 4;
7379b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T.hasQualifiers())
7380b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 5;
7381b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (!T.isTriviallyCopyableType(Context))
7382b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      // Some other non-trivially-copyable type (probably a C++ class)
7383b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 6;
7384b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
7385b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (DisallowedKind != -1) {
7386b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T;
7387b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return QualType();
7388b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
7389b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
7390b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // FIXME: Do we need any handling for ARC here?
7391b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
7392b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
7393b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Build the pointer type.
7394b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return Context.getAtomicType(T);
7395b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
7396