SemaType.cpp revision dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements type-related semantic analysis.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
142d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
1636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor#include "clang/AST/ASTMutationListener.h"
17a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor#include "clang/AST/CXXInheritance.h"
18980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
192943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor#include "clang/AST/DeclTemplate.h"
2055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/Expr.h"
214adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
2251bd803fbdade51d674598ed45da3d54190a656cJohn McCall#include "clang/AST/TypeLocVisitor.h"
2355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/OpenCL.h"
2491a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
25d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis#include "clang/Basic/TargetInfo.h"
262792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall#include "clang/Lex/Preprocessor.h"
27bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman#include "clang/Parse/ParseDiagnostic.h"
2819510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
29f85e193739c953358c865005855253af4f68a497John McCall#include "clang/Sema/DelayedDiagnostic.h"
30d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor#include "clang/Sema/Lookup.h"
3155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/ScopeInfo.h"
3255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Template.h"
334994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl#include "llvm/ADT/SmallPtrSet.h"
348c952cd40ccec9d720931f27e7d722fed207d536Richard Smith#include "llvm/ADT/SmallString.h"
3587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor#include "llvm/Support/ErrorHandling.h"
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
385db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// isOmittedBlockReturnType - Return true if this declarator is missing a
3991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier/// return type because this is a omitted return type on a block literal.
408ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic bool isOmittedBlockReturnType(const Declarator &D) {
415db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getContext() != Declarator::BlockLiteralContext ||
428ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl      D.getDeclSpec().hasTypeSpecifier())
435db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return false;
4491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
455db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 0)
46a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^{ ... }
4791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
485db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 1 &&
495db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      D.getTypeObject(0).Kind == DeclaratorChunk::Function)
50a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^(int X, float Y) { ... }
5191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
525db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  return false;
535db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner}
545db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
552792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// diagnoseBadTypeAttribute - Diagnoses a type attribute which
562792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// doesn't apply to the given type.
572792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCallstatic void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr,
582792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall                                     QualType type) {
59108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth  bool useExpansionLoc = false;
602792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
612792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  unsigned diagID = 0;
622792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  switch (attr.getKind()) {
638e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  case AttributeList::AT_ObjCGC:
642792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    diagID = diag::warn_pointer_attribute_wrong_type;
65108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth    useExpansionLoc = true;
662792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    break;
672792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
688e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  case AttributeList::AT_ObjCOwnership:
6905d4876a64865e34366b58fc8a6848c3cde895d9Argyrios Kyrtzidis    diagID = diag::warn_objc_object_attribute_wrong_type;
70108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth    useExpansionLoc = true;
7105d4876a64865e34366b58fc8a6848c3cde895d9Argyrios Kyrtzidis    break;
7205d4876a64865e34366b58fc8a6848c3cde895d9Argyrios Kyrtzidis
732792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  default:
742792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    // Assume everything else was a function attribute.
752792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    diagID = diag::warn_function_attribute_wrong_type;
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.
83108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth  if (useExpansionLoc && loc.isMacroID() && attr.getParameterName()) {
84834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    if (attr.getParameterName()->isStr("strong")) {
85834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__strong")) name = "__strong";
86834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    } else if (attr.getParameterName()->isStr("weak")) {
87834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__weak")) name = "__weak";
882792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    }
892792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  }
902792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
912792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  S.Diag(loc, diagID) << name << type;
922792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall}
932792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
94711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// objc_gc applies to Objective-C pointers or, otherwise, to the
95711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// smallest available pointer type (i.e. 'void*' in 'void**').
96711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
978e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ObjCGC: \
988e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ObjCOwnership
99711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
100711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// Function type attributes.
101711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define FUNCTION_TYPE_ATTRS_CASELIST \
1028e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NoReturn: \
1038e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_CDecl: \
1048e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_FastCall: \
1058e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_StdCall: \
1068e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ThisCall: \
1078e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Pascal: \
1088e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Regparm: \
109263366f9241366f29ba65b703120f302490c39ffDerek Schuff    case AttributeList::AT_Pcs: \
11038980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    case AttributeList::AT_PnaclCall: \
11138980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    case AttributeList::AT_IntelOclBicc \
112711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
113711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace {
114711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// An object which stores processing state for the entire
115711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// GetTypeForDeclarator process.
116711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  class TypeProcessingState {
117711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &sema;
118711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
119711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The declarator being processed.
120711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &declarator;
121711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
122711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The index of the declarator chunk we're currently processing.
123711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// May be the total number of valid chunks, indicating the
124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// DeclSpec.
125711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex;
126711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
127711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Whether there are non-trivial modifications to the decl spec.
128711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    bool trivial;
129711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1307ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    /// Whether we saved the attributes in the decl spec.
1317ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    bool hasSavedAttrs;
1327ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
133711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The original set of attributes on the DeclSpec.
1345f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> savedAttrs;
135711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
136711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// A list of attributes to diagnose the uselessness of when the
137711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// processing is complete.
1385f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> ignoredTypeAttrs;
139711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
140711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  public:
141711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    TypeProcessingState(Sema &sema, Declarator &declarator)
142711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : sema(sema), declarator(declarator),
143711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        chunkIndex(declarator.getNumTypeObjects()),
1447ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        trivial(true), hasSavedAttrs(false) {}
145711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
146711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &getSema() const {
147711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return sema;
148711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
149711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
150711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &getDeclarator() const {
151711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator;
152711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
153711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
154b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    bool isProcessingDeclSpec() const {
155b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      return chunkIndex == declarator.getNumTypeObjects();
156b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
157b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
158711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned getCurrentChunkIndex() const {
159711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return chunkIndex;
160711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
161711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void setCurrentChunkIndex(unsigned idx) {
163711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(idx <= declarator.getNumTypeObjects());
164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      chunkIndex = idx;
165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
166711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&getCurrentAttrListRef() const {
168b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (isProcessingDeclSpec())
169711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        return getMutableDeclSpec().getAttributes().getListRef();
170711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator.getTypeObject(chunkIndex).getAttrListRef();
171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
172711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Save the current set of attributes on the DeclSpec.
174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void saveDeclSpecAttrs() {
175711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Don't try to save them multiple times.
1767ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (hasSavedAttrs) return;
177711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
178711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      DeclSpec &spec = getMutableDeclSpec();
179711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (AttributeList *attr = spec.getAttributes().getList(); attr;
180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             attr = attr->getNext())
181711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs.push_back(attr);
182711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      trivial &= savedAttrs.empty();
1837ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      hasSavedAttrs = true;
184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
185711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
186711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Record that we had nowhere to put the given type attribute.
187711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// We will diagnose such attributes later.
188711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void addIgnoredTypeAttr(AttributeList &attr) {
189711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      ignoredTypeAttrs.push_back(&attr);
190711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
191711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
192711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Diagnose all the ignored type attributes, given that the
193711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// declarator worked out to the given type.
194711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void diagnoseIgnoredTypeAttrs(QualType type) const {
1955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      for (SmallVectorImpl<AttributeList*>::const_iterator
196711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
1972792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall           i != e; ++i)
1982792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall        diagnoseBadTypeAttribute(getSema(), **i, type);
199711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
201711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    ~TypeProcessingState() {
202711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (trivial) return;
203711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
204711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      restoreDeclSpecAttrs();
205711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
206711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
207711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  private:
208711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclSpec &getMutableDeclSpec() const {
209711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return const_cast<DeclSpec&>(declarator.getDeclSpec());
210711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
211711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
212711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void restoreDeclSpecAttrs() {
2137ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      assert(hasSavedAttrs);
2147ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
2157ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (savedAttrs.empty()) {
2167ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        getMutableDeclSpec().getAttributes().set(0);
2177ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        return;
2187ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      }
2197ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
220711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
221711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
222711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs[i]->setNext(savedAttrs[i+1]);
223711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      savedAttrs.back()->setNext(0);
224711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
225711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
226711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
227711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// Basically std::pair except that we really want to avoid an
228711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// implicit operator= for safety concerns.  It's also a minor
229711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// link-time optimization for this to be a private type.
230711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  struct AttrAndList {
231711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The attribute.
232711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &first;
233711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
234711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The head of the list the attribute is currently in.
235711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&second;
236711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
237711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttrAndList(AttributeList &attr, AttributeList *&head)
238711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : first(attr), second(head) {}
239711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
24004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
24104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
242711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace llvm {
243711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  template <> struct isPodLike<AttrAndList> {
244711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    static const bool value = true;
245711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
246711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
247711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
248711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
249711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  attr.setNext(head);
250711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  head = &attr;
251711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
252711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
253711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
254711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (head == &attr) {
255711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    head = attr.getNext();
256711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
25704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
258711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
259711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *cur = head;
260711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  while (true) {
261711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    assert(cur && cur->getNext() && "ran out of attrs?");
262711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (cur->getNext() == &attr) {
263711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      cur->setNext(attr.getNext());
264711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
265711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
266711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    cur = cur->getNext();
267711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
268711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
269711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
270711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void moveAttrFromListToList(AttributeList &attr,
271711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&fromList,
272711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&toList) {
273711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, fromList);
274711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrIntoList(attr, toList);
275711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
276711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
277f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith/// The location of a type attribute.
278f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smithenum TypeAttrLocation {
279f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is in the decl-specifier-seq.
280f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclSpec,
281f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is part of a DeclaratorChunk.
282f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclChunk,
283f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is immediately after the declaration's name.
284f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclName
285f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith};
286f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
287711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state,
288f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             QualType &type, TypeAttrLocation TAL,
289711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             AttributeList *attrs);
290711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
291711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
292711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
293711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type);
294711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
295711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
296711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr, QualType &type);
297711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
298b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
299f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr, QualType &type);
300f85e193739c953358c865005855253af4f68a497John McCall
301711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCPointerTypeAttr(TypeProcessingState &state,
302711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr, QualType &type) {
3038e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_ObjCGC)
304f85e193739c953358c865005855253af4f68a497John McCall    return handleObjCGCTypeAttr(state, attr, type);
3058e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  assert(attr.getKind() == AttributeList::AT_ObjCOwnership);
306b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  return handleObjCOwnershipTypeAttr(state, attr, type);
307711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
308711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
309b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// Given the index of a declarator chunk, check whether that chunk
310b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// directly specifies the return type of a function and, if so, find
311b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// an appropriate place for it.
312b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall///
313b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall/// \param i - a notional index which the search will start
314b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall///   immediately inside
315b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCallstatic DeclaratorChunk *maybeMovePastReturnType(Declarator &declarator,
316b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall                                                unsigned i) {
317b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  assert(i <= declarator.getNumTypeObjects());
318b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
319b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  DeclaratorChunk *result = 0;
320b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
321b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  // First, look inwards past parens for a function declarator.
322b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  for (; i != 0; --i) {
323b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    DeclaratorChunk &fnChunk = declarator.getTypeObject(i-1);
324b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    switch (fnChunk.Kind) {
325b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Paren:
326b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      continue;
327b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
328b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // If we find anything except a function, bail out.
329b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Pointer:
330b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::BlockPointer:
331b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Array:
332b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Reference:
333b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::MemberPointer:
334b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      return result;
335b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
336b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // If we do find a function declarator, scan inwards from that,
337b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // looking for a block-pointer declarator.
338b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Function:
339b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      for (--i; i != 0; --i) {
340b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        DeclaratorChunk &blockChunk = declarator.getTypeObject(i-1);
341b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        switch (blockChunk.Kind) {
342b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Paren:
343b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Pointer:
344b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Array:
345b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Function:
346b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::Reference:
347b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::MemberPointer:
348b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          continue;
349b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        case DeclaratorChunk::BlockPointer:
350b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          result = &blockChunk;
351b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          goto continue_outer;
352b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        }
353b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        llvm_unreachable("bad declarator chunk kind");
354b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      }
355b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
356b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      // If we run out of declarators doing that, we're done.
357b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      return result;
358b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
359b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    llvm_unreachable("bad declarator chunk kind");
360b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
361b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // Okay, reconsider from our new point.
362b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  continue_outer: ;
363b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  }
364b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
365b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  // Ran out of chunks, bail out.
366b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  return result;
367b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall}
368b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
369711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Given that an objc_gc attribute was written somewhere on a
370711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declaration *other* than on the declarator itself (for which, use
371711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
372711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// didn't apply in whatever position it was written in, try to move
373711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// it to a more appropriate position.
374711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeObjCPointerTypeAttr(TypeProcessingState &state,
375711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          AttributeList &attr,
376711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType type) {
377711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
378b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
379b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall  // Move it to the outermost normal or block pointer declarator.
380711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
381711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
382711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
383711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
384b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::BlockPointer: {
385b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      // But don't move an ARC ownership attribute to the return type
386b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      // of a block.
387b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      DeclaratorChunk *destChunk = 0;
388b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (state.isProcessingDeclSpec() &&
389b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          attr.getKind() == AttributeList::AT_ObjCOwnership)
390b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        destChunk = maybeMovePastReturnType(declarator, i - 1);
391b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (!destChunk) destChunk = &chunk;
392b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
393711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
394b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall                             destChunk->getAttrListRef());
395711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
396b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
397711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
398711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
399711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
400711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
401711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
402b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // We may be starting at the return type of a block.
403b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    case DeclaratorChunk::Function:
404b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (state.isProcessingDeclSpec() &&
405b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          attr.getKind() == AttributeList::AT_ObjCOwnership) {
406b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        if (DeclaratorChunk *dest = maybeMovePastReturnType(declarator, i)) {
407b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
408b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall                                 dest->getAttrListRef());
409b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          return;
410b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        }
411b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      }
412b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      goto error;
413b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
414711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Don't walk through these.
415711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
416711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
417711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto error;
418711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
419711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
420711c52bb20d0c69063b52a99826fb7d2835501f1John McCall error:
4212792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
4222792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
423711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
424711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
425711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Distribute an objc_gc type attribute that was written on the
426711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator.
427711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
428711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
429711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            AttributeList &attr,
430711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            QualType &declSpecType) {
431711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
432711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
433711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // objc_gc goes on the innermost pointer to something that's not a
434711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // pointer.
435711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  unsigned innermost = -1U;
436711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  bool considerDeclSpec = true;
437711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
438711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
439711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
440711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
441711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
442711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      innermost = i;
443ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      continue;
444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
445711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
446711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
448711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
449711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
450711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
451711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
452711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      considerDeclSpec = false;
453711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto done;
454711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
455711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
456711c52bb20d0c69063b52a99826fb7d2835501f1John McCall done:
457711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
458711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // That might actually be the decl spec if we weren't blocked by
459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // anything in the declarator.
460711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (considerDeclSpec) {
4617ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    if (handleObjCPointerTypeAttr(state, attr, declSpecType)) {
4627ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // Splice the attribute into the decl spec.  Prevents the
4637ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // attribute from being applied multiple times and gives
4647ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // the source-location-filler something to work with.
4657ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      state.saveDeclSpecAttrs();
4667ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      moveAttrFromListToList(attr, declarator.getAttrListRef(),
4677ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall               declarator.getMutableDeclSpec().getAttributes().getListRef());
468711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
4697ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    }
470711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
471711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
472711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, if we found an appropriate chunk, splice the attribute
473711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // into it.
474711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (innermost != -1U) {
475711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, declarator.getAttrListRef(),
476711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                       declarator.getTypeObject(innermost).getAttrListRef());
477711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
478711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
479711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
480711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, diagnose when we're done building the type.
481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
482711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
484711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
485711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written somewhere in a declaration
486711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// *other* than on the declarator itself or in the decl spec.  Given
487711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// that it didn't apply in whatever position it was written in, try
488711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// to move it to a more appropriate position.
489711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeFunctionTypeAttr(TypeProcessingState &state,
490711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
491711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType type) {
492711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
493711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
494711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to push the attribute from the return type of a function to
495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the function itself.
496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
497711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
498711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
499711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
501711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
505711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
506711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
507711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
508711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
509711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
510711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
511711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
512711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
51391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5142792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Try to distribute a function type attribute to the innermost
518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// function chunk or type.  Returns true if the attribute was
519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributed, false if no location was found.
520711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool
521711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr,
523711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList *&attrList,
524711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      QualType &declSpecType) {
525711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
526711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
527711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Put it on the innermost function chunk, if there is one.
528711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
529711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (chunk.Kind != DeclaratorChunk::Function) continue;
531711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
532711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
534711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
535711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
536f85e193739c953358c865005855253af4f68a497John McCall  if (handleFunctionTypeAttr(state, attr, declSpecType)) {
537f85e193739c953358c865005855253af4f68a497John McCall    spliceAttrOutOfList(attr, attrList);
538f85e193739c953358c865005855253af4f68a497John McCall    return true;
539f85e193739c953358c865005855253af4f68a497John McCall  }
540f85e193739c953358c865005855253af4f68a497John McCall
541f85e193739c953358c865005855253af4f68a497John McCall  return false;
542711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
543711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
544711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written in the decl spec.  Try to
545711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
546711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
547711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
548711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
549711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType &declSpecType) {
550711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.saveDeclSpecAttrs();
551711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5525c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // C++11 attributes before the decl specifiers actually appertain to
5535c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // the declarators. Move them straight there. We don't support the
5545c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // 'put them wherever you like' semantics we allow for GNU attributes.
5555c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  if (attr.isCXX11Attribute()) {
5565c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
5575c52166525f8714c3e3a979b156ec23426947fd3Richard Smith                           state.getDeclarator().getAttrListRef());
5585c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    return;
5595c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  }
5605c52166525f8714c3e3a979b156ec23426947fd3Richard Smith
561711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
562711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
563711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            state.getCurrentAttrListRef(),
564711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
565711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
566711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
567711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
568711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
569711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
570711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
571711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
572711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written on the declarator.  Try to
573711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
574711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
575711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
576711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         AttributeList &attr,
577711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         QualType &declSpecType) {
578711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
579711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
580711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
581711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
582711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declarator.getAttrListRef(),
583711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
584711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
585711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
586711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
587711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
588711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
589711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
590711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
591711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
592711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// \brief Given that there are attributes written on the declarator
593711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// itself, try to distribute any type attributes to the appropriate
594711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator chunk.
595711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///
596711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// These are attributes like the following:
597711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f ATTR;
598711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int (f ATTR)();
599711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// but not necessarily this:
600711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f() ATTR;
601711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
602711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                              QualType &declSpecType) {
603711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Collect all the type attributes from the declarator itself.
604711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
605711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *attr = state.getDeclarator().getAttributes();
606711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
607711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
608711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr->getNext();
609711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
6105c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    // Do not distribute C++11 attributes. They have strict rules for what
6115c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    // they appertain to.
6125c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    if (attr->isCXX11Attribute())
6135c52166525f8714c3e3a979b156ec23426947fd3Richard Smith      continue;
6145c52166525f8714c3e3a979b156ec23426947fd3Richard Smith
615711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr->getKind()) {
616711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
617711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
618711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
619711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
6208e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
6214e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
622f85e193739c953358c865005855253af4f68a497John McCall        break;
623f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough
624f85e193739c953358c865005855253af4f68a497John McCall
625711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
626711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
627711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
628711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
629711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    default:
630711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
631711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
632711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attr = next));
633711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
634711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
635711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Add a synthetic '()' to a block-literal declarator if it is
636711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// required, given the return type.
637711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void maybeSynthesizeBlockSignature(TypeProcessingState &state,
638711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType declSpecType) {
639711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
640711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
641711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // First, check whether the declarator would produce a function,
642711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // i.e. whether the innermost semantic chunk is a function.
643711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.isFunctionDeclarator()) {
644711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // If so, make that declarator a prototyped declarator.
645711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.getFunctionTypeInfo().hasPrototype = true;
646711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
647711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
648711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
649da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // If there are any type objects, the type as written won't name a
650da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // function, regardless of the decl spec type.  This is because a
651da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // block signature declarator is always an abstract-declarator, and
652da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // abstract-declarators can't just be parentheses chunks.  Therefore
653da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // we need to build a function chunk unless there are no type
654da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // objects and the decl spec type is a function.
655711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
656711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
657711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
658da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // Note that there *are* cases with invalid declarators where
659da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // declarators consist solely of parentheses.  In general, these
660da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // occur only in failed efforts to make function declarators, so
661da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // faking up the function chunk is still the right thing to do.
662711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
663711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, we need to fake up a function declarator.
66496a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar  SourceLocation loc = declarator.getLocStart();
665711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
666711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // ...and *prepend* it to the declarator.
66759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara  SourceLocation NoLoc;
668711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
66959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*HasProto=*/true,
67059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*IsAmbiguous=*/false,
67159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*LParenLoc=*/NoLoc,
67259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ArgInfo=*/0,
67359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NumArgs=*/0,
67459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*EllipsisLoc=*/NoLoc,
67559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RParenLoc=*/NoLoc,
67659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*TypeQuals=*/0,
67759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RefQualifierIsLvalueRef=*/true,
67859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RefQualifierLoc=*/NoLoc,
67959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ConstQualifierLoc=*/NoLoc,
68059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*VolatileQualifierLoc=*/NoLoc,
68159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*MutableLoc=*/NoLoc,
68259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             EST_None,
68359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ESpecLoc=*/NoLoc,
68459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*Exceptions=*/0,
68559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ExceptionRanges=*/0,
68659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NumExceptions=*/0,
68759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NoexceptExpr=*/0,
68859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             loc, loc, declarator));
689711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
690711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // For consistency, make sure the state still has us as processing
691711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the decl spec.
692711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
693711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.setCurrentChunkIndex(declarator.getNumTypeObjects());
69404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
69504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
696930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
697930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
6981dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// \param state Specifies the declarator containing the declaration specifier
6991dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// to be converted, along with other associated processing state.
7005153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
7015153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
7028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType ConvertDeclSpecToType(TypeProcessingState &state) {
7035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
7045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
705711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
7068cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
707711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
708711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const DeclSpec &DS = declarator.getDeclSpec();
709711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation DeclLoc = declarator.getIdentifierLoc();
7105db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
71196a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar    DeclLoc = DS.getLocStart();
71291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
713711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  ASTContext &Context = S.Context;
7141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7155db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
7165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
71796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
71896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
71996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
7205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
7215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
722fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
7235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
724fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
7255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
7265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
7275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
728fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
730958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
73164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
73264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
73364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
73464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
735711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
736f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
73764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
73864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
73964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
74064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
741711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
742f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
74364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
74464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
74564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
746f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
747f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
748f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
749f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
750f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
751f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
752f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
753f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
754f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
755f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
756d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
75762f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
758097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
759c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
76031ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                         (ObjCProtocolDecl*const*)PQ,
761c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         DS.getNumProtocolQualifiers());
762c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectPointerType(Result);
76362f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
76462f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
76591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
7665db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
7675db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
768f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // The declspec is always missing in a lambda expr context; it is either
769f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // specified with a trailing return type or inferred.
770f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    if (declarator.getContext() == Declarator::LambdaExprContext ||
771f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman        isOmittedBlockReturnType(declarator)) {
7725db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      Result = Context.DependentTy;
7735db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
7745db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
7751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
776d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
777d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
778d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
779d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
780d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
781d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
782d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
7834e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().ImplicitInt) {
78435d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
78535d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
7863f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
787711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_declspec)
7883f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
78996a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        << FixItHint::CreateInsertion(DS.getLocStart(), "int");
7903f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
7914310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
792d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
793d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
794d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
795d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
7964310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
7974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (S.getLangOpts().CPlusPlus &&
7984e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          !S.getLangOpts().MicrosoftExt) {
799711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_missing_type_specifier)
8003f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
8011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
802b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
803b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
804b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
805711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
806b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
807711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_type_specifier)
8083f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
809b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
810d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
8111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
8133cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
8145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
8155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
816fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
817fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
818fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
819311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
820311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
82191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
822e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
823e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
824e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
825e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
82680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
827e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
828e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
829e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
830e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
831311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
8325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
8335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
8345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
835fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
836fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
837fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
838311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
839311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
84091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
841e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
842e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
843e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
844e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
84580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
846e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
847e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
848e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
849e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
850311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
8515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
8525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
853958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
8543cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
8555a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith  case DeclSpec::TST_int128:
85684268904947ada7e251932a6f5b0f4364df7a2c7Richard Smith    if (!S.PP.getTargetInfo().hasInt128Type())
85784268904947ada7e251932a6f5b0f4364df7a2c7Richard Smith      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_int128_unsupported);
8585a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    if (DS.getTypeSpecSign() == DeclSpec::TSS_unsigned)
8595a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.UnsignedInt128Ty;
8605a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    else
8615a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.Int128Ty;
8625a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    break;
863aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case DeclSpec::TST_half: Result = Context.HalfTy; break;
864fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
865958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
866958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
867fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
868958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
869fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
87039d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne
8714e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
87239d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
87339d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      declarator.setInvalidType(true);
87439d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    }
875958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
876fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
8775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
8785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
880711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
8818f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
882711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
8838f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
88499dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
8865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
8876666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_struct:
8886666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_interface: {
889b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
8906e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
8916e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
8926e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
893711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
8946e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
8956e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
8966e24726524c2b51b31bb4b622aa678a46b024f42John McCall
897a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
8980daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc());
89991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
9005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
901a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
90291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
9035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
904a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
9052191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
9060daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    // In both C and C++, make an ElaboratedType.
9070daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    ElaboratedTypeKeyword Keyword
9080daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
9090daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
910958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
9111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
9121a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
9135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
9145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
916711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
91727940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    if (Result.isNull())
918711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
91927940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    else if (DeclSpec::ProtocolQualifierListTy PQ
92027940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall               = DS.getProtocolQualifiers()) {
921c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
922c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // Silently drop any existing protocol qualifiers.
923c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // TODO: determine whether that's the right thing to do.
924c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjT->getNumProtocols())
925c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = ObjT->getBaseType();
926c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
927c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (DS.getNumProtocolQualifiers())
928c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = Context.getObjCObjectType(Result,
92931ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                             (ObjCProtocolDecl*const*) PQ,
930c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             DS.getNumProtocolQualifiers());
931c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCIdType()) {
932ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
933c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
93431ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                           (ObjCProtocolDecl*const*) PQ,
935c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
936c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
937c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCClassType()) {
9384262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
939c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
94031ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                           (ObjCProtocolDecl*const*) PQ,
941c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
942c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
9433f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
944711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
9453f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
946711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
9473f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
948c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
9491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
951958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
9525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
953958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
954e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
955711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
956958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
957730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian    if (!Result->isDependentType())
958730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian      if (const TagType *TT = Result->getAs<TagType>())
959711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
960d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
961fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
962958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
963d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
964b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
965d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
966d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
967711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
9684b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
9694b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
970711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
9714b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
972958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
973d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
9746fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
975b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
9766fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
9776fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
978711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
979af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
980af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
981711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
982af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
9836fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
9846fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
985ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case DeclSpec::TST_underlyingType:
986db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    Result = S.GetTypeFromParser(DS.getRepAsType());
987db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    assert(!Result.isNull() && "Didn't get a type for __underlying_type?");
988ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    Result = S.BuildUnaryTransformType(Result,
989ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::EnumUnderlyingType,
990ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       DS.getTypeSpecTypeLoc());
991ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (Result.isNull()) {
992ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Result = Context.IntTy;
993ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      declarator.setInvalidType(true);
994db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    }
99591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
996db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
997a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  case DeclSpec::TST_auto:
998e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
999a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    Result = Context.getAutoType(QualType(), /*decltype(auto)*/false);
1000a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    break;
1001a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
1002a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  case DeclSpec::TST_decltype_auto:
1003a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    Result = Context.getAutoType(QualType(), /*decltype(auto)*/true);
1004e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
10051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1006a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall  case DeclSpec::TST_unknown_anytype:
1007a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    Result = Context.UnknownAnyTy;
1008a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    break;
1009a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall
1010b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case DeclSpec::TST_atomic:
1011b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.GetTypeFromParser(DS.getRepAsType());
1012b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(!Result.isNull() && "Didn't get a type for _Atomic?");
1013b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc());
1014b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Result.isNull()) {
1015b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Result = Context.IntTy;
1016b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      declarator.setInvalidType(true);
1017b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
101891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
1019b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1020b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_t:
1021b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dTy;
1022b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1023b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1024b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_array_t:
1025b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dArrayTy;
1026b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1027b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1028b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_buffer_t:
1029b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dBufferTy;
1030b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1031b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1032b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_t:
1033b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dTy;
1034b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1035b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1036b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_array_t:
1037b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dArrayTy;
1038b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1039b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1040b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image3d_t:
1041b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage3dTy;
1042b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1043b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
104421f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei  case DeclSpec::TST_sampler_t:
104521f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    Result = Context.OCLSamplerTy;
104621f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    break;
104721f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei
1048e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei  case DeclSpec::TST_event_t:
1049e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    Result = Context.OCLEventTy;
1050e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    break;
1051e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei
1052809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
10535153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
1054711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
10555153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
10565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1058958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
1059f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
10604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().Freestanding)
1061711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
1062fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
106382287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
106482287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
106582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
1066e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
1067788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
1068e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
1069788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
1070e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
1071e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
1072f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
10731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
107447423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
107547423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
1076711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
10771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1078711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
1079711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
1080711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
1081711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
1082711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1083711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
1084711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
1085711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
1086711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (AttributeList *attrs = DS.getAttributes().getList())
1087f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    processTypeAttrs(state, Result, TAL_DeclSpec, attrs);
10881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
108996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
109096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
109196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
109296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
109396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
109496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
109596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
10960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
10974cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        S.Diag(DS.getConstSpecLoc(), diag::warn_typecheck_function_qualifiers)
10984cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith          << Result << DS.getSourceRange();
10990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
11004cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        S.Diag(DS.getVolatileSpecLoc(), diag::warn_typecheck_function_qualifiers)
11014cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith          << Result << DS.getSourceRange();
11020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
11034cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        assert((TypeQuals & (DeclSpec::TQ_restrict | DeclSpec::TQ_atomic)) &&
11044cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith               "Has CVRA quals but not C, V, R, or A?");
11054cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // No diagnostic; we'll diagnose 'restrict' or '_Atomic' applied to a
11064cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // function type later, in BuildQualifiedType.
110796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
110896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
11091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1110f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
1111f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
1112f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
1113f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
1114f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
11151a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
11161a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
1117f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
11180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
11190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
11204cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      TypeQuals &= ~DeclSpec::TQ_atomic;
11211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
11221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1123bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // C90 6.5.3 constraints: "The same type qualifier shall not appear more
1124bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // than once in the same specifier-list or qualifier-list, either directly
1125bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // or via one or more typedefs."
112691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus
1127bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman        && TypeQuals & Result.getCVRQualifiers()) {
1128bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()) {
112991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec)
1130bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "const";
1131bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1132bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
1133bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()) {
113491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec)
1135bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "volatile";
1136bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1137bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
11384cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // C90 doesn't have restrict nor _Atomic, so it doesn't force us to
11394cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // produce a warning in this case.
1140bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    }
1141bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
11424cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    QualType Qualified = S.BuildQualifiedType(Result, DeclLoc, TypeQuals, &DS);
11434cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
11444cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // If adding qualifiers fails, just use the unqualified type.
11454cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (Qualified.isNull())
11464cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      declarator.setInvalidType(true);
11474cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    else
11484cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      Result = Qualified;
114996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
11500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1151f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
1152f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
1153f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
1154cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
1155cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
1156cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
11571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1158cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
1159cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1160cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
11612865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
116293d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith                                  Qualifiers Qs, const DeclSpec *DS) {
11632865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
11642865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
11652865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
11662865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
11672865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
11682865474261a608c7873b87ba4af110d17907896dJohn McCall
116993d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith    if (T->isAnyPointerType() || T->isReferenceType() ||
117093d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        T->isMemberPointerType()) {
117193d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      QualType EltTy;
117293d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      if (T->isObjCObjectPointerType())
117393d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = T;
117493d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>())
117593d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = PTy->getPointeeType();
117693d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      else
117793d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = T->getPointeeType();
117893d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith
117993d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      // If we have a pointer or reference, the pointee must have an object
118093d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      // incomplete type.
118193d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      if (!EltTy->isIncompleteOrObjectType()) {
11822865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
118393d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        ProblemTy = EltTy;
118491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
118593d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith    } else if (!T->isDependentType()) {
118693d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      DiagID = diag::err_typecheck_invalid_restrict_not_pointer;
11872865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
11882865474261a608c7873b87ba4af110d17907896dJohn McCall    }
11892865474261a608c7873b87ba4af110d17907896dJohn McCall
11902865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
119193d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << ProblemTy;
11922865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
11932865474261a608c7873b87ba4af110d17907896dJohn McCall    }
11942865474261a608c7873b87ba4af110d17907896dJohn McCall  }
11952865474261a608c7873b87ba4af110d17907896dJohn McCall
11962865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
11972865474261a608c7873b87ba4af110d17907896dJohn McCall}
11982865474261a608c7873b87ba4af110d17907896dJohn McCall
11994cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard SmithQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
12004cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith                                  unsigned CVRA, const DeclSpec *DS) {
12014cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // Convert from DeclSpec::TQ to Qualifiers::TQ by just dropping TQ_atomic.
12024cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  unsigned CVR = CVRA & ~DeclSpec::TQ_atomic;
12034cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
12044cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // C11 6.7.3/5:
12054cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   If the same qualifier appears more than once in the same
12064cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   specifier-qualifier-list, either directly or via one or more typedefs,
12074cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   the behavior is the same as if it appeared only once.
12084cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //
12094cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // It's not specified what happens when the _Atomic qualifier is applied to
12104cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // a type specified with the _Atomic specifier, but we assume that this
12114cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // should be treated as if the _Atomic qualifier appeared multiple times.
12124cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  if (CVRA & DeclSpec::TQ_atomic && !T->isAtomicType()) {
12134cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // C11 6.7.3/5:
12144cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   If other qualifiers appear along with the _Atomic qualifier in a
12154cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   specifier-qualifier-list, the resulting type is the so-qualified
12164cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   atomic type.
12174cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //
12184cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // Don't need to worry about array types here, since _Atomic can't be
12194cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // applied to such types.
12204cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    SplitQualType Split = T.getSplitUnqualifiedType();
12214cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    T = BuildAtomicType(QualType(Split.Ty, 0),
12224cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith                        DS ? DS->getAtomicSpecLoc() : Loc);
12234cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (T.isNull())
12244cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      return T;
12254cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    Split.Quals.addCVRQualifiers(CVR);
12264cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    return BuildQualifiedType(T, Loc, Split.Quals);
12274cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
12284cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
12294cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  return BuildQualifiedType(T, Loc, Qualifiers::fromCVRMask(CVR), DS);
12304cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith}
12314cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
1232075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
1233075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
1234075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
1235075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
1236075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
1237f85e193739c953358c865005855253af4f68a497John McCall/// Given that we're building a pointer or reference to the given
1238f85e193739c953358c865005855253af4f68a497John McCallstatic QualType inferARCLifetimeForPointee(Sema &S, QualType type,
1239f85e193739c953358c865005855253af4f68a497John McCall                                           SourceLocation loc,
1240f85e193739c953358c865005855253af4f68a497John McCall                                           bool isReference) {
1241f85e193739c953358c865005855253af4f68a497John McCall  // Bail out if retention is unrequired or already specified.
1242f85e193739c953358c865005855253af4f68a497John McCall  if (!type->isObjCLifetimeType() ||
1243f85e193739c953358c865005855253af4f68a497John McCall      type.getObjCLifetime() != Qualifiers::OCL_None)
1244f85e193739c953358c865005855253af4f68a497John McCall    return type;
1245f85e193739c953358c865005855253af4f68a497John McCall
1246f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None;
1247f85e193739c953358c865005855253af4f68a497John McCall
1248f85e193739c953358c865005855253af4f68a497John McCall  // If the object type is const-qualified, we can safely use
1249f85e193739c953358c865005855253af4f68a497John McCall  // __unsafe_unretained.  This is safe (because there are no read
1250f85e193739c953358c865005855253af4f68a497John McCall  // barriers), and it'll be safe to coerce anything but __weak* to
1251f85e193739c953358c865005855253af4f68a497John McCall  // the resulting type.
1252f85e193739c953358c865005855253af4f68a497John McCall  if (type.isConstQualified()) {
1253f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
1254f85e193739c953358c865005855253af4f68a497John McCall
1255f85e193739c953358c865005855253af4f68a497John McCall  // Otherwise, check whether the static type does not require
1256f85e193739c953358c865005855253af4f68a497John McCall  // retaining.  This currently only triggers for Class (possibly
1257f85e193739c953358c865005855253af4f68a497John McCall  // protocol-qualifed, and arrays thereof).
1258f85e193739c953358c865005855253af4f68a497John McCall  } else if (type->isObjCARCImplicitlyUnretainedType()) {
1259f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
12605b76f373d23cc3b292ecf523349aaaa388eea375Argyrios Kyrtzidis
1261ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // If we are in an unevaluated context, like sizeof, skip adding a
1262ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // qualification.
126371f55f771794674a410171dbf3cb5dbedf95d033David Blaikie  } else if (S.isUnevaluatedContext()) {
1264ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    return type;
1265f85e193739c953358c865005855253af4f68a497John McCall
1266e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // If that failed, give an error and recover using __strong.  __strong
1267e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // is the option most likely to prevent spurious second-order diagnostics,
1268e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // like when binding a reference to a field.
1269f85e193739c953358c865005855253af4f68a497John McCall  } else {
1270f85e193739c953358c865005855253af4f68a497John McCall    // These types can show up in private ivars in system headers, so
1271f85e193739c953358c865005855253af4f68a497John McCall    // we need this to not be an error in those cases.  Instead we
1272f85e193739c953358c865005855253af4f68a497John McCall    // want to delay.
1273f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1274ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.DelayedDiagnostics.add(
1275ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman          sema::DelayedDiagnostic::makeForbiddenType(loc,
1276ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman              diag::err_arc_indirect_no_ownership, type, isReference));
1277f85e193739c953358c865005855253af4f68a497John McCall    } else {
1278ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference;
1279f85e193739c953358c865005855253af4f68a497John McCall    }
1280e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall    implicitLifetime = Qualifiers::OCL_Strong;
1281f85e193739c953358c865005855253af4f68a497John McCall  }
1282f85e193739c953358c865005855253af4f68a497John McCall  assert(implicitLifetime && "didn't infer any lifetime!");
1283f85e193739c953358c865005855253af4f68a497John McCall
1284f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers qs;
1285f85e193739c953358c865005855253af4f68a497John McCall  qs.addObjCLifetime(implicitLifetime);
1286f85e193739c953358c865005855253af4f68a497John McCall  return S.Context.getQualifiedType(type, qs);
1287f85e193739c953358c865005855253af4f68a497John McCall}
1288f85e193739c953358c865005855253af4f68a497John McCall
1289cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
1290cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1291cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
1292cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1293cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1294cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
1295cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
1296cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1297cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
1298cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1299cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1300cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
1301cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
13022865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
1303cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
1304cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
1305cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
1306cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
1307ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1308cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1309cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1310cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1311c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
131292e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
1313f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build pointers to unqualified pointers.
13144e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1315f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false);
1316f85e193739c953358c865005855253af4f68a497John McCall
1317cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
13182865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
1319cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1320cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1321cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
1322cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1323cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
1324cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1325cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1326cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
1327cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
1328cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1329cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
1330cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1331cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1332cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
1333cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
133454e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
13352865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
133654e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
133791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  assert(Context.getCanonicalType(T) != Context.OverloadTy &&
13389625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
133991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
134069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // C++0x [dcl.ref]p6:
134191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   If a typedef (7.1.3), a type template-parameter (14.3.1), or a
134291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
134391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   type T, an attempt to create the type "lvalue reference to cv TR" creates
134491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   the type "lvalue reference to T", while an attempt to create the type
134569d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   "rvalue reference to cv TR" creates the type TR.
134654e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
134754e14c4db764c0636160d26c5bbf491637c83a76John McCall
134854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
134954e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
135054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
135154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
135254e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
135354e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
135454e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
135554e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
135654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
135754e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
135854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
135969d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // collapsing of references-to-references as described in C++0x.
136069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // DR 106 and 540 introduce reference-collapsing into C++98/03.
1361cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1362cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
136333a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
1364cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
1365cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
1366cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
1367cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1368cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1369cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1370f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build references to unqualified pointers.
13714e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1372f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true);
1373f85e193739c953358c865005855253af4f68a497John McCall
1374cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
13757c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
13762865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
13772865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
1378cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1379cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1380e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// Check whether the specified array size makes the array type a VLA.  If so,
1381e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// return true, if not, return the size of the array in SizeVal.
1382282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smithstatic bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal) {
1383282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode
1384282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // (like gnu99, but not c99) accept any evaluatable value as an extension.
1385ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  class VLADiagnoser : public Sema::VerifyICEDiagnoser {
1386ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  public:
1387ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    VLADiagnoser() : Sema::VerifyICEDiagnoser(true) {}
138891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1389ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) {
1390ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
139191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1392ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseFold(Sema &S, SourceLocation Loc, SourceRange SR) {
1393ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      S.Diag(Loc, diag::ext_vla_folded_to_constant) << SR;
1394ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
1395ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  } Diagnoser;
139691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1397ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  return S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser,
1398ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                           S.LangOpts.GNUMode).isInvalid();
1399e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner}
1400e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1401e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1402cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
1403cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1404cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
1405cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1406cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
14071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
14081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
1409cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1410efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Brackets The range from the opening '[' to the closing ']'.
1411cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1412cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
1413cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1414cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1415cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
1416cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
1417cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1418cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
14197e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
14200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
14217e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
14224e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
1423138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
1424138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
142591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   type, the (possibly cv-qualified) type void, a function type or an
1426138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
1427138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
1428bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    // C++ [dcl.array]p3:
1429bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   When several "array of" specifications are adjacent, [...] only the
1430bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   first of the constant expressions that specify the bounds of the arrays
1431bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   may be omitted.
1432bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //
1433138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
1434138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
1435138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
1436138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
1437138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1438138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
143991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1440bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    if (T->isVoidType() || T->isIncompleteArrayType()) {
1441923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1442923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1443923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
144491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
144591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (RequireNonAbstractType(Brackets.getBegin(), T,
1446138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
1447138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
144891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1449923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
1450138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1451138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
1452923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
1453923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
1454923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1455923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
1456cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1457cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
1458cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
1459ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1460cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1461cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
14621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14636217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
1464cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
1465cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
1466cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
1467cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
1468c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
1469c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1470c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
1471cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
14721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1473806054db6653d29cb0d9692df3612cbcd03d0530John McCall  // Do placeholder conversions on the array size expression.
1474806054db6653d29cb0d9692df3612cbcd03d0530John McCall  if (ArraySize && ArraySize->hasPlaceholderType()) {
1475806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ExprResult Result = CheckPlaceholderExpr(ArraySize);
1476806054db6653d29cb0d9692df3612cbcd03d0530John McCall    if (Result.isInvalid()) return QualType();
1477806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ArraySize = Result.take();
1478806054db6653d29cb0d9692df3612cbcd03d0530John McCall  }
1479806054db6653d29cb0d9692df3612cbcd03d0530John McCall
14805e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
1481429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ArraySize && !ArraySize->isRValue()) {
1482429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult Result = DefaultLvalueConversion(ArraySize);
1483429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Result.isInvalid())
1484429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return QualType();
1485429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
1486429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ArraySize = Result.take();
1487429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
14885e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1489cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
1490282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // C++11 allows contextual conversions to such types.
149180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (!getLangOpts().CPlusPlus11 &&
1492282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      ArraySize && !ArraySize->isTypeDependent() &&
14931274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1494cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1495cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
1496cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1497cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1498282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
14992767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
1500cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
1501f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
15027e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
1503f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
1504f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
1505ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
15067e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
1507282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  } else if ((!T->isDependentType() && !T->isIncompleteType() &&
1508282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith              !T->isConstantSizeType()) ||
1509282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith             isArraySizeVLA(*this, ArraySize, ConstVal)) {
1510282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // Even in C++11, don't allow contextual conversions in the array bound
1511282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // of a VLA.
151280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (getLangOpts().CPlusPlus11 &&
1513282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1514282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1515282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        << ArraySize->getType() << ArraySize->getSourceRange();
1516282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      return QualType();
1517282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    }
1518282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
1519e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with an element type that has a non-constant-size is a VLA.
1520e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with a non-ICE size is a VLA.  We accept any expression
1521e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // that we can fold to a non-zero positive value as an extension.
15227e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1523cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
1524cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1525cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
1526923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
1527b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
1528b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1529b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1530b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
1531b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1532b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
1533923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1534923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1535923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
153602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
153702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
153891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      Diag(ArraySize->getLocStart(),
153902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
154002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
1541923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
154220cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne
154320cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      if (ASM == ArrayType::Static) {
154420cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        Diag(ArraySize->getLocStart(),
154520cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne             diag::warn_typecheck_zero_static_array_size)
154620cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne          << ArraySize->getSourceRange();
154720cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        ASM = ArrayType::Normal;
154820cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      }
154991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
15502767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor               !T->isIncompleteType()) {
155191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // Is the array too large?
15522767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
15532767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
15542767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
15552767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
15562767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
15572767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
15581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
155991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
156046a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
1561cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1562617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly
1563617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  // OpenCL v1.2 s6.9.d: variable length arrays are not supported.
1564617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  if (getLangOpts().OpenCL && T->isVariableArrayType()) {
1565617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    Diag(Loc, diag::err_opencl_vla);
1566617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    return QualType();
1567617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  }
1568af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
15694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().C99) {
15700fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
15710fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
157239b0e269dc8895ecc0f92f08126d3082b2a837a8Richard Smith      // FIXME: C++1y allows this.
1573f85e193739c953358c865005855253af4f68a497John McCall      QualType BaseT = Context.getBaseElementType(T);
157491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      if (!T->isDependentType() &&
15753c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT.isPODType(Context) &&
15763c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT->isObjCLifetimeType()) {
15770fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
1578f85e193739c953358c865005855253af4f68a497John McCall          << BaseT;
15790fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
158091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
1581a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
1582a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
1583a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
1584a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
1585a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
15860fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
15870fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
158839b0e269dc8895ecc0f92f08126d3082b2a837a8Richard Smith        Diag(Loc, getLangOpts().CPlusPlus1y
158939b0e269dc8895ecc0f92f08126d3082b2a837a8Richard Smith                      ? diag::warn_cxx11_compat_array_of_runtime_bound
159039b0e269dc8895ecc0f92f08126d3082b2a837a8Richard Smith                      : diag::ext_vla);
15910fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
1592d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith      Diag(Loc,
15934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie           getLangOpts().CPlusPlus? diag::err_c99_array_usage_cxx
1594d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith                                     : diag::ext_c99_array_usage) << ASM;
1595cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1596cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1597630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko  if (T->isVariableArrayType()) {
1598630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko    // Warn about VLAs for -Wvla.
1599630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko    Diag(Loc, diag::warn_vla_used);
1600630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko  }
1601630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko
1602cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
1603cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
16049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
16059cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
16069cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
16079cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
16089ae2f076ca5ab1feb3ba95629099ec2319833701John McCallQualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
16099cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
16109cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
16119cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
16121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
16139cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
16149cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
16159cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
16169cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
16179cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
16189ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
16199cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
16209ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
16219cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
16229ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        << "ext_vector_type" << ArraySize->getSourceRange();
16239cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
16249cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
16251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
16279cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
16281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
16291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16309cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
16319cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
16329ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      << ArraySize->getSourceRange();
16339cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
16349cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
16351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16364ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    return Context.getExtVectorType(T, vectorSize);
16371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
16381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16399ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
16409cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
16411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1642724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
1643bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                 llvm::MutableArrayRef<QualType> ParamTypes,
1644fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
16450918989f0eed08870e50418df97d1486d977d773Jordan Rose                                 const FunctionProtoType::ExtProtoInfo &EPI) {
1646724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
164791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    Diag(Loc, diag::err_func_returning_array_function)
164858408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
1649724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1650724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1651aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1652aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // Functions cannot return half FP.
1653aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  if (T->isHalfType()) {
1654aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 <<
1655aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      FixItHint::CreateInsertion(Loc, "*");
1656aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    return QualType();
1657aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  }
1658aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1659724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
1660bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  for (unsigned Idx = 0, Cnt = ParamTypes.size(); Idx < Cnt; ++Idx) {
1661aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    // FIXME: Loc is too inprecise here, should use proper locations for args.
166279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]);
16632dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
1664724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
1665724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
1666aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    } else if (ParamType->isHalfType()) {
1667aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Disallow half FP arguments.
1668aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 <<
1669aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        FixItHint::CreateInsertion(Loc, "*");
1670aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Invalid = true;
1671724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
1672cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
167354e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
1674724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1675724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1676724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
1677724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1678724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1679bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  return Context.getFunctionType(T, ParamTypes, EPI);
1680724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
16811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1682949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
1683949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1684949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
1685949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
1686949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
1687949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
1688949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1689949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
1690949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
16911eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
16922865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
1693949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
1694949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
1695949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
1696949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
1697949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
1698949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1699949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
1700949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
1701949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1702949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
1703949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
1704949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
1705949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1706949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1707737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
1708949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
1709949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
17108d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
1711ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
1712949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1713949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1714949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1715949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
1716949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1717949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
1718949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1719949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1720949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1721949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
1722949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1723949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1724949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1725949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1726b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // C++ allows the class type in a member pointer to be an incomplete type.
1727b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // In the Microsoft ABI, the size of the member pointer can vary
1728b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // according to the class type, which means that we really need a
1729b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // complete type if possible, which means we need to instantiate templates.
1730b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  //
173184e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  // If template instantiation fails or the type is just incomplete, we have to
173284e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  // add an extra slot to the member pointer.  Yes, this does cause problems
173384e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  // when passing pointers between TUs that disagree about the size.
173484e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  if (Context.getTargetInfo().getCXXABI().isMicrosoft()) {
173584e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner    CXXRecordDecl *RD = Class->getAsCXXRecordDecl();
1736e93e2552e76ab704ec85919cc2c76f02b8b081eeReid Kleckner    if (RD && !RD->hasAttr<MSInheritanceAttr>()) {
173784e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // Lock in the inheritance model on the first use of a member pointer.
173884e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // Otherwise we may disagree about the size at different points in the TU.
173984e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // FIXME: MSVC picks a model on the first use that needs to know the size,
174084e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      // rather than on the first mention of the type, e.g. typedefs.
1741e93e2552e76ab704ec85919cc2c76f02b8b081eeReid Kleckner      if (RequireCompleteType(Loc, Class, 0) && !RD->isBeingDefined()) {
174284e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        // We know it doesn't have an attribute and it's incomplete, so use the
174384e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        // unspecified inheritance model.  If we're in the record body, we can
174484e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        // figure out the inheritance model.
174584e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(),
174684e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner             E = RD->redecls_end(); I != E; ++I) {
174784e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner          I->addAttr(::new (Context) UnspecifiedInheritanceAttr(
174884e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner              RD->getSourceRange(), Context));
174984e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner        }
175084e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner      }
175184e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner    }
175284e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner  }
1753679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos
17542865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
1755949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
17561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17579a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
17589a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
17599a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
17609a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
1761efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Loc The source location, used for diagnostics.
17629a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
17639a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
17649a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
17659a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
17669a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
17679a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
176891cbbbf506c892a26d4301e2b3ccd377b0938817Chad RosierQualType Sema::BuildBlockPointerType(QualType T,
17691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
17709a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
17710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
17729a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
17739a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
17749a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
17751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17762865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
17779a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
17789a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
1779b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1780b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
17813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
1782a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
17833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
17843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
17853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1786a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
1787f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
1788e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
1789a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
1790e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
17911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1792a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
1793e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
1794e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
1795e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
1796a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
1797a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
1798a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            unsigned chunkIndex);
1799a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis
1800f85e193739c953358c865005855253af4f68a497John McCall/// Given that this is the declaration of a parameter under ARC,
1801f85e193739c953358c865005855253af4f68a497John McCall/// attempt to infer attributes and such for pointer-to-whatever
1802f85e193739c953358c865005855253af4f68a497John McCall/// types.
1803f85e193739c953358c865005855253af4f68a497John McCallstatic void inferARCWriteback(TypeProcessingState &state,
1804f85e193739c953358c865005855253af4f68a497John McCall                              QualType &declSpecType) {
1805f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
1806f85e193739c953358c865005855253af4f68a497John McCall  Declarator &declarator = state.getDeclarator();
1807f85e193739c953358c865005855253af4f68a497John McCall
1808f85e193739c953358c865005855253af4f68a497John McCall  // TODO: should we care about decl qualifiers?
1809f85e193739c953358c865005855253af4f68a497John McCall
1810f85e193739c953358c865005855253af4f68a497John McCall  // Check whether the declarator has the expected form.  We walk
1811f85e193739c953358c865005855253af4f68a497John McCall  // from the inside out in order to make the block logic work.
1812f85e193739c953358c865005855253af4f68a497John McCall  unsigned outermostPointerIndex = 0;
1813f85e193739c953358c865005855253af4f68a497John McCall  bool isBlockPointer = false;
1814f85e193739c953358c865005855253af4f68a497John McCall  unsigned numPointers = 0;
1815f85e193739c953358c865005855253af4f68a497John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
1816f85e193739c953358c865005855253af4f68a497John McCall    unsigned chunkIndex = i;
1817f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex);
1818f85e193739c953358c865005855253af4f68a497John McCall    switch (chunk.Kind) {
1819f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Paren:
1820f85e193739c953358c865005855253af4f68a497John McCall      // Ignore parens.
1821f85e193739c953358c865005855253af4f68a497John McCall      break;
1822f85e193739c953358c865005855253af4f68a497John McCall
1823f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Reference:
1824f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Pointer:
1825f85e193739c953358c865005855253af4f68a497John McCall      // Count the number of pointers.  Treat references
1826f85e193739c953358c865005855253af4f68a497John McCall      // interchangeably as pointers; if they're mis-ordered, normal
1827f85e193739c953358c865005855253af4f68a497John McCall      // type building will discover that.
1828f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1829f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1830f85e193739c953358c865005855253af4f68a497John McCall      break;
1831f85e193739c953358c865005855253af4f68a497John McCall
1832f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::BlockPointer:
1833f85e193739c953358c865005855253af4f68a497John McCall      // If we have a pointer to block pointer, that's an acceptable
1834f85e193739c953358c865005855253af4f68a497John McCall      // indirect reference; anything else is not an application of
1835f85e193739c953358c865005855253af4f68a497John McCall      // the rules.
1836f85e193739c953358c865005855253af4f68a497John McCall      if (numPointers != 1) return;
1837f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1838f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1839f85e193739c953358c865005855253af4f68a497John McCall      isBlockPointer = true;
1840f85e193739c953358c865005855253af4f68a497John McCall
1841f85e193739c953358c865005855253af4f68a497John McCall      // We don't care about pointer structure in return values here.
1842f85e193739c953358c865005855253af4f68a497John McCall      goto done;
1843f85e193739c953358c865005855253af4f68a497John McCall
1844f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Array: // suppress if written (id[])?
1845f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Function:
1846f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::MemberPointer:
1847f85e193739c953358c865005855253af4f68a497John McCall      return;
1848f85e193739c953358c865005855253af4f68a497John McCall    }
1849f85e193739c953358c865005855253af4f68a497John McCall  }
1850f85e193739c953358c865005855253af4f68a497John McCall done:
1851f85e193739c953358c865005855253af4f68a497John McCall
1852f85e193739c953358c865005855253af4f68a497John McCall  // If we have *one* pointer, then we want to throw the qualifier on
1853f85e193739c953358c865005855253af4f68a497John McCall  // the declaration-specifiers, which means that it needs to be a
1854f85e193739c953358c865005855253af4f68a497John McCall  // retainable object type.
1855f85e193739c953358c865005855253af4f68a497John McCall  if (numPointers == 1) {
1856f85e193739c953358c865005855253af4f68a497John McCall    // If it's not a retainable object type, the rule doesn't apply.
1857f85e193739c953358c865005855253af4f68a497John McCall    if (!declSpecType->isObjCRetainableType()) return;
1858f85e193739c953358c865005855253af4f68a497John McCall
1859f85e193739c953358c865005855253af4f68a497John McCall    // If it already has lifetime, don't do anything.
1860f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType.getObjCLifetime()) return;
1861f85e193739c953358c865005855253af4f68a497John McCall
1862f85e193739c953358c865005855253af4f68a497John McCall    // Otherwise, modify the type in-place.
1863f85e193739c953358c865005855253af4f68a497John McCall    Qualifiers qs;
186491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1865f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType->isObjCARCImplicitlyUnretainedType())
1866f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
1867f85e193739c953358c865005855253af4f68a497John McCall    else
1868f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing);
1869f85e193739c953358c865005855253af4f68a497John McCall    declSpecType = S.Context.getQualifiedType(declSpecType, qs);
1870f85e193739c953358c865005855253af4f68a497John McCall
1871f85e193739c953358c865005855253af4f68a497John McCall  // If we have *two* pointers, then we want to throw the qualifier on
1872f85e193739c953358c865005855253af4f68a497John McCall  // the outermost pointer.
1873f85e193739c953358c865005855253af4f68a497John McCall  } else if (numPointers == 2) {
1874f85e193739c953358c865005855253af4f68a497John McCall    // If we don't have a block pointer, we need to check whether the
1875f85e193739c953358c865005855253af4f68a497John McCall    // declaration-specifiers gave us something that will turn into a
1876f85e193739c953358c865005855253af4f68a497John McCall    // retainable object pointer after we slap the first pointer on it.
1877f85e193739c953358c865005855253af4f68a497John McCall    if (!isBlockPointer && !declSpecType->isObjCObjectType())
1878f85e193739c953358c865005855253af4f68a497John McCall      return;
1879f85e193739c953358c865005855253af4f68a497John McCall
1880f85e193739c953358c865005855253af4f68a497John McCall    // Look for an explicit lifetime attribute there.
1881f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex);
18821c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis    if (chunk.Kind != DeclaratorChunk::Pointer &&
18831c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis        chunk.Kind != DeclaratorChunk::BlockPointer)
18841c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis      return;
1885f85e193739c953358c865005855253af4f68a497John McCall    for (const AttributeList *attr = chunk.getAttrs(); attr;
1886f85e193739c953358c865005855253af4f68a497John McCall           attr = attr->getNext())
18878e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt      if (attr->getKind() == AttributeList::AT_ObjCOwnership)
1888f85e193739c953358c865005855253af4f68a497John McCall        return;
1889f85e193739c953358c865005855253af4f68a497John McCall
1890a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis    transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing,
1891a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                          outermostPointerIndex);
1892f85e193739c953358c865005855253af4f68a497John McCall
1893f85e193739c953358c865005855253af4f68a497John McCall  // Any other number of pointers/references does not trigger the rule.
1894f85e193739c953358c865005855253af4f68a497John McCall  } else return;
1895f85e193739c953358c865005855253af4f68a497John McCall
1896f85e193739c953358c865005855253af4f68a497John McCall  // TODO: mark whether we did this inference?
1897f85e193739c953358c865005855253af4f68a497John McCall}
1898f85e193739c953358c865005855253af4f68a497John McCall
18998c952cd40ccec9d720931f27e7d722fed207d536Richard Smithstatic void diagnoseIgnoredQualifiers(
19008c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    Sema &S, unsigned Quals,
19018c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation FallbackLoc,
19028c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation ConstQualLoc = SourceLocation(),
19038c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation VolatileQualLoc = SourceLocation(),
19048c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation RestrictQualLoc = SourceLocation(),
19058c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation AtomicQualLoc = SourceLocation()) {
1906eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith  if (!Quals)
1907eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith    return;
1908eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith
19098c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  const SourceManager &SM = S.getSourceManager();
19108c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19118c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  struct Qual {
19128c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    unsigned Mask;
19138c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    const char *Name;
19148c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation Loc;
19158c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  } const QualKinds[4] = {
19168c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_const, "const", ConstQualLoc },
19178c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_volatile, "volatile", VolatileQualLoc },
19188c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_restrict, "restrict", RestrictQualLoc },
19198c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_atomic, "_Atomic", AtomicQualLoc }
19208c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  };
19218c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19228c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  llvm::SmallString<32> QualStr;
1923d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  unsigned NumQuals = 0;
1924d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  SourceLocation Loc;
19258c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  FixItHint FixIts[4];
19268c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19278c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // Build a string naming the redundant qualifiers.
19288c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  for (unsigned I = 0; I != 4; ++I) {
19298c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    if (Quals & QualKinds[I].Mask) {
19308c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if (!QualStr.empty()) QualStr += ' ';
19318c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      QualStr += QualKinds[I].Name;
19328c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19338c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // If we have a location for the qualifier, offer a fixit.
19348c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      SourceLocation QualLoc = QualKinds[I].Loc;
19358c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if (!QualLoc.isInvalid()) {
19368c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        FixIts[NumQuals] = FixItHint::CreateRemoval(QualLoc);
19378c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        if (Loc.isInvalid() || SM.isBeforeInTranslationUnit(QualLoc, Loc))
19388c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          Loc = QualLoc;
19398c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      }
1940d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
19418c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      ++NumQuals;
19428c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
19438c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  }
1944d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
19458c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  S.Diag(Loc.isInvalid() ? FallbackLoc : Loc, diag::warn_qual_return_type)
19468c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    << QualStr << NumQuals << FixIts[0] << FixIts[1] << FixIts[2] << FixIts[3];
19478c952cd40ccec9d720931f27e7d722fed207d536Richard Smith}
1948a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg
19498c952cd40ccec9d720931f27e7d722fed207d536Richard Smith// Diagnose pointless type qualifiers on the return type of a function.
19508c952cd40ccec9d720931f27e7d722fed207d536Richard Smithstatic void diagnoseIgnoredFunctionQualifiers(Sema &S, QualType RetTy,
19518c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                              Declarator &D,
19528c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                              unsigned FunctionChunkIndex) {
19538c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  if (D.getTypeObject(FunctionChunkIndex).Fun.hasTrailingReturnType()) {
19548c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    // FIXME: TypeSourceInfo doesn't preserve location information for
19558c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    // qualifiers.
1956eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith    diagnoseIgnoredQualifiers(S, RetTy.getLocalCVRQualifiers(),
19578c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                              D.getIdentifierLoc());
19588c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    return;
1959d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
19608c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19618c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  for (unsigned OuterChunkIndex = FunctionChunkIndex + 1,
19628c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                End = D.getNumTypeObjects();
19638c952cd40ccec9d720931f27e7d722fed207d536Richard Smith       OuterChunkIndex != End; ++OuterChunkIndex) {
19648c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    DeclaratorChunk &OuterChunk = D.getTypeObject(OuterChunkIndex);
19658c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    switch (OuterChunk.Kind) {
19668c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Paren:
19678c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      continue;
19688c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19698c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Pointer: {
19708c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      DeclaratorChunk::PointerTypeInfo &PTI = OuterChunk.Ptr;
19718c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      diagnoseIgnoredQualifiers(
19728c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          S, PTI.TypeQuals,
19738c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation(),
19748c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
19758c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
19768c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
19778c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.AtomicQualLoc));
19788c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      return;
19798c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
19808c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19818c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Function:
19828c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::BlockPointer:
19838c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Reference:
19848c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Array:
19858c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::MemberPointer:
19868c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // FIXME: We can't currently provide an accurate source location and a
19878c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // fix-it hint for these.
1988eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith      unsigned AtomicQual = RetTy->isAtomicType() ? DeclSpec::TQ_atomic : 0;
19898c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      diagnoseIgnoredQualifiers(S, RetTy.getCVRQualifiers() | AtomicQual,
19908c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                D.getIdentifierLoc());
19918c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      return;
19928c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
19938c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19948c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    llvm_unreachable("unknown declarator chunk kind");
19954cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
1996d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
19978c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // If the qualifiers come from a conversion function type, don't diagnose
19988c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // them -- they're not necessarily redundant, since such a conversion
19998c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // operator can be explicitly called as "x.operator const int()".
20008c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  if (D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId)
20018c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    return;
2002d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
20038c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // Just parens all the way out to the decl specifiers. Diagnose any qualifiers
20048c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // which are present there.
2005eb82a53aaa7880b7d3fd733aeee38b9aeee919baRichard Smith  diagnoseIgnoredQualifiers(S, D.getDeclSpec().getTypeQualifiers(),
20068c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getIdentifierLoc(),
20078c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getConstSpecLoc(),
20088c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getVolatileSpecLoc(),
20098c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getRestrictSpecLoc(),
20108c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getAtomicSpecLoc());
2011d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth}
2012d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
20138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
20148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                             TypeSourceInfo *&ReturnTypeInfo) {
20158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &SemaRef = state.getSema();
20168cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
2017930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
20188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ReturnTypeInfo = 0;
2019711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
20208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // The TagDecl owned by the DeclSpec.
20218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TagDecl *OwnedTagDecl = 0;
20228999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl
20233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
202498a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian  case UnqualifiedId::IK_ImplicitSelfParam:
20253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
20268999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  case UnqualifiedId::IK_Identifier:
20270486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
20283f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
20298cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = ConvertDeclSpecToType(state);
203091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2031591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
2032d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
203315987970eeaa1842c29ec8797affd1c1dea05585Abramo Bagnara      // Owned declaration is embedded in declarator.
2034d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl->setEmbeddedInDeclarator(true);
2035591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
2036930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
2037930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
20383f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
20390efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
20403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
2041930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
204291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // "void" instead.
20438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = SemaRef.Context.VoidTy;
2044a78a640c5f59720f2c2b8034eca4fbf8525d9026Rafael Espindola    if (AttributeList *attrs = D.getDeclSpec().getAttributes().getList())
2045f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclSpec, attrs);
2046930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
204748026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
204848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
204948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
205048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
205191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId,
20528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  &ReturnTypeInfo);
205348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
2054930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
2055dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
2056711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
2057711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
2058711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2059d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
2060d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // In C++11, a function declarator using 'auto' must have a trailing return
20618110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // type (this is checked later) and we can skip this. In other languages
20628110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // using auto, we need to check regardless.
2063a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  if (D.getDeclSpec().containsPlaceholderType() &&
206480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      (!SemaRef.getLangOpts().CPlusPlus11 || !D.isFunctionDeclarator())) {
2065baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
20661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2067baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
2068baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
2069b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("K&R type lists aren't allowed in C++");
2070f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2071f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman      llvm_unreachable("Can't specify a type specifier in lambda grammar");
2072cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2073cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
2074baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
2075baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
2076baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2077baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
20787a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)
20797a614d8380297fcd2bc23986241905d97222948cRichard Smith        break;
20808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) {
2081eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie      case TTK_Enum: llvm_unreachable("unhandled tag kind");
2082465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
2083465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
2084465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
20856666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      case TTK_Interface: Error = 4; /* Interface member */ break;
20861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
2087baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2088baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
208917b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
20906666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 5; // Exception declaration
2091baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2092baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
20936666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 6; // Template parameter
2094baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2095baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
20966666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 7; // Block literal
209734b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
209834b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TemplateTypeArgContext:
20996666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 8; // Template type argument
210034b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
2101162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
21023e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
21036666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 10; // Type alias
2104162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      break;
21057796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
21066666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11; // Function return type
21077796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
210834b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TypeNameContext:
21096666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 12; // Generic
2110baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2111baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
2112baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
2113baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
2114baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
21150b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
2116baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2117baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
2118baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
2119ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith    if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
21206666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 9;
2121ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith
21228110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    // In Objective-C it is an error to use 'auto' on a function declarator.
21238110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    if (D.isFunctionDeclarator())
21246666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11;
21258110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith
2126d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.spec.auto]p2: 'auto' is always fine if the declarator
2127e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // contains a trailing return type. That is only legal at the outermost
2128e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // level. Check all declarator chunks (outermost first) anyway, to give
2129e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // better diagnostics.
213080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (SemaRef.getLangOpts().CPlusPlus11 && Error != -1) {
2131e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2132e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned chunkIndex = e - i - 1;
2133e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        state.setCurrentChunkIndex(chunkIndex);
2134e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2135e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (DeclType.Kind == DeclaratorChunk::Function) {
2136e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
213754655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (FTI.hasTrailingReturnType()) {
2138e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Error = -1;
2139e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            break;
2140e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          }
2141e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        }
2142e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2143e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    }
2144e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2145baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
21468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
21478cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_auto_not_allowed)
2148baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
21498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = SemaRef.Context.IntTy;
2150baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
21510aa86c0463a881be85fd34e04c7de3379997621dRichard Smith    } else
21520aa86c0463a881be85fd34e04c7de3379997621dRichard Smith      SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
21530aa86c0463a881be85fd34e04c7de3379997621dRichard Smith                   diag::warn_cxx98_compat_auto_type_specifier);
2154baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
21558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
21564e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().CPlusPlus &&
21575e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall      OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) {
21588cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // Check the contexts where C++ forbids the declaration of a new class
21598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // or enumeration in a type-specifier-seq.
21608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    switch (D.getContext()) {
21617796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
21627796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // Class and enumeration definitions are syntactically not allowed in
21637796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // trailing return types.
21647796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      llvm_unreachable("parser should not have allowed this");
21657796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
21668cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::FileContext:
21678cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::MemberContext:
21688cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockContext:
21698cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ForContext:
21708cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockLiteralContext:
2171f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2172d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // C++11 [dcl.type]p3:
21738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   A type-specifier-seq shall not define a class or enumeration unless
21748cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   it appears in the type-id of an alias-declaration (7.1.3) that is not
21758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   the declaration of a template-declaration.
21768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasDeclContext:
21778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
21788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasTemplateContext:
21798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
21808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_alias_template)
21818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2182601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
21838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
21848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TypeNameContext:
21858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateParamContext:
21868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXNewContext:
21878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXCatchContext:
21888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ObjCCatchContext:
21898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateTypeArgContext:
21908cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
21918cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_type_specifier)
21928cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2193601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
21948cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
21958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::PrototypeContext:
2196cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2197cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
21988cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::KNRTypeListContext:
21998cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ [dcl.fct]p6:
22008cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   Types shall not be defined in return or parameter types.
22018cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_param_type)
22038cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2204601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22058cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22068cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ConditionContext:
22078cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ 6.4p2:
22088cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // The type-specifier-seq shall not contain typedef and shall not declare
22098cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // a new class or enumeration.
22108cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
22118cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_condition);
2212601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
22138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
22148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    }
22158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  }
22168cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
22178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return T;
22188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
22198cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2220a08c2fb74ef823c185619ecc532f8fced6a1982fBenjamin Kramerstatic std::string getFunctionQualifiersAsString(const FunctionProtoType *FnTy){
2221d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  std::string Quals =
2222d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
2223d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2224d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (FnTy->getRefQualifier()) {
2225d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_None:
2226d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2227d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2228d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_LValue:
2229d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2230d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2231d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += '&';
2232d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2233d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2234d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_RValue:
2235d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2236d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2237d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += "&&";
2238d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2239d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2240d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2241d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  return Quals;
2242d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2243d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2244d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// Check that the function type T, which has a cv-qualifier or a ref-qualifier,
2245d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// can be contained within the declarator chunk DeclType, and produce an
2246d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// appropriate diagnostic if not.
2247d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smithstatic void checkQualifiedFunction(Sema &S, QualType T,
2248d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                   DeclaratorChunk &DeclType) {
2249d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++98 [dcl.fct]p4 / C++11 [dcl.fct]p6: a function type with a
2250d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // cv-qualifier or a ref-qualifier can only appear at the topmost level
2251d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // of a type.
2252d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  int DiagKind = -1;
2253d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (DeclType.Kind) {
2254d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Paren:
2255d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::MemberPointer:
2256d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases are permitted.
2257d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2258d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Array:
2259d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Function:
2260d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases don't allow function types at all; no need to diagnose the
2261d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // qualifiers separately.
2262d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2263d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::BlockPointer:
2264d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 0;
2265d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2266d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Pointer:
2267d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 1;
2268d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2269d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Reference:
2270d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 2;
2271d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2272d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2273d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2274d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  assert(DiagKind != -1);
2275d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  S.Diag(DeclType.Loc, diag::err_compound_qualified_function_type)
2276d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << DiagKind << isa<FunctionType>(T.IgnoreParens()) << T
2277d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << getFunctionQualifiersAsString(T->castAs<FunctionProtoType>());
2278d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2279d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2280b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// Produce an approprioate diagnostic for an ambiguity between a function
2281b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// declarator and a C++ direct-initializer.
2282b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smithstatic void warnAboutAmbiguousFunction(Sema &S, Declarator &D,
2283b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith                                       DeclaratorChunk &DeclType, QualType RT) {
2284b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2285b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  assert(FTI.isAmbiguous && "no direct-initializer / function ambiguity");
2286b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2287b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // If the return type is void there is no ambiguity.
2288b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isVoidType())
2289b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2290b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2291b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a non-class type can have at most one argument.
2292b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!RT->isRecordType() && FTI.NumArgs > 1)
2293b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2294b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2295b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a reference must have exactly one argument.
2296b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isReferenceType() && FTI.NumArgs != 1)
2297b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2298b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2299b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Only warn if this declarator is declaring a function at block scope, and
2300b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // doesn't have a storage class (such as 'extern') specified.
2301b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!D.isFunctionDeclarator() ||
2302b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      D.getFunctionDefinitionKind() != FDK_Declaration ||
2303b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      !S.CurContext->isFunctionOrMethod() ||
2304d2615cc53b916e8aae45783ca7113b93de515ce3Rafael Espindola      D.getDeclSpec().getStorageClassSpec()
2305b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        != DeclSpec::SCS_unspecified)
2306b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2307b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2308b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Inside a condition, a direct initializer is not permitted. We allow one to
2309b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // be parsed in order to give better diagnostics in condition parsing.
2310b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (D.getContext() == Declarator::ConditionContext)
2311b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2312b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2313b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  SourceRange ParenRange(DeclType.Loc, DeclType.EndLoc);
2314b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2315d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  S.Diag(DeclType.Loc,
2316d64effc4e31044c05d6e4400150edb26e914983aRichard Smith         FTI.NumArgs ? diag::warn_parens_disambiguated_as_function_declaration
2317d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                     : diag::warn_empty_parens_are_function_decl)
2318d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    << ParenRange;
2319d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2320d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // If the declaration looks like:
2321d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   T var1,
2322d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   f();
2323d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // and name lookup finds a function named 'f', then the ',' was
2324d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // probably intended to be a ';'.
2325d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (!D.isFirstDeclarator() && D.getIdentifier()) {
2326d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Comma(D.getCommaLoc(), S.SourceMgr);
2327d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Name(D.getIdentifierLoc(), S.SourceMgr);
2328d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    if (Comma.getFileID() != Name.getFileID() ||
2329d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) {
2330d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      LookupResult Result(S, D.getIdentifier(), SourceLocation(),
2331d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                          Sema::LookupOrdinaryName);
2332d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      if (S.LookupName(Result, S.getCurScope()))
2333d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        S.Diag(D.getCommaLoc(), diag::note_empty_parens_function_call)
2334d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << FixItHint::CreateReplacement(D.getCommaLoc(), ";")
2335d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << D.getIdentifier();
2336d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    }
2337d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  }
2338b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2339d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (FTI.NumArgs > 0) {
2340d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration with parameters, eg. "T var(T());", suggest adding parens
2341d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // around the first parameter to turn the declaration into a variable
2342d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2343b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceRange Range = FTI.ArgInfo[0].Param->getSourceRange();
2344b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation B = Range.getBegin();
2345b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation E = S.PP.getLocForEndOfToken(Range.getEnd());
2346b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // FIXME: Maybe we should suggest adding braces instead of parens
2347b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // in C++11 for classes that don't have an initializer_list constructor.
2348b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    S.Diag(B, diag::note_additional_parens_for_variable_declaration)
2349b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(B, "(")
2350b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(E, ")");
2351d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  } else {
2352d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration without parameters, eg. "T var();", suggest replacing the
2353d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // parens with an initializer to turn the declaration into a variable
2354d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2355b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
2356d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2357b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // Empty parens mean value-initialization, and no parens mean
2358b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // default initialization. These are equivalent if the default
2359b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // constructor is user-provided or if zero-initialization is a
2360b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // no-op.
2361b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    if (RD && RD->hasDefinition() &&
2362b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        (RD->isEmpty() || RD->hasUserProvidedDefaultConstructor()))
2363b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor)
2364b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        << FixItHint::CreateRemoval(ParenRange);
2365b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    else {
2366b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      std::string Init = S.getFixItZeroInitializerForType(RT);
236780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      if (Init.empty() && S.LangOpts.CPlusPlus11)
2368b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        Init = "{}";
2369b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (!Init.empty())
2370b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        S.Diag(DeclType.Loc, diag::note_empty_parens_zero_initialize)
2371b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith          << FixItHint::CreateReplacement(ParenRange, Init);
2372b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    }
2373b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  }
2374b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith}
2375b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
23768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
23778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                QualType declSpecType,
23788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                TypeSourceInfo *TInfo) {
23798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
23808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = declSpecType;
23818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
23828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
23838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ASTContext &Context = S.Context;
23844e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &LangOpts = S.getLangOpts();
23858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2386cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
2387cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
2388cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
2389cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
23901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2391162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  // Does this declaration declare a typedef-name?
2392162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  bool IsTypedefName =
2393162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef ||
23943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasDeclContext ||
23953e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasTemplateContext;
2396162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2397d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // Does T refer to a function type with a cv-qualifier or a ref-qualifier?
2398d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  bool IsQualifiedFunction = T->isFunctionProtoType() &&
2399d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      (T->castAs<FunctionProtoType>()->getTypeQuals() != 0 ||
2400d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith       T->castAs<FunctionProtoType>()->getRefQualifier() != RQ_None);
2401d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2402a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  // If T is 'decltype(auto)', the only declarators we can have are parens
2403a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  // and at most one function declarator if this is a function declaration.
2404a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  if (const AutoType *AT = T->getAs<AutoType>()) {
2405a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    if (AT->isDecltypeAuto()) {
2406a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) {
2407a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        unsigned Index = E - I - 1;
2408a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        DeclaratorChunk &DeclChunk = D.getTypeObject(Index);
2409a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        unsigned DiagId = diag::err_decltype_auto_compound_type;
2410a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        unsigned DiagKind = 0;
2411a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        switch (DeclChunk.Kind) {
2412a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Paren:
2413a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          continue;
2414a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Function: {
2415a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          unsigned FnIndex;
2416a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          if (D.isFunctionDeclarationContext() &&
2417a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith              D.isFunctionDeclarator(FnIndex) && FnIndex == Index)
2418a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            continue;
2419a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagId = diag::err_decltype_auto_function_declarator_not_declaration;
2420a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2421a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        }
2422a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Pointer:
2423a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::BlockPointer:
2424a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::MemberPointer:
2425a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagKind = 0;
2426a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2427a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Reference:
2428a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagKind = 1;
2429a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2430a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        case DeclaratorChunk::Array:
2431a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          DiagKind = 2;
2432a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          break;
2433a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        }
2434a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
2435a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        S.Diag(DeclChunk.Loc, DiagId) << DiagKind;
2436a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        D.setInvalidType(true);
2437a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        break;
2438a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      }
2439a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    }
2440a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  }
2441a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
244298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
244398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
244498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
24458ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2446711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
2447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
2448711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2449d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction) {
2450d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      checkQualifiedFunction(S, T, DeclType);
2451d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = DeclType.Kind == DeclaratorChunk::Paren;
2452d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    }
24535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
2454075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
24558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildParenType(T);
2456075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
24575618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
24589af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
24599af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
24608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_blocks_disable);
24611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
24632865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
24648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
24655618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
24665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
24676a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
24686a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
24698cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
24708cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
24716a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
24726a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
24736a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
24748cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.ObjC1 && T->getAs<ObjCObjectType>()) {
2475c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
24762865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
24778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
247814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
247914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
24808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildPointerType(T, DeclType.Loc, Name);
24812865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
24828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
2483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
24845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
24850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
24866a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
24876a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
24888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
24898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
24906a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
24916a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
24926a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
24938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
24942865474261a608c7873b87ba4af110d17907896dJohn McCall
24952865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
24962865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
24978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
24985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
24990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
25005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
25016a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
25026a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
25038cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
25048cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
25056a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
25066a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
25076a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
2508fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
250994f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
25105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
25115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
25125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
25135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
25145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
25155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
25165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
2517c05a94b7accd4035bf5d5897c434c445b22da855John McCall      if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
2518f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
2519f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
2520f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
25218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
2522f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
2523f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
2524f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
25257f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
25267f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // C99 6.7.5.2p1: The optional type qualifiers and the keyword static
25277f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // shall appear only in a declaration of a function parameter with an
25287f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // array type, ...
25297f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      if (ASM == ArrayType::Static || ATI.TypeQuals) {
253099570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay        if (!(D.isPrototypeContext() ||
253199570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay              D.getContext() == Declarator::KNRTypeListContext)) {
25327f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          S.Diag(DeclType.Loc, diag::err_array_static_outside_prototype) <<
25337f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
25347f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Remove the 'static' and the type qualifiers.
25357f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          if (ASM == ArrayType::Static)
25367f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ASM = ArrayType::Normal;
25377f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          ATI.TypeQuals = 0;
25387f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          D.setInvalidType(true);
25397f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
25407f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
25417f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // C99 6.7.5.2p1: ... and then only in the outermost array type
25427f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // derivation.
25437f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        unsigned x = chunkIndex;
25447f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        while (x != 0) {
25457f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Walk outwards along the declarator chunks.
25467f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          x--;
25477f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          const DeclaratorChunk &DC = D.getTypeObject(x);
25487f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          switch (DC.Kind) {
25497f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Paren:
25507f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            continue;
25517f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Array:
25527f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Pointer:
25537f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Reference:
25547f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::MemberPointer:
25557f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            S.Diag(DeclType.Loc, diag::err_array_static_not_outermost) <<
25567f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
25577f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            if (ASM == ArrayType::Static)
25587f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              ASM = ArrayType::Normal;
25597f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ATI.TypeQuals = 0;
25607f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            D.setInvalidType(true);
25617f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
25627f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Function:
25637f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::BlockPointer:
25647f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            // These are invalid anyway, so just ignore.
25657f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
25667f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          }
25677f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
25687f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      }
25697f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
2570a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      if (const AutoType *AT = T->getContainedAutoType()) {
2571a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith        // We've already diagnosed this for decltype(auto).
2572dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        if (!AT->isDecltypeAuto())
2573a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith          S.Diag(DeclType.Loc, diag::err_illegal_decl_array_of_auto)
2574a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith            << getPrintableNameForEntity(Name) << T;
2575dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        T = QualType();
2576dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith        break;
2577a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith      }
2578a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith
25798ac2c66a1442985091c5ec2b33ce6d3df3bcb529Eli Friedman      T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
25808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                           SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
25815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
25825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2583f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
25845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
25855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
25865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
25875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2588d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = FTI.TypeQuals || FTI.hasRefQualifier();
25893cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
259034b41d939a1328f484511c6002ba2456db879a29Richard Smith      // Check for auto functions and trailing return type and adjust the
259134b41d939a1328f484511c6002ba2456db879a29Richard Smith      // return type accordingly.
259234b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!D.isInvalidType()) {
259334b41d939a1328f484511c6002ba2456db879a29Richard Smith        // trailing-return-type is only required if we're declaring a function,
259434b41d939a1328f484511c6002ba2456db879a29Richard Smith        // and not, for instance, a pointer to a function.
259534b41d939a1328f484511c6002ba2456db879a29Richard Smith        if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
259654655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            !FTI.hasTrailingReturnType() && chunkIndex == 0) {
25978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
259834b41d939a1328f484511c6002ba2456db879a29Richard Smith               diag::err_auto_missing_trailing_return);
259934b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = Context.IntTy;
260034b41d939a1328f484511c6002ba2456db879a29Richard Smith          D.setInvalidType(true);
260154655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        } else if (FTI.hasTrailingReturnType()) {
2602e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          // T must be exactly 'auto' at this point. See CWG issue 681.
2603e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (isa<ParenType>(T)) {
26048cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
2605e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith                 diag::err_trailing_return_in_parens)
2606e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith              << T << D.getDeclSpec().getSourceRange();
2607e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.setInvalidType(true);
2608f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman          } else if (D.getContext() != Declarator::LambdaExprContext &&
2609a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                     (T.hasQualifiers() || !isa<AutoType>(T) ||
2610a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                      cast<AutoType>(T)->isDecltypeAuto())) {
26118cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
261234b41d939a1328f484511c6002ba2456db879a29Richard Smith                 diag::err_trailing_return_without_auto)
261334b41d939a1328f484511c6002ba2456db879a29Richard Smith              << T << D.getDeclSpec().getSourceRange();
261434b41d939a1328f484511c6002ba2456db879a29Richard Smith            D.setInvalidType(true);
261534b41d939a1328f484511c6002ba2456db879a29Richard Smith          }
261654655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          T = S.GetTypeFromParser(FTI.getTrailingReturnType(), &TInfo);
261754655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (T.isNull()) {
261854655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            // An error occurred parsing the trailing return type.
261954655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            T = Context.IntTy;
262054655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            D.setInvalidType(true);
262154655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          }
262234b41d939a1328f484511c6002ba2456db879a29Richard Smith        }
262334b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
262434b41d939a1328f484511c6002ba2456db879a29Richard Smith
2625e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // C99 6.7.5.3p1: The return type may not be a function or array type.
2626e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // For conversion functions, we'll diagnose this particular error later.
2627e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      if ((T->isArrayType() || T->isFunctionType()) &&
2628e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
2629e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned diagID = diag::err_func_returning_array_function;
2630e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // Last processing chunk in block context means this function chunk
2631e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // represents the block.
2632e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (chunkIndex == 0 &&
2633e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.getContext() == Declarator::BlockLiteralContext)
2634e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          diagID = diag::err_block_returning_array_function;
26358cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
2636e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        T = Context.IntTy;
2637e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        D.setInvalidType(true);
2638e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2639e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2640aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Do not allow returning half FP value.
2641aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // FIXME: This really should be in BuildFunctionType.
2642aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      if (T->isHalfType()) {
264319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        if (S.getLangOpts().OpenCL) {
264419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          if (!S.getOpenCLOptions().cl_khr_fp16) {
264519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            S.Diag(D.getIdentifierLoc(), diag::err_opencl_half_return) << T;
264619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            D.setInvalidType(true);
264719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          }
264819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        } else {
264919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          S.Diag(D.getIdentifierLoc(),
265019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            diag::err_parameters_retval_cannot_have_fp16_type) << 1;
265119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          D.setInvalidType(true);
265219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        }
2653aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      }
2654aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
26555291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
26565291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
26578c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if ((T.getCVRQualifiers() || T->isAtomicType()) &&
26588c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          !(S.getLangOpts().CPlusPlus &&
26598c952cd40ccec9d720931f27e7d722fed207d536Richard Smith            (T->isDependentType() || T->isRecordType())))
26608c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        diagnoseIgnoredFunctionQualifiers(S, T, D, chunkIndex);
2661d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
266202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // Objective-C ARC ownership qualifiers are ignored on the function
266302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // return type (by type canonicalization). Complain if this attribute
266402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // was written here.
266502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      if (T.getQualifiers().hasObjCLifetime()) {
266602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        SourceLocation AttrLoc;
266702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (chunkIndex + 1 < D.getNumTypeObjects()) {
266802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
266902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr = ReturnTypeChunk.getAttrs();
267002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
267102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
267202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
267302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
267402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
267502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
267602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
267702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isInvalid()) {
267802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr
267902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor                 = D.getDeclSpec().getAttributes().getList();
268002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
268102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
268202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
268302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
268402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
268502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
268602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
268702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
268802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isValid()) {
268902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // The ownership attributes are almost always written via
269002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // the predefined
269102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // __strong/__weak/__autoreleasing/__unsafe_unretained.
269202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          if (AttrLoc.isMacroID())
269302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            AttrLoc = S.SourceMgr.getImmediateExpansionRange(AttrLoc).first;
269402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
269502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          S.Diag(AttrLoc, diag::warn_arc_lifetime_result_type)
269602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            << T.getQualifiers().getObjCLifetime();
269702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
269802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      }
269902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
27008cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
2701402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
2702402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
2703b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
27045e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall        if (Tag->isCompleteDefinition())
27058cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
2706402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
2707402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
2708402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
27093cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
27103cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
2711162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (IsTypedefName && FTI.getExceptionSpecType())
27128cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef)
27133e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          << (D.getContext() == Declarator::AliasDeclContext ||
27143e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith              D.getContext() == Declarator::AliasTemplateContext);
27153cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
2716b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // If we see "T var();" or "T var(T());" at block scope, it is probably
2717b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // an attempt to initialize a variable, not a function declaration.
2718b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (FTI.isAmbiguous)
2719b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        warnAboutAmbiguousFunction(S, D, DeclType, T);
2720b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
27218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (!FTI.NumArgs && !FTI.isVariadic && !LangOpts.CPlusPlus) {
27222865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
27232865474261a608c7873b87ba4af110d17907896dJohn McCall        T = Context.getFunctionNoProtoType(T);
27242865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
27252865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
27262865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
27272865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
27288cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        if (!FTI.NumArgs && FTI.isVariadic && !LangOpts.CPlusPlus) {
2729965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
2730965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
2731965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
27328e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt            if (Attrs->getKind() == AttributeList::AT_Overloadable) {
2733965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
2734965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
2735965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
2736965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
2737965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
2738965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
27398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
2740c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
27412865474261a608c7873b87ba4af110d17907896dJohn McCall
27422865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
2743788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
2744788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
27458cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
27462865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
2747d5668a2447c2afeea38815b51a80a5a6ac235599Argyrios Kyrtzidis          // Recover by creating a K&R-style function type.
2748d5668a2447c2afeea38815b51a80a5a6ac235599Argyrios Kyrtzidis          T = Context.getFunctionNoProtoType(T);
27492865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
27502865474261a608c7873b87ba4af110d17907896dJohn McCall        }
27512865474261a608c7873b87ba4af110d17907896dJohn McCall
2752e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
2753e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
275454655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        EPI.HasTrailingReturn = FTI.hasTrailingReturnType();
2755e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
2756c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
2757c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : FTI.RefQualifierIsLValueRef? RQ_LValue
2758c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : RQ_RValue;
275991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
27605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
27615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
27625f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 16> ArgTys;
27632865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
27641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27655f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<bool, 16> ConsumedArguments;
2766f85e193739c953358c865005855253af4f68a497John McCall        ConsumedArguments.reserve(FTI.NumArgs);
2767f85e193739c953358c865005855253af4f68a497John McCall        bool HasAnyConsumedArguments = false;
2768f85e193739c953358c865005855253af4f68a497John McCall
27695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
2770d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
27718123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
277278c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
27732dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
27742dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
277591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier          assert((ArgTy == Context.getAdjustedParameterType(ArgTy)) &&
277679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor                 "Unadjusted type?");
27772dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
27785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
27795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
278072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
27812dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
27825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
27835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
27845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
27855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
27868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(DeclType.Loc, diag::err_void_only_param);
27872ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
27888123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
27892ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
27902ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
27918cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(FTI.ArgInfo[i].IdentLoc,
27924565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
27932ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
27948123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
27952ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
27962ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
27970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
27988cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                S.Diag(DeclType.Loc, diag::err_void_param_qualified);
27991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28002ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
28012ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
28022ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
2803aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov          } else if (ArgTy->isHalfType()) {
2804aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // Disallow half FP arguments.
2805aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // FIXME: This really should be in BuildFunctionType.
280619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            if (S.getLangOpts().OpenCL) {
280719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              if (!S.getOpenCLOptions().cl_khr_fp16) {
280819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                S.Diag(Param->getLocation(),
280919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                  diag::err_opencl_half_argument) << ArgTy;
281019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                D.setInvalidType();
28119dd74c5504c743c96ea3a1d691d6a75ec3a98147John McCall                Param->setInvalidDecl();
281219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              }
281319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            } else {
281419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              S.Diag(Param->getLocation(),
281519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                diag::err_parameters_retval_cannot_have_fp16_type) << 0;
281619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              D.setInvalidType();
281719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            }
2818eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
2819eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
2820a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
2821eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              Param->setKNRPromoted(true);
2822183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
2823eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              if (BTy->getKind() == BuiltinType::Float) {
2824eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
2825eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall                Param->setKNRPromoted(true);
2826eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              }
2827eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
28285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
282956a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
28308cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          if (LangOpts.ObjCAutoRefCount) {
2831f85e193739c953358c865005855253af4f68a497John McCall            bool Consumed = Param->hasAttr<NSConsumedAttr>();
2832f85e193739c953358c865005855253af4f68a497John McCall            ConsumedArguments.push_back(Consumed);
2833f85e193739c953358c865005855253af4f68a497John McCall            HasAnyConsumedArguments |= Consumed;
2834f85e193739c953358c865005855253af4f68a497John McCall          }
2835f85e193739c953358c865005855253af4f68a497John McCall
283654e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
28375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2838465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2839f85e193739c953358c865005855253af4f68a497John McCall        if (HasAnyConsumedArguments)
2840f85e193739c953358c865005855253af4f68a497John McCall          EPI.ConsumedArguments = ConsumedArguments.data();
2841f85e193739c953358c865005855253af4f68a497John McCall
28425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 4> Exceptions;
284374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<ParsedType, 2> DynamicExceptions;
284474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<SourceRange, 2> DynamicExceptionRanges;
284574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        Expr *NoexceptExpr = 0;
284691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
28478b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        if (FTI.getExceptionSpecType() == EST_Dynamic) {
284874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // FIXME: It's rather inefficient to have to split into two vectors
284974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // here.
285074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          unsigned N = FTI.NumExceptions;
285174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptions.reserve(N);
285274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptionRanges.reserve(N);
285374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          for (unsigned I = 0; I != N; ++I) {
285474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptions.push_back(FTI.Exceptions[I].Ty);
285574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptionRanges.push_back(FTI.Exceptions[I].Range);
2856e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
28578b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
285874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          NoexceptExpr = FTI.NoexceptExpr;
285974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        }
286091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
286174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        S.checkExceptionSpecification(FTI.getExceptionSpecType(),
286274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptions,
286374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptionRanges,
286474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      NoexceptExpr,
286574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      Exceptions,
286674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      EPI);
286791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2868bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose        T = Context.getFunctionType(T, ArgTys, EPI);
28695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
287004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
28715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
28725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2873f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
2874f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
28757bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
2876f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
28777bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
2878edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
2879edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
28808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      } else if (S.isDependentScopeSpecifier(SS) ||
28818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                 dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) {
28821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
28837bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
288487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
288587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
288687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
28877bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
28884a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
288987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
289087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
289187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
289214aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor        case NestedNameSpecifier::NamespaceAlias:
289387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
28949f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
28957bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
289687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
289787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
289887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
289991ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Note: if the NNS has a prefix and ClsType is a nondependent
290091ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // TemplateSpecializationType, then the NNS prefix is NOT included
290191ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // in ClsType; hence we wrap ClsType into an ElaboratedType.
290291ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // NOTE: in particular, no wrap occurs if ClsType already is an
290391ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Elaborated, DependentName, or DependentTemplateSpecialization.
290491ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
29057bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
290687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
290787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
2908f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
29098cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Mem.Scope().getBeginLoc(),
2910949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
2911949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
2912949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
2913f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
2914f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2915f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2916949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
29178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
2918949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
2919f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
2920949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
29212865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
29228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
2923f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2924f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
2925f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
2926f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2927cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
2928cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
2929cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
2930cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
2931cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
2932c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
2933711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
2934f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclChunk, attrs);
29355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2936971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
29378cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (LangOpts.CPlusPlus && T->isFunctionType()) {
2938183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
2939778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
2940971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
294191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // C++ 8.3.5p4:
2942708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
2943708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
2944708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
2945708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
2946683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    //
2947683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // Core issue 547 also allows cv-qualifiers on function types that are
2948683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // top-level template type arguments.
2949613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
2950613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
2951906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman      FreeFunction = ((D.getContext() != Declarator::MemberContext &&
2952906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman                       D.getContext() != Declarator::LambdaExprContext) ||
2953613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall                      D.getDeclSpec().isFriendSpecified());
2954613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
29558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec());
2956613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
2957613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
2958613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
2959d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.fct]p6 (w/DR1417):
2960d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // An attempt to specify a function type with a cv-qualifier-seq or a
2961d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // ref-qualifier (including by typedef-name) is ill-formed unless it is:
2962d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type for a non-static member function,
2963d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type to which a pointer to member refers,
2964d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the top-level function type of a function typedef declaration or
2965d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //    alias-declaration,
2966d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id in the default argument of a type-parameter, or
2967d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id of a template-argument for a type-parameter
2968d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction &&
2969d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !(!FreeFunction &&
2970d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) &&
2971d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !IsTypedefName &&
2972d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        D.getContext() != Declarator::TemplateTypeArgContext) {
297396a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      SourceLocation Loc = D.getLocStart();
2974d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      SourceRange RemovalRange;
2975d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      unsigned I;
2976d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      if (D.isFunctionDeclarator(I)) {
2977d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        SmallVector<SourceLocation, 4> RemovalLocs;
2978d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        const DeclaratorChunk &Chunk = D.getTypeObject(I);
2979d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        assert(Chunk.Kind == DeclaratorChunk::Function);
2980d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.hasRefQualifier())
2981d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getRefQualifierLoc());
2982d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Const)
2983d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getConstQualifierLoc());
2984d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Volatile)
2985d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getVolatileQualifierLoc());
2986d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        // FIXME: We do not track the location of the __restrict qualifier.
2987d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //if (Chunk.Fun.TypeQuals & Qualifiers::Restrict)
2988d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //  RemovalLocs.push_back(Chunk.Fun.getRestrictQualifierLoc());
2989d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (!RemovalLocs.empty()) {
2990d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          std::sort(RemovalLocs.begin(), RemovalLocs.end(),
2991aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                    BeforeThanCompare<SourceLocation>(S.getSourceManager()));
2992d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalRange = SourceRange(RemovalLocs.front(), RemovalLocs.back());
2993d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          Loc = RemovalLocs.front();
2994683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
2995683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      }
2996d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2997d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      S.Diag(Loc, diag::err_invalid_qualified_function_type)
2998d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FreeFunction << D.isFunctionDeclarator() << T
2999d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << getFunctionQualifiersAsString(FnTy)
3000d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FixItHint::CreateRemoval(RemovalRange);
3001d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
3002d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // Strip the cv-qualifiers and ref-qualifiers from the type.
3003d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
3004d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.TypeQuals = 0;
3005d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.RefQualifier = RQ_None;
3006d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
300791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      T = Context.getFunctionType(FnTy->getResultType(),
3008bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                  ArrayRef<QualType>(FnTy->arg_type_begin(),
3009bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                                     FnTy->getNumArgs()),
3010bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                  EPI);
3011e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      // Rebuild any parens around the identifier in the function type.
3012e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
3013e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        if (D.getTypeObject(i).Kind != DeclaratorChunk::Paren)
3014e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith          break;
3015e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        T = S.BuildParenType(T);
3016e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      }
3017971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
3018971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
30191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3020711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
3021711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull())
3022711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = D.getAttributes())
3023f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclName, attrs);
3024711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3025711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
3026711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
3027711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3028148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  // C++0x [dcl.constexpr]p9:
3029148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  //  A constexpr specifier used in an object declaration declares the object
303091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //  as const.
3031148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
3032737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
3033737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
3034737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
303591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // If there was an ellipsis in the declarator, the declaration declares a
3036a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
3037a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (D.hasEllipsis() && !T.isNull()) {
3038a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
303991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   A declarator-id or abstract-declarator containing an ellipsis shall
3040a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
3041a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
3042a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
3043a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
3044a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
304591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   [...] When it is part of a parameter-declaration-clause, the
304691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   parameter pack is a function parameter pack (14.5.3). The type T
3047a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
304891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   a template parameter pack; each template parameter pack in T is
3049a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
3050a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
3051a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
3052a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
3053a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
305491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(D.getEllipsisLoc(),
3055a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
3056a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
3057a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
3058a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
305966874fb18afbffb8b2ca05576851a64534be3352David Blaikie        T = Context.getPackExpansionType(T, None);
3060a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
3061a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
306291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3063a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
3064a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
306591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   If a template-parameter is a [...] is a parameter-declaration that
3066a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
3067a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
3068a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
3069a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
3070a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
3071a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
3072a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
307366874fb18afbffb8b2ca05576851a64534be3352David Blaikie        T = Context.getPackExpansionType(T, None);
3074e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith      else
3075e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith        S.Diag(D.getEllipsisLoc(),
307680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith               LangOpts.CPlusPlus11
3077e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 ? diag::warn_cxx98_compat_variadic_templates
3078e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 : diag::ext_variadic_templates);
3079a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
308091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3081a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
3082a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
3083cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:  // FIXME: special diagnostic here?
3084cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:     // FIXME: special diagnostic here?
3085a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
30860b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
3087162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
30883e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
3089a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
3090a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
3091a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
3092a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
3093a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
309417b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
3095a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
3096f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
30977796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
3098683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    case Declarator::TemplateTypeArgContext:
3099a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
3100a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
31018cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      S.Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
3102a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
3103a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
3104a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
3105a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
3106e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
3107bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
3108bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
3109bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
3110bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
3111db7abf78dedc2ef6ccb42b3dac6ab330fe2ea469Argyrios Kyrtzidis
31128cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return S.GetTypeSourceInfoForDeclarator(D, T, TInfo);
31138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
31148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
31158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// GetTypeForDeclarator - Convert the type for the specified
31168cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// declarator to Type instances.
31178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis///
31188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// The result of this call will never be null, but the associated
31198cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// type may be a null type if there's an unrecoverable error.
31208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
31218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // Determine the type of the declarator. Not all forms of declarator
31228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // have a type.
31238cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
31248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeProcessingState state(*this, D);
31258cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
31268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
31278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
31288cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (T.isNull())
31298cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
31308cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
31314e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (D.isPrototypeContext() && getLangOpts().ObjCAutoRefCount)
31328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    inferARCWriteback(state, T);
313391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
31348cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return GetFullTypeForDeclarator(state, T, ReturnTypeInfo);
31355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
31365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
313731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclSpec(Sema &S,
313831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           QualType &declSpecTy,
313931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           Qualifiers::ObjCLifetime ownership) {
314031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy->isObjCRetainableType() &&
314131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) {
314231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers qs;
314331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    qs.addObjCLifetime(ownership);
314431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    declSpecTy = S.Context.getQualifiedType(declSpecTy, qs);
314531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
314631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
314731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
314831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
314931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
315031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            unsigned chunkIndex) {
315131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
315231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
315331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
315431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Look for an explicit lifetime attribute.
315531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclaratorChunk &chunk = D.getTypeObject(chunkIndex);
315631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (const AttributeList *attr = chunk.getAttrs(); attr;
315731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis         attr = attr->getNext())
31588e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    if (attr->getKind() == AttributeList::AT_ObjCOwnership)
315931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
316031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
316131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  const char *attrStr = 0;
316231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  switch (ownership) {
31633026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
316431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break;
316531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Strong: attrStr = "strong"; break;
316631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Weak: attrStr = "weak"; break;
316731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break;
316831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
316931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
317031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // If there wasn't one, add one (with an invalid source location
317131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // so that we don't make an AttributedType for it).
317231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  AttributeList *attr = D.getAttributePool()
317331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    .create(&S.Context.Idents.get("objc_ownership"), SourceLocation(),
317431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            /*scope*/ 0, SourceLocation(),
317531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            &S.Context.Idents.get(attrStr), SourceLocation(),
317693f95f2a2cbb6bb3d17bfb5fc74ce1cccea751b6Sean Hunt            /*args*/ 0, 0, AttributeList::AS_GNU);
317731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  spliceAttrIntoList(*attr, chunk.getAttrListRef());
317831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
317931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // TODO: mark whether we did this inference?
318031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
318131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
318248d798ce32447607144db70a484cdb99c1180663Benjamin Kramer/// \brief Used for transferring ownership in casts resulting in l-values.
318331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnership(TypeProcessingState &state,
318431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 QualType &declSpecTy,
318531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 Qualifiers::ObjCLifetime ownership) {
318631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
318731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
318831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
318931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  int inner = -1;
31906ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis  bool hasIndirection = false;
319131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
319231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    DeclaratorChunk &chunk = D.getTypeObject(i);
319331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    switch (chunk.Kind) {
319431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Paren:
319531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      // Ignore parens.
319631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
319731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
319831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Array:
319931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Reference:
320031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Pointer:
32016ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
32026ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        hasIndirection = true;
320331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      inner = i;
320431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
320531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
320631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::BlockPointer:
32076ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
32086ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        transferARCOwnershipToDeclaratorChunk(state, ownership, i);
32096ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      return;
321031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
321131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Function:
321231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::MemberPointer:
321331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
321431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    }
321531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
321631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
321731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (inner == -1)
32186ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    return;
321931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
322091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  DeclaratorChunk &chunk = D.getTypeObject(inner);
322131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (chunk.Kind == DeclaratorChunk::Pointer) {
322231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (declSpecTy->isObjCRetainableType())
322331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
32246ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    if (declSpecTy->isObjCObjectType() && hasIndirection)
322531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclaratorChunk(state, ownership, inner);
322631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  } else {
322731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    assert(chunk.Kind == DeclaratorChunk::Array ||
322831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis           chunk.Kind == DeclaratorChunk::Reference);
322931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
323031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
323131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
323231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
323331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) {
323431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeProcessingState state(*this, D);
323531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
323631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
323731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
323831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy.isNull())
323931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
324031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
32414e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount) {
324231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy);
324331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (ownership != Qualifiers::OCL_None)
324431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      transferARCOwnership(state, declSpecTy, ownership);
324531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
324631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
324731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo);
324831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
324931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
325014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall/// Map an AttributedType::Kind to an AttributeList::Kind.
325114aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
325214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  switch (kind) {
325314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_address_space:
32548e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_AddressSpace;
325514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_regparm:
32568e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Regparm;
325714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_vector_size:
32588e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_VectorSize;
325914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_vector_type:
32608e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonVectorType;
326114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_polyvector_type:
32628e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonPolyVectorType;
326314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_objc_gc:
32648e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCGC;
3265b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  case AttributedType::attr_objc_ownership:
32668e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCOwnership;
326714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_noreturn:
32688e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NoReturn;
326914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_cdecl:
32708e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_CDecl;
327114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_fastcall:
32728e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_FastCall;
327314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_stdcall:
32748e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_StdCall;
327514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_thiscall:
32768e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ThisCall;
327714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_pascal:
32788e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pascal;
3279414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case AttributedType::attr_pcs:
32808e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pcs;
3281263366f9241366f29ba65b703120f302490c39ffDerek Schuff  case AttributedType::attr_pnaclcall:
3282263366f9241366f29ba65b703120f302490c39ffDerek Schuff    return AttributeList::AT_PnaclCall;
328338980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei  case AttributedType::attr_inteloclbicc:
328438980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    return AttributeList::AT_IntelOclBicc;
328514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
328614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  llvm_unreachable("unexpected attribute kind!");
328714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
328814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
328914aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic void fillAttributedTypeLoc(AttributedTypeLoc TL,
329014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                  const AttributeList *attrs) {
329114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributedType::Kind kind = TL.getAttrKind();
329214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
329314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  assert(attrs && "no type attributes in the expected location!");
329414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributeList::Kind parsedKind = getAttrListKind(kind);
329514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  while (attrs->getKind() != parsedKind) {
329614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    attrs = attrs->getNext();
329714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    assert(attrs && "no matching attribute in expected location!");
329814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
329914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
330014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  TL.setAttrNameLoc(attrs->getLoc());
330114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrExprOperand())
330214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrExprOperand(attrs->getArg(0));
330314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  else if (TL.hasAttrEnumOperand())
330414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrEnumOperandLoc(attrs->getParameterLoc());
330514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
330614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // FIXME: preserve this information to here.
330714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrOperand())
330814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrOperandParensRange(SourceRange());
330914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
331014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
331151bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
331251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
3313c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    ASTContext &Context;
331451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
3315f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
331651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
331791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
3318c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      : Context(Context), DS(DS) {}
3319f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
332014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
332114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, DS.getAttributes().getList());
332214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      Visit(TL.getModifiedLoc());
332314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
332451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
332551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
332651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
332751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
332851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
332951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
333051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
333151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
33321de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // FIXME. We should have DS.getTypeSpecTypeEndLoc(). But, it requires
33331de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // addition field. What we have is good enough for dispay of location
33341de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // of 'fixit' on interface name.
33351de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      TL.setNameEndLoc(DS.getLocEnd());
3336c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
3337c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3338c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
3339c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
3340c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
3341c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.getBaseLoc().initialize(Context, SourceLocation());
3342c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
3343c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
3344c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
3345c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
334654e14c4db764c0636160d26c5bbf491637c83a76John McCall
3347c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
334854e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
334954e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
335054e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
335154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
335254e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
335354e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
335454e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
335554e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
335654e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
335754e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
335854e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
335954e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
336051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
336154e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
336254e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
3363c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
336451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
3365833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
3366a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
3367b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3368833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3369833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
3370833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
3371a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
33720daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara        TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
3373833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
3374833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
3375833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3376e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
3377e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
337839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        ElaboratedTypeLoc ElabTL = OldTL.castAs<ElaboratedTypeLoc>();
337939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        TemplateSpecializationTypeLoc NamedTL = ElabTL.getNamedTypeLoc()
338039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            .castAs<TemplateSpecializationTypeLoc>();
3381e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
3382e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
3383e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      else
338439e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        TL.copy(OldTL.castAs<TemplateSpecializationTypeLoc>());
3385833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
3386cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3387cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
3388cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3389cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3390cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3391cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3392cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
3393cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3394cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3395b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
3396cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
3397b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3398cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
3399cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3400ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3401ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      // FIXME: This holds only because we only have one unary transform.
3402ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType);
3403ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setKWLoc(DS.getTypeSpecTypeLoc());
3404ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setParensRange(DS.getTypeofParensRange());
3405ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getRepAsType());
3406ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TypeSourceInfo *TInfo = 0;
3407ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3408ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setUnderlyingTInfo(TInfo);
3409ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
3410ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3411ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
3412ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
3413ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
3414ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
3415ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
3416ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
3417ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
3418ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
3419ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
3420ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
3421ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
3422ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
3423ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
3424ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
3425e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3426e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
3427e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
3428253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
3429e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
3430b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3431e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
343239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          TL.copy(TInfo->getTypeLoc().castAs<ElaboratedTypeLoc>());
3433e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
3434e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
3435e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
343638a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara      TL.setElaboratedKeywordLoc(Keyword != ETK_None
343738a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 ? DS.getTypeSpecTypeLoc()
343838a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 : SourceLocation());
3439e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
34409e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
3441e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
3442e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3443e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
344466581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
344566581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
344666581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
344766581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
344839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      TL.copy(TInfo->getTypeLoc().castAs<DependentNameTypeLoc>());
344933500955d731c73717af52088b7fc0e7a85681e7John McCall    }
345033500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
345133500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
345266581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
345366581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
345466581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
345566581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
345639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      TL.copy(
345739e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          TInfo->getTypeLoc().castAs<DependentTemplateSpecializationTypeLoc>());
34580daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    }
34590daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    void VisitTagTypeLoc(TagTypeLoc TL) {
34600daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
3461e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3462b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
34634cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // An AtomicTypeLoc can come from either an _Atomic(...) type specifier
34644cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // or an _Atomic qualifier.
34654cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      if (DS.getTypeSpecType() == DeclSpec::TST_atomic) {
34664cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setKWLoc(DS.getTypeSpecTypeLoc());
34674cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setParensRange(DS.getTypeofParensRange());
346891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
34694cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TypeSourceInfo *TInfo = 0;
34704cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
34714cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        assert(TInfo);
34724cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
34734cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      } else {
34744cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setKWLoc(DS.getAtomicSpecLoc());
34754cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // No parens, to indicate this was spelled as an _Atomic qualifier.
34764cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setParensRange(SourceRange());
34774cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        Visit(TL.getValueLoc());
34784cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      }
3479b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
3480e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
348151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
348251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
3483c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initialize(Context, DS.getTypeSpecTypeLoc());
348451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
348551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
3486eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
348751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
3488b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    ASTContext &Context;
348951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
3490f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
349151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
3492b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
3493b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      : Context(Context), Chunk(Chunk) {}
34944adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
349551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
34969f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
349751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
34984adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3499f85e193739c953358c865005855253af4f68a497John McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3500f85e193739c953358c865005855253af4f68a497John McCall      fillAttributedTypeLoc(TL, Chunk.getAttrs());
3501f85e193739c953358c865005855253af4f68a497John McCall    }
350251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
350351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
350451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
35054adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
350651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
350751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
350851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
35094adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
351051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
351151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
351251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
35134adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
351451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
351551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
3516b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const CXXScopeSpec& SS = Chunk.Mem.Scope();
3517b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
3518b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3519b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const Type* ClsTy = TL.getClass();
3520b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      QualType ClsQT = QualType(ClsTy, 0);
3521b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
3522b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Now copy source location info into the type loc component.
3523b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeLoc ClsTL = ClsTInfo->getTypeLoc();
3524b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
3525b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Identifier:
3526b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
3527b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        {
352839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          DependentNameTypeLoc DNTLoc = ClsTL.castAs<DependentNameTypeLoc>();
352938a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          DNTLoc.setElaboratedKeywordLoc(SourceLocation());
3530b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
3531b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
3532b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3533b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3534b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3535b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpec:
3536b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpecWithTemplate:
3537b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        if (isa<ElaboratedType>(ClsTy)) {
353839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          ElaboratedTypeLoc ETLoc = ClsTL.castAs<ElaboratedTypeLoc>();
353938a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          ETLoc.setElaboratedKeywordLoc(SourceLocation());
3540b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ETLoc.setQualifierLoc(NNSLoc.getPrefix());
3541b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
3542b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
3543b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        } else {
3544b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
3545b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3546b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3547b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3548b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Namespace:
3549b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::NamespaceAlias:
3550b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Global:
3551b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        llvm_unreachable("Nested-name-specifier must name a type");
3552b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      }
3553b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3554b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Finally fill in MemberPointerLocInfo fields.
355551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
3556b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TL.setClassTInfo(ClsTInfo);
35574adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
355851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
355951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
356054e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
356154e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
356251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
356351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
356451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
356551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
356651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
356751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
356851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
356951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
357051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
357151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
357251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
357351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
357451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
357551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
357651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
3577796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeBegin(Chunk.Loc);
3578796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeEnd(Chunk.EndLoc);
357951bd803fbdade51d674598ed45da3d54190a656cJohn McCall
358051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
358159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setLParenLoc(FTI.getLParenLoc());
358259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setRParenLoc(FTI.getRParenLoc());
358354e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
3584d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
358554e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
35864adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
358751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
35884adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
3589075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
3590075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
3591075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
3592075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
3593075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
35941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
359551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
35969f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
35974adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
359851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
359951bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
36004adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
36014cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smithstatic void fillAtomicQualLoc(AtomicTypeLoc ATL, const DeclaratorChunk &Chunk) {
36024cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  SourceLocation Loc;
36034cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  switch (Chunk.Kind) {
36044cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Function:
36054cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Array:
36064cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Paren:
36074cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    llvm_unreachable("cannot be _Atomic qualified");
36084cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
36094cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Pointer:
36104cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    Loc = SourceLocation::getFromRawEncoding(Chunk.Ptr.AtomicQualLoc);
36114cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    break;
36124cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
36134cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::BlockPointer:
36144cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Reference:
36154cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::MemberPointer:
36164cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // FIXME: Provide a source location for the _Atomic keyword.
36174cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    break;
36184cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
36194cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
36204cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  ATL.setKWLoc(Loc);
36214cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  ATL.setParensRange(SourceRange());
36224cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith}
36234cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
3624a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
362551bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
362651bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
362705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
362805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
362905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
363005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
363105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
3632a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
363305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
363405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
3635a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
3636a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
363751bd803fbdade51d674598ed45da3d54190a656cJohn McCall
3638a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
3639a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
364039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    CurrTL.castAs<PackExpansionTypeLoc>().setEllipsisLoc(D.getEllipsisLoc());
364191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
3642a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
364391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
36448ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
36454cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // An AtomicTypeLoc might be produced by an atomic qualifier in this
36464cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // declarator chunk.
36474cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (AtomicTypeLoc ATL = CurrTL.getAs<AtomicTypeLoc>()) {
36484cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      fillAtomicQualLoc(ATL, D.getTypeObject(i));
36494cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      CurrTL = ATL.getValueLoc().getUnqualifiedLoc();
36504cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    }
36514cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
365239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    while (AttributedTypeLoc TL = CurrTL.getAs<AttributedTypeLoc>()) {
365314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs());
365414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
365514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
365614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3657b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
365851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
36594adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
366091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3661b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
3662b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
3663b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
366405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
366505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
366605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
3667b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
3668c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
366905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
367091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3671a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
36724adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
36734adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3674a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
3675b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
36761bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
36771bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
36781bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
367991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
3680eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
3681a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
36821bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
36831bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
3684b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
36851bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
36861bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
36871bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
36881bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
3689b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*"
369035d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
369135d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
36921bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
36931bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
3694f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
36955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
36965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
36975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
36981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3699d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
3700bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
37015153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
3702809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
37035912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
3704e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall  // Make sure there are no unused decl attributes on the declarator.
3705cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // We don't want to do this for ObjC parameters because we're going
3706cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // to apply them to the actual parameter declaration.
37076b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  // Likewise, we don't want to do this for alias declarations, because
37086b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  // we are actually going to build a declaration from this eventually.
37096b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  if (D.getContext() != Declarator::ObjCParameterContext &&
37106b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith      D.getContext() != Declarator::AliasDeclContext &&
37116b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith      D.getContext() != Declarator::AliasTemplateContext)
3712cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    checkUnusedDeclAttributes(D);
3713e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
37144e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
3715402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
37166d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
3717402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
3718402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
3719b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
37205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
37215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3722e97179c675b341927807c718be215c8d1aab8acbDouglas GregorParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) {
3723e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  QualType T = Context.getObjCInstanceType();
3724e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc);
3725e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return CreateParsedType(T, TInfo);
3726e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
3727e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3728e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3729c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3730c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
3731c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3732232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3733232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
3734c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
3735c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
37361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
3737c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
37380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
3739232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
374029e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified by
374129e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // qualifiers for two or more different address spaces."
3742232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
3743c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
3744e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3745c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3746232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
37471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3748020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be
3749020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // qualified by an address-space qualifier."
3750020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  if (Type->isFunctionType()) {
3751020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type);
3752020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    Attr.setInvalid();
3753020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    return;
3754020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  }
3755020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne
3756232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
3757545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
3758f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3759e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3760c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3761232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3762545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
3763232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
3764ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
3765ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
3766dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
3767dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
3768e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3769c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3770232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3771232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3772efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
3773efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
3774efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
3775efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
3776efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
3777e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
3778efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
3779efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
3780efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
3781efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3782efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
37830953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
3784efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
3785efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
37860953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
3787e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3788efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
3789efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3790efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
37911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
3792f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
3793c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
3794c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
3795d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// Does this type have a "direct" ownership qualifier?  That is,
3796d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// is it written like "__strong id", as opposed to something like
3797d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// "typeof(foo)", where that happens to be strong?
3798d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCallstatic bool hasDirectOwnershipQualifier(QualType type) {
3799d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Fast path: no qualifier at all.
3800d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  assert(type.getQualifiers().hasObjCLifetime());
3801d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3802d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  while (true) {
3803d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // __strong id
3804d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (const AttributedType *attr = dyn_cast<AttributedType>(type)) {
3805d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      if (attr->getAttrKind() == AttributedType::attr_objc_ownership)
3806d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        return true;
3807d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3808d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = attr->getModifiedType();
3809d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3810d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // X *__strong (...)
3811d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else if (const ParenType *paren = dyn_cast<ParenType>(type)) {
3812d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = paren->getInnerType();
381391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3814d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // That's it for things we want to complain about.  In particular,
3815d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // we do not want to look through typedefs, typeof(expr),
3816d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // typeof(type), or any other way that the type is somehow
3817d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // abstracted.
3818d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else {
381991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3820d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return false;
3821d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3822d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
3823d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall}
3824d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3825b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis/// handleObjCOwnershipTypeAttr - Process an objc_ownership
3826f85e193739c953358c865005855253af4f68a497John McCall/// attribute on the specified type.
3827f85e193739c953358c865005855253af4f68a497John McCall///
3828f85e193739c953358c865005855253af4f68a497John McCall/// Returns 'true' if the attribute was handled.
3829b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
3830f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr,
3831f85e193739c953358c865005855253af4f68a497John McCall                                       QualType &type) {
3832e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  bool NonObjCPointer = false;
3833e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3834dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith  if (!type->isDependentType() && !type->isUndeducedType()) {
3835e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    if (const PointerType *ptr = type->getAs<PointerType>()) {
3836e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      QualType pointee = ptr->getPointeeType();
3837e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      if (pointee->isObjCRetainableType() || pointee->isPointerType())
3838e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis        return false;
3839e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // It is important not to lose the source info that there was an attribute
3840e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // applied to non-objc pointer. We will create an attributed type but
3841e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // its type will be the same as the original type.
3842e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      NonObjCPointer = true;
3843e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    } else if (!type->isObjCRetainableType()) {
3844e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      return false;
3845e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3846b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
3847b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // Don't accept an ownership attribute in the declspec if it would
3848b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // just be the return type of a block pointer.
3849b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    if (state.isProcessingDeclSpec()) {
3850b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      Declarator &D = state.getDeclarator();
3851b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (maybeMovePastReturnType(D, D.getNumTypeObjects()))
3852b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        return false;
3853b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
3854e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3855f85e193739c953358c865005855253af4f68a497John McCall
3856f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
3857440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  SourceLocation AttrLoc = attr.getLoc();
3858440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isMacroID())
3859440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    AttrLoc = S.getSourceManager().getImmediateExpansionRange(AttrLoc).first;
3860f85e193739c953358c865005855253af4f68a497John McCall
3861f85e193739c953358c865005855253af4f68a497John McCall  if (!attr.getParameterName()) {
3862440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::err_attribute_argument_n_not_string)
3863b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      << "objc_ownership" << 1;
3864f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3865f85e193739c953358c865005855253af4f68a497John McCall    return true;
3866f85e193739c953358c865005855253af4f68a497John McCall  }
3867f85e193739c953358c865005855253af4f68a497John McCall
3868d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Consume lifetime attributes without further comment outside of
3869d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // ARC mode.
38704e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!S.getLangOpts().ObjCAutoRefCount)
3871d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    return true;
3872d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3873f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime lifetime;
3874f85e193739c953358c865005855253af4f68a497John McCall  if (attr.getParameterName()->isStr("none"))
3875f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_ExplicitNone;
3876f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("strong"))
3877f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Strong;
3878f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("weak"))
3879f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Weak;
3880f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("autoreleasing"))
3881f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Autoreleasing;
3882f85e193739c953358c865005855253af4f68a497John McCall  else {
3883440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_attribute_type_not_supported)
3884b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      << "objc_ownership" << attr.getParameterName();
3885f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3886f85e193739c953358c865005855253af4f68a497John McCall    return true;
3887f85e193739c953358c865005855253af4f68a497John McCall  }
3888f85e193739c953358c865005855253af4f68a497John McCall
3889d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  SplitQualType underlyingType = type.split();
3890d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3891d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Check for redundant/conflicting ownership qualifiers.
3892d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  if (Qualifiers::ObjCLifetime previousLifetime
3893d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        = type.getQualifiers().getObjCLifetime()) {
3894d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // If it's written directly, that's an error.
3895d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (hasDirectOwnershipQualifier(type)) {
3896d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant)
3897d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        << type;
3898d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return true;
3899d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3900d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3901d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // Otherwise, if the qualifiers actually conflict, pull sugar off
3902d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // until we reach a type that is directly qualified.
3903d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (previousLifetime != lifetime) {
3904d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // This should always terminate: the canonical type is
3905d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // qualified, so some bit of sugar must be hiding it.
3906d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      while (!underlyingType.Quals.hasObjCLifetime()) {
3907d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        underlyingType = underlyingType.getSingleStepDesugaredType();
3908d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      }
3909d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      underlyingType.Quals.removeObjCLifetime();
3910d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3911d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
3912d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3913d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  underlyingType.Quals.addObjCLifetime(lifetime);
3914f85e193739c953358c865005855253af4f68a497John McCall
3915e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (NonObjCPointer) {
3916e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    StringRef name = attr.getName()->getName();
3917e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    switch (lifetime) {
3918e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_None:
3919e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_ExplicitNone:
3920e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      break;
3921e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Strong: name = "__strong"; break;
3922e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Weak: name = "__weak"; break;
3923e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break;
3924e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3925e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_objc_object_attribute_wrong_type)
3926e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      << name << type;
3927e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3928e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3929f85e193739c953358c865005855253af4f68a497John McCall  QualType origType = type;
3930e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!NonObjCPointer)
3931d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    type = S.Context.getQualifiedType(underlyingType);
3932f85e193739c953358c865005855253af4f68a497John McCall
3933f85e193739c953358c865005855253af4f68a497John McCall  // If we have a valid source location for the attribute, use an
3934f85e193739c953358c865005855253af4f68a497John McCall  // AttributedType instead.
3935440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isValid())
3936b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis    type = S.Context.getAttributedType(AttributedType::attr_objc_ownership,
3937f85e193739c953358c865005855253af4f68a497John McCall                                       origType, type);
3938f85e193739c953358c865005855253af4f68a497John McCall
39399f084a3166b684573ba49df28fc5792bc37d92e1John McCall  // Forbid __weak if the runtime doesn't support it.
3940f85e193739c953358c865005855253af4f68a497John McCall  if (lifetime == Qualifiers::OCL_Weak &&
39410a7dd788dbef975f35f273c7ab913f480f7edd60John McCall      !S.getLangOpts().ObjCARCWeak && !NonObjCPointer) {
3942f85e193739c953358c865005855253af4f68a497John McCall
3943f85e193739c953358c865005855253af4f68a497John McCall    // Actually, delay this until we know what we're parsing.
3944f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
3945f85e193739c953358c865005855253af4f68a497John McCall      S.DelayedDiagnostics.add(
3946440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis          sema::DelayedDiagnostic::makeForbiddenType(
3947440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis              S.getSourceManager().getExpansionLoc(AttrLoc),
3948f85e193739c953358c865005855253af4f68a497John McCall              diag::err_arc_weak_no_runtime, type, /*ignored*/ 0));
3949f85e193739c953358c865005855253af4f68a497John McCall    } else {
3950440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis      S.Diag(AttrLoc, diag::err_arc_weak_no_runtime);
3951f85e193739c953358c865005855253af4f68a497John McCall    }
3952f85e193739c953358c865005855253af4f68a497John McCall
3953f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3954f85e193739c953358c865005855253af4f68a497John McCall    return true;
3955f85e193739c953358c865005855253af4f68a497John McCall  }
395691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
395791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // Forbid __weak for class objects marked as
3958742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  // objc_arc_weak_reference_unavailable
3959742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  if (lifetime == Qualifiers::OCL_Weak) {
3960b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    if (const ObjCObjectPointerType *ObjT =
3961b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          type->getAs<ObjCObjectPointerType>()) {
39624e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith      if (ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl()) {
39634e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        if (Class->isArcWeakrefUnavailable()) {
39644e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class);
39654e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(ObjT->getInterfaceDecl()->getLocation(),
39664e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith                   diag::note_class_declared);
39674e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        }
3968742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      }
3969742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    }
3970742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  }
397191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3972f85e193739c953358c865005855253af4f68a497John McCall  return true;
3973f85e193739c953358c865005855253af4f68a497John McCall}
3974f85e193739c953358c865005855253af4f68a497John McCall
3975711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
3976711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
3977711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
3978711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
3979711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
3980711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
3981711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
3982711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
3983711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3984711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
3985711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
3986711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
3987711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
3988711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
3989711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3990711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
3991711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
3992711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3993711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3994d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
39951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3996d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
3997711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!attr.getParameterName()) {
3998711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3999ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
4000711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4001711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4002ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
40030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
4004711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getNumArgs() != 0) {
4005711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4006711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4007711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4008d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
4009711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getParameterName()->isStr("weak"))
40100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
4011711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  else if (attr.getParameterName()->isStr("strong"))
40120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
4013d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
4014711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
4015711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      << "objc_gc" << attr.getParameterName();
4016711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4017711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4018d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
40191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
402014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  QualType origType = type;
402114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  type = S.Context.getObjCGCQualType(origType, GCAttr);
402214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
402314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // Make an attributed type to preserve the source information.
402414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (attr.getLoc().isValid())
402514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
402614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                       origType, type);
402714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
4028711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
4029d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
4030d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
4031e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallnamespace {
4032e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// A helper class to unwrap a type down to a function for the
4033e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// purposes of applying attributes there.
4034e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///
4035e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// Use:
4036e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
4037e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   if (unwrapped.isFunctionType()) {
4038e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     const FunctionType *fn = unwrapped.get();
4039e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     // change fn somehow
4040e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     T = unwrapped.wrap(fn);
4041e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   }
4042e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  struct FunctionTypeUnwrapper {
4043e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    enum WrapKind {
4044e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Desugar,
4045e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Parens,
4046e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Pointer,
4047e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      BlockPointer,
4048e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Reference,
4049e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      MemberPointer
4050e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    };
4051e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4052e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType Original;
4053e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *Fn;
40545f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<unsigned char /*WrapKind*/, 8> Stack;
4055e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4056e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
4057e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      while (true) {
4058e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const Type *Ty = T.getTypePtr();
4059e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<FunctionType>(Ty)) {
4060e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Fn = cast<FunctionType>(Ty);
4061e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return;
4062e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ParenType>(Ty)) {
4063e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ParenType>(Ty)->getInnerType();
4064e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Parens);
4065e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<PointerType>(Ty)) {
4066e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<PointerType>(Ty)->getPointeeType();
4067e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Pointer);
4068e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<BlockPointerType>(Ty)) {
4069e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<BlockPointerType>(Ty)->getPointeeType();
4070e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(BlockPointer);
4071e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<MemberPointerType>(Ty)) {
4072e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<MemberPointerType>(Ty)->getPointeeType();
4073e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(MemberPointer);
4074e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ReferenceType>(Ty)) {
4075e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ReferenceType>(Ty)->getPointeeType();
4076e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Reference);
4077e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else {
4078e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          const Type *DTy = Ty->getUnqualifiedDesugaredType();
4079e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          if (Ty == DTy) {
4080e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            Fn = 0;
4081e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            return;
4082e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          }
4083e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4084e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = QualType(DTy, 0);
4085e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Desugar);
4086e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        }
4087e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4088e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4089e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4090e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    bool isFunctionType() const { return (Fn != 0); }
4091e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *get() const { return Fn; }
4092e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4093e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(Sema &S, const FunctionType *New) {
4094e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // If T wasn't modified from the unwrapped type, do nothing.
4095e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (New == get()) return Original;
4096e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4097e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Fn = New;
4098e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return wrap(S.Context, Original, 0);
4099e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4100e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4101e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  private:
4102e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
4103e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size())
4104e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getQualifiedType(Fn, Old.getQualifiers());
4105e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4106e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // Build up the inner type, applying the qualifiers from the old
4107e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // type to the new type.
4108e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      SplitQualType SplitOld = Old.split();
4109e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4110e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // As a special case, tail-recurse if there are no qualifiers.
4111200fa53fd420aa8369586f569dbece04930ad6a3John McCall      if (SplitOld.Quals.empty())
4112200fa53fd420aa8369586f569dbece04930ad6a3John McCall        return wrap(C, SplitOld.Ty, I);
4113200fa53fd420aa8369586f569dbece04930ad6a3John McCall      return C.getQualifiedType(wrap(C, SplitOld.Ty, I), SplitOld.Quals);
4114e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4115e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4116e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
4117e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size()) return QualType(Fn, 0);
4118e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4119e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      switch (static_cast<WrapKind>(Stack[I++])) {
4120e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Desugar:
4121e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // This is the point at which we potentially lose source
4122e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // information.
4123e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
4124e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4125e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Parens: {
4126e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
4127e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getParenType(New);
4128e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4129e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4130e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Pointer: {
4131e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
4132e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getPointerType(New);
4133e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4134e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4135e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case BlockPointer: {
4136e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
4137e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getBlockPointerType(New);
4138e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4139e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4140e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case MemberPointer: {
4141e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
4142e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldMPT->getPointeeType(), I);
4143e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getMemberPointerType(New, OldMPT->getClass());
4144e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4145e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4146e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Reference: {
4147e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const ReferenceType *OldRef = cast<ReferenceType>(Old);
4148e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldRef->getPointeeType(), I);
4149e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<LValueReferenceType>(OldRef))
4150e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
4151e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        else
4152e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getRValueReferenceType(New);
4153e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4154e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4155e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4156e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      llvm_unreachable("unknown wrapping kind");
4157e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4158e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  };
4159e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
4160e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4161711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
4162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
4163711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
4164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
4165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
4166711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
4167e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4168711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
41692455636163fdd18581d7fdae816433f886d88213Mike Stump
41708e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NoReturn) {
4171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
417204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
4173e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4174e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
4175711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
4176711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
4177425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
4178425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
4179711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
4180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4181711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4182711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
4183425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
4184f85e193739c953358c865005855253af4f68a497John McCall  // ns_returns_retained is not always a type attribute, but if we got
4185f85e193739c953358c865005855253af4f68a497John McCall  // here, we're treating it as one right now.
41868e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NSReturnsRetained) {
41874e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(S.getLangOpts().ObjCAutoRefCount &&
4188f85e193739c953358c865005855253af4f68a497John McCall           "ns_returns_retained treated as type attribute in non-ARC");
4189f85e193739c953358c865005855253af4f68a497John McCall    if (attr.getNumArgs()) return true;
4190f85e193739c953358c865005855253af4f68a497John McCall
4191f85e193739c953358c865005855253af4f68a497John McCall    // Delay if this is not a function type.
4192f85e193739c953358c865005855253af4f68a497John McCall    if (!unwrapped.isFunctionType())
4193f85e193739c953358c865005855253af4f68a497John McCall      return false;
4194f85e193739c953358c865005855253af4f68a497John McCall
4195f85e193739c953358c865005855253af4f68a497John McCall    FunctionType::ExtInfo EI
4196f85e193739c953358c865005855253af4f68a497John McCall      = unwrapped.get()->getExtInfo().withProducesResult(true);
4197f85e193739c953358c865005855253af4f68a497John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4198f85e193739c953358c865005855253af4f68a497John McCall    return true;
4199f85e193739c953358c865005855253af4f68a497John McCall  }
4200f85e193739c953358c865005855253af4f68a497John McCall
42018e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_Regparm) {
4202711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
4203711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
4204711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
42051e030eb1194763b42c1752723be23b1515f48981John McCall
4206711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
4207711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
4208008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
42091e030eb1194763b42c1752723be23b1515f48981John McCall
4210ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Diagnose regparm with fastcall.
4211ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    const FunctionType *fn = unwrapped.get();
4212ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    CallingConv CC = fn->getCallConv();
4213ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (CC == CC_X86FastCall) {
4214ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
4215ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC)
4216ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm";
4217ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
4218ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
4219ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
4220ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
422191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    FunctionType::ExtInfo EI =
4222711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
4223711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4224711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4225425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
4226425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
422782bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  // Delay if the type didn't work out to a function.
422882bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  if (!unwrapped.isFunctionType()) return false;
422982bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman
423004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
4231711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
4232711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
4233711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4234f82b4e85b1219295cad4b5851b035575bc293010John McCall
4235711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
4236711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
4237064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis  if (S.Context.getCanonicalCallConv(CC) ==
4238e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      S.Context.getCanonicalCallConv(CCOld)) {
4239ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
4240ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4241711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4242e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  }
424304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
42448e68f1c8a2919ea83c2053731d6011074f1062e1Roman Divacky  if (CCOld != (S.LangOpts.MRTD ? CC_X86StdCall : CC_Default)) {
424504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Should we diagnose reapplications of the same convention?
4246711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
424704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CC)
424804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CCOld);
4249711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4250711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
425104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
425204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
425304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
425404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
4255711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isa<FunctionNoProtoType>(fn)) {
4256711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_knr)
425704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
4258711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
4259711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
426004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
426104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
4262711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
426304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
4264711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
426504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
4266711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
4267711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
426804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
4269ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
4270ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Also diagnose fastcall with regparm.
4271a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    if (fn->getHasRegParm()) {
4272ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
4273ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm"
4274ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC);
4275ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
4276ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
4277ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
427804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
4279f82b4e85b1219295cad4b5851b035575bc293010John McCall
4280711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
4281711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4282711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
4283f82b4e85b1219295cad4b5851b035575bc293010John McCall}
4284f82b4e85b1219295cad4b5851b035575bc293010John McCall
4285207f4d8543529221932af82836016a2ef066c917Peter Collingbourne/// Handle OpenCL image access qualifiers: read_only, write_only, read_write
4286207f4d8543529221932af82836016a2ef066c917Peter Collingbournestatic void HandleOpenCLImageAccessAttribute(QualType& CurType,
4287207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const AttributeList &Attr,
4288207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             Sema &S) {
4289207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  // Check the attribute arguments.
4290207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (Attr.getNumArgs() != 1) {
4291207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4292207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4293207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4294207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4295207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
4296207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  llvm::APSInt arg(32);
4297207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4298207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      !sizeExpr->isIntegerConstantExpr(arg, S.Context)) {
4299207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4300207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << "opencl_image_access" << sizeExpr->getSourceRange();
4301207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4302207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4303207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4304207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  unsigned iarg = static_cast<unsigned>(arg.getZExtValue());
4305207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  switch (iarg) {
4306207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_only:
4307207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_write_only:
4308207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_write:
4309207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4310207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4311207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  default:
4312207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4313207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
4314207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << sizeExpr->getSourceRange();
4315207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4316207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4317207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4318207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
4319207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
43206e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
43216e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
43226e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
43236e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
43246e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
43256e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
43266e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
4327788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
4328788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
432956affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
43306e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
43316e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4332e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
43336e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
43346e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
43356e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
43366e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
4337ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4338ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
43396e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
43406e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << "vector_size" << sizeExpr->getSourceRange();
4341e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
43426e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
43436e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
43446e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
4345f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
43466e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
4347e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
43486e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
43496e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
43506e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
43516e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
43526e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
43536e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
43546e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
43556e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
43566e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
43576e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4358e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
43596e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
43606e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
43616e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
43626e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
43636e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4364e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
43656e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
43666e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
43676e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
43686e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
43696e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
4370788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
4371e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
43726e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
43736e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
43744ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// \brief Process the OpenCL-like ext_vector_type attribute when it occurs on
43754ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// a type.
437691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierstatic void HandleExtVectorTypeAttr(QualType &CurType,
437791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                                    const AttributeList &Attr,
43784ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                    Sema &S) {
43794ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  Expr *sizeExpr;
438091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
43814ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Special case where the argument is a template id.
43824ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (Attr.getParameterName()) {
43834ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CXXScopeSpec SS;
4384e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    SourceLocation TemplateKWLoc;
43854ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    UnqualifiedId id;
43864ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
4387e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
4388e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    ExprResult Size = S.ActOnIdExpression(S.getCurScope(), SS, TemplateKWLoc,
4389e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          id, false, false);
43904ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Size.isInvalid())
43914ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
439291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
43934ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Size.get();
43944ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  } else {
43954ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    // check the attribute arguments.
43964ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Attr.getNumArgs() != 1) {
43974ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
43984ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
43994ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    }
44004ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Attr.getArg(0);
44014ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  }
440291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
44034ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Create the vector type.
44044ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  QualType T = S.BuildExtVectorType(CurType, sizeExpr, Attr.getLoc());
44054ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (!T.isNull())
44064ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CurType = T;
44074ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor}
44084ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
44094211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
44104211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
44114211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
44124211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
44134211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
44144211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
44154211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
44164211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
44174211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
44184211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     VectorType::VectorKind VecKind,
44194211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const char *AttrName) {
44204211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
44214211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
44224211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
44234211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
44244211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
44254211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
44264211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
44274211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
44284211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
44294211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
44304211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
44314211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
44324211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      << AttrName << numEltsExpr->getSourceRange();
44334211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
44344211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
44354211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
44364211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
44374211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  const BuiltinType* BTy = CurType->getAs<BuiltinType>();
44384211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (!BTy ||
44394211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (VecKind == VectorType::NeonPolyVector &&
44404211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::SChar &&
44414211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short) ||
44424211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (BTy->getKind() != BuiltinType::SChar &&
44434211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UChar &&
44444211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short &&
44454211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UShort &&
44464211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Int &&
44474211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UInt &&
44484211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::LongLong &&
44494211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::ULongLong &&
44504211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Float)) {
44514211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
44524211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
44534211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
44544211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
44554211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
44564211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
44574211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
44584211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
44594211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
44604211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
44614211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
44624211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
44634211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
44644211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
44654211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
44664211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
44674211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
4468711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
4469f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             TypeAttrLocation TAL, AttributeList *attrs) {
4470c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
4471c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
4472c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
4473c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
4474711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4475711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
4476711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
4477711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
4478711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr.getNext();
4479711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4480e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
4481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
4482e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
4483e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
4484cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    if (attr.isCXX11Attribute()) {
4485cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // [[gnu::...]] attributes are treated as declaration attributes, so may
4486cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // not appertain to a DeclaratorChunk, even if we handle them as type
4487cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // attributes.
4488cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.getScopeName() && attr.getScopeName()->isStr("gnu")) {
4489cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        if (TAL == TAL_DeclChunk) {
4490cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          state.getSema().Diag(attr.getLoc(),
4491cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                               diag::warn_cxx11_gnu_attribute_on_type)
4492cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith              << attr.getName();
4493cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          continue;
4494cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        }
4495cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      } else if (TAL != TAL_DeclChunk) {
4496cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // Otherwise, only consider type processing for a C++11 attribute if
4497cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // it's actually been applied to a type.
4498cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        continue;
4499cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      }
4500f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    }
4501f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
4502b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
4503b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
4504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
4505cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    default:
4506cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // A C++11 attribute on a declarator chunk must appertain to a type.
4507d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk) {
4508cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(), diag::err_attribute_not_type_attr)
4509d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith          << attr.getName();
4510d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith        attr.setUsedAsTypeAttr();
4511d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      }
4512cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
4513cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
4514cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::UnknownAttribute:
4515cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk)
4516cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(),
4517cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                             diag::warn_unknown_attribute_ignored)
4518cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          << attr.getName();
4519cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
4520cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
4521cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::IgnoredAttribute:
4522cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
452304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
45248e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_MayAlias:
4525682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // FIXME: This attribute needs to actually be handled, but if we ignore
4526682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // it it breaks large amounts of Linux software.
4527682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      attr.setUsedAsTypeAttr();
4528682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      break;
45298e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_AddressSpace:
4530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
4531e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4532c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
4533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
4534711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
4535711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
4536e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4537d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
45388e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_VectorSize:
4539711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
4540e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4541f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
45428e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ExtVectorType:
4543a4fa9008988985f9cf01712a99ddd923aea278a0Richard Smith      HandleExtVectorTypeAttr(type, attr, state.getSema());
4544e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
45454ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      break;
45468e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonVectorType:
4547711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
4548711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonVector, "neon_vector_type");
4549e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
45504211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
45518e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonPolyVectorType:
4552711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
4553711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonPolyVector,
45544211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                               "neon_polyvector_type");
4555e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
45564211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
45578e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_OpenCLImageAccess:
4558207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      HandleOpenCLImageAccessAttribute(type, attr, state.getSema());
4559e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4560207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      break;
4561207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
4562d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith    case AttributeList::AT_Win64:
4563d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith    case AttributeList::AT_Ptr32:
4564d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith    case AttributeList::AT_Ptr64:
4565d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      // FIXME: Don't ignore these. We have partial handling for them as
4566d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      // declaration attributes in SemaDeclAttr.cpp; that should be moved here.
4567d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      attr.setUsedAsTypeAttr();
4568d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      break;
4569d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith
45708e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
45714e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
4572cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        break;
4573f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough into the function attrs
4574f85e193739c953358c865005855253af4f68a497John McCall
4575711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
4576e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4577e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
4578711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
4579711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
4580f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      if (TAL == TAL_DeclSpec)
4581711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
4582711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4583711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
4584711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
4585711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
45866e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
4587c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
4588711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attrs = next));
4589232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
4590232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
4591e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \brief Ensure that the type of the given expression is complete.
4592e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4593e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// This routine checks whether the expression \p E has a complete type. If the
4594e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// expression refers to an instantiable construct, that instantiation is
4595e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// performed as needed to complete its type. Furthermore
4596e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// Sema::RequireCompleteType is called for the expression's type (or in the
4597e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// case of a reference type, the referred-to type).
4598e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4599e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \param E The expression whose type is required to be complete.
4600d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// \param Diagnoser The object that will emit a diagnostic if the type is
4601d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// incomplete.
4602e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4603e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \returns \c true if the type of \p E is incomplete and diagnosed, \c false
4604e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// otherwise.
4605f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, TypeDiagnoser &Diagnoser){
4606e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  QualType T = E->getType();
4607e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4608e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Fast path the case where the type is already complete.
4609e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (!T->isIncompleteType())
4610e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    return false;
4611e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4612e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Incomplete array types may be completed by the initializer attached to
4613e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // their definitions. For static data members of class templates we need to
4614e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // instantiate the definition to get this initializer and complete the type.
4615e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (T->isIncompleteArrayType()) {
4616e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4617e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
4618e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        if (Var->isStaticDataMember() &&
4619e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth            Var->getInstantiatedFromStaticDataMember()) {
462091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
462136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
462236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          assert(MSInfo && "Missing member specialization information?");
462336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          if (MSInfo->getTemplateSpecializationKind()
462436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                != TSK_ExplicitSpecialization) {
462536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // If we don't already have a point of instantiation, this is it.
462636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (MSInfo->getPointOfInstantiation().isInvalid()) {
462736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              MSInfo->setPointOfInstantiation(E->getLocStart());
462891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
462991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier              // This is a modification of an existing AST node. Notify
463036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              // listeners.
463136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              if (ASTMutationListener *L = getASTMutationListener())
463236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                L->StaticDataMemberInstantiated(Var);
463336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
463491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
463536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            InstantiateStaticDataMemberDefinition(E->getExprLoc(), Var);
463691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
463736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // Update the type to the newly instantiated definition's type both
463836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // here and within the expression.
463936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (VarDecl *Def = Var->getDefinition()) {
464036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setDecl(Def);
464136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              T = Def->getType();
464236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setType(T);
464336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              E->setType(T);
464436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
4645f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor          }
464691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4647e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // We still go on to try to complete the type independently, as it
4648e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // may also require instantiations or diagnostics if it remains
4649e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // incomplete.
4650e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        }
4651e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      }
4652e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    }
4653e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  }
4654e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4655e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // FIXME: Are there other cases which require instantiating something other
4656e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // than the type to complete the type of an expression?
4657e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4658e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Look through reference types and complete the referred type.
4659e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
4660e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    T = Ref->getPointeeType();
4661e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4662d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(E->getExprLoc(), T, Diagnoser);
4663d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4664d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4665d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregornamespace {
4666f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  struct TypeDiagnoserDiag : Sema::TypeDiagnoser {
4667d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    unsigned DiagID;
466891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4669f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor    TypeDiagnoserDiag(unsigned DiagID)
4670f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor      : Sema::TypeDiagnoser(DiagID == 0), DiagID(DiagID) {}
467191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4672d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) {
4673d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      if (Suppressed) return;
4674d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      S.Diag(Loc, DiagID) << T;
4675d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    }
4676d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  };
4677d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4678d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4679d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, unsigned DiagID) {
4680f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4681d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteExprType(E, Diagnoser);
4682e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth}
4683e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
46841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
46854ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
46864ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
46874ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
468886447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
468986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
469086447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
469186447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
469286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
46934ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
46944ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
46954ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
46964ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
46974ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
46984ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
46994ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
47004ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
470191a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
4702f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                               TypeDiagnoser &Diagnoser) {
4703573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
4704573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
4705690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
4706690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
4707690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
47081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
4709690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
4710690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
47114ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
4712d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  NamedDecl *Def = 0;
4713d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  if (!T->isIncompleteType(&Def)) {
4714d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    // If we know about the definition but it is not visible, complain.
4715d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (!Diagnoser.Suppressed && Def && !LookupResult::isVisible(Def)) {
4716d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // Suppress this error outside of a SFINAE context if we've already
471791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // emitted the error once for this type. There's no usefulness in
4718d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // repeating the diagnostic.
4719d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // FIXME: Add a Fix-It that imports the corresponding module or includes
4720d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // the header.
4721ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Module *Owner = Def->getOwningModule();
4722ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Loc, diag::err_module_private_definition)
4723ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        << T << Owner->getFullModuleName();
4724ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Def->getLocation(), diag::note_previous_definition);
4725ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
4726ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      if (!isSFINAEContext()) {
4727ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        // Recover by implicitly importing this module.
4728ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        createImplicitModuleImport(Loc, Owner);
4729d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      }
4730d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    }
473191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
47324ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
4733d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
47344ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
4735bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const TagType *Tag = T->getAs<TagType>();
4736bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const ObjCInterfaceType *IFace = 0;
473791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4738bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  if (Tag) {
4739bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4740bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->isInvalidDecl())
4741bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
4742bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan
4743bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4744bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->hasExternalLexicalStorage()) {
4745bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(Tag->getDecl());
4746bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!Tag->isIncompleteType())
4747bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4748bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4749bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
4750bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  else if ((IFace = T->getAs<ObjCInterfaceType>())) {
4751bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4752bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->isInvalidDecl())
4753bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
475491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4755bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4756bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->hasExternalLexicalStorage()) {
4757bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(IFace->getDecl());
4758bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!IFace->isIncompleteType())
4759bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4760bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4761bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
476291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4763d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
4764923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
4765923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
4766923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
4767e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor  while (const ConstantArrayType *Array
4768e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor           = Context.getAsConstantArrayType(MaybeTemplate))
4769923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
4770923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
47712943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
4772d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
4773972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
4774972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
4775d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
4776d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                            /*Complain=*/!Diagnoser.Suppressed);
47771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
4778d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
4779564f4c5664f552becbd05407611a92754c40e641Richard Smith      CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass();
4780564f4c5664f552becbd05407611a92754c40e641Richard Smith      if (!Rec->isBeingDefined() && Pattern) {
4781564f4c5664f552becbd05407611a92754c40e641Richard Smith        MemberSpecializationInfo *MSI = Rec->getMemberSpecializationInfo();
4782564f4c5664f552becbd05407611a92754c40e641Richard Smith        assert(MSI && "Missing member specialization information?");
4783357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
4784564f4c5664f552becbd05407611a92754c40e641Richard Smith        if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
4785f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
4786f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
4787f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
4788d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                  /*Complain=*/!Diagnoser.Suppressed);
4789d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
4790d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
4791d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
47922943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
4793d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  if (Diagnoser.Suppressed)
47945842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
4795d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
47964ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
4797d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
479891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
47994ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
480001620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
48014ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
48021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
48034ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
48044ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
4805b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor      << QualType(Tag, 0);
480691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4807b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  // If the Objective-C class was a forward declaration, produce a note.
4808b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  if (IFace && !IFace->getDecl()->isInvalidDecl())
4809b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor    Diag(IFace->getDecl()->getLocation(), diag::note_forward_class);
48104ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
48114ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
48124ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
4813e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
4814fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
481591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                               unsigned DiagID) {
4816f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4817d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(Loc, T, Diagnoser);
4818fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
4819fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
48206666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \brief Get diagnostic %select index for tag kind for
48216666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// literal type diagnostic message.
48226666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// WARNING: Indexes apply to particular diagnostics only!
48236666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos///
48246666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \returns diagnostic %select index.
4825f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matosstatic unsigned getLiteralDiagFromTagKind(TagTypeKind Tag) {
48266666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  switch (Tag) {
4827f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Struct: return 0;
4828f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Interface: return 1;
4829f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Class:  return 2;
4830f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  default: llvm_unreachable("Invalid tag kind for literal type diagnostic!");
48316666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  }
48326666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos}
48336666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos
48349f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @brief Ensure that the type T is a literal type.
48359f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
48369f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// This routine checks whether the type @p T is a literal type. If @p T is an
48379f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// incomplete type, an attempt is made to complete it. If @p T is a literal
48389f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// type, or @p AllowIncompleteType is true and @p T is an incomplete type,
48399f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// returns false. Otherwise, this routine issues the diagnostic @p PD (giving
48409f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// it the type @p T), along with notes explaining why the type is not a
48419f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// literal type, and returns true.
48429f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
48439f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param Loc  The location in the source that the non-literal type
48449f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// diagnostic should refer to.
48459f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
48469f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param T  The type that this routine is examining for literalness.
48479f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
4848f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor/// @param Diagnoser Emits a diagnostic if T is not a literal type.
48499f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
48509f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @returns @c true if @p T is not a literal type and a diagnostic was emitted,
48519f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @c false otherwise.
48529f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithbool Sema::RequireLiteralType(SourceLocation Loc, QualType T,
4853f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                              TypeDiagnoser &Diagnoser) {
48549f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  assert(!T->isDependentType() && "type should not be dependent");
48559f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4856ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  QualType ElemType = Context.getBaseElementType(T);
4857ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  RequireCompleteType(Loc, ElemType, 0);
4858ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
4859a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  if (T->isLiteralType(Context))
48609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return false;
48619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4862f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  if (Diagnoser.Suppressed)
48639f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
48649f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4865f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
48669f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
48679f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (T->isVariableArrayType())
48689f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
48699f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4870ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  const RecordType *RT = ElemType->getAs<RecordType>();
48719f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (!RT)
48729f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
48739f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
48749f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
48759f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4876c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // A partially-defined class type can't be a literal type, because a literal
4877c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // class type must have a trivial destructor (which can't be checked until
4878c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // the class definition is complete).
4879c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  if (!RD->isCompleteDefinition()) {
4880d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T);
4881ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman    return true;
4882c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  }
4883ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
48849f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // If the class has virtual base classes, then it's not an aggregate, and
488586c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // cannot have any constexpr constructors or a trivial default constructor,
488686c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // so is non-literal. This is better to diagnose than the resulting absence
488786c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // of constexpr constructors.
48889f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (RD->getNumVBases()) {
48899f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_virtual_base)
48906666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      << getLiteralDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases();
48919f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
48929f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           E = RD->vbases_end(); I != E; ++I)
489396a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      Diag(I->getLocStart(),
48949f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           diag::note_constexpr_virtual_base_here) << I->getSourceRange();
489586c3ae46250cdcc57778c27826060779a92f3815Richard Smith  } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor() &&
489686c3ae46250cdcc57778c27826060779a92f3815Richard Smith             !RD->hasTrivialDefaultConstructor()) {
48979f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD;
48989f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (RD->hasNonLiteralTypeFieldsOrBases()) {
48999f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
49009f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->bases_end(); I != E; ++I) {
4901a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith      if (!I->getType()->isLiteralType(Context)) {
490296a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        Diag(I->getLocStart(),
49039f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith             diag::note_non_literal_base_class)
49049f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith          << RD << I->getType() << I->getSourceRange();
49059f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
49069f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
49079f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
49089f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::field_iterator I = RD->field_begin(),
49099f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->field_end(); I != E; ++I) {
4910a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith      if (!I->getType()->isLiteralType(Context) ||
4911262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          I->getType().isVolatileQualified()) {
4912262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        Diag(I->getLocation(), diag::note_non_literal_field)
4913581deb3da481053c4993c7600f97acf7768caac5David Blaikie          << RD << *I << I->getType()
4914262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          << I->getType().isVolatileQualified();
49159f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
49169f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
49179f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
49189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (!RD->hasTrivialDestructor()) {
49199f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // All fields and bases are of literal types, so have trivial destructors.
49209f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // If this class's destructor is non-trivial it must be user-declared.
49219f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    CXXDestructorDecl *Dtor = RD->getDestructor();
49229f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    assert(Dtor && "class has literal fields and bases but no dtor?");
49239f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    if (!Dtor)
49249f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      return true;
49259f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
49269f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(Dtor->getLocation(), Dtor->isUserProvided() ?
49279f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_user_provided_dtor :
49289f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_nontrivial_dtor) << RD;
4929ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    if (!Dtor->isUserProvided())
4930ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith      SpecialMemberIsTrivial(Dtor, CXXDestructor, /*Diagnose*/true);
49319f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  }
49329f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
49339f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return true;
49349f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
49359f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
493691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierbool Sema::RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID) {
4937f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4938f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  return RequireLiteralType(Loc, T, Diagnoser);
4939f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor}
4940f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor
4941465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
4942465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
4943465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
4944465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
4945465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
4946e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
4947465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
4948e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
4949465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
4950465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
4951465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
4952465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
4953465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
4954465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
4955465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
4956e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
4957af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
49582a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
4959fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
49602a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
49612a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
49622a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
49632b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
49642b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
4965aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
4966aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
49672b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
4968af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
4969af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
4970af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
4971f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// getDecltypeForExpr - Given an expr, will return the decltype for
4972f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// that expression, according to the rules in C++11
4973f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18.
4974f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregorstatic QualType getDecltypeForExpr(Sema &S, Expr *E) {
4975f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (E->isTypeDependent())
4976f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    return S.Context.DependentTy;
4977f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
49786d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
49796d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   The type denoted by decltype(e) is defined as follows:
49806d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //
49816d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //     - if e is an unparenthesized id-expression or an unparenthesized class
498291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       member access (5.2.5), decltype(e) is the type of the entity named
498391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       by e. If there is no such entity, or if e names a set of overloaded
49846d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       functions, the program is ill-formed;
498584dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  //
498684dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  // We apply the same rules for Objective-C ivar and property references.
4987f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
4988f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
4989f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return VD->getType();
499084dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
4991f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
4992f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return FD->getType();
499384dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCIvarRefExpr *IR = dyn_cast<ObjCIvarRefExpr>(E)) {
499484dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    return IR->getDecl()->getType();
499584dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCPropertyRefExpr *PR = dyn_cast<ObjCPropertyRefExpr>(E)) {
499684dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    if (PR->isExplicitProperty())
499784dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor      return PR->getExplicitProperty()->getType();
4998f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
499984dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor
5000f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  // C++11 [expr.lambda.prim]p18:
5001f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   Every occurrence of decltype((x)) where x is a possibly
5002f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   parenthesized id-expression that names an entity of automatic
5003f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   storage duration is treated as if x were transformed into an
5004f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   access to a corresponding data member of the closure type that
5005f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   would have been declared if x were an odr-use of the denoted
5006f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   entity.
5007f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  using namespace sema;
5008f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (S.getCurLambda()) {
5009f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (isa<ParenExpr>(E)) {
5010f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
5011f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
501268932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          QualType T = S.getCapturedDeclRefType(Var, DRE->getLocation());
501368932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          if (!T.isNull())
501468932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor            return S.Context.getLValueReferenceType(T);
5015f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        }
5016f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      }
5017f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    }
5018f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
5019f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
5020f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
50216d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
50226d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   [...]
50236d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  QualType T = E->getType();
50246d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  switch (E->getValueKind()) {
502591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the
50266d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
50276d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_XValue: T = S.Context.getRValueReferenceType(T); break;
502891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an lvalue, decltype(e) is T&, where T is the
50296d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
50306d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_LValue: T = S.Context.getLValueReferenceType(T); break;
50316d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //  - otherwise, decltype(e) is the type of e.
50326d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_RValue: break;
50336d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  }
503491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5035f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return T;
5036f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor}
5037f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
50382a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
5039fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
50402a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
50412a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
504291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
5043f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return Context.getDecltypeType(E, getDecltypeForExpr(*this, E));
5044af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
5045ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
5046ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType Sema::BuildUnaryTransformType(QualType BaseType,
5047ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::UTTKind UKind,
5048ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       SourceLocation Loc) {
5049ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  switch (UKind) {
5050ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case UnaryTransformType::EnumUnderlyingType:
5051ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) {
5052ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Diag(Loc, diag::err_only_enums_have_underlying_types);
5053ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return QualType();
5054ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    } else {
5055ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      QualType Underlying = BaseType;
5056ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      if (!BaseType->isDependentType()) {
5057ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl();
5058ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        assert(ED && "EnumType has no EnumDecl");
5059ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        DiagnoseUseOfDecl(ED, Loc);
5060ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        Underlying = ED->getIntegerType();
5061ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      }
5062ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(!Underlying.isNull());
5063ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return Context.getUnaryTransformType(BaseType, Underlying,
5064ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                        UnaryTransformType::EnumUnderlyingType);
5065ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
5066ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  }
5067ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  llvm_unreachable("unknown unary transform type");
5068ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
5069b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5070b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) {
5071b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T->isDependentType()) {
50728327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // FIXME: It isn't entirely clear whether incomplete atomic types
50738327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // are allowed or not; for simplicity, ban them for the moment.
5074d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (RequireCompleteType(Loc, T, diag::err_atomic_specifier_bad_type, 0))
50758327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith      return QualType();
50768327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith
5077b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    int DisallowedKind = -1;
50788327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    if (T->isArrayType())
5079b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 1;
5080b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isFunctionType())
5081b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 2;
5082b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isReferenceType())
5083b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 3;
5084b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isAtomicType())
5085b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 4;
5086b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T.hasQualifiers())
5087b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 5;
5088b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (!T.isTriviallyCopyableType(Context))
5089b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      // Some other non-trivially-copyable type (probably a C++ class)
5090b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 6;
5091b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5092b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (DisallowedKind != -1) {
5093b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T;
5094b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return QualType();
5095b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
5096b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5097b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // FIXME: Do we need any handling for ARC here?
5098b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
5099b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5100b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Build the pointer type.
5101b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return Context.getAtomicType(T);
5102b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
5103