SemaType.cpp revision 9dd74c5504c743c96ea3a1d691d6a75ec3a98147
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"
3487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor#include "llvm/Support/ErrorHandling.h"
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
375db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// isOmittedBlockReturnType - Return true if this declarator is missing a
3891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier/// return type because this is a omitted return type on a block literal.
398ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic bool isOmittedBlockReturnType(const Declarator &D) {
405db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getContext() != Declarator::BlockLiteralContext ||
418ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl      D.getDeclSpec().hasTypeSpecifier())
425db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return false;
4391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
445db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 0)
45a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^{ ... }
4691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
475db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 1 &&
485db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      D.getTypeObject(0).Kind == DeclaratorChunk::Function)
49a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^(int X, float Y) { ... }
5091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
515db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  return false;
525db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner}
535db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
542792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// diagnoseBadTypeAttribute - Diagnoses a type attribute which
552792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// doesn't apply to the given type.
562792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCallstatic void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr,
572792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall                                     QualType type) {
58108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth  bool useExpansionLoc = false;
592792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
602792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  unsigned diagID = 0;
612792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  switch (attr.getKind()) {
628e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  case AttributeList::AT_ObjCGC:
632792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    diagID = diag::warn_pointer_attribute_wrong_type;
64108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth    useExpansionLoc = true;
652792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    break;
662792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
678e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  case AttributeList::AT_ObjCOwnership:
6805d4876a64865e34366b58fc8a6848c3cde895d9Argyrios Kyrtzidis    diagID = diag::warn_objc_object_attribute_wrong_type;
69108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth    useExpansionLoc = true;
7005d4876a64865e34366b58fc8a6848c3cde895d9Argyrios Kyrtzidis    break;
7105d4876a64865e34366b58fc8a6848c3cde895d9Argyrios Kyrtzidis
722792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  default:
732792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    // Assume everything else was a function attribute.
742792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    diagID = diag::warn_function_attribute_wrong_type;
752792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    break;
762792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  }
772792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
782792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  SourceLocation loc = attr.getLoc();
795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef name = attr.getName()->getName();
802792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
812792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  // The GC attributes are usually written with macros;  special-case them.
82108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth  if (useExpansionLoc && loc.isMacroID() && attr.getParameterName()) {
83834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    if (attr.getParameterName()->isStr("strong")) {
84834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__strong")) name = "__strong";
85834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    } else if (attr.getParameterName()->isStr("weak")) {
86834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__weak")) name = "__weak";
872792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    }
882792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  }
892792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
902792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  S.Diag(loc, diagID) << name << type;
912792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall}
922792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
93711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// objc_gc applies to Objective-C pointers or, otherwise, to the
94711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// smallest available pointer type (i.e. 'void*' in 'void**').
95711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
968e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ObjCGC: \
978e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ObjCOwnership
98711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
99711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// Function type attributes.
100711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define FUNCTION_TYPE_ATTRS_CASELIST \
1018e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NoReturn: \
1028e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_CDecl: \
1038e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_FastCall: \
1048e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_StdCall: \
1058e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ThisCall: \
1068e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Pascal: \
1078e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Regparm: \
108263366f9241366f29ba65b703120f302490c39ffDerek Schuff    case AttributeList::AT_Pcs: \
10938980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    case AttributeList::AT_PnaclCall: \
11038980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    case AttributeList::AT_IntelOclBicc \
111711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
112711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace {
113711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// An object which stores processing state for the entire
114711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// GetTypeForDeclarator process.
115711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  class TypeProcessingState {
116711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &sema;
117711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
118711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The declarator being processed.
119711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &declarator;
120711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
121711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The index of the declarator chunk we're currently processing.
122711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// May be the total number of valid chunks, indicating the
123711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// DeclSpec.
124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex;
125711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
126711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Whether there are non-trivial modifications to the decl spec.
127711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    bool trivial;
128711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1297ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    /// Whether we saved the attributes in the decl spec.
1307ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    bool hasSavedAttrs;
1317ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
132711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The original set of attributes on the DeclSpec.
1335f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> savedAttrs;
134711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
135711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// A list of attributes to diagnose the uselessness of when the
136711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// processing is complete.
1375f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> ignoredTypeAttrs;
138711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
139711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  public:
140711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    TypeProcessingState(Sema &sema, Declarator &declarator)
141711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : sema(sema), declarator(declarator),
142711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        chunkIndex(declarator.getNumTypeObjects()),
1437ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        trivial(true), hasSavedAttrs(false) {}
144711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
145711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &getSema() const {
146711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return sema;
147711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
148711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
149711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &getDeclarator() const {
150711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator;
151711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
152711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
153711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned getCurrentChunkIndex() const {
154711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return chunkIndex;
155711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
156711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
157711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void setCurrentChunkIndex(unsigned idx) {
158711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(idx <= declarator.getNumTypeObjects());
159711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      chunkIndex = idx;
160711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
161711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&getCurrentAttrListRef() const {
163711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(chunkIndex <= declarator.getNumTypeObjects());
164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (chunkIndex == declarator.getNumTypeObjects())
165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        return getMutableDeclSpec().getAttributes().getListRef();
166711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator.getTypeObject(chunkIndex).getAttrListRef();
167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
168711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
169711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Save the current set of attributes on the DeclSpec.
170711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void saveDeclSpecAttrs() {
171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Don't try to save them multiple times.
1727ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (hasSavedAttrs) return;
173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      DeclSpec &spec = getMutableDeclSpec();
175711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (AttributeList *attr = spec.getAttributes().getList(); attr;
176711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             attr = attr->getNext())
177711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs.push_back(attr);
178711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      trivial &= savedAttrs.empty();
1797ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      hasSavedAttrs = true;
180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
181711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
182711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Record that we had nowhere to put the given type attribute.
183711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// We will diagnose such attributes later.
184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void addIgnoredTypeAttr(AttributeList &attr) {
185711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      ignoredTypeAttrs.push_back(&attr);
186711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
187711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
188711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Diagnose all the ignored type attributes, given that the
189711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// declarator worked out to the given type.
190711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void diagnoseIgnoredTypeAttrs(QualType type) const {
1915f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      for (SmallVectorImpl<AttributeList*>::const_iterator
192711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
1932792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall           i != e; ++i)
1942792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall        diagnoseBadTypeAttribute(getSema(), **i, type);
195711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
196711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
197711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    ~TypeProcessingState() {
198711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (trivial) return;
199711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      restoreDeclSpecAttrs();
201711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
202711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
203711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  private:
204711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclSpec &getMutableDeclSpec() const {
205711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return const_cast<DeclSpec&>(declarator.getDeclSpec());
206711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
207711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
208711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void restoreDeclSpecAttrs() {
2097ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      assert(hasSavedAttrs);
2107ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
2117ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (savedAttrs.empty()) {
2127ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        getMutableDeclSpec().getAttributes().set(0);
2137ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        return;
2147ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      }
2157ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
216711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
217711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
218711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs[i]->setNext(savedAttrs[i+1]);
219711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      savedAttrs.back()->setNext(0);
220711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
221711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
222711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
223711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// Basically std::pair except that we really want to avoid an
224711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// implicit operator= for safety concerns.  It's also a minor
225711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// link-time optimization for this to be a private type.
226711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  struct AttrAndList {
227711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The attribute.
228711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &first;
229711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
230711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The head of the list the attribute is currently in.
231711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&second;
232711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
233711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttrAndList(AttributeList &attr, AttributeList *&head)
234711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : first(attr), second(head) {}
235711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
23604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
23704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
238711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace llvm {
239711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  template <> struct isPodLike<AttrAndList> {
240711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    static const bool value = true;
241711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
242711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
243711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
244711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
245711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  attr.setNext(head);
246711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  head = &attr;
247711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
248711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
249711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
250711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (head == &attr) {
251711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    head = attr.getNext();
252711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
25304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
254711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
255711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *cur = head;
256711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  while (true) {
257711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    assert(cur && cur->getNext() && "ran out of attrs?");
258711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (cur->getNext() == &attr) {
259711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      cur->setNext(attr.getNext());
260711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
261711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
262711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    cur = cur->getNext();
263711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
264711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
265711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
266711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void moveAttrFromListToList(AttributeList &attr,
267711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&fromList,
268711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&toList) {
269711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, fromList);
270711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrIntoList(attr, toList);
271711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
272711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
273f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith/// The location of a type attribute.
274f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smithenum TypeAttrLocation {
275f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is in the decl-specifier-seq.
276f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclSpec,
277f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is part of a DeclaratorChunk.
278f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclChunk,
279f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is immediately after the declaration's name.
280f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclName
281f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith};
282f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
283711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state,
284f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             QualType &type, TypeAttrLocation TAL,
285711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             AttributeList *attrs);
286711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
287711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
288711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
289711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type);
290711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
291711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
292711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr, QualType &type);
293711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
294b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
295f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr, QualType &type);
296f85e193739c953358c865005855253af4f68a497John McCall
297711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCPointerTypeAttr(TypeProcessingState &state,
298711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr, QualType &type) {
2998e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_ObjCGC)
300f85e193739c953358c865005855253af4f68a497John McCall    return handleObjCGCTypeAttr(state, attr, type);
3018e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  assert(attr.getKind() == AttributeList::AT_ObjCOwnership);
302b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  return handleObjCOwnershipTypeAttr(state, attr, type);
303711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
304711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
305711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Given that an objc_gc attribute was written somewhere on a
306711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declaration *other* than on the declarator itself (for which, use
307711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
308711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// didn't apply in whatever position it was written in, try to move
309711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// it to a more appropriate position.
310711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeObjCPointerTypeAttr(TypeProcessingState &state,
311711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          AttributeList &attr,
312711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType type) {
313711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
314711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
315711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
316711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
317711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
318711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
319711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
320711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
321711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
322711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
323711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
324711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
325711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
326711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
327711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Don't walk through these.
328711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
329711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
330711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
331711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto error;
332711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
333711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
334711c52bb20d0c69063b52a99826fb7d2835501f1John McCall error:
3352792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
3362792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
337711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
338711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
339711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Distribute an objc_gc type attribute that was written on the
340711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator.
341711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
342711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
343711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            AttributeList &attr,
344711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            QualType &declSpecType) {
345711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
346711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
347711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // objc_gc goes on the innermost pointer to something that's not a
348711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // pointer.
349711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  unsigned innermost = -1U;
350711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  bool considerDeclSpec = true;
351711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
352711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
353711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
354711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
355711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
356711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      innermost = i;
357ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      continue;
358711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
359711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
360711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
361711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
362711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
363711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
364711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
365711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
366711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      considerDeclSpec = false;
367711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto done;
368711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
369711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
370711c52bb20d0c69063b52a99826fb7d2835501f1John McCall done:
371711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
372711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // That might actually be the decl spec if we weren't blocked by
373711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // anything in the declarator.
374711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (considerDeclSpec) {
3757ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    if (handleObjCPointerTypeAttr(state, attr, declSpecType)) {
3767ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // Splice the attribute into the decl spec.  Prevents the
3777ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // attribute from being applied multiple times and gives
3787ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // the source-location-filler something to work with.
3797ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      state.saveDeclSpecAttrs();
3807ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      moveAttrFromListToList(attr, declarator.getAttrListRef(),
3817ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall               declarator.getMutableDeclSpec().getAttributes().getListRef());
382711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
3837ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    }
384711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
385711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
386711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, if we found an appropriate chunk, splice the attribute
387711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // into it.
388711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (innermost != -1U) {
389711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, declarator.getAttrListRef(),
390711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                       declarator.getTypeObject(innermost).getAttrListRef());
391711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
392711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
393711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
394711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, diagnose when we're done building the type.
395711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
396711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
397711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
398711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
399711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written somewhere in a declaration
400711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// *other* than on the declarator itself or in the decl spec.  Given
401711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// that it didn't apply in whatever position it was written in, try
402711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// to move it to a more appropriate position.
403711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeFunctionTypeAttr(TypeProcessingState &state,
404711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
405711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType type) {
406711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
407711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
408711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to push the attribute from the return type of a function to
409711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the function itself.
410711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
411711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
412711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
414711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
415711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
416711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
417711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
418711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
419711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
420711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
421711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
422711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
423711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
424711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
425711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
426711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
42791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4282792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
429711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
430711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
431711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Try to distribute a function type attribute to the innermost
432711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// function chunk or type.  Returns true if the attribute was
433711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributed, false if no location was found.
434711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool
435711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
436711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr,
437711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList *&attrList,
438711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      QualType &declSpecType) {
439711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
440711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
441711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Put it on the innermost function chunk, if there is one.
442711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
443711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (chunk.Kind != DeclaratorChunk::Function) continue;
445711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
446711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
448711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
449711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
450f85e193739c953358c865005855253af4f68a497John McCall  if (handleFunctionTypeAttr(state, attr, declSpecType)) {
451f85e193739c953358c865005855253af4f68a497John McCall    spliceAttrOutOfList(attr, attrList);
452f85e193739c953358c865005855253af4f68a497John McCall    return true;
453f85e193739c953358c865005855253af4f68a497John McCall  }
454f85e193739c953358c865005855253af4f68a497John McCall
455f85e193739c953358c865005855253af4f68a497John McCall  return false;
456711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
457711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
458711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written in the decl spec.  Try to
459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
460711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
461711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
462711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
463711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType &declSpecType) {
464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.saveDeclSpecAttrs();
465711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4665c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // C++11 attributes before the decl specifiers actually appertain to
4675c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // the declarators. Move them straight there. We don't support the
4685c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // 'put them wherever you like' semantics we allow for GNU attributes.
4695c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  if (attr.isCXX11Attribute()) {
4705c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
4715c52166525f8714c3e3a979b156ec23426947fd3Richard Smith                           state.getDeclarator().getAttrListRef());
4725c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    return;
4735c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  }
4745c52166525f8714c3e3a979b156ec23426947fd3Richard Smith
475711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
476711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
477711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            state.getCurrentAttrListRef(),
478711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
479711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
480711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
482711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
484711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
485711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
486711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written on the declarator.  Try to
487711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
488711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
489711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
490711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         AttributeList &attr,
491711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         QualType &declSpecType) {
492711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
493711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
494711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declarator.getAttrListRef(),
497711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
498711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
499711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
501711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
505711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
506711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// \brief Given that there are attributes written on the declarator
507711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// itself, try to distribute any type attributes to the appropriate
508711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator chunk.
509711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///
510711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// These are attributes like the following:
511711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f ATTR;
512711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int (f ATTR)();
513711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// but not necessarily this:
514711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f() ATTR;
515711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                              QualType &declSpecType) {
517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Collect all the type attributes from the declarator itself.
518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *attr = state.getDeclarator().getAttributes();
520711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
521711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr->getNext();
523711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5245c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    // Do not distribute C++11 attributes. They have strict rules for what
5255c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    // they appertain to.
5265c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    if (attr->isCXX11Attribute())
5275c52166525f8714c3e3a979b156ec23426947fd3Richard Smith      continue;
5285c52166525f8714c3e3a979b156ec23426947fd3Richard Smith
529711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr->getKind()) {
530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
531711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
532711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5348e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
5354e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
536f85e193739c953358c865005855253af4f68a497John McCall        break;
537f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough
538f85e193739c953358c865005855253af4f68a497John McCall
539711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
540711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
541711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
542711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
543711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    default:
544711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
545711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
546711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attr = next));
547711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
548711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
549711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Add a synthetic '()' to a block-literal declarator if it is
550711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// required, given the return type.
551711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void maybeSynthesizeBlockSignature(TypeProcessingState &state,
552711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType declSpecType) {
553711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
554711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
555711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // First, check whether the declarator would produce a function,
556711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // i.e. whether the innermost semantic chunk is a function.
557711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.isFunctionDeclarator()) {
558711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // If so, make that declarator a prototyped declarator.
559711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.getFunctionTypeInfo().hasPrototype = true;
560711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
561711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
562711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
563da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // If there are any type objects, the type as written won't name a
564da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // function, regardless of the decl spec type.  This is because a
565da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // block signature declarator is always an abstract-declarator, and
566da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // abstract-declarators can't just be parentheses chunks.  Therefore
567da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // we need to build a function chunk unless there are no type
568da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // objects and the decl spec type is a function.
569711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
570711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
571711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
572da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // Note that there *are* cases with invalid declarators where
573da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // declarators consist solely of parentheses.  In general, these
574da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // occur only in failed efforts to make function declarators, so
575da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // faking up the function chunk is still the right thing to do.
576711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
577711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, we need to fake up a function declarator.
57896a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar  SourceLocation loc = declarator.getLocStart();
579711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
580711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // ...and *prepend* it to the declarator.
58159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara  SourceLocation NoLoc;
582711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
58359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*HasProto=*/true,
58459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*IsAmbiguous=*/false,
58559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*LParenLoc=*/NoLoc,
58659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ArgInfo=*/0,
58759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NumArgs=*/0,
58859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*EllipsisLoc=*/NoLoc,
58959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RParenLoc=*/NoLoc,
59059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*TypeQuals=*/0,
59159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RefQualifierIsLvalueRef=*/true,
59259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RefQualifierLoc=*/NoLoc,
59359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ConstQualifierLoc=*/NoLoc,
59459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*VolatileQualifierLoc=*/NoLoc,
59559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*MutableLoc=*/NoLoc,
59659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             EST_None,
59759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ESpecLoc=*/NoLoc,
59859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*Exceptions=*/0,
59959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ExceptionRanges=*/0,
60059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NumExceptions=*/0,
60159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NoexceptExpr=*/0,
60259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             loc, loc, declarator));
603711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
604711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // For consistency, make sure the state still has us as processing
605711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the decl spec.
606711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
607711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.setCurrentChunkIndex(declarator.getNumTypeObjects());
60804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
60904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
610930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
611930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
6121dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// \param state Specifies the declarator containing the declaration specifier
6131dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// to be converted, along with other associated processing state.
6145153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
6155153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
6168cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType ConvertDeclSpecToType(TypeProcessingState &state) {
6175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
6185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
619711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
6208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
621711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
622711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const DeclSpec &DS = declarator.getDeclSpec();
623711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation DeclLoc = declarator.getIdentifierLoc();
6245db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
62596a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar    DeclLoc = DS.getLocStart();
62691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
627711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  ASTContext &Context = S.Context;
6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6295db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
6305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
63196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
63296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
63396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
6345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
6355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
636fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
638fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
6395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
6405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
6415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
642fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
6435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
644958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
64564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
64664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
64764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
64864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
649711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
650f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
65164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
65264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
65364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
65464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
655711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
656f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
65764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
65864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
65964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
660f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
661f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
662f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
663f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
664f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
665f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
666f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
667f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
668f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
669f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
670d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
67162f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
672097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
673c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
67431ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                         (ObjCProtocolDecl*const*)PQ,
675c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         DS.getNumProtocolQualifiers());
676c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectPointerType(Result);
67762f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
67862f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
67991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
6805db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
6815db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
682f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // The declspec is always missing in a lambda expr context; it is either
683f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // specified with a trailing return type or inferred.
684f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    if (declarator.getContext() == Declarator::LambdaExprContext ||
685f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman        isOmittedBlockReturnType(declarator)) {
6865db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      Result = Context.DependentTy;
6875db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
6885db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
6891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
690d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
691d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
692d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
693d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
694d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
695d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
696d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
6974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().ImplicitInt) {
69835d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
69935d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
7003f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
701711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_declspec)
7023f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
70396a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        << FixItHint::CreateInsertion(DS.getLocStart(), "int");
7043f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
7054310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
706d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
707d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
708d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
709d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
7104310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
7114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (S.getLangOpts().CPlusPlus &&
7124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          !S.getLangOpts().MicrosoftExt) {
713711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_missing_type_specifier)
7143f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
7151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
716b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
717b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
718b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
719711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
720b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
721711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_type_specifier)
7223f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
723b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
724d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
7251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
7273cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
7285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
730fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
731fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
732fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
733311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
734311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
73591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
736e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
737e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
738e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
739e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
74080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
741e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
742e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
743e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
744e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
745311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
7485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
749fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
750fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
751fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
752311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
753311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
75491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
755e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
756e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
757e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
758e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
75980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
760e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
761e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
762e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
763e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
764311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
7655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
7665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
767958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7683cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
7695a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith  case DeclSpec::TST_int128:
77084268904947ada7e251932a6f5b0f4364df7a2c7Richard Smith    if (!S.PP.getTargetInfo().hasInt128Type())
77184268904947ada7e251932a6f5b0f4364df7a2c7Richard Smith      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_int128_unsupported);
7725a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    if (DS.getTypeSpecSign() == DeclSpec::TSS_unsigned)
7735a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.UnsignedInt128Ty;
7745a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    else
7755a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.Int128Ty;
7765a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    break;
777aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case DeclSpec::TST_half: Result = Context.HalfTy; break;
778fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
779958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
780958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
781fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
782958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
783fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
78439d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne
7854e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
78639d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
78739d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      declarator.setInvalidType(true);
78839d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    }
789958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
790fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
7915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
7925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
7935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
794711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
7958f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
796711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
7978f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
79899dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
7995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
8005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
8016666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_struct:
8026666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_interface: {
803b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
8046e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
8056e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
8066e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
807711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
8086e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
8096e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
8106e24726524c2b51b31bb4b622aa678a46b024f42John McCall
811a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
8120daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc());
81391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
8145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
815a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
81691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
8175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
818a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
8192191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
8200daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    // In both C and C++, make an ElaboratedType.
8210daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    ElaboratedTypeKeyword Keyword
8220daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
8230daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
824958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
8251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
8261a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
8275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
8285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
8295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
830711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
83127940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    if (Result.isNull())
832711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
83327940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    else if (DeclSpec::ProtocolQualifierListTy PQ
83427940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall               = DS.getProtocolQualifiers()) {
835c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
836c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // Silently drop any existing protocol qualifiers.
837c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // TODO: determine whether that's the right thing to do.
838c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjT->getNumProtocols())
839c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = ObjT->getBaseType();
840c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
841c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (DS.getNumProtocolQualifiers())
842c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = Context.getObjCObjectType(Result,
84331ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                             (ObjCProtocolDecl*const*) PQ,
844c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             DS.getNumProtocolQualifiers());
845c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCIdType()) {
846ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
847c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
84831ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                           (ObjCProtocolDecl*const*) PQ,
849c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
850c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
851c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCClassType()) {
8524262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
853c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
85431ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                           (ObjCProtocolDecl*const*) PQ,
855c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
856c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
8573f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
858711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
8593f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
860711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
8613f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
862c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
8631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
865958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
867958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
868e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
869711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
870958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
871730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian    if (!Result->isDependentType())
872730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian      if (const TagType *TT = Result->getAs<TagType>())
873711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
874d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
875fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
876958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
877d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
878b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
879d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
880d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
881711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
8824b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
8834b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
884711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
8854b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
886958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
887d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
8886fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
889b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
8906fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
8916fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
892711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
893af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
894af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
895711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
896af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
8976fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
8986fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
899ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case DeclSpec::TST_underlyingType:
900db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    Result = S.GetTypeFromParser(DS.getRepAsType());
901db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    assert(!Result.isNull() && "Didn't get a type for __underlying_type?");
902ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    Result = S.BuildUnaryTransformType(Result,
903ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::EnumUnderlyingType,
904ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       DS.getTypeSpecTypeLoc());
905ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (Result.isNull()) {
906ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Result = Context.IntTy;
907ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      declarator.setInvalidType(true);
908db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    }
90991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
910db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
911e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
912e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
91334b41d939a1328f484511c6002ba2456db879a29Richard Smith    Result = Context.getAutoType(QualType());
914e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
915e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
9161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
917a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall  case DeclSpec::TST_unknown_anytype:
918a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    Result = Context.UnknownAnyTy;
919a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    break;
920a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall
921b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case DeclSpec::TST_atomic:
922b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.GetTypeFromParser(DS.getRepAsType());
923b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(!Result.isNull() && "Didn't get a type for _Atomic?");
924b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc());
925b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Result.isNull()) {
926b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Result = Context.IntTy;
927b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      declarator.setInvalidType(true);
928b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
92991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
930b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
931b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_t:
932b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dTy;
933b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
934b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
935b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_array_t:
936b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dArrayTy;
937b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
938b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
939b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_buffer_t:
940b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dBufferTy;
941b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
942b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
943b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_t:
944b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dTy;
945b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
946b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
947b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_array_t:
948b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dArrayTy;
949b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
950b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
951b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image3d_t:
952b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage3dTy;
953b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
954b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
95521f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei  case DeclSpec::TST_sampler_t:
95621f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    Result = Context.OCLSamplerTy;
95721f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    break;
95821f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei
959e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei  case DeclSpec::TST_event_t:
960e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    Result = Context.OCLEventTy;
961e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    break;
962e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei
963809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
9645153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
965711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
9665153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
9675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
969958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
970f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
9714e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().Freestanding)
972711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
973fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
97482287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
97582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
97682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
977e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
978788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
979e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
980788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
981e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
982e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
983f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
9841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
98547423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
98647423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
987711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
9881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
989711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
990711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
991711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
992711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
993711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
994711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
995711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
996711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
997711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (AttributeList *attrs = DS.getAttributes().getList())
998f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    processTypeAttrs(state, Result, TAL_DeclSpec, attrs);
9991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
100096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
100196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
100296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
100396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
100496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
100596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
10060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (TypeQuals & DeclSpec::TQ_restrict) {
10072b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian      if (Result->isAnyPointerType() || Result->isReferenceType()) {
10082b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        QualType EltTy;
10092b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        if (Result->isObjCObjectPointerType())
10102b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result;
10112b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        else
10122b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result->isPointerType() ?
10132b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<PointerType>()->getPointeeType() :
10142b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<ReferenceType>()->getPointeeType();
10151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1016bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
1017bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
1018bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
1019711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getRestrictSpecLoc(),
1020d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
1021d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
10220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall          TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
1023bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
1024bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
1025711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DS.getRestrictSpecLoc(),
1026711c52bb20d0c69063b52a99826fb7d2835501f1John McCall               diag::err_typecheck_invalid_restrict_not_pointer)
1027d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
10280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
102996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
103096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
10311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
103296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
103396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
103496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
103596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
103696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
103796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
10380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
103996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
10400953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
104196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
10420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
10430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        assert((TypeQuals & DeclSpec::TQ_restrict) &&
10440953e767ff7817f97b3ab20896b229891eeff45bJohn McCall               "Has CVR quals but not C, V, or R?");
10450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Loc = DS.getRestrictSpecLoc();
104696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
1047711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(Loc, diag::warn_typecheck_function_qualifiers)
1048d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
104996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
10501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1051f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
1052f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
1053f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
1054f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
1055f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
10561a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
10571a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
1058f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
10590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
10600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
10611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
10621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1063bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // C90 6.5.3 constraints: "The same type qualifier shall not appear more
1064bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // than once in the same specifier-list or qualifier-list, either directly
1065bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // or via one or more typedefs."
106691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus
1067bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman        && TypeQuals & Result.getCVRQualifiers()) {
1068bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()) {
106991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec)
1070bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "const";
1071bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1072bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
1073bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()) {
107491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec)
1075bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "volatile";
1076bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1077bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
1078bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      // C90 doesn't have restrict, so it doesn't force us to produce a warning
1079bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      // in this case.
1080bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    }
1081bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
10820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
10830953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Result = Context.getQualifiedType(Result, Quals);
108496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
10850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1086f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
1087f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
1088f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
1089cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
1090cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
1091cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
10921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1093cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
1094cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1095cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
10962865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
10972865474261a608c7873b87ba4af110d17907896dJohn McCall                                  Qualifiers Qs) {
10982865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
10992865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
11002865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
11012865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
11022865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
11032865474261a608c7873b87ba4af110d17907896dJohn McCall
11042865474261a608c7873b87ba4af110d17907896dJohn McCall    const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
11052865474261a608c7873b87ba4af110d17907896dJohn McCall    if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
11062865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
11072865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
11082865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
11092865474261a608c7873b87ba4af110d17907896dJohn McCall      }
11102865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
11112865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
11122865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
11132865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
11142865474261a608c7873b87ba4af110d17907896dJohn McCall      }
11152865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
11162865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
11172865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
11182865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
111991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
11202865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (!Ty->isDependentType()) {
11212865474261a608c7873b87ba4af110d17907896dJohn McCall      // FIXME: this deserves a proper diagnostic
11222865474261a608c7873b87ba4af110d17907896dJohn McCall      DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
11232865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
11242865474261a608c7873b87ba4af110d17907896dJohn McCall    }
11252865474261a608c7873b87ba4af110d17907896dJohn McCall
11262865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
11272865474261a608c7873b87ba4af110d17907896dJohn McCall      Diag(Loc, DiagID) << ProblemTy;
11282865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
11292865474261a608c7873b87ba4af110d17907896dJohn McCall    }
11302865474261a608c7873b87ba4af110d17907896dJohn McCall  }
11312865474261a608c7873b87ba4af110d17907896dJohn McCall
11322865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
11332865474261a608c7873b87ba4af110d17907896dJohn McCall}
11342865474261a608c7873b87ba4af110d17907896dJohn McCall
1135075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
1136075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
1137075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
1138075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
1139075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
1140f85e193739c953358c865005855253af4f68a497John McCall/// Given that we're building a pointer or reference to the given
1141f85e193739c953358c865005855253af4f68a497John McCallstatic QualType inferARCLifetimeForPointee(Sema &S, QualType type,
1142f85e193739c953358c865005855253af4f68a497John McCall                                           SourceLocation loc,
1143f85e193739c953358c865005855253af4f68a497John McCall                                           bool isReference) {
1144f85e193739c953358c865005855253af4f68a497John McCall  // Bail out if retention is unrequired or already specified.
1145f85e193739c953358c865005855253af4f68a497John McCall  if (!type->isObjCLifetimeType() ||
1146f85e193739c953358c865005855253af4f68a497John McCall      type.getObjCLifetime() != Qualifiers::OCL_None)
1147f85e193739c953358c865005855253af4f68a497John McCall    return type;
1148f85e193739c953358c865005855253af4f68a497John McCall
1149f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None;
1150f85e193739c953358c865005855253af4f68a497John McCall
1151f85e193739c953358c865005855253af4f68a497John McCall  // If the object type is const-qualified, we can safely use
1152f85e193739c953358c865005855253af4f68a497John McCall  // __unsafe_unretained.  This is safe (because there are no read
1153f85e193739c953358c865005855253af4f68a497John McCall  // barriers), and it'll be safe to coerce anything but __weak* to
1154f85e193739c953358c865005855253af4f68a497John McCall  // the resulting type.
1155f85e193739c953358c865005855253af4f68a497John McCall  if (type.isConstQualified()) {
1156f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
1157f85e193739c953358c865005855253af4f68a497John McCall
1158f85e193739c953358c865005855253af4f68a497John McCall  // Otherwise, check whether the static type does not require
1159f85e193739c953358c865005855253af4f68a497John McCall  // retaining.  This currently only triggers for Class (possibly
1160f85e193739c953358c865005855253af4f68a497John McCall  // protocol-qualifed, and arrays thereof).
1161f85e193739c953358c865005855253af4f68a497John McCall  } else if (type->isObjCARCImplicitlyUnretainedType()) {
1162f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
11635b76f373d23cc3b292ecf523349aaaa388eea375Argyrios Kyrtzidis
1164ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // If we are in an unevaluated context, like sizeof, skip adding a
1165ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // qualification.
116671f55f771794674a410171dbf3cb5dbedf95d033David Blaikie  } else if (S.isUnevaluatedContext()) {
1167ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    return type;
1168f85e193739c953358c865005855253af4f68a497John McCall
1169e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // If that failed, give an error and recover using __strong.  __strong
1170e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // is the option most likely to prevent spurious second-order diagnostics,
1171e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // like when binding a reference to a field.
1172f85e193739c953358c865005855253af4f68a497John McCall  } else {
1173f85e193739c953358c865005855253af4f68a497John McCall    // These types can show up in private ivars in system headers, so
1174f85e193739c953358c865005855253af4f68a497John McCall    // we need this to not be an error in those cases.  Instead we
1175f85e193739c953358c865005855253af4f68a497John McCall    // want to delay.
1176f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1177ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.DelayedDiagnostics.add(
1178ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman          sema::DelayedDiagnostic::makeForbiddenType(loc,
1179ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman              diag::err_arc_indirect_no_ownership, type, isReference));
1180f85e193739c953358c865005855253af4f68a497John McCall    } else {
1181ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference;
1182f85e193739c953358c865005855253af4f68a497John McCall    }
1183e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall    implicitLifetime = Qualifiers::OCL_Strong;
1184f85e193739c953358c865005855253af4f68a497John McCall  }
1185f85e193739c953358c865005855253af4f68a497John McCall  assert(implicitLifetime && "didn't infer any lifetime!");
1186f85e193739c953358c865005855253af4f68a497John McCall
1187f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers qs;
1188f85e193739c953358c865005855253af4f68a497John McCall  qs.addObjCLifetime(implicitLifetime);
1189f85e193739c953358c865005855253af4f68a497John McCall  return S.Context.getQualifiedType(type, qs);
1190f85e193739c953358c865005855253af4f68a497John McCall}
1191f85e193739c953358c865005855253af4f68a497John McCall
1192cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
1193cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1194cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
1195cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1196cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1197cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
1198cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
1199cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1200cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
1201cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1202cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1203cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
1204cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
12052865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
1206cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
1207cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
1208cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
1209cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
1210ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1211cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1212cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1213cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1214c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
121592e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
1216f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build pointers to unqualified pointers.
12174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1218f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false);
1219f85e193739c953358c865005855253af4f68a497John McCall
1220cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
12212865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
1222cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1223cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1224cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
1225cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1226cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
1227cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1228cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1229cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
1230cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
1231cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1232cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
1233cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1234cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1235cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
1236cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
123754e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
12382865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
123954e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
124091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  assert(Context.getCanonicalType(T) != Context.OverloadTy &&
12419625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
124291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
124369d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // C++0x [dcl.ref]p6:
124491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   If a typedef (7.1.3), a type template-parameter (14.3.1), or a
124591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
124691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   type T, an attempt to create the type "lvalue reference to cv TR" creates
124791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   the type "lvalue reference to T", while an attempt to create the type
124869d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   "rvalue reference to cv TR" creates the type TR.
124954e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
125054e14c4db764c0636160d26c5bbf491637c83a76John McCall
125154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
125254e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
125354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
125454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
125554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
125654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
125754e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
125854e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
125954e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
126054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
126154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
126269d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // collapsing of references-to-references as described in C++0x.
126369d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // DR 106 and 540 introduce reference-collapsing into C++98/03.
1264cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1265cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
126633a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
1267cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
1268cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
1269cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
1270cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1271cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1272cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1273f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build references to unqualified pointers.
12744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1275f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true);
1276f85e193739c953358c865005855253af4f68a497John McCall
1277cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
12787c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
12792865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
12802865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
1281cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1282cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1283e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// Check whether the specified array size makes the array type a VLA.  If so,
1284e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// return true, if not, return the size of the array in SizeVal.
1285282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smithstatic bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal) {
1286282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode
1287282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // (like gnu99, but not c99) accept any evaluatable value as an extension.
1288ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  class VLADiagnoser : public Sema::VerifyICEDiagnoser {
1289ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  public:
1290ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    VLADiagnoser() : Sema::VerifyICEDiagnoser(true) {}
129191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1292ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) {
1293ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
129491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1295ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseFold(Sema &S, SourceLocation Loc, SourceRange SR) {
1296ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      S.Diag(Loc, diag::ext_vla_folded_to_constant) << SR;
1297ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
1298ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  } Diagnoser;
129991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1300ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  return S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser,
1301ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                           S.LangOpts.GNUMode).isInvalid();
1302e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner}
1303e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1304e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1305cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
1306cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1307cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
1308cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1309cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
13101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
13111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
1312cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1313efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Brackets The range from the opening '[' to the closing ']'.
1314cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1315cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
1316cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1317cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1318cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
1319cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
1320cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1321cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
13227e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
13230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
13247e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
13254e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
1326138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
1327138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
132891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   type, the (possibly cv-qualified) type void, a function type or an
1329138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
1330138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
1331bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    // C++ [dcl.array]p3:
1332bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   When several "array of" specifications are adjacent, [...] only the
1333bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   first of the constant expressions that specify the bounds of the arrays
1334bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   may be omitted.
1335bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //
1336138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
1337138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
1338138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
1339138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
1340138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1341138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
134291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1343bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    if (T->isVoidType() || T->isIncompleteArrayType()) {
1344923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1345923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1346923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
134791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
134891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (RequireNonAbstractType(Brackets.getBegin(), T,
1349138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
1350138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
135191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1352923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
1353138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1354138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
1355923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
1356923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
1357923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1358923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
1359cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1360cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
1361cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
1362ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1363cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1364cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
13651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
136634b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (T->getContainedAutoType()) {
136734b41d939a1328f484511c6002ba2456db879a29Richard Smith    Diag(Loc, diag::err_illegal_decl_array_of_auto)
136834b41d939a1328f484511c6002ba2456db879a29Richard Smith      << getPrintableNameForEntity(Entity) << T;
1369e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
1370e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
13711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13726217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
1373cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
1374cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
1375cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
1376cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
1377c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
1378c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1379c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
1380cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
13811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1382806054db6653d29cb0d9692df3612cbcd03d0530John McCall  // Do placeholder conversions on the array size expression.
1383806054db6653d29cb0d9692df3612cbcd03d0530John McCall  if (ArraySize && ArraySize->hasPlaceholderType()) {
1384806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ExprResult Result = CheckPlaceholderExpr(ArraySize);
1385806054db6653d29cb0d9692df3612cbcd03d0530John McCall    if (Result.isInvalid()) return QualType();
1386806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ArraySize = Result.take();
1387806054db6653d29cb0d9692df3612cbcd03d0530John McCall  }
1388806054db6653d29cb0d9692df3612cbcd03d0530John McCall
13895e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
1390429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ArraySize && !ArraySize->isRValue()) {
1391429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult Result = DefaultLvalueConversion(ArraySize);
1392429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Result.isInvalid())
1393429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return QualType();
1394429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
1395429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ArraySize = Result.take();
1396429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
13975e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1398cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
1399282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // C++11 allows contextual conversions to such types.
140080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (!getLangOpts().CPlusPlus11 &&
1401282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      ArraySize && !ArraySize->isTypeDependent() &&
14021274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1403cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1404cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
1405cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1406cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1407282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
14082767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
1409cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
1410f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
14117e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
1412f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
1413f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
1414ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
14157e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
1416282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  } else if ((!T->isDependentType() && !T->isIncompleteType() &&
1417282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith              !T->isConstantSizeType()) ||
1418282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith             isArraySizeVLA(*this, ArraySize, ConstVal)) {
1419282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // Even in C++11, don't allow contextual conversions in the array bound
1420282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // of a VLA.
142180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (getLangOpts().CPlusPlus11 &&
1422282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1423282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1424282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        << ArraySize->getType() << ArraySize->getSourceRange();
1425282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      return QualType();
1426282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    }
1427282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
1428e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with an element type that has a non-constant-size is a VLA.
1429e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with a non-ICE size is a VLA.  We accept any expression
1430e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // that we can fold to a non-zero positive value as an extension.
14317e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1432cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
1433cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1434cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
1435923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
1436b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
1437b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1438b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1439b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
1440b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1441b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
1442923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1443923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1444923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
144502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
144602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
144791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      Diag(ArraySize->getLocStart(),
144802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
144902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
1450923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
145120cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne
145220cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      if (ASM == ArrayType::Static) {
145320cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        Diag(ArraySize->getLocStart(),
145420cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne             diag::warn_typecheck_zero_static_array_size)
145520cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne          << ArraySize->getSourceRange();
145620cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        ASM = ArrayType::Normal;
145720cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      }
145891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
14592767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor               !T->isIncompleteType()) {
146091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // Is the array too large?
14612767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
14622767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
14632767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
14642767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
14652767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
14662767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
14671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
146891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
146946a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
1470cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1471617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly
1472617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  // OpenCL v1.2 s6.9.d: variable length arrays are not supported.
1473617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  if (getLangOpts().OpenCL && T->isVariableArrayType()) {
1474617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    Diag(Loc, diag::err_opencl_vla);
1475617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly    return QualType();
1476617bb317a7aeb6c3468a4170a5d6c1058da7cea1Joey Gouly  }
1477af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
14784e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().C99) {
14790fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
14800fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
1481f85e193739c953358c865005855253af4f68a497John McCall      QualType BaseT = Context.getBaseElementType(T);
148291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      if (!T->isDependentType() &&
14833c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT.isPODType(Context) &&
14843c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT->isObjCLifetimeType()) {
14850fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
1486f85e193739c953358c865005855253af4f68a497John McCall          << BaseT;
14870fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
148891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
1489a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
1490a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
1491a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
1492a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
1493a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
14940fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
14950fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
14960fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::ext_vla);
14970fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
1498d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith      Diag(Loc,
14994e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie           getLangOpts().CPlusPlus? diag::err_c99_array_usage_cxx
1500d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith                                     : diag::ext_c99_array_usage) << ASM;
1501cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1502cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1503630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko  if (T->isVariableArrayType()) {
1504630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko    // Warn about VLAs for -Wvla.
1505630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko    Diag(Loc, diag::warn_vla_used);
1506630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko  }
1507630f4bb9f12e330438281c4e46deb6656620b73aDmitri Gribenko
1508cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
1509cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
15109cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
15119cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
15129cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
15139cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
15149ae2f076ca5ab1feb3ba95629099ec2319833701John McCallQualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
15159cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
15169cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
15179cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
15181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
15199cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
15209cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
15219cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
15229cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
15239cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
15249ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
15259cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
15269ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
15279cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
15289ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        << "ext_vector_type" << ArraySize->getSourceRange();
15299cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
15309cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
15311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
15339cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
15341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
15351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15369cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
15379cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
15389ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      << ArraySize->getSourceRange();
15399cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
15409cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
15411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15424ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    return Context.getExtVectorType(T, vectorSize);
15431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
15441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15459ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
15469cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
15471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1548724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
1549724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1550724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
1551724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
1552724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
15532943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
1554724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
1555724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1556724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
1557724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1558724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
1559724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
1560724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
1561724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1562724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
1563724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1564724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
1565724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1566eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith/// \param HasTrailingReturn Whether this function has a trailing return type.
1567eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith///
1568724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
1569724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1570724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
1571724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
1572724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
1573724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1574724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
1575724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
1576724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1577724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
1578724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
1579724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
15801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 QualType *ParamTypes,
1581724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
1582eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith                                 bool Variadic, bool HasTrailingReturn,
1583eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith                                 unsigned Quals,
1584c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                                 RefQualifierKind RefQualifier,
1585fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
1586e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                 FunctionType::ExtInfo Info) {
1587724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
158891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    Diag(Loc, diag::err_func_returning_array_function)
158958408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
1590724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1591724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1592aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1593aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // Functions cannot return half FP.
1594aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  if (T->isHalfType()) {
1595aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 <<
1596aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      FixItHint::CreateInsertion(Loc, "*");
1597aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    return QualType();
1598aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  }
1599aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1600724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
1601724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
1602aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    // FIXME: Loc is too inprecise here, should use proper locations for args.
160379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]);
16042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
1605724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
1606724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
1607aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    } else if (ParamType->isHalfType()) {
1608aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Disallow half FP arguments.
1609aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 <<
1610aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        FixItHint::CreateInsertion(Loc, "*");
1611aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Invalid = true;
1612724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
1613cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
161454e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
1615724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1616724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1617724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
1618724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1619724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1620e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
1621e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.Variadic = Variadic;
1622eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith  EPI.HasTrailingReturn = HasTrailingReturn;
1623e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.TypeQuals = Quals;
1624c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor  EPI.RefQualifier = RefQualifier;
1625e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.ExtInfo = Info;
1626e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1627e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI);
1628724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
16291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1630949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
1631949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1632949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
1633949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
1634949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
1635949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
1636949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1637949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
1638949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
16391eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
16402865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
1641949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
1642949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
1643949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
1644949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
1645949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
1646949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1647949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
1648949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
1649949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1650949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
1651949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
1652949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
1653949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1654949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1655737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
1656949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
1657949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
16588d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
1659ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
1660949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1661949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1662949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1663949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
1664949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1665949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
1666949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1667949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1668949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1669949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
1670949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1671949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1672949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1673949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1674b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // C++ allows the class type in a member pointer to be an incomplete type.
1675b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // In the Microsoft ABI, the size of the member pointer can vary
1676b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // according to the class type, which means that we really need a
1677b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // complete type if possible, which means we need to instantiate templates.
1678b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  //
1679b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // For now, just require a complete type, which will instantiate
1680b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // templates.  This will also error if the type is just forward-declared,
1681b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // which is a bug, but it's a bug that saves us from dealing with some
1682b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  // complexities at the moment.
1683b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  if (Context.getTargetInfo().getCXXABI().isMicrosoft() &&
1684679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos      RequireCompleteType(Loc, Class, diag::err_incomplete_type))
1685679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos    return QualType();
1686679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos
16872865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
1688949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
16891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16909a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
16919a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
16929a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
16939a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
1694efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Loc The source location, used for diagnostics.
16959a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
16969a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
16979a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
16989a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
16999a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
17009a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
170191cbbbf506c892a26d4301e2b3ccd377b0938817Chad RosierQualType Sema::BuildBlockPointerType(QualType T,
17021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
17039a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
17040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
17059a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
17069a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
17079a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
17081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17092865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
17109a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
17119a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
1712b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1713b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
17143f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
1715a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
17163f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
17173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
17183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1719a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
1720f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
1721e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
1722a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
1723e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
17241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1725a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
1726e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
1727e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
1728e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
1729a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
1730a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
1731a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            unsigned chunkIndex);
1732a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis
1733f85e193739c953358c865005855253af4f68a497John McCall/// Given that this is the declaration of a parameter under ARC,
1734f85e193739c953358c865005855253af4f68a497John McCall/// attempt to infer attributes and such for pointer-to-whatever
1735f85e193739c953358c865005855253af4f68a497John McCall/// types.
1736f85e193739c953358c865005855253af4f68a497John McCallstatic void inferARCWriteback(TypeProcessingState &state,
1737f85e193739c953358c865005855253af4f68a497John McCall                              QualType &declSpecType) {
1738f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
1739f85e193739c953358c865005855253af4f68a497John McCall  Declarator &declarator = state.getDeclarator();
1740f85e193739c953358c865005855253af4f68a497John McCall
1741f85e193739c953358c865005855253af4f68a497John McCall  // TODO: should we care about decl qualifiers?
1742f85e193739c953358c865005855253af4f68a497John McCall
1743f85e193739c953358c865005855253af4f68a497John McCall  // Check whether the declarator has the expected form.  We walk
1744f85e193739c953358c865005855253af4f68a497John McCall  // from the inside out in order to make the block logic work.
1745f85e193739c953358c865005855253af4f68a497John McCall  unsigned outermostPointerIndex = 0;
1746f85e193739c953358c865005855253af4f68a497John McCall  bool isBlockPointer = false;
1747f85e193739c953358c865005855253af4f68a497John McCall  unsigned numPointers = 0;
1748f85e193739c953358c865005855253af4f68a497John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
1749f85e193739c953358c865005855253af4f68a497John McCall    unsigned chunkIndex = i;
1750f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex);
1751f85e193739c953358c865005855253af4f68a497John McCall    switch (chunk.Kind) {
1752f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Paren:
1753f85e193739c953358c865005855253af4f68a497John McCall      // Ignore parens.
1754f85e193739c953358c865005855253af4f68a497John McCall      break;
1755f85e193739c953358c865005855253af4f68a497John McCall
1756f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Reference:
1757f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Pointer:
1758f85e193739c953358c865005855253af4f68a497John McCall      // Count the number of pointers.  Treat references
1759f85e193739c953358c865005855253af4f68a497John McCall      // interchangeably as pointers; if they're mis-ordered, normal
1760f85e193739c953358c865005855253af4f68a497John McCall      // type building will discover that.
1761f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1762f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1763f85e193739c953358c865005855253af4f68a497John McCall      break;
1764f85e193739c953358c865005855253af4f68a497John McCall
1765f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::BlockPointer:
1766f85e193739c953358c865005855253af4f68a497John McCall      // If we have a pointer to block pointer, that's an acceptable
1767f85e193739c953358c865005855253af4f68a497John McCall      // indirect reference; anything else is not an application of
1768f85e193739c953358c865005855253af4f68a497John McCall      // the rules.
1769f85e193739c953358c865005855253af4f68a497John McCall      if (numPointers != 1) return;
1770f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1771f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1772f85e193739c953358c865005855253af4f68a497John McCall      isBlockPointer = true;
1773f85e193739c953358c865005855253af4f68a497John McCall
1774f85e193739c953358c865005855253af4f68a497John McCall      // We don't care about pointer structure in return values here.
1775f85e193739c953358c865005855253af4f68a497John McCall      goto done;
1776f85e193739c953358c865005855253af4f68a497John McCall
1777f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Array: // suppress if written (id[])?
1778f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Function:
1779f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::MemberPointer:
1780f85e193739c953358c865005855253af4f68a497John McCall      return;
1781f85e193739c953358c865005855253af4f68a497John McCall    }
1782f85e193739c953358c865005855253af4f68a497John McCall  }
1783f85e193739c953358c865005855253af4f68a497John McCall done:
1784f85e193739c953358c865005855253af4f68a497John McCall
1785f85e193739c953358c865005855253af4f68a497John McCall  // If we have *one* pointer, then we want to throw the qualifier on
1786f85e193739c953358c865005855253af4f68a497John McCall  // the declaration-specifiers, which means that it needs to be a
1787f85e193739c953358c865005855253af4f68a497John McCall  // retainable object type.
1788f85e193739c953358c865005855253af4f68a497John McCall  if (numPointers == 1) {
1789f85e193739c953358c865005855253af4f68a497John McCall    // If it's not a retainable object type, the rule doesn't apply.
1790f85e193739c953358c865005855253af4f68a497John McCall    if (!declSpecType->isObjCRetainableType()) return;
1791f85e193739c953358c865005855253af4f68a497John McCall
1792f85e193739c953358c865005855253af4f68a497John McCall    // If it already has lifetime, don't do anything.
1793f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType.getObjCLifetime()) return;
1794f85e193739c953358c865005855253af4f68a497John McCall
1795f85e193739c953358c865005855253af4f68a497John McCall    // Otherwise, modify the type in-place.
1796f85e193739c953358c865005855253af4f68a497John McCall    Qualifiers qs;
179791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1798f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType->isObjCARCImplicitlyUnretainedType())
1799f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
1800f85e193739c953358c865005855253af4f68a497John McCall    else
1801f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing);
1802f85e193739c953358c865005855253af4f68a497John McCall    declSpecType = S.Context.getQualifiedType(declSpecType, qs);
1803f85e193739c953358c865005855253af4f68a497John McCall
1804f85e193739c953358c865005855253af4f68a497John McCall  // If we have *two* pointers, then we want to throw the qualifier on
1805f85e193739c953358c865005855253af4f68a497John McCall  // the outermost pointer.
1806f85e193739c953358c865005855253af4f68a497John McCall  } else if (numPointers == 2) {
1807f85e193739c953358c865005855253af4f68a497John McCall    // If we don't have a block pointer, we need to check whether the
1808f85e193739c953358c865005855253af4f68a497John McCall    // declaration-specifiers gave us something that will turn into a
1809f85e193739c953358c865005855253af4f68a497John McCall    // retainable object pointer after we slap the first pointer on it.
1810f85e193739c953358c865005855253af4f68a497John McCall    if (!isBlockPointer && !declSpecType->isObjCObjectType())
1811f85e193739c953358c865005855253af4f68a497John McCall      return;
1812f85e193739c953358c865005855253af4f68a497John McCall
1813f85e193739c953358c865005855253af4f68a497John McCall    // Look for an explicit lifetime attribute there.
1814f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex);
18151c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis    if (chunk.Kind != DeclaratorChunk::Pointer &&
18161c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis        chunk.Kind != DeclaratorChunk::BlockPointer)
18171c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis      return;
1818f85e193739c953358c865005855253af4f68a497John McCall    for (const AttributeList *attr = chunk.getAttrs(); attr;
1819f85e193739c953358c865005855253af4f68a497John McCall           attr = attr->getNext())
18208e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt      if (attr->getKind() == AttributeList::AT_ObjCOwnership)
1821f85e193739c953358c865005855253af4f68a497John McCall        return;
1822f85e193739c953358c865005855253af4f68a497John McCall
1823a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis    transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing,
1824a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                          outermostPointerIndex);
1825f85e193739c953358c865005855253af4f68a497John McCall
1826f85e193739c953358c865005855253af4f68a497John McCall  // Any other number of pointers/references does not trigger the rule.
1827f85e193739c953358c865005855253af4f68a497John McCall  } else return;
1828f85e193739c953358c865005855253af4f68a497John McCall
1829f85e193739c953358c865005855253af4f68a497John McCall  // TODO: mark whether we did this inference?
1830f85e193739c953358c865005855253af4f68a497John McCall}
1831f85e193739c953358c865005855253af4f68a497John McCall
1832d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruthstatic void DiagnoseIgnoredQualifiers(unsigned Quals,
1833d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation ConstQualLoc,
1834d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation VolatileQualLoc,
1835d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation RestrictQualLoc,
1836d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      Sema& S) {
1837d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  std::string QualStr;
1838d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  unsigned NumQuals = 0;
1839d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  SourceLocation Loc;
1840d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1841d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint ConstFixIt;
1842d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint VolatileFixIt;
1843d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint RestrictFixIt;
1844d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1845a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg  const SourceManager &SM = S.getSourceManager();
1846a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg
1847d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to
1848d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // find a range and grow it to encompass all the qualifiers, regardless of
1849d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // the order in which they textually appear.
1850d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Const) {
1851d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc);
1852d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    QualStr = "const";
1853a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    ++NumQuals;
1854a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(ConstQualLoc, Loc))
1855a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = ConstQualLoc;
1856d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1857d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Volatile) {
1858d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc);
1859a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    QualStr += (NumQuals == 0 ? "volatile" : " volatile");
1860d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ++NumQuals;
1861a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(VolatileQualLoc, Loc))
1862a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = VolatileQualLoc;
1863d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1864d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Restrict) {
1865d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc);
1866a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    QualStr += (NumQuals == 0 ? "restrict" : " restrict");
1867d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ++NumQuals;
1868a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(RestrictQualLoc, Loc))
1869a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = RestrictQualLoc;
1870d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1871d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1872d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  assert(NumQuals > 0 && "No known qualifiers?");
1873d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1874d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  S.Diag(Loc, diag::warn_qual_return_type)
1875a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    << QualStr << NumQuals << ConstFixIt << VolatileFixIt << RestrictFixIt;
1876d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth}
1877d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
18788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
18798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                             TypeSourceInfo *&ReturnTypeInfo) {
18808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &SemaRef = state.getSema();
18818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
1882930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
18838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ReturnTypeInfo = 0;
1884711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
18858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // The TagDecl owned by the DeclSpec.
18868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TagDecl *OwnedTagDecl = 0;
18878999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl
18883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
188998a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian  case UnqualifiedId::IK_ImplicitSelfParam:
18903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
18918999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  case UnqualifiedId::IK_Identifier:
18920486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
18933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
18948cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = ConvertDeclSpecToType(state);
189591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1896591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
1897d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
189815987970eeaa1842c29ec8797affd1c1dea05585Abramo Bagnara      // Owned declaration is embedded in declarator.
1899d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl->setEmbeddedInDeclarator(true);
1900591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
1901930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
1902930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
19033f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
19040efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
19053f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
1906930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
190791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // "void" instead.
19088cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = SemaRef.Context.VoidTy;
1909a78a640c5f59720f2c2b8034eca4fbf8525d9026Rafael Espindola    if (AttributeList *attrs = D.getDeclSpec().getAttributes().getList())
1910f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclSpec, attrs);
1911930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
191248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
191348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
191448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
191548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
191691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId,
19178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  &ReturnTypeInfo);
191848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
1919930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
1920dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
1921711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
1922711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
1923711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1924d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
1925d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // In C++11, a function declarator using 'auto' must have a trailing return
19268110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // type (this is checked later) and we can skip this. In other languages
19278110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // using auto, we need to check regardless.
192834b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
192980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      (!SemaRef.getLangOpts().CPlusPlus11 || !D.isFunctionDeclarator())) {
1930baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
19311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1932baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
1933baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
1934b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("K&R type lists aren't allowed in C++");
1935f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
1936f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman      llvm_unreachable("Can't specify a type specifier in lambda grammar");
1937cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
1938cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
1939baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
1940baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
1941baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1942baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
19437a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)
19447a614d8380297fcd2bc23986241905d97222948cRichard Smith        break;
19458cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) {
1946eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie      case TTK_Enum: llvm_unreachable("unhandled tag kind");
1947465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
1948465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
1949465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
19506666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      case TTK_Interface: Error = 4; /* Interface member */ break;
19511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
1952baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1953baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
195417b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
19556666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 5; // Exception declaration
1956baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1957baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
19586666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 6; // Template parameter
1959baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1960baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
19616666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 7; // Block literal
196234b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
196334b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TemplateTypeArgContext:
19646666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 8; // Template type argument
196534b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
1966162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
19673e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
19686666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 10; // Type alias
1969162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      break;
19707796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
19716666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11; // Function return type
19727796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
197334b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TypeNameContext:
19746666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 12; // Generic
1975baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1976baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
1977baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
1978baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
1979baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
19800b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
1981baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1982baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1983baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
1984ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith    if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
19856666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 9;
1986ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith
19878110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    // In Objective-C it is an error to use 'auto' on a function declarator.
19888110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    if (D.isFunctionDeclarator())
19896666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11;
19908110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith
1991d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.spec.auto]p2: 'auto' is always fine if the declarator
1992e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // contains a trailing return type. That is only legal at the outermost
1993e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // level. Check all declarator chunks (outermost first) anyway, to give
1994e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // better diagnostics.
199580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (SemaRef.getLangOpts().CPlusPlus11 && Error != -1) {
1996e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1997e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned chunkIndex = e - i - 1;
1998e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        state.setCurrentChunkIndex(chunkIndex);
1999e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2000e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (DeclType.Kind == DeclaratorChunk::Function) {
2001e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
200254655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (FTI.hasTrailingReturnType()) {
2003e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Error = -1;
2004e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            break;
2005e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          }
2006e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        }
2007e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2008e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    }
2009e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2010baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
20118cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
20128cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_auto_not_allowed)
2013baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
20148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = SemaRef.Context.IntTy;
2015baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
20160aa86c0463a881be85fd34e04c7de3379997621dRichard Smith    } else
20170aa86c0463a881be85fd34e04c7de3379997621dRichard Smith      SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
20180aa86c0463a881be85fd34e04c7de3379997621dRichard Smith                   diag::warn_cxx98_compat_auto_type_specifier);
2019baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
20208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
20214e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().CPlusPlus &&
20225e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall      OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) {
20238cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // Check the contexts where C++ forbids the declaration of a new class
20248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // or enumeration in a type-specifier-seq.
20258cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    switch (D.getContext()) {
20267796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
20277796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // Class and enumeration definitions are syntactically not allowed in
20287796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // trailing return types.
20297796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      llvm_unreachable("parser should not have allowed this");
20307796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
20318cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::FileContext:
20328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::MemberContext:
20338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockContext:
20348cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ForContext:
20358cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockLiteralContext:
2036f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2037d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // C++11 [dcl.type]p3:
20388cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   A type-specifier-seq shall not define a class or enumeration unless
20398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   it appears in the type-id of an alias-declaration (7.1.3) that is not
20408cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   the declaration of a template-declaration.
20418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasDeclContext:
20428cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
20438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasTemplateContext:
20448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
20458cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_alias_template)
20468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2047601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
20488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
20498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TypeNameContext:
20508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateParamContext:
20518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXNewContext:
20528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXCatchContext:
20538cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ObjCCatchContext:
20548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateTypeArgContext:
20558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
20568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_type_specifier)
20578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2058601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
20598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
20608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::PrototypeContext:
2061cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2062cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
20638cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::KNRTypeListContext:
20648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ [dcl.fct]p6:
20658cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   Types shall not be defined in return or parameter types.
20668cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
20678cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_param_type)
20688cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2069601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
20708cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
20718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ConditionContext:
20728cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ 6.4p2:
20738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // The type-specifier-seq shall not contain typedef and shall not declare
20748cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // a new class or enumeration.
20758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
20768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_condition);
2077601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
20788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
20798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    }
20808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  }
20818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
20828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return T;
20838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
20848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2085a08c2fb74ef823c185619ecc532f8fced6a1982fBenjamin Kramerstatic std::string getFunctionQualifiersAsString(const FunctionProtoType *FnTy){
2086d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  std::string Quals =
2087d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
2088d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2089d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (FnTy->getRefQualifier()) {
2090d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_None:
2091d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2092d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2093d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_LValue:
2094d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2095d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2096d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += '&';
2097d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2098d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2099d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_RValue:
2100d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2101d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2102d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += "&&";
2103d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2104d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2105d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2106d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  return Quals;
2107d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2108d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2109d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// Check that the function type T, which has a cv-qualifier or a ref-qualifier,
2110d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// can be contained within the declarator chunk DeclType, and produce an
2111d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// appropriate diagnostic if not.
2112d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smithstatic void checkQualifiedFunction(Sema &S, QualType T,
2113d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                   DeclaratorChunk &DeclType) {
2114d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++98 [dcl.fct]p4 / C++11 [dcl.fct]p6: a function type with a
2115d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // cv-qualifier or a ref-qualifier can only appear at the topmost level
2116d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // of a type.
2117d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  int DiagKind = -1;
2118d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (DeclType.Kind) {
2119d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Paren:
2120d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::MemberPointer:
2121d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases are permitted.
2122d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2123d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Array:
2124d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Function:
2125d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases don't allow function types at all; no need to diagnose the
2126d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // qualifiers separately.
2127d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2128d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::BlockPointer:
2129d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 0;
2130d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2131d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Pointer:
2132d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 1;
2133d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2134d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Reference:
2135d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 2;
2136d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2137d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2138d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2139d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  assert(DiagKind != -1);
2140d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  S.Diag(DeclType.Loc, diag::err_compound_qualified_function_type)
2141d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << DiagKind << isa<FunctionType>(T.IgnoreParens()) << T
2142d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << getFunctionQualifiersAsString(T->castAs<FunctionProtoType>());
2143d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2144d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2145b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// Produce an approprioate diagnostic for an ambiguity between a function
2146b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// declarator and a C++ direct-initializer.
2147b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smithstatic void warnAboutAmbiguousFunction(Sema &S, Declarator &D,
2148b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith                                       DeclaratorChunk &DeclType, QualType RT) {
2149b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2150b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  assert(FTI.isAmbiguous && "no direct-initializer / function ambiguity");
2151b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2152b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // If the return type is void there is no ambiguity.
2153b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isVoidType())
2154b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2155b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2156b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a non-class type can have at most one argument.
2157b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!RT->isRecordType() && FTI.NumArgs > 1)
2158b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2159b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2160b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a reference must have exactly one argument.
2161b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isReferenceType() && FTI.NumArgs != 1)
2162b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2163b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2164b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Only warn if this declarator is declaring a function at block scope, and
2165b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // doesn't have a storage class (such as 'extern') specified.
2166b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!D.isFunctionDeclarator() ||
2167b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      D.getFunctionDefinitionKind() != FDK_Declaration ||
2168b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      !S.CurContext->isFunctionOrMethod() ||
2169b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      D.getDeclSpec().getStorageClassSpecAsWritten()
2170b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        != DeclSpec::SCS_unspecified)
2171b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2172b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2173b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Inside a condition, a direct initializer is not permitted. We allow one to
2174b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // be parsed in order to give better diagnostics in condition parsing.
2175b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (D.getContext() == Declarator::ConditionContext)
2176b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2177b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2178b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  SourceRange ParenRange(DeclType.Loc, DeclType.EndLoc);
2179b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2180d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  S.Diag(DeclType.Loc,
2181d64effc4e31044c05d6e4400150edb26e914983aRichard Smith         FTI.NumArgs ? diag::warn_parens_disambiguated_as_function_declaration
2182d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                     : diag::warn_empty_parens_are_function_decl)
2183d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    << ParenRange;
2184d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2185d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // If the declaration looks like:
2186d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   T var1,
2187d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   f();
2188d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // and name lookup finds a function named 'f', then the ',' was
2189d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // probably intended to be a ';'.
2190d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (!D.isFirstDeclarator() && D.getIdentifier()) {
2191d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Comma(D.getCommaLoc(), S.SourceMgr);
2192d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Name(D.getIdentifierLoc(), S.SourceMgr);
2193d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    if (Comma.getFileID() != Name.getFileID() ||
2194d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) {
2195d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      LookupResult Result(S, D.getIdentifier(), SourceLocation(),
2196d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                          Sema::LookupOrdinaryName);
2197d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      if (S.LookupName(Result, S.getCurScope()))
2198d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        S.Diag(D.getCommaLoc(), diag::note_empty_parens_function_call)
2199d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << FixItHint::CreateReplacement(D.getCommaLoc(), ";")
2200d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << D.getIdentifier();
2201d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    }
2202d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  }
2203b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2204d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (FTI.NumArgs > 0) {
2205d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration with parameters, eg. "T var(T());", suggest adding parens
2206d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // around the first parameter to turn the declaration into a variable
2207d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2208b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceRange Range = FTI.ArgInfo[0].Param->getSourceRange();
2209b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation B = Range.getBegin();
2210b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation E = S.PP.getLocForEndOfToken(Range.getEnd());
2211b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // FIXME: Maybe we should suggest adding braces instead of parens
2212b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // in C++11 for classes that don't have an initializer_list constructor.
2213b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    S.Diag(B, diag::note_additional_parens_for_variable_declaration)
2214b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(B, "(")
2215b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(E, ")");
2216d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  } else {
2217d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration without parameters, eg. "T var();", suggest replacing the
2218d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // parens with an initializer to turn the declaration into a variable
2219d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2220b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
2221d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2222b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // Empty parens mean value-initialization, and no parens mean
2223b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // default initialization. These are equivalent if the default
2224b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // constructor is user-provided or if zero-initialization is a
2225b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // no-op.
2226b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    if (RD && RD->hasDefinition() &&
2227b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        (RD->isEmpty() || RD->hasUserProvidedDefaultConstructor()))
2228b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor)
2229b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        << FixItHint::CreateRemoval(ParenRange);
2230b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    else {
2231b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      std::string Init = S.getFixItZeroInitializerForType(RT);
223280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      if (Init.empty() && S.LangOpts.CPlusPlus11)
2233b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        Init = "{}";
2234b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (!Init.empty())
2235b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        S.Diag(DeclType.Loc, diag::note_empty_parens_zero_initialize)
2236b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith          << FixItHint::CreateReplacement(ParenRange, Init);
2237b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    }
2238b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  }
2239b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith}
2240b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
22418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
22428cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                QualType declSpecType,
22438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                TypeSourceInfo *TInfo) {
22448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
22458cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = declSpecType;
22468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
22478cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
22488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ASTContext &Context = S.Context;
22494e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &LangOpts = S.getLangOpts();
22508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2251cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
2252cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
2253cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
2254cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
22551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2256162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  // Does this declaration declare a typedef-name?
2257162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  bool IsTypedefName =
2258162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef ||
22593e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasDeclContext ||
22603e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasTemplateContext;
2261162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2262d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // Does T refer to a function type with a cv-qualifier or a ref-qualifier?
2263d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  bool IsQualifiedFunction = T->isFunctionProtoType() &&
2264d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      (T->castAs<FunctionProtoType>()->getTypeQuals() != 0 ||
2265d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith       T->castAs<FunctionProtoType>()->getRefQualifier() != RQ_None);
2266d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
226798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
226898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
226998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
22708ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2271711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
2272711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
2273711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2274d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction) {
2275d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      checkQualifiedFunction(S, T, DeclType);
2276d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = DeclType.Kind == DeclaratorChunk::Paren;
2277d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    }
22785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
2279075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
22808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildParenType(T);
2281075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
22825618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
22839af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
22849af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
22858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_blocks_disable);
22861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
22882865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
22898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
22905618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
22915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
22926a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
22936a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
22948cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
22958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
22966a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
22976a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
22986a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
22998cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.ObjC1 && T->getAs<ObjCObjectType>()) {
2300c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
23012865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
23028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
230314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
230414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
23058cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildPointerType(T, DeclType.Loc, Name);
23062865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
23078cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
2308711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
23095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
23100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
23116a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
23126a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
23138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
23148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
23156a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
23166a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
23176a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
23188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
23192865474261a608c7873b87ba4af110d17907896dJohn McCall
23202865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
23212865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
23228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
23235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
23240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
23255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
23266a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
23276a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
23288cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
23298cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
23306a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
23316a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
23326a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
2333fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
233494f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
23355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
23365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
23375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
23385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
23395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
23405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
23415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
2342c05a94b7accd4035bf5d5897c434c445b22da855John McCall      if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
2343f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
2344f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
2345f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
23468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
2347f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
2348f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
2349f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
23507f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
23517f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // C99 6.7.5.2p1: The optional type qualifiers and the keyword static
23527f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // shall appear only in a declaration of a function parameter with an
23537f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // array type, ...
23547f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      if (ASM == ArrayType::Static || ATI.TypeQuals) {
235599570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay        if (!(D.isPrototypeContext() ||
235699570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay              D.getContext() == Declarator::KNRTypeListContext)) {
23577f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          S.Diag(DeclType.Loc, diag::err_array_static_outside_prototype) <<
23587f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
23597f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Remove the 'static' and the type qualifiers.
23607f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          if (ASM == ArrayType::Static)
23617f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ASM = ArrayType::Normal;
23627f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          ATI.TypeQuals = 0;
23637f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          D.setInvalidType(true);
23647f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
23657f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
23667f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // C99 6.7.5.2p1: ... and then only in the outermost array type
23677f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // derivation.
23687f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        unsigned x = chunkIndex;
23697f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        while (x != 0) {
23707f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Walk outwards along the declarator chunks.
23717f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          x--;
23727f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          const DeclaratorChunk &DC = D.getTypeObject(x);
23737f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          switch (DC.Kind) {
23747f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Paren:
23757f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            continue;
23767f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Array:
23777f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Pointer:
23787f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Reference:
23797f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::MemberPointer:
23807f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            S.Diag(DeclType.Loc, diag::err_array_static_not_outermost) <<
23817f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
23827f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            if (ASM == ArrayType::Static)
23837f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              ASM = ArrayType::Normal;
23847f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ATI.TypeQuals = 0;
23857f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            D.setInvalidType(true);
23867f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
23877f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Function:
23887f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::BlockPointer:
23897f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            // These are invalid anyway, so just ignore.
23907f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
23917f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          }
23927f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
23937f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      }
23947f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
23958ac2c66a1442985091c5ec2b33ce6d3df3bcb529Eli Friedman      T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
23968cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                           SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
23975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
23985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2399f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
24005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
24015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
24025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
24035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2404d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = FTI.TypeQuals || FTI.hasRefQualifier();
24053cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
240634b41d939a1328f484511c6002ba2456db879a29Richard Smith      // Check for auto functions and trailing return type and adjust the
240734b41d939a1328f484511c6002ba2456db879a29Richard Smith      // return type accordingly.
240834b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!D.isInvalidType()) {
240934b41d939a1328f484511c6002ba2456db879a29Richard Smith        // trailing-return-type is only required if we're declaring a function,
241034b41d939a1328f484511c6002ba2456db879a29Richard Smith        // and not, for instance, a pointer to a function.
241134b41d939a1328f484511c6002ba2456db879a29Richard Smith        if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
241254655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            !FTI.hasTrailingReturnType() && chunkIndex == 0) {
24138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
241434b41d939a1328f484511c6002ba2456db879a29Richard Smith               diag::err_auto_missing_trailing_return);
241534b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = Context.IntTy;
241634b41d939a1328f484511c6002ba2456db879a29Richard Smith          D.setInvalidType(true);
241754655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        } else if (FTI.hasTrailingReturnType()) {
2418e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          // T must be exactly 'auto' at this point. See CWG issue 681.
2419e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (isa<ParenType>(T)) {
24208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
2421e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith                 diag::err_trailing_return_in_parens)
2422e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith              << T << D.getDeclSpec().getSourceRange();
2423e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.setInvalidType(true);
2424f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman          } else if (D.getContext() != Declarator::LambdaExprContext &&
2425f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman                     (T.hasQualifiers() || !isa<AutoType>(T))) {
24268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
242734b41d939a1328f484511c6002ba2456db879a29Richard Smith                 diag::err_trailing_return_without_auto)
242834b41d939a1328f484511c6002ba2456db879a29Richard Smith              << T << D.getDeclSpec().getSourceRange();
242934b41d939a1328f484511c6002ba2456db879a29Richard Smith            D.setInvalidType(true);
243034b41d939a1328f484511c6002ba2456db879a29Richard Smith          }
243154655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          T = S.GetTypeFromParser(FTI.getTrailingReturnType(), &TInfo);
243254655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (T.isNull()) {
243354655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            // An error occurred parsing the trailing return type.
243454655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            T = Context.IntTy;
243554655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            D.setInvalidType(true);
243654655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          }
243734b41d939a1328f484511c6002ba2456db879a29Richard Smith        }
243834b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
243934b41d939a1328f484511c6002ba2456db879a29Richard Smith
2440e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // C99 6.7.5.3p1: The return type may not be a function or array type.
2441e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // For conversion functions, we'll diagnose this particular error later.
2442e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      if ((T->isArrayType() || T->isFunctionType()) &&
2443e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
2444e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned diagID = diag::err_func_returning_array_function;
2445e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // Last processing chunk in block context means this function chunk
2446e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // represents the block.
2447e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (chunkIndex == 0 &&
2448e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.getContext() == Declarator::BlockLiteralContext)
2449e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          diagID = diag::err_block_returning_array_function;
24508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
2451e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        T = Context.IntTy;
2452e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        D.setInvalidType(true);
2453e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2454e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2455aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Do not allow returning half FP value.
2456aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // FIXME: This really should be in BuildFunctionType.
2457aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      if (T->isHalfType()) {
245819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        if (S.getLangOpts().OpenCL) {
245919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          if (!S.getOpenCLOptions().cl_khr_fp16) {
246019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            S.Diag(D.getIdentifierLoc(), diag::err_opencl_half_return) << T;
246119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            D.setInvalidType(true);
246219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          }
246319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        } else {
246419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          S.Diag(D.getIdentifierLoc(),
246519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            diag::err_parameters_retval_cannot_have_fp16_type) << 1;
246619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          D.setInvalidType(true);
246719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly        }
2468aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      }
2469aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
24705291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
24715291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
2472fff951371dfc309160a99d423e43a7841aeb35aaDouglas Gregor      if (isa<PointerType>(T) && T.getLocalCVRQualifiers() &&
24731e15394853bfae25112d9cc6b445504905e1f34aRafael Espindola          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId) &&
24748cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          (!LangOpts.CPlusPlus || !T->isDependentType())) {
2475d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?");
2476d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
2477d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer);
2478d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2479d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr;
2480d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2481d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DiagnoseIgnoredQualifiers(PTI.TypeQuals,
2482d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
2483d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
2484d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
24858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S);
2486d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2487d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth      } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
24888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          (!LangOpts.CPlusPlus ||
24895291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor           (!T->isDependentType() && !T->isRecordType()))) {
2490d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2491d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(),
2492d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getConstSpecLoc(),
2493d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getVolatileSpecLoc(),
2494d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getRestrictSpecLoc(),
24958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  S);
24965291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      }
2497d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
249802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // Objective-C ARC ownership qualifiers are ignored on the function
249902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // return type (by type canonicalization). Complain if this attribute
250002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      // was written here.
250102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      if (T.getQualifiers().hasObjCLifetime()) {
250202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        SourceLocation AttrLoc;
250302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (chunkIndex + 1 < D.getNumTypeObjects()) {
250402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
250502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr = ReturnTypeChunk.getAttrs();
250602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
250702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
250802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
250902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
251002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
251102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
251202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
251302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isInvalid()) {
251402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          for (const AttributeList *Attr
251502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor                 = D.getDeclSpec().getAttributes().getList();
251602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor               Attr; Attr = Attr->getNext()) {
251702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            if (Attr->getKind() == AttributeList::AT_ObjCOwnership) {
251802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              AttrLoc = Attr->getLoc();
251902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor              break;
252002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            }
252102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          }
252202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
252302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
252402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        if (AttrLoc.isValid()) {
252502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // The ownership attributes are almost always written via
252602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // the predefined
252702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          // __strong/__weak/__autoreleasing/__unsafe_unretained.
252802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          if (AttrLoc.isMacroID())
252902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            AttrLoc = S.SourceMgr.getImmediateExpansionRange(AttrLoc).first;
253002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
253102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          S.Diag(AttrLoc, diag::warn_arc_lifetime_result_type)
253202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor            << T.getQualifiers().getObjCLifetime();
253302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor        }
253402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      }
253502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
25368cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
2537402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
2538402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
2539b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
25405e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall        if (Tag->isCompleteDefinition())
25418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
2542402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
2543402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
2544402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
25453cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
25463cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
2547162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (IsTypedefName && FTI.getExceptionSpecType())
25488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef)
25493e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          << (D.getContext() == Declarator::AliasDeclContext ||
25503e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith              D.getContext() == Declarator::AliasTemplateContext);
25513cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
2552b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // If we see "T var();" or "T var(T());" at block scope, it is probably
2553b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // an attempt to initialize a variable, not a function declaration.
2554b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (FTI.isAmbiguous)
2555b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        warnAboutAmbiguousFunction(S, D, DeclType, T);
2556b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
25578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (!FTI.NumArgs && !FTI.isVariadic && !LangOpts.CPlusPlus) {
25582865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
25592865474261a608c7873b87ba4af110d17907896dJohn McCall        T = Context.getFunctionNoProtoType(T);
25602865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
25612865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
25622865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
25632865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
25648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        if (!FTI.NumArgs && FTI.isVariadic && !LangOpts.CPlusPlus) {
2565965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
2566965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
2567965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
25688e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt            if (Attrs->getKind() == AttributeList::AT_Overloadable) {
2569965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
2570965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
2571965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
2572965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
2573965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
2574965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
25758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
2576c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
25772865474261a608c7873b87ba4af110d17907896dJohn McCall
25782865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
2579788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
2580788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
25818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
25822865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
25832865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
25842865474261a608c7873b87ba4af110d17907896dJohn McCall        }
25852865474261a608c7873b87ba4af110d17907896dJohn McCall
2586e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
2587e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
258854655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        EPI.HasTrailingReturn = FTI.hasTrailingReturnType();
2589e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
2590c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
2591c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : FTI.RefQualifierIsLValueRef? RQ_LValue
2592c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : RQ_RValue;
259391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
25945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
25955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
25965f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 16> ArgTys;
25972865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
25981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<bool, 16> ConsumedArguments;
2600f85e193739c953358c865005855253af4f68a497John McCall        ConsumedArguments.reserve(FTI.NumArgs);
2601f85e193739c953358c865005855253af4f68a497John McCall        bool HasAnyConsumedArguments = false;
2602f85e193739c953358c865005855253af4f68a497John McCall
26035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
2604d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
26058123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
260678c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
26072dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
26082dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
260991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier          assert((ArgTy == Context.getAdjustedParameterType(ArgTy)) &&
261079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor                 "Unadjusted type?");
26112dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
26125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
26135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
261472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
26152dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
26165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
26175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
26185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
26195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
26208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(DeclType.Loc, diag::err_void_only_param);
26212ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
26228123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
26232ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
26242ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
26258cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(FTI.ArgInfo[i].IdentLoc,
26264565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
26272ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
26288123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
26292ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
26302ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
26310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
26328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                S.Diag(DeclType.Loc, diag::err_void_param_qualified);
26331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26342ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
26352ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
26362ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
2637aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov          } else if (ArgTy->isHalfType()) {
2638aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // Disallow half FP arguments.
2639aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // FIXME: This really should be in BuildFunctionType.
264019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            if (S.getLangOpts().OpenCL) {
264119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              if (!S.getOpenCLOptions().cl_khr_fp16) {
264219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                S.Diag(Param->getLocation(),
264319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                  diag::err_opencl_half_argument) << ArgTy;
264419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                D.setInvalidType();
26459dd74c5504c743c96ea3a1d691d6a75ec3a98147John McCall                Param->setInvalidDecl();
264619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              }
264719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            } else {
264819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              S.Diag(Param->getLocation(),
264919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                diag::err_parameters_retval_cannot_have_fp16_type) << 0;
265019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly              D.setInvalidType();
265119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly            }
2652eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
2653eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
2654a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
2655eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              Param->setKNRPromoted(true);
2656183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
2657eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              if (BTy->getKind() == BuiltinType::Float) {
2658eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
2659eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall                Param->setKNRPromoted(true);
2660eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              }
2661eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
26625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
266356a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
26648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          if (LangOpts.ObjCAutoRefCount) {
2665f85e193739c953358c865005855253af4f68a497John McCall            bool Consumed = Param->hasAttr<NSConsumedAttr>();
2666f85e193739c953358c865005855253af4f68a497John McCall            ConsumedArguments.push_back(Consumed);
2667f85e193739c953358c865005855253af4f68a497John McCall            HasAnyConsumedArguments |= Consumed;
2668f85e193739c953358c865005855253af4f68a497John McCall          }
2669f85e193739c953358c865005855253af4f68a497John McCall
267054e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
26715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2672465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2673f85e193739c953358c865005855253af4f68a497John McCall        if (HasAnyConsumedArguments)
2674f85e193739c953358c865005855253af4f68a497John McCall          EPI.ConsumedArguments = ConsumedArguments.data();
2675f85e193739c953358c865005855253af4f68a497John McCall
26765f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 4> Exceptions;
267774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<ParsedType, 2> DynamicExceptions;
267874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<SourceRange, 2> DynamicExceptionRanges;
267974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        Expr *NoexceptExpr = 0;
268091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
26818b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        if (FTI.getExceptionSpecType() == EST_Dynamic) {
268274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // FIXME: It's rather inefficient to have to split into two vectors
268374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // here.
268474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          unsigned N = FTI.NumExceptions;
268574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptions.reserve(N);
268674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptionRanges.reserve(N);
268774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          for (unsigned I = 0; I != N; ++I) {
268874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptions.push_back(FTI.Exceptions[I].Ty);
268974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptionRanges.push_back(FTI.Exceptions[I].Range);
2690e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
26918b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
269274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          NoexceptExpr = FTI.NoexceptExpr;
269374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        }
269491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
269574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        S.checkExceptionSpecification(FTI.getExceptionSpecType(),
269674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptions,
269774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptionRanges,
269874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      NoexceptExpr,
269974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      Exceptions,
270074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      EPI);
270191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2702e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI);
27035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
270404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
27055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
27065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2707f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
2708f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
27097bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
2710f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
27117bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
2712edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
2713edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
27148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      } else if (S.isDependentScopeSpecifier(SS) ||
27158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                 dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) {
27161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
27177bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
271887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
271987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
272087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
27217bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
27224a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
272387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
272487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
272587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
272614aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor        case NestedNameSpecifier::NamespaceAlias:
272787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
27289f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
27297bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
273087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
273187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
273287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
273391ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Note: if the NNS has a prefix and ClsType is a nondependent
273491ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // TemplateSpecializationType, then the NNS prefix is NOT included
273591ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // in ClsType; hence we wrap ClsType into an ElaboratedType.
273691ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // NOTE: in particular, no wrap occurs if ClsType already is an
273791ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Elaborated, DependentName, or DependentTemplateSpecialization.
273891ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
27397bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
274087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
274187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
2742f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
27438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Mem.Scope().getBeginLoc(),
2744949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
2745949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
2746949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
2747f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
2748f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2749f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2750949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
27518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
2752949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
2753f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
2754949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
27552865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
27568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
2757f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2758f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
2759f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
2760f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2761cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
2762cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
2763cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
2764cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
2765cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
2766c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
2767711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
2768f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclChunk, attrs);
27695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2770971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
27718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (LangOpts.CPlusPlus && T->isFunctionType()) {
2772183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
2773778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
2774971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
277591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // C++ 8.3.5p4:
2776708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
2777708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
2778708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
2779708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
2780683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    //
2781683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // Core issue 547 also allows cv-qualifiers on function types that are
2782683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // top-level template type arguments.
2783613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
2784613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
2785906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman      FreeFunction = ((D.getContext() != Declarator::MemberContext &&
2786906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman                       D.getContext() != Declarator::LambdaExprContext) ||
2787613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall                      D.getDeclSpec().isFriendSpecified());
2788613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
27898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec());
2790613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
2791613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
2792613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
2793d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.fct]p6 (w/DR1417):
2794d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // An attempt to specify a function type with a cv-qualifier-seq or a
2795d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // ref-qualifier (including by typedef-name) is ill-formed unless it is:
2796d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type for a non-static member function,
2797d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type to which a pointer to member refers,
2798d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the top-level function type of a function typedef declaration or
2799d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //    alias-declaration,
2800d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id in the default argument of a type-parameter, or
2801d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id of a template-argument for a type-parameter
2802d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction &&
2803d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !(!FreeFunction &&
2804d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) &&
2805d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !IsTypedefName &&
2806d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        D.getContext() != Declarator::TemplateTypeArgContext) {
280796a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      SourceLocation Loc = D.getLocStart();
2808d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      SourceRange RemovalRange;
2809d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      unsigned I;
2810d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      if (D.isFunctionDeclarator(I)) {
2811d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        SmallVector<SourceLocation, 4> RemovalLocs;
2812d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        const DeclaratorChunk &Chunk = D.getTypeObject(I);
2813d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        assert(Chunk.Kind == DeclaratorChunk::Function);
2814d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.hasRefQualifier())
2815d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getRefQualifierLoc());
2816d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Const)
2817d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getConstQualifierLoc());
2818d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Volatile)
2819d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getVolatileQualifierLoc());
2820d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        // FIXME: We do not track the location of the __restrict qualifier.
2821d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //if (Chunk.Fun.TypeQuals & Qualifiers::Restrict)
2822d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //  RemovalLocs.push_back(Chunk.Fun.getRestrictQualifierLoc());
2823d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (!RemovalLocs.empty()) {
2824d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          std::sort(RemovalLocs.begin(), RemovalLocs.end(),
2825aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                    BeforeThanCompare<SourceLocation>(S.getSourceManager()));
2826d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalRange = SourceRange(RemovalLocs.front(), RemovalLocs.back());
2827d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          Loc = RemovalLocs.front();
2828683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
2829683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      }
2830d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2831d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      S.Diag(Loc, diag::err_invalid_qualified_function_type)
2832d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FreeFunction << D.isFunctionDeclarator() << T
2833d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << getFunctionQualifiersAsString(FnTy)
2834d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FixItHint::CreateRemoval(RemovalRange);
2835d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2836d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // Strip the cv-qualifiers and ref-qualifiers from the type.
2837d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
2838d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.TypeQuals = 0;
2839d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.RefQualifier = RQ_None;
2840d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
284191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      T = Context.getFunctionType(FnTy->getResultType(),
2842d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                  FnTy->arg_type_begin(),
2843d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                  FnTy->getNumArgs(), EPI);
2844e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      // Rebuild any parens around the identifier in the function type.
2845e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2846e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        if (D.getTypeObject(i).Kind != DeclaratorChunk::Paren)
2847e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith          break;
2848e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        T = S.BuildParenType(T);
2849e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      }
2850971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
2851971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
28521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2853711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
2854711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull())
2855711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = D.getAttributes())
2856f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclName, attrs);
2857711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2858711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
2859711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
2860711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2861148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  // C++0x [dcl.constexpr]p9:
2862148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  //  A constexpr specifier used in an object declaration declares the object
286391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //  as const.
2864148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
2865737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
2866737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
2867737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
286891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // If there was an ellipsis in the declarator, the declaration declares a
2869a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
2870a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (D.hasEllipsis() && !T.isNull()) {
2871a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
287291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   A declarator-id or abstract-declarator containing an ellipsis shall
2873a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
2874a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
2875a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
2876a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
2877a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
287891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   [...] When it is part of a parameter-declaration-clause, the
287991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   parameter pack is a function parameter pack (14.5.3). The type T
2880a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
288191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   a template parameter pack; each template parameter pack in T is
2882a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
2883a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2884a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
2885a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
2886a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
288791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(D.getEllipsisLoc(),
2888a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
2889a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
2890a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
2891a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
2892cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
2893a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
2894a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
289591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2896a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
2897a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
289891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   If a template-parameter is a [...] is a parameter-declaration that
2899a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
2900a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
2901a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2902a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
2903a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
2904a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
2905a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
2906cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
2907e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith      else
2908e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith        S.Diag(D.getEllipsisLoc(),
290980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith               LangOpts.CPlusPlus11
2910e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 ? diag::warn_cxx98_compat_variadic_templates
2911e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 : diag::ext_variadic_templates);
2912a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
291391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2914a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
2915a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
2916cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:  // FIXME: special diagnostic here?
2917cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:     // FIXME: special diagnostic here?
2918a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
29190b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
2920162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
29213e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
2922a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
2923a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
2924a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
2925a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
2926a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
292717b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
2928a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
2929f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
29307796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
2931683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    case Declarator::TemplateTypeArgContext:
2932a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
2933a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
29348cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      S.Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
2935a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
2936a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2937a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
2938a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
2939e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2940bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
2941bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
2942bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
2943bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
2944db7abf78dedc2ef6ccb42b3dac6ab330fe2ea469Argyrios Kyrtzidis
29458cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return S.GetTypeSourceInfoForDeclarator(D, T, TInfo);
29468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
29478cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
29488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// GetTypeForDeclarator - Convert the type for the specified
29498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// declarator to Type instances.
29508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis///
29518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// The result of this call will never be null, but the associated
29528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// type may be a null type if there's an unrecoverable error.
29538cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
29548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // Determine the type of the declarator. Not all forms of declarator
29558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // have a type.
29568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
29578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeProcessingState state(*this, D);
29588cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
29598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
29608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
29618cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (T.isNull())
29628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
29638cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
29644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (D.isPrototypeContext() && getLangOpts().ObjCAutoRefCount)
29658cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    inferARCWriteback(state, T);
296691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
29678cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return GetFullTypeForDeclarator(state, T, ReturnTypeInfo);
29685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
29695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
297031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclSpec(Sema &S,
297131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           QualType &declSpecTy,
297231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           Qualifiers::ObjCLifetime ownership) {
297331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy->isObjCRetainableType() &&
297431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) {
297531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers qs;
297631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    qs.addObjCLifetime(ownership);
297731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    declSpecTy = S.Context.getQualifiedType(declSpecTy, qs);
297831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
297931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
298031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
298131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
298231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
298331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            unsigned chunkIndex) {
298431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
298531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
298631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
298731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Look for an explicit lifetime attribute.
298831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclaratorChunk &chunk = D.getTypeObject(chunkIndex);
298931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (const AttributeList *attr = chunk.getAttrs(); attr;
299031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis         attr = attr->getNext())
29918e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    if (attr->getKind() == AttributeList::AT_ObjCOwnership)
299231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
299331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
299431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  const char *attrStr = 0;
299531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  switch (ownership) {
29963026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
299731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break;
299831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Strong: attrStr = "strong"; break;
299931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Weak: attrStr = "weak"; break;
300031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break;
300131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
300231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
300331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // If there wasn't one, add one (with an invalid source location
300431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // so that we don't make an AttributedType for it).
300531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  AttributeList *attr = D.getAttributePool()
300631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    .create(&S.Context.Idents.get("objc_ownership"), SourceLocation(),
300731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            /*scope*/ 0, SourceLocation(),
300831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            &S.Context.Idents.get(attrStr), SourceLocation(),
300993f95f2a2cbb6bb3d17bfb5fc74ce1cccea751b6Sean Hunt            /*args*/ 0, 0, AttributeList::AS_GNU);
301031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  spliceAttrIntoList(*attr, chunk.getAttrListRef());
301131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
301231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // TODO: mark whether we did this inference?
301331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
301431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
301548d798ce32447607144db70a484cdb99c1180663Benjamin Kramer/// \brief Used for transferring ownership in casts resulting in l-values.
301631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnership(TypeProcessingState &state,
301731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 QualType &declSpecTy,
301831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 Qualifiers::ObjCLifetime ownership) {
301931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
302031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
302131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
302231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  int inner = -1;
30236ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis  bool hasIndirection = false;
302431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
302531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    DeclaratorChunk &chunk = D.getTypeObject(i);
302631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    switch (chunk.Kind) {
302731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Paren:
302831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      // Ignore parens.
302931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
303031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
303131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Array:
303231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Reference:
303331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Pointer:
30346ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
30356ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        hasIndirection = true;
303631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      inner = i;
303731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
303831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
303931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::BlockPointer:
30406ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
30416ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        transferARCOwnershipToDeclaratorChunk(state, ownership, i);
30426ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      return;
304331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
304431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Function:
304531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::MemberPointer:
304631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
304731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    }
304831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
304931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
305031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (inner == -1)
30516ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    return;
305231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
305391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  DeclaratorChunk &chunk = D.getTypeObject(inner);
305431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (chunk.Kind == DeclaratorChunk::Pointer) {
305531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (declSpecTy->isObjCRetainableType())
305631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
30576ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    if (declSpecTy->isObjCObjectType() && hasIndirection)
305831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclaratorChunk(state, ownership, inner);
305931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  } else {
306031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    assert(chunk.Kind == DeclaratorChunk::Array ||
306131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis           chunk.Kind == DeclaratorChunk::Reference);
306231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
306331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
306431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
306531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
306631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) {
306731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeProcessingState state(*this, D);
306831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
306931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
307031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
307131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy.isNull())
307231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
307331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
30744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount) {
307531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy);
307631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (ownership != Qualifiers::OCL_None)
307731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      transferARCOwnership(state, declSpecTy, ownership);
307831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
307931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
308031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo);
308131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
308231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
308314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall/// Map an AttributedType::Kind to an AttributeList::Kind.
308414aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
308514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  switch (kind) {
308614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_address_space:
30878e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_AddressSpace;
308814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_regparm:
30898e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Regparm;
309014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_vector_size:
30918e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_VectorSize;
309214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_vector_type:
30938e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonVectorType;
309414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_polyvector_type:
30958e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonPolyVectorType;
309614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_objc_gc:
30978e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCGC;
3098b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  case AttributedType::attr_objc_ownership:
30998e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCOwnership;
310014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_noreturn:
31018e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NoReturn;
310214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_cdecl:
31038e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_CDecl;
310414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_fastcall:
31058e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_FastCall;
310614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_stdcall:
31078e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_StdCall;
310814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_thiscall:
31098e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ThisCall;
311014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_pascal:
31118e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pascal;
3112414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case AttributedType::attr_pcs:
31138e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pcs;
3114263366f9241366f29ba65b703120f302490c39ffDerek Schuff  case AttributedType::attr_pnaclcall:
3115263366f9241366f29ba65b703120f302490c39ffDerek Schuff    return AttributeList::AT_PnaclCall;
311638980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei  case AttributedType::attr_inteloclbicc:
311738980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    return AttributeList::AT_IntelOclBicc;
311814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
311914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  llvm_unreachable("unexpected attribute kind!");
312014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
312114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
312214aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic void fillAttributedTypeLoc(AttributedTypeLoc TL,
312314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                  const AttributeList *attrs) {
312414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributedType::Kind kind = TL.getAttrKind();
312514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
312614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  assert(attrs && "no type attributes in the expected location!");
312714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributeList::Kind parsedKind = getAttrListKind(kind);
312814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  while (attrs->getKind() != parsedKind) {
312914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    attrs = attrs->getNext();
313014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    assert(attrs && "no matching attribute in expected location!");
313114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
313214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
313314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  TL.setAttrNameLoc(attrs->getLoc());
313414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrExprOperand())
313514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrExprOperand(attrs->getArg(0));
313614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  else if (TL.hasAttrEnumOperand())
313714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrEnumOperandLoc(attrs->getParameterLoc());
313814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
313914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // FIXME: preserve this information to here.
314014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrOperand())
314114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrOperandParensRange(SourceRange());
314214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
314314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
314451bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
314551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
3146c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    ASTContext &Context;
314751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
3148f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
314951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
315091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
3151c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      : Context(Context), DS(DS) {}
3152f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
315314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
315414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, DS.getAttributes().getList());
315514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      Visit(TL.getModifiedLoc());
315614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
315751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
315851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
315951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
316051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
316151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
316251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
316351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
316451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
31651de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // FIXME. We should have DS.getTypeSpecTypeEndLoc(). But, it requires
31661de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // addition field. What we have is good enough for dispay of location
31671de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // of 'fixit' on interface name.
31681de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      TL.setNameEndLoc(DS.getLocEnd());
3169c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
3170c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3171c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
3172c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
3173c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
3174c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.getBaseLoc().initialize(Context, SourceLocation());
3175c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
3176c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
3177c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
3178c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
317954e14c4db764c0636160d26c5bbf491637c83a76John McCall
3180c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
318154e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
318254e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
318354e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
318454e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
318554e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
318654e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
318754e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
318854e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
318954e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
319054e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
319154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
319254e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
319351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
319454e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
319554e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
3196c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
319751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
3198833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
3199a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
3200b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3201833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3202833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
3203833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
3204a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
32050daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara        TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
3206833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
3207833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
3208833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3209e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
3210e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
3211e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
3212e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TemplateSpecializationTypeLoc NamedTL =
3213e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
3214e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
3215e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
3216e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      else
3217e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
3218833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
3219cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3220cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
3221cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3222cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3223cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3224cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3225cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
3226cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3227cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3228b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
3229cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
3230b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3231cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
3232cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3233ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3234ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      // FIXME: This holds only because we only have one unary transform.
3235ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType);
3236ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setKWLoc(DS.getTypeSpecTypeLoc());
3237ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setParensRange(DS.getTypeofParensRange());
3238ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getRepAsType());
3239ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TypeSourceInfo *TInfo = 0;
3240ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3241ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setUnderlyingTInfo(TInfo);
3242ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
3243ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3244ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
3245ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
3246ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
3247ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
3248ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
3249ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
3250ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
3251ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
3252ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
3253ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
3254ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
3255ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
3256ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
3257ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
3258e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3259e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
3260e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
3261253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
3262e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
3263b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3264e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
3265e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
3266e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
3267e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
3268e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
326938a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara      TL.setElaboratedKeywordLoc(Keyword != ETK_None
327038a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 ? DS.getTypeSpecTypeLoc()
327138a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 : SourceLocation());
3272e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
32739e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
3274e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
3275e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3276e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
327766581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
327866581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
327966581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
328066581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
328166581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
328233500955d731c73717af52088b7fc0e7a85681e7John McCall    }
328333500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
328433500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
328566581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
328666581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
328766581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
328866581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
328966581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
329066581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara                TInfo->getTypeLoc()));
32910daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    }
32920daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    void VisitTagTypeLoc(TagTypeLoc TL) {
32930daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
3294e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3295b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
3296b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      TL.setKWLoc(DS.getTypeSpecTypeLoc());
3297b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      TL.setParensRange(DS.getTypeofParensRange());
329891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
329943fe245b37c3cd36d837aab9eb98551328d30141Douglas Gregor      TypeSourceInfo *TInfo = 0;
330043fe245b37c3cd36d837aab9eb98551328d30141Douglas Gregor      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
330143fe245b37c3cd36d837aab9eb98551328d30141Douglas Gregor      TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
3302b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
3303e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
330451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
330551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
3306c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initialize(Context, DS.getTypeSpecTypeLoc());
330751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
330851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
3309eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
331051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
3311b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    ASTContext &Context;
331251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
3313f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
331451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
3315b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
3316b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      : Context(Context), Chunk(Chunk) {}
33174adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
331851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
33199f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
332051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
33214adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3322f85e193739c953358c865005855253af4f68a497John McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3323f85e193739c953358c865005855253af4f68a497John McCall      fillAttributedTypeLoc(TL, Chunk.getAttrs());
3324f85e193739c953358c865005855253af4f68a497John McCall    }
332551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
332651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
332751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
33284adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
332951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
333051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
333151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
33324adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
333351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
333451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
333551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
33364adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
333751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
333851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
3339b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const CXXScopeSpec& SS = Chunk.Mem.Scope();
3340b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
3341b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3342b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const Type* ClsTy = TL.getClass();
3343b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      QualType ClsQT = QualType(ClsTy, 0);
3344b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
3345b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Now copy source location info into the type loc component.
3346b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeLoc ClsTL = ClsTInfo->getTypeLoc();
3347b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
3348b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Identifier:
3349b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
3350b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        {
3351fd9c42ed22fb4f7f865f7d8f8848df84ddf8262cAbramo Bagnara          DependentNameTypeLoc DNTLoc = cast<DependentNameTypeLoc>(ClsTL);
335238a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          DNTLoc.setElaboratedKeywordLoc(SourceLocation());
3353b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
3354b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
3355b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3356b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3357b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3358b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpec:
3359b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpecWithTemplate:
3360b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        if (isa<ElaboratedType>(ClsTy)) {
3361b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ElaboratedTypeLoc ETLoc = *cast<ElaboratedTypeLoc>(&ClsTL);
336238a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          ETLoc.setElaboratedKeywordLoc(SourceLocation());
3363b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ETLoc.setQualifierLoc(NNSLoc.getPrefix());
3364b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
3365b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
3366b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        } else {
3367b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
3368b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3369b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3370b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3371b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Namespace:
3372b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::NamespaceAlias:
3373b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Global:
3374b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        llvm_unreachable("Nested-name-specifier must name a type");
3375b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      }
3376b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3377b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Finally fill in MemberPointerLocInfo fields.
337851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
3379b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TL.setClassTInfo(ClsTInfo);
33804adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
338151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
338251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
338354e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
338454e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
338551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
338651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
338751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
338851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
338951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
339051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
339151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
339251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
339351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
339451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
339551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
339651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
339751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
339851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
339951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
3400796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeBegin(Chunk.Loc);
3401796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeEnd(Chunk.EndLoc);
340251bd803fbdade51d674598ed45da3d54190a656cJohn McCall
340351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
340459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setLParenLoc(FTI.getLParenLoc());
340559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setRParenLoc(FTI.getRParenLoc());
340654e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
3407d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
340854e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
34094adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
341051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
34114adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
3412075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
3413075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
3414075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
3415075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
3416075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
34171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
341851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
34199f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
34204adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
342151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
342251bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
34234adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3424a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
342551bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
342651bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
342705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
342805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
342905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
343005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
343105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
3432a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
343305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
343405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
3435a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
3436a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
343751bd803fbdade51d674598ed45da3d54190a656cJohn McCall
3438a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
3439a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
3440a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc());
344191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
3442a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
344391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
34448ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
344514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    while (isa<AttributedTypeLoc>(CurrTL)) {
344614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      AttributedTypeLoc TL = cast<AttributedTypeLoc>(CurrTL);
344714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs());
344814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
344914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
345014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3451b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
345251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
34534adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
345491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3455b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
3456b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
3457b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
345805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
345905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
346005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
3461b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
3462c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
346305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
346491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3465a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
34664adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
34674adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3468a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
3469b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
34701bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
34711bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
34721bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
347391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
3474eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
3475a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
34761bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
34771bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
3478b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
34791bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
34801bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
34811bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
34821bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
3483b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*"
348435d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
348535d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
34861bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
34871bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
3488f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
34895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
34905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
34915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
34921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3493d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
3494bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
34955153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
3496809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
34975912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
3498e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall  // Make sure there are no unused decl attributes on the declarator.
3499cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // We don't want to do this for ObjC parameters because we're going
3500cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // to apply them to the actual parameter declaration.
3501cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  if (D.getContext() != Declarator::ObjCParameterContext)
3502cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    checkUnusedDeclAttributes(D);
3503e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
35044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
3505402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
35066d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
3507402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
3508402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
3509b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
35105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
35115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3512e97179c675b341927807c718be215c8d1aab8acbDouglas GregorParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) {
3513e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  QualType T = Context.getObjCInstanceType();
3514e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc);
3515e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return CreateParsedType(T, TInfo);
3516e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
3517e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3518e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3519c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3520c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
3521c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3522232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3523232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
3524c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
3525c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
35261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
3527c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
35280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
3529232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
353029e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified by
353129e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // qualifiers for two or more different address spaces."
3532232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
3533c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
3534e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3535c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3536232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
35371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3538020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be
3539020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // qualified by an address-space qualifier."
3540020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  if (Type->isFunctionType()) {
3541020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type);
3542020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    Attr.setInvalid();
3543020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    return;
3544020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  }
3545020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne
3546232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
3547545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
3548f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3549e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3550c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3551232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3552545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
3553232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
3554ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
3555ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
3556dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
3557dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
3558e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3559c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3560232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3561232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3562efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
3563efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
3564efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
3565efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
3566efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
3567e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
3568efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
3569efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
3570efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
3571efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3572efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
35730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
3574efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
3575efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
35760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
3577e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3578efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
3579efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3580efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
35811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
3582f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
3583c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
3584c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
3585d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// Does this type have a "direct" ownership qualifier?  That is,
3586d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// is it written like "__strong id", as opposed to something like
3587d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// "typeof(foo)", where that happens to be strong?
3588d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCallstatic bool hasDirectOwnershipQualifier(QualType type) {
3589d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Fast path: no qualifier at all.
3590d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  assert(type.getQualifiers().hasObjCLifetime());
3591d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3592d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  while (true) {
3593d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // __strong id
3594d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (const AttributedType *attr = dyn_cast<AttributedType>(type)) {
3595d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      if (attr->getAttrKind() == AttributedType::attr_objc_ownership)
3596d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        return true;
3597d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3598d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = attr->getModifiedType();
3599d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3600d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // X *__strong (...)
3601d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else if (const ParenType *paren = dyn_cast<ParenType>(type)) {
3602d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = paren->getInnerType();
360391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3604d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // That's it for things we want to complain about.  In particular,
3605d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // we do not want to look through typedefs, typeof(expr),
3606d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // typeof(type), or any other way that the type is somehow
3607d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // abstracted.
3608d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else {
360991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3610d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return false;
3611d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3612d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
3613d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall}
3614d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3615b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis/// handleObjCOwnershipTypeAttr - Process an objc_ownership
3616f85e193739c953358c865005855253af4f68a497John McCall/// attribute on the specified type.
3617f85e193739c953358c865005855253af4f68a497John McCall///
3618f85e193739c953358c865005855253af4f68a497John McCall/// Returns 'true' if the attribute was handled.
3619b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
3620f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr,
3621f85e193739c953358c865005855253af4f68a497John McCall                                       QualType &type) {
3622e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  bool NonObjCPointer = false;
3623e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3624e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!type->isDependentType()) {
3625e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    if (const PointerType *ptr = type->getAs<PointerType>()) {
3626e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      QualType pointee = ptr->getPointeeType();
3627e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      if (pointee->isObjCRetainableType() || pointee->isPointerType())
3628e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis        return false;
3629e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // It is important not to lose the source info that there was an attribute
3630e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // applied to non-objc pointer. We will create an attributed type but
3631e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // its type will be the same as the original type.
3632e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      NonObjCPointer = true;
3633e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    } else if (!type->isObjCRetainableType()) {
3634e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      return false;
3635e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3636e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3637f85e193739c953358c865005855253af4f68a497John McCall
3638f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
3639440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  SourceLocation AttrLoc = attr.getLoc();
3640440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isMacroID())
3641440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    AttrLoc = S.getSourceManager().getImmediateExpansionRange(AttrLoc).first;
3642f85e193739c953358c865005855253af4f68a497John McCall
3643f85e193739c953358c865005855253af4f68a497John McCall  if (!attr.getParameterName()) {
3644440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::err_attribute_argument_n_not_string)
3645b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      << "objc_ownership" << 1;
3646f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3647f85e193739c953358c865005855253af4f68a497John McCall    return true;
3648f85e193739c953358c865005855253af4f68a497John McCall  }
3649f85e193739c953358c865005855253af4f68a497John McCall
3650d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Consume lifetime attributes without further comment outside of
3651d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // ARC mode.
36524e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!S.getLangOpts().ObjCAutoRefCount)
3653d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    return true;
3654d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3655f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime lifetime;
3656f85e193739c953358c865005855253af4f68a497John McCall  if (attr.getParameterName()->isStr("none"))
3657f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_ExplicitNone;
3658f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("strong"))
3659f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Strong;
3660f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("weak"))
3661f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Weak;
3662f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("autoreleasing"))
3663f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Autoreleasing;
3664f85e193739c953358c865005855253af4f68a497John McCall  else {
3665440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_attribute_type_not_supported)
3666b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      << "objc_ownership" << attr.getParameterName();
3667f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3668f85e193739c953358c865005855253af4f68a497John McCall    return true;
3669f85e193739c953358c865005855253af4f68a497John McCall  }
3670f85e193739c953358c865005855253af4f68a497John McCall
3671d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  SplitQualType underlyingType = type.split();
3672d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3673d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Check for redundant/conflicting ownership qualifiers.
3674d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  if (Qualifiers::ObjCLifetime previousLifetime
3675d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        = type.getQualifiers().getObjCLifetime()) {
3676d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // If it's written directly, that's an error.
3677d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (hasDirectOwnershipQualifier(type)) {
3678d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant)
3679d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        << type;
3680d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return true;
3681d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3682d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3683d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // Otherwise, if the qualifiers actually conflict, pull sugar off
3684d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // until we reach a type that is directly qualified.
3685d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (previousLifetime != lifetime) {
3686d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // This should always terminate: the canonical type is
3687d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // qualified, so some bit of sugar must be hiding it.
3688d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      while (!underlyingType.Quals.hasObjCLifetime()) {
3689d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        underlyingType = underlyingType.getSingleStepDesugaredType();
3690d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      }
3691d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      underlyingType.Quals.removeObjCLifetime();
3692d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3693d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
3694d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3695d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  underlyingType.Quals.addObjCLifetime(lifetime);
3696f85e193739c953358c865005855253af4f68a497John McCall
3697e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (NonObjCPointer) {
3698e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    StringRef name = attr.getName()->getName();
3699e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    switch (lifetime) {
3700e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_None:
3701e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_ExplicitNone:
3702e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      break;
3703e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Strong: name = "__strong"; break;
3704e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Weak: name = "__weak"; break;
3705e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break;
3706e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3707e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_objc_object_attribute_wrong_type)
3708e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      << name << type;
3709e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3710e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3711f85e193739c953358c865005855253af4f68a497John McCall  QualType origType = type;
3712e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!NonObjCPointer)
3713d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    type = S.Context.getQualifiedType(underlyingType);
3714f85e193739c953358c865005855253af4f68a497John McCall
3715f85e193739c953358c865005855253af4f68a497John McCall  // If we have a valid source location for the attribute, use an
3716f85e193739c953358c865005855253af4f68a497John McCall  // AttributedType instead.
3717440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isValid())
3718b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis    type = S.Context.getAttributedType(AttributedType::attr_objc_ownership,
3719f85e193739c953358c865005855253af4f68a497John McCall                                       origType, type);
3720f85e193739c953358c865005855253af4f68a497John McCall
37219f084a3166b684573ba49df28fc5792bc37d92e1John McCall  // Forbid __weak if the runtime doesn't support it.
3722f85e193739c953358c865005855253af4f68a497John McCall  if (lifetime == Qualifiers::OCL_Weak &&
37230a7dd788dbef975f35f273c7ab913f480f7edd60John McCall      !S.getLangOpts().ObjCARCWeak && !NonObjCPointer) {
3724f85e193739c953358c865005855253af4f68a497John McCall
3725f85e193739c953358c865005855253af4f68a497John McCall    // Actually, delay this until we know what we're parsing.
3726f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
3727f85e193739c953358c865005855253af4f68a497John McCall      S.DelayedDiagnostics.add(
3728440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis          sema::DelayedDiagnostic::makeForbiddenType(
3729440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis              S.getSourceManager().getExpansionLoc(AttrLoc),
3730f85e193739c953358c865005855253af4f68a497John McCall              diag::err_arc_weak_no_runtime, type, /*ignored*/ 0));
3731f85e193739c953358c865005855253af4f68a497John McCall    } else {
3732440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis      S.Diag(AttrLoc, diag::err_arc_weak_no_runtime);
3733f85e193739c953358c865005855253af4f68a497John McCall    }
3734f85e193739c953358c865005855253af4f68a497John McCall
3735f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3736f85e193739c953358c865005855253af4f68a497John McCall    return true;
3737f85e193739c953358c865005855253af4f68a497John McCall  }
373891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
373991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // Forbid __weak for class objects marked as
3740742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  // objc_arc_weak_reference_unavailable
3741742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  if (lifetime == Qualifiers::OCL_Weak) {
3742742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    QualType T = type;
3743742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    while (const PointerType *ptr = T->getAs<PointerType>())
3744742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      T = ptr->getPointeeType();
3745742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
37464e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith      if (ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl()) {
37474e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        if (Class->isArcWeakrefUnavailable()) {
37484e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class);
37494e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(ObjT->getInterfaceDecl()->getLocation(),
37504e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith                   diag::note_class_declared);
37514e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        }
3752742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      }
3753742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    }
3754742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  }
375591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3756f85e193739c953358c865005855253af4f68a497John McCall  return true;
3757f85e193739c953358c865005855253af4f68a497John McCall}
3758f85e193739c953358c865005855253af4f68a497John McCall
3759711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
3760711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
3761711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
3762711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
3763711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
3764711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
3765711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
3766711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
3767711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3768711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
3769711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
3770711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
3771711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
3772711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
3773711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3774711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
3775711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
3776711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3777711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3778d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
37791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3780d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
3781711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!attr.getParameterName()) {
3782711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3783ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
3784711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3785711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3786ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
37870953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
3788711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getNumArgs() != 0) {
3789711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3790711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3791711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3792d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
3793711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getParameterName()->isStr("weak"))
37940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
3795711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  else if (attr.getParameterName()->isStr("strong"))
37960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
3797d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
3798711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
3799711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      << "objc_gc" << attr.getParameterName();
3800711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3801711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3802d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
38031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
380414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  QualType origType = type;
380514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  type = S.Context.getObjCGCQualType(origType, GCAttr);
380614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
380714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // Make an attributed type to preserve the source information.
380814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (attr.getLoc().isValid())
380914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
381014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                       origType, type);
381114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3812711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
3813d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
3814d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
3815e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallnamespace {
3816e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// A helper class to unwrap a type down to a function for the
3817e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// purposes of applying attributes there.
3818e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///
3819e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// Use:
3820e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
3821e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   if (unwrapped.isFunctionType()) {
3822e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     const FunctionType *fn = unwrapped.get();
3823e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     // change fn somehow
3824e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     T = unwrapped.wrap(fn);
3825e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   }
3826e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  struct FunctionTypeUnwrapper {
3827e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    enum WrapKind {
3828e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Desugar,
3829e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Parens,
3830e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Pointer,
3831e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      BlockPointer,
3832e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Reference,
3833e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      MemberPointer
3834e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    };
3835e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3836e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType Original;
3837e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *Fn;
38385f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<unsigned char /*WrapKind*/, 8> Stack;
3839e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3840e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
3841e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      while (true) {
3842e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const Type *Ty = T.getTypePtr();
3843e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<FunctionType>(Ty)) {
3844e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Fn = cast<FunctionType>(Ty);
3845e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return;
3846e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ParenType>(Ty)) {
3847e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ParenType>(Ty)->getInnerType();
3848e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Parens);
3849e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<PointerType>(Ty)) {
3850e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<PointerType>(Ty)->getPointeeType();
3851e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Pointer);
3852e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<BlockPointerType>(Ty)) {
3853e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<BlockPointerType>(Ty)->getPointeeType();
3854e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(BlockPointer);
3855e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<MemberPointerType>(Ty)) {
3856e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<MemberPointerType>(Ty)->getPointeeType();
3857e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(MemberPointer);
3858e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ReferenceType>(Ty)) {
3859e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ReferenceType>(Ty)->getPointeeType();
3860e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Reference);
3861e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else {
3862e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          const Type *DTy = Ty->getUnqualifiedDesugaredType();
3863e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          if (Ty == DTy) {
3864e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            Fn = 0;
3865e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            return;
3866e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          }
3867e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3868e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = QualType(DTy, 0);
3869e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Desugar);
3870e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        }
3871e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3872e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3873e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3874e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    bool isFunctionType() const { return (Fn != 0); }
3875e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *get() const { return Fn; }
3876e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3877e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(Sema &S, const FunctionType *New) {
3878e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // If T wasn't modified from the unwrapped type, do nothing.
3879e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (New == get()) return Original;
3880e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3881e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Fn = New;
3882e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return wrap(S.Context, Original, 0);
3883e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3884e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3885e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  private:
3886e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
3887e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size())
3888e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getQualifiedType(Fn, Old.getQualifiers());
3889e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3890e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // Build up the inner type, applying the qualifiers from the old
3891e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // type to the new type.
3892e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      SplitQualType SplitOld = Old.split();
3893e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3894e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // As a special case, tail-recurse if there are no qualifiers.
3895200fa53fd420aa8369586f569dbece04930ad6a3John McCall      if (SplitOld.Quals.empty())
3896200fa53fd420aa8369586f569dbece04930ad6a3John McCall        return wrap(C, SplitOld.Ty, I);
3897200fa53fd420aa8369586f569dbece04930ad6a3John McCall      return C.getQualifiedType(wrap(C, SplitOld.Ty, I), SplitOld.Quals);
3898e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3899e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3900e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
3901e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size()) return QualType(Fn, 0);
3902e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3903e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      switch (static_cast<WrapKind>(Stack[I++])) {
3904e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Desugar:
3905e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // This is the point at which we potentially lose source
3906e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // information.
3907e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
3908e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3909e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Parens: {
3910e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
3911e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getParenType(New);
3912e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3913e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3914e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Pointer: {
3915e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
3916e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getPointerType(New);
3917e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3918e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3919e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case BlockPointer: {
3920e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
3921e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getBlockPointerType(New);
3922e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3923e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3924e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case MemberPointer: {
3925e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
3926e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldMPT->getPointeeType(), I);
3927e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getMemberPointerType(New, OldMPT->getClass());
3928e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3929e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3930e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Reference: {
3931e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const ReferenceType *OldRef = cast<ReferenceType>(Old);
3932e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldRef->getPointeeType(), I);
3933e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<LValueReferenceType>(OldRef))
3934e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
3935e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        else
3936e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getRValueReferenceType(New);
3937e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3938e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3939e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3940e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      llvm_unreachable("unknown wrapping kind");
3941e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3942e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  };
3943e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
3944e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3945711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
3946711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
3947711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
3948711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
3949711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
3950711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
3951e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3952711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
39532455636163fdd18581d7fdae816433f886d88213Mike Stump
39548e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NoReturn) {
3955711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
395604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
3957e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3958e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
3959711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
3960711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
3961425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
3962425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
3963711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
3964711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3965711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3966711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
3967425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
3968f85e193739c953358c865005855253af4f68a497John McCall  // ns_returns_retained is not always a type attribute, but if we got
3969f85e193739c953358c865005855253af4f68a497John McCall  // here, we're treating it as one right now.
39708e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NSReturnsRetained) {
39714e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(S.getLangOpts().ObjCAutoRefCount &&
3972f85e193739c953358c865005855253af4f68a497John McCall           "ns_returns_retained treated as type attribute in non-ARC");
3973f85e193739c953358c865005855253af4f68a497John McCall    if (attr.getNumArgs()) return true;
3974f85e193739c953358c865005855253af4f68a497John McCall
3975f85e193739c953358c865005855253af4f68a497John McCall    // Delay if this is not a function type.
3976f85e193739c953358c865005855253af4f68a497John McCall    if (!unwrapped.isFunctionType())
3977f85e193739c953358c865005855253af4f68a497John McCall      return false;
3978f85e193739c953358c865005855253af4f68a497John McCall
3979f85e193739c953358c865005855253af4f68a497John McCall    FunctionType::ExtInfo EI
3980f85e193739c953358c865005855253af4f68a497John McCall      = unwrapped.get()->getExtInfo().withProducesResult(true);
3981f85e193739c953358c865005855253af4f68a497John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3982f85e193739c953358c865005855253af4f68a497John McCall    return true;
3983f85e193739c953358c865005855253af4f68a497John McCall  }
3984f85e193739c953358c865005855253af4f68a497John McCall
39858e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_Regparm) {
3986711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
3987711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
3988711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
39891e030eb1194763b42c1752723be23b1515f48981John McCall
3990711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
3991711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
3992008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
39931e030eb1194763b42c1752723be23b1515f48981John McCall
3994ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Diagnose regparm with fastcall.
3995ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    const FunctionType *fn = unwrapped.get();
3996ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    CallingConv CC = fn->getCallConv();
3997ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (CC == CC_X86FastCall) {
3998ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
3999ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC)
4000ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm";
4001ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
4002ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
4003ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
4004ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
400591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    FunctionType::ExtInfo EI =
4006711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
4007711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4008711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4009425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
4010425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
401182bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  // Delay if the type didn't work out to a function.
401282bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  if (!unwrapped.isFunctionType()) return false;
401382bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman
401404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
4015711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
4016711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
4017711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4018f82b4e85b1219295cad4b5851b035575bc293010John McCall
4019711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
4020711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
4021064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis  if (S.Context.getCanonicalCallConv(CC) ==
4022e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      S.Context.getCanonicalCallConv(CCOld)) {
4023ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
4024ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4025711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
4026e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  }
402704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
40288e68f1c8a2919ea83c2053731d6011074f1062e1Roman Divacky  if (CCOld != (S.LangOpts.MRTD ? CC_X86StdCall : CC_Default)) {
402904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Should we diagnose reapplications of the same convention?
4030711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
403104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CC)
403204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CCOld);
4033711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
4034711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
403504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
403604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
403704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
403804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
4039711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isa<FunctionNoProtoType>(fn)) {
4040711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_knr)
404104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
4042711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
4043711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
404404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
404504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
4046711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
404704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
4048711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
404904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
4050711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
4051711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
405204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
4053ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
4054ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Also diagnose fastcall with regparm.
4055a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    if (fn->getHasRegParm()) {
4056ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
4057ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm"
4058ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC);
4059ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
4060ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
4061ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
406204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
4063f82b4e85b1219295cad4b5851b035575bc293010John McCall
4064711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
4065711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
4066711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
4067f82b4e85b1219295cad4b5851b035575bc293010John McCall}
4068f82b4e85b1219295cad4b5851b035575bc293010John McCall
4069207f4d8543529221932af82836016a2ef066c917Peter Collingbourne/// Handle OpenCL image access qualifiers: read_only, write_only, read_write
4070207f4d8543529221932af82836016a2ef066c917Peter Collingbournestatic void HandleOpenCLImageAccessAttribute(QualType& CurType,
4071207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const AttributeList &Attr,
4072207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             Sema &S) {
4073207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  // Check the attribute arguments.
4074207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (Attr.getNumArgs() != 1) {
4075207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4076207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4077207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4078207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4079207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
4080207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  llvm::APSInt arg(32);
4081207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4082207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      !sizeExpr->isIntegerConstantExpr(arg, S.Context)) {
4083207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4084207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << "opencl_image_access" << sizeExpr->getSourceRange();
4085207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4086207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4087207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4088207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  unsigned iarg = static_cast<unsigned>(arg.getZExtValue());
4089207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  switch (iarg) {
4090207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_only:
4091207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_write_only:
4092207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_write:
4093207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4094207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4095207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  default:
4096207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4097207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
4098207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << sizeExpr->getSourceRange();
4099207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4100207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4101207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4102207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
4103207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
41046e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
41056e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
41066e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
41076e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
41086e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
41096e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
41106e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
4111788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
4112788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
411356affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
41146e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
41156e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4116e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
41176e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
41186e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
41196e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
41206e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
4121ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4122ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
41236e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
41246e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << "vector_size" << sizeExpr->getSourceRange();
4125e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
41266e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
41276e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
41286e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
4129f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
41306e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
4131e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
41326e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
41336e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
41346e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
41356e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
41366e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
41376e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
41386e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
41396e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
41406e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
41416e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4142e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
41436e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
41446e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
41456e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
41466e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
41476e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4148e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
41496e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
41506e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
41516e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
41526e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
41536e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
4154788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
4155e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
41566e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
41576e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
41584ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// \brief Process the OpenCL-like ext_vector_type attribute when it occurs on
41594ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// a type.
416091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierstatic void HandleExtVectorTypeAttr(QualType &CurType,
416191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                                    const AttributeList &Attr,
41624ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                    Sema &S) {
41634ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  Expr *sizeExpr;
416491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
41654ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Special case where the argument is a template id.
41664ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (Attr.getParameterName()) {
41674ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CXXScopeSpec SS;
4168e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    SourceLocation TemplateKWLoc;
41694ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    UnqualifiedId id;
41704ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
4171e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
4172e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    ExprResult Size = S.ActOnIdExpression(S.getCurScope(), SS, TemplateKWLoc,
4173e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          id, false, false);
41744ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Size.isInvalid())
41754ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
417691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
41774ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Size.get();
41784ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  } else {
41794ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    // check the attribute arguments.
41804ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Attr.getNumArgs() != 1) {
41814ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
41824ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
41834ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    }
41844ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Attr.getArg(0);
41854ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  }
418691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
41874ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Create the vector type.
41884ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  QualType T = S.BuildExtVectorType(CurType, sizeExpr, Attr.getLoc());
41894ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (!T.isNull())
41904ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CurType = T;
41914ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor}
41924ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
41934211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
41944211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
41954211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
41964211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
41974211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
41984211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
41994211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
42004211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
42014211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
42024211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     VectorType::VectorKind VecKind,
42034211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const char *AttrName) {
42044211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
42054211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
42064211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
42074211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
42084211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
42094211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
42104211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
42114211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
42124211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
42134211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
42144211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
42154211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
42164211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      << AttrName << numEltsExpr->getSourceRange();
42174211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
42184211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
42194211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
42204211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
42214211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  const BuiltinType* BTy = CurType->getAs<BuiltinType>();
42224211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (!BTy ||
42234211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (VecKind == VectorType::NeonPolyVector &&
42244211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::SChar &&
42254211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short) ||
42264211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (BTy->getKind() != BuiltinType::SChar &&
42274211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UChar &&
42284211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short &&
42294211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UShort &&
42304211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Int &&
42314211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UInt &&
42324211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::LongLong &&
42334211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::ULongLong &&
42344211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Float)) {
42354211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
42364211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
42374211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
42384211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
42394211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
42404211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
42414211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
42424211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
42434211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
42444211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
42454211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
42464211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
42474211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
42484211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
42494211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
42504211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
42514211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
4252711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
4253f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             TypeAttrLocation TAL, AttributeList *attrs) {
4254c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
4255c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
4256c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
4257c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
4258711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4259711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
4260711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
4261711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
4262711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr.getNext();
4263711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4264e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
4265711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
4266e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
4267e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
4268cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    if (attr.isCXX11Attribute()) {
4269cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // [[gnu::...]] attributes are treated as declaration attributes, so may
4270cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // not appertain to a DeclaratorChunk, even if we handle them as type
4271cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // attributes.
4272cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.getScopeName() && attr.getScopeName()->isStr("gnu")) {
4273cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        if (TAL == TAL_DeclChunk) {
4274cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          state.getSema().Diag(attr.getLoc(),
4275cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                               diag::warn_cxx11_gnu_attribute_on_type)
4276cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith              << attr.getName();
4277cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          continue;
4278cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        }
4279cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      } else if (TAL != TAL_DeclChunk) {
4280cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // Otherwise, only consider type processing for a C++11 attribute if
4281cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        // it's actually been applied to a type.
4282cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        continue;
4283cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      }
4284f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    }
4285f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
4286b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
4287b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
4288711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
4289cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    default:
4290cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      // A C++11 attribute on a declarator chunk must appertain to a type.
4291d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk) {
4292cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(), diag::err_attribute_not_type_attr)
4293d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith          << attr.getName();
4294d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith        attr.setUsedAsTypeAttr();
4295d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      }
4296cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
4297cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
4298cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::UnknownAttribute:
4299cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk)
4300cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        state.getSema().Diag(attr.getLoc(),
4301cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith                             diag::warn_unknown_attribute_ignored)
4302cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith          << attr.getName();
4303cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
4304cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith
4305cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith    case AttributeList::IgnoredAttribute:
4306cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith      break;
430704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
43088e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_MayAlias:
4309682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // FIXME: This attribute needs to actually be handled, but if we ignore
4310682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // it it breaks large amounts of Linux software.
4311682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      attr.setUsedAsTypeAttr();
4312682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      break;
43138e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_AddressSpace:
4314711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
4315e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4316c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
4317711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
4318711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
4319711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
4320e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4321d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
43228e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_VectorSize:
4323711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
4324e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4325f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
43268e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ExtVectorType:
4327a4fa9008988985f9cf01712a99ddd923aea278a0Richard Smith      HandleExtVectorTypeAttr(type, attr, state.getSema());
4328e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
43294ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      break;
43308e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonVectorType:
4331711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
4332711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonVector, "neon_vector_type");
4333e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
43344211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
43358e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonPolyVectorType:
4336711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
4337711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonPolyVector,
43384211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                               "neon_polyvector_type");
4339e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
43404211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
43418e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_OpenCLImageAccess:
4342207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      HandleOpenCLImageAccessAttribute(type, attr, state.getSema());
4343e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4344207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      break;
4345207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
4346d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith    case AttributeList::AT_Win64:
4347d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith    case AttributeList::AT_Ptr32:
4348d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith    case AttributeList::AT_Ptr64:
4349d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      // FIXME: Don't ignore these. We have partial handling for them as
4350d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      // declaration attributes in SemaDeclAttr.cpp; that should be moved here.
4351d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      attr.setUsedAsTypeAttr();
4352d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith      break;
4353d03de6aaa312d57dcd6e2bc76bed1e89f5c5019dRichard Smith
43548e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
43554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
4356cd8ab51a44e80625d84126780b0d85a7732e25afRichard Smith        break;
4357f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough into the function attrs
4358f85e193739c953358c865005855253af4f68a497John McCall
4359711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
4360e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4361e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
4362711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
4363711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
4364f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      if (TAL == TAL_DeclSpec)
4365711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
4366711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4367711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
4368711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
4369711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
43706e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
4371c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
4372711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attrs = next));
4373232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
4374232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
4375e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \brief Ensure that the type of the given expression is complete.
4376e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4377e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// This routine checks whether the expression \p E has a complete type. If the
4378e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// expression refers to an instantiable construct, that instantiation is
4379e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// performed as needed to complete its type. Furthermore
4380e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// Sema::RequireCompleteType is called for the expression's type (or in the
4381e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// case of a reference type, the referred-to type).
4382e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4383e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \param E The expression whose type is required to be complete.
4384d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// \param Diagnoser The object that will emit a diagnostic if the type is
4385d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// incomplete.
4386e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4387e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \returns \c true if the type of \p E is incomplete and diagnosed, \c false
4388e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// otherwise.
4389f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, TypeDiagnoser &Diagnoser){
4390e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  QualType T = E->getType();
4391e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4392e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Fast path the case where the type is already complete.
4393e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (!T->isIncompleteType())
4394e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    return false;
4395e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4396e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Incomplete array types may be completed by the initializer attached to
4397e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // their definitions. For static data members of class templates we need to
4398e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // instantiate the definition to get this initializer and complete the type.
4399e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (T->isIncompleteArrayType()) {
4400e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4401e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
4402e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        if (Var->isStaticDataMember() &&
4403e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth            Var->getInstantiatedFromStaticDataMember()) {
440491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
440536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
440636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          assert(MSInfo && "Missing member specialization information?");
440736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          if (MSInfo->getTemplateSpecializationKind()
440836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                != TSK_ExplicitSpecialization) {
440936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // If we don't already have a point of instantiation, this is it.
441036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (MSInfo->getPointOfInstantiation().isInvalid()) {
441136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              MSInfo->setPointOfInstantiation(E->getLocStart());
441291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
441391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier              // This is a modification of an existing AST node. Notify
441436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              // listeners.
441536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              if (ASTMutationListener *L = getASTMutationListener())
441636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                L->StaticDataMemberInstantiated(Var);
441736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
441891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
441936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            InstantiateStaticDataMemberDefinition(E->getExprLoc(), Var);
442091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
442136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // Update the type to the newly instantiated definition's type both
442236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // here and within the expression.
442336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (VarDecl *Def = Var->getDefinition()) {
442436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setDecl(Def);
442536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              T = Def->getType();
442636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setType(T);
442736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              E->setType(T);
442836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
4429f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor          }
443091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4431e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // We still go on to try to complete the type independently, as it
4432e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // may also require instantiations or diagnostics if it remains
4433e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // incomplete.
4434e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        }
4435e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      }
4436e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    }
4437e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  }
4438e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4439e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // FIXME: Are there other cases which require instantiating something other
4440e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // than the type to complete the type of an expression?
4441e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4442e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Look through reference types and complete the referred type.
4443e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
4444e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    T = Ref->getPointeeType();
4445e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4446d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(E->getExprLoc(), T, Diagnoser);
4447d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4448d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4449d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregornamespace {
4450f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  struct TypeDiagnoserDiag : Sema::TypeDiagnoser {
4451d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    unsigned DiagID;
445291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4453f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor    TypeDiagnoserDiag(unsigned DiagID)
4454f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor      : Sema::TypeDiagnoser(DiagID == 0), DiagID(DiagID) {}
445591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4456d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) {
4457d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      if (Suppressed) return;
4458d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      S.Diag(Loc, DiagID) << T;
4459d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    }
4460d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  };
4461d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4462d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4463d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, unsigned DiagID) {
4464f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4465d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteExprType(E, Diagnoser);
4466e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth}
4467e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
44681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
44694ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
44704ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
44714ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
447286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
447386447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
447486447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
447586447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
447686447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
44774ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
44784ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
44794ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
44804ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
44814ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
44824ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
44834ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
44844ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
448591a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
4486f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                               TypeDiagnoser &Diagnoser) {
4487573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
4488573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
4489690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
4490690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
4491690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
44921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
4493690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
4494690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
44954ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
4496d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  NamedDecl *Def = 0;
4497d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  if (!T->isIncompleteType(&Def)) {
4498d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    // If we know about the definition but it is not visible, complain.
4499d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (!Diagnoser.Suppressed && Def && !LookupResult::isVisible(Def)) {
4500d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // Suppress this error outside of a SFINAE context if we've already
450191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // emitted the error once for this type. There's no usefulness in
4502d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // repeating the diagnostic.
4503d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // FIXME: Add a Fix-It that imports the corresponding module or includes
4504d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // the header.
4505ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Module *Owner = Def->getOwningModule();
4506ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Loc, diag::err_module_private_definition)
4507ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        << T << Owner->getFullModuleName();
4508ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Def->getLocation(), diag::note_previous_definition);
4509ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
4510ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      if (!isSFINAEContext()) {
4511ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        // Recover by implicitly importing this module.
4512ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        createImplicitModuleImport(Loc, Owner);
4513d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      }
4514d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    }
451591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
45164ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
4517d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
45184ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
4519bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const TagType *Tag = T->getAs<TagType>();
4520bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const ObjCInterfaceType *IFace = 0;
452191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4522bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  if (Tag) {
4523bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4524bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->isInvalidDecl())
4525bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
4526bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan
4527bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4528bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->hasExternalLexicalStorage()) {
4529bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(Tag->getDecl());
4530bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!Tag->isIncompleteType())
4531bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4532bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4533bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
4534bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  else if ((IFace = T->getAs<ObjCInterfaceType>())) {
4535bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4536bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->isInvalidDecl())
4537bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
453891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4539bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4540bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->hasExternalLexicalStorage()) {
4541bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(IFace->getDecl());
4542bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!IFace->isIncompleteType())
4543bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4544bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4545bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
454691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4547d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
4548923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
4549923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
4550923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
4551e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor  while (const ConstantArrayType *Array
4552e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor           = Context.getAsConstantArrayType(MaybeTemplate))
4553923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
4554923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
45552943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
4556d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
4557972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
4558972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
4559d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
4560d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                            /*Complain=*/!Diagnoser.Suppressed);
45611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
4562d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
4563564f4c5664f552becbd05407611a92754c40e641Richard Smith      CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass();
4564564f4c5664f552becbd05407611a92754c40e641Richard Smith      if (!Rec->isBeingDefined() && Pattern) {
4565564f4c5664f552becbd05407611a92754c40e641Richard Smith        MemberSpecializationInfo *MSI = Rec->getMemberSpecializationInfo();
4566564f4c5664f552becbd05407611a92754c40e641Richard Smith        assert(MSI && "Missing member specialization information?");
4567357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
4568564f4c5664f552becbd05407611a92754c40e641Richard Smith        if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
4569f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
4570f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
4571f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
4572d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                  /*Complain=*/!Diagnoser.Suppressed);
4573d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
4574d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
4575d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
45762943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
4577d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  if (Diagnoser.Suppressed)
45785842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
4579d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
45804ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
4581d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
458291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
45834ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
458401620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
45854ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
45861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
45874ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
45884ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
4589b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor      << QualType(Tag, 0);
459091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4591b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  // If the Objective-C class was a forward declaration, produce a note.
4592b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  if (IFace && !IFace->getDecl()->isInvalidDecl())
4593b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor    Diag(IFace->getDecl()->getLocation(), diag::note_forward_class);
45944ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
45954ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
45964ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
4597e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
4598fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
459991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                               unsigned DiagID) {
4600f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4601d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(Loc, T, Diagnoser);
4602fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
4603fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
46046666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \brief Get diagnostic %select index for tag kind for
46056666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// literal type diagnostic message.
46066666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// WARNING: Indexes apply to particular diagnostics only!
46076666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos///
46086666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \returns diagnostic %select index.
4609f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matosstatic unsigned getLiteralDiagFromTagKind(TagTypeKind Tag) {
46106666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  switch (Tag) {
4611f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Struct: return 0;
4612f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Interface: return 1;
4613f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Class:  return 2;
4614f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  default: llvm_unreachable("Invalid tag kind for literal type diagnostic!");
46156666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  }
46166666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos}
46176666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos
46189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @brief Ensure that the type T is a literal type.
46199f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
46209f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// This routine checks whether the type @p T is a literal type. If @p T is an
46219f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// incomplete type, an attempt is made to complete it. If @p T is a literal
46229f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// type, or @p AllowIncompleteType is true and @p T is an incomplete type,
46239f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// returns false. Otherwise, this routine issues the diagnostic @p PD (giving
46249f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// it the type @p T), along with notes explaining why the type is not a
46259f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// literal type, and returns true.
46269f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
46279f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param Loc  The location in the source that the non-literal type
46289f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// diagnostic should refer to.
46299f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
46309f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param T  The type that this routine is examining for literalness.
46319f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
4632f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor/// @param Diagnoser Emits a diagnostic if T is not a literal type.
46339f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
46349f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @returns @c true if @p T is not a literal type and a diagnostic was emitted,
46359f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @c false otherwise.
46369f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithbool Sema::RequireLiteralType(SourceLocation Loc, QualType T,
4637f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                              TypeDiagnoser &Diagnoser) {
46389f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  assert(!T->isDependentType() && "type should not be dependent");
46399f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4640ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  QualType ElemType = Context.getBaseElementType(T);
4641ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  RequireCompleteType(Loc, ElemType, 0);
4642ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
464386c3ae46250cdcc57778c27826060779a92f3815Richard Smith  if (T->isLiteralType())
46449f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return false;
46459f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4646f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  if (Diagnoser.Suppressed)
46479f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
46489f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4649f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
46509f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
46519f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (T->isVariableArrayType())
46529f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
46539f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4654ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  const RecordType *RT = ElemType->getAs<RecordType>();
46559f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (!RT)
46569f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
46579f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
46589f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
46599f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4660c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // A partially-defined class type can't be a literal type, because a literal
4661c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // class type must have a trivial destructor (which can't be checked until
4662c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // the class definition is complete).
4663c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  if (!RD->isCompleteDefinition()) {
4664d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T);
4665ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman    return true;
4666c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  }
4667ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
46689f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // If the class has virtual base classes, then it's not an aggregate, and
466986c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // cannot have any constexpr constructors or a trivial default constructor,
467086c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // so is non-literal. This is better to diagnose than the resulting absence
467186c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // of constexpr constructors.
46729f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (RD->getNumVBases()) {
46739f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_virtual_base)
46746666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      << getLiteralDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases();
46759f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
46769f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           E = RD->vbases_end(); I != E; ++I)
467796a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      Diag(I->getLocStart(),
46789f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           diag::note_constexpr_virtual_base_here) << I->getSourceRange();
467986c3ae46250cdcc57778c27826060779a92f3815Richard Smith  } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor() &&
468086c3ae46250cdcc57778c27826060779a92f3815Richard Smith             !RD->hasTrivialDefaultConstructor()) {
46819f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD;
46829f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (RD->hasNonLiteralTypeFieldsOrBases()) {
46839f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
46849f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->bases_end(); I != E; ++I) {
46859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      if (!I->getType()->isLiteralType()) {
468696a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        Diag(I->getLocStart(),
46879f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith             diag::note_non_literal_base_class)
46889f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith          << RD << I->getType() << I->getSourceRange();
46899f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
46909f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
46919f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
46929f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::field_iterator I = RD->field_begin(),
46939f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->field_end(); I != E; ++I) {
4694262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      if (!I->getType()->isLiteralType() ||
4695262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          I->getType().isVolatileQualified()) {
4696262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        Diag(I->getLocation(), diag::note_non_literal_field)
4697581deb3da481053c4993c7600f97acf7768caac5David Blaikie          << RD << *I << I->getType()
4698262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          << I->getType().isVolatileQualified();
46999f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
47009f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
47019f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
47029f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (!RD->hasTrivialDestructor()) {
47039f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // All fields and bases are of literal types, so have trivial destructors.
47049f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // If this class's destructor is non-trivial it must be user-declared.
47059f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    CXXDestructorDecl *Dtor = RD->getDestructor();
47069f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    assert(Dtor && "class has literal fields and bases but no dtor?");
47079f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    if (!Dtor)
47089f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      return true;
47099f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
47109f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(Dtor->getLocation(), Dtor->isUserProvided() ?
47119f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_user_provided_dtor :
47129f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_nontrivial_dtor) << RD;
4713ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    if (!Dtor->isUserProvided())
4714ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith      SpecialMemberIsTrivial(Dtor, CXXDestructor, /*Diagnose*/true);
47159f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  }
47169f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
47179f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return true;
47189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
47199f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
472091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierbool Sema::RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID) {
4721f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4722f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  return RequireLiteralType(Loc, T, Diagnoser);
4723f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor}
4724f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor
4725465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
4726465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
4727465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
4728465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
4729465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
4730e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
4731465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
4732e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
4733465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
4734465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
4735465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
4736465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
4737465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
4738465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
4739465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
4740e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
4741af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
47422a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
4743fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
47442a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
47452a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
47462a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
47472b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
47482b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
4749aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
4750aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
47512b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
4752af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
4753af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
4754af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
4755f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// getDecltypeForExpr - Given an expr, will return the decltype for
4756f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// that expression, according to the rules in C++11
4757f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18.
4758f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregorstatic QualType getDecltypeForExpr(Sema &S, Expr *E) {
4759f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (E->isTypeDependent())
4760f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    return S.Context.DependentTy;
4761f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
47626d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
47636d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   The type denoted by decltype(e) is defined as follows:
47646d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //
47656d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //     - if e is an unparenthesized id-expression or an unparenthesized class
476691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       member access (5.2.5), decltype(e) is the type of the entity named
476791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       by e. If there is no such entity, or if e names a set of overloaded
47686d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       functions, the program is ill-formed;
476984dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  //
477084dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  // We apply the same rules for Objective-C ivar and property references.
4771f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
4772f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
4773f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return VD->getType();
477484dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
4775f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
4776f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return FD->getType();
477784dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCIvarRefExpr *IR = dyn_cast<ObjCIvarRefExpr>(E)) {
477884dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    return IR->getDecl()->getType();
477984dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCPropertyRefExpr *PR = dyn_cast<ObjCPropertyRefExpr>(E)) {
478084dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    if (PR->isExplicitProperty())
478184dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor      return PR->getExplicitProperty()->getType();
4782f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
478384dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor
4784f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  // C++11 [expr.lambda.prim]p18:
4785f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   Every occurrence of decltype((x)) where x is a possibly
4786f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   parenthesized id-expression that names an entity of automatic
4787f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   storage duration is treated as if x were transformed into an
4788f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   access to a corresponding data member of the closure type that
4789f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   would have been declared if x were an odr-use of the denoted
4790f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   entity.
4791f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  using namespace sema;
4792f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (S.getCurLambda()) {
4793f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (isa<ParenExpr>(E)) {
4794f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4795f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
479668932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          QualType T = S.getCapturedDeclRefType(Var, DRE->getLocation());
479768932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          if (!T.isNull())
479868932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor            return S.Context.getLValueReferenceType(T);
4799f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        }
4800f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      }
4801f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    }
4802f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
4803f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
4804f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
48056d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
48066d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   [...]
48076d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  QualType T = E->getType();
48086d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  switch (E->getValueKind()) {
480991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the
48106d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
48116d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_XValue: T = S.Context.getRValueReferenceType(T); break;
481291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an lvalue, decltype(e) is T&, where T is the
48136d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
48146d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_LValue: T = S.Context.getLValueReferenceType(T); break;
48156d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //  - otherwise, decltype(e) is the type of e.
48166d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_RValue: break;
48176d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  }
481891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4819f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return T;
4820f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor}
4821f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
48222a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
4823fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
48242a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
48252a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
482691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4827f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return Context.getDecltypeType(E, getDecltypeForExpr(*this, E));
4828af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
4829ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
4830ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType Sema::BuildUnaryTransformType(QualType BaseType,
4831ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::UTTKind UKind,
4832ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       SourceLocation Loc) {
4833ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  switch (UKind) {
4834ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case UnaryTransformType::EnumUnderlyingType:
4835ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) {
4836ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Diag(Loc, diag::err_only_enums_have_underlying_types);
4837ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return QualType();
4838ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    } else {
4839ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      QualType Underlying = BaseType;
4840ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      if (!BaseType->isDependentType()) {
4841ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl();
4842ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        assert(ED && "EnumType has no EnumDecl");
4843ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        DiagnoseUseOfDecl(ED, Loc);
4844ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        Underlying = ED->getIntegerType();
4845ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      }
4846ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(!Underlying.isNull());
4847ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return Context.getUnaryTransformType(BaseType, Underlying,
4848ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                        UnaryTransformType::EnumUnderlyingType);
4849ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
4850ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  }
4851ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  llvm_unreachable("unknown unary transform type");
4852ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
4853b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4854b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) {
4855b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T->isDependentType()) {
48568327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // FIXME: It isn't entirely clear whether incomplete atomic types
48578327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // are allowed or not; for simplicity, ban them for the moment.
4858d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (RequireCompleteType(Loc, T, diag::err_atomic_specifier_bad_type, 0))
48598327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith      return QualType();
48608327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith
4861b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    int DisallowedKind = -1;
48628327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    if (T->isArrayType())
4863b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 1;
4864b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isFunctionType())
4865b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 2;
4866b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isReferenceType())
4867b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 3;
4868b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isAtomicType())
4869b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 4;
4870b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T.hasQualifiers())
4871b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 5;
4872b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (!T.isTriviallyCopyableType(Context))
4873b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      // Some other non-trivially-copyable type (probably a C++ class)
4874b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 6;
4875b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4876b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (DisallowedKind != -1) {
4877b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T;
4878b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return QualType();
4879b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
4880b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4881b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // FIXME: Do we need any handling for ARC here?
4882b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
4883b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4884b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Build the pointer type.
4885b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return Context.getAtomicType(T);
4886b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
4887