SemaType.cpp revision 78a542478dd63c2789816dcc1cdab5c9a6eef99b
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"
157cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/Sema/Template.h"
16207f4d8543529221932af82836016a2ef066c917Peter Collingbourne#include "clang/Basic/OpenCL.h"
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
1836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor#include "clang/AST/ASTMutationListener.h"
19a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor#include "clang/AST/CXXInheritance.h"
20980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
212943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor#include "clang/AST/DeclTemplate.h"
224adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
2351bd803fbdade51d674598ed45da3d54190a656cJohn McCall#include "clang/AST/TypeLocVisitor.h"
24e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
2591a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
26d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis#include "clang/Basic/TargetInfo.h"
272792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall#include "clang/Lex/Preprocessor.h"
2819510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
29f85e193739c953358c865005855253af4f68a497John McCall#include "clang/Sema/DelayedDiagnostic.h"
30d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor#include "clang/Sema/Lookup.h"
314994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl#include "llvm/ADT/SmallPtrSet.h"
3287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor#include "llvm/Support/ErrorHandling.h"
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
355db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// isOmittedBlockReturnType - Return true if this declarator is missing a
365db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// return type because this is a omitted return type on a block literal.
378ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic bool isOmittedBlockReturnType(const Declarator &D) {
385db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getContext() != Declarator::BlockLiteralContext ||
398ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl      D.getDeclSpec().hasTypeSpecifier())
405db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return false;
415db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
425db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 0)
43a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^{ ... }
445db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
455db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 1 &&
465db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      D.getTypeObject(0).Kind == DeclaratorChunk::Function)
47a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^(int X, float Y) { ... }
485db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
495db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  return false;
505db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner}
515db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
522792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// diagnoseBadTypeAttribute - Diagnoses a type attribute which
532792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// doesn't apply to the given type.
542792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCallstatic void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr,
552792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall                                     QualType type) {
56108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth  bool useExpansionLoc = false;
572792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
582792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  unsigned diagID = 0;
592792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  switch (attr.getKind()) {
602792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  case AttributeList::AT_objc_gc:
612792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    diagID = diag::warn_pointer_attribute_wrong_type;
62108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth    useExpansionLoc = true;
632792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    break;
642792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
6505d4876a64865e34366b58fc8a6848c3cde895d9Argyrios Kyrtzidis  case AttributeList::AT_objc_ownership:
6605d4876a64865e34366b58fc8a6848c3cde895d9Argyrios Kyrtzidis    diagID = diag::warn_objc_object_attribute_wrong_type;
67108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth    useExpansionLoc = true;
6805d4876a64865e34366b58fc8a6848c3cde895d9Argyrios Kyrtzidis    break;
6905d4876a64865e34366b58fc8a6848c3cde895d9Argyrios Kyrtzidis
702792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  default:
712792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    // Assume everything else was a function attribute.
722792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    diagID = diag::warn_function_attribute_wrong_type;
732792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    break;
742792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  }
752792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
762792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  SourceLocation loc = attr.getLoc();
775f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef name = attr.getName()->getName();
782792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
792792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  // The GC attributes are usually written with macros;  special-case them.
80108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth  if (useExpansionLoc && loc.isMacroID() && attr.getParameterName()) {
81834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    if (attr.getParameterName()->isStr("strong")) {
82834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__strong")) name = "__strong";
83834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    } else if (attr.getParameterName()->isStr("weak")) {
84834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__weak")) name = "__weak";
852792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    }
862792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  }
872792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
882792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  S.Diag(loc, diagID) << name << type;
892792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall}
902792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
91711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// objc_gc applies to Objective-C pointers or, otherwise, to the
92711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// smallest available pointer type (i.e. 'void*' in 'void**').
93711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
94f85e193739c953358c865005855253af4f68a497John McCall    case AttributeList::AT_objc_gc: \
95b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis    case AttributeList::AT_objc_ownership
96711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
97711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// Function type attributes.
98711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define FUNCTION_TYPE_ATTRS_CASELIST \
99711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_noreturn: \
100711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_cdecl: \
101711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_fastcall: \
102711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_stdcall: \
103711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_thiscall: \
104711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_pascal: \
105414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    case AttributeList::AT_regparm: \
106414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    case AttributeList::AT_pcs \
107711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
108711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace {
109711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// An object which stores processing state for the entire
110711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// GetTypeForDeclarator process.
111711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  class TypeProcessingState {
112711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &sema;
113711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
114711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The declarator being processed.
115711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &declarator;
116711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
117711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The index of the declarator chunk we're currently processing.
118711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// May be the total number of valid chunks, indicating the
119711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// DeclSpec.
120711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex;
121711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
122711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Whether there are non-trivial modifications to the decl spec.
123711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    bool trivial;
124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1257ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    /// Whether we saved the attributes in the decl spec.
1267ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    bool hasSavedAttrs;
1277ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
128711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The original set of attributes on the DeclSpec.
1295f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> savedAttrs;
130711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
131711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// A list of attributes to diagnose the uselessness of when the
132711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// processing is complete.
1335f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> ignoredTypeAttrs;
134711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
135711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  public:
136711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    TypeProcessingState(Sema &sema, Declarator &declarator)
137711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : sema(sema), declarator(declarator),
138711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        chunkIndex(declarator.getNumTypeObjects()),
1397ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        trivial(true), hasSavedAttrs(false) {}
140711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
141711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &getSema() const {
142711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return sema;
143711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
144711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
145711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &getDeclarator() const {
146711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator;
147711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
148711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
149711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned getCurrentChunkIndex() const {
150711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return chunkIndex;
151711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
152711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
153711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void setCurrentChunkIndex(unsigned idx) {
154711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(idx <= declarator.getNumTypeObjects());
155711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      chunkIndex = idx;
156711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
157711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
158711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&getCurrentAttrListRef() const {
159711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(chunkIndex <= declarator.getNumTypeObjects());
160711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (chunkIndex == declarator.getNumTypeObjects())
161711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        return getMutableDeclSpec().getAttributes().getListRef();
162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator.getTypeObject(chunkIndex).getAttrListRef();
163711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Save the current set of attributes on the DeclSpec.
166711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void saveDeclSpecAttrs() {
167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Don't try to save them multiple times.
1687ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (hasSavedAttrs) return;
169711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
170711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      DeclSpec &spec = getMutableDeclSpec();
171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (AttributeList *attr = spec.getAttributes().getList(); attr;
172711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             attr = attr->getNext())
173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs.push_back(attr);
174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      trivial &= savedAttrs.empty();
1757ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      hasSavedAttrs = true;
176711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
177711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
178711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Record that we had nowhere to put the given type attribute.
179711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// We will diagnose such attributes later.
180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void addIgnoredTypeAttr(AttributeList &attr) {
181711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      ignoredTypeAttrs.push_back(&attr);
182711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
183711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Diagnose all the ignored type attributes, given that the
185711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// declarator worked out to the given type.
186711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void diagnoseIgnoredTypeAttrs(QualType type) const {
1875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      for (SmallVectorImpl<AttributeList*>::const_iterator
188711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
1892792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall           i != e; ++i)
1902792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall        diagnoseBadTypeAttribute(getSema(), **i, type);
191711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
192711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
193711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    ~TypeProcessingState() {
194711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (trivial) return;
195711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
196711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      restoreDeclSpecAttrs();
197711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
198711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
199711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  private:
200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclSpec &getMutableDeclSpec() const {
201711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return const_cast<DeclSpec&>(declarator.getDeclSpec());
202711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
203711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
204711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void restoreDeclSpecAttrs() {
2057ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      assert(hasSavedAttrs);
2067ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
2077ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (savedAttrs.empty()) {
2087ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        getMutableDeclSpec().getAttributes().set(0);
2097ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        return;
2107ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      }
2117ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
212711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
213711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
214711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs[i]->setNext(savedAttrs[i+1]);
215711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      savedAttrs.back()->setNext(0);
216711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
217711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
218711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
219711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// Basically std::pair except that we really want to avoid an
220711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// implicit operator= for safety concerns.  It's also a minor
221711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// link-time optimization for this to be a private type.
222711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  struct AttrAndList {
223711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The attribute.
224711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &first;
225711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
226711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The head of the list the attribute is currently in.
227711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&second;
228711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
229711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttrAndList(AttributeList &attr, AttributeList *&head)
230711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : first(attr), second(head) {}
231711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
23204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
23304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
234711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace llvm {
235711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  template <> struct isPodLike<AttrAndList> {
236711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    static const bool value = true;
237711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
238711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
239711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
240711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
241711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  attr.setNext(head);
242711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  head = &attr;
243711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
244711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
245711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
246711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (head == &attr) {
247711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    head = attr.getNext();
248711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
24904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
250711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
251711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *cur = head;
252711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  while (true) {
253711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    assert(cur && cur->getNext() && "ran out of attrs?");
254711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (cur->getNext() == &attr) {
255711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      cur->setNext(attr.getNext());
256711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
257711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
258711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    cur = cur->getNext();
259711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
260711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
261711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
262711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void moveAttrFromListToList(AttributeList &attr,
263711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&fromList,
264711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&toList) {
265711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, fromList);
266711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrIntoList(attr, toList);
267711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
268711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
269711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state,
270711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             QualType &type, bool isDeclSpec,
271711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             AttributeList *attrs);
272711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
273711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
274711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
275711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type);
276711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
277711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
278711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr, QualType &type);
279711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
280b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
281f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr, QualType &type);
282f85e193739c953358c865005855253af4f68a497John McCall
283711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCPointerTypeAttr(TypeProcessingState &state,
284711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr, QualType &type) {
285f85e193739c953358c865005855253af4f68a497John McCall  if (attr.getKind() == AttributeList::AT_objc_gc)
286f85e193739c953358c865005855253af4f68a497John McCall    return handleObjCGCTypeAttr(state, attr, type);
287b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  assert(attr.getKind() == AttributeList::AT_objc_ownership);
288b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  return handleObjCOwnershipTypeAttr(state, attr, type);
289711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
290711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
291711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Given that an objc_gc attribute was written somewhere on a
292711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declaration *other* than on the declarator itself (for which, use
293711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
294711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// didn't apply in whatever position it was written in, try to move
295711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// it to a more appropriate position.
296711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeObjCPointerTypeAttr(TypeProcessingState &state,
297711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          AttributeList &attr,
298711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType type) {
299711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
300711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
301711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
302711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
303711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
304711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
305711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
306711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
307711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
308711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
309711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
310711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
311711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
312711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
313711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Don't walk through these.
314711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
315711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
316711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
317711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto error;
318711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
319711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
320711c52bb20d0c69063b52a99826fb7d2835501f1John McCall error:
3212792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
3222792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
323711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
324711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
325711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Distribute an objc_gc type attribute that was written on the
326711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator.
327711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
328711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
329711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            AttributeList &attr,
330711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            QualType &declSpecType) {
331711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
332711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
333711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // objc_gc goes on the innermost pointer to something that's not a
334711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // pointer.
335711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  unsigned innermost = -1U;
336711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  bool considerDeclSpec = true;
337711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
338711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
339711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
340711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
341711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
342711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      innermost = i;
343ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      continue;
344711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
345711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
346711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
347711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
348711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
349711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
350711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
351711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
352711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      considerDeclSpec = false;
353711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto done;
354711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
355711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
356711c52bb20d0c69063b52a99826fb7d2835501f1John McCall done:
357711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
358711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // That might actually be the decl spec if we weren't blocked by
359711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // anything in the declarator.
360711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (considerDeclSpec) {
3617ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    if (handleObjCPointerTypeAttr(state, attr, declSpecType)) {
3627ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // Splice the attribute into the decl spec.  Prevents the
3637ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // attribute from being applied multiple times and gives
3647ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // the source-location-filler something to work with.
3657ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      state.saveDeclSpecAttrs();
3667ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      moveAttrFromListToList(attr, declarator.getAttrListRef(),
3677ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall               declarator.getMutableDeclSpec().getAttributes().getListRef());
368711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
3697ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    }
370711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
371711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
372711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, if we found an appropriate chunk, splice the attribute
373711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // into it.
374711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (innermost != -1U) {
375711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, declarator.getAttrListRef(),
376711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                       declarator.getTypeObject(innermost).getAttrListRef());
377711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
378711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
379711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
380711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, diagnose when we're done building the type.
381711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
382711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
383711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
384711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
385711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written somewhere in a declaration
386711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// *other* than on the declarator itself or in the decl spec.  Given
387711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// that it didn't apply in whatever position it was written in, try
388711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// to move it to a more appropriate position.
389711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeFunctionTypeAttr(TypeProcessingState &state,
390711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
391711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType type) {
392711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
393711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
394711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to push the attribute from the return type of a function to
395711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the function itself.
396711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
397711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
398711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
399711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
400711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
401711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
402711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
403711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
404711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
405711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
406711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
407711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
408711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
409711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
410711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
411711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
412711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4142792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
415711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
416711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
417711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Try to distribute a function type attribute to the innermost
418711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// function chunk or type.  Returns true if the attribute was
419711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributed, false if no location was found.
420711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool
421711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
422711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr,
423711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList *&attrList,
424711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      QualType &declSpecType) {
425711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
426711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
427711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Put it on the innermost function chunk, if there is one.
428711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
429711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
430711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (chunk.Kind != DeclaratorChunk::Function) continue;
431711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
432711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
433711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
434711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
435711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
436f85e193739c953358c865005855253af4f68a497John McCall  if (handleFunctionTypeAttr(state, attr, declSpecType)) {
437f85e193739c953358c865005855253af4f68a497John McCall    spliceAttrOutOfList(attr, attrList);
438f85e193739c953358c865005855253af4f68a497John McCall    return true;
439f85e193739c953358c865005855253af4f68a497John McCall  }
440f85e193739c953358c865005855253af4f68a497John McCall
441f85e193739c953358c865005855253af4f68a497John McCall  return false;
442711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
443711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written in the decl spec.  Try to
445711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
446711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
447711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
448711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
449711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType &declSpecType) {
450711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.saveDeclSpecAttrs();
451711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
452711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
453711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
454711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            state.getCurrentAttrListRef(),
455711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
456711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
457711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
458711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
460711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
461711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
462711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
463711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written on the declarator.  Try to
464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
465711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
466711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
467711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         AttributeList &attr,
468711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         QualType &declSpecType) {
469711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
470711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
471711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
472711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
473711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declarator.getAttrListRef(),
474711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
475711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
476711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
477711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
478711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
479711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
480711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
482711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// \brief Given that there are attributes written on the declarator
484711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// itself, try to distribute any type attributes to the appropriate
485711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator chunk.
486711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///
487711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// These are attributes like the following:
488711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f ATTR;
489711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int (f ATTR)();
490711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// but not necessarily this:
491711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f() ATTR;
492711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
493711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                              QualType &declSpecType) {
494711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Collect all the type attributes from the declarator itself.
495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *attr = state.getDeclarator().getAttributes();
497711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
498711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
499711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr->getNext();
500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
501711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr->getKind()) {
502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
505711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
506f85e193739c953358c865005855253af4f68a497John McCall    case AttributeList::AT_ns_returns_retained:
507f85e193739c953358c865005855253af4f68a497John McCall      if (!state.getSema().getLangOptions().ObjCAutoRefCount)
508f85e193739c953358c865005855253af4f68a497John McCall        break;
509f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough
510f85e193739c953358c865005855253af4f68a497John McCall
511711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
512711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
513711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
514711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    default:
516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attr = next));
519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
520711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
521711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Add a synthetic '()' to a block-literal declarator if it is
522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// required, given the return type.
523711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void maybeSynthesizeBlockSignature(TypeProcessingState &state,
524711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType declSpecType) {
525711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
526711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
527711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // First, check whether the declarator would produce a function,
528711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // i.e. whether the innermost semantic chunk is a function.
529711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.isFunctionDeclarator()) {
530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // If so, make that declarator a prototyped declarator.
531711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.getFunctionTypeInfo().hasPrototype = true;
532711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
534711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
535da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // If there are any type objects, the type as written won't name a
536da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // function, regardless of the decl spec type.  This is because a
537da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // block signature declarator is always an abstract-declarator, and
538da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // abstract-declarators can't just be parentheses chunks.  Therefore
539da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // we need to build a function chunk unless there are no type
540da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // objects and the decl spec type is a function.
541711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
542711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
543711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
544da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // Note that there *are* cases with invalid declarators where
545da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // declarators consist solely of parentheses.  In general, these
546da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // occur only in failed efforts to make function declarators, so
547da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // faking up the function chunk is still the right thing to do.
548711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
549711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, we need to fake up a function declarator.
550711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation loc = declarator.getSourceRange().getBegin();
551711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
552711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // ...and *prepend* it to the declarator.
553711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
554711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*proto*/ true,
555711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*variadic*/ false, SourceLocation(),
556711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*args*/ 0, 0,
557711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*type quals*/ 0,
55883f51722ed2b8134810cb178f39e44da811de7cdDouglas Gregor                             /*ref-qualifier*/true, SourceLocation(),
55943f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                             /*const qualifier*/SourceLocation(),
56043f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                             /*volatile qualifier*/SourceLocation(),
56190ebed0734fac9b464c9bdff53fbf85a86b27f32Douglas Gregor                             /*mutable qualifier*/SourceLocation(),
5626e5d319b671dbb0ecf70619834aa23c853d17621Sebastian Redl                             /*EH*/ EST_None, SourceLocation(), 0, 0, 0, 0,
563711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*parens*/ loc, loc,
564711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             declarator));
565711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
566711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // For consistency, make sure the state still has us as processing
567711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the decl spec.
568711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
569711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.setCurrentChunkIndex(declarator.getNumTypeObjects());
57004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
57104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
572930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
573930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
5745db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// \param D  the declarator containing the declaration specifier.
5755153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
5765153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
5778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType ConvertDeclSpecToType(TypeProcessingState &state) {
5785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
5795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
580711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
582711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
583711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const DeclSpec &DS = declarator.getDeclSpec();
584711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation DeclLoc = declarator.getIdentifierLoc();
5855db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
5865db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    DeclLoc = DS.getSourceRange().getBegin();
5871564e3906cad604a42bd131e584751a75589a9c4Chris Lattner
588711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  ASTContext &Context = S.Context;
5891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5905db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
5915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
59296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
59396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
59496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
5955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
5965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
597fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
5985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
599fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
6005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
6015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
6025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
603fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
605958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
60664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
60764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
60864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
60964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
610711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
611f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
61264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
61364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
61464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
61564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
616711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
617f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
61864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
61964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
62064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
621f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
622f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
623f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
624f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
625f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
626f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
627f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
628f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
629f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
630f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
631d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
63262f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
633097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
634c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
635c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         (ObjCProtocolDecl**)PQ,
636c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         DS.getNumProtocolQualifiers());
637c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectPointerType(Result);
63862f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
63962f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
6405db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
6415db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
6425db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
643f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // The declspec is always missing in a lambda expr context; it is either
644f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // specified with a trailing return type or inferred.
645f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    if (declarator.getContext() == Declarator::LambdaExprContext ||
646f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman        isOmittedBlockReturnType(declarator)) {
6475db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      Result = Context.DependentTy;
6485db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
6495db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
6501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
651d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
652d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
653d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
654d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
655d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
656d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
657d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
658711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.getLangOptions().ImplicitInt) {
65935d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
66035d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
6613f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
662711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_declspec)
6633f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
664849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int");
6653f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
6664310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
667d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
668d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
669d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
670d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
6714310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
672711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (S.getLangOptions().CPlusPlus &&
67362ec1f2fd7368542bb926c04797fb07023547694Francois Pichet          !S.getLangOptions().MicrosoftExt) {
674711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_missing_type_specifier)
6753f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
677b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
678b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
679b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
680711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
681b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
682711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_type_specifier)
6833f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
684b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
685d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
6861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
6883cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
6895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
6905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
691fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
692fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
693fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
694311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
695311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
696311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
697311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
698ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith        if (!S.getLangOptions().C99)
699ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith          S.Diag(DS.getTypeSpecWidthLoc(),
700ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith                 S.getLangOptions().CPlusPlus0x ?
701ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith                   diag::warn_cxx98_compat_longlong : diag::ext_longlong);
702311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
7035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
7045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
7055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
706fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
707fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
708fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
709311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
710311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
711311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
712311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
713ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith        if (!S.getLangOptions().C99)
714ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith          S.Diag(DS.getTypeSpecWidthLoc(),
715ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith                 S.getLangOptions().CPlusPlus0x ?
716ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith                   diag::warn_cxx98_compat_longlong : diag::ext_longlong);
717311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
7185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
7195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
720958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7213cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
722aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case DeclSpec::TST_half: Result = Context.HalfTy; break;
723fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
724958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
725958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
726fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
727958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
728fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
72939d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne
73039d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    if (S.getLangOptions().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
73139d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
73239d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      declarator.setInvalidType(true);
73339d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    }
734958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
735fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
7365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
7375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
7385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
739711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
7408f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
741711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
7428f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
74399dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
7445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
7455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
747b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
7486e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
7496e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
7506e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
751711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
7526e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
7536e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
7546e24726524c2b51b31bb4b622aa678a46b024f42John McCall
755a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
7560daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc());
757a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
759a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
760a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
762a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
7632191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
7640daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    // In both C and C++, make an ElaboratedType.
7650daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    ElaboratedTypeKeyword Keyword
7660daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
7670daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
7680daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara
7695153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    if (D->isInvalidDecl())
770711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
771958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7731a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
7745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
7755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
7765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
777711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
77827940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    if (Result.isNull())
779711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
78027940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    else if (DeclSpec::ProtocolQualifierListTy PQ
78127940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall               = DS.getProtocolQualifiers()) {
782c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
783c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // Silently drop any existing protocol qualifiers.
784c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // TODO: determine whether that's the right thing to do.
785c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjT->getNumProtocols())
786c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = ObjT->getBaseType();
787c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
788c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (DS.getNumProtocolQualifiers())
789c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = Context.getObjCObjectType(Result,
790c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             (ObjCProtocolDecl**) PQ,
791c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             DS.getNumProtocolQualifiers());
792c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCIdType()) {
793ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
794c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
795c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
796c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
797c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
798c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCClassType()) {
7994262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
800c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
801c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
802c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
803c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
8043f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
805711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
8063f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
807711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
8083f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
809c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
8101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
812958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
8135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
814958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
815e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
816711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
817958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
818730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian    if (!Result->isDependentType())
819730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian      if (const TagType *TT = Result->getAs<TagType>())
820711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
821d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
822fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
823958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
824d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
825b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
826d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
827d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
828711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
8294b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
8304b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
831711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
8324b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
833958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
834d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
8356fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
836b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
8376fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
8386fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
839711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
840af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
841af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
842711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
843af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
8446fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
8456fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
846ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case DeclSpec::TST_underlyingType:
847db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    Result = S.GetTypeFromParser(DS.getRepAsType());
848db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    assert(!Result.isNull() && "Didn't get a type for __underlying_type?");
849ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    Result = S.BuildUnaryTransformType(Result,
850ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::EnumUnderlyingType,
851ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       DS.getTypeSpecTypeLoc());
852ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (Result.isNull()) {
853ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Result = Context.IntTy;
854ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      declarator.setInvalidType(true);
855db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    }
856db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    break;
857db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
858e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
859e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
86034b41d939a1328f484511c6002ba2456db879a29Richard Smith    Result = Context.getAutoType(QualType());
861e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
862e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
8631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
864a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall  case DeclSpec::TST_unknown_anytype:
865a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    Result = Context.UnknownAnyTy;
866a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    break;
867a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall
868b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case DeclSpec::TST_atomic:
869b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.GetTypeFromParser(DS.getRepAsType());
870b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(!Result.isNull() && "Didn't get a type for _Atomic?");
871b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc());
872b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Result.isNull()) {
873b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Result = Context.IntTy;
874b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      declarator.setInvalidType(true);
875b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
876b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    break;
877b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
878809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
8795153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
880711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
8815153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
8825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
884958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
885f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
886711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.getLangOptions().Freestanding)
887711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
888fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
88982287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
89082287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
89182287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
892e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
893788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
894e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
895788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
896e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
897e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
898f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
8991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90047423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
90147423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
902711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
904711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
905711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
906711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
907711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
908711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
909711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
910711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
911711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
912711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (AttributeList *attrs = DS.getAttributes().getList())
913711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    processTypeAttrs(state, Result, true, attrs);
9141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
91596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
91696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
91796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
91896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
91996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
92096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
9210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (TypeQuals & DeclSpec::TQ_restrict) {
9222b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian      if (Result->isAnyPointerType() || Result->isReferenceType()) {
9232b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        QualType EltTy;
9242b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        if (Result->isObjCObjectPointerType())
9252b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result;
9262b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        else
9272b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result->isPointerType() ?
9282b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<PointerType>()->getPointeeType() :
9292b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<ReferenceType>()->getPointeeType();
9301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
931bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
932bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
933bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
934711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getRestrictSpecLoc(),
935d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
936d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
9370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall          TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
938bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
939bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
940711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DS.getRestrictSpecLoc(),
941711c52bb20d0c69063b52a99826fb7d2835501f1John McCall               diag::err_typecheck_invalid_restrict_not_pointer)
942d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
9430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
94496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
94596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
9461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
94796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
94896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
94996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
95096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
95196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
95296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
9530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
95496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
9550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
95696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
9570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
9580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        assert((TypeQuals & DeclSpec::TQ_restrict) &&
9590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall               "Has CVR quals but not C, V, or R?");
9600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Loc = DS.getRestrictSpecLoc();
96196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
962711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(Loc, diag::warn_typecheck_function_qualifiers)
963d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
96496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
9651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
966f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
967f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
968f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
969f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
970f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
9711a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
9721a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
973f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
9740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
9750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
9761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
9771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
9790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Result = Context.getQualifiedType(Result, Quals);
98096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
9810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
982f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
983f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
984f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
985cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
986cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
987cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
9881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
989cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
990cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
991cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
9922865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
9932865474261a608c7873b87ba4af110d17907896dJohn McCall                                  Qualifiers Qs) {
9942865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
9952865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
9962865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
9972865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
9982865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
9992865474261a608c7873b87ba4af110d17907896dJohn McCall
10002865474261a608c7873b87ba4af110d17907896dJohn McCall    const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
10012865474261a608c7873b87ba4af110d17907896dJohn McCall    if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
10022865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
10032865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
10042865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
10052865474261a608c7873b87ba4af110d17907896dJohn McCall      }
10062865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
10072865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
10082865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
10092865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
10102865474261a608c7873b87ba4af110d17907896dJohn McCall      }
10112865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
10122865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
10132865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
10142865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
10152865474261a608c7873b87ba4af110d17907896dJohn McCall      }
10162865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (!Ty->isDependentType()) {
10172865474261a608c7873b87ba4af110d17907896dJohn McCall      // FIXME: this deserves a proper diagnostic
10182865474261a608c7873b87ba4af110d17907896dJohn McCall      DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
10192865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
10202865474261a608c7873b87ba4af110d17907896dJohn McCall    }
10212865474261a608c7873b87ba4af110d17907896dJohn McCall
10222865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
10232865474261a608c7873b87ba4af110d17907896dJohn McCall      Diag(Loc, DiagID) << ProblemTy;
10242865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
10252865474261a608c7873b87ba4af110d17907896dJohn McCall    }
10262865474261a608c7873b87ba4af110d17907896dJohn McCall  }
10272865474261a608c7873b87ba4af110d17907896dJohn McCall
10282865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
10292865474261a608c7873b87ba4af110d17907896dJohn McCall}
10302865474261a608c7873b87ba4af110d17907896dJohn McCall
1031075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
1032075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
1033075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
1034075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
1035075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
1036f85e193739c953358c865005855253af4f68a497John McCall/// Given that we're building a pointer or reference to the given
1037f85e193739c953358c865005855253af4f68a497John McCallstatic QualType inferARCLifetimeForPointee(Sema &S, QualType type,
1038f85e193739c953358c865005855253af4f68a497John McCall                                           SourceLocation loc,
1039f85e193739c953358c865005855253af4f68a497John McCall                                           bool isReference) {
1040f85e193739c953358c865005855253af4f68a497John McCall  // Bail out if retention is unrequired or already specified.
1041f85e193739c953358c865005855253af4f68a497John McCall  if (!type->isObjCLifetimeType() ||
1042f85e193739c953358c865005855253af4f68a497John McCall      type.getObjCLifetime() != Qualifiers::OCL_None)
1043f85e193739c953358c865005855253af4f68a497John McCall    return type;
1044f85e193739c953358c865005855253af4f68a497John McCall
1045f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None;
1046f85e193739c953358c865005855253af4f68a497John McCall
1047f85e193739c953358c865005855253af4f68a497John McCall  // If the object type is const-qualified, we can safely use
1048f85e193739c953358c865005855253af4f68a497John McCall  // __unsafe_unretained.  This is safe (because there are no read
1049f85e193739c953358c865005855253af4f68a497John McCall  // barriers), and it'll be safe to coerce anything but __weak* to
1050f85e193739c953358c865005855253af4f68a497John McCall  // the resulting type.
1051f85e193739c953358c865005855253af4f68a497John McCall  if (type.isConstQualified()) {
1052f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
1053f85e193739c953358c865005855253af4f68a497John McCall
1054f85e193739c953358c865005855253af4f68a497John McCall  // Otherwise, check whether the static type does not require
1055f85e193739c953358c865005855253af4f68a497John McCall  // retaining.  This currently only triggers for Class (possibly
1056f85e193739c953358c865005855253af4f68a497John McCall  // protocol-qualifed, and arrays thereof).
1057f85e193739c953358c865005855253af4f68a497John McCall  } else if (type->isObjCARCImplicitlyUnretainedType()) {
1058f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
10595b76f373d23cc3b292ecf523349aaaa388eea375Argyrios Kyrtzidis
1060ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // If we are in an unevaluated context, like sizeof, skip adding a
1061ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // qualification.
106278a542478dd63c2789816dcc1cdab5c9a6eef99bEli Friedman  } else if (S.ExprEvalContexts.back().Context == Sema::Unevaluated) {
1063ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    return type;
1064f85e193739c953358c865005855253af4f68a497John McCall
1065f85e193739c953358c865005855253af4f68a497John McCall  // If that failed, give an error and recover using __autoreleasing.
1066f85e193739c953358c865005855253af4f68a497John McCall  } else {
1067f85e193739c953358c865005855253af4f68a497John McCall    // These types can show up in private ivars in system headers, so
1068f85e193739c953358c865005855253af4f68a497John McCall    // we need this to not be an error in those cases.  Instead we
1069f85e193739c953358c865005855253af4f68a497John McCall    // want to delay.
1070f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1071ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.DelayedDiagnostics.add(
1072ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman          sema::DelayedDiagnostic::makeForbiddenType(loc,
1073ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman              diag::err_arc_indirect_no_ownership, type, isReference));
1074f85e193739c953358c865005855253af4f68a497John McCall    } else {
1075ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference;
1076f85e193739c953358c865005855253af4f68a497John McCall    }
1077f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_Autoreleasing;
1078f85e193739c953358c865005855253af4f68a497John McCall  }
1079f85e193739c953358c865005855253af4f68a497John McCall  assert(implicitLifetime && "didn't infer any lifetime!");
1080f85e193739c953358c865005855253af4f68a497John McCall
1081f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers qs;
1082f85e193739c953358c865005855253af4f68a497John McCall  qs.addObjCLifetime(implicitLifetime);
1083f85e193739c953358c865005855253af4f68a497John McCall  return S.Context.getQualifiedType(type, qs);
1084f85e193739c953358c865005855253af4f68a497John McCall}
1085f85e193739c953358c865005855253af4f68a497John McCall
1086cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
1087cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1088cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
1089cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1090cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1091cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
1092cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
1093cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1094cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
1095cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1096cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1097cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
1098cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
10992865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
1100cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
1101cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
1102cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
1103cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
1104ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1105cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1106cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1107cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1108c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
110992e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
1110f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build pointers to unqualified pointers.
1111f85e193739c953358c865005855253af4f68a497John McCall  if (getLangOptions().ObjCAutoRefCount)
1112f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false);
1113f85e193739c953358c865005855253af4f68a497John McCall
1114cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
11152865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
1116cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1117cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1118cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
1119cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1120cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
1121cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1122cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1123cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
1124cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
1125cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1126cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
1127cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1128cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1129cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
1130cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
113154e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
11322865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
113354e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
11349625e44c0252485277a340746ed8ac950686156fDouglas Gregor  assert(Context.getCanonicalType(T) != Context.OverloadTy &&
11359625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
11369625e44c0252485277a340746ed8ac950686156fDouglas Gregor
113769d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // C++0x [dcl.ref]p6:
113869d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   If a typedef (7.1.3), a type template-parameter (14.3.1), or a
113969d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
114069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   type T, an attempt to create the type "lvalue reference to cv TR" creates
114169d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   the type "lvalue reference to T", while an attempt to create the type
114269d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   "rvalue reference to cv TR" creates the type TR.
114354e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
114454e14c4db764c0636160d26c5bbf491637c83a76John McCall
114554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
114654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
114754e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
114854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
114954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
115054e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
115154e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
115254e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
115354e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
115454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
115554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
115669d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // collapsing of references-to-references as described in C++0x.
115769d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // DR 106 and 540 introduce reference-collapsing into C++98/03.
1158cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1159cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
116033a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
1161cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
1162cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
1163cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
1164cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1165cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1166cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1167f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build references to unqualified pointers.
1168f85e193739c953358c865005855253af4f68a497John McCall  if (getLangOptions().ObjCAutoRefCount)
1169f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true);
1170f85e193739c953358c865005855253af4f68a497John McCall
1171cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
11727c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
11732865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
11742865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
1175cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1176cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1177e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// Check whether the specified array size makes the array type a VLA.  If so,
1178e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// return true, if not, return the size of the array in SizeVal.
1179e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattnerstatic bool isArraySizeVLA(Expr *ArraySize, llvm::APSInt &SizeVal, Sema &S) {
1180e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner  // If the size is an ICE, it certainly isn't a VLA.
1181e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner  if (ArraySize->isIntegerConstantExpr(SizeVal, S.Context))
1182e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    return false;
1183e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1184e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner  // If we're in a GNU mode (like gnu99, but not c99) accept any evaluatable
1185e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner  // value as an extension.
118680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (S.LangOpts.GNUMode && ArraySize->EvaluateAsInt(SizeVal, S.Context)) {
118780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    S.Diag(ArraySize->getLocStart(), diag::ext_vla_folded_to_constant);
118880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return false;
1189e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner  }
1190e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1191e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner  return true;
1192e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner}
1193e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1194e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1195cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
1196cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1197cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
1198cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1199cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
12001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
12011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
1202cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1203cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1204cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// array type or, if there is no such entity, the location of the
1205cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have array type.
1206cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1207cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
1208cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1209cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1210cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
1211cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
1212cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1213cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
12147e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
12150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
12167e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
1217923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (getLangOptions().CPlusPlus) {
1218138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
1219138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
1220138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   type, the (possibly cv-qualified) type void, a function type or an
1221138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
1222138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
1223138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
1224138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
1225138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
1226138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
1227138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1228138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
1229138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1230923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (T->isVoidType()) {
1231923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1232923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1233923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1234138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1235138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (RequireNonAbstractType(Brackets.getBegin(), T,
1236138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
1237138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1238138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1239923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
1240138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1241138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
1242923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
1243923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
1244923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1245923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
1246cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1247cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
1248cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
1249ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1250cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1251cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
12521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
125334b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (T->getContainedAutoType()) {
125434b41d939a1328f484511c6002ba2456db879a29Richard Smith    Diag(Loc, diag::err_illegal_decl_array_of_auto)
125534b41d939a1328f484511c6002ba2456db879a29Richard Smith      << getPrintableNameForEntity(Entity) << T;
1256e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
1257e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
12581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12596217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
1260cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
1261cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
1262cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
1263cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
1264c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
1265c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1266c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
1267cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
12681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1269806054db6653d29cb0d9692df3612cbcd03d0530John McCall  // Do placeholder conversions on the array size expression.
1270806054db6653d29cb0d9692df3612cbcd03d0530John McCall  if (ArraySize && ArraySize->hasPlaceholderType()) {
1271806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ExprResult Result = CheckPlaceholderExpr(ArraySize);
1272806054db6653d29cb0d9692df3612cbcd03d0530John McCall    if (Result.isInvalid()) return QualType();
1273806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ArraySize = Result.take();
1274806054db6653d29cb0d9692df3612cbcd03d0530John McCall  }
1275806054db6653d29cb0d9692df3612cbcd03d0530John McCall
12765e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
1277429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ArraySize && !ArraySize->isRValue()) {
1278429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult Result = DefaultLvalueConversion(ArraySize);
1279429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Result.isInvalid())
1280429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return QualType();
1281429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
1282429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ArraySize = Result.take();
1283429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
12845e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1285cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
12865e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // TODO: in theory, if we were insane, we could allow contextual
12875e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // conversions to integer type here.
1288cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (ArraySize && !ArraySize->isTypeDependent() &&
12891274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1290cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1291cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
1292cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1293cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
12942767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
1295cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
1296f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
12977e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
1298f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
1299f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
1300ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
13017e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
1302e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner  } else if (!T->isDependentType() && !T->isIncompleteType() &&
1303e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner             !T->isConstantSizeType()) {
1304e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with an element type that has a non-constant-size is a VLA.
1305e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1306e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner  } else if (isArraySizeVLA(ArraySize, ConstVal, *this)) {
1307e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with a non-ICE size is a VLA.  We accept any expression
1308e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // that we can fold to a non-zero positive value as an extension.
13097e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1310cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
1311cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1312cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
1313923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
1314b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
1315b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1316b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1317b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
1318b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1319b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
1320923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1321923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1322923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
132302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
132402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
132502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      Diag(ArraySize->getLocStart(),
132602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
132702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
1328923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
132920cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne
133020cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      if (ASM == ArrayType::Static) {
133120cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        Diag(ArraySize->getLocStart(),
133220cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne             diag::warn_typecheck_zero_static_array_size)
133320cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne          << ArraySize->getSourceRange();
133420cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        ASM = ArrayType::Normal;
133520cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      }
13362767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
13372767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor               !T->isIncompleteType()) {
13382767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      // Is the array too large?
13392767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
13402767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
13412767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
13422767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
13432767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
13442767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
13451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
13462767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
134746a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
1348cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1349af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
1350af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  if (!getLangOptions().C99) {
13510fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
13520fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
1353f85e193739c953358c865005855253af4f68a497John McCall      QualType BaseT = Context.getBaseElementType(T);
1354204ce17e0cfd9bbe229627e1e5a20c3f2f587c8cDouglas Gregor      if (!T->isDependentType() &&
1355f85e193739c953358c865005855253af4f68a497John McCall          !BaseT.isPODType(Context) &&
1356f85e193739c953358c865005855253af4f68a497John McCall          !BaseT->isObjCLifetimeType()) {
13570fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
1358f85e193739c953358c865005855253af4f68a497John McCall          << BaseT;
13590fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
13600fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      }
1361a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
1362a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
1363a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
1364a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
1365a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
13660fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
13670fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
13680fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::ext_vla);
13690fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
1370d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith      Diag(Loc,
1371043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor           getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
1372d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith                                     : diag::ext_c99_array_usage) << ASM;
1373cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1374cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1375cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
1376cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
13779cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
13789cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
13799cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
13809cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
13819ae2f076ca5ab1feb3ba95629099ec2319833701John McCallQualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
13829cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
13839cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
13849cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
13851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
13869cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
13879cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
13889cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
13899cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
13909cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
13919ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
13929cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
13939ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
13949cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
13959ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        << "ext_vector_type" << ArraySize->getSourceRange();
13969cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
13979cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
13981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
14009cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
14011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
14021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14039cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
14049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
14059ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      << ArraySize->getSourceRange();
14069cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
14079cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
14081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14094ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    return Context.getExtVectorType(T, vectorSize);
14101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
14111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14129ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
14139cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
14141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1415724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
1416724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1417724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
1418724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
1419724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
14202943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
1421724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
1422724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1423724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
1424724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1425724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
1426724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
1427724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
1428724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1429724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
1430724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1431724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
1432724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1433724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
1434724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1435724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
1436724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
1437724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
1438724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1439724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
1440724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
1441724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1442724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
1443724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
1444724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
14451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 QualType *ParamTypes,
1446724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
1447724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 bool Variadic, unsigned Quals,
1448c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                                 RefQualifierKind RefQualifier,
1449fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
1450e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                 FunctionType::ExtInfo Info) {
1451724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
145258408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor    Diag(Loc, diag::err_func_returning_array_function)
145358408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
1454724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1455724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1456aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1457aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // Functions cannot return half FP.
1458aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  if (T->isHalfType()) {
1459aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 <<
1460aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      FixItHint::CreateInsertion(Loc, "*");
1461aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    return QualType();
1462aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  }
1463aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1464724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
1465724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
1466aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    // FIXME: Loc is too inprecise here, should use proper locations for args.
146779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]);
14682dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
1469724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
1470724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
1471aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    } else if (ParamType->isHalfType()) {
1472aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Disallow half FP arguments.
1473aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 <<
1474aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        FixItHint::CreateInsertion(Loc, "*");
1475aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Invalid = true;
1476724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
1477cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
147854e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
1479724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1480724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1481724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
1482724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1483724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1484e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
1485e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.Variadic = Variadic;
1486e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.TypeQuals = Quals;
1487c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor  EPI.RefQualifier = RefQualifier;
1488e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.ExtInfo = Info;
1489e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1490e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI);
1491724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
14921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1493949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
1494949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1495949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
1496949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
14970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR Qualifiers applied to the member pointer type
1498949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
1499949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
1500949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1501949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
1502949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
15031eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
15042865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
1505949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
1506949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
1507949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
1508949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
1509949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
1510949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1511949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
1512949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
1513949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1514949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
1515949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
1516949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
1517949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1518949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1519737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
1520949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
1521949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
15228d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
1523ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
1524949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1525949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1526949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1527949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
1528949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1529949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
1530949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1531949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1532949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1533949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
1534949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1535949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1536949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1537949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1538d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // In the Microsoft ABI, the class is allowed to be an incomplete
1539d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // type. In such cases, the compiler makes a worst-case assumption.
1540d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // We make no such assumption right now, so emit an error if the
1541d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // class isn't a complete type.
1542bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  if (Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft &&
1543d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis      RequireCompleteType(Loc, Class, diag::err_incomplete_type))
1544d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis    return QualType();
1545d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis
15462865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
1547949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
15481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15499a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
15509a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
15519a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
15529a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
15530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
15549a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
15559a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Loc The location of the entity whose type involves this
15569a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// block pointer type or, if there is no such entity, the location of the
15579a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type that will have block pointer type.
15589a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
15599a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
15609a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
15619a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
15629a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
15639a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
15642865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildBlockPointerType(QualType T,
15651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
15669a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
15670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
15689a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
15699a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
15709a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
15711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15722865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
15739a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
15749a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
1575b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1576b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
15773f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
1578a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
15793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
15803f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
15813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1582a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
1583f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
1584e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
1585a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
1586e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
15871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1588a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
1589e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
1590e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
1591e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
1592a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
1593a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
1594a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            unsigned chunkIndex);
1595a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis
1596f85e193739c953358c865005855253af4f68a497John McCall/// Given that this is the declaration of a parameter under ARC,
1597f85e193739c953358c865005855253af4f68a497John McCall/// attempt to infer attributes and such for pointer-to-whatever
1598f85e193739c953358c865005855253af4f68a497John McCall/// types.
1599f85e193739c953358c865005855253af4f68a497John McCallstatic void inferARCWriteback(TypeProcessingState &state,
1600f85e193739c953358c865005855253af4f68a497John McCall                              QualType &declSpecType) {
1601f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
1602f85e193739c953358c865005855253af4f68a497John McCall  Declarator &declarator = state.getDeclarator();
1603f85e193739c953358c865005855253af4f68a497John McCall
1604f85e193739c953358c865005855253af4f68a497John McCall  // TODO: should we care about decl qualifiers?
1605f85e193739c953358c865005855253af4f68a497John McCall
1606f85e193739c953358c865005855253af4f68a497John McCall  // Check whether the declarator has the expected form.  We walk
1607f85e193739c953358c865005855253af4f68a497John McCall  // from the inside out in order to make the block logic work.
1608f85e193739c953358c865005855253af4f68a497John McCall  unsigned outermostPointerIndex = 0;
1609f85e193739c953358c865005855253af4f68a497John McCall  bool isBlockPointer = false;
1610f85e193739c953358c865005855253af4f68a497John McCall  unsigned numPointers = 0;
1611f85e193739c953358c865005855253af4f68a497John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
1612f85e193739c953358c865005855253af4f68a497John McCall    unsigned chunkIndex = i;
1613f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex);
1614f85e193739c953358c865005855253af4f68a497John McCall    switch (chunk.Kind) {
1615f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Paren:
1616f85e193739c953358c865005855253af4f68a497John McCall      // Ignore parens.
1617f85e193739c953358c865005855253af4f68a497John McCall      break;
1618f85e193739c953358c865005855253af4f68a497John McCall
1619f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Reference:
1620f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Pointer:
1621f85e193739c953358c865005855253af4f68a497John McCall      // Count the number of pointers.  Treat references
1622f85e193739c953358c865005855253af4f68a497John McCall      // interchangeably as pointers; if they're mis-ordered, normal
1623f85e193739c953358c865005855253af4f68a497John McCall      // type building will discover that.
1624f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1625f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1626f85e193739c953358c865005855253af4f68a497John McCall      break;
1627f85e193739c953358c865005855253af4f68a497John McCall
1628f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::BlockPointer:
1629f85e193739c953358c865005855253af4f68a497John McCall      // If we have a pointer to block pointer, that's an acceptable
1630f85e193739c953358c865005855253af4f68a497John McCall      // indirect reference; anything else is not an application of
1631f85e193739c953358c865005855253af4f68a497John McCall      // the rules.
1632f85e193739c953358c865005855253af4f68a497John McCall      if (numPointers != 1) return;
1633f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1634f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1635f85e193739c953358c865005855253af4f68a497John McCall      isBlockPointer = true;
1636f85e193739c953358c865005855253af4f68a497John McCall
1637f85e193739c953358c865005855253af4f68a497John McCall      // We don't care about pointer structure in return values here.
1638f85e193739c953358c865005855253af4f68a497John McCall      goto done;
1639f85e193739c953358c865005855253af4f68a497John McCall
1640f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Array: // suppress if written (id[])?
1641f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Function:
1642f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::MemberPointer:
1643f85e193739c953358c865005855253af4f68a497John McCall      return;
1644f85e193739c953358c865005855253af4f68a497John McCall    }
1645f85e193739c953358c865005855253af4f68a497John McCall  }
1646f85e193739c953358c865005855253af4f68a497John McCall done:
1647f85e193739c953358c865005855253af4f68a497John McCall
1648f85e193739c953358c865005855253af4f68a497John McCall  // If we have *one* pointer, then we want to throw the qualifier on
1649f85e193739c953358c865005855253af4f68a497John McCall  // the declaration-specifiers, which means that it needs to be a
1650f85e193739c953358c865005855253af4f68a497John McCall  // retainable object type.
1651f85e193739c953358c865005855253af4f68a497John McCall  if (numPointers == 1) {
1652f85e193739c953358c865005855253af4f68a497John McCall    // If it's not a retainable object type, the rule doesn't apply.
1653f85e193739c953358c865005855253af4f68a497John McCall    if (!declSpecType->isObjCRetainableType()) return;
1654f85e193739c953358c865005855253af4f68a497John McCall
1655f85e193739c953358c865005855253af4f68a497John McCall    // If it already has lifetime, don't do anything.
1656f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType.getObjCLifetime()) return;
1657f85e193739c953358c865005855253af4f68a497John McCall
1658f85e193739c953358c865005855253af4f68a497John McCall    // Otherwise, modify the type in-place.
1659f85e193739c953358c865005855253af4f68a497John McCall    Qualifiers qs;
1660f85e193739c953358c865005855253af4f68a497John McCall
1661f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType->isObjCARCImplicitlyUnretainedType())
1662f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
1663f85e193739c953358c865005855253af4f68a497John McCall    else
1664f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing);
1665f85e193739c953358c865005855253af4f68a497John McCall    declSpecType = S.Context.getQualifiedType(declSpecType, qs);
1666f85e193739c953358c865005855253af4f68a497John McCall
1667f85e193739c953358c865005855253af4f68a497John McCall  // If we have *two* pointers, then we want to throw the qualifier on
1668f85e193739c953358c865005855253af4f68a497John McCall  // the outermost pointer.
1669f85e193739c953358c865005855253af4f68a497John McCall  } else if (numPointers == 2) {
1670f85e193739c953358c865005855253af4f68a497John McCall    // If we don't have a block pointer, we need to check whether the
1671f85e193739c953358c865005855253af4f68a497John McCall    // declaration-specifiers gave us something that will turn into a
1672f85e193739c953358c865005855253af4f68a497John McCall    // retainable object pointer after we slap the first pointer on it.
1673f85e193739c953358c865005855253af4f68a497John McCall    if (!isBlockPointer && !declSpecType->isObjCObjectType())
1674f85e193739c953358c865005855253af4f68a497John McCall      return;
1675f85e193739c953358c865005855253af4f68a497John McCall
1676f85e193739c953358c865005855253af4f68a497John McCall    // Look for an explicit lifetime attribute there.
1677f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex);
16781c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis    if (chunk.Kind != DeclaratorChunk::Pointer &&
16791c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis        chunk.Kind != DeclaratorChunk::BlockPointer)
16801c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis      return;
1681f85e193739c953358c865005855253af4f68a497John McCall    for (const AttributeList *attr = chunk.getAttrs(); attr;
1682f85e193739c953358c865005855253af4f68a497John McCall           attr = attr->getNext())
1683b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      if (attr->getKind() == AttributeList::AT_objc_ownership)
1684f85e193739c953358c865005855253af4f68a497John McCall        return;
1685f85e193739c953358c865005855253af4f68a497John McCall
1686a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis    transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing,
1687a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                          outermostPointerIndex);
1688f85e193739c953358c865005855253af4f68a497John McCall
1689f85e193739c953358c865005855253af4f68a497John McCall  // Any other number of pointers/references does not trigger the rule.
1690f85e193739c953358c865005855253af4f68a497John McCall  } else return;
1691f85e193739c953358c865005855253af4f68a497John McCall
1692f85e193739c953358c865005855253af4f68a497John McCall  // TODO: mark whether we did this inference?
1693f85e193739c953358c865005855253af4f68a497John McCall}
1694f85e193739c953358c865005855253af4f68a497John McCall
1695d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruthstatic void DiagnoseIgnoredQualifiers(unsigned Quals,
1696d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation ConstQualLoc,
1697d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation VolatileQualLoc,
1698d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation RestrictQualLoc,
1699d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      Sema& S) {
1700d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  std::string QualStr;
1701d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  unsigned NumQuals = 0;
1702d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  SourceLocation Loc;
1703d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1704d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint ConstFixIt;
1705d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint VolatileFixIt;
1706d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint RestrictFixIt;
1707d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1708a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg  const SourceManager &SM = S.getSourceManager();
1709a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg
1710d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to
1711d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // find a range and grow it to encompass all the qualifiers, regardless of
1712d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // the order in which they textually appear.
1713d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Const) {
1714d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc);
1715d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    QualStr = "const";
1716a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    ++NumQuals;
1717a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(ConstQualLoc, Loc))
1718a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = ConstQualLoc;
1719d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1720d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Volatile) {
1721d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc);
1722a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    QualStr += (NumQuals == 0 ? "volatile" : " volatile");
1723d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ++NumQuals;
1724a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(VolatileQualLoc, Loc))
1725a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = VolatileQualLoc;
1726d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1727d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Restrict) {
1728d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc);
1729a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    QualStr += (NumQuals == 0 ? "restrict" : " restrict");
1730d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ++NumQuals;
1731a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(RestrictQualLoc, Loc))
1732a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = RestrictQualLoc;
1733d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1734d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1735d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  assert(NumQuals > 0 && "No known qualifiers?");
1736d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1737d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  S.Diag(Loc, diag::warn_qual_return_type)
1738a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    << QualStr << NumQuals << ConstFixIt << VolatileFixIt << RestrictFixIt;
1739d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth}
1740d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
17418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
17428cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                             TypeSourceInfo *&ReturnTypeInfo) {
17438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &SemaRef = state.getSema();
17448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
1745930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
17468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ReturnTypeInfo = 0;
1747711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
17488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // The TagDecl owned by the DeclSpec.
17498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TagDecl *OwnedTagDecl = 0;
17508999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl
17513f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
175298a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian  case UnqualifiedId::IK_ImplicitSelfParam:
17533f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
17548999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  case UnqualifiedId::IK_Identifier:
17550486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
17563f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
17578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = ConvertDeclSpecToType(state);
17585db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
1759591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
1760d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
176115987970eeaa1842c29ec8797affd1c1dea05585Abramo Bagnara      // Owned declaration is embedded in declarator.
1762d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl->setEmbeddedInDeclarator(true);
1763591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
1764930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
1765930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
17663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
17670efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
17683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
1769930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
177048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // "void" instead.
17718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = SemaRef.Context.VoidTy;
1772930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
177348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
177448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
177548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
177648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
17778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId,
17788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  &ReturnTypeInfo);
177948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
1780930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
1781dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
1782711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
1783711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
1784711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1785e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith  // C++0x [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
17868110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // In C++0x, a function declarator using 'auto' must have a trailing return
17878110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // type (this is checked later) and we can skip this. In other languages
17888110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // using auto, we need to check regardless.
178934b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
17908cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      (!SemaRef.getLangOptions().CPlusPlus0x || !D.isFunctionDeclarator())) {
1791baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
17921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1793baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
1794baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
1795b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("K&R type lists aren't allowed in C++");
1796f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
1797f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman      llvm_unreachable("Can't specify a type specifier in lambda grammar");
1798cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
1799cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
1800baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
1801baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
1802baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1803baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
18047a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)
18057a614d8380297fcd2bc23986241905d97222948cRichard Smith        break;
18068cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) {
1807eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie      case TTK_Enum: llvm_unreachable("unhandled tag kind");
1808465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
1809465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
1810465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
18111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
1812baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1813baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
181417b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
1815baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 4; // Exception declaration
1816baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1817baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
1818baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 5; // Template parameter
1819baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1820baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
182134b41d939a1328f484511c6002ba2456db879a29Richard Smith      Error = 6; // Block literal
182234b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
182334b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TemplateTypeArgContext:
182434b41d939a1328f484511c6002ba2456db879a29Richard Smith      Error = 7; // Template type argument
182534b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
1826162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
18273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
1828162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      Error = 9; // Type alias
1829162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      break;
183034b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TypeNameContext:
18310b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis      Error = 11; // Generic
1832baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1833baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
1834baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
1835baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
1836baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
18370b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
1838baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1839baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1840baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
1841ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith    if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1842ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith      Error = 8;
1843ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith
18448110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    // In Objective-C it is an error to use 'auto' on a function declarator.
18458110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    if (D.isFunctionDeclarator())
1846162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      Error = 10;
18478110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith
1848e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // C++0x [dcl.spec.auto]p2: 'auto' is always fine if the declarator
1849e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // contains a trailing return type. That is only legal at the outermost
1850e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // level. Check all declarator chunks (outermost first) anyway, to give
1851e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // better diagnostics.
18528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    if (SemaRef.getLangOptions().CPlusPlus0x && Error != -1) {
1853e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1854e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned chunkIndex = e - i - 1;
1855e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        state.setCurrentChunkIndex(chunkIndex);
1856e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
1857e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (DeclType.Kind == DeclaratorChunk::Function) {
1858e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1859e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (FTI.TrailingReturnType) {
1860e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Error = -1;
1861e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            break;
1862e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          }
1863e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        }
1864e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
1865e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    }
1866e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
1867baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
18688cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
18698cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_auto_not_allowed)
1870baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
18718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = SemaRef.Context.IntTy;
1872baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
18730aa86c0463a881be85fd34e04c7de3379997621dRichard Smith    } else
18740aa86c0463a881be85fd34e04c7de3379997621dRichard Smith      SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
18750aa86c0463a881be85fd34e04c7de3379997621dRichard Smith                   diag::warn_cxx98_compat_auto_type_specifier);
1876baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
18778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
18788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (SemaRef.getLangOptions().CPlusPlus &&
18795e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall      OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) {
18808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // Check the contexts where C++ forbids the declaration of a new class
18818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // or enumeration in a type-specifier-seq.
18828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    switch (D.getContext()) {
18838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::FileContext:
18848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::MemberContext:
18858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockContext:
18868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ForContext:
18878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockLiteralContext:
1888f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
18898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++0x [dcl.type]p3:
18908cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   A type-specifier-seq shall not define a class or enumeration unless
18918cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   it appears in the type-id of an alias-declaration (7.1.3) that is not
18928cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   the declaration of a template-declaration.
18938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasDeclContext:
18948cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
18958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasTemplateContext:
18968cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
18978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_alias_template)
18988cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
18998cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
19008cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TypeNameContext:
19018cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateParamContext:
19028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXNewContext:
19038cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXCatchContext:
19048cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ObjCCatchContext:
19058cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateTypeArgContext:
19068cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
19078cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_type_specifier)
19088cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
19098cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
19108cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::PrototypeContext:
1911cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
1912cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
19138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::KNRTypeListContext:
19148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ [dcl.fct]p6:
19158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   Types shall not be defined in return or parameter types.
19168cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
19178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_param_type)
19188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
19198cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
19208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ConditionContext:
19218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ 6.4p2:
19228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // The type-specifier-seq shall not contain typedef and shall not declare
19238cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // a new class or enumeration.
19248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
19258cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_condition);
19268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
19278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    }
19288cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  }
19298cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
19308cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return T;
19318cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
19328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
19338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
19348cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                QualType declSpecType,
19358cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                TypeSourceInfo *TInfo) {
19368cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
19378cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = declSpecType;
19388cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
19398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
19408cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ASTContext &Context = S.Context;
19418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  const LangOptions &LangOpts = S.getLangOptions();
19428cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
19438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  bool ImplicitlyNoexcept = false;
19448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (D.getName().getKind() == UnqualifiedId::IK_OperatorFunctionId &&
19458cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      LangOpts.CPlusPlus0x) {
19468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    OverloadedOperatorKind OO = D.getName().OperatorFunctionId.Operator;
19478cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    /// In C++0x, deallocation functions (normal and array operator delete)
19488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    /// are implicitly noexcept.
19498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    if (OO == OO_Delete || OO == OO_Array_Delete)
19508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      ImplicitlyNoexcept = true;
19518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  }
195234b41d939a1328f484511c6002ba2456db879a29Richard Smith
1953cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
1954cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
1955cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
1956cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
19571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1958162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  // Does this declaration declare a typedef-name?
1959162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  bool IsTypedefName =
1960162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef ||
19613e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasDeclContext ||
19623e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasTemplateContext;
1963162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
196498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
196598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
196698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
19678ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1968711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
1969711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
1970711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
19715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
1972075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
19738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildParenType(T);
1974075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
19755618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
19769af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
19779af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
19788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_blocks_disable);
19791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
19812865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
19828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
19835618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
19845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
19856a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
19866a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
19878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
19888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
19896a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
19906a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
19916a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
19928cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.ObjC1 && T->getAs<ObjCObjectType>()) {
1993c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
19942865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
19958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
199614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
199714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
19988cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildPointerType(T, DeclType.Loc, Name);
19992865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
20008cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
2001711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
20025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
20030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
20046a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
20056a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
20068cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
20078cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
20086a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
20096a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
20106a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
20118cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
20122865474261a608c7873b87ba4af110d17907896dJohn McCall
20132865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
20142865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
20158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
20165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
20170953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
20185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
20196a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
20206a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
20218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
20228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
20236a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
20246a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
20256a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
2026fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
202794f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
20285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
20295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
20305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
20315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
20325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
20335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
20345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
2035c05a94b7accd4035bf5d5897c434c445b22da855John McCall      if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
2036f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
2037f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
2038f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
20398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
2040f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
2041f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
2042f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
20438ac2c66a1442985091c5ec2b33ce6d3df3bcb529Eli Friedman      T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
20448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                           SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
20455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
20465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2047f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
20485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
20495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
20505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
20515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
20523cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
205334b41d939a1328f484511c6002ba2456db879a29Richard Smith      // Check for auto functions and trailing return type and adjust the
205434b41d939a1328f484511c6002ba2456db879a29Richard Smith      // return type accordingly.
205534b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!D.isInvalidType()) {
205634b41d939a1328f484511c6002ba2456db879a29Richard Smith        // trailing-return-type is only required if we're declaring a function,
205734b41d939a1328f484511c6002ba2456db879a29Richard Smith        // and not, for instance, a pointer to a function.
205834b41d939a1328f484511c6002ba2456db879a29Richard Smith        if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
205934b41d939a1328f484511c6002ba2456db879a29Richard Smith            !FTI.TrailingReturnType && chunkIndex == 0) {
20608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
206134b41d939a1328f484511c6002ba2456db879a29Richard Smith               diag::err_auto_missing_trailing_return);
206234b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = Context.IntTy;
206334b41d939a1328f484511c6002ba2456db879a29Richard Smith          D.setInvalidType(true);
206434b41d939a1328f484511c6002ba2456db879a29Richard Smith        } else if (FTI.TrailingReturnType) {
2065e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          // T must be exactly 'auto' at this point. See CWG issue 681.
2066e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (isa<ParenType>(T)) {
20678cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
2068e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith                 diag::err_trailing_return_in_parens)
2069e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith              << T << D.getDeclSpec().getSourceRange();
2070e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.setInvalidType(true);
2071f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman          } else if (D.getContext() != Declarator::LambdaExprContext &&
2072f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman                     (T.hasQualifiers() || !isa<AutoType>(T))) {
20738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
207434b41d939a1328f484511c6002ba2456db879a29Richard Smith                 diag::err_trailing_return_without_auto)
207534b41d939a1328f484511c6002ba2456db879a29Richard Smith              << T << D.getDeclSpec().getSourceRange();
207634b41d939a1328f484511c6002ba2456db879a29Richard Smith            D.setInvalidType(true);
207734b41d939a1328f484511c6002ba2456db879a29Richard Smith          }
207834b41d939a1328f484511c6002ba2456db879a29Richard Smith
20798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          T = S.GetTypeFromParser(
208034b41d939a1328f484511c6002ba2456db879a29Richard Smith            ParsedType::getFromOpaquePtr(FTI.TrailingReturnType),
20818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            &TInfo);
208234b41d939a1328f484511c6002ba2456db879a29Richard Smith        }
208334b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
208434b41d939a1328f484511c6002ba2456db879a29Richard Smith
2085e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // C99 6.7.5.3p1: The return type may not be a function or array type.
2086e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // For conversion functions, we'll diagnose this particular error later.
2087e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      if ((T->isArrayType() || T->isFunctionType()) &&
2088e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
2089e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned diagID = diag::err_func_returning_array_function;
2090e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // Last processing chunk in block context means this function chunk
2091e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // represents the block.
2092e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (chunkIndex == 0 &&
2093e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.getContext() == Declarator::BlockLiteralContext)
2094e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          diagID = diag::err_block_returning_array_function;
20958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
2096e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        T = Context.IntTy;
2097e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        D.setInvalidType(true);
2098e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2099e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2100aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Do not allow returning half FP value.
2101aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // FIXME: This really should be in BuildFunctionType.
2102aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      if (T->isHalfType()) {
2103aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        S.Diag(D.getIdentifierLoc(),
2104aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov             diag::err_parameters_retval_cannot_have_fp16_type) << 1
2105aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov          << FixItHint::CreateInsertion(D.getIdentifierLoc(), "*");
2106aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        D.setInvalidType(true);
2107aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      }
2108aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
21095291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
21105291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
2111fff951371dfc309160a99d423e43a7841aeb35aaDouglas Gregor      if (isa<PointerType>(T) && T.getLocalCVRQualifiers() &&
21121e15394853bfae25112d9cc6b445504905e1f34aRafael Espindola          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId) &&
21138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          (!LangOpts.CPlusPlus || !T->isDependentType())) {
2114d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?");
2115d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
2116d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer);
2117d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2118d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr;
2119d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2120d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DiagnoseIgnoredQualifiers(PTI.TypeQuals,
2121d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
2122d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
2123d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
21248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S);
2125d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2126d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth      } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
21278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          (!LangOpts.CPlusPlus ||
21285291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor           (!T->isDependentType() && !T->isRecordType()))) {
2129d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2130d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(),
2131d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getConstSpecLoc(),
2132d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getVolatileSpecLoc(),
2133d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getRestrictSpecLoc(),
21348cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  S);
21355291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      }
2136d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
21378cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
2138402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
2139402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
2140b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
21415e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall        if (Tag->isCompleteDefinition())
21428cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
2143402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
2144402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
2145402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
21463cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
21473cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
2148162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (IsTypedefName && FTI.getExceptionSpecType())
21498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef)
21503e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          << (D.getContext() == Declarator::AliasDeclContext ||
21513e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith              D.getContext() == Declarator::AliasTemplateContext);
21523cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
21538cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (!FTI.NumArgs && !FTI.isVariadic && !LangOpts.CPlusPlus) {
21542865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
21552865474261a608c7873b87ba4af110d17907896dJohn McCall        T = Context.getFunctionNoProtoType(T);
21562865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
21572865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
21582865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
21592865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
21608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        if (!FTI.NumArgs && FTI.isVariadic && !LangOpts.CPlusPlus) {
2161965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
2162965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
2163965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
2164965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
2165965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
2166965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
2167965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
2168965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
2169965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
2170965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
21718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
2172c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
21732865474261a608c7873b87ba4af110d17907896dJohn McCall
21742865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
2175788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
2176788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
21778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
21782865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
21792865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
21802865474261a608c7873b87ba4af110d17907896dJohn McCall        }
21812865474261a608c7873b87ba4af110d17907896dJohn McCall
2182e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
2183e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
2184e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
2185c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
2186c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : FTI.RefQualifierIsLValueRef? RQ_LValue
2187c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : RQ_RValue;
2188c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
21895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
21905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
21915f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 16> ArgTys;
21922865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
21931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21945f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<bool, 16> ConsumedArguments;
2195f85e193739c953358c865005855253af4f68a497John McCall        ConsumedArguments.reserve(FTI.NumArgs);
2196f85e193739c953358c865005855253af4f68a497John McCall        bool HasAnyConsumedArguments = false;
2197f85e193739c953358c865005855253af4f68a497John McCall
21985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
2199d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
22008123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
220178c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
22022dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
22032dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
220479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor          assert((ArgTy == Context.getAdjustedParameterType(ArgTy)) &&
220579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor                 "Unadjusted type?");
22062dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
22075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
22085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
220972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
22102dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
22115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
22125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
22135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
22145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
22158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(DeclType.Loc, diag::err_void_only_param);
22162ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
22178123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
22182ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
22192ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
22208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(FTI.ArgInfo[i].IdentLoc,
22214565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
22222ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
22238123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
22242ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
22252ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
22260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
22278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                S.Diag(DeclType.Loc, diag::err_void_param_qualified);
22281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22292ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
22302ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
22312ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
2232aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov          } else if (ArgTy->isHalfType()) {
2233aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // Disallow half FP arguments.
2234aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // FIXME: This really should be in BuildFunctionType.
2235aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            S.Diag(Param->getLocation(),
2236aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov               diag::err_parameters_retval_cannot_have_fp16_type) << 0
2237aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            << FixItHint::CreateInsertion(Param->getLocation(), "*");
2238aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            D.setInvalidType();
2239eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
2240eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
2241a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
2242eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              Param->setKNRPromoted(true);
2243183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
2244eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              if (BTy->getKind() == BuiltinType::Float) {
2245eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
2246eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall                Param->setKNRPromoted(true);
2247eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              }
2248eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
22495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
225056a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
22518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          if (LangOpts.ObjCAutoRefCount) {
2252f85e193739c953358c865005855253af4f68a497John McCall            bool Consumed = Param->hasAttr<NSConsumedAttr>();
2253f85e193739c953358c865005855253af4f68a497John McCall            ConsumedArguments.push_back(Consumed);
2254f85e193739c953358c865005855253af4f68a497John McCall            HasAnyConsumedArguments |= Consumed;
2255f85e193739c953358c865005855253af4f68a497John McCall          }
2256f85e193739c953358c865005855253af4f68a497John McCall
225754e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
22585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2259465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2260f85e193739c953358c865005855253af4f68a497John McCall        if (HasAnyConsumedArguments)
2261f85e193739c953358c865005855253af4f68a497John McCall          EPI.ConsumedArguments = ConsumedArguments.data();
2262f85e193739c953358c865005855253af4f68a497John McCall
22635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 4> Exceptions;
22648b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        EPI.ExceptionSpecType = FTI.getExceptionSpecType();
22658b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        if (FTI.getExceptionSpecType() == EST_Dynamic) {
2266e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          Exceptions.reserve(FTI.NumExceptions);
2267e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
2268e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // FIXME: Preserve type source info.
22698cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            QualType ET = S.GetTypeFromParser(FTI.Exceptions[ei].Ty);
2270e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // Check that the type is valid for an exception spec, and
2271e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // drop it if not.
22728cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            if (!S.CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
2273e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall              Exceptions.push_back(ET);
2274e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
2275373920bd733b1d28fe7bf209945a62eb9248d948John McCall          EPI.NumExceptions = Exceptions.size();
2276e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          EPI.Exceptions = Exceptions.data();
22778b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
227860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl          // If an error occurred, there's no expression here.
227960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl          if (Expr *NoexceptExpr = FTI.NoexceptExpr) {
228060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl            assert((NoexceptExpr->isTypeDependent() ||
228160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                    NoexceptExpr->getType()->getCanonicalTypeUnqualified() ==
228260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                        Context.BoolTy) &&
228360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                 "Parser should have made sure that the expression is boolean");
228460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl            SourceLocation ErrLoc;
228560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl            llvm::APSInt Dummy;
228660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl            if (!NoexceptExpr->isValueDependent() &&
228760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                !NoexceptExpr->isIntegerConstantExpr(Dummy, Context, &ErrLoc,
228860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                                     /*evaluated*/false))
22898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(ErrLoc, diag::err_noexcept_needs_constant_expression)
229060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                  << NoexceptExpr->getSourceRange();
229160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl            else
229260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl              EPI.NoexceptExpr = NoexceptExpr;
229360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl          }
22948999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl        } else if (FTI.getExceptionSpecType() == EST_None &&
22958999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl                   ImplicitlyNoexcept && chunkIndex == 0) {
22968999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl          // Only the outermost chunk is marked noexcept, of course.
22978999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl          EPI.ExceptionSpecType = EST_BasicNoexcept;
2298ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl        }
2299465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2300e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI);
23015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
230204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
23035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
23045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2305f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
2306f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
23077bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
2308f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
23097bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
2310edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
2311edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
23128cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      } else if (S.isDependentScopeSpecifier(SS) ||
23138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                 dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) {
23141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
23157bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
231687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
231787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
231887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
23197bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
23204a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
232187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
232287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
232387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
232414aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor        case NestedNameSpecifier::NamespaceAlias:
232587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
23269f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
23277bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
232887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
232987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
233087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
233191ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Note: if the NNS has a prefix and ClsType is a nondependent
233291ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // TemplateSpecializationType, then the NNS prefix is NOT included
233391ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // in ClsType; hence we wrap ClsType into an ElaboratedType.
233491ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // NOTE: in particular, no wrap occurs if ClsType already is an
233591ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Elaborated, DependentName, or DependentTemplateSpecialization.
233691ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
23377bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
233887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
233987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
2340f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
23418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Mem.Scope().getBeginLoc(),
2342949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
2343949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
2344949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
2345f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
2346f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2347f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2348949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
23498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
2350949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
2351f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
2352949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
23532865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
23548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
2355f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2356f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
2357f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
2358f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2359cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
2360cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
2361cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
2362cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
2363cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
2364c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
2365711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
2366711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      processTypeAttrs(state, T, false, attrs);
23675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2368971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
23698cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (LangOpts.CPlusPlus && T->isFunctionType()) {
2370183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
2371778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
2372971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
2373708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    // C++ 8.3.5p4:
2374708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
2375708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
2376708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
2377708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
2378683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    //
2379683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // Core issue 547 also allows cv-qualifiers on function types that are
2380683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // top-level template type arguments.
2381613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
2382613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
2383906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman      FreeFunction = ((D.getContext() != Declarator::MemberContext &&
2384906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman                       D.getContext() != Declarator::LambdaExprContext) ||
2385613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall                      D.getDeclSpec().isFriendSpecified());
2386613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
23878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec());
2388613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
2389613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
2390613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
239155dec868977ccb89cab0286122f9345f63bb5de7Richard Smith    // C++0x [dcl.constexpr]p8: A constexpr specifier for a non-static member
239255dec868977ccb89cab0286122f9345f63bb5de7Richard Smith    // function that is not a constructor declares that function to be const.
239355dec868977ccb89cab0286122f9345f63bb5de7Richard Smith    if (D.getDeclSpec().isConstexprSpecified() && !FreeFunction &&
23941bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static &&
239555dec868977ccb89cab0286122f9345f63bb5de7Richard Smith        D.getName().getKind() != UnqualifiedId::IK_ConstructorName &&
239655dec868977ccb89cab0286122f9345f63bb5de7Richard Smith        D.getName().getKind() != UnqualifiedId::IK_ConstructorTemplateId &&
239755dec868977ccb89cab0286122f9345f63bb5de7Richard Smith        !(FnTy->getTypeQuals() & DeclSpec::TQ_const)) {
239855dec868977ccb89cab0286122f9345f63bb5de7Richard Smith      // Rebuild function type adding a 'const' qualifier.
239955dec868977ccb89cab0286122f9345f63bb5de7Richard Smith      FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
240055dec868977ccb89cab0286122f9345f63bb5de7Richard Smith      EPI.TypeQuals |= DeclSpec::TQ_const;
240155dec868977ccb89cab0286122f9345f63bb5de7Richard Smith      T = Context.getFunctionType(FnTy->getResultType(),
240255dec868977ccb89cab0286122f9345f63bb5de7Richard Smith                                  FnTy->arg_type_begin(),
240355dec868977ccb89cab0286122f9345f63bb5de7Richard Smith                                  FnTy->getNumArgs(), EPI);
240455dec868977ccb89cab0286122f9345f63bb5de7Richard Smith    }
240555dec868977ccb89cab0286122f9345f63bb5de7Richard Smith
2406c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    // C++0x [dcl.fct]p6:
2407c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   A ref-qualifier shall only be part of the function type for a
2408c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   non-static member function, the function type to which a pointer to
2409c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   member refers, or the top-level function type of a function typedef
2410c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   declaration.
2411c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    if ((FnTy->getTypeQuals() != 0 || FnTy->getRefQualifier()) &&
2412683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        !(D.getContext() == Declarator::TemplateTypeArgContext &&
2413162e1c1b487352434552147967c3dd296ebee2f7Richard Smith          !D.isFunctionDeclarator()) && !IsTypedefName &&
2414c61bb2056148891375bfa591fa2859b9b6ec2734Sebastian Redl        (FreeFunction ||
2415971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
2416683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      if (D.getContext() == Declarator::TemplateTypeArgContext) {
2417683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        // Accept qualified function types as template type arguments as a GNU
2418683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        // extension. This is also the subject of C++ core issue 547.
2419683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        std::string Quals;
2420683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        if (FnTy->getTypeQuals() != 0)
2421683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          Quals = Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
2422683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
2423683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        switch (FnTy->getRefQualifier()) {
2424683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        case RQ_None:
2425683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          break;
2426683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
2427683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        case RQ_LValue:
2428683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (!Quals.empty())
2429683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Quals += ' ';
2430683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          Quals += '&';
2431683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          break;
2432c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
2433683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        case RQ_RValue:
2434683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (!Quals.empty())
2435683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Quals += ' ';
2436683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          Quals += "&&";
2437683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          break;
2438683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
2439683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
24408cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(),
2441683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor             diag::ext_qualified_function_type_template_arg)
2442683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          << Quals;
2443683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      } else {
2444683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        if (FnTy->getTypeQuals() != 0) {
244543f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor          if (D.isFunctionDeclarator()) {
244643f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor            SourceRange Range = D.getIdentifierLoc();
244743f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor            for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
244843f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor              const DeclaratorChunk &Chunk = D.getTypeObject(N-I-1);
244943f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor              if (Chunk.Kind == DeclaratorChunk::Function &&
245043f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                  Chunk.Fun.TypeQuals != 0) {
245143f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                switch (Chunk.Fun.TypeQuals) {
245243f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                case Qualifiers::Const:
245343f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                  Range = Chunk.Fun.getConstQualifierLoc();
245443f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                  break;
245543f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                case Qualifiers::Volatile:
245643f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                  Range = Chunk.Fun.getVolatileQualifierLoc();
245743f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                  break;
245843f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                case Qualifiers::Const | Qualifiers::Volatile: {
245943f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                    SourceLocation CLoc = Chunk.Fun.getConstQualifierLoc();
246043f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                    SourceLocation VLoc = Chunk.Fun.getVolatileQualifierLoc();
246143f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                    if (S.getSourceManager()
246243f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                        .isBeforeInTranslationUnit(CLoc, VLoc)) {
246343f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                      Range = SourceRange(CLoc, VLoc);
246443f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                    } else {
246543f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                      Range = SourceRange(VLoc, CLoc);
246643f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                    }
246743f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                  }
246843f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                  break;
246943f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                }
247043f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                break;
247143f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor              }
247243f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor            }
247343f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor            S.Diag(Range.getBegin(), diag::err_invalid_qualified_function_type)
247443f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                << FixItHint::CreateRemoval(Range);
247543f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor          } else
24768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getIdentifierLoc(),
2477683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                 diag::err_invalid_qualified_typedef_function_type_use)
2478683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << FreeFunction;
2479683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
2480683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
2481683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        if (FnTy->getRefQualifier()) {
2482683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (D.isFunctionDeclarator()) {
2483683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            SourceLocation Loc = D.getIdentifierLoc();
2484683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
2485683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              const DeclaratorChunk &Chunk = D.getTypeObject(N-I-1);
2486683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              if (Chunk.Kind == DeclaratorChunk::Function &&
2487683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                  Chunk.Fun.hasRefQualifier()) {
2488683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                Loc = Chunk.Fun.getRefQualifierLoc();
2489683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                break;
2490683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              }
2491944aa60777e6ea1015c9423107f7925f6d91f4a0Douglas Gregor            }
2492944aa60777e6ea1015c9423107f7925f6d91f4a0Douglas Gregor
24938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(Loc, diag::err_invalid_ref_qualifier_function_type)
2494683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << (FnTy->getRefQualifier() == RQ_LValue)
2495683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << FixItHint::CreateRemoval(Loc);
2496683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          } else {
24978cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getIdentifierLoc(),
2498683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                 diag::err_invalid_ref_qualifier_typedef_function_type_use)
2499683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << FreeFunction
2500683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << (FnTy->getRefQualifier() == RQ_LValue);
2501683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          }
2502c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        }
2503c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
2504683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        // Strip the cv-qualifiers and ref-qualifiers from the type.
2505683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
2506683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        EPI.TypeQuals = 0;
2507683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        EPI.RefQualifier = RQ_None;
2508c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
2509683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        T = Context.getFunctionType(FnTy->getResultType(),
2510683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                                    FnTy->arg_type_begin(),
2511683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                                    FnTy->getNumArgs(), EPI);
2512683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      }
2513971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
2514971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
25151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
2517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull())
2518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = D.getAttributes())
2519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      processTypeAttrs(state, T, false, attrs);
2520711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2521711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
2522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
2523711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2524148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  // C++0x [dcl.constexpr]p9:
2525148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  //  A constexpr specifier used in an object declaration declares the object
2526148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  //  as const.
2527148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
2528737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
2529737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
2530737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
2531a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // If there was an ellipsis in the declarator, the declaration declares a
2532a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
2533a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (D.hasEllipsis() && !T.isNull()) {
2534a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
2535a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   A declarator-id or abstract-declarator containing an ellipsis shall
2536a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
2537a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
2538a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
2539a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
2540a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
2541a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   [...] When it is part of a parameter-declaration-clause, the
2542a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   parameter pack is a function parameter pack (14.5.3). The type T
2543a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
2544a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   a template parameter pack; each template parameter pack in T is
2545a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
2546a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2547a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
2548a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
2549a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
25508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getEllipsisLoc(),
2551a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
2552a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
2553a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
2554a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
2555cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
2556a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
2557a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2558a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
2559a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
2560a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
2561a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   If a template-parameter is a [...] is a parameter-declaration that
2562a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
2563a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
2564a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2565a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
2566a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
2567a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
2568a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
2569cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
2570e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith      else
2571e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith        S.Diag(D.getEllipsisLoc(),
2572e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith               LangOpts.CPlusPlus0x
2573e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 ? diag::warn_cxx98_compat_variadic_templates
2574e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 : diag::ext_variadic_templates);
2575a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2576a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
2577a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
2578a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
2579cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:  // FIXME: special diagnostic here?
2580cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:     // FIXME: special diagnostic here?
2581a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
25820b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
2583162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
25843e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
2585a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
2586a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
2587a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
2588a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
2589a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
259017b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
2591a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
2592f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2593683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    case Declarator::TemplateTypeArgContext:
2594a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
2595a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
25968cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      S.Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
2597a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
2598a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2599a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
2600a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
2601e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2602bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
2603bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
2604bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
2605bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
2606db7abf78dedc2ef6ccb42b3dac6ab330fe2ea469Argyrios Kyrtzidis
26078cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return S.GetTypeSourceInfoForDeclarator(D, T, TInfo);
26088cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
26098cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
26108cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// GetTypeForDeclarator - Convert the type for the specified
26118cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// declarator to Type instances.
26128cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis///
26138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// The result of this call will never be null, but the associated
26148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// type may be a null type if there's an unrecoverable error.
26158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
26168cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // Determine the type of the declarator. Not all forms of declarator
26178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // have a type.
26188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
26198cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeProcessingState state(*this, D);
26208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
26218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
26228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
26238cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (T.isNull())
26248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
26258cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
26268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (D.isPrototypeContext() && getLangOptions().ObjCAutoRefCount)
26278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    inferARCWriteback(state, T);
2628db7abf78dedc2ef6ccb42b3dac6ab330fe2ea469Argyrios Kyrtzidis
26298cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return GetFullTypeForDeclarator(state, T, ReturnTypeInfo);
26305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
26315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
263231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclSpec(Sema &S,
263331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           QualType &declSpecTy,
263431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           Qualifiers::ObjCLifetime ownership) {
263531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy->isObjCRetainableType() &&
263631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) {
263731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers qs;
263831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    qs.addObjCLifetime(ownership);
263931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    declSpecTy = S.Context.getQualifiedType(declSpecTy, qs);
264031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
264131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
264231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
264331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
264431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
264531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            unsigned chunkIndex) {
264631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
264731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
264831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
264931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Look for an explicit lifetime attribute.
265031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclaratorChunk &chunk = D.getTypeObject(chunkIndex);
265131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (const AttributeList *attr = chunk.getAttrs(); attr;
265231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis         attr = attr->getNext())
265331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (attr->getKind() == AttributeList::AT_objc_ownership)
265431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
265531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
265631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  const char *attrStr = 0;
265731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  switch (ownership) {
26583026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
265931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break;
266031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Strong: attrStr = "strong"; break;
266131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Weak: attrStr = "weak"; break;
266231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break;
266331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
266431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
266531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // If there wasn't one, add one (with an invalid source location
266631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // so that we don't make an AttributedType for it).
266731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  AttributeList *attr = D.getAttributePool()
266831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    .create(&S.Context.Idents.get("objc_ownership"), SourceLocation(),
266931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            /*scope*/ 0, SourceLocation(),
267031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            &S.Context.Idents.get(attrStr), SourceLocation(),
267131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            /*args*/ 0, 0,
267231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            /*declspec*/ false, /*C++0x*/ false);
267331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  spliceAttrIntoList(*attr, chunk.getAttrListRef());
267431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
267531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // TODO: mark whether we did this inference?
267631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
267731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
26786ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis/// \brief Used for transfering ownership in casts resulting in l-values.
267931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnership(TypeProcessingState &state,
268031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 QualType &declSpecTy,
268131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 Qualifiers::ObjCLifetime ownership) {
268231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
268331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
268431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
268531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  int inner = -1;
26866ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis  bool hasIndirection = false;
268731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
268831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    DeclaratorChunk &chunk = D.getTypeObject(i);
268931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    switch (chunk.Kind) {
269031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Paren:
269131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      // Ignore parens.
269231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
269331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
269431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Array:
269531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Reference:
269631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Pointer:
26976ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
26986ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        hasIndirection = true;
269931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      inner = i;
270031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
270131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
270231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::BlockPointer:
27036ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
27046ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        transferARCOwnershipToDeclaratorChunk(state, ownership, i);
27056ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      return;
270631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
270731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Function:
270831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::MemberPointer:
270931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
271031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    }
271131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
271231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
271331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (inner == -1)
27146ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    return;
271531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
271631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclaratorChunk &chunk = D.getTypeObject(inner);
271731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (chunk.Kind == DeclaratorChunk::Pointer) {
271831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (declSpecTy->isObjCRetainableType())
271931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
27206ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    if (declSpecTy->isObjCObjectType() && hasIndirection)
272131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclaratorChunk(state, ownership, inner);
272231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  } else {
272331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    assert(chunk.Kind == DeclaratorChunk::Array ||
272431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis           chunk.Kind == DeclaratorChunk::Reference);
272531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
272631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
272731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
272831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
272931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) {
273031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeProcessingState state(*this, D);
273131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
273231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
273331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
273431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy.isNull())
273531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
273631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
273731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (getLangOptions().ObjCAutoRefCount) {
273831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy);
273931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (ownership != Qualifiers::OCL_None)
274031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      transferARCOwnership(state, declSpecTy, ownership);
274131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
274231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
274331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo);
274431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
274531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
274614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall/// Map an AttributedType::Kind to an AttributeList::Kind.
274714aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
274814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  switch (kind) {
274914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_address_space:
275014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_address_space;
275114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_regparm:
275214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_regparm;
275314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_vector_size:
275414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_vector_size;
275514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_vector_type:
275614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_neon_vector_type;
275714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_polyvector_type:
275814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_neon_polyvector_type;
275914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_objc_gc:
276014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_objc_gc;
2761b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  case AttributedType::attr_objc_ownership:
2762b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis    return AttributeList::AT_objc_ownership;
276314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_noreturn:
276414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_noreturn;
276514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_cdecl:
276614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_cdecl;
276714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_fastcall:
276814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_fastcall;
276914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_stdcall:
277014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_stdcall;
277114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_thiscall:
277214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_thiscall;
277314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_pascal:
277414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_pascal;
2775414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case AttributedType::attr_pcs:
2776414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    return AttributeList::AT_pcs;
277714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
277814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  llvm_unreachable("unexpected attribute kind!");
277914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
278014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
278114aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic void fillAttributedTypeLoc(AttributedTypeLoc TL,
278214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                  const AttributeList *attrs) {
278314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributedType::Kind kind = TL.getAttrKind();
278414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
278514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  assert(attrs && "no type attributes in the expected location!");
278614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributeList::Kind parsedKind = getAttrListKind(kind);
278714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  while (attrs->getKind() != parsedKind) {
278814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    attrs = attrs->getNext();
278914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    assert(attrs && "no matching attribute in expected location!");
279014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
279114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
279214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  TL.setAttrNameLoc(attrs->getLoc());
279314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrExprOperand())
279414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrExprOperand(attrs->getArg(0));
279514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  else if (TL.hasAttrEnumOperand())
279614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrEnumOperandLoc(attrs->getParameterLoc());
279714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
279814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // FIXME: preserve this information to here.
279914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrOperand())
280014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrOperandParensRange(SourceRange());
280114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
280214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
280351bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
280451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
2805c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    ASTContext &Context;
280651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
2807f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
280851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
2809c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
2810c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      : Context(Context), DS(DS) {}
2811f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
281214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
281314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, DS.getAttributes().getList());
281414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      Visit(TL.getModifiedLoc());
281514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
281651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
281751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
281851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
281951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
282051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
282151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
282251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
282351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
2824c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
2825c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2826c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
2827c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
2828c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
2829c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.getBaseLoc().initialize(Context, SourceLocation());
2830c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
2831c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
2832c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
2833c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
283454e14c4db764c0636160d26c5bbf491637c83a76John McCall
2835c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
283654e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
283754e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
283854e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
283954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
284054e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
284154e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
284254e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
284354e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
284454e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
284554e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
284654e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
284754e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
284851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
284954e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
285054e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
2851c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
285251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
2853833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
2854a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
2855b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2856833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2857833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
2858833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
2859a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
28600daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara        TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
2861833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
2862833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
2863833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2864e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
2865e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
2866e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
2867e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TemplateSpecializationTypeLoc NamedTL =
2868e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
2869e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
2870e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2871e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      else
2872e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
2873833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
2874cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
2875cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
2876cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2877cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
2878cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
2879cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
2880cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
2881cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2882cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
2883b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
2884cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
2885b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2886cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
2887cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
2888ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
2889ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      // FIXME: This holds only because we only have one unary transform.
2890ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType);
2891ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setKWLoc(DS.getTypeSpecTypeLoc());
2892ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setParensRange(DS.getTypeofParensRange());
2893ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getRepAsType());
2894ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TypeSourceInfo *TInfo = 0;
2895ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2896ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setUnderlyingTInfo(TInfo);
2897ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
2898ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
2899ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
2900ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
2901ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
2902ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
2903ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
2904ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
2905ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
2906ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
2907ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
2908ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
2909ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
2910ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
2911ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
2912ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
2913e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
2914e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
2915e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
2916253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
2917e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
2918b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2919e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
2920e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
2921e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
2922e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
2923e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2924e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setKeywordLoc(Keyword != ETK_None
2925e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       ? DS.getTypeSpecTypeLoc()
2926e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       : SourceLocation());
2927e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
29289e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
2929e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
2930e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
2931e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
2932e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
2933e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
2934253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
2935e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
2936b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2937e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
2938e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
2939e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
2940e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
2941e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2942e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setKeywordLoc(Keyword != ETK_None
2943e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       ? DS.getTypeSpecTypeLoc()
2944e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       : SourceLocation());
2945e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
29462494dd024b392b8def58bf067cc94b51c214cf77Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
29470daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
294833500955d731c73717af52088b7fc0e7a85681e7John McCall    }
294933500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
295033500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
295133500955d731c73717af52088b7fc0e7a85681e7John McCall      ElaboratedTypeKeyword Keyword
295233500955d731c73717af52088b7fc0e7a85681e7John McCall        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
295333500955d731c73717af52088b7fc0e7a85681e7John McCall      if (Keyword == ETK_Typename) {
295433500955d731c73717af52088b7fc0e7a85681e7John McCall        TypeSourceInfo *TInfo = 0;
2955b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
295633500955d731c73717af52088b7fc0e7a85681e7John McCall        if (TInfo) {
295733500955d731c73717af52088b7fc0e7a85681e7John McCall          TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
295833500955d731c73717af52088b7fc0e7a85681e7John McCall                    TInfo->getTypeLoc()));
295933500955d731c73717af52088b7fc0e7a85681e7John McCall          return;
296033500955d731c73717af52088b7fc0e7a85681e7John McCall        }
296133500955d731c73717af52088b7fc0e7a85681e7John McCall      }
2962c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initializeLocal(Context, SourceLocation());
296333500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setKeywordLoc(Keyword != ETK_None
296433500955d731c73717af52088b7fc0e7a85681e7John McCall                       ? DS.getTypeSpecTypeLoc()
296533500955d731c73717af52088b7fc0e7a85681e7John McCall                       : SourceLocation());
296633500955d731c73717af52088b7fc0e7a85681e7John McCall      const CXXScopeSpec& SS = DS.getTypeSpecScope();
296794fdffa4a572fc14ac296f5f1aae9db3734c72f1Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
29680daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
29690daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    }
29700daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    void VisitTagTypeLoc(TagTypeLoc TL) {
29710daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
2972e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
2973b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
2974b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      TL.setKWLoc(DS.getTypeSpecTypeLoc());
2975b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      TL.setParensRange(DS.getTypeofParensRange());
297643fe245b37c3cd36d837aab9eb98551328d30141Douglas Gregor
297743fe245b37c3cd36d837aab9eb98551328d30141Douglas Gregor      TypeSourceInfo *TInfo = 0;
297843fe245b37c3cd36d837aab9eb98551328d30141Douglas Gregor      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
297943fe245b37c3cd36d837aab9eb98551328d30141Douglas Gregor      TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
2980b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
2981e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
298251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
298351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
2984c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initialize(Context, DS.getTypeSpecTypeLoc());
298551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
298651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
2987eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
298851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
2989b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    ASTContext &Context;
299051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
2991f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
299251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
2993b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
2994b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      : Context(Context), Chunk(Chunk) {}
29954adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
299651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
29979f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
299851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
29994adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3000f85e193739c953358c865005855253af4f68a497John McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3001f85e193739c953358c865005855253af4f68a497John McCall      fillAttributedTypeLoc(TL, Chunk.getAttrs());
3002f85e193739c953358c865005855253af4f68a497John McCall    }
300351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
300451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
300551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
30064adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
300751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
300851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
300951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
30104adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
301151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
301251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
301351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
30144adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
301551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
301651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
3017b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const CXXScopeSpec& SS = Chunk.Mem.Scope();
3018b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
3019b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3020b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const Type* ClsTy = TL.getClass();
3021b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      QualType ClsQT = QualType(ClsTy, 0);
3022b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
3023b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Now copy source location info into the type loc component.
3024b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeLoc ClsTL = ClsTInfo->getTypeLoc();
3025b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
3026b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Identifier:
3027b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
3028b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        {
3029fd9c42ed22fb4f7f865f7d8f8848df84ddf8262cAbramo Bagnara          DependentNameTypeLoc DNTLoc = cast<DependentNameTypeLoc>(ClsTL);
3030b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setKeywordLoc(SourceLocation());
3031b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
3032b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
3033b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3034b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3035b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3036b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpec:
3037b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpecWithTemplate:
3038b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        if (isa<ElaboratedType>(ClsTy)) {
3039b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ElaboratedTypeLoc ETLoc = *cast<ElaboratedTypeLoc>(&ClsTL);
3040b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ETLoc.setKeywordLoc(SourceLocation());
3041b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ETLoc.setQualifierLoc(NNSLoc.getPrefix());
3042b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
3043b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
3044b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        } else {
3045b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
3046b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3047b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3048b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3049b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Namespace:
3050b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::NamespaceAlias:
3051b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Global:
3052b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        llvm_unreachable("Nested-name-specifier must name a type");
3053b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      }
3054b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3055b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Finally fill in MemberPointerLocInfo fields.
305651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
3057b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TL.setClassTInfo(ClsTInfo);
30584adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
305951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
306051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
306154e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
306254e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
306351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
306451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
306551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
306651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
306751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
306851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
306951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
307051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
307151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
307251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
307351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
307451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
307551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
307651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
307751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
3078796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeBegin(Chunk.Loc);
3079796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeEnd(Chunk.EndLoc);
3080dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor      TL.setTrailingReturn(!!Chunk.Fun.TrailingReturnType);
308151bd803fbdade51d674598ed45da3d54190a656cJohn McCall
308251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
308354e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
3084d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
308554e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
30864adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
308751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
30884adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
3089075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
3090075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
3091075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
3092075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
3093075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
30941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
309551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
30969f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
30974adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
309851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
309951bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
31004adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3101a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
310251bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
310351bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
310405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
310505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
310605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
310705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
310805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
3109a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
311005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
311105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
3112a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
3113a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
311451bd803fbdade51d674598ed45da3d54190a656cJohn McCall
3115a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
3116a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
3117a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc());
3118a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
3119a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
3120a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
31218ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
312214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    while (isa<AttributedTypeLoc>(CurrTL)) {
312314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      AttributedTypeLoc TL = cast<AttributedTypeLoc>(CurrTL);
312414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs());
312514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
312614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
312714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3128b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
312951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
31304adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
3131f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
3132b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
3133b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
3134b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
313505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
313605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
313705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
3138b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
3139c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
314005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
314105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor
3142a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
31434adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
31444adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3145a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
3146b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
31471bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
31481bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
31491bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
3150eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
3151eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
3152a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
31531bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
31541bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
3155b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
31561bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
31571bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
31581bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
31591bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
3160b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*"
316135d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
316235d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
31631bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
31641bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
3165f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
31665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
31675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
31685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
31691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3170d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
3171bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
31725153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
3173809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
31745912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
3175e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall  // Make sure there are no unused decl attributes on the declarator.
3176cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // We don't want to do this for ObjC parameters because we're going
3177cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // to apply them to the actual parameter declaration.
3178cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  if (D.getContext() != Declarator::ObjCParameterContext)
3179cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    checkUnusedDeclAttributes(D);
3180e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
3181402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  if (getLangOptions().CPlusPlus) {
3182402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
31836d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
3184402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
3185402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
3186b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
31875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
31885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3189e97179c675b341927807c718be215c8d1aab8acbDouglas GregorParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) {
3190e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  QualType T = Context.getObjCInstanceType();
3191e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc);
3192e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return CreateParsedType(T, TInfo);
3193e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
3194e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3195e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3196c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3197c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
3198c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3199232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3200232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
3201c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
3202c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
32031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
3204c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
32050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
3206232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
320729e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified by
320829e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // qualifiers for two or more different address spaces."
3209232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
3210c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
3211e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3212c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3213232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
32141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3215020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be
3216020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // qualified by an address-space qualifier."
3217020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  if (Type->isFunctionType()) {
3218020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type);
3219020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    Attr.setInvalid();
3220020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    return;
3221020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  }
3222020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne
3223232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
3224545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
3225f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3226e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3227c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3228232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3229545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
3230232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
3231ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
3232ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
3233dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
3234dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
3235e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3236c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3237232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3238232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3239efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
3240efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
3241efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
3242efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
3243efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
3244e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
3245efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
3246efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
3247efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
3248efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3249efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
32500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
3251efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
3252efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
32530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
3254e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3255efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
3256efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3257efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
32581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
3259f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
3260c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
3261c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
3262b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis/// handleObjCOwnershipTypeAttr - Process an objc_ownership
3263f85e193739c953358c865005855253af4f68a497John McCall/// attribute on the specified type.
3264f85e193739c953358c865005855253af4f68a497John McCall///
3265f85e193739c953358c865005855253af4f68a497John McCall/// Returns 'true' if the attribute was handled.
3266b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
3267f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr,
3268f85e193739c953358c865005855253af4f68a497John McCall                                       QualType &type) {
3269e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  bool NonObjCPointer = false;
3270e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3271e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!type->isDependentType()) {
3272e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    if (const PointerType *ptr = type->getAs<PointerType>()) {
3273e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      QualType pointee = ptr->getPointeeType();
3274e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      if (pointee->isObjCRetainableType() || pointee->isPointerType())
3275e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis        return false;
3276e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // It is important not to lose the source info that there was an attribute
3277e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // applied to non-objc pointer. We will create an attributed type but
3278e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // its type will be the same as the original type.
3279e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      NonObjCPointer = true;
3280e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    } else if (!type->isObjCRetainableType()) {
3281e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      return false;
3282e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3283e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3284f85e193739c953358c865005855253af4f68a497John McCall
3285f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
3286440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  SourceLocation AttrLoc = attr.getLoc();
3287440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isMacroID())
3288440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    AttrLoc = S.getSourceManager().getImmediateExpansionRange(AttrLoc).first;
3289f85e193739c953358c865005855253af4f68a497John McCall
3290f85e193739c953358c865005855253af4f68a497John McCall  if (type.getQualifiers().getObjCLifetime()) {
3291440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant)
3292f85e193739c953358c865005855253af4f68a497John McCall      << type;
3293f85e193739c953358c865005855253af4f68a497John McCall    return true;
3294f85e193739c953358c865005855253af4f68a497John McCall  }
3295f85e193739c953358c865005855253af4f68a497John McCall
3296f85e193739c953358c865005855253af4f68a497John McCall  if (!attr.getParameterName()) {
3297440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::err_attribute_argument_n_not_string)
3298b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      << "objc_ownership" << 1;
3299f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3300f85e193739c953358c865005855253af4f68a497John McCall    return true;
3301f85e193739c953358c865005855253af4f68a497John McCall  }
3302f85e193739c953358c865005855253af4f68a497John McCall
3303f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime lifetime;
3304f85e193739c953358c865005855253af4f68a497John McCall  if (attr.getParameterName()->isStr("none"))
3305f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_ExplicitNone;
3306f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("strong"))
3307f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Strong;
3308f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("weak"))
3309f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Weak;
3310f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("autoreleasing"))
3311f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Autoreleasing;
3312f85e193739c953358c865005855253af4f68a497John McCall  else {
3313440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_attribute_type_not_supported)
3314b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      << "objc_ownership" << attr.getParameterName();
3315f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3316f85e193739c953358c865005855253af4f68a497John McCall    return true;
3317f85e193739c953358c865005855253af4f68a497John McCall  }
3318f85e193739c953358c865005855253af4f68a497John McCall
3319f85e193739c953358c865005855253af4f68a497John McCall  // Consume lifetime attributes without further comment outside of
3320f85e193739c953358c865005855253af4f68a497John McCall  // ARC mode.
3321f85e193739c953358c865005855253af4f68a497John McCall  if (!S.getLangOptions().ObjCAutoRefCount)
3322f85e193739c953358c865005855253af4f68a497John McCall    return true;
3323f85e193739c953358c865005855253af4f68a497John McCall
3324e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (NonObjCPointer) {
3325e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    StringRef name = attr.getName()->getName();
3326e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    switch (lifetime) {
3327e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_None:
3328e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_ExplicitNone:
3329e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      break;
3330e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Strong: name = "__strong"; break;
3331e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Weak: name = "__weak"; break;
3332e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break;
3333e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3334e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_objc_object_attribute_wrong_type)
3335e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      << name << type;
3336e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3337e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3338f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers qs;
3339f85e193739c953358c865005855253af4f68a497John McCall  qs.setObjCLifetime(lifetime);
3340f85e193739c953358c865005855253af4f68a497John McCall  QualType origType = type;
3341e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!NonObjCPointer)
3342e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    type = S.Context.getQualifiedType(type, qs);
3343f85e193739c953358c865005855253af4f68a497John McCall
3344f85e193739c953358c865005855253af4f68a497John McCall  // If we have a valid source location for the attribute, use an
3345f85e193739c953358c865005855253af4f68a497John McCall  // AttributedType instead.
3346440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isValid())
3347b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis    type = S.Context.getAttributedType(AttributedType::attr_objc_ownership,
3348f85e193739c953358c865005855253af4f68a497John McCall                                       origType, type);
3349f85e193739c953358c865005855253af4f68a497John McCall
33509f084a3166b684573ba49df28fc5792bc37d92e1John McCall  // Forbid __weak if the runtime doesn't support it.
3351f85e193739c953358c865005855253af4f68a497John McCall  if (lifetime == Qualifiers::OCL_Weak &&
33525cad82236ecc0a2eeed2edd75e119f6069a99f4cArgyrios Kyrtzidis      !S.getLangOptions().ObjCRuntimeHasWeak && !NonObjCPointer) {
3353f85e193739c953358c865005855253af4f68a497John McCall
3354f85e193739c953358c865005855253af4f68a497John McCall    // Actually, delay this until we know what we're parsing.
3355f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
3356f85e193739c953358c865005855253af4f68a497John McCall      S.DelayedDiagnostics.add(
3357440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis          sema::DelayedDiagnostic::makeForbiddenType(
3358440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis              S.getSourceManager().getExpansionLoc(AttrLoc),
3359f85e193739c953358c865005855253af4f68a497John McCall              diag::err_arc_weak_no_runtime, type, /*ignored*/ 0));
3360f85e193739c953358c865005855253af4f68a497John McCall    } else {
3361440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis      S.Diag(AttrLoc, diag::err_arc_weak_no_runtime);
3362f85e193739c953358c865005855253af4f68a497John McCall    }
3363f85e193739c953358c865005855253af4f68a497John McCall
3364f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3365f85e193739c953358c865005855253af4f68a497John McCall    return true;
3366f85e193739c953358c865005855253af4f68a497John McCall  }
3367742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian
3368742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  // Forbid __weak for class objects marked as
3369742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  // objc_arc_weak_reference_unavailable
3370742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  if (lifetime == Qualifiers::OCL_Weak) {
3371742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    QualType T = type;
3372742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    while (const PointerType *ptr = T->getAs<PointerType>())
3373742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      T = ptr->getPointeeType();
3374742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
3375742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl();
33767263feeb367ab55af7e9a6fd701148b1b8264dbaFariborz Jahanian      if (Class->isArcWeakrefUnavailable()) {
3377440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis          S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class);
3378742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian          S.Diag(ObjT->getInterfaceDecl()->getLocation(),
3379742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian                 diag::note_class_declared);
3380742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      }
3381742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    }
3382742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  }
3383742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian
3384f85e193739c953358c865005855253af4f68a497John McCall  return true;
3385f85e193739c953358c865005855253af4f68a497John McCall}
3386f85e193739c953358c865005855253af4f68a497John McCall
3387711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
3388711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
3389711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
3390711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
3391711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
3392711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
3393711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
3394711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
3395711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3396711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
3397711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
3398711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
3399711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
3400711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
3401711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3402711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
3403711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
3404711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3405711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3406d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
34071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3408d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
3409711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!attr.getParameterName()) {
3410711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3411ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
3412711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3414ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
34150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
3416711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getNumArgs() != 0) {
3417711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3418711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3419711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3420d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
3421711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getParameterName()->isStr("weak"))
34220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
3423711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  else if (attr.getParameterName()->isStr("strong"))
34240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
3425d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
3426711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
3427711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      << "objc_gc" << attr.getParameterName();
3428711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3429711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3430d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
34311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
343214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  QualType origType = type;
343314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  type = S.Context.getObjCGCQualType(origType, GCAttr);
343414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
343514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // Make an attributed type to preserve the source information.
343614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (attr.getLoc().isValid())
343714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
343814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                       origType, type);
343914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3440711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
3441d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
3442d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
3443e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallnamespace {
3444e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// A helper class to unwrap a type down to a function for the
3445e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// purposes of applying attributes there.
3446e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///
3447e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// Use:
3448e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
3449e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   if (unwrapped.isFunctionType()) {
3450e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     const FunctionType *fn = unwrapped.get();
3451e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     // change fn somehow
3452e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     T = unwrapped.wrap(fn);
3453e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   }
3454e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  struct FunctionTypeUnwrapper {
3455e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    enum WrapKind {
3456e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Desugar,
3457e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Parens,
3458e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Pointer,
3459e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      BlockPointer,
3460e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Reference,
3461e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      MemberPointer
3462e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    };
3463e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3464e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType Original;
3465e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *Fn;
34665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<unsigned char /*WrapKind*/, 8> Stack;
3467e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3468e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
3469e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      while (true) {
3470e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const Type *Ty = T.getTypePtr();
3471e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<FunctionType>(Ty)) {
3472e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Fn = cast<FunctionType>(Ty);
3473e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return;
3474e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ParenType>(Ty)) {
3475e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ParenType>(Ty)->getInnerType();
3476e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Parens);
3477e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<PointerType>(Ty)) {
3478e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<PointerType>(Ty)->getPointeeType();
3479e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Pointer);
3480e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<BlockPointerType>(Ty)) {
3481e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<BlockPointerType>(Ty)->getPointeeType();
3482e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(BlockPointer);
3483e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<MemberPointerType>(Ty)) {
3484e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<MemberPointerType>(Ty)->getPointeeType();
3485e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(MemberPointer);
3486e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ReferenceType>(Ty)) {
3487e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ReferenceType>(Ty)->getPointeeType();
3488e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Reference);
3489e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else {
3490e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          const Type *DTy = Ty->getUnqualifiedDesugaredType();
3491e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          if (Ty == DTy) {
3492e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            Fn = 0;
3493e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            return;
3494e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          }
3495e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3496e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = QualType(DTy, 0);
3497e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Desugar);
3498e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        }
3499e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3500e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3501e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3502e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    bool isFunctionType() const { return (Fn != 0); }
3503e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *get() const { return Fn; }
3504e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3505e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(Sema &S, const FunctionType *New) {
3506e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // If T wasn't modified from the unwrapped type, do nothing.
3507e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (New == get()) return Original;
3508e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3509e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Fn = New;
3510e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return wrap(S.Context, Original, 0);
3511e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3512e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3513e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  private:
3514e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
3515e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size())
3516e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getQualifiedType(Fn, Old.getQualifiers());
3517e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3518e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // Build up the inner type, applying the qualifiers from the old
3519e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // type to the new type.
3520e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      SplitQualType SplitOld = Old.split();
3521e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3522e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // As a special case, tail-recurse if there are no qualifiers.
3523e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (SplitOld.second.empty())
3524e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, SplitOld.first, I);
3525e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return C.getQualifiedType(wrap(C, SplitOld.first, I), SplitOld.second);
3526e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3527e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3528e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
3529e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size()) return QualType(Fn, 0);
3530e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3531e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      switch (static_cast<WrapKind>(Stack[I++])) {
3532e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Desugar:
3533e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // This is the point at which we potentially lose source
3534e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // information.
3535e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
3536e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3537e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Parens: {
3538e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
3539e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getParenType(New);
3540e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3541e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3542e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Pointer: {
3543e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
3544e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getPointerType(New);
3545e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3546e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3547e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case BlockPointer: {
3548e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
3549e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getBlockPointerType(New);
3550e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3551e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3552e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case MemberPointer: {
3553e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
3554e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldMPT->getPointeeType(), I);
3555e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getMemberPointerType(New, OldMPT->getClass());
3556e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3557e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3558e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Reference: {
3559e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const ReferenceType *OldRef = cast<ReferenceType>(Old);
3560e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldRef->getPointeeType(), I);
3561e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<LValueReferenceType>(OldRef))
3562e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
3563e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        else
3564e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getRValueReferenceType(New);
3565e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3566e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3567e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3568e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      llvm_unreachable("unknown wrapping kind");
3569e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3570e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  };
3571e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
3572e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3573711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
3574711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
3575711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
3576711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
3577711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
3578711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
3579e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3580711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
35812455636163fdd18581d7fdae816433f886d88213Mike Stump
3582711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getKind() == AttributeList::AT_noreturn) {
3583711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
358404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
3585e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3586e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
3587711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
3588711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
3589425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
3590425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
3591711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
3592711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3593711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3594711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
3595425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
3596f85e193739c953358c865005855253af4f68a497John McCall  // ns_returns_retained is not always a type attribute, but if we got
3597f85e193739c953358c865005855253af4f68a497John McCall  // here, we're treating it as one right now.
3598f85e193739c953358c865005855253af4f68a497John McCall  if (attr.getKind() == AttributeList::AT_ns_returns_retained) {
3599f85e193739c953358c865005855253af4f68a497John McCall    assert(S.getLangOptions().ObjCAutoRefCount &&
3600f85e193739c953358c865005855253af4f68a497John McCall           "ns_returns_retained treated as type attribute in non-ARC");
3601f85e193739c953358c865005855253af4f68a497John McCall    if (attr.getNumArgs()) return true;
3602f85e193739c953358c865005855253af4f68a497John McCall
3603f85e193739c953358c865005855253af4f68a497John McCall    // Delay if this is not a function type.
3604f85e193739c953358c865005855253af4f68a497John McCall    if (!unwrapped.isFunctionType())
3605f85e193739c953358c865005855253af4f68a497John McCall      return false;
3606f85e193739c953358c865005855253af4f68a497John McCall
3607f85e193739c953358c865005855253af4f68a497John McCall    FunctionType::ExtInfo EI
3608f85e193739c953358c865005855253af4f68a497John McCall      = unwrapped.get()->getExtInfo().withProducesResult(true);
3609f85e193739c953358c865005855253af4f68a497John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3610f85e193739c953358c865005855253af4f68a497John McCall    return true;
3611f85e193739c953358c865005855253af4f68a497John McCall  }
3612f85e193739c953358c865005855253af4f68a497John McCall
3613711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getKind() == AttributeList::AT_regparm) {
3614711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
3615711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
3616711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
36171e030eb1194763b42c1752723be23b1515f48981John McCall
3618711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
3619711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
3620008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
36211e030eb1194763b42c1752723be23b1515f48981John McCall
3622ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Diagnose regparm with fastcall.
3623ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    const FunctionType *fn = unwrapped.get();
3624ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    CallingConv CC = fn->getCallConv();
3625ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (CC == CC_X86FastCall) {
3626ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
3627ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC)
3628ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm";
3629ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
3630ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
3631ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
3632ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
3633e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionType::ExtInfo EI =
3634711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
3635711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3636711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3637425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
3638425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
363904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
3640711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
3641711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
3642711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3643f82b4e85b1219295cad4b5851b035575bc293010John McCall
364404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Delay if the type didn't work out to a function.
3645711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!unwrapped.isFunctionType()) return false;
364604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
3647711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
3648711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
3649064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis  if (S.Context.getCanonicalCallConv(CC) ==
3650e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      S.Context.getCanonicalCallConv(CCOld)) {
3651ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
3652ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3653711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3654e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  }
365504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
36568e68f1c8a2919ea83c2053731d6011074f1062e1Roman Divacky  if (CCOld != (S.LangOpts.MRTD ? CC_X86StdCall : CC_Default)) {
365704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Should we diagnose reapplications of the same convention?
3658711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
365904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CC)
366004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CCOld);
3661711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3662711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
366304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
366404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
366504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
366604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
3667711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isa<FunctionNoProtoType>(fn)) {
3668711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_knr)
366904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
3670711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
3671711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
367204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
367304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
3674711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
367504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
3676711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
367704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
3678711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
3679711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
368004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
3681ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
3682ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Also diagnose fastcall with regparm.
3683a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    if (fn->getHasRegParm()) {
3684ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
3685ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm"
3686ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC);
3687ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
3688ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
3689ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
369004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
3691f82b4e85b1219295cad4b5851b035575bc293010John McCall
3692711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
3693711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3694711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
3695f82b4e85b1219295cad4b5851b035575bc293010John McCall}
3696f82b4e85b1219295cad4b5851b035575bc293010John McCall
3697207f4d8543529221932af82836016a2ef066c917Peter Collingbourne/// Handle OpenCL image access qualifiers: read_only, write_only, read_write
3698207f4d8543529221932af82836016a2ef066c917Peter Collingbournestatic void HandleOpenCLImageAccessAttribute(QualType& CurType,
3699207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const AttributeList &Attr,
3700207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             Sema &S) {
3701207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  // Check the attribute arguments.
3702207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (Attr.getNumArgs() != 1) {
3703207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3704207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
3705207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
3706207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
3707207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
3708207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  llvm::APSInt arg(32);
3709207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
3710207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      !sizeExpr->isIntegerConstantExpr(arg, S.Context)) {
3711207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3712207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << "opencl_image_access" << sizeExpr->getSourceRange();
3713207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
3714207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
3715207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
3716207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  unsigned iarg = static_cast<unsigned>(arg.getZExtValue());
3717207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  switch (iarg) {
3718207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_only:
3719207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_write_only:
3720207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_write:
3721207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
3722207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
3723207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  default:
3724207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
3725207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
3726207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << sizeExpr->getSourceRange();
3727207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
3728207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
3729207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
3730207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
3731207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
37326e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
37336e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
37346e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
37356e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
37366e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
37376e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
37386e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
3739788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
3740788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
374156affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
37426e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
37436e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3744e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
37456e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
37466e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
37476e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
37486e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
3749ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
3750ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
37516e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
37526e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << "vector_size" << sizeExpr->getSourceRange();
3753e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
37546e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
37556e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
37566e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
3757f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
37586e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
3759e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
37606e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
37616e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
37626e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
37636e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
37646e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
37656e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
37666e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
37676e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
37686e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
37696e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
3770e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
37716e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
37726e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
37736e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
37746e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
37756e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
3776e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
37776e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
37786e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
37796e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
37806e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
37816e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
3782788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
3783e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
37846e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
37856e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
37864ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// \brief Process the OpenCL-like ext_vector_type attribute when it occurs on
37874ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// a type.
37884ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregorstatic void HandleExtVectorTypeAttr(QualType &CurType,
37894ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                    const AttributeList &Attr,
37904ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                    Sema &S) {
37914ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  Expr *sizeExpr;
37924ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
37934ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Special case where the argument is a template id.
37944ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (Attr.getParameterName()) {
37954ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CXXScopeSpec SS;
37964ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    UnqualifiedId id;
37974ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
37984ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
37994ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    ExprResult Size = S.ActOnIdExpression(S.getCurScope(), SS, id, false,
38004ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                          false);
38014ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Size.isInvalid())
38024ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
38034ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
38044ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Size.get();
38054ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  } else {
38064ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    // check the attribute arguments.
38074ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Attr.getNumArgs() != 1) {
38084ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
38094ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
38104ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    }
38114ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Attr.getArg(0);
38124ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  }
38134ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
38144ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Create the vector type.
38154ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  QualType T = S.BuildExtVectorType(CurType, sizeExpr, Attr.getLoc());
38164ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (!T.isNull())
38174ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CurType = T;
38184ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor}
38194ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
38204211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
38214211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
38224211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
38234211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
38244211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
38254211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
38264211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
38274211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
38284211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
38294211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     VectorType::VectorKind VecKind,
38304211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const char *AttrName) {
38314211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
38324211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
38334211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
38344211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
38354211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
38364211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
38374211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
38384211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
38394211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
38404211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
38414211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
38424211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
38434211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      << AttrName << numEltsExpr->getSourceRange();
38444211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
38454211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
38464211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
38474211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
38484211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  const BuiltinType* BTy = CurType->getAs<BuiltinType>();
38494211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (!BTy ||
38504211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (VecKind == VectorType::NeonPolyVector &&
38514211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::SChar &&
38524211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short) ||
38534211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (BTy->getKind() != BuiltinType::SChar &&
38544211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UChar &&
38554211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short &&
38564211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UShort &&
38574211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Int &&
38584211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UInt &&
38594211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::LongLong &&
38604211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::ULongLong &&
38614211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Float)) {
38624211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
38634211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
38644211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
38654211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
38664211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
38674211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
38684211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
38694211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
38704211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
38714211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
38724211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
38734211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
38744211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
38754211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
38764211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
38774211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
38784211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
3879711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
3880711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             bool isDeclSpec, AttributeList *attrs) {
3881c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
3882c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
3883c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
3884c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
3885711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3886711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
3887711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
3888711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
3889711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr.getNext();
3890711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3891e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
3892711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
3893e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
3894e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
3895b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
3896b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
3897711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
3898c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
389904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
3900682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth    case AttributeList::AT_may_alias:
3901682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // FIXME: This attribute needs to actually be handled, but if we ignore
3902682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // it it breaks large amounts of Linux software.
3903682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      attr.setUsedAsTypeAttr();
3904682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      break;
3905c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
3906711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
3907e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
3908c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
3909711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
3910711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
3911711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
3912e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
3913d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
391404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    case AttributeList::AT_vector_size:
3915711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
3916e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
3917f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
39184ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case AttributeList::AT_ext_vector_type:
39194ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      if (state.getDeclarator().getDeclSpec().getStorageClassSpec()
39204ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor            != DeclSpec::SCS_typedef)
39214ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        HandleExtVectorTypeAttr(type, attr, state.getSema());
3922e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
39234ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      break;
39244211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    case AttributeList::AT_neon_vector_type:
3925711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
3926711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonVector, "neon_vector_type");
3927e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
39284211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
39294211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    case AttributeList::AT_neon_polyvector_type:
3930711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
3931711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonPolyVector,
39324211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                               "neon_polyvector_type");
3933e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
39344211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
3935207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    case AttributeList::AT_opencl_image_access:
3936207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      HandleOpenCLImageAccessAttribute(type, attr, state.getSema());
3937e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
3938207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      break;
3939207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
3940f85e193739c953358c865005855253af4f68a497John McCall    case AttributeList::AT_ns_returns_retained:
3941f85e193739c953358c865005855253af4f68a497John McCall      if (!state.getSema().getLangOptions().ObjCAutoRefCount)
3942f85e193739c953358c865005855253af4f68a497John McCall	break;
3943f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough into the function attrs
3944f85e193739c953358c865005855253af4f68a497John McCall
3945711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
3946e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
3947e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
3948711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
3949711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
3950711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (isDeclSpec)
3951711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
3952711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3953711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
3954711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
3955711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
39566e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
3957c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
3958711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attrs = next));
3959232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
3960232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3961e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \brief Ensure that the type of the given expression is complete.
3962e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
3963e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// This routine checks whether the expression \p E has a complete type. If the
3964e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// expression refers to an instantiable construct, that instantiation is
3965e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// performed as needed to complete its type. Furthermore
3966e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// Sema::RequireCompleteType is called for the expression's type (or in the
3967e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// case of a reference type, the referred-to type).
3968e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
3969e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \param E The expression whose type is required to be complete.
3970e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \param PD The partial diagnostic that will be printed out if the type cannot
3971e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// be completed.
3972e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
3973e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \returns \c true if the type of \p E is incomplete and diagnosed, \c false
3974e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// otherwise.
3975e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruthbool Sema::RequireCompleteExprType(Expr *E, const PartialDiagnostic &PD,
3976e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth                                   std::pair<SourceLocation,
3977e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth                                             PartialDiagnostic> Note) {
3978e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  QualType T = E->getType();
3979e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
3980e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Fast path the case where the type is already complete.
3981e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (!T->isIncompleteType())
3982e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    return false;
3983e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
3984e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Incomplete array types may be completed by the initializer attached to
3985e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // their definitions. For static data members of class templates we need to
3986e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // instantiate the definition to get this initializer and complete the type.
3987e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (T->isIncompleteArrayType()) {
3988e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
3989e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
3990e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        if (Var->isStaticDataMember() &&
3991e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth            Var->getInstantiatedFromStaticDataMember()) {
399236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor
399336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
399436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          assert(MSInfo && "Missing member specialization information?");
399536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          if (MSInfo->getTemplateSpecializationKind()
399636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                != TSK_ExplicitSpecialization) {
399736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // If we don't already have a point of instantiation, this is it.
399836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (MSInfo->getPointOfInstantiation().isInvalid()) {
399936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              MSInfo->setPointOfInstantiation(E->getLocStart());
400036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor
400136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              // This is a modification of an existing AST node. Notify
400236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              // listeners.
400336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              if (ASTMutationListener *L = getASTMutationListener())
400436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                L->StaticDataMemberInstantiated(Var);
400536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
400636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor
400736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            InstantiateStaticDataMemberDefinition(E->getExprLoc(), Var);
400836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor
400936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // Update the type to the newly instantiated definition's type both
401036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // here and within the expression.
401136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (VarDecl *Def = Var->getDefinition()) {
401236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setDecl(Def);
401336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              T = Def->getType();
401436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setType(T);
401536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              E->setType(T);
401636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
4017f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor          }
4018f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor
4019e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // We still go on to try to complete the type independently, as it
4020e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // may also require instantiations or diagnostics if it remains
4021e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // incomplete.
4022e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        }
4023e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      }
4024e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    }
4025e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  }
4026e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4027e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // FIXME: Are there other cases which require instantiating something other
4028e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // than the type to complete the type of an expression?
4029e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4030e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Look through reference types and complete the referred type.
4031e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
4032e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    T = Ref->getPointeeType();
4033e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4034e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  return RequireCompleteType(E->getExprLoc(), T, PD, Note);
4035e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth}
4036e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
40371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
40384ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
40394ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
40404ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
404186447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
404286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
404386447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
404486447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
404586447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
40464ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
40474ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
40484ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
40494ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
40504ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
40514ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
40521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @param PD The partial diagnostic that will be printed out if T is not a
4053b790661a15d93941d2c33a0ea328254277b3d7e3Anders Carlsson/// complete type.
40544ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
40554ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
40564ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
405791a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
40588c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               const PartialDiagnostic &PD,
40598c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               std::pair<SourceLocation,
40608c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                                         PartialDiagnostic> Note) {
406191a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  unsigned diag = PD.getDiagID();
40621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4063573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
4064573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
4065690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
4066690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
4067690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
40681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
4069690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
4070690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
40714ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
4072d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  NamedDecl *Def = 0;
4073d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  if (!T->isIncompleteType(&Def)) {
4074d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    // If we know about the definition but it is not visible, complain.
4075d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    if (diag != 0 && Def && !LookupResult::isVisible(Def)) {
4076d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // Suppress this error outside of a SFINAE context if we've already
4077d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // emitted the error once for this type. There's no usefulness in
4078d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // repeating the diagnostic.
4079d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // FIXME: Add a Fix-It that imports the corresponding module or includes
4080d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // the header.
4081d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      if (isSFINAEContext() || HiddenDefinitions.insert(Def)) {
4082d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor        Diag(Loc, diag::err_module_private_definition) << T;
4083d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor        Diag(Def->getLocation(), diag::note_previous_definition);
4084d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      }
4085d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    }
4086d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor
40874ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
4088d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
40894ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
4090bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const TagType *Tag = T->getAs<TagType>();
4091bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const ObjCInterfaceType *IFace = 0;
4092bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan
4093bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  if (Tag) {
4094bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4095bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->isInvalidDecl())
4096bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
4097bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan
4098bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4099bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->hasExternalLexicalStorage()) {
4100bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(Tag->getDecl());
4101bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!Tag->isIncompleteType())
4102bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4103bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4104bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
4105bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  else if ((IFace = T->getAs<ObjCInterfaceType>())) {
4106bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4107bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->isInvalidDecl())
4108bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
4109bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan
4110bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4111bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->hasExternalLexicalStorage()) {
4112bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(IFace->getDecl());
4113bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!IFace->isIncompleteType())
4114bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4115bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4116bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
4117bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan
4118d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
4119923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
4120923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
4121923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
412289c49f09b0292dc7c03885f6c765d667a9837597Douglas Gregor  if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
4123923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
4124923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
41252943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
4126d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
4127972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
4128972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
4129d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
41305842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor                                                      /*Complain=*/diag != 0);
41311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
4132d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
4133d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
4134b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
4135b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        assert(MSInfo && "Missing member specialization information?");
4136357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
4137b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        if (MSInfo->getTemplateSpecializationKind()
4138972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor                                               != TSK_ExplicitSpecialization)
4139f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
4140f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
4141f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
4142f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  /*Complain=*/diag != 0);
4143d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
4144d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
4145d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
41462943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
41475842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor  if (diag == 0)
41485842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
4149b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor
41504ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
415191a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  Diag(Loc, PD) << T;
4152b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor
41538c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  // If we have a note, produce it.
41548c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  if (!Note.first.isInvalid())
41558c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson    Diag(Note.first, Note.second);
41568c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson
41574ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
415801620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
41594ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
41601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
41614ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
41624ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
4163b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor      << QualType(Tag, 0);
4164b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor
4165b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  // If the Objective-C class was a forward declaration, produce a note.
4166b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  if (IFace && !IFace->getDecl()->isInvalidDecl())
4167b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor    Diag(IFace->getDecl()->getLocation(), diag::note_forward_class);
41684ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
41694ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
41704ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
4171e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
4172fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
4173fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               const PartialDiagnostic &PD) {
4174fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PD,
4175fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
4176fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
4177fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
4178fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
4179fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               unsigned DiagID) {
4180fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PDiag(DiagID),
4181fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
4182fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
4183fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
41849f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @brief Ensure that the type T is a literal type.
41859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
41869f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// This routine checks whether the type @p T is a literal type. If @p T is an
41879f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// incomplete type, an attempt is made to complete it. If @p T is a literal
41889f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// type, or @p AllowIncompleteType is true and @p T is an incomplete type,
41899f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// returns false. Otherwise, this routine issues the diagnostic @p PD (giving
41909f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// it the type @p T), along with notes explaining why the type is not a
41919f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// literal type, and returns true.
41929f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
41939f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param Loc  The location in the source that the non-literal type
41949f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// diagnostic should refer to.
41959f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
41969f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param T  The type that this routine is examining for literalness.
41979f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
41989f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param PD The partial diagnostic that will be printed out if T is not a
41999f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// literal type.
42009f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
42019f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param AllowIncompleteType If true, an incomplete type will be considered
42029f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// acceptable.
42039f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
42049f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @returns @c true if @p T is not a literal type and a diagnostic was emitted,
42059f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @c false otherwise.
42069f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithbool Sema::RequireLiteralType(SourceLocation Loc, QualType T,
42079f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                              const PartialDiagnostic &PD,
42089f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                              bool AllowIncompleteType) {
42099f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  assert(!T->isDependentType() && "type should not be dependent");
42109f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
42119f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  bool Incomplete = RequireCompleteType(Loc, T, 0);
42129f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (T->isLiteralType() || (AllowIncompleteType && Incomplete))
42139f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return false;
42149f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
42159f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (PD.getDiagID() == 0)
42169f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
42179f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
42189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  Diag(Loc, PD) << T;
42199f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
42209f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (T->isVariableArrayType())
42219f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
42229f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
42239f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  const RecordType *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>();
42249f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (!RT)
42259f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
42269f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
42279f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
42289f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
42299f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // If the class has virtual base classes, then it's not an aggregate, and
42309f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // cannot have any constexpr constructors, so is non-literal. This is better
42319f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // to diagnose than the resulting absence of constexpr constructors.
42329f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (RD->getNumVBases()) {
42339f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_virtual_base)
42349f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      << RD->isStruct() << RD->getNumVBases();
42359f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
42369f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           E = RD->vbases_end(); I != E; ++I)
42379f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      Diag(I->getSourceRange().getBegin(),
42389f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           diag::note_constexpr_virtual_base_here) << I->getSourceRange();
42399f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor()) {
42409f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD;
42419f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
42429f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    switch (RD->getTemplateSpecializationKind()) {
42439f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    case TSK_Undeclared:
42449f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    case TSK_ExplicitSpecialization:
42459f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      break;
42469f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
42479f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    case TSK_ImplicitInstantiation:
42489f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    case TSK_ExplicitInstantiationDeclaration:
42499f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    case TSK_ExplicitInstantiationDefinition:
42509f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      // If the base template had constexpr constructors which were
42519f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      // instantiated as non-constexpr constructors, explain why.
42529f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      for (CXXRecordDecl::ctor_iterator I = RD->ctor_begin(),
42539f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           E = RD->ctor_end(); I != E; ++I) {
42549f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        if ((*I)->isCopyConstructor() || (*I)->isMoveConstructor())
42559f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith          continue;
42569f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
42579f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        FunctionDecl *Base = (*I)->getInstantiatedFromMemberFunction();
42589f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        if (Base && Base->isConstexpr())
42599f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith          CheckConstexprFunctionDecl(*I, CCK_NoteNonConstexprInstantiation);
42609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
42619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
42629f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (RD->hasNonLiteralTypeFieldsOrBases()) {
42639f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
42649f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->bases_end(); I != E; ++I) {
42659f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      if (!I->getType()->isLiteralType()) {
42669f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        Diag(I->getSourceRange().getBegin(),
42679f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith             diag::note_non_literal_base_class)
42689f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith          << RD << I->getType() << I->getSourceRange();
42699f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
42709f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
42719f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
42729f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::field_iterator I = RD->field_begin(),
42739f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->field_end(); I != E; ++I) {
42749f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      if (!(*I)->getType()->isLiteralType()) {
42759f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        Diag((*I)->getLocation(), diag::note_non_literal_field)
42769f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith          << RD << (*I) << (*I)->getType();
42779f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
42785fa6a0422f12216d549d0f2991a29d5690634065Richard Smith      } else if ((*I)->isMutable()) {
42795fa6a0422f12216d549d0f2991a29d5690634065Richard Smith        Diag((*I)->getLocation(), diag::note_non_literal_mutable_field) << RD;
42805fa6a0422f12216d549d0f2991a29d5690634065Richard Smith        return true;
42819f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
42829f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
42839f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (!RD->hasTrivialDestructor()) {
42849f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // All fields and bases are of literal types, so have trivial destructors.
42859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // If this class's destructor is non-trivial it must be user-declared.
42869f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    CXXDestructorDecl *Dtor = RD->getDestructor();
42879f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    assert(Dtor && "class has literal fields and bases but no dtor?");
42889f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    if (!Dtor)
42899f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      return true;
42909f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
42919f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(Dtor->getLocation(), Dtor->isUserProvided() ?
42929f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_user_provided_dtor :
42939f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_nontrivial_dtor) << RD;
42949f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  }
42959f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
42969f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return true;
42979f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
42989f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4299465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
4300465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
4301465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
4302465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
4303465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
4304e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
4305465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
4306e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
4307465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
4308465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
4309465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
4310465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
4311465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
4312465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
4313465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
4314e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
4315af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
43162a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
4317fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
43182a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
43192a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
43202a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
43212b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
43222b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
4323aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
4324aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
43252b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
4326af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
4327af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
4328af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
43292a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
4330fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
43312a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
43322a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
43334b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
4334af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getDecltypeType(E);
4335af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
4336ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
4337ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType Sema::BuildUnaryTransformType(QualType BaseType,
4338ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::UTTKind UKind,
4339ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       SourceLocation Loc) {
4340ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  switch (UKind) {
4341ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case UnaryTransformType::EnumUnderlyingType:
4342ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) {
4343ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Diag(Loc, diag::err_only_enums_have_underlying_types);
4344ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return QualType();
4345ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    } else {
4346ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      QualType Underlying = BaseType;
4347ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      if (!BaseType->isDependentType()) {
4348ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl();
4349ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        assert(ED && "EnumType has no EnumDecl");
4350ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        DiagnoseUseOfDecl(ED, Loc);
4351ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        Underlying = ED->getIntegerType();
4352ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      }
4353ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(!Underlying.isNull());
4354ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return Context.getUnaryTransformType(BaseType, Underlying,
4355ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                        UnaryTransformType::EnumUnderlyingType);
4356ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
4357ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  }
4358ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  llvm_unreachable("unknown unary transform type");
4359ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
4360b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4361b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) {
4362b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T->isDependentType()) {
4363b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    int DisallowedKind = -1;
4364b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (T->isIncompleteType())
4365b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      // FIXME: It isn't entirely clear whether incomplete atomic types
4366b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      // are allowed or not; for simplicity, ban them for the moment.
4367b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 0;
4368b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isArrayType())
4369b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 1;
4370b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isFunctionType())
4371b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 2;
4372b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isReferenceType())
4373b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 3;
4374b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isAtomicType())
4375b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 4;
4376b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T.hasQualifiers())
4377b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 5;
4378b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (!T.isTriviallyCopyableType(Context))
4379b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      // Some other non-trivially-copyable type (probably a C++ class)
4380b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 6;
4381b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4382b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (DisallowedKind != -1) {
4383b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T;
4384b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return QualType();
4385b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
4386b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4387b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // FIXME: Do we need any handling for ARC here?
4388b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
4389b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4390b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Build the pointer type.
4391b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return Context.getAtomicType(T);
4392b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
4393