SemaType.cpp revision 8c952cd40ccec9d720931f27e7d722fed207d536
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
997e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
998e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
99934b41d939a1328f484511c6002ba2456db879a29Richard Smith    Result = Context.getAutoType(QualType());
1000e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
1001e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
10021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1003a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall  case DeclSpec::TST_unknown_anytype:
1004a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    Result = Context.UnknownAnyTy;
1005a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    break;
1006a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall
1007b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case DeclSpec::TST_atomic:
1008b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.GetTypeFromParser(DS.getRepAsType());
1009b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(!Result.isNull() && "Didn't get a type for _Atomic?");
1010b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc());
1011b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Result.isNull()) {
1012b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Result = Context.IntTy;
1013b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      declarator.setInvalidType(true);
1014b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
101591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
1016b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1017b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_t:
1018b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dTy;
1019b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1020b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1021b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_array_t:
1022b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dArrayTy;
1023b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1024b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1025b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_buffer_t:
1026b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dBufferTy;
1027b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1028b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1029b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_t:
1030b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dTy;
1031b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1032b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1033b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_array_t:
1034b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dArrayTy;
1035b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1036b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
1037b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image3d_t:
1038b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage3dTy;
1039b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
1040b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
104121f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei  case DeclSpec::TST_sampler_t:
104221f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    Result = Context.OCLSamplerTy;
104321f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    break;
104421f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei
1045e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei  case DeclSpec::TST_event_t:
1046e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    Result = Context.OCLEventTy;
1047e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    break;
1048e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei
1049809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
10505153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
1051711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
10525153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
10535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1055958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
1056f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
10574e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().Freestanding)
1058711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
1059fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
106082287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
106182287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
106282287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
1063e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
1064788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
1065e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
1066788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
1067e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
1068e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
1069f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
10701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
107147423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
107247423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
1073711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
10741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1075711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
1076711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
1077711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
1078711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
1079711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1080711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
1081711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
1082711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
1083711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (AttributeList *attrs = DS.getAttributes().getList())
1084f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    processTypeAttrs(state, Result, TAL_DeclSpec, attrs);
10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
108696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
108796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
108896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
108996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
109096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
109196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
109296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
10930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
10944cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        S.Diag(DS.getConstSpecLoc(), diag::warn_typecheck_function_qualifiers)
10954cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith          << Result << DS.getSourceRange();
10960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
10974cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        S.Diag(DS.getVolatileSpecLoc(), diag::warn_typecheck_function_qualifiers)
10984cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith          << Result << DS.getSourceRange();
10990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
11004cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        assert((TypeQuals & (DeclSpec::TQ_restrict | DeclSpec::TQ_atomic)) &&
11014cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith               "Has CVRA quals but not C, V, R, or A?");
11024cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // No diagnostic; we'll diagnose 'restrict' or '_Atomic' applied to a
11034cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // function type later, in BuildQualifiedType.
110496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
110596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
11061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1107f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
1108f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
1109f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
1110f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
1111f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
11121a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
11131a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
1114f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
11150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
11160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
11174cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      TypeQuals &= ~DeclSpec::TQ_atomic;
11181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
11191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1120bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // C90 6.5.3 constraints: "The same type qualifier shall not appear more
1121bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // than once in the same specifier-list or qualifier-list, either directly
1122bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // or via one or more typedefs."
112391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus
1124bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman        && TypeQuals & Result.getCVRQualifiers()) {
1125bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()) {
112691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec)
1127bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "const";
1128bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1129bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
1130bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()) {
113191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec)
1132bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "volatile";
1133bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1134bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
11354cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // C90 doesn't have restrict nor _Atomic, so it doesn't force us to
11364cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // produce a warning in this case.
1137bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    }
1138bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
11394cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    QualType Qualified = S.BuildQualifiedType(Result, DeclLoc, TypeQuals, &DS);
11404cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
11414cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // If adding qualifiers fails, just use the unqualified type.
11424cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (Qualified.isNull())
11434cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      declarator.setInvalidType(true);
11444cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    else
11454cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      Result = Qualified;
114696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
11470953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1148f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
1149f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
1150f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
1151cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
1152cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
1153cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
11541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1155cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
1156cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1157cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
11582865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
115993d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith                                  Qualifiers Qs, const DeclSpec *DS) {
11602865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
11612865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
11622865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
11632865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
11642865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
11652865474261a608c7873b87ba4af110d17907896dJohn McCall
116693d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith    if (T->isAnyPointerType() || T->isReferenceType() ||
116793d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        T->isMemberPointerType()) {
116893d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      QualType EltTy;
116993d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      if (T->isObjCObjectPointerType())
117093d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = T;
117193d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>())
117293d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = PTy->getPointeeType();
117393d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      else
117493d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        EltTy = T->getPointeeType();
117593d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith
117693d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      // If we have a pointer or reference, the pointee must have an object
117793d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      // incomplete type.
117893d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      if (!EltTy->isIncompleteOrObjectType()) {
11792865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
118093d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith        ProblemTy = EltTy;
118191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
118293d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith    } else if (!T->isDependentType()) {
118393d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      DiagID = diag::err_typecheck_invalid_restrict_not_pointer;
11842865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
11852865474261a608c7873b87ba4af110d17907896dJohn McCall    }
11862865474261a608c7873b87ba4af110d17907896dJohn McCall
11872865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
118893d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smith      Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << ProblemTy;
11892865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
11902865474261a608c7873b87ba4af110d17907896dJohn McCall    }
11912865474261a608c7873b87ba4af110d17907896dJohn McCall  }
11922865474261a608c7873b87ba4af110d17907896dJohn McCall
11932865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
11942865474261a608c7873b87ba4af110d17907896dJohn McCall}
11952865474261a608c7873b87ba4af110d17907896dJohn McCall
11964cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard SmithQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
11974cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith                                  unsigned CVRA, const DeclSpec *DS) {
11984cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // Convert from DeclSpec::TQ to Qualifiers::TQ by just dropping TQ_atomic.
11994cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  unsigned CVR = CVRA & ~DeclSpec::TQ_atomic;
12004cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
12014cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // C11 6.7.3/5:
12024cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   If the same qualifier appears more than once in the same
12034cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   specifier-qualifier-list, either directly or via one or more typedefs,
12044cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //   the behavior is the same as if it appeared only once.
12054cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  //
12064cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // It's not specified what happens when the _Atomic qualifier is applied to
12074cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // a type specified with the _Atomic specifier, but we assume that this
12084cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  // should be treated as if the _Atomic qualifier appeared multiple times.
12094cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  if (CVRA & DeclSpec::TQ_atomic && !T->isAtomicType()) {
12104cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // C11 6.7.3/5:
12114cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   If other qualifiers appear along with the _Atomic qualifier in a
12124cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   specifier-qualifier-list, the resulting type is the so-qualified
12134cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //   atomic type.
12144cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    //
12154cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // Don't need to worry about array types here, since _Atomic can't be
12164cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // applied to such types.
12174cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    SplitQualType Split = T.getSplitUnqualifiedType();
12184cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    T = BuildAtomicType(QualType(Split.Ty, 0),
12194cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith                        DS ? DS->getAtomicSpecLoc() : Loc);
12204cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (T.isNull())
12214cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      return T;
12224cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    Split.Quals.addCVRQualifiers(CVR);
12234cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    return BuildQualifiedType(T, Loc, Split.Quals);
12244cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
12254cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
12264cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  return BuildQualifiedType(T, Loc, Qualifiers::fromCVRMask(CVR), DS);
12274cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith}
12284cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
1229075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
1230075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
1231075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
1232075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
1233075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
1234f85e193739c953358c865005855253af4f68a497John McCall/// Given that we're building a pointer or reference to the given
1235f85e193739c953358c865005855253af4f68a497John McCallstatic QualType inferARCLifetimeForPointee(Sema &S, QualType type,
1236f85e193739c953358c865005855253af4f68a497John McCall                                           SourceLocation loc,
1237f85e193739c953358c865005855253af4f68a497John McCall                                           bool isReference) {
1238f85e193739c953358c865005855253af4f68a497John McCall  // Bail out if retention is unrequired or already specified.
1239f85e193739c953358c865005855253af4f68a497John McCall  if (!type->isObjCLifetimeType() ||
1240f85e193739c953358c865005855253af4f68a497John McCall      type.getObjCLifetime() != Qualifiers::OCL_None)
1241f85e193739c953358c865005855253af4f68a497John McCall    return type;
1242f85e193739c953358c865005855253af4f68a497John McCall
1243f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None;
1244f85e193739c953358c865005855253af4f68a497John McCall
1245f85e193739c953358c865005855253af4f68a497John McCall  // If the object type is const-qualified, we can safely use
1246f85e193739c953358c865005855253af4f68a497John McCall  // __unsafe_unretained.  This is safe (because there are no read
1247f85e193739c953358c865005855253af4f68a497John McCall  // barriers), and it'll be safe to coerce anything but __weak* to
1248f85e193739c953358c865005855253af4f68a497John McCall  // the resulting type.
1249f85e193739c953358c865005855253af4f68a497John McCall  if (type.isConstQualified()) {
1250f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
1251f85e193739c953358c865005855253af4f68a497John McCall
1252f85e193739c953358c865005855253af4f68a497John McCall  // Otherwise, check whether the static type does not require
1253f85e193739c953358c865005855253af4f68a497John McCall  // retaining.  This currently only triggers for Class (possibly
1254f85e193739c953358c865005855253af4f68a497John McCall  // protocol-qualifed, and arrays thereof).
1255f85e193739c953358c865005855253af4f68a497John McCall  } else if (type->isObjCARCImplicitlyUnretainedType()) {
1256f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
12575b76f373d23cc3b292ecf523349aaaa388eea375Argyrios Kyrtzidis
1258ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // If we are in an unevaluated context, like sizeof, skip adding a
1259ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // qualification.
126071f55f771794674a410171dbf3cb5dbedf95d033David Blaikie  } else if (S.isUnevaluatedContext()) {
1261ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    return type;
1262f85e193739c953358c865005855253af4f68a497John McCall
1263e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // If that failed, give an error and recover using __strong.  __strong
1264e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // is the option most likely to prevent spurious second-order diagnostics,
1265e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // like when binding a reference to a field.
1266f85e193739c953358c865005855253af4f68a497John McCall  } else {
1267f85e193739c953358c865005855253af4f68a497John McCall    // These types can show up in private ivars in system headers, so
1268f85e193739c953358c865005855253af4f68a497John McCall    // we need this to not be an error in those cases.  Instead we
1269f85e193739c953358c865005855253af4f68a497John McCall    // want to delay.
1270f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1271ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.DelayedDiagnostics.add(
1272ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman          sema::DelayedDiagnostic::makeForbiddenType(loc,
1273ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman              diag::err_arc_indirect_no_ownership, type, isReference));
1274f85e193739c953358c865005855253af4f68a497John McCall    } else {
1275ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference;
1276f85e193739c953358c865005855253af4f68a497John McCall    }
1277e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall    implicitLifetime = Qualifiers::OCL_Strong;
1278f85e193739c953358c865005855253af4f68a497John McCall  }
1279f85e193739c953358c865005855253af4f68a497John McCall  assert(implicitLifetime && "didn't infer any lifetime!");
1280f85e193739c953358c865005855253af4f68a497John McCall
1281f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers qs;
1282f85e193739c953358c865005855253af4f68a497John McCall  qs.addObjCLifetime(implicitLifetime);
1283f85e193739c953358c865005855253af4f68a497John McCall  return S.Context.getQualifiedType(type, qs);
1284f85e193739c953358c865005855253af4f68a497John McCall}
1285f85e193739c953358c865005855253af4f68a497John McCall
1286cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
1287cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1288cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
1289cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1290cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1291cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
1292cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
1293cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1294cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
1295cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1296cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1297cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
1298cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
12992865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
1300cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
1301cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
1302cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
1303cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
1304ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1305cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1306cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1307cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1308c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
130992e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
1310f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build pointers to unqualified pointers.
13114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1312f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false);
1313f85e193739c953358c865005855253af4f68a497John McCall
1314cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
13152865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
1316cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1317cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1318cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
1319cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1320cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
1321cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1322cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1323cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
1324cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
1325cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1326cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
1327cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1328cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1329cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
1330cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
133154e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
13322865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
133354e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
133491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  assert(Context.getCanonicalType(T) != Context.OverloadTy &&
13359625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
133691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
133769d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // C++0x [dcl.ref]p6:
133891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   If a typedef (7.1.3), a type template-parameter (14.3.1), or a
133991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
134091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   type T, an attempt to create the type "lvalue reference to cv TR" creates
134191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   the type "lvalue reference to T", while an attempt to create the type
134269d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   "rvalue reference to cv TR" creates the type TR.
134354e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
134454e14c4db764c0636160d26c5bbf491637c83a76John McCall
134554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
134654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
134754e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
134854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
134954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
135054e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
135154e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
135254e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
135354e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
135454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
135554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
135669d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // collapsing of references-to-references as described in C++0x.
135769d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // DR 106 and 540 introduce reference-collapsing into C++98/03.
1358cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1359cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
136033a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
1361cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
1362cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
1363cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
1364cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1365cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1366cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1367f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build references to unqualified pointers.
13684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1369f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true);
1370f85e193739c953358c865005855253af4f68a497John McCall
1371cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
13727c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
13732865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
13742865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
1375cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1376cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1377e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// Check whether the specified array size makes the array type a VLA.  If so,
1378e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// return true, if not, return the size of the array in SizeVal.
1379282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smithstatic bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal) {
1380282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode
1381282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // (like gnu99, but not c99) accept any evaluatable value as an extension.
1382ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  class VLADiagnoser : public Sema::VerifyICEDiagnoser {
1383ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  public:
1384ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    VLADiagnoser() : Sema::VerifyICEDiagnoser(true) {}
138591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1386ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) {
1387ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
138891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1389ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseFold(Sema &S, SourceLocation Loc, SourceRange SR) {
1390ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      S.Diag(Loc, diag::ext_vla_folded_to_constant) << SR;
1391ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
1392ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  } Diagnoser;
139391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1394ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  return S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser,
1395ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                           S.LangOpts.GNUMode).isInvalid();
1396e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner}
1397e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1398e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1399cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
1400cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1401cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
1402cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1403cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
14041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
14051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
1406cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1407efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Brackets The range from the opening '[' to the closing ']'.
1408cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1409cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
1410cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1411cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1412cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
1413cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
1414cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1415cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
14167e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
14170953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
14187e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
14194e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
1420138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
1421138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
142291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   type, the (possibly cv-qualified) type void, a function type or an
1423138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
1424138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
1425bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    // C++ [dcl.array]p3:
1426bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   When several "array of" specifications are adjacent, [...] only the
1427bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   first of the constant expressions that specify the bounds of the arrays
1428bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   may be omitted.
1429bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //
1430138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
1431138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
1432138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
1433138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
1434138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1435138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
143691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1437bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    if (T->isVoidType() || T->isIncompleteArrayType()) {
1438923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1439923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1440923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
144191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
144291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (RequireNonAbstractType(Brackets.getBegin(), T,
1443138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
1444138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
144591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1446923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
1447138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1448138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
1449923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
1450923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
1451923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1452923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
1453cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1454cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
1455cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
1456ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1457cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1458cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
14591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
146034b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (T->getContainedAutoType()) {
146134b41d939a1328f484511c6002ba2456db879a29Richard Smith    Diag(Loc, diag::err_illegal_decl_array_of_auto)
146234b41d939a1328f484511c6002ba2456db879a29Richard Smith      << getPrintableNameForEntity(Entity) << T;
1463e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
1464e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
14651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14666217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
1467cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
1468cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
1469cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
1470cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
1471c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
1472c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1473c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
1474cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
14751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1476806054db6653d29cb0d9692df3612cbcd03d0530John McCall  // Do placeholder conversions on the array size expression.
1477806054db6653d29cb0d9692df3612cbcd03d0530John McCall  if (ArraySize && ArraySize->hasPlaceholderType()) {
1478806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ExprResult Result = CheckPlaceholderExpr(ArraySize);
1479806054db6653d29cb0d9692df3612cbcd03d0530John McCall    if (Result.isInvalid()) return QualType();
1480806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ArraySize = Result.take();
1481806054db6653d29cb0d9692df3612cbcd03d0530John McCall  }
1482806054db6653d29cb0d9692df3612cbcd03d0530John McCall
14835e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
1484429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ArraySize && !ArraySize->isRValue()) {
1485429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult Result = DefaultLvalueConversion(ArraySize);
1486429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Result.isInvalid())
1487429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return QualType();
1488429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
1489429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ArraySize = Result.take();
1490429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
14915e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1492cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
1493282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // C++11 allows contextual conversions to such types.
149480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (!getLangOpts().CPlusPlus11 &&
1495282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      ArraySize && !ArraySize->isTypeDependent() &&
14961274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1497cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1498cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
1499cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1500cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1501282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
15022767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
1503cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
1504f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
15057e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
1506f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
1507f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
1508ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
15097e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
1510282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  } else if ((!T->isDependentType() && !T->isIncompleteType() &&
1511282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith              !T->isConstantSizeType()) ||
1512282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith             isArraySizeVLA(*this, ArraySize, ConstVal)) {
1513282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // Even in C++11, don't allow contextual conversions in the array bound
1514282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // of a VLA.
151580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (getLangOpts().CPlusPlus11 &&
1516282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1517282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1518282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        << ArraySize->getType() << ArraySize->getSourceRange();
1519282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      return QualType();
1520282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    }
1521282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
1522e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with an element type that has a non-constant-size is a VLA.
1523e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with a non-ICE size is a VLA.  We accept any expression
1524e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // that we can fold to a non-zero positive value as an extension.
15257e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1526cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
1527cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1528cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
1529923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
1530b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
1531b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1532b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1533b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
1534b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1535b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
1536923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1537923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1538923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
153902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
154002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
154191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      Diag(ArraySize->getLocStart(),
154202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
154302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
1544923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
154520cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne
154620cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      if (ASM == ArrayType::Static) {
154720cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        Diag(ArraySize->getLocStart(),
154820cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne             diag::warn_typecheck_zero_static_array_size)
154920cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne          << ArraySize->getSourceRange();
155020cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        ASM = ArrayType::Normal;
155120cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      }
155291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
15532767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor               !T->isIncompleteType()) {
155491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // Is the array too large?
15552767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
15562767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
15572767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
15582767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
15592767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
15602767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
15611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
156291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
156346a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
1564cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1565617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly
1566617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  // OpenCL v1.2 s6.9.d: variable length arrays are not supported.
1567617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  if (getLangOpts().OpenCL && T->isVariableArrayType()) {
1568617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    Diag(Loc, diag::err_opencl_vla);
1569617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    return QualType();
1570617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  }
1571af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
15724e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().C99) {
15730fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
15740fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
1575f85e193739c953358c865005855253af4f68a497John McCall      QualType BaseT = Context.getBaseElementType(T);
157691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      if (!T->isDependentType() &&
15773c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT.isPODType(Context) &&
15783c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT->isObjCLifetimeType()) {
15790fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
1580f85e193739c953358c865005855253af4f68a497John McCall          << BaseT;
15810fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
158291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
1583a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
1584a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
1585a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
1586a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
1587a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
15880fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
15890fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
15900fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, 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  //
1731b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // For now, just require a complete type, which will instantiate
1732b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // templates.  This will also error if the type is just forward-declared,
1733b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // which is a bug, but it's a bug that saves us from dealing with some
1734b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // complexities at the moment.
1735b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  if (Context.getTargetInfo().getCXXABI().isMicrosoft() &&
1736679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos      RequireCompleteType(Loc, Class, diag::err_incomplete_type))
1737679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos    return QualType();
1738679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos
17392865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
1740949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
17411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17429a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
17439a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
17449a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
17459a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
1746efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Loc The source location, used for diagnostics.
17479a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
17489a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
17499a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
17509a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
17519a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
17529a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
175391cbbbf506c892a26d4301e2b3ccd377b0938817Chad RosierQualType Sema::BuildBlockPointerType(QualType T,
17541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
17559a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
17560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
17579a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
17589a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
17599a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
17601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17612865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
17629a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
17639a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
1764b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1765b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
17663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
1767a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
17683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
17693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
17703f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1771a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
1772f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
1773e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
1774a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
1775e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
17761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1777a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
1778e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
1779e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
1780e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
1781a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
1782a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
1783a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            unsigned chunkIndex);
1784a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis
1785f85e193739c953358c865005855253af4f68a497John McCall/// Given that this is the declaration of a parameter under ARC,
1786f85e193739c953358c865005855253af4f68a497John McCall/// attempt to infer attributes and such for pointer-to-whatever
1787f85e193739c953358c865005855253af4f68a497John McCall/// types.
1788f85e193739c953358c865005855253af4f68a497John McCallstatic void inferARCWriteback(TypeProcessingState &state,
1789f85e193739c953358c865005855253af4f68a497John McCall                              QualType &declSpecType) {
1790f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
1791f85e193739c953358c865005855253af4f68a497John McCall  Declarator &declarator = state.getDeclarator();
1792f85e193739c953358c865005855253af4f68a497John McCall
1793f85e193739c953358c865005855253af4f68a497John McCall  // TODO: should we care about decl qualifiers?
1794f85e193739c953358c865005855253af4f68a497John McCall
1795f85e193739c953358c865005855253af4f68a497John McCall  // Check whether the declarator has the expected form.  We walk
1796f85e193739c953358c865005855253af4f68a497John McCall  // from the inside out in order to make the block logic work.
1797f85e193739c953358c865005855253af4f68a497John McCall  unsigned outermostPointerIndex = 0;
1798f85e193739c953358c865005855253af4f68a497John McCall  bool isBlockPointer = false;
1799f85e193739c953358c865005855253af4f68a497John McCall  unsigned numPointers = 0;
1800f85e193739c953358c865005855253af4f68a497John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
1801f85e193739c953358c865005855253af4f68a497John McCall    unsigned chunkIndex = i;
1802f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex);
1803f85e193739c953358c865005855253af4f68a497John McCall    switch (chunk.Kind) {
1804f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Paren:
1805f85e193739c953358c865005855253af4f68a497John McCall      // Ignore parens.
1806f85e193739c953358c865005855253af4f68a497John McCall      break;
1807f85e193739c953358c865005855253af4f68a497John McCall
1808f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Reference:
1809f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Pointer:
1810f85e193739c953358c865005855253af4f68a497John McCall      // Count the number of pointers.  Treat references
1811f85e193739c953358c865005855253af4f68a497John McCall      // interchangeably as pointers; if they're mis-ordered, normal
1812f85e193739c953358c865005855253af4f68a497John McCall      // type building will discover that.
1813f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1814f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1815f85e193739c953358c865005855253af4f68a497John McCall      break;
1816f85e193739c953358c865005855253af4f68a497John McCall
1817f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::BlockPointer:
1818f85e193739c953358c865005855253af4f68a497John McCall      // If we have a pointer to block pointer, that's an acceptable
1819f85e193739c953358c865005855253af4f68a497John McCall      // indirect reference; anything else is not an application of
1820f85e193739c953358c865005855253af4f68a497John McCall      // the rules.
1821f85e193739c953358c865005855253af4f68a497John McCall      if (numPointers != 1) return;
1822f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1823f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1824f85e193739c953358c865005855253af4f68a497John McCall      isBlockPointer = true;
1825f85e193739c953358c865005855253af4f68a497John McCall
1826f85e193739c953358c865005855253af4f68a497John McCall      // We don't care about pointer structure in return values here.
1827f85e193739c953358c865005855253af4f68a497John McCall      goto done;
1828f85e193739c953358c865005855253af4f68a497John McCall
1829f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Array: // suppress if written (id[])?
1830f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Function:
1831f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::MemberPointer:
1832f85e193739c953358c865005855253af4f68a497John McCall      return;
1833f85e193739c953358c865005855253af4f68a497John McCall    }
1834f85e193739c953358c865005855253af4f68a497John McCall  }
1835f85e193739c953358c865005855253af4f68a497John McCall done:
1836f85e193739c953358c865005855253af4f68a497John McCall
1837f85e193739c953358c865005855253af4f68a497John McCall  // If we have *one* pointer, then we want to throw the qualifier on
1838f85e193739c953358c865005855253af4f68a497John McCall  // the declaration-specifiers, which means that it needs to be a
1839f85e193739c953358c865005855253af4f68a497John McCall  // retainable object type.
1840f85e193739c953358c865005855253af4f68a497John McCall  if (numPointers == 1) {
1841f85e193739c953358c865005855253af4f68a497John McCall    // If it's not a retainable object type, the rule doesn't apply.
1842f85e193739c953358c865005855253af4f68a497John McCall    if (!declSpecType->isObjCRetainableType()) return;
1843f85e193739c953358c865005855253af4f68a497John McCall
1844f85e193739c953358c865005855253af4f68a497John McCall    // If it already has lifetime, don't do anything.
1845f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType.getObjCLifetime()) return;
1846f85e193739c953358c865005855253af4f68a497John McCall
1847f85e193739c953358c865005855253af4f68a497John McCall    // Otherwise, modify the type in-place.
1848f85e193739c953358c865005855253af4f68a497John McCall    Qualifiers qs;
184991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1850f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType->isObjCARCImplicitlyUnretainedType())
1851f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
1852f85e193739c953358c865005855253af4f68a497John McCall    else
1853f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing);
1854f85e193739c953358c865005855253af4f68a497John McCall    declSpecType = S.Context.getQualifiedType(declSpecType, qs);
1855f85e193739c953358c865005855253af4f68a497John McCall
1856f85e193739c953358c865005855253af4f68a497John McCall  // If we have *two* pointers, then we want to throw the qualifier on
1857f85e193739c953358c865005855253af4f68a497John McCall  // the outermost pointer.
1858f85e193739c953358c865005855253af4f68a497John McCall  } else if (numPointers == 2) {
1859f85e193739c953358c865005855253af4f68a497John McCall    // If we don't have a block pointer, we need to check whether the
1860f85e193739c953358c865005855253af4f68a497John McCall    // declaration-specifiers gave us something that will turn into a
1861f85e193739c953358c865005855253af4f68a497John McCall    // retainable object pointer after we slap the first pointer on it.
1862f85e193739c953358c865005855253af4f68a497John McCall    if (!isBlockPointer && !declSpecType->isObjCObjectType())
1863f85e193739c953358c865005855253af4f68a497John McCall      return;
1864f85e193739c953358c865005855253af4f68a497John McCall
1865f85e193739c953358c865005855253af4f68a497John McCall    // Look for an explicit lifetime attribute there.
1866f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex);
18671c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis    if (chunk.Kind != DeclaratorChunk::Pointer &&
18681c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis        chunk.Kind != DeclaratorChunk::BlockPointer)
18691c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis      return;
1870f85e193739c953358c865005855253af4f68a497John McCall    for (const AttributeList *attr = chunk.getAttrs(); attr;
1871f85e193739c953358c865005855253af4f68a497John McCall           attr = attr->getNext())
18728e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt      if (attr->getKind() == AttributeList::AT_ObjCOwnership)
1873f85e193739c953358c865005855253af4f68a497John McCall        return;
1874f85e193739c953358c865005855253af4f68a497John McCall
1875a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis    transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing,
1876a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                          outermostPointerIndex);
1877f85e193739c953358c865005855253af4f68a497John McCall
1878f85e193739c953358c865005855253af4f68a497John McCall  // Any other number of pointers/references does not trigger the rule.
1879f85e193739c953358c865005855253af4f68a497John McCall  } else return;
1880f85e193739c953358c865005855253af4f68a497John McCall
1881f85e193739c953358c865005855253af4f68a497John McCall  // TODO: mark whether we did this inference?
1882f85e193739c953358c865005855253af4f68a497John McCall}
1883f85e193739c953358c865005855253af4f68a497John McCall
18848c952cd40ccec9d720931f27e7d722fed207d536Richard Smithstatic void diagnoseIgnoredQualifiers(
18858c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    Sema &S, unsigned Quals,
18868c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation FallbackLoc,
18878c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation ConstQualLoc = SourceLocation(),
18888c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation VolatileQualLoc = SourceLocation(),
18898c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation RestrictQualLoc = SourceLocation(),
18908c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation AtomicQualLoc = SourceLocation()) {
18918c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  assert(Quals && "no qualifiers to diagnose");
18928c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  const SourceManager &SM = S.getSourceManager();
18938c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
18948c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  struct Qual {
18958c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    unsigned Mask;
18968c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    const char *Name;
18978c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    SourceLocation Loc;
18988c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  } const QualKinds[4] = {
18998c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_const, "const", ConstQualLoc },
19008c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_volatile, "volatile", VolatileQualLoc },
19018c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_restrict, "restrict", RestrictQualLoc },
19028c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    { DeclSpec::TQ_atomic, "_Atomic", AtomicQualLoc }
19038c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  };
19048c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19058c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  llvm::SmallString<32> QualStr;
1906d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  unsigned NumQuals = 0;
1907d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  SourceLocation Loc;
19088c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  FixItHint FixIts[4];
19098c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19108c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // Build a string naming the redundant qualifiers.
19118c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  for (unsigned I = 0; I != 4; ++I) {
19128c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    if (Quals & QualKinds[I].Mask) {
19138c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if (!QualStr.empty()) QualStr += ' ';
19148c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      QualStr += QualKinds[I].Name;
19158c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19168c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // If we have a location for the qualifier, offer a fixit.
19178c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      SourceLocation QualLoc = QualKinds[I].Loc;
19188c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if (!QualLoc.isInvalid()) {
19198c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        FixIts[NumQuals] = FixItHint::CreateRemoval(QualLoc);
19208c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        if (Loc.isInvalid() || SM.isBeforeInTranslationUnit(QualLoc, Loc))
19218c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          Loc = QualLoc;
19228c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      }
1923d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
19248c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      ++NumQuals;
19258c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
19268c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  }
1927d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
19288c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  S.Diag(Loc.isInvalid() ? FallbackLoc : Loc, diag::warn_qual_return_type)
19298c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    << QualStr << NumQuals << FixIts[0] << FixIts[1] << FixIts[2] << FixIts[3];
19308c952cd40ccec9d720931f27e7d722fed207d536Richard Smith}
1931a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg
19328c952cd40ccec9d720931f27e7d722fed207d536Richard Smith// Diagnose pointless type qualifiers on the return type of a function.
19338c952cd40ccec9d720931f27e7d722fed207d536Richard Smithstatic void diagnoseIgnoredFunctionQualifiers(Sema &S, QualType RetTy,
19348c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                              Declarator &D,
19358c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                              unsigned FunctionChunkIndex) {
19368c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  unsigned AtomicQual = RetTy->isAtomicType() ? DeclSpec::TQ_atomic : 0;
19378c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19388c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  if (D.getTypeObject(FunctionChunkIndex).Fun.hasTrailingReturnType()) {
19398c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    // FIXME: TypeSourceInfo doesn't preserve location information for
19408c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    // qualifiers.
19418c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    diagnoseIgnoredQualifiers(S, RetTy.getCVRQualifiers() | AtomicQual,
19428c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                              D.getIdentifierLoc());
19438c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    return;
1944d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
19458c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19468c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  for (unsigned OuterChunkIndex = FunctionChunkIndex + 1,
19478c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                End = D.getNumTypeObjects();
19488c952cd40ccec9d720931f27e7d722fed207d536Richard Smith       OuterChunkIndex != End; ++OuterChunkIndex) {
19498c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    DeclaratorChunk &OuterChunk = D.getTypeObject(OuterChunkIndex);
19508c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    switch (OuterChunk.Kind) {
19518c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Paren:
19528c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      continue;
19538c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19548c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Pointer: {
19558c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      DeclaratorChunk::PointerTypeInfo &PTI = OuterChunk.Ptr;
19568c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      diagnoseIgnoredQualifiers(
19578c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          S, PTI.TypeQuals,
19588c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation(),
19598c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
19608c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
19618c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
19628c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          SourceLocation::getFromRawEncoding(PTI.AtomicQualLoc));
19638c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      return;
19648c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
19658c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19668c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Function:
19678c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::BlockPointer:
19688c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Reference:
19698c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::Array:
19708c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    case DeclaratorChunk::MemberPointer:
19718c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // FIXME: We can't currently provide an accurate source location and a
19728c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      // fix-it hint for these.
19738c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      diagnoseIgnoredQualifiers(S, RetTy.getCVRQualifiers() | AtomicQual,
19748c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                                D.getIdentifierLoc());
19758c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      return;
19768c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    }
19778c952cd40ccec9d720931f27e7d722fed207d536Richard Smith
19788c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    llvm_unreachable("unknown declarator chunk kind");
19794cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
1980d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
19818c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // If the qualifiers come from a conversion function type, don't diagnose
19828c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // them -- they're not necessarily redundant, since such a conversion
19838c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // operator can be explicitly called as "x.operator const int()".
19848c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  if (D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId)
19858c952cd40ccec9d720931f27e7d722fed207d536Richard Smith    return;
1986d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
19878c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // Just parens all the way out to the decl specifiers. Diagnose any qualifiers
19888c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  // which are present there.
19898c952cd40ccec9d720931f27e7d722fed207d536Richard Smith  diagnoseIgnoredQualifiers(S, D.getDeclSpec().getTypeQualifiers() | AtomicQual,
19908c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getIdentifierLoc(),
19918c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getConstSpecLoc(),
19928c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getVolatileSpecLoc(),
19938c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getRestrictSpecLoc(),
19948c952cd40ccec9d720931f27e7d722fed207d536Richard Smith                            D.getDeclSpec().getAtomicSpecLoc());
1995d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth}
1996d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
19978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
19988cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                             TypeSourceInfo *&ReturnTypeInfo) {
19998cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &SemaRef = state.getSema();
20008cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
2001930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
20028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ReturnTypeInfo = 0;
2003711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
20048cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // The TagDecl owned by the DeclSpec.
20058cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TagDecl *OwnedTagDecl = 0;
20068999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl
20073f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
200898a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian  case UnqualifiedId::IK_ImplicitSelfParam:
20093f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
20108999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  case UnqualifiedId::IK_Identifier:
20110486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
20123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
20138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = ConvertDeclSpecToType(state);
201491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2015591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
2016d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
201715987970eeaa1842c29ec8797affd1c1dea05585Abramo Bagnara      // Owned declaration is embedded in declarator.
2018d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl->setEmbeddedInDeclarator(true);
2019591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
2020930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
2021930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
20223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
20230efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
20243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
2025930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
202691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // "void" instead.
20278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = SemaRef.Context.VoidTy;
2028a78a640c5f59720f2c2b8034eca4fbf8525d9026Rafael Espindola    if (AttributeList *attrs = D.getDeclSpec().getAttributes().getList())
2029f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclSpec, attrs);
2030930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
203148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
203248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
203348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
203448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
203591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId,
20368cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  &ReturnTypeInfo);
203748026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
2038930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
2039dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
2040711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
2041711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
2042711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2043d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
2044d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // In C++11, a function declarator using 'auto' must have a trailing return
20458110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // type (this is checked later) and we can skip this. In other languages
20468110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // using auto, we need to check regardless.
204734b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
204880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      (!SemaRef.getLangOpts().CPlusPlus11 || !D.isFunctionDeclarator())) {
2049baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
20501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2051baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
2052baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
2053b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("K&R type lists aren't allowed in C++");
2054f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2055f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman      llvm_unreachable("Can't specify a type specifier in lambda grammar");
2056cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2057cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
2058baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
2059baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
2060baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2061baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
20627a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)
20637a614d8380297fcd2bc23986241905d97222948cRichard Smith        break;
20648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) {
2065eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie      case TTK_Enum: llvm_unreachable("unhandled tag kind");
2066465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
2067465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
2068465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
20696666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      case TTK_Interface: Error = 4; /* Interface member */ break;
20701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
2071baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2072baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
207317b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
20746666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 5; // Exception declaration
2075baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2076baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
20776666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 6; // Template parameter
2078baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2079baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
20806666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 7; // Block literal
208134b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
208234b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TemplateTypeArgContext:
20836666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 8; // Template type argument
208434b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
2085162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
20863e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
20876666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 10; // Type alias
2088162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      break;
20897796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
20906666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11; // Function return type
20917796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
209234b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TypeNameContext:
20936666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 12; // Generic
2094baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2095baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
2096baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
2097baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
2098baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
20990b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
2100baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
2101baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
2102baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
2103ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith    if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
21046666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 9;
2105ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith
21068110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    // In Objective-C it is an error to use 'auto' on a function declarator.
21078110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    if (D.isFunctionDeclarator())
21086666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11;
21098110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith
2110d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.spec.auto]p2: 'auto' is always fine if the declarator
2111e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // contains a trailing return type. That is only legal at the outermost
2112e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // level. Check all declarator chunks (outermost first) anyway, to give
2113e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // better diagnostics.
211480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (SemaRef.getLangOpts().CPlusPlus11 && Error != -1) {
2115e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2116e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned chunkIndex = e - i - 1;
2117e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        state.setCurrentChunkIndex(chunkIndex);
2118e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2119e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (DeclType.Kind == DeclaratorChunk::Function) {
2120e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
212154655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (FTI.hasTrailingReturnType()) {
2122e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Error = -1;
2123e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            break;
2124e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          }
2125e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        }
2126e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2127e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    }
2128e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2129baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
21308cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
21318cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_auto_not_allowed)
2132baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
21338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = SemaRef.Context.IntTy;
2134baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
21350aa86c0463a881be85fd34e04c7de3379997621dRichard Smith    } else
21360aa86c0463a881be85fd34e04c7de3379997621dRichard Smith      SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
21370aa86c0463a881be85fd34e04c7de3379997621dRichard Smith                   diag::warn_cxx98_compat_auto_type_specifier);
2138baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
21398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
21404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().CPlusPlus &&
21415e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall      OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) {
21428cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // Check the contexts where C++ forbids the declaration of a new class
21438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // or enumeration in a type-specifier-seq.
21448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    switch (D.getContext()) {
21457796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
21467796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // Class and enumeration definitions are syntactically not allowed in
21477796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // trailing return types.
21487796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      llvm_unreachable("parser should not have allowed this");
21497796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
21508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::FileContext:
21518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::MemberContext:
21528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockContext:
21538cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ForContext:
21548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockLiteralContext:
2155f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2156d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // C++11 [dcl.type]p3:
21578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   A type-specifier-seq shall not define a class or enumeration unless
21588cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   it appears in the type-id of an alias-declaration (7.1.3) that is not
21598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   the declaration of a template-declaration.
21608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasDeclContext:
21618cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
21628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasTemplateContext:
21638cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
21648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_alias_template)
21658cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2166601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
21678cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
21688cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TypeNameContext:
21698cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateParamContext:
21708cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXNewContext:
21718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXCatchContext:
21728cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ObjCCatchContext:
21738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateTypeArgContext:
21748cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
21758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_type_specifier)
21768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2177601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
21788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
21798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::PrototypeContext:
2180cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2181cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
21828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::KNRTypeListContext:
21838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ [dcl.fct]p6:
21848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   Types shall not be defined in return or parameter types.
21858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
21868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_param_type)
21878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2188601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
21898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
21908cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ConditionContext:
21918cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ 6.4p2:
21928cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // The type-specifier-seq shall not contain typedef and shall not declare
21938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // a new class or enumeration.
21948cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
21958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_condition);
2196601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
21978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
21988cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    }
21998cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  }
22008cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
22018cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return T;
22028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
22038cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2204a08c2fb74ef823c185619ecc532f8fced6a1982fBenjamin Kramerstatic std::string getFunctionQualifiersAsString(const FunctionProtoType *FnTy){
2205d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  std::string Quals =
2206d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
2207d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2208d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (FnTy->getRefQualifier()) {
2209d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_None:
2210d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2211d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2212d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_LValue:
2213d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2214d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2215d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += '&';
2216d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2217d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2218d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_RValue:
2219d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2220d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2221d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += "&&";
2222d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2223d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2224d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2225d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  return Quals;
2226d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2227d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2228d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// Check that the function type T, which has a cv-qualifier or a ref-qualifier,
2229d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// can be contained within the declarator chunk DeclType, and produce an
2230d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// appropriate diagnostic if not.
2231d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smithstatic void checkQualifiedFunction(Sema &S, QualType T,
2232d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                   DeclaratorChunk &DeclType) {
2233d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++98 [dcl.fct]p4 / C++11 [dcl.fct]p6: a function type with a
2234d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // cv-qualifier or a ref-qualifier can only appear at the topmost level
2235d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // of a type.
2236d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  int DiagKind = -1;
2237d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (DeclType.Kind) {
2238d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Paren:
2239d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::MemberPointer:
2240d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases are permitted.
2241d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2242d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Array:
2243d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Function:
2244d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases don't allow function types at all; no need to diagnose the
2245d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // qualifiers separately.
2246d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2247d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::BlockPointer:
2248d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 0;
2249d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2250d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Pointer:
2251d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 1;
2252d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2253d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Reference:
2254d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 2;
2255d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2256d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2257d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2258d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  assert(DiagKind != -1);
2259d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  S.Diag(DeclType.Loc, diag::err_compound_qualified_function_type)
2260d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << DiagKind << isa<FunctionType>(T.IgnoreParens()) << T
2261d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << getFunctionQualifiersAsString(T->castAs<FunctionProtoType>());
2262d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2263d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2264b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// Produce an approprioate diagnostic for an ambiguity between a function
2265b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// declarator and a C++ direct-initializer.
2266b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smithstatic void warnAboutAmbiguousFunction(Sema &S, Declarator &D,
2267b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith                                       DeclaratorChunk &DeclType, QualType RT) {
2268b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2269b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  assert(FTI.isAmbiguous && "no direct-initializer / function ambiguity");
2270b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2271b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // If the return type is void there is no ambiguity.
2272b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isVoidType())
2273b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2274b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2275b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a non-class type can have at most one argument.
2276b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!RT->isRecordType() && FTI.NumArgs > 1)
2277b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2278b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2279b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a reference must have exactly one argument.
2280b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isReferenceType() && FTI.NumArgs != 1)
2281b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2282b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2283b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Only warn if this declarator is declaring a function at block scope, and
2284b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // doesn't have a storage class (such as 'extern') specified.
2285b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!D.isFunctionDeclarator() ||
2286b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      D.getFunctionDefinitionKind() != FDK_Declaration ||
2287b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      !S.CurContext->isFunctionOrMethod() ||
2288b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      D.getDeclSpec().getStorageClassSpecAsWritten()
2289b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        != DeclSpec::SCS_unspecified)
2290b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2291b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2292b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Inside a condition, a direct initializer is not permitted. We allow one to
2293b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // be parsed in order to give better diagnostics in condition parsing.
2294b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (D.getContext() == Declarator::ConditionContext)
2295b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2296b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2297b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  SourceRange ParenRange(DeclType.Loc, DeclType.EndLoc);
2298b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2299d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  S.Diag(DeclType.Loc,
2300d64effc4e31044c05d6e4400150edb26e914983aRichard Smith         FTI.NumArgs ? diag::warn_parens_disambiguated_as_function_declaration
2301d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                     : diag::warn_empty_parens_are_function_decl)
2302d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    << ParenRange;
2303d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2304d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // If the declaration looks like:
2305d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   T var1,
2306d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   f();
2307d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // and name lookup finds a function named 'f', then the ',' was
2308d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // probably intended to be a ';'.
2309d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (!D.isFirstDeclarator() && D.getIdentifier()) {
2310d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Comma(D.getCommaLoc(), S.SourceMgr);
2311d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Name(D.getIdentifierLoc(), S.SourceMgr);
2312d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    if (Comma.getFileID() != Name.getFileID() ||
2313d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) {
2314d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      LookupResult Result(S, D.getIdentifier(), SourceLocation(),
2315d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                          Sema::LookupOrdinaryName);
2316d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      if (S.LookupName(Result, S.getCurScope()))
2317d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        S.Diag(D.getCommaLoc(), diag::note_empty_parens_function_call)
2318d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << FixItHint::CreateReplacement(D.getCommaLoc(), ";")
2319d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << D.getIdentifier();
2320d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    }
2321d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  }
2322b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2323d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (FTI.NumArgs > 0) {
2324d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration with parameters, eg. "T var(T());", suggest adding parens
2325d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // around the first parameter to turn the declaration into a variable
2326d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2327b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceRange Range = FTI.ArgInfo[0].Param->getSourceRange();
2328b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation B = Range.getBegin();
2329b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation E = S.PP.getLocForEndOfToken(Range.getEnd());
2330b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // FIXME: Maybe we should suggest adding braces instead of parens
2331b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // in C++11 for classes that don't have an initializer_list constructor.
2332b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    S.Diag(B, diag::note_additional_parens_for_variable_declaration)
2333b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(B, "(")
2334b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(E, ")");
2335d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  } else {
2336d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration without parameters, eg. "T var();", suggest replacing the
2337d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // parens with an initializer to turn the declaration into a variable
2338d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2339b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
2340d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2341b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // Empty parens mean value-initialization, and no parens mean
2342b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // default initialization. These are equivalent if the default
2343b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // constructor is user-provided or if zero-initialization is a
2344b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // no-op.
2345b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    if (RD && RD->hasDefinition() &&
2346b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        (RD->isEmpty() || RD->hasUserProvidedDefaultConstructor()))
2347b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor)
2348b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        << FixItHint::CreateRemoval(ParenRange);
2349b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    else {
2350b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      std::string Init = S.getFixItZeroInitializerForType(RT);
235180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      if (Init.empty() && S.LangOpts.CPlusPlus11)
2352b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        Init = "{}";
2353b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (!Init.empty())
2354b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        S.Diag(DeclType.Loc, diag::note_empty_parens_zero_initialize)
2355b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith          << FixItHint::CreateReplacement(ParenRange, Init);
2356b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    }
2357b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  }
2358b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith}
2359b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
23608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
23618cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                QualType declSpecType,
23628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                TypeSourceInfo *TInfo) {
23638cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
23648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = declSpecType;
23658cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
23668cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
23678cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ASTContext &Context = S.Context;
23684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &LangOpts = S.getLangOpts();
23698cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2370cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
2371cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
2372cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
2373cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
23741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2375162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  // Does this declaration declare a typedef-name?
2376162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  bool IsTypedefName =
2377162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef ||
23783e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasDeclContext ||
23793e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasTemplateContext;
2380162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2381d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // Does T refer to a function type with a cv-qualifier or a ref-qualifier?
2382d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  bool IsQualifiedFunction = T->isFunctionProtoType() &&
2383d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      (T->castAs<FunctionProtoType>()->getTypeQuals() != 0 ||
2384d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith       T->castAs<FunctionProtoType>()->getRefQualifier() != RQ_None);
2385d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
238698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
238798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
238898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
23898ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2390711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
2391711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
2392711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2393d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction) {
2394d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      checkQualifiedFunction(S, T, DeclType);
2395d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = DeclType.Kind == DeclaratorChunk::Paren;
2396d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    }
23975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
2398075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
23998cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildParenType(T);
2400075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
24015618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
24029af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
24039af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
24048cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_blocks_disable);
24051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24068cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
24072865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
24088cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
24095618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
24105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
24116a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
24126a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
24138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
24148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
24156a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
24166a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
24176a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
24188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.ObjC1 && T->getAs<ObjCObjectType>()) {
2419c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
24202865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
24218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
242214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
242314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
24248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildPointerType(T, DeclType.Loc, Name);
24252865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
24268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
2427711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
24285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
24290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
24306a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
24316a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
24328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
24338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
24346a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
24356a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
24366a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
24378cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
24382865474261a608c7873b87ba4af110d17907896dJohn McCall
24392865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
24402865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
24418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
24425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
24430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
24445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
24456a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
24466a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
24478cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
24488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
24496a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
24506a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
24516a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
2452fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
245394f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
24545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
24555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
24565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
24575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
24585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
24595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
24605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
2461c05a94b7accd4035bf5d5897c434c445b22da855John McCall      if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
2462f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
2463f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
2464f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
24658cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
2466f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
2467f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
2468f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
24697f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
24707f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // C99 6.7.5.2p1: The optional type qualifiers and the keyword static
24717f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // shall appear only in a declaration of a function parameter with an
24727f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // array type, ...
24737f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      if (ASM == ArrayType::Static || ATI.TypeQuals) {
247499570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay        if (!(D.isPrototypeContext() ||
247599570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay              D.getContext() == Declarator::KNRTypeListContext)) {
24767f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          S.Diag(DeclType.Loc, diag::err_array_static_outside_prototype) <<
24777f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
24787f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Remove the 'static' and the type qualifiers.
24797f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          if (ASM == ArrayType::Static)
24807f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ASM = ArrayType::Normal;
24817f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          ATI.TypeQuals = 0;
24827f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          D.setInvalidType(true);
24837f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
24847f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
24857f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // C99 6.7.5.2p1: ... and then only in the outermost array type
24867f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // derivation.
24877f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        unsigned x = chunkIndex;
24887f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        while (x != 0) {
24897f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Walk outwards along the declarator chunks.
24907f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          x--;
24917f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          const DeclaratorChunk &DC = D.getTypeObject(x);
24927f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          switch (DC.Kind) {
24937f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Paren:
24947f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            continue;
24957f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Array:
24967f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Pointer:
24977f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Reference:
24987f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::MemberPointer:
24997f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            S.Diag(DeclType.Loc, diag::err_array_static_not_outermost) <<
25007f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
25017f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            if (ASM == ArrayType::Static)
25027f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              ASM = ArrayType::Normal;
25037f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ATI.TypeQuals = 0;
25047f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            D.setInvalidType(true);
25057f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
25067f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Function:
25077f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::BlockPointer:
25087f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            // These are invalid anyway, so just ignore.
25097f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
25107f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          }
25117f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
25127f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      }
25137f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
25148ac2c66a1442985091c5ec2b33ce6d3df3bcb529Eli Friedman      T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
25158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                           SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
25165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
25175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2518f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
25195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
25205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
25215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
25225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2523d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = FTI.TypeQuals || FTI.hasRefQualifier();
25243cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
252534b41d939a1328f484511c6002ba2456db879a29Richard Smith      // Check for auto functions and trailing return type and adjust the
252634b41d939a1328f484511c6002ba2456db879a29Richard Smith      // return type accordingly.
252734b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!D.isInvalidType()) {
252834b41d939a1328f484511c6002ba2456db879a29Richard Smith        // trailing-return-type is only required if we're declaring a function,
252934b41d939a1328f484511c6002ba2456db879a29Richard Smith        // and not, for instance, a pointer to a function.
253034b41d939a1328f484511c6002ba2456db879a29Richard Smith        if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
253154655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            !FTI.hasTrailingReturnType() && chunkIndex == 0) {
25328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
253334b41d939a1328f484511c6002ba2456db879a29Richard Smith               diag::err_auto_missing_trailing_return);
253434b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = Context.IntTy;
253534b41d939a1328f484511c6002ba2456db879a29Richard Smith          D.setInvalidType(true);
253654655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        } else if (FTI.hasTrailingReturnType()) {
2537e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          // T must be exactly 'auto' at this point. See CWG issue 681.
2538e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (isa<ParenType>(T)) {
25398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
2540e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith                 diag::err_trailing_return_in_parens)
2541e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith              << T << D.getDeclSpec().getSourceRange();
2542e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.setInvalidType(true);
2543f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman          } else if (D.getContext() != Declarator::LambdaExprContext &&
2544f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman                     (T.hasQualifiers() || !isa<AutoType>(T))) {
25458cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
254634b41d939a1328f484511c6002ba2456db879a29Richard Smith                 diag::err_trailing_return_without_auto)
254734b41d939a1328f484511c6002ba2456db879a29Richard Smith              << T << D.getDeclSpec().getSourceRange();
254834b41d939a1328f484511c6002ba2456db879a29Richard Smith            D.setInvalidType(true);
254934b41d939a1328f484511c6002ba2456db879a29Richard Smith          }
255054655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          T = S.GetTypeFromParser(FTI.getTrailingReturnType(), &TInfo);
255154655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (T.isNull()) {
255254655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            // An error occurred parsing the trailing return type.
255354655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            T = Context.IntTy;
255454655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            D.setInvalidType(true);
255554655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          }
255634b41d939a1328f484511c6002ba2456db879a29Richard Smith        }
255734b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
255834b41d939a1328f484511c6002ba2456db879a29Richard Smith
2559e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // C99 6.7.5.3p1: The return type may not be a function or array type.
2560e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // For conversion functions, we'll diagnose this particular error later.
2561e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      if ((T->isArrayType() || T->isFunctionType()) &&
2562e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
2563e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned diagID = diag::err_func_returning_array_function;
2564e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // Last processing chunk in block context means this function chunk
2565e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // represents the block.
2566e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (chunkIndex == 0 &&
2567e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.getContext() == Declarator::BlockLiteralContext)
2568e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          diagID = diag::err_block_returning_array_function;
25698cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
2570e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        T = Context.IntTy;
2571e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        D.setInvalidType(true);
2572e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2573e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2574aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Do not allow returning half FP value.
2575aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // FIXME: This really should be in BuildFunctionType.
2576aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      if (T->isHalfType()) {
257719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        if (S.getLangOpts().OpenCL) {
257819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          if (!S.getOpenCLOptions().cl_khr_fp16) {
257919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            S.Diag(D.getIdentifierLoc(), diag::err_opencl_half_return) << T;
258019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            D.setInvalidType(true);
258119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          }
258219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        } else {
258319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          S.Diag(D.getIdentifierLoc(),
258419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            diag::err_parameters_retval_cannot_have_fp16_type) << 1;
258519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          D.setInvalidType(true);
258619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        }
2587aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      }
2588aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
25895291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
25905291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
25918c952cd40ccec9d720931f27e7d722fed207d536Richard Smith      if ((T.getCVRQualifiers() || T->isAtomicType()) &&
25928c952cd40ccec9d720931f27e7d722fed207d536Richard Smith          !(S.getLangOpts().CPlusPlus &&
25938c952cd40ccec9d720931f27e7d722fed207d536Richard Smith            (T->isDependentType() || T->isRecordType())))
25948c952cd40ccec9d720931f27e7d722fed207d536Richard Smith        diagnoseIgnoredFunctionQualifiers(S, T, D, chunkIndex);
2595d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
259602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // Objective-C ARC ownership qualifiers are ignored on the function
259702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // return type (by type canonicalization). Complain if this attribute
259802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // was written here.
259902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      if (T.getQualifiers().hasObjCLifetime()) {
260002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        SourceLocation AttrLoc;
260102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (chunkIndex + 1 < D.getNumTypeObjects()) {
260202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
260302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr = ReturnTypeChunk.getAttrs();
260402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
260502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
260602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
260702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
260802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
260902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
261002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
261102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isInvalid()) {
261202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr
261302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor                 = D.getDeclSpec().getAttributes().getList();
261402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
261502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
261602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
261702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
261802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
261902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
262002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
262102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
262202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isValid()) {
262302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // The ownership attributes are almost always written via
262402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // the predefined
262502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // __strong/__weak/__autoreleasing/__unsafe_unretained.
262602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          if (AttrLoc.isMacroID())
262702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            AttrLoc = S.SourceMgr.getImmediateExpansionRange(AttrLoc).first;
262802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
262902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          S.Diag(AttrLoc, diag::warn_arc_lifetime_result_type)
263002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            << T.getQualifiers().getObjCLifetime();
263102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
263202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      }
263302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
26348cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
2635402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
2636402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
2637b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
26385e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall        if (Tag->isCompleteDefinition())
26398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
2640402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
2641402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
2642402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
26433cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
26443cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
2645162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (IsTypedefName && FTI.getExceptionSpecType())
26468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef)
26473e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          << (D.getContext() == Declarator::AliasDeclContext ||
26483e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith              D.getContext() == Declarator::AliasTemplateContext);
26493cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
2650b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // If we see "T var();" or "T var(T());" at block scope, it is probably
2651b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // an attempt to initialize a variable, not a function declaration.
2652b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (FTI.isAmbiguous)
2653b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        warnAboutAmbiguousFunction(S, D, DeclType, T);
2654b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
26558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (!FTI.NumArgs && !FTI.isVariadic && !LangOpts.CPlusPlus) {
26562865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
26572865474261a608c7873b87ba4af110d17907896dJohn McCall        T = Context.getFunctionNoProtoType(T);
26582865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
26592865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
26602865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
26612865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
26628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        if (!FTI.NumArgs && FTI.isVariadic && !LangOpts.CPlusPlus) {
2663965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
2664965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
2665965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
26668e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt            if (Attrs->getKind() == AttributeList::AT_Overloadable) {
2667965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
2668965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
2669965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
2670965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
2671965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
2672965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
26738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
2674c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
26752865474261a608c7873b87ba4af110d17907896dJohn McCall
26762865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
2677788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
2678788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
26798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
26802865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
2681d5668a2447c2afeea38815b51a80a5a6ac235599Argyrios Kyrtzidis          // Recover by creating a K&R-style function type.
2682d5668a2447c2afeea38815b51a80a5a6ac235599Argyrios Kyrtzidis          T = Context.getFunctionNoProtoType(T);
26832865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
26842865474261a608c7873b87ba4af110d17907896dJohn McCall        }
26852865474261a608c7873b87ba4af110d17907896dJohn McCall
2686e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
2687e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
268854655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        EPI.HasTrailingReturn = FTI.hasTrailingReturnType();
2689e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
2690c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
2691c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : FTI.RefQualifierIsLValueRef? RQ_LValue
2692c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : RQ_RValue;
269391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
26945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
26955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
26965f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 16> ArgTys;
26972865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
26981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<bool, 16> ConsumedArguments;
2700f85e193739c953358c865005855253af4f68a497John McCall        ConsumedArguments.reserve(FTI.NumArgs);
2701f85e193739c953358c865005855253af4f68a497John McCall        bool HasAnyConsumedArguments = false;
2702f85e193739c953358c865005855253af4f68a497John McCall
27035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
2704d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
27058123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
270678c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
27072dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
27082dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
270991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier          assert((ArgTy == Context.getAdjustedParameterType(ArgTy)) &&
271079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor                 "Unadjusted type?");
27112dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
27125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
27135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
271472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
27152dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
27165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
27175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
27185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
27195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
27208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(DeclType.Loc, diag::err_void_only_param);
27212ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
27228123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
27232ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
27242ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
27258cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(FTI.ArgInfo[i].IdentLoc,
27264565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
27272ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
27288123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
27292ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
27302ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
27310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
27328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                S.Diag(DeclType.Loc, diag::err_void_param_qualified);
27331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27342ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
27352ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
27362ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
2737aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov          } else if (ArgTy->isHalfType()) {
2738aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // Disallow half FP arguments.
2739aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // FIXME: This really should be in BuildFunctionType.
274019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            if (S.getLangOpts().OpenCL) {
274119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              if (!S.getOpenCLOptions().cl_khr_fp16) {
274219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                S.Diag(Param->getLocation(),
274319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                  diag::err_opencl_half_argument) << ArgTy;
274419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                D.setInvalidType();
27459dd74c5504c743c96ea3a1d691d6a75ec3a98147John McCall                Param->setInvalidDecl();
274619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              }
274719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            } else {
274819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              S.Diag(Param->getLocation(),
274919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                diag::err_parameters_retval_cannot_have_fp16_type) << 0;
275019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              D.setInvalidType();
275119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            }
2752eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
2753eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
2754a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
2755eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              Param->setKNRPromoted(true);
2756183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
2757eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              if (BTy->getKind() == BuiltinType::Float) {
2758eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
2759eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall                Param->setKNRPromoted(true);
2760eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              }
2761eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
27625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
276356a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
27648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          if (LangOpts.ObjCAutoRefCount) {
2765f85e193739c953358c865005855253af4f68a497John McCall            bool Consumed = Param->hasAttr<NSConsumedAttr>();
2766f85e193739c953358c865005855253af4f68a497John McCall            ConsumedArguments.push_back(Consumed);
2767f85e193739c953358c865005855253af4f68a497John McCall            HasAnyConsumedArguments |= Consumed;
2768f85e193739c953358c865005855253af4f68a497John McCall          }
2769f85e193739c953358c865005855253af4f68a497John McCall
277054e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
27715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2772465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2773f85e193739c953358c865005855253af4f68a497John McCall        if (HasAnyConsumedArguments)
2774f85e193739c953358c865005855253af4f68a497John McCall          EPI.ConsumedArguments = ConsumedArguments.data();
2775f85e193739c953358c865005855253af4f68a497John McCall
27765f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 4> Exceptions;
277774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<ParsedType, 2> DynamicExceptions;
277874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<SourceRange, 2> DynamicExceptionRanges;
277974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        Expr *NoexceptExpr = 0;
278091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
27818b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        if (FTI.getExceptionSpecType() == EST_Dynamic) {
278274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // FIXME: It's rather inefficient to have to split into two vectors
278374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // here.
278474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          unsigned N = FTI.NumExceptions;
278574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptions.reserve(N);
278674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptionRanges.reserve(N);
278774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          for (unsigned I = 0; I != N; ++I) {
278874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptions.push_back(FTI.Exceptions[I].Ty);
278974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptionRanges.push_back(FTI.Exceptions[I].Range);
2790e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
27918b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
279274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          NoexceptExpr = FTI.NoexceptExpr;
279374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        }
279491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
279574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        S.checkExceptionSpecification(FTI.getExceptionSpecType(),
279674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptions,
279774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptionRanges,
279874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      NoexceptExpr,
279974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      Exceptions,
280074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      EPI);
280191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2802bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose        T = Context.getFunctionType(T, ArgTys, EPI);
28035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
280404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
28055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
28065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2807f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
2808f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
28097bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
2810f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
28117bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
2812edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
2813edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
28148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      } else if (S.isDependentScopeSpecifier(SS) ||
28158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                 dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) {
28161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
28177bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
281887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
281987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
282087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
28217bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
28224a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
282387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
282487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
282587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
282614aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor        case NestedNameSpecifier::NamespaceAlias:
282787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
28289f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
28297bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
283087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
283187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
283287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
283391ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Note: if the NNS has a prefix and ClsType is a nondependent
283491ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // TemplateSpecializationType, then the NNS prefix is NOT included
283591ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // in ClsType; hence we wrap ClsType into an ElaboratedType.
283691ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // NOTE: in particular, no wrap occurs if ClsType already is an
283791ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Elaborated, DependentName, or DependentTemplateSpecialization.
283891ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
28397bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
284087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
284187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
2842f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
28438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Mem.Scope().getBeginLoc(),
2844949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
2845949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
2846949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
2847f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
2848f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2849f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2850949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
28518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
2852949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
2853f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
2854949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
28552865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
28568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
2857f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2858f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
2859f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
2860f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2861cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
2862cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
2863cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
2864cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
2865cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
2866c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
2867711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
2868f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclChunk, attrs);
28695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2870971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
28718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (LangOpts.CPlusPlus && T->isFunctionType()) {
2872183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
2873778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
2874971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
287591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // C++ 8.3.5p4:
2876708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
2877708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
2878708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
2879708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
2880683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    //
2881683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // Core issue 547 also allows cv-qualifiers on function types that are
2882683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // top-level template type arguments.
2883613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
2884613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
2885906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman      FreeFunction = ((D.getContext() != Declarator::MemberContext &&
2886906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman                       D.getContext() != Declarator::LambdaExprContext) ||
2887613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall                      D.getDeclSpec().isFriendSpecified());
2888613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
28898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec());
2890613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
2891613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
2892613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
2893d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.fct]p6 (w/DR1417):
2894d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // An attempt to specify a function type with a cv-qualifier-seq or a
2895d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // ref-qualifier (including by typedef-name) is ill-formed unless it is:
2896d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type for a non-static member function,
2897d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type to which a pointer to member refers,
2898d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the top-level function type of a function typedef declaration or
2899d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //    alias-declaration,
2900d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id in the default argument of a type-parameter, or
2901d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id of a template-argument for a type-parameter
2902d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction &&
2903d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !(!FreeFunction &&
2904d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) &&
2905d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !IsTypedefName &&
2906d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        D.getContext() != Declarator::TemplateTypeArgContext) {
290796a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      SourceLocation Loc = D.getLocStart();
2908d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      SourceRange RemovalRange;
2909d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      unsigned I;
2910d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      if (D.isFunctionDeclarator(I)) {
2911d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        SmallVector<SourceLocation, 4> RemovalLocs;
2912d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        const DeclaratorChunk &Chunk = D.getTypeObject(I);
2913d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        assert(Chunk.Kind == DeclaratorChunk::Function);
2914d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.hasRefQualifier())
2915d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getRefQualifierLoc());
2916d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Const)
2917d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getConstQualifierLoc());
2918d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Volatile)
2919d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getVolatileQualifierLoc());
2920d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        // FIXME: We do not track the location of the __restrict qualifier.
2921d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //if (Chunk.Fun.TypeQuals & Qualifiers::Restrict)
2922d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //  RemovalLocs.push_back(Chunk.Fun.getRestrictQualifierLoc());
2923d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (!RemovalLocs.empty()) {
2924d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          std::sort(RemovalLocs.begin(), RemovalLocs.end(),
2925aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                    BeforeThanCompare<SourceLocation>(S.getSourceManager()));
2926d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalRange = SourceRange(RemovalLocs.front(), RemovalLocs.back());
2927d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          Loc = RemovalLocs.front();
2928683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
2929683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      }
2930d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2931d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      S.Diag(Loc, diag::err_invalid_qualified_function_type)
2932d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FreeFunction << D.isFunctionDeclarator() << T
2933d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << getFunctionQualifiersAsString(FnTy)
2934d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FixItHint::CreateRemoval(RemovalRange);
2935d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2936d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // Strip the cv-qualifiers and ref-qualifiers from the type.
2937d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
2938d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.TypeQuals = 0;
2939d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.RefQualifier = RQ_None;
2940d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
294191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      T = Context.getFunctionType(FnTy->getResultType(),
2942bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                  ArrayRef<QualType>(FnTy->arg_type_begin(),
2943bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                                     FnTy->getNumArgs()),
2944bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                  EPI);
2945e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      // Rebuild any parens around the identifier in the function type.
2946e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2947e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        if (D.getTypeObject(i).Kind != DeclaratorChunk::Paren)
2948e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith          break;
2949e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        T = S.BuildParenType(T);
2950e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      }
2951971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
2952971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
29531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2954711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
2955711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull())
2956711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = D.getAttributes())
2957f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclName, attrs);
2958711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2959711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
2960711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
2961711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2962148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  // C++0x [dcl.constexpr]p9:
2963148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  //  A constexpr specifier used in an object declaration declares the object
296491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //  as const.
2965148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
2966737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
2967737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
2968737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
296991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // If there was an ellipsis in the declarator, the declaration declares a
2970a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
2971a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (D.hasEllipsis() && !T.isNull()) {
2972a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
297391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   A declarator-id or abstract-declarator containing an ellipsis shall
2974a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
2975a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
2976a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
2977a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
2978a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
297991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   [...] When it is part of a parameter-declaration-clause, the
298091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   parameter pack is a function parameter pack (14.5.3). The type T
2981a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
298291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   a template parameter pack; each template parameter pack in T is
2983a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
2984a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2985a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
2986a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
2987a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
298891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(D.getEllipsisLoc(),
2989a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
2990a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
2991a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
2992a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
299366874fb18afbffb8b2ca05576851a64534be3352David Blaikie        T = Context.getPackExpansionType(T, None);
2994a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
2995a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
299691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2997a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
2998a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
299991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   If a template-parameter is a [...] is a parameter-declaration that
3000a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
3001a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
3002a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
3003a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
3004a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
3005a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
3006a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
300766874fb18afbffb8b2ca05576851a64534be3352David Blaikie        T = Context.getPackExpansionType(T, None);
3008e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith      else
3009e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith        S.Diag(D.getEllipsisLoc(),
301080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith               LangOpts.CPlusPlus11
3011e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 ? diag::warn_cxx98_compat_variadic_templates
3012e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 : diag::ext_variadic_templates);
3013a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
301491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3015a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
3016a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
3017cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:  // FIXME: special diagnostic here?
3018cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:     // FIXME: special diagnostic here?
3019a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
30200b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
3021162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
30223e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
3023a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
3024a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
3025a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
3026a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
3027a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
302817b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
3029a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
3030f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
30317796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
3032683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    case Declarator::TemplateTypeArgContext:
3033a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
3034a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
30358cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      S.Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
3036a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
3037a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
3038a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
3039a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
3040e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
3041bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
3042bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
3043bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
3044bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
3045db7abf78dedc2ef6ccb42b3dac6ab330fe2ea469Argyrios Kyrtzidis
30468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return S.GetTypeSourceInfoForDeclarator(D, T, TInfo);
30478cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
30488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
30498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// GetTypeForDeclarator - Convert the type for the specified
30508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// declarator to Type instances.
30518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis///
30528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// The result of this call will never be null, but the associated
30538cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// type may be a null type if there's an unrecoverable error.
30548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
30558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // Determine the type of the declarator. Not all forms of declarator
30568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // have a type.
30578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
30588cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeProcessingState state(*this, D);
30598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
30608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
30618cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
30628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (T.isNull())
30638cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
30648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
30654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (D.isPrototypeContext() && getLangOpts().ObjCAutoRefCount)
30668cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    inferARCWriteback(state, T);
306791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
30688cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return GetFullTypeForDeclarator(state, T, ReturnTypeInfo);
30695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
30705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
307131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclSpec(Sema &S,
307231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           QualType &declSpecTy,
307331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           Qualifiers::ObjCLifetime ownership) {
307431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy->isObjCRetainableType() &&
307531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) {
307631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers qs;
307731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    qs.addObjCLifetime(ownership);
307831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    declSpecTy = S.Context.getQualifiedType(declSpecTy, qs);
307931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
308031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
308131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
308231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
308331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
308431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            unsigned chunkIndex) {
308531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
308631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
308731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
308831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Look for an explicit lifetime attribute.
308931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclaratorChunk &chunk = D.getTypeObject(chunkIndex);
309031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (const AttributeList *attr = chunk.getAttrs(); attr;
309131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis         attr = attr->getNext())
30928e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    if (attr->getKind() == AttributeList::AT_ObjCOwnership)
309331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
309431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
309531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  const char *attrStr = 0;
309631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  switch (ownership) {
30973026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
309831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break;
309931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Strong: attrStr = "strong"; break;
310031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Weak: attrStr = "weak"; break;
310131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break;
310231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
310331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
310431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // If there wasn't one, add one (with an invalid source location
310531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // so that we don't make an AttributedType for it).
310631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  AttributeList *attr = D.getAttributePool()
310731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    .create(&S.Context.Idents.get("objc_ownership"), SourceLocation(),
310831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            /*scope*/ 0, SourceLocation(),
310931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            &S.Context.Idents.get(attrStr), SourceLocation(),
311093f95f2a2cbb6bb3d17bfb5fc74ce1cccea751b6Sean Hunt            /*args*/ 0, 0, AttributeList::AS_GNU);
311131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  spliceAttrIntoList(*attr, chunk.getAttrListRef());
311231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
311331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // TODO: mark whether we did this inference?
311431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
311531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
311648d798ce32447607144db70a484cdb99c1180663Benjamin Kramer/// \brief Used for transferring ownership in casts resulting in l-values.
311731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnership(TypeProcessingState &state,
311831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 QualType &declSpecTy,
311931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 Qualifiers::ObjCLifetime ownership) {
312031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
312131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
312231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
312331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  int inner = -1;
31246ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis  bool hasIndirection = false;
312531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
312631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    DeclaratorChunk &chunk = D.getTypeObject(i);
312731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    switch (chunk.Kind) {
312831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Paren:
312931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      // Ignore parens.
313031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
313131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
313231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Array:
313331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Reference:
313431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Pointer:
31356ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
31366ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        hasIndirection = true;
313731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      inner = i;
313831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
313931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
314031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::BlockPointer:
31416ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
31426ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        transferARCOwnershipToDeclaratorChunk(state, ownership, i);
31436ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      return;
314431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
314531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Function:
314631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::MemberPointer:
314731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
314831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    }
314931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
315031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
315131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (inner == -1)
31526ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    return;
315331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
315491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  DeclaratorChunk &chunk = D.getTypeObject(inner);
315531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (chunk.Kind == DeclaratorChunk::Pointer) {
315631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (declSpecTy->isObjCRetainableType())
315731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
31586ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    if (declSpecTy->isObjCObjectType() && hasIndirection)
315931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclaratorChunk(state, ownership, inner);
316031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  } else {
316131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    assert(chunk.Kind == DeclaratorChunk::Array ||
316231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis           chunk.Kind == DeclaratorChunk::Reference);
316331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
316431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
316531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
316631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
316731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) {
316831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeProcessingState state(*this, D);
316931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
317031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
317131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
317231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy.isNull())
317331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
317431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
31754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount) {
317631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy);
317731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (ownership != Qualifiers::OCL_None)
317831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      transferARCOwnership(state, declSpecTy, ownership);
317931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
318031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
318131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo);
318231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
318331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
318414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall/// Map an AttributedType::Kind to an AttributeList::Kind.
318514aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
318614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  switch (kind) {
318714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_address_space:
31888e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_AddressSpace;
318914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_regparm:
31908e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Regparm;
319114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_vector_size:
31928e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_VectorSize;
319314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_vector_type:
31948e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonVectorType;
319514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_polyvector_type:
31968e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonPolyVectorType;
319714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_objc_gc:
31988e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCGC;
3199b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  case AttributedType::attr_objc_ownership:
32008e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCOwnership;
320114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_noreturn:
32028e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NoReturn;
320314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_cdecl:
32048e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_CDecl;
320514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_fastcall:
32068e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_FastCall;
320714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_stdcall:
32088e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_StdCall;
320914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_thiscall:
32108e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ThisCall;
321114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_pascal:
32128e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pascal;
3213414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case AttributedType::attr_pcs:
32148e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pcs;
3215263366f9241366f29ba65b703120f302490c39ffDerek Schuff  case AttributedType::attr_pnaclcall:
3216263366f9241366f29ba65b703120f302490c39ffDerek Schuff    return AttributeList::AT_PnaclCall;
321738980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei  case AttributedType::attr_inteloclbicc:
321838980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    return AttributeList::AT_IntelOclBicc;
321914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
322014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  llvm_unreachable("unexpected attribute kind!");
322114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
322214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
322314aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic void fillAttributedTypeLoc(AttributedTypeLoc TL,
322414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                  const AttributeList *attrs) {
322514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributedType::Kind kind = TL.getAttrKind();
322614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
322714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  assert(attrs && "no type attributes in the expected location!");
322814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributeList::Kind parsedKind = getAttrListKind(kind);
322914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  while (attrs->getKind() != parsedKind) {
323014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    attrs = attrs->getNext();
323114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    assert(attrs && "no matching attribute in expected location!");
323214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
323314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
323414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  TL.setAttrNameLoc(attrs->getLoc());
323514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrExprOperand())
323614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrExprOperand(attrs->getArg(0));
323714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  else if (TL.hasAttrEnumOperand())
323814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrEnumOperandLoc(attrs->getParameterLoc());
323914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
324014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // FIXME: preserve this information to here.
324114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrOperand())
324214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrOperandParensRange(SourceRange());
324314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
324414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
324551bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
324651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
3247c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    ASTContext &Context;
324851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
3249f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
325051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
325191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
3252c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      : Context(Context), DS(DS) {}
3253f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
325414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
325514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, DS.getAttributes().getList());
325614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      Visit(TL.getModifiedLoc());
325714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
325851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
325951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
326051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
326151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
326251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
326351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
326451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
326551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
32661de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // FIXME. We should have DS.getTypeSpecTypeEndLoc(). But, it requires
32671de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // addition field. What we have is good enough for dispay of location
32681de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // of 'fixit' on interface name.
32691de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      TL.setNameEndLoc(DS.getLocEnd());
3270c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
3271c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3272c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
3273c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
3274c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
3275c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.getBaseLoc().initialize(Context, SourceLocation());
3276c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
3277c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
3278c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
3279c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
328054e14c4db764c0636160d26c5bbf491637c83a76John McCall
3281c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
328254e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
328354e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
328454e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
328554e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
328654e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
328754e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
328854e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
328954e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
329054e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
329154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
329254e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
329354e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
329451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
329554e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
329654e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
3297c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
329851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
3299833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
3300a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
3301b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3302833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3303833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
3304833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
3305a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
33060daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara        TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
3307833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
3308833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
3309833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3310e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
3311e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
331239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        ElaboratedTypeLoc ElabTL = OldTL.castAs<ElaboratedTypeLoc>();
331339e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        TemplateSpecializationTypeLoc NamedTL = ElabTL.getNamedTypeLoc()
331439e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            .castAs<TemplateSpecializationTypeLoc>();
3315e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
3316e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
3317e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      else
331839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        TL.copy(OldTL.castAs<TemplateSpecializationTypeLoc>());
3319833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
3320cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3321cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
3322cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3323cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3324cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3325cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3326cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
3327cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3328cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3329b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
3330cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
3331b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3332cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
3333cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3334ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3335ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      // FIXME: This holds only because we only have one unary transform.
3336ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType);
3337ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setKWLoc(DS.getTypeSpecTypeLoc());
3338ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setParensRange(DS.getTypeofParensRange());
3339ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getRepAsType());
3340ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TypeSourceInfo *TInfo = 0;
3341ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3342ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setUnderlyingTInfo(TInfo);
3343ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
3344ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3345ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
3346ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
3347ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
3348ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
3349ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
3350ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
3351ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
3352ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
3353ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
3354ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
3355ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
3356ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
3357ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
3358ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
3359e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3360e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
3361e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
3362253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
3363e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
3364b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3365e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
336639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          TL.copy(TInfo->getTypeLoc().castAs<ElaboratedTypeLoc>());
3367e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
3368e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
3369e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
337038a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara      TL.setElaboratedKeywordLoc(Keyword != ETK_None
337138a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 ? DS.getTypeSpecTypeLoc()
337238a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 : SourceLocation());
3373e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
33749e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
3375e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
3376e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3377e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
337866581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
337966581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
338066581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
338166581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
338239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      TL.copy(TInfo->getTypeLoc().castAs<DependentNameTypeLoc>());
338333500955d731c73717af52088b7fc0e7a85681e7John McCall    }
338433500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
338533500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
338666581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
338766581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
338866581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
338966581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
339039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      TL.copy(
339139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          TInfo->getTypeLoc().castAs<DependentTemplateSpecializationTypeLoc>());
33920daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    }
33930daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    void VisitTagTypeLoc(TagTypeLoc TL) {
33940daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
3395e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3396b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
33974cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // An AtomicTypeLoc can come from either an _Atomic(...) type specifier
33984cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      // or an _Atomic qualifier.
33994cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      if (DS.getTypeSpecType() == DeclSpec::TST_atomic) {
34004cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setKWLoc(DS.getTypeSpecTypeLoc());
34014cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setParensRange(DS.getTypeofParensRange());
340291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
34034cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TypeSourceInfo *TInfo = 0;
34044cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
34054cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        assert(TInfo);
34064cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
34074cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      } else {
34084cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setKWLoc(DS.getAtomicSpecLoc());
34094cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        // No parens, to indicate this was spelled as an _Atomic qualifier.
34104cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        TL.setParensRange(SourceRange());
34114cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith        Visit(TL.getValueLoc());
34124cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      }
3413b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
3414e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
341551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
341651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
3417c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initialize(Context, DS.getTypeSpecTypeLoc());
341851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
341951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
3420eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
342151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
3422b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    ASTContext &Context;
342351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
3424f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
342551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
3426b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
3427b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      : Context(Context), Chunk(Chunk) {}
34284adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
342951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
34309f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
343151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
34324adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3433f85e193739c953358c865005855253af4f68a497John McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3434f85e193739c953358c865005855253af4f68a497John McCall      fillAttributedTypeLoc(TL, Chunk.getAttrs());
3435f85e193739c953358c865005855253af4f68a497John McCall    }
343651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
343751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
343851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
34394adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
344051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
344151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
344251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
34434adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
344451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
344551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
344651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
34474adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
344851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
344951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
3450b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const CXXScopeSpec& SS = Chunk.Mem.Scope();
3451b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
3452b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3453b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const Type* ClsTy = TL.getClass();
3454b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      QualType ClsQT = QualType(ClsTy, 0);
3455b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
3456b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Now copy source location info into the type loc component.
3457b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeLoc ClsTL = ClsTInfo->getTypeLoc();
3458b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
3459b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Identifier:
3460b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
3461b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        {
346239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          DependentNameTypeLoc DNTLoc = ClsTL.castAs<DependentNameTypeLoc>();
346338a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          DNTLoc.setElaboratedKeywordLoc(SourceLocation());
3464b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
3465b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
3466b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3467b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3468b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3469b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpec:
3470b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpecWithTemplate:
3471b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        if (isa<ElaboratedType>(ClsTy)) {
347239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          ElaboratedTypeLoc ETLoc = ClsTL.castAs<ElaboratedTypeLoc>();
347338a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          ETLoc.setElaboratedKeywordLoc(SourceLocation());
3474b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ETLoc.setQualifierLoc(NNSLoc.getPrefix());
3475b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
3476b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
3477b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        } else {
3478b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
3479b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3480b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3481b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3482b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Namespace:
3483b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::NamespaceAlias:
3484b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Global:
3485b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        llvm_unreachable("Nested-name-specifier must name a type");
3486b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      }
3487b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3488b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Finally fill in MemberPointerLocInfo fields.
348951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
3490b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TL.setClassTInfo(ClsTInfo);
34914adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
349251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
349351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
349454e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
349554e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
349651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
349751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
349851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
349951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
350051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
350151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
350251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
350351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
350451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
350551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
350651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
350751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
350851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
350951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
351051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
3511796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeBegin(Chunk.Loc);
3512796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeEnd(Chunk.EndLoc);
351351bd803fbdade51d674598ed45da3d54190a656cJohn McCall
351451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
351559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setLParenLoc(FTI.getLParenLoc());
351659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setRParenLoc(FTI.getRParenLoc());
351754e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
3518d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
351954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
35204adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
352151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
35224adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
3523075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
3524075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
3525075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
3526075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
3527075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
35281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
352951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
35309f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
35314adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
353251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
353351bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
35344adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
35354cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smithstatic void fillAtomicQualLoc(AtomicTypeLoc ATL, const DeclaratorChunk &Chunk) {
35364cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  SourceLocation Loc;
35374cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  switch (Chunk.Kind) {
35384cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Function:
35394cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Array:
35404cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Paren:
35414cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    llvm_unreachable("cannot be _Atomic qualified");
35424cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
35434cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Pointer:
35444cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    Loc = SourceLocation::getFromRawEncoding(Chunk.Ptr.AtomicQualLoc);
35454cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    break;
35464cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
35474cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::BlockPointer:
35484cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::Reference:
35494cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  case DeclaratorChunk::MemberPointer:
35504cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // FIXME: Provide a source location for the _Atomic keyword.
35514cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    break;
35524cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  }
35534cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
35544cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  ATL.setKWLoc(Loc);
35554cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith  ATL.setParensRange(SourceRange());
35564cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith}
35574cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
3558a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
355951bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
356051bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
356105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
356205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
356305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
356405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
356505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
3566a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
356705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
356805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
3569a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
3570a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
357151bd803fbdade51d674598ed45da3d54190a656cJohn McCall
3572a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
3573a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
357439e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    CurrTL.castAs<PackExpansionTypeLoc>().setEllipsisLoc(D.getEllipsisLoc());
357591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
3576a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
357791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
35788ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
35794cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // An AtomicTypeLoc might be produced by an atomic qualifier in this
35804cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    // declarator chunk.
35814cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    if (AtomicTypeLoc ATL = CurrTL.getAs<AtomicTypeLoc>()) {
35824cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      fillAtomicQualLoc(ATL, D.getTypeObject(i));
35834cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith      CurrTL = ATL.getValueLoc().getUnqualifiedLoc();
35844cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith    }
35854cf4a5e96ab0babd13774b17112e7c1d83042ea7Richard Smith
358639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    while (AttributedTypeLoc TL = CurrTL.getAs<AttributedTypeLoc>()) {
358714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs());
358814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
358914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
359014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3591b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
359251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
35934adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
359491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3595b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
3596b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
3597b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
359805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
359905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
360005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
3601b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
3602c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
360305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
360491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3605a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
36064adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
36074adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3608a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
3609b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
36101bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
36111bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
36121bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
361391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
3614eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
3615a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
36161bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
36171bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
3618b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
36191bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
36201bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
36211bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
36221bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
3623b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*"
362435d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
362535d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
36261bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
36271bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
3628f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
36295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
36305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
36315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
36321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3633d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
3634bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
36355153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
3636809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
36375912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
3638e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall  // Make sure there are no unused decl attributes on the declarator.
3639cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // We don't want to do this for ObjC parameters because we're going
3640cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // to apply them to the actual parameter declaration.
36416b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  // Likewise, we don't want to do this for alias declarations, because
36426b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  // we are actually going to build a declaration from this eventually.
36436b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith  if (D.getContext() != Declarator::ObjCParameterContext &&
36446b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith      D.getContext() != Declarator::AliasDeclContext &&
36456b3d3e54c003b03f16e235ad2ff49e95587bbf92Richard Smith      D.getContext() != Declarator::AliasTemplateContext)
3646cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    checkUnusedDeclAttributes(D);
3647e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
36484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
3649402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
36506d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
3651402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
3652402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
3653b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
36545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
36555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3656e97179c675b341927807c718be215c8d1aab8acbDouglas GregorParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) {
3657e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  QualType T = Context.getObjCInstanceType();
3658e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc);
3659e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return CreateParsedType(T, TInfo);
3660e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
3661e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3662e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3663c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3664c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
3665c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3666232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3667232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
3668c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
3669c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
36701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
3671c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
36720953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
3673232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
367429e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified by
367529e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // qualifiers for two or more different address spaces."
3676232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
3677c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
3678e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3679c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3680232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
36811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3682020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be
3683020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // qualified by an address-space qualifier."
3684020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  if (Type->isFunctionType()) {
3685020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type);
3686020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    Attr.setInvalid();
3687020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    return;
3688020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  }
3689020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne
3690232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
3691545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
3692f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3693e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3694c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3695232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3696545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
3697232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
3698ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
3699ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
3700dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
3701dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
3702e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3703c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3704232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3705232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3706efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
3707efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
3708efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
3709efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
3710efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
3711e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
3712efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
3713efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
3714efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
3715efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3716efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
37170953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
3718efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
3719efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
37200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
3721e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3722efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
3723efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3724efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
37251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
3726f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
3727c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
3728c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
3729d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// Does this type have a "direct" ownership qualifier?  That is,
3730d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// is it written like "__strong id", as opposed to something like
3731d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// "typeof(foo)", where that happens to be strong?
3732d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCallstatic bool hasDirectOwnershipQualifier(QualType type) {
3733d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Fast path: no qualifier at all.
3734d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  assert(type.getQualifiers().hasObjCLifetime());
3735d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3736d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  while (true) {
3737d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // __strong id
3738d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (const AttributedType *attr = dyn_cast<AttributedType>(type)) {
3739d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      if (attr->getAttrKind() == AttributedType::attr_objc_ownership)
3740d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        return true;
3741d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3742d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = attr->getModifiedType();
3743d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3744d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // X *__strong (...)
3745d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else if (const ParenType *paren = dyn_cast<ParenType>(type)) {
3746d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = paren->getInnerType();
374791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3748d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // That's it for things we want to complain about.  In particular,
3749d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // we do not want to look through typedefs, typeof(expr),
3750d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // typeof(type), or any other way that the type is somehow
3751d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // abstracted.
3752d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else {
375391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3754d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return false;
3755d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3756d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
3757d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall}
3758d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3759b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis/// handleObjCOwnershipTypeAttr - Process an objc_ownership
3760f85e193739c953358c865005855253af4f68a497John McCall/// attribute on the specified type.
3761f85e193739c953358c865005855253af4f68a497John McCall///
3762f85e193739c953358c865005855253af4f68a497John McCall/// Returns 'true' if the attribute was handled.
3763b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
3764f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr,
3765f85e193739c953358c865005855253af4f68a497John McCall                                       QualType &type) {
3766e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  bool NonObjCPointer = false;
3767e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3768e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!type->isDependentType()) {
3769e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    if (const PointerType *ptr = type->getAs<PointerType>()) {
3770e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      QualType pointee = ptr->getPointeeType();
3771e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      if (pointee->isObjCRetainableType() || pointee->isPointerType())
3772e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis        return false;
3773e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // It is important not to lose the source info that there was an attribute
3774e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // applied to non-objc pointer. We will create an attributed type but
3775e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // its type will be the same as the original type.
3776e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      NonObjCPointer = true;
3777e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    } else if (!type->isObjCRetainableType()) {
3778e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      return false;
3779e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3780b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall
3781b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // Don't accept an ownership attribute in the declspec if it would
3782b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    // just be the return type of a block pointer.
3783b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    if (state.isProcessingDeclSpec()) {
3784b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      Declarator &D = state.getDeclarator();
3785b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall      if (maybeMovePastReturnType(D, D.getNumTypeObjects()))
3786b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall        return false;
3787b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    }
3788e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3789f85e193739c953358c865005855253af4f68a497John McCall
3790f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
3791440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  SourceLocation AttrLoc = attr.getLoc();
3792440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isMacroID())
3793440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    AttrLoc = S.getSourceManager().getImmediateExpansionRange(AttrLoc).first;
3794f85e193739c953358c865005855253af4f68a497John McCall
3795f85e193739c953358c865005855253af4f68a497John McCall  if (!attr.getParameterName()) {
3796440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::err_attribute_argument_n_not_string)
3797b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      << "objc_ownership" << 1;
3798f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3799f85e193739c953358c865005855253af4f68a497John McCall    return true;
3800f85e193739c953358c865005855253af4f68a497John McCall  }
3801f85e193739c953358c865005855253af4f68a497John McCall
3802d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Consume lifetime attributes without further comment outside of
3803d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // ARC mode.
38044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!S.getLangOpts().ObjCAutoRefCount)
3805d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    return true;
3806d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3807f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime lifetime;
3808f85e193739c953358c865005855253af4f68a497John McCall  if (attr.getParameterName()->isStr("none"))
3809f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_ExplicitNone;
3810f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("strong"))
3811f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Strong;
3812f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("weak"))
3813f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Weak;
3814f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("autoreleasing"))
3815f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Autoreleasing;
3816f85e193739c953358c865005855253af4f68a497John McCall  else {
3817440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_attribute_type_not_supported)
3818b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      << "objc_ownership" << attr.getParameterName();
3819f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3820f85e193739c953358c865005855253af4f68a497John McCall    return true;
3821f85e193739c953358c865005855253af4f68a497John McCall  }
3822f85e193739c953358c865005855253af4f68a497John McCall
3823d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  SplitQualType underlyingType = type.split();
3824d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3825d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Check for redundant/conflicting ownership qualifiers.
3826d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  if (Qualifiers::ObjCLifetime previousLifetime
3827d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        = type.getQualifiers().getObjCLifetime()) {
3828d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // If it's written directly, that's an error.
3829d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (hasDirectOwnershipQualifier(type)) {
3830d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant)
3831d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        << type;
3832d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return true;
3833d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3834d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3835d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // Otherwise, if the qualifiers actually conflict, pull sugar off
3836d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // until we reach a type that is directly qualified.
3837d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (previousLifetime != lifetime) {
3838d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // This should always terminate: the canonical type is
3839d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // qualified, so some bit of sugar must be hiding it.
3840d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      while (!underlyingType.Quals.hasObjCLifetime()) {
3841d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        underlyingType = underlyingType.getSingleStepDesugaredType();
3842d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      }
3843d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      underlyingType.Quals.removeObjCLifetime();
3844d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3845d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
3846d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3847d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  underlyingType.Quals.addObjCLifetime(lifetime);
3848f85e193739c953358c865005855253af4f68a497John McCall
3849e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (NonObjCPointer) {
3850e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    StringRef name = attr.getName()->getName();
3851e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    switch (lifetime) {
3852e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_None:
3853e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_ExplicitNone:
3854e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      break;
3855e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Strong: name = "__strong"; break;
3856e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Weak: name = "__weak"; break;
3857e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break;
3858e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3859e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_objc_object_attribute_wrong_type)
3860e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      << name << type;
3861e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3862e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3863f85e193739c953358c865005855253af4f68a497John McCall  QualType origType = type;
3864e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!NonObjCPointer)
3865d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    type = S.Context.getQualifiedType(underlyingType);
3866f85e193739c953358c865005855253af4f68a497John McCall
3867f85e193739c953358c865005855253af4f68a497John McCall  // If we have a valid source location for the attribute, use an
3868f85e193739c953358c865005855253af4f68a497John McCall  // AttributedType instead.
3869440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isValid())
3870b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis    type = S.Context.getAttributedType(AttributedType::attr_objc_ownership,
3871f85e193739c953358c865005855253af4f68a497John McCall                                       origType, type);
3872f85e193739c953358c865005855253af4f68a497John McCall
38739f084a3166b684573ba49df28fc5792bc37d92e1John McCall  // Forbid __weak if the runtime doesn't support it.
3874f85e193739c953358c865005855253af4f68a497John McCall  if (lifetime == Qualifiers::OCL_Weak &&
38750a7dd788dbef975f35f273c7ab913f480f7edd60John McCall      !S.getLangOpts().ObjCARCWeak && !NonObjCPointer) {
3876f85e193739c953358c865005855253af4f68a497John McCall
3877f85e193739c953358c865005855253af4f68a497John McCall    // Actually, delay this until we know what we're parsing.
3878f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
3879f85e193739c953358c865005855253af4f68a497John McCall      S.DelayedDiagnostics.add(
3880440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis          sema::DelayedDiagnostic::makeForbiddenType(
3881440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis              S.getSourceManager().getExpansionLoc(AttrLoc),
3882f85e193739c953358c865005855253af4f68a497John McCall              diag::err_arc_weak_no_runtime, type, /*ignored*/ 0));
3883f85e193739c953358c865005855253af4f68a497John McCall    } else {
3884440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis      S.Diag(AttrLoc, diag::err_arc_weak_no_runtime);
3885f85e193739c953358c865005855253af4f68a497John McCall    }
3886f85e193739c953358c865005855253af4f68a497John McCall
3887f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3888f85e193739c953358c865005855253af4f68a497John McCall    return true;
3889f85e193739c953358c865005855253af4f68a497John McCall  }
389091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
389191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // Forbid __weak for class objects marked as
3892742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  // objc_arc_weak_reference_unavailable
3893742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  if (lifetime == Qualifiers::OCL_Weak) {
3894b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall    if (const ObjCObjectPointerType *ObjT =
3895b2381b1c91ac5dc2407e98f36e3a6ba93d771791John McCall          type->getAs<ObjCObjectPointerType>()) {
38964e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith      if (ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl()) {
38974e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        if (Class->isArcWeakrefUnavailable()) {
38984e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class);
38994e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(ObjT->getInterfaceDecl()->getLocation(),
39004e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith                   diag::note_class_declared);
39014e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        }
3902742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      }
3903742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    }
3904742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  }
390591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3906f85e193739c953358c865005855253af4f68a497John McCall  return true;
3907f85e193739c953358c865005855253af4f68a497John McCall}
3908f85e193739c953358c865005855253af4f68a497John McCall
3909711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
3910711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
3911711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
3912711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
3913711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
3914711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
3915711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
3916711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
3917711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3918711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
3919711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
3920711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
3921711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
3922711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
3923711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3924711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
3925711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
3926711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3927711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3928d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
39291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3930d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
3931711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!attr.getParameterName()) {
3932711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3933ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
3934711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3935711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3936ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
39370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
3938711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getNumArgs() != 0) {
3939711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3940711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3941711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3942d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
3943711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getParameterName()->isStr("weak"))
39440953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
3945711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  else if (attr.getParameterName()->isStr("strong"))
39460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
3947d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
3948711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
3949711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      << "objc_gc" << attr.getParameterName();
3950711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3951711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3952d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
39531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
395414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  QualType origType = type;
395514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  type = S.Context.getObjCGCQualType(origType, GCAttr);
395614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
395714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // Make an attributed type to preserve the source information.
395814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (attr.getLoc().isValid())
395914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
396014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                       origType, type);
396114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3962711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
3963d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
3964d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
3965e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallnamespace {
3966e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// A helper class to unwrap a type down to a function for the
3967e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// purposes of applying attributes there.
3968e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///
3969e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// Use:
3970e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
3971e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   if (unwrapped.isFunctionType()) {
3972e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     const FunctionType *fn = unwrapped.get();
3973e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     // change fn somehow
3974e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     T = unwrapped.wrap(fn);
3975e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   }
3976e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  struct FunctionTypeUnwrapper {
3977e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    enum WrapKind {
3978e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Desugar,
3979e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Parens,
3980e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Pointer,
3981e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      BlockPointer,
3982e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Reference,
3983e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      MemberPointer
3984e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    };
3985e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3986e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType Original;
3987e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *Fn;
39885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<unsigned char /*WrapKind*/, 8> Stack;
3989e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3990e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
3991e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      while (true) {
3992e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const Type *Ty = T.getTypePtr();
3993e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<FunctionType>(Ty)) {
3994e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Fn = cast<FunctionType>(Ty);
3995e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return;
3996e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ParenType>(Ty)) {
3997e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ParenType>(Ty)->getInnerType();
3998e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Parens);
3999e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<PointerType>(Ty)) {
4000e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<PointerType>(Ty)->getPointeeType();
4001e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Pointer);
4002e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<BlockPointerType>(Ty)) {
4003e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<BlockPointerType>(Ty)->getPointeeType();
4004e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(BlockPointer);
4005e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<MemberPointerType>(Ty)) {
4006e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<MemberPointerType>(Ty)->getPointeeType();
4007e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(MemberPointer);
4008e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ReferenceType>(Ty)) {
4009e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ReferenceType>(Ty)->getPointeeType();
4010e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Reference);
4011e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else {
4012e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          const Type *DTy = Ty->getUnqualifiedDesugaredType();
4013e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          if (Ty == DTy) {
4014e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            Fn = 0;
4015e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            return;
4016e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          }
4017e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4018e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = QualType(DTy, 0);
4019e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Desugar);
4020e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        }
4021e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4022e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4023e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4024e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    bool isFunctionType() const { return (Fn != 0); }
4025e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *get() const { return Fn; }
4026e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4027e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(Sema &S, const FunctionType *New) {
4028e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // If T wasn't modified from the unwrapped type, do nothing.
4029e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (New == get()) return Original;
4030e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4031e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Fn = New;
4032e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return wrap(S.Context, Original, 0);
4033e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4034e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4035e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  private:
4036e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
4037e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size())
4038e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getQualifiedType(Fn, Old.getQualifiers());
4039e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4040e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // Build up the inner type, applying the qualifiers from the old
4041e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // type to the new type.
4042e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      SplitQualType SplitOld = Old.split();
4043e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4044e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // As a special case, tail-recurse if there are no qualifiers.
4045200fa53fd420aa8369586f569dbece04930ad6a3John McCall      if (SplitOld.Quals.empty())
4046200fa53fd420aa8369586f569dbece04930ad6a3John McCall        return wrap(C, SplitOld.Ty, I);
4047200fa53fd420aa8369586f569dbece04930ad6a3John McCall      return C.getQualifiedType(wrap(C, SplitOld.Ty, I), SplitOld.Quals);
4048e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4049e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4050e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
4051e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size()) return QualType(Fn, 0);
4052e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4053e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      switch (static_cast<WrapKind>(Stack[I++])) {
4054e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Desugar:
4055e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // This is the point at which we potentially lose source
4056e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // information.
4057e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
4058e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4059e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Parens: {
4060e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
4061e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getParenType(New);
4062e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4063e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4064e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Pointer: {
4065e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
4066e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getPointerType(New);
4067e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4068e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4069e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case BlockPointer: {
4070e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
4071e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getBlockPointerType(New);
4072e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4073e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4074e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case MemberPointer: {
4075e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
4076e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldMPT->getPointeeType(), I);
4077e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getMemberPointerType(New, OldMPT->getClass());
4078e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4079e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4080e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Reference: {
4081e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const ReferenceType *OldRef = cast<ReferenceType>(Old);
4082e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldRef->getPointeeType(), I);
4083e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<LValueReferenceType>(OldRef))
4084e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
4085e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        else
4086e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getRValueReferenceType(New);
4087e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4088e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
4089e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4090e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      llvm_unreachable("unknown wrapping kind");
4091e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
4092e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  };
4093e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
4094e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4095711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
4096711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
4097711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
4098711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
4099711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
4100711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
4101e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4102711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
41032455636163fdd18581d7fdae816433f886d88213Mike Stump
41048e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NoReturn) {
4105711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
410604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
4107e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
4108e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
4109711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
4110711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
4111425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
4112425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
4113711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
4114711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4115711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4116711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
4117425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
4118f85e193739c953358c865005855253af4f68a497John McCall  // ns_returns_retained is not always a type attribute, but if we got
4119f85e193739c953358c865005855253af4f68a497John McCall  // here, we're treating it as one right now.
41208e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NSReturnsRetained) {
41214e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(S.getLangOpts().ObjCAutoRefCount &&
4122f85e193739c953358c865005855253af4f68a497John McCall           "ns_returns_retained treated as type attribute in non-ARC");
4123f85e193739c953358c865005855253af4f68a497John McCall    if (attr.getNumArgs()) return true;
4124f85e193739c953358c865005855253af4f68a497John McCall
4125f85e193739c953358c865005855253af4f68a497John McCall    // Delay if this is not a function type.
4126f85e193739c953358c865005855253af4f68a497John McCall    if (!unwrapped.isFunctionType())
4127f85e193739c953358c865005855253af4f68a497John McCall      return false;
4128f85e193739c953358c865005855253af4f68a497John McCall
4129f85e193739c953358c865005855253af4f68a497John McCall    FunctionType::ExtInfo EI
4130f85e193739c953358c865005855253af4f68a497John McCall      = unwrapped.get()->getExtInfo().withProducesResult(true);
4131f85e193739c953358c865005855253af4f68a497John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4132f85e193739c953358c865005855253af4f68a497John McCall    return true;
4133f85e193739c953358c865005855253af4f68a497John McCall  }
4134f85e193739c953358c865005855253af4f68a497John McCall
41358e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_Regparm) {
4136711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
4137711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
4138711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
41391e030eb1194763b42c1752723be23b1515f48981John McCall
4140711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
4141711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
4142008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
41431e030eb1194763b42c1752723be23b1515f48981John McCall
4144ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Diagnose regparm with fastcall.
4145ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    const FunctionType *fn = unwrapped.get();
4146ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    CallingConv CC = fn->getCallConv();
4147ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (CC == CC_X86FastCall) {
4148ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
4149ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC)
4150ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm";
4151ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
4152ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
4153ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
4154ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
415591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    FunctionType::ExtInfo EI =
4156711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
4157711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4158711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4159425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
4160425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
416182bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  // Delay if the type didn't work out to a function.
416282bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  if (!unwrapped.isFunctionType()) return false;
416382bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman
416404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
4165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
4166711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
4167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4168f82b4e85b1219295cad4b5851b035575bc293010John McCall
4169711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
4170711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
4171064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis  if (S.Context.getCanonicalCallConv(CC) ==
4172e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      S.Context.getCanonicalCallConv(CCOld)) {
4173ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
4174ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4175711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4176e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  }
417704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
41788e68f1c8a2919ea83c2053731d6011074f1062e1Roman Divacky  if (CCOld != (S.LangOpts.MRTD ? CC_X86StdCall : CC_Default)) {
417904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Should we diagnose reapplications of the same convention?
4180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
418104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CC)
418204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CCOld);
4183711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
418504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
418604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
418704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
418804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
4189711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isa<FunctionNoProtoType>(fn)) {
4190711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_knr)
419104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
4192711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
4193711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
419404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
419504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
4196711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
419704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
4198711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
419904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
4200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
4201711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
420204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
4203ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
4204ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Also diagnose fastcall with regparm.
4205a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    if (fn->getHasRegParm()) {
4206ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
4207ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm"
4208ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC);
4209ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
4210ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
4211ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
421204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
4213f82b4e85b1219295cad4b5851b035575bc293010John McCall
4214711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
4215711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4216711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
4217f82b4e85b1219295cad4b5851b035575bc293010John McCall}
4218f82b4e85b1219295cad4b5851b035575bc293010John McCall
4219207f4d8543529221932af82836016a2ef066c917Peter Collingbourne/// Handle OpenCL image access qualifiers: read_only, write_only, read_write
4220207f4d8543529221932af82836016a2ef066c917Peter Collingbournestatic void HandleOpenCLImageAccessAttribute(QualType& CurType,
4221207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const AttributeList &Attr,
4222207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             Sema &S) {
4223207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  // Check the attribute arguments.
4224207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (Attr.getNumArgs() != 1) {
4225207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4226207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4227207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4228207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4229207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
4230207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  llvm::APSInt arg(32);
4231207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4232207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      !sizeExpr->isIntegerConstantExpr(arg, S.Context)) {
4233207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4234207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << "opencl_image_access" << sizeExpr->getSourceRange();
4235207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4236207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4237207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4238207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  unsigned iarg = static_cast<unsigned>(arg.getZExtValue());
4239207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  switch (iarg) {
4240207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_only:
4241207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_write_only:
4242207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_write:
4243207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4244207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4245207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  default:
4246207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4247207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
4248207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << sizeExpr->getSourceRange();
4249207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4250207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4251207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4252207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
4253207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
42546e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
42556e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
42566e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
42576e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
42586e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
42596e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
42606e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
4261788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
4262788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
426356affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
42646e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
42656e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4266e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
42676e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
42686e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
42696e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
42706e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
4271ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4272ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
42736e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
42746e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << "vector_size" << sizeExpr->getSourceRange();
4275e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
42766e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
42776e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
42786e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
4279f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
42806e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
4281e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
42826e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
42836e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
42846e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
42856e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
42866e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
42876e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
42886e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
42896e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
42906e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
42916e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4292e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
42936e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
42946e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
42956e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
42966e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
42976e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4298e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
42996e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
43006e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
43016e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
43026e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
43036e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
4304788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
4305e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
43066e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
43076e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
43084ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// \brief Process the OpenCL-like ext_vector_type attribute when it occurs on
43094ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// a type.
431091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierstatic void HandleExtVectorTypeAttr(QualType &CurType,
431191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                                    const AttributeList &Attr,
43124ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                    Sema &S) {
43134ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  Expr *sizeExpr;
431491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
43154ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Special case where the argument is a template id.
43164ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (Attr.getParameterName()) {
43174ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CXXScopeSpec SS;
4318e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    SourceLocation TemplateKWLoc;
43194ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    UnqualifiedId id;
43204ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
4321e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
4322e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    ExprResult Size = S.ActOnIdExpression(S.getCurScope(), SS, TemplateKWLoc,
4323e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          id, false, false);
43244ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Size.isInvalid())
43254ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
432691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
43274ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Size.get();
43284ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  } else {
43294ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    // check the attribute arguments.
43304ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Attr.getNumArgs() != 1) {
43314ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
43324ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
43334ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    }
43344ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Attr.getArg(0);
43354ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  }
433691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
43374ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Create the vector type.
43384ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  QualType T = S.BuildExtVectorType(CurType, sizeExpr, Attr.getLoc());
43394ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (!T.isNull())
43404ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CurType = T;
43414ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor}
43424ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
43434211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
43444211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
43454211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
43464211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
43474211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
43484211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
43494211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
43504211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
43514211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
43524211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     VectorType::VectorKind VecKind,
43534211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const char *AttrName) {
43544211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
43554211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
43564211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
43574211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
43584211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
43594211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
43604211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
43614211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
43624211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
43634211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
43644211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
43654211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
43664211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      << AttrName << numEltsExpr->getSourceRange();
43674211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
43684211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
43694211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
43704211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
43714211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  const BuiltinType* BTy = CurType->getAs<BuiltinType>();
43724211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (!BTy ||
43734211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (VecKind == VectorType::NeonPolyVector &&
43744211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::SChar &&
43754211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short) ||
43764211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (BTy->getKind() != BuiltinType::SChar &&
43774211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UChar &&
43784211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short &&
43794211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UShort &&
43804211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Int &&
43814211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UInt &&
43824211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::LongLong &&
43834211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::ULongLong &&
43844211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Float)) {
43854211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
43864211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
43874211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
43884211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
43894211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
43904211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
43914211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
43924211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
43934211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
43944211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
43954211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
43964211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
43974211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
43984211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
43994211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
44004211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
44014211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
4402711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
4403f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             TypeAttrLocation TAL, AttributeList *attrs) {
4404c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
4405c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
4406c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
4407c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
4408711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4409711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
4410711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
4411711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
4412711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr.getNext();
4413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4414e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
4415711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
4416e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
4417e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
4418cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    if (attr.isCXX11Attribute()) {
4419cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // [[gnu::...]] attributes are treated as declaration attributes, so may
4420cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // not appertain to a DeclaratorChunk, even if we handle them as type
4421cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // attributes.
4422cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.getScopeName() && attr.getScopeName()->isStr("gnu")) {
4423cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        if (TAL == TAL_DeclChunk) {
4424cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          state.getSema().Diag(attr.getLoc(),
4425cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                               diag::warn_cxx11_gnu_attribute_on_type)
4426cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith              << attr.getName();
4427cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          continue;
4428cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        }
4429cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      } else if (TAL != TAL_DeclChunk) {
4430cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // Otherwise, only consider type processing for a C++11 attribute if
4431cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // it's actually been applied to a type.
4432cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        continue;
4433cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      }
4434f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    }
4435f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
4436b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
4437b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
4438711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
4439cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    default:
4440cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // A C++11 attribute on a declarator chunk must appertain to a type.
4441d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk) {
4442cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(), diag::err_attribute_not_type_attr)
4443d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith          << attr.getName();
4444d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith        attr.setUsedAsTypeAttr();
4445d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      }
4446cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
4447cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
4448cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::UnknownAttribute:
4449cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk)
4450cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(),
4451cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                             diag::warn_unknown_attribute_ignored)
4452cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          << attr.getName();
4453cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
4454cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
4455cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::IgnoredAttribute:
4456cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
445704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
44588e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_MayAlias:
4459682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // FIXME: This attribute needs to actually be handled, but if we ignore
4460682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // it it breaks large amounts of Linux software.
4461682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      attr.setUsedAsTypeAttr();
4462682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      break;
44638e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_AddressSpace:
4464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
4465e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4466c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
4467711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
4468711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
4469711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
4470e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4471d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
44728e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_VectorSize:
4473711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
4474e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4475f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
44768e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ExtVectorType:
4477a4fa9008988985f9cf01712a99ddd923aea278a0Richard Smith      HandleExtVectorTypeAttr(type, attr, state.getSema());
4478e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
44794ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      break;
44808e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonVectorType:
4481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
4482711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonVector, "neon_vector_type");
4483e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
44844211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
44858e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonPolyVectorType:
4486711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
4487711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonPolyVector,
44884211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                               "neon_polyvector_type");
4489e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
44904211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
44918e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_OpenCLImageAccess:
4492207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      HandleOpenCLImageAccessAttribute(type, attr, state.getSema());
4493e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4494207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      break;
4495207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
4496d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith    case AttributeList::AT_Win64:
4497d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith    case AttributeList::AT_Ptr32:
4498d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith    case AttributeList::AT_Ptr64:
4499d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      // FIXME: Don't ignore these. We have partial handling for them as
4500d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      // declaration attributes in SemaDeclAttr.cpp; that should be moved here.
4501d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      attr.setUsedAsTypeAttr();
4502d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      break;
4503d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith
45048e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
45054e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
4506cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        break;
4507f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough into the function attrs
4508f85e193739c953358c865005855253af4f68a497John McCall
4509711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
4510e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4511e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
4512711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
4513711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
4514f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      if (TAL == TAL_DeclSpec)
4515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
4516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
4518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
4519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
45206e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
4521c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
4522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attrs = next));
4523232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
4524232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
4525e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \brief Ensure that the type of the given expression is complete.
4526e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4527e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// This routine checks whether the expression \p E has a complete type. If the
4528e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// expression refers to an instantiable construct, that instantiation is
4529e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// performed as needed to complete its type. Furthermore
4530e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// Sema::RequireCompleteType is called for the expression's type (or in the
4531e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// case of a reference type, the referred-to type).
4532e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4533e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \param E The expression whose type is required to be complete.
4534d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// \param Diagnoser The object that will emit a diagnostic if the type is
4535d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// incomplete.
4536e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4537e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \returns \c true if the type of \p E is incomplete and diagnosed, \c false
4538e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// otherwise.
4539f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, TypeDiagnoser &Diagnoser){
4540e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  QualType T = E->getType();
4541e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4542e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Fast path the case where the type is already complete.
4543e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (!T->isIncompleteType())
4544e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    return false;
4545e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4546e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Incomplete array types may be completed by the initializer attached to
4547e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // their definitions. For static data members of class templates we need to
4548e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // instantiate the definition to get this initializer and complete the type.
4549e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (T->isIncompleteArrayType()) {
4550e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4551e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
4552e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        if (Var->isStaticDataMember() &&
4553e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth            Var->getInstantiatedFromStaticDataMember()) {
455491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
455536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
455636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          assert(MSInfo && "Missing member specialization information?");
455736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          if (MSInfo->getTemplateSpecializationKind()
455836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                != TSK_ExplicitSpecialization) {
455936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // If we don't already have a point of instantiation, this is it.
456036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (MSInfo->getPointOfInstantiation().isInvalid()) {
456136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              MSInfo->setPointOfInstantiation(E->getLocStart());
456291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
456391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier              // This is a modification of an existing AST node. Notify
456436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              // listeners.
456536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              if (ASTMutationListener *L = getASTMutationListener())
456636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                L->StaticDataMemberInstantiated(Var);
456736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
456891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
456936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            InstantiateStaticDataMemberDefinition(E->getExprLoc(), Var);
457091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
457136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // Update the type to the newly instantiated definition's type both
457236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // here and within the expression.
457336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (VarDecl *Def = Var->getDefinition()) {
457436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setDecl(Def);
457536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              T = Def->getType();
457636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setType(T);
457736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              E->setType(T);
457836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
4579f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor          }
458091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4581e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // We still go on to try to complete the type independently, as it
4582e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // may also require instantiations or diagnostics if it remains
4583e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // incomplete.
4584e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        }
4585e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      }
4586e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    }
4587e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  }
4588e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4589e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // FIXME: Are there other cases which require instantiating something other
4590e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // than the type to complete the type of an expression?
4591e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4592e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Look through reference types and complete the referred type.
4593e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
4594e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    T = Ref->getPointeeType();
4595e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4596d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(E->getExprLoc(), T, Diagnoser);
4597d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4598d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4599d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregornamespace {
4600f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  struct TypeDiagnoserDiag : Sema::TypeDiagnoser {
4601d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    unsigned DiagID;
460291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4603f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor    TypeDiagnoserDiag(unsigned DiagID)
4604f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor      : Sema::TypeDiagnoser(DiagID == 0), DiagID(DiagID) {}
460591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4606d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) {
4607d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      if (Suppressed) return;
4608d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      S.Diag(Loc, DiagID) << T;
4609d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    }
4610d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  };
4611d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4612d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4613d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, unsigned DiagID) {
4614f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4615d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteExprType(E, Diagnoser);
4616e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth}
4617e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
46181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
46194ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
46204ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
46214ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
462286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
462386447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
462486447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
462586447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
462686447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
46274ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
46284ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
46294ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
46304ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
46314ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
46324ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
46334ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
46344ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
463591a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
4636f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                               TypeDiagnoser &Diagnoser) {
4637573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
4638573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
4639690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
4640690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
4641690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
46421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
4643690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
4644690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
46454ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
4646d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  NamedDecl *Def = 0;
4647d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  if (!T->isIncompleteType(&Def)) {
4648d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    // If we know about the definition but it is not visible, complain.
4649d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (!Diagnoser.Suppressed && Def && !LookupResult::isVisible(Def)) {
4650d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // Suppress this error outside of a SFINAE context if we've already
465191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // emitted the error once for this type. There's no usefulness in
4652d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // repeating the diagnostic.
4653d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // FIXME: Add a Fix-It that imports the corresponding module or includes
4654d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // the header.
4655ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Module *Owner = Def->getOwningModule();
4656ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Loc, diag::err_module_private_definition)
4657ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        << T << Owner->getFullModuleName();
4658ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Def->getLocation(), diag::note_previous_definition);
4659ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
4660ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      if (!isSFINAEContext()) {
4661ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        // Recover by implicitly importing this module.
4662ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        createImplicitModuleImport(Loc, Owner);
4663d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      }
4664d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    }
466591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
46664ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
4667d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
46684ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
4669bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const TagType *Tag = T->getAs<TagType>();
4670bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const ObjCInterfaceType *IFace = 0;
467191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4672bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  if (Tag) {
4673bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4674bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->isInvalidDecl())
4675bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
4676bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan
4677bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4678bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->hasExternalLexicalStorage()) {
4679bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(Tag->getDecl());
4680bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!Tag->isIncompleteType())
4681bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4682bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4683bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
4684bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  else if ((IFace = T->getAs<ObjCInterfaceType>())) {
4685bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4686bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->isInvalidDecl())
4687bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
468891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4689bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4690bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->hasExternalLexicalStorage()) {
4691bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(IFace->getDecl());
4692bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!IFace->isIncompleteType())
4693bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4694bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4695bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
469691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4697d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
4698923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
4699923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
4700923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
4701e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor  while (const ConstantArrayType *Array
4702e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor           = Context.getAsConstantArrayType(MaybeTemplate))
4703923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
4704923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
47052943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
4706d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
4707972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
4708972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
4709d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
4710d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                            /*Complain=*/!Diagnoser.Suppressed);
47111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
4712d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
4713564f4c5664f552becbd05407611a92754c40e641Richard Smith      CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass();
4714564f4c5664f552becbd05407611a92754c40e641Richard Smith      if (!Rec->isBeingDefined() && Pattern) {
4715564f4c5664f552becbd05407611a92754c40e641Richard Smith        MemberSpecializationInfo *MSI = Rec->getMemberSpecializationInfo();
4716564f4c5664f552becbd05407611a92754c40e641Richard Smith        assert(MSI && "Missing member specialization information?");
4717357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
4718564f4c5664f552becbd05407611a92754c40e641Richard Smith        if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
4719f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
4720f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
4721f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
4722d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                  /*Complain=*/!Diagnoser.Suppressed);
4723d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
4724d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
4725d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
47262943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
4727d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  if (Diagnoser.Suppressed)
47285842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
4729d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
47304ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
4731d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
473291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
47334ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
473401620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
47354ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
47361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
47374ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
47384ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
4739b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor      << QualType(Tag, 0);
474091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4741b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  // If the Objective-C class was a forward declaration, produce a note.
4742b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  if (IFace && !IFace->getDecl()->isInvalidDecl())
4743b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor    Diag(IFace->getDecl()->getLocation(), diag::note_forward_class);
47444ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
47454ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
47464ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
4747e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
4748fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
474991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                               unsigned DiagID) {
4750f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4751d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(Loc, T, Diagnoser);
4752fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
4753fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
47546666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \brief Get diagnostic %select index for tag kind for
47556666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// literal type diagnostic message.
47566666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// WARNING: Indexes apply to particular diagnostics only!
47576666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos///
47586666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \returns diagnostic %select index.
4759f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matosstatic unsigned getLiteralDiagFromTagKind(TagTypeKind Tag) {
47606666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  switch (Tag) {
4761f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Struct: return 0;
4762f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Interface: return 1;
4763f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Class:  return 2;
4764f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  default: llvm_unreachable("Invalid tag kind for literal type diagnostic!");
47656666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  }
47666666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos}
47676666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos
47689f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @brief Ensure that the type T is a literal type.
47699f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
47709f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// This routine checks whether the type @p T is a literal type. If @p T is an
47719f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// incomplete type, an attempt is made to complete it. If @p T is a literal
47729f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// type, or @p AllowIncompleteType is true and @p T is an incomplete type,
47739f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// returns false. Otherwise, this routine issues the diagnostic @p PD (giving
47749f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// it the type @p T), along with notes explaining why the type is not a
47759f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// literal type, and returns true.
47769f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
47779f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param Loc  The location in the source that the non-literal type
47789f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// diagnostic should refer to.
47799f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
47809f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param T  The type that this routine is examining for literalness.
47819f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
4782f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor/// @param Diagnoser Emits a diagnostic if T is not a literal type.
47839f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
47849f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @returns @c true if @p T is not a literal type and a diagnostic was emitted,
47859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @c false otherwise.
47869f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithbool Sema::RequireLiteralType(SourceLocation Loc, QualType T,
4787f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                              TypeDiagnoser &Diagnoser) {
47889f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  assert(!T->isDependentType() && "type should not be dependent");
47899f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4790ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  QualType ElemType = Context.getBaseElementType(T);
4791ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  RequireCompleteType(Loc, ElemType, 0);
4792ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
479386c3ae46250cdcc57778c27826060779a92f3815Richard Smith  if (T->isLiteralType())
47949f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return false;
47959f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4796f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  if (Diagnoser.Suppressed)
47979f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
47989f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4799f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
48009f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
48019f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (T->isVariableArrayType())
48029f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
48039f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4804ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  const RecordType *RT = ElemType->getAs<RecordType>();
48059f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (!RT)
48069f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
48079f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
48089f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
48099f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4810c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // A partially-defined class type can't be a literal type, because a literal
4811c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // class type must have a trivial destructor (which can't be checked until
4812c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // the class definition is complete).
4813c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  if (!RD->isCompleteDefinition()) {
4814d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T);
4815ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman    return true;
4816c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  }
4817ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
48189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // If the class has virtual base classes, then it's not an aggregate, and
481986c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // cannot have any constexpr constructors or a trivial default constructor,
482086c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // so is non-literal. This is better to diagnose than the resulting absence
482186c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // of constexpr constructors.
48229f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (RD->getNumVBases()) {
48239f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_virtual_base)
48246666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      << getLiteralDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases();
48259f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
48269f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           E = RD->vbases_end(); I != E; ++I)
482796a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      Diag(I->getLocStart(),
48289f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           diag::note_constexpr_virtual_base_here) << I->getSourceRange();
482986c3ae46250cdcc57778c27826060779a92f3815Richard Smith  } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor() &&
483086c3ae46250cdcc57778c27826060779a92f3815Richard Smith             !RD->hasTrivialDefaultConstructor()) {
48319f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD;
48329f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (RD->hasNonLiteralTypeFieldsOrBases()) {
48339f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
48349f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->bases_end(); I != E; ++I) {
48359f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      if (!I->getType()->isLiteralType()) {
483696a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        Diag(I->getLocStart(),
48379f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith             diag::note_non_literal_base_class)
48389f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith          << RD << I->getType() << I->getSourceRange();
48399f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
48409f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
48419f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
48429f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::field_iterator I = RD->field_begin(),
48439f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->field_end(); I != E; ++I) {
4844262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      if (!I->getType()->isLiteralType() ||
4845262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          I->getType().isVolatileQualified()) {
4846262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        Diag(I->getLocation(), diag::note_non_literal_field)
4847581deb3da481053c4993c7600f97acf7768caac5David Blaikie          << RD << *I << I->getType()
4848262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          << I->getType().isVolatileQualified();
48499f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
48509f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
48519f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
48529f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (!RD->hasTrivialDestructor()) {
48539f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // All fields and bases are of literal types, so have trivial destructors.
48549f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // If this class's destructor is non-trivial it must be user-declared.
48559f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    CXXDestructorDecl *Dtor = RD->getDestructor();
48569f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    assert(Dtor && "class has literal fields and bases but no dtor?");
48579f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    if (!Dtor)
48589f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      return true;
48599f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
48609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(Dtor->getLocation(), Dtor->isUserProvided() ?
48619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_user_provided_dtor :
48629f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_nontrivial_dtor) << RD;
4863ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    if (!Dtor->isUserProvided())
4864ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith      SpecialMemberIsTrivial(Dtor, CXXDestructor, /*Diagnose*/true);
48659f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  }
48669f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
48679f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return true;
48689f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
48699f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
487091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierbool Sema::RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID) {
4871f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4872f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  return RequireLiteralType(Loc, T, Diagnoser);
4873f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor}
4874f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor
4875465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
4876465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
4877465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
4878465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
4879465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
4880e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
4881465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
4882e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
4883465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
4884465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
4885465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
4886465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
4887465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
4888465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
4889465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
4890e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
4891af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
48922a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
4893fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
48942a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
48952a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
48962a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
48972b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
48982b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
4899aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
4900aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
49012b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
4902af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
4903af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
4904af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
4905f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// getDecltypeForExpr - Given an expr, will return the decltype for
4906f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// that expression, according to the rules in C++11
4907f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18.
4908f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregorstatic QualType getDecltypeForExpr(Sema &S, Expr *E) {
4909f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (E->isTypeDependent())
4910f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    return S.Context.DependentTy;
4911f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
49126d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
49136d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   The type denoted by decltype(e) is defined as follows:
49146d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //
49156d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //     - if e is an unparenthesized id-expression or an unparenthesized class
491691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       member access (5.2.5), decltype(e) is the type of the entity named
491791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       by e. If there is no such entity, or if e names a set of overloaded
49186d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       functions, the program is ill-formed;
491984dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  //
492084dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  // We apply the same rules for Objective-C ivar and property references.
4921f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
4922f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
4923f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return VD->getType();
492484dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
4925f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
4926f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return FD->getType();
492784dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCIvarRefExpr *IR = dyn_cast<ObjCIvarRefExpr>(E)) {
492884dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    return IR->getDecl()->getType();
492984dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCPropertyRefExpr *PR = dyn_cast<ObjCPropertyRefExpr>(E)) {
493084dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    if (PR->isExplicitProperty())
493184dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor      return PR->getExplicitProperty()->getType();
4932f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
493384dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor
4934f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  // C++11 [expr.lambda.prim]p18:
4935f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   Every occurrence of decltype((x)) where x is a possibly
4936f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   parenthesized id-expression that names an entity of automatic
4937f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   storage duration is treated as if x were transformed into an
4938f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   access to a corresponding data member of the closure type that
4939f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   would have been declared if x were an odr-use of the denoted
4940f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   entity.
4941f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  using namespace sema;
4942f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (S.getCurLambda()) {
4943f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (isa<ParenExpr>(E)) {
4944f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4945f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
494668932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          QualType T = S.getCapturedDeclRefType(Var, DRE->getLocation());
494768932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          if (!T.isNull())
494868932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor            return S.Context.getLValueReferenceType(T);
4949f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        }
4950f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      }
4951f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    }
4952f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
4953f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
4954f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
49556d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
49566d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   [...]
49576d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  QualType T = E->getType();
49586d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  switch (E->getValueKind()) {
495991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the
49606d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
49616d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_XValue: T = S.Context.getRValueReferenceType(T); break;
496291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an lvalue, decltype(e) is T&, where T is the
49636d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
49646d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_LValue: T = S.Context.getLValueReferenceType(T); break;
49656d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //  - otherwise, decltype(e) is the type of e.
49666d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_RValue: break;
49676d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  }
496891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4969f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return T;
4970f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor}
4971f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
49722a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
4973fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
49742a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
49752a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
497691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4977f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return Context.getDecltypeType(E, getDecltypeForExpr(*this, E));
4978af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
4979ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
4980ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType Sema::BuildUnaryTransformType(QualType BaseType,
4981ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::UTTKind UKind,
4982ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       SourceLocation Loc) {
4983ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  switch (UKind) {
4984ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case UnaryTransformType::EnumUnderlyingType:
4985ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) {
4986ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Diag(Loc, diag::err_only_enums_have_underlying_types);
4987ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return QualType();
4988ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    } else {
4989ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      QualType Underlying = BaseType;
4990ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      if (!BaseType->isDependentType()) {
4991ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl();
4992ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        assert(ED && "EnumType has no EnumDecl");
4993ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        DiagnoseUseOfDecl(ED, Loc);
4994ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        Underlying = ED->getIntegerType();
4995ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      }
4996ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(!Underlying.isNull());
4997ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return Context.getUnaryTransformType(BaseType, Underlying,
4998ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                        UnaryTransformType::EnumUnderlyingType);
4999ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
5000ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  }
5001ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  llvm_unreachable("unknown unary transform type");
5002ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
5003b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5004b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) {
5005b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T->isDependentType()) {
50068327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // FIXME: It isn't entirely clear whether incomplete atomic types
50078327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // are allowed or not; for simplicity, ban them for the moment.
5008d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (RequireCompleteType(Loc, T, diag::err_atomic_specifier_bad_type, 0))
50098327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith      return QualType();
50108327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith
5011b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    int DisallowedKind = -1;
50128327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    if (T->isArrayType())
5013b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 1;
5014b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isFunctionType())
5015b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 2;
5016b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isReferenceType())
5017b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 3;
5018b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isAtomicType())
5019b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 4;
5020b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T.hasQualifiers())
5021b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 5;
5022b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (!T.isTriviallyCopyableType(Context))
5023b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      // Some other non-trivially-copyable type (probably a C++ class)
5024b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 6;
5025b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5026b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (DisallowedKind != -1) {
5027b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T;
5028b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return QualType();
5029b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
5030b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5031b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // FIXME: Do we need any handling for ARC here?
5032b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
5033b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
5034b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Build the pointer type.
5035b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return Context.getAtomicType(T);
5036b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
5037