SemaType.cpp revision 9e876876afc13aa671cc11a17c19907c599b9ab9
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"
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
17a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor#include "clang/AST/CXXInheritance.h"
18980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
192943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor#include "clang/AST/DeclTemplate.h"
204adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
2151bd803fbdade51d674598ed45da3d54190a656cJohn McCall#include "clang/AST/TypeLocVisitor.h"
22e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
2391a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
24d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis#include "clang/Basic/TargetInfo.h"
2519510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
264994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl#include "llvm/ADT/SmallPtrSet.h"
2787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor#include "llvm/Support/ErrorHandling.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// \brief Perform adjustment on the parameter type of a function.
312dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor///
322dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// This routine adjusts the given parameter type @p T to the actual
331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
352dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorQualType Sema::adjustParameterType(QualType T) {
362dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // C99 6.7.5.3p7:
37778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   A declaration of a parameter as "array of type" shall be
38778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   adjusted to "qualified pointer to type", where the type
39778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   qualifiers (if any) are those specified within the [ and ] of
40778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   the array type derivation.
41778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  if (T->isArrayType())
422dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getArrayDecayedType(T);
43778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner
44778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  // C99 6.7.5.3p8:
45778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   A declaration of a parameter as "function returning type"
46778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   shall be adjusted to "pointer to function returning type", as
47778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   in 6.3.2.1.
48778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  if (T->isFunctionType())
492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getPointerType(T);
502dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
512dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return T;
522dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
532dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
545db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
555db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
565db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// isOmittedBlockReturnType - Return true if this declarator is missing a
575db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// return type because this is a omitted return type on a block literal.
588ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic bool isOmittedBlockReturnType(const Declarator &D) {
595db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getContext() != Declarator::BlockLiteralContext ||
608ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl      D.getDeclSpec().hasTypeSpecifier())
615db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return false;
625db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
635db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 0)
64a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^{ ... }
655db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
665db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 1 &&
675db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      D.getTypeObject(0).Kind == DeclaratorChunk::Function)
68a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^(int X, float Y) { ... }
695db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
705db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  return false;
715db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner}
725db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
73711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// objc_gc applies to Objective-C pointers or, otherwise, to the
74711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// smallest available pointer type (i.e. 'void*' in 'void**').
75711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
76711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_objc_gc
77711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
78711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// Function type attributes.
79711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define FUNCTION_TYPE_ATTRS_CASELIST \
80711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_noreturn: \
81711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_cdecl: \
82711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_fastcall: \
83711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_stdcall: \
84711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_thiscall: \
85711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_pascal: \
86711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_regparm
87711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
88711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace {
89711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// An object which stores processing state for the entire
90711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// GetTypeForDeclarator process.
91711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  class TypeProcessingState {
92711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &sema;
93711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
94711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The declarator being processed.
95711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &declarator;
96711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
97711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The index of the declarator chunk we're currently processing.
98711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// May be the total number of valid chunks, indicating the
99711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// DeclSpec.
100711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex;
101711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
102711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Whether there are non-trivial modifications to the decl spec.
103711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    bool trivial;
104711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
105711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The original set of attributes on the DeclSpec.
106711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    llvm::SmallVector<AttributeList*, 2> savedAttrs;
107711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
108711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// A list of attributes to diagnose the uselessness of when the
109711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// processing is complete.
110711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    llvm::SmallVector<AttributeList*, 2> ignoredTypeAttrs;
111711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
112711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  public:
113711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    TypeProcessingState(Sema &sema, Declarator &declarator)
114711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : sema(sema), declarator(declarator),
115711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        chunkIndex(declarator.getNumTypeObjects()),
116711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        trivial(true) {}
117711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
118711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &getSema() const {
119711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return sema;
120711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
121711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
122711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &getDeclarator() const {
123711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator;
124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
125711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
126711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned getCurrentChunkIndex() const {
127711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return chunkIndex;
128711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
129711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
130711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void setCurrentChunkIndex(unsigned idx) {
131711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(idx <= declarator.getNumTypeObjects());
132711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      chunkIndex = idx;
133711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
134711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
135711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&getCurrentAttrListRef() const {
136711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(chunkIndex <= declarator.getNumTypeObjects());
137711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (chunkIndex == declarator.getNumTypeObjects())
138711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        return getMutableDeclSpec().getAttributes().getListRef();
139711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator.getTypeObject(chunkIndex).getAttrListRef();
140711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
141711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
142711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Save the current set of attributes on the DeclSpec.
143711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void saveDeclSpecAttrs() {
144711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Don't try to save them multiple times.
145711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!savedAttrs.empty()) return;
146711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
147711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      DeclSpec &spec = getMutableDeclSpec();
148711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (AttributeList *attr = spec.getAttributes().getList(); attr;
149711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             attr = attr->getNext())
150711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs.push_back(attr);
151711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      trivial &= savedAttrs.empty();
152711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
153711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
154711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Record that we had nowhere to put the given type attribute.
155711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// We will diagnose such attributes later.
156711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void addIgnoredTypeAttr(AttributeList &attr) {
157711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      ignoredTypeAttrs.push_back(&attr);
158711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
159711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
160711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Diagnose all the ignored type attributes, given that the
161711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// declarator worked out to the given type.
162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void diagnoseIgnoredTypeAttrs(QualType type) const {
163711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (llvm::SmallVectorImpl<AttributeList*>::const_iterator
164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall           i != e; ++i) {
166711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        AttributeList &attr = **i;
167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        getSema().Diag(attr.getLoc(), diag::warn_function_attribute_wrong_type)
168711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          << attr.getName() << type;
169711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      }
170711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
172711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    ~TypeProcessingState() {
173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (trivial) return;
174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
175711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      restoreDeclSpecAttrs();
176711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
177711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
178711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  private:
179711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclSpec &getMutableDeclSpec() const {
180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return const_cast<DeclSpec&>(declarator.getDeclSpec());
181711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
182711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
183711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void restoreDeclSpecAttrs() {
184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(!savedAttrs.empty());
185711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
186711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
187711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs[i]->setNext(savedAttrs[i+1]);
188711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      savedAttrs.back()->setNext(0);
189711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
190711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
191711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
192711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// Basically std::pair except that we really want to avoid an
193711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// implicit operator= for safety concerns.  It's also a minor
194711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// link-time optimization for this to be a private type.
195711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  struct AttrAndList {
196711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The attribute.
197711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &first;
198711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
199711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The head of the list the attribute is currently in.
200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&second;
201711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
202711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttrAndList(AttributeList &attr, AttributeList *&head)
203711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : first(attr), second(head) {}
204711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
20504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
20604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
207711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace llvm {
208711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  template <> struct isPodLike<AttrAndList> {
209711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    static const bool value = true;
210711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
211711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
212711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
213711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
214711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  attr.setNext(head);
215711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  head = &attr;
216711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
217711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
218711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
219711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (head == &attr) {
220711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    head = attr.getNext();
221711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
22204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
223711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
224711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *cur = head;
225711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  while (true) {
226711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    assert(cur && cur->getNext() && "ran out of attrs?");
227711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (cur->getNext() == &attr) {
228711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      cur->setNext(attr.getNext());
229711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
230711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
231711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    cur = cur->getNext();
232711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
233711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
234711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
235711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void moveAttrFromListToList(AttributeList &attr,
236711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&fromList,
237711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&toList) {
238711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, fromList);
239711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrIntoList(attr, toList);
240711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
241711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
242711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state,
243711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             QualType &type, bool isDeclSpec,
244711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             AttributeList *attrs);
245711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
246711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
247711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
248711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type);
249711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
250711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
251711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr, QualType &type);
252711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
253711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCPointerTypeAttr(TypeProcessingState &state,
254711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr, QualType &type) {
255711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Right now, we have exactly one of these attributes: objc_gc.
256711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(attr.getKind() == AttributeList::AT_objc_gc);
257711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return handleObjCGCTypeAttr(state, attr, type);
258711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
259711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
260711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Given that an objc_gc attribute was written somewhere on a
261711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declaration *other* than on the declarator itself (for which, use
262711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
263711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// didn't apply in whatever position it was written in, try to move
264711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// it to a more appropriate position.
265711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeObjCPointerTypeAttr(TypeProcessingState &state,
266711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          AttributeList &attr,
267711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType type) {
268711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
269711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
270711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
271711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
272711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
273711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
274711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
275711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
276711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
277711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
278711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
279711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
280711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
281711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
282711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Don't walk through these.
283711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
284711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
285711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
286711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto error;
287711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
288711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
289711c52bb20d0c69063b52a99826fb7d2835501f1John McCall error:
290711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
291711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.getSema().Diag(attr.getLoc(), diag::warn_function_attribute_wrong_type)
292711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    << attr.getName() << type;
293711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
294711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
295711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Distribute an objc_gc type attribute that was written on the
296711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator.
297711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
298711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
299711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            AttributeList &attr,
300711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            QualType &declSpecType) {
301711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
302711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
303711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // objc_gc goes on the innermost pointer to something that's not a
304711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // pointer.
305711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  unsigned innermost = -1U;
306711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  bool considerDeclSpec = true;
307711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
308711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
309711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
310711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
311711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
312711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      innermost = i;
313ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      continue;
314711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
315711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
316711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
317711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
318711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
319711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
320711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
321711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
322711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      considerDeclSpec = false;
323711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto done;
324711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
325711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
326711c52bb20d0c69063b52a99826fb7d2835501f1John McCall done:
327711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
328711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // That might actually be the decl spec if we weren't blocked by
329711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // anything in the declarator.
330711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (considerDeclSpec) {
331711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (handleObjCPointerTypeAttr(state, attr, declSpecType))
332711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
333711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
334711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
335711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, if we found an appropriate chunk, splice the attribute
336711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // into it.
337711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (innermost != -1U) {
338711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, declarator.getAttrListRef(),
339711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                       declarator.getTypeObject(innermost).getAttrListRef());
340711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
341711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
342711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
343711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, diagnose when we're done building the type.
344711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
345711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
346711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
347711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
348711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written somewhere in a declaration
349711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// *other* than on the declarator itself or in the decl spec.  Given
350711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// that it didn't apply in whatever position it was written in, try
351711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// to move it to a more appropriate position.
352711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeFunctionTypeAttr(TypeProcessingState &state,
353711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
354711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType type) {
355711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
356711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
357711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to push the attribute from the return type of a function to
358711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the function itself.
359711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
360711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
361711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
362711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
363711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
364711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
365711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
366711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
367711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
368711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
369711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
370711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
371711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
372711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
373711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
374711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
375711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
376711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
377711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.getSema().Diag(attr.getLoc(), diag::warn_function_attribute_wrong_type)
378711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    << attr.getName() << type;
379711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
380711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
381711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Try to distribute a function type attribute to the innermost
382711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// function chunk or type.  Returns true if the attribute was
383711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributed, false if no location was found.
384711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool
385711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
386711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr,
387711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList *&attrList,
388711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      QualType &declSpecType) {
389711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
390711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
391711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Put it on the innermost function chunk, if there is one.
392711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
393711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
394711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (chunk.Kind != DeclaratorChunk::Function) continue;
395711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
396711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
397711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
398711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
399711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
400711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return handleFunctionTypeAttr(state, attr, declSpecType);
401711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
402711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
403711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written in the decl spec.  Try to
404711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
405711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
406711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
407711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
408711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType &declSpecType) {
409711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.saveDeclSpecAttrs();
410711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
411711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
412711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            state.getCurrentAttrListRef(),
414711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
415711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
416711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
417711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
418711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
419711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
420711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
421711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
422711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written on the declarator.  Try to
423711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
424711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
425711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
426711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         AttributeList &attr,
427711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         QualType &declSpecType) {
428711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
429711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
430711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
431711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
432711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declarator.getAttrListRef(),
433711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
434711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
435711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
436711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
437711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
438711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
439711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
440711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
441711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
442711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// \brief Given that there are attributes written on the declarator
443711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// itself, try to distribute any type attributes to the appropriate
444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator chunk.
445711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///
446711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// These are attributes like the following:
447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f ATTR;
448711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int (f ATTR)();
449711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// but not necessarily this:
450711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f() ATTR;
451711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
452711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                              QualType &declSpecType) {
453711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Collect all the type attributes from the declarator itself.
454711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
455711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *attr = state.getDeclarator().getAttributes();
456711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
457711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
458711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr->getNext();
459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
460711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr->getKind()) {
461711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
462711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
463711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
465711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
466711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
467711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
468711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
469711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    default:
470711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
471711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
472711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attr = next));
473711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
474711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
475711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Add a synthetic '()' to a block-literal declarator if it is
476711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// required, given the return type.
477711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void maybeSynthesizeBlockSignature(TypeProcessingState &state,
478711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType declSpecType) {
479711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
480711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // First, check whether the declarator would produce a function,
482711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // i.e. whether the innermost semantic chunk is a function.
483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.isFunctionDeclarator()) {
484711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // If so, make that declarator a prototyped declarator.
485711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.getFunctionTypeInfo().hasPrototype = true;
486711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
487711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
488711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
489da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // If there are any type objects, the type as written won't name a
490da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // function, regardless of the decl spec type.  This is because a
491da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // block signature declarator is always an abstract-declarator, and
492da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // abstract-declarators can't just be parentheses chunks.  Therefore
493da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // we need to build a function chunk unless there are no type
494da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // objects and the decl spec type is a function.
495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
497711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
498da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // Note that there *are* cases with invalid declarators where
499da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // declarators consist solely of parentheses.  In general, these
500da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // occur only in failed efforts to make function declarators, so
501da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // faking up the function chunk is still the right thing to do.
502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, we need to fake up a function declarator.
504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation loc = declarator.getSourceRange().getBegin();
505711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
506711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // ...and *prepend* it to the declarator.
507711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
508711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             ParsedAttributes(),
509711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*proto*/ true,
510711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*variadic*/ false, SourceLocation(),
511711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*args*/ 0, 0,
512711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*type quals*/ 0,
51383f51722ed2b8134810cb178f39e44da811de7cdDouglas Gregor                             /*ref-qualifier*/true, SourceLocation(),
514711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*EH*/ false, SourceLocation(), false, 0, 0, 0,
515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*parens*/ loc, loc,
516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             declarator));
517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // For consistency, make sure the state still has us as processing
519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the decl spec.
520711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
521711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.setCurrentChunkIndex(declarator.getNumTypeObjects());
52204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
52304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
524930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
525930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
5265db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// \param D  the declarator containing the declaration specifier.
5275153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
5285153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
529711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic QualType ConvertDeclSpecToType(Sema &S, TypeProcessingState &state) {
5305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
532711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
534711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const DeclSpec &DS = declarator.getDeclSpec();
535711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation DeclLoc = declarator.getIdentifierLoc();
5365db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
5375db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    DeclLoc = DS.getSourceRange().getBegin();
5381564e3906cad604a42bd131e584751a75589a9c4Chris Lattner
539711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  ASTContext &Context = S.Context;
5401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5415db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
5425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
54396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
54496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
54596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
5465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
5475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
548fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
5495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
550fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
5515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
5535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
554fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
5555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
556958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
55764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
55864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
55964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
56064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
561711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
562f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
56364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
56464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
56564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
56664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
567711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
568f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
56964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
57064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
57164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
572f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
573f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
574f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
575f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
576f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
577f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
578f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
579f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
580f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
581f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
582d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
58362f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
584097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
585c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
586c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         (ObjCProtocolDecl**)PQ,
587c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         DS.getNumProtocolQualifiers());
588c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectPointerType(Result);
58962f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
59062f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
5915db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
5925db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
5935db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
594711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isOmittedBlockReturnType(declarator)) {
5955db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      Result = Context.DependentTy;
5965db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
5975db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
5981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
599d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
600d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
601d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
602d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
603d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
604d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
605d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
606711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.getLangOptions().ImplicitInt) {
60735d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
60835d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
6093f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
610711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_declspec)
6113f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
612849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int");
6133f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
6144310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
615d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
616d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
617d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
618d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
6194310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
620711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (S.getLangOptions().CPlusPlus &&
621711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          !S.getLangOptions().Microsoft) {
622711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_missing_type_specifier)
6233f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
625b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
626b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
627b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
628711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
629b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
630711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_type_specifier)
6313f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
632b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
633d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
6363cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
6385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
639fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
640fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
641fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
642311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
643311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
644311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
645311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
646711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        if (!S.getLangOptions().C99 &&
647711c52bb20d0c69063b52a99826fb7d2835501f1John McCall            !S.getLangOptions().CPlusPlus0x)
648711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
649311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
6505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
6515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
6525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
653fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
654fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
655fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
656311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
657311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
658311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
659311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
660711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        if (!S.getLangOptions().C99 &&
661711c52bb20d0c69063b52a99826fb7d2835501f1John McCall            !S.getLangOptions().CPlusPlus0x)
662711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
663311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
6645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
6655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
666958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
6673cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
668fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
669958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
670958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
671fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
672958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
673fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
67439d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne
67539d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    if (S.getLangOptions().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
67639d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
67739d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      declarator.setInvalidType(true);
67839d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    }
679958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
680fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
6815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
6825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
6835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
684711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
6858f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
686711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
6878f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
68899dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
6895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
6905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
6915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
692b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
6936e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
6946e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
6956e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
696711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
6976e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
6986e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
6996e24726524c2b51b31bb4b622aa678a46b024f42John McCall
700a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
701711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
702a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
7035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
704a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
705a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
7065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
707a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
7082191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
7092191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    // In C++, make an ElaboratedType.
710711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.getLangOptions().CPlusPlus) {
711465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      ElaboratedTypeKeyword Keyword
712465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara        = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
713711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
7142191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    }
7155153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    if (D->isInvalidDecl())
716711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
717958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7191a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
7205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
7215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
7225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
723711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
72427940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    if (Result.isNull())
725711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
72627940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    else if (DeclSpec::ProtocolQualifierListTy PQ
72727940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall               = DS.getProtocolQualifiers()) {
728c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
729c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // Silently drop any existing protocol qualifiers.
730c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // TODO: determine whether that's the right thing to do.
731c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjT->getNumProtocols())
732c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = ObjT->getBaseType();
733c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
734c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (DS.getNumProtocolQualifiers())
735c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = Context.getObjCObjectType(Result,
736c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             (ObjCProtocolDecl**) PQ,
737c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             DS.getNumProtocolQualifiers());
738c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCIdType()) {
739ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
740c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
741c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
742c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
743c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
744c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCClassType()) {
7454262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
746c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
747c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
748c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
749c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
7503f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
751711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
7523f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
753711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
7543f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
755c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
7561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
758958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
760958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
761e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
762711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
763958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
764730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian    if (!Result->isDependentType())
765730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian      if (const TagType *TT = Result->getAs<TagType>())
766711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
767d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
768fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
769958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
770d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
771b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
772d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
773d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
774711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
7754b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
7764b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
777711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
7784b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
779958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
780d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
7816fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
782b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
7836fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
7846fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
785711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
786af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
787af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
788711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
789af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
7906fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
7916fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
792e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
793e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
79434b41d939a1328f484511c6002ba2456db879a29Richard Smith    Result = Context.getAutoType(QualType());
795e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
796e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
7971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
798809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
7995153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
800711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
8015153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
8025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
804958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
805f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
806711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.getLangOptions().Freestanding)
807711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
808fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
80982287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
81082287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
81182287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
812e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
813788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
814e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
815788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
816e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
817e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
818f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
8191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82047423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
82147423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
822711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
8231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
824711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
825711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
826711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
827711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
828711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
829711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
830711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
831711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
832711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (AttributeList *attrs = DS.getAttributes().getList())
833711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    processTypeAttrs(state, Result, true, attrs);
8341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
83596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
83696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
83796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
83896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
83996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
84096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
8410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (TypeQuals & DeclSpec::TQ_restrict) {
8422b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian      if (Result->isAnyPointerType() || Result->isReferenceType()) {
8432b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        QualType EltTy;
8442b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        if (Result->isObjCObjectPointerType())
8452b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result;
8462b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        else
8472b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result->isPointerType() ?
8482b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<PointerType>()->getPointeeType() :
8492b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<ReferenceType>()->getPointeeType();
8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
851bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
852bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
853bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
854711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getRestrictSpecLoc(),
855d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
856d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
8570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall          TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
858bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
859bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
860711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DS.getRestrictSpecLoc(),
861711c52bb20d0c69063b52a99826fb7d2835501f1John McCall               diag::err_typecheck_invalid_restrict_not_pointer)
862d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
8630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
86496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
86596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
8661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
86796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
86896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
86996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
87096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
87196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
87296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
8730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
87496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
8750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
87696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
8770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
8780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        assert((TypeQuals & DeclSpec::TQ_restrict) &&
8790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall               "Has CVR quals but not C, V, or R?");
8800953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Loc = DS.getRestrictSpecLoc();
88196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
882711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(Loc, diag::warn_typecheck_function_qualifiers)
883d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
88496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
8851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
886f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
887f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
888f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
889f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
890f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
8911a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
8921a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
893f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
8940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
8950953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
8961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
8971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
8990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Result = Context.getQualifiedType(Result, Quals);
90096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
9010953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
902f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
903f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
904f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
905cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
906cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
907cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
9081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
909cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
910cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
911cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
9122865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
9132865474261a608c7873b87ba4af110d17907896dJohn McCall                                  Qualifiers Qs) {
9142865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
9152865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
9162865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
9172865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
9182865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
9192865474261a608c7873b87ba4af110d17907896dJohn McCall
9202865474261a608c7873b87ba4af110d17907896dJohn McCall    const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
9212865474261a608c7873b87ba4af110d17907896dJohn McCall    if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
9222865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
9232865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9242865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
9252865474261a608c7873b87ba4af110d17907896dJohn McCall      }
9262865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
9272865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
9282865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9292865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
9302865474261a608c7873b87ba4af110d17907896dJohn McCall      }
9312865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
9322865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
9332865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9342865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
9352865474261a608c7873b87ba4af110d17907896dJohn McCall      }
9362865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (!Ty->isDependentType()) {
9372865474261a608c7873b87ba4af110d17907896dJohn McCall      // FIXME: this deserves a proper diagnostic
9382865474261a608c7873b87ba4af110d17907896dJohn McCall      DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9392865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
9402865474261a608c7873b87ba4af110d17907896dJohn McCall    }
9412865474261a608c7873b87ba4af110d17907896dJohn McCall
9422865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
9432865474261a608c7873b87ba4af110d17907896dJohn McCall      Diag(Loc, DiagID) << ProblemTy;
9442865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
9452865474261a608c7873b87ba4af110d17907896dJohn McCall    }
9462865474261a608c7873b87ba4af110d17907896dJohn McCall  }
9472865474261a608c7873b87ba4af110d17907896dJohn McCall
9482865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
9492865474261a608c7873b87ba4af110d17907896dJohn McCall}
9502865474261a608c7873b87ba4af110d17907896dJohn McCall
951075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
952075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
953075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
954075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
955075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
956cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
957cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
958cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
959cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
960cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
961cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
962cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
963cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
964cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
965cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
966cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
967cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
968cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
9692865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
970cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
971cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
972cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
973cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
974ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
975cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
976cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
977cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
978c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
97992e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
980cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
9812865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
982cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
983cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
984cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
985cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
986cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
987cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
988cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
989cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
990cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
991cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
992cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
993cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
994cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
995cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
996cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
99754e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
9982865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
99954e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
100069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // C++0x [dcl.ref]p6:
100169d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   If a typedef (7.1.3), a type template-parameter (14.3.1), or a
100269d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
100369d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   type T, an attempt to create the type "lvalue reference to cv TR" creates
100469d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   the type "lvalue reference to T", while an attempt to create the type
100569d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   "rvalue reference to cv TR" creates the type TR.
100654e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
100754e14c4db764c0636160d26c5bbf491637c83a76John McCall
100854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
100954e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
101054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
101154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
101254e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
101354e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
101454e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
101554e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
101654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
101754e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
101854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
101969d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // collapsing of references-to-references as described in C++0x.
102069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // DR 106 and 540 introduce reference-collapsing into C++98/03.
1021cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1022cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
102333a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
1024cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
1025cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
1026cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
1027cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1028cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1029cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1030cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
10317c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
10322865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
10332865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
1034cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1035cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1036cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
1037cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1038cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
1039cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1040cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
10411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
10421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
1043cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1044cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1045cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// array type or, if there is no such entity, the location of the
1046cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have array type.
1047cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1048cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
1049cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1050cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1051cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
1052cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
1053cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1054cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
10557e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
10560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10577e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
1058923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (getLangOptions().CPlusPlus) {
1059138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
1060138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
1061138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   type, the (possibly cv-qualified) type void, a function type or an
1062138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
1063138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
1064138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
1065138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
1066138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
1067138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
1068138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1069138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
1070138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1071923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (T->isVoidType()) {
1072923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1073923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1074923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1075138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1076138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (RequireNonAbstractType(Brackets.getBegin(), T,
1077138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
1078138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1079138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1080923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
1081138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1082138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
1083923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
1084923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
1085923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1086923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
1087cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1088cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
1089cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
1090ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1091cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1092cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
10931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
109434b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (T->getContainedAutoType()) {
109534b41d939a1328f484511c6002ba2456db879a29Richard Smith    Diag(Loc, diag::err_illegal_decl_array_of_auto)
109634b41d939a1328f484511c6002ba2456db879a29Richard Smith      << getPrintableNameForEntity(Entity) << T;
1097e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
1098e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
10991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11006217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
1101cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
1102cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
1103cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
1104cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
1105c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
1106c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1107c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
1108cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
11091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11105e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
11115e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  if (ArraySize && !ArraySize->isRValue())
11125e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall    DefaultLvalueConversion(ArraySize);
11135e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1114cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
11155e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // TODO: in theory, if we were insane, we could allow contextual
11165e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // conversions to integer type here.
1117cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (ArraySize && !ArraySize->isTypeDependent() &&
11181274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1119cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1120cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
1121cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1122cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
11232767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
1124cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
1125f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
11267e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
1127f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
1128f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
1129ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
11307e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
1131cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
1132923d56d436f750bc1f29db50e641078725558a1bSebastian Redl             (!T->isDependentType() && !T->isIncompleteType() &&
1133923d56d436f750bc1f29db50e641078725558a1bSebastian Redl              !T->isConstantSizeType())) {
1134cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Per C99, a variable array is an array with either a non-constant
1135cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // size or an element type that has a non-constant-size
11367e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1137cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
1138cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1139cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
1140923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
1141b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
1142b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1143b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1144b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
1145b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1146b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
1147923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1148923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1149923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
115002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
115102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
115202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      Diag(ArraySize->getLocStart(),
115302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
115402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
1155923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
11562767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
11572767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor               !T->isIncompleteType()) {
11582767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      // Is the array too large?
11592767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
11602767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
11612767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
11622767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
11632767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
11642767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
11651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
11662767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
116746a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
1168cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1169af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
1170af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  if (!getLangOptions().C99) {
11710fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
11720fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
1173204ce17e0cfd9bbe229627e1e5a20c3f2f587c8cDouglas Gregor      if (!T->isDependentType() &&
1174204ce17e0cfd9bbe229627e1e5a20c3f2f587c8cDouglas Gregor          !Context.getBaseElementType(T)->isPODType()) {
11750fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
11760fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor          << Context.getBaseElementType(T);
11770fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
11780fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      }
1179a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
1180a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
1181a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
1182a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
1183a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
11840fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
11850fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
11860fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::ext_vla);
11870fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
1188043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      Diag(Loc,
1189043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor           getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
1190043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor                                     : diag::ext_c99_array_usage);
1191cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1192cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1193cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
1194cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
11959cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
11969cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
11979cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
11989cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
11999ae2f076ca5ab1feb3ba95629099ec2319833701John McCallQualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
12009cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
12019cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
12029cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
12031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
12049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
12059cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
12069cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
12079cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
12089cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
12099ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
12109cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
12119ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
12129cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
12139ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        << "ext_vector_type" << ArraySize->getSourceRange();
12149cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
12159cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
12161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
12189cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
12191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12219cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
12229cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
12239ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      << ArraySize->getSourceRange();
12249cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
12259cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
12261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12279cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!T->isDependentType())
12289cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return Context.getExtVectorType(T, vectorSize);
12291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
12301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12319ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
12329cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
12331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1234724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
1235724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1236724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
1237724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
1238724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
12392943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
1240724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
1241724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1242724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
1243724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1244724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
1245724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
1246724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
1247724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1248724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
1249724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1250724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
1251724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1252724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
1253724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1254724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
1255724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
1256724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
1257724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1258724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
1259724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
1260724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1261724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
1262724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
1263724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
12641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 QualType *ParamTypes,
1265724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
1266724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 bool Variadic, unsigned Quals,
1267c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                                 RefQualifierKind RefQualifier,
1268fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
1269e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                 FunctionType::ExtInfo Info) {
1270724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
127158408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor    Diag(Loc, diag::err_func_returning_array_function)
127258408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
1273724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1274724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
12755291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
1276724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
1277724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
12782dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    QualType ParamType = adjustParameterType(ParamTypes[Idx]);
12792dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
1280724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
1281724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
1282724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
1283cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
128454e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
1285724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1286724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1287724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
1288724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1289724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1290e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
1291e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.Variadic = Variadic;
1292e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.TypeQuals = Quals;
1293c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor  EPI.RefQualifier = RefQualifier;
1294e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.ExtInfo = Info;
1295e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1296e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI);
1297724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
12981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1299949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
1300949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1301949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
1302949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
13030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR Qualifiers applied to the member pointer type
1304949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
1305949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
1306949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1307949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
1308949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
13091eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
13102865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
1311949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
1312949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
1313949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
1314949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
1315949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
1316949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1317949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
1318949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
1319949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1320949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
1321949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
1322949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
1323949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1324949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1325737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
1326949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
1327949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
13288d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
1329ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
1330949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1331949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1332949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1333949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
1334949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1335949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
1336949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1337949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1338949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1339949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
1340949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1341949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1342949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1343949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1344d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // In the Microsoft ABI, the class is allowed to be an incomplete
1345d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // type. In such cases, the compiler makes a worst-case assumption.
1346d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // We make no such assumption right now, so emit an error if the
1347d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // class isn't a complete type.
134820cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  if (Context.Target.getCXXABI() == CXXABI_Microsoft &&
1349d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis      RequireCompleteType(Loc, Class, diag::err_incomplete_type))
1350d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis    return QualType();
1351d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis
13522865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
1353949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
13541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13559a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
13569a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13579a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
13589a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
13609a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13619a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Loc The location of the entity whose type involves this
13629a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// block pointer type or, if there is no such entity, the location of the
13639a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type that will have block pointer type.
13649a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13659a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
13669a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
13679a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13689a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
13699a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
13702865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildBlockPointerType(QualType T,
13711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
13729a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
13730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
13749a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
13759a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
13769a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
13771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13782865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
13799a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
13809a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
1381b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1382b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
13833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
1384a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
13853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
13863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
13873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1388a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
1389f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
1390e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
1391a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
1392e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
13931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1394a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
1395e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
1396e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
1397e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
1398d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruthstatic void DiagnoseIgnoredQualifiers(unsigned Quals,
1399d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation ConstQualLoc,
1400d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation VolatileQualLoc,
1401d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation RestrictQualLoc,
1402d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      Sema& S) {
1403d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  std::string QualStr;
1404d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  unsigned NumQuals = 0;
1405d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  SourceLocation Loc;
1406d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1407d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint ConstFixIt;
1408d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint VolatileFixIt;
1409d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint RestrictFixIt;
1410d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1411d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to
1412d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // find a range and grow it to encompass all the qualifiers, regardless of
1413d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // the order in which they textually appear.
1414d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Const) {
1415d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc);
1416d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    Loc = ConstQualLoc;
1417d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ++NumQuals;
1418d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    QualStr = "const";
1419d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1420d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Volatile) {
1421d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc);
1422d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    if (NumQuals == 0) {
1423d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth      Loc = VolatileQualLoc;
1424d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth      QualStr = "volatile";
1425d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    } else {
1426d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth      QualStr += " volatile";
1427d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    }
1428d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ++NumQuals;
1429d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1430d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Restrict) {
1431d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc);
1432d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    if (NumQuals == 0) {
1433d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth      Loc = RestrictQualLoc;
1434d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth      QualStr = "restrict";
1435d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    } else {
1436d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth      QualStr += " restrict";
1437d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    }
1438d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ++NumQuals;
1439d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1440d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1441d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  assert(NumQuals > 0 && "No known qualifiers?");
1442d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1443d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  S.Diag(Loc, diag::warn_qual_return_type)
1444d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    << QualStr << NumQuals
1445d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    << ConstFixIt << VolatileFixIt << RestrictFixIt;
1446d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth}
1447d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
144898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
14498ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl/// declarator to Type instances.
1450402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor///
1451402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
1452402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// owns the declaration of a type (e.g., the definition of a struct
1453402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// type), then *OwnedDecl will receive the owned declaration.
1454bf1a028246d884a540aeafa38e89be59a269b072John McCall///
1455bf1a028246d884a540aeafa38e89be59a269b072John McCall/// The result of this call will never be null, but the associated
1456bf1a028246d884a540aeafa38e89be59a269b072John McCall/// type may be a null type if there's an unrecoverable error.
1457bf1a028246d884a540aeafa38e89be59a269b072John McCallTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
145834b41d939a1328f484511c6002ba2456db879a29Richard Smith                                           TagDecl **OwnedDecl,
145934b41d939a1328f484511c6002ba2456db879a29Richard Smith                                           bool AutoAllowedInTypeName) {
1460930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
1461930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
1462930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
146305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  TypeSourceInfo *ReturnTypeInfo = 0;
1464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1465711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  TypeProcessingState state(*this, D);
146604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
14673f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
14683f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
14693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
14700486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
14713f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
1472711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    T = ConvertDeclSpecToType(*this, state);
14735db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
1474591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
1475b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      TagDecl* Owned = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
1476b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      // Owned is embedded if it was defined here, or if it is the
1477b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      // very first (i.e., canonical) declaration of this tag type.
1478b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      Owned->setEmbeddedInDeclarator(Owned->isDefinition() ||
1479b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor                                     Owned->isCanonicalDecl());
1480591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor      if (OwnedDecl) *OwnedDecl = Owned;
1481591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
1482930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
1483930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
14843f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
14850efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
14863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
1487930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
148848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // "void" instead.
1489930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
1490930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
149148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
149248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
149348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
149448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
149505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    T = GetTypeFromParser(D.getName().ConversionFunctionId,
1496bf1a028246d884a540aeafa38e89be59a269b072John McCall                          &ReturnTypeInfo);
149748026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
1498930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
1499dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
1500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
1501711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
1502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1503e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith  // C++0x [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
15048110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // In C++0x, a function declarator using 'auto' must have a trailing return
15058110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // type (this is checked later) and we can skip this. In other languages
15068110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // using auto, we need to check regardless.
150734b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
15088110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith      (!getLangOptions().CPlusPlus0x || !D.isFunctionDeclarator())) {
1509baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
15101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1511baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
1512baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
1513baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      assert(0 && "K&R type lists aren't allowed in C++");
1514baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1515baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
1516baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
1517baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1518baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
1519baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      switch (cast<TagDecl>(CurContext)->getTagKind()) {
1520465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Enum: assert(0 && "unhandled tag kind"); break;
1521465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
1522465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
1523465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
15241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
1525baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1526baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
1527baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 4; // Exception declaration
1528baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1529baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
1530baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 5; // Template parameter
1531baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1532baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
153334b41d939a1328f484511c6002ba2456db879a29Richard Smith      Error = 6; // Block literal
153434b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
153534b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TemplateTypeArgContext:
153634b41d939a1328f484511c6002ba2456db879a29Richard Smith      Error = 7; // Template type argument
153734b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
153834b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TypeNameContext:
153934b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!AutoAllowedInTypeName)
15408110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith        Error = 10; // Generic
1541baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1542baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
1543baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
1544baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
1545baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
1546baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1547baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1548baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
1549ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith    if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1550ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith      Error = 8;
1551ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith
15528110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    // In Objective-C it is an error to use 'auto' on a function declarator.
15538110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    if (D.isFunctionDeclarator())
15548110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith      Error = 9;
15558110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith
1556e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // C++0x [dcl.spec.auto]p2: 'auto' is always fine if the declarator
1557e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // contains a trailing return type. That is only legal at the outermost
1558e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // level. Check all declarator chunks (outermost first) anyway, to give
1559e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // better diagnostics.
15608110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    if (getLangOptions().CPlusPlus0x && Error != -1) {
1561e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1562e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned chunkIndex = e - i - 1;
1563e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        state.setCurrentChunkIndex(chunkIndex);
1564e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
1565e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (DeclType.Kind == DeclaratorChunk::Function) {
1566e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1567e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (FTI.TrailingReturnType) {
1568e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Error = -1;
1569e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            break;
1570e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          }
1571e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        }
1572e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
1573e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    }
1574e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
1575baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
1576baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
1577baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
1578baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      T = Context.IntTy;
1579baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
1580baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1581baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
1582e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
158334b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (T.isNull())
158434b41d939a1328f484511c6002ba2456db879a29Richard Smith    return Context.getNullTypeSourceInfo();
158534b41d939a1328f484511c6002ba2456db879a29Richard Smith
1586cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
1587cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
1588cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
1589cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
15901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
159198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
159298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
159398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
15948ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1595711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
1596711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
1597711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
15985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
1600075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
1601075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      T = BuildParenType(T);
1602075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
16035618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
16049af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
16059af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
16069af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner        Diag(DeclType.Loc, diag::err_blocks_disable);
16071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16082865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
16092865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
16102865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
16115618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
16125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
16136a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
16146a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
16156a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
16166a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
16176a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
16186a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
16196a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
1620c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) {
1621c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
16222865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
16232865474261a608c7873b87ba4af110d17907896dJohn McCall          T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
162414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
162514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
16262865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildPointerType(T, DeclType.Loc, Name);
16272865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
16282865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
1629711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
16305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
16310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
16326a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
16336a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
16346a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
16356a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
16366a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
16376a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
16386a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
16392865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
16402865474261a608c7873b87ba4af110d17907896dJohn McCall
16412865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
16422865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
16432865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
16445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
16450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
16465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
16476a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
16486a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
16496a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
16506a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
16516a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
16526a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
16536a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
1654fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
165594f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
16565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
16575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
16585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
16595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
16605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
16615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
16625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
1663f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      if (ASM == ArrayType::Star &&
1664f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman          D.getContext() != Declarator::PrototypeContext) {
1665f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
1666f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
1667f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1668f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1669f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
1670f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
1671f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
16720953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      T = BuildArrayType(T, ASM, ArraySize,
16730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                         Qualifiers::fromCVRMask(ATI.TypeQuals),
16747e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                         SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
16755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
16765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1677f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
16785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
16795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
16805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
16815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
16823cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
168334b41d939a1328f484511c6002ba2456db879a29Richard Smith      // Check for auto functions and trailing return type and adjust the
168434b41d939a1328f484511c6002ba2456db879a29Richard Smith      // return type accordingly.
168534b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!D.isInvalidType()) {
168634b41d939a1328f484511c6002ba2456db879a29Richard Smith        // trailing-return-type is only required if we're declaring a function,
168734b41d939a1328f484511c6002ba2456db879a29Richard Smith        // and not, for instance, a pointer to a function.
168834b41d939a1328f484511c6002ba2456db879a29Richard Smith        if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
168934b41d939a1328f484511c6002ba2456db879a29Richard Smith            !FTI.TrailingReturnType && chunkIndex == 0) {
169034b41d939a1328f484511c6002ba2456db879a29Richard Smith          Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
169134b41d939a1328f484511c6002ba2456db879a29Richard Smith               diag::err_auto_missing_trailing_return);
169234b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = Context.IntTy;
169334b41d939a1328f484511c6002ba2456db879a29Richard Smith          D.setInvalidType(true);
169434b41d939a1328f484511c6002ba2456db879a29Richard Smith        } else if (FTI.TrailingReturnType) {
1695e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          // T must be exactly 'auto' at this point. See CWG issue 681.
1696e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (isa<ParenType>(T)) {
1697e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1698e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith                 diag::err_trailing_return_in_parens)
1699e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith              << T << D.getDeclSpec().getSourceRange();
1700e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.setInvalidType(true);
1701e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          } else if (T.hasQualifiers() || !isa<AutoType>(T)) {
170234b41d939a1328f484511c6002ba2456db879a29Richard Smith            Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
170334b41d939a1328f484511c6002ba2456db879a29Richard Smith                 diag::err_trailing_return_without_auto)
170434b41d939a1328f484511c6002ba2456db879a29Richard Smith              << T << D.getDeclSpec().getSourceRange();
170534b41d939a1328f484511c6002ba2456db879a29Richard Smith            D.setInvalidType(true);
170634b41d939a1328f484511c6002ba2456db879a29Richard Smith          }
170734b41d939a1328f484511c6002ba2456db879a29Richard Smith
170834b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = GetTypeFromParser(
170934b41d939a1328f484511c6002ba2456db879a29Richard Smith            ParsedType::getFromOpaquePtr(FTI.TrailingReturnType),
171034b41d939a1328f484511c6002ba2456db879a29Richard Smith            &ReturnTypeInfo);
171134b41d939a1328f484511c6002ba2456db879a29Richard Smith        }
171234b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
171334b41d939a1328f484511c6002ba2456db879a29Richard Smith
1714e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // C99 6.7.5.3p1: The return type may not be a function or array type.
1715e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // For conversion functions, we'll diagnose this particular error later.
1716e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      if ((T->isArrayType() || T->isFunctionType()) &&
1717e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
1718e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned diagID = diag::err_func_returning_array_function;
1719e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // Last processing chunk in block context means this function chunk
1720e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // represents the block.
1721e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (chunkIndex == 0 &&
1722e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.getContext() == Declarator::BlockLiteralContext)
1723e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          diagID = diag::err_block_returning_array_function;
1724e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
1725e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        T = Context.IntTy;
1726e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        D.setInvalidType(true);
1727e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
1728e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
17295291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
17305291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
1731fff951371dfc309160a99d423e43a7841aeb35aaDouglas Gregor      if (isa<PointerType>(T) && T.getLocalCVRQualifiers() &&
1732d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth          (!getLangOptions().CPlusPlus || !T->isDependentType())) {
1733d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?");
1734d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
1735d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer);
1736d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1737d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr;
1738d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1739d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DiagnoseIgnoredQualifiers(PTI.TypeQuals,
1740d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
1741d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
1742d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
1743d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            *this);
1744d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1745d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth      } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
17465291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          (!getLangOptions().CPlusPlus ||
17475291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor           (!T->isDependentType() && !T->isRecordType()))) {
1748d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1749d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(),
1750d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getConstSpecLoc(),
1751d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getVolatileSpecLoc(),
1752d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getRestrictSpecLoc(),
1753d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  *this);
17545291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      }
1755d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1756402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1757402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
1758402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
1759b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
1760402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        if (Tag->isDefinition())
1761402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor          Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1762402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
1763402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
1764402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
17653cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
17663cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
17673cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      if (FTI.hasExceptionSpec &&
17683cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl          D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
17693cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl        Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
17703cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
17712865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) {
17722865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
17732865474261a608c7873b87ba4af110d17907896dJohn McCall        T = Context.getFunctionNoProtoType(T);
17742865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
17752865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
17762865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
17772865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
17782865474261a608c7873b87ba4af110d17907896dJohn McCall        if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) {
1779965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
1780965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
1781965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
1782965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
1783965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
1784965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
1785965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
1786965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
1787965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
1788965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
1789965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1790c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
17912865474261a608c7873b87ba4af110d17907896dJohn McCall
17922865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
1793788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
1794788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
17952865474261a608c7873b87ba4af110d17907896dJohn McCall          Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
17962865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
17972865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
17982865474261a608c7873b87ba4af110d17907896dJohn McCall        }
17992865474261a608c7873b87ba4af110d17907896dJohn McCall
1800e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
1801e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
1802e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
1803c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
1804c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : FTI.RefQualifierIsLValueRef? RQ_LValue
1805c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : RQ_RValue;
1806c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
18075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
18085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
18095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
18102865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
18111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1813d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
18148123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
181578c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
18162dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
18172dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
1818beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor          assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
18192dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
18205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
18215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
182272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
18232dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
18245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
18255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
18265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
18275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
18285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
18292ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
18308123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
18312ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
18322ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
18335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
18344565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
18352ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
18368123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
18372ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
18382ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
18390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
18402ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
18411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18422ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
18432ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
18442ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
1845eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
1846eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
1847a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
1848183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
1849eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
1850eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
1851eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
18525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
185356a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
185454e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
18555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
1856465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1857465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        llvm::SmallVector<QualType, 4> Exceptions;
1858e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        if (FTI.hasExceptionSpec) {
1859e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          EPI.HasExceptionSpec = FTI.hasExceptionSpec;
1860e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          EPI.HasAnyExceptionSpec = FTI.hasAnyExceptionSpec;
1861e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          Exceptions.reserve(FTI.NumExceptions);
1862e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1863e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // FIXME: Preserve type source info.
1864e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1865e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // Check that the type is valid for an exception spec, and
1866e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // drop it if not.
1867e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1868e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall              Exceptions.push_back(ET);
1869e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
1870373920bd733b1d28fe7bf209945a62eb9248d948John McCall          EPI.NumExceptions = Exceptions.size();
1871e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          EPI.Exceptions = Exceptions.data();
1872ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl        }
1873465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1874e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI);
18755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
187604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
18775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
18785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1879f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
1880f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
18817bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
1882f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
18837bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
1884edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
1885edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
18867bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      } else if (isDependentScopeSpecifier(SS) ||
18877bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara                 dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS))) {
18881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
18897bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
189087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
189187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
189287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
18937bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
18944a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
189587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
189687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
189787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
189814aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor        case NestedNameSpecifier::NamespaceAlias:
189987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
19009f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
190187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
19027bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
190387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
190487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
190587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
19067bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // Note: if NNS is dependent, then its prefix (if any) is already
19077bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // included in ClsType; this does not hold if the NNS is
19087bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // nondependent: in this case (if there is indeed a prefix)
19097bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // ClsType needs to be wrapped into an elaborated type.
19107bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          if (NNSPrefix && !NNS->isDependent())
19117bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
191287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
191387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
1914f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
1915949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        Diag(DeclType.Mem.Scope().getBeginLoc(),
1916949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
1917949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1918949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
1919f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
1920f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1921f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1922949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
19232865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
1924949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
1925f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
1926949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
19272865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
19282865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
1929f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1930f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
1931f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
1932f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1933cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
1934cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
1935cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
1936cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
1937cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1938c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
1939711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
1940711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      processTypeAttrs(state, T, false, attrs);
19415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1942971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1943971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
1944183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
1945778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
1946971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1947708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    // C++ 8.3.5p4:
1948708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
1949708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
1950708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
1951708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
1952683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    //
1953683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // Core issue 547 also allows cv-qualifiers on function types that are
1954683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // top-level template type arguments.
1955613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
1956613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
1957613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (D.getContext() != Declarator::MemberContext ||
1958613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall                      D.getDeclSpec().isFriendSpecified());
1959613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
1960613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      DeclContext *DC = computeDeclContext(D.getCXXScopeSpec());
1961613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
1962613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
1963613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
1964c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    // C++0x [dcl.fct]p6:
1965c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   A ref-qualifier shall only be part of the function type for a
1966c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   non-static member function, the function type to which a pointer to
1967c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   member refers, or the top-level function type of a function typedef
1968c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   declaration.
1969c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    if ((FnTy->getTypeQuals() != 0 || FnTy->getRefQualifier()) &&
1970683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        !(D.getContext() == Declarator::TemplateTypeArgContext &&
1971683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          !D.isFunctionDeclarator()) &&
1972971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
1973c61bb2056148891375bfa591fa2859b9b6ec2734Sebastian Redl        (FreeFunction ||
1974971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
1975683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      if (D.getContext() == Declarator::TemplateTypeArgContext) {
1976683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        // Accept qualified function types as template type arguments as a GNU
1977683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        // extension. This is also the subject of C++ core issue 547.
1978683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        std::string Quals;
1979683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        if (FnTy->getTypeQuals() != 0)
1980683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          Quals = Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
1981683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
1982683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        switch (FnTy->getRefQualifier()) {
1983683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        case RQ_None:
1984683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          break;
1985683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
1986683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        case RQ_LValue:
1987683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (!Quals.empty())
1988683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Quals += ' ';
1989683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          Quals += '&';
1990683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          break;
1991c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
1992683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        case RQ_RValue:
1993683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (!Quals.empty())
1994683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Quals += ' ';
1995683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          Quals += "&&";
1996683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          break;
1997683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
1998683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
1999683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        Diag(D.getIdentifierLoc(),
2000683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor             diag::ext_qualified_function_type_template_arg)
2001683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          << Quals;
2002683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      } else {
2003683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        if (FnTy->getTypeQuals() != 0) {
2004683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (D.isFunctionDeclarator())
2005683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Diag(D.getIdentifierLoc(),
2006683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                 diag::err_invalid_qualified_function_type);
2007683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          else
2008683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Diag(D.getIdentifierLoc(),
2009683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                 diag::err_invalid_qualified_typedef_function_type_use)
2010683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << FreeFunction;
2011683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
2012683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
2013683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        if (FnTy->getRefQualifier()) {
2014683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (D.isFunctionDeclarator()) {
2015683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            SourceLocation Loc = D.getIdentifierLoc();
2016683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
2017683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              const DeclaratorChunk &Chunk = D.getTypeObject(N-I-1);
2018683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              if (Chunk.Kind == DeclaratorChunk::Function &&
2019683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                  Chunk.Fun.hasRefQualifier()) {
2020683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                Loc = Chunk.Fun.getRefQualifierLoc();
2021683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                break;
2022683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              }
2023944aa60777e6ea1015c9423107f7925f6d91f4a0Douglas Gregor            }
2024944aa60777e6ea1015c9423107f7925f6d91f4a0Douglas Gregor
2025683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Diag(Loc, diag::err_invalid_ref_qualifier_function_type)
2026683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << (FnTy->getRefQualifier() == RQ_LValue)
2027683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << FixItHint::CreateRemoval(Loc);
2028683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          } else {
2029683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Diag(D.getIdentifierLoc(),
2030683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                 diag::err_invalid_ref_qualifier_typedef_function_type_use)
2031683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << FreeFunction
2032683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << (FnTy->getRefQualifier() == RQ_LValue);
2033683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          }
2034c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        }
2035c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
2036683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        // Strip the cv-qualifiers and ref-qualifiers from the type.
2037683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
2038683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        EPI.TypeQuals = 0;
2039683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        EPI.RefQualifier = RQ_None;
2040c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
2041683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        T = Context.getFunctionType(FnTy->getResultType(),
2042683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                                    FnTy->arg_type_begin(),
2043683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                                    FnTy->getNumArgs(), EPI);
2044683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      }
2045971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
2046971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
20471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2048711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
2049711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull())
2050711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = D.getAttributes())
2051711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      processTypeAttrs(state, T, false, attrs);
2052711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2053711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
2054711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
2055711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2056737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // If there's a constexpr specifier, treat it as a top-level const.
2057737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  if (D.getDeclSpec().isConstexprSpecified()) {
2058737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
2059737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
2060737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
2061a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // If there was an ellipsis in the declarator, the declaration declares a
2062a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
2063a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (D.hasEllipsis() && !T.isNull()) {
2064a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
2065a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   A declarator-id or abstract-declarator containing an ellipsis shall
2066a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
2067a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
2068a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
2069a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
2070a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
2071a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   [...] When it is part of a parameter-declaration-clause, the
2072a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   parameter pack is a function parameter pack (14.5.3). The type T
2073a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
2074a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   a template parameter pack; each template parameter pack in T is
2075a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
2076a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2077a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
2078a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
2079a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
2080a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        Diag(D.getEllipsisLoc(),
2081a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
2082a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
2083a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
2084a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
2085cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
2086a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
2087a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2088a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
2089a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
2090a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
2091a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   If a template-parameter is a [...] is a parameter-declaration that
2092a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
2093a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
2094a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2095a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
2096a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
2097a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
2098a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
2099cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
210010738d36b150aa65206890c1c845cdba076e4200Douglas Gregor      else if (!getLangOptions().CPlusPlus0x)
21015ce5f5221217b64193799c2a4d5aa84432d3fba4Douglas Gregor        Diag(D.getEllipsisLoc(), diag::ext_variadic_templates);
2102a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2103a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
2104a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
2105a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
2106a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
2107a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
2108a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
2109a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
2110a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
2111a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
2112a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
2113683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    case Declarator::TemplateTypeArgContext:
2114a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
2115a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
2116a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
2117a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
2118a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2119a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
2120a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
2121e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2122bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
2123bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
2124bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
2125bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
2126bf1a028246d884a540aeafa38e89be59a269b072John McCall  return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo);
21275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
212951bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
213051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
2131c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    ASTContext &Context;
213251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
2133f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
213451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
2135c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
2136c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      : Context(Context), DS(DS) {}
2137f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
213851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
213951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
214051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
214151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
214251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
214351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
214451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
214551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
2146c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
2147c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2148c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
2149c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
2150c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
2151c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.getBaseLoc().initialize(Context, SourceLocation());
2152c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
2153c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
2154c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
2155c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
215654e14c4db764c0636160d26c5bbf491637c83a76John McCall
2157c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
215854e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
215954e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
216054e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
216154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
216254e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
216354e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
216454e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
216554e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
216654e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
216754e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
216854e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
216954e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
217051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
217154e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
217254e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
2173c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
217451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
2175833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
2176a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
2177b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2178833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2179833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
2180833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
2181a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
2182c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.initialize(Context, DS.getTypeSpecTypeLoc());
2183833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
2184833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
2185833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2186e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
2187e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
2188e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
2189e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TemplateSpecializationTypeLoc NamedTL =
2190e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
2191e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
2192e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2193e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      else
2194e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
2195833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
2196cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
2197cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
2198cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2199cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
2200cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
2201cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
2202cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
2203cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2204cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
2205b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
2206cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
2207b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2208cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
2209cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
2210ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
2211ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
2212ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
2213ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
2214ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
2215ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
2216ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
2217ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
2218ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
2219ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
2220ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
2221ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
2222ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
2223ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
2224ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
2225e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
2226e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
2227e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
2228253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
2229e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
2230b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2231e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
2232e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
2233e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
2234e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
2235e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2236e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setKeywordLoc(Keyword != ETK_None
2237e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       ? DS.getTypeSpecTypeLoc()
2238e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       : SourceLocation());
2239e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
22409e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
2241e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
2242e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
2243e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
2244e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
2245e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
2246253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
2247e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
2248b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2249e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
2250e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
2251e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
2252e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
2253e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2254e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setKeywordLoc(Keyword != ETK_None
2255e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       ? DS.getTypeSpecTypeLoc()
2256e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       : SourceLocation());
2257e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
22582494dd024b392b8def58bf067cc94b51c214cf77Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
225933500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
226033500955d731c73717af52088b7fc0e7a85681e7John McCall    }
226133500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
226233500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
226333500955d731c73717af52088b7fc0e7a85681e7John McCall      ElaboratedTypeKeyword Keyword
226433500955d731c73717af52088b7fc0e7a85681e7John McCall        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
226533500955d731c73717af52088b7fc0e7a85681e7John McCall      if (Keyword == ETK_Typename) {
226633500955d731c73717af52088b7fc0e7a85681e7John McCall        TypeSourceInfo *TInfo = 0;
2267b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
226833500955d731c73717af52088b7fc0e7a85681e7John McCall        if (TInfo) {
226933500955d731c73717af52088b7fc0e7a85681e7John McCall          TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
227033500955d731c73717af52088b7fc0e7a85681e7John McCall                    TInfo->getTypeLoc()));
227133500955d731c73717af52088b7fc0e7a85681e7John McCall          return;
227233500955d731c73717af52088b7fc0e7a85681e7John McCall        }
227333500955d731c73717af52088b7fc0e7a85681e7John McCall      }
2274c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initializeLocal(Context, SourceLocation());
227533500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setKeywordLoc(Keyword != ETK_None
227633500955d731c73717af52088b7fc0e7a85681e7John McCall                       ? DS.getTypeSpecTypeLoc()
227733500955d731c73717af52088b7fc0e7a85681e7John McCall                       : SourceLocation());
227833500955d731c73717af52088b7fc0e7a85681e7John McCall      const CXXScopeSpec& SS = DS.getTypeSpecScope();
227933500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
228033500955d731c73717af52088b7fc0e7a85681e7John McCall      // FIXME: load appropriate source location.
2281e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeLoc());
2282e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
2283e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
228451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
228551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
2286c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initialize(Context, DS.getTypeSpecTypeLoc());
228751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
228851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
2289eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
229051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
229151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
2292f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
229351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
229451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
22954adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
229651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
22979f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
229851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
22994adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
230051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
230151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
230251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
23034adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
230451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
230551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
230651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
23074adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
230851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
230951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
231051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
23114adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
231251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
231351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
231451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
231551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: nested name specifier
23164adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
231751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
231851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
231954e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
232054e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
232151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
232251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
232351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
232451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
232551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
232651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
232751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
232851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
232951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
233051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
233151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
233251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
233351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
233451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
233551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
233651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLParenLoc(Chunk.Loc);
233751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRParenLoc(Chunk.EndLoc);
2338dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor      TL.setTrailingReturn(!!Chunk.Fun.TrailingReturnType);
233951bd803fbdade51d674598ed45da3d54190a656cJohn McCall
234051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
234154e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
2342d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
234354e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
23444adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
234551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
23464adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
2347075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
2348075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
2349075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
2350075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
2351075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
23521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
235351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
23549f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
23554adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
235651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
235751bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
23584adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
2359a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
236051bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
236151bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
236205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
236305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
236405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
236505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
236605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
2367a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
236805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
236905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
2370a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
2371a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
237251bd803fbdade51d674598ed45da3d54190a656cJohn McCall
2373a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
2374a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
2375a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc());
2376a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
2377a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
2378a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
23798ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
238051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
238151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
23824adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
2383f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
2384b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
2385b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
2386b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
238705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
238805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
238905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
2390b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
2391c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
239205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
239305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor
2394a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
23954adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
23964adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
2397a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
2398b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
23991bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
24001bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
24011bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
2402eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
2403eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
2404a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
24051bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
24061bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
2407b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
24081bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
24091bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
24101bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
24111bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
241235d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis  assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
241335d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
241435d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
24151bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
24161bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
2417f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
24185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
24195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
24205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
24211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2422402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  TagDecl *OwnedTag = 0;
2423bf1a028246d884a540aeafa38e89be59a269b072John McCall  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag);
2424bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
24255153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
2426809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
24275912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
2428402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  if (getLangOptions().CPlusPlus) {
2429402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
24306d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
24316d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
2432402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // C++0x [dcl.type]p3:
2433402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   A type-specifier-seq shall not define a class or enumeration
2434402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   unless it appears in the type-id of an alias-declaration
2435402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   (7.1.3).
2436402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    if (OwnedTag && OwnedTag->isDefinition())
2437402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
2438402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        << Context.getTypeDeclType(OwnedTag);
2439402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
2440402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
2441b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
24425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2444c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
2445c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
2446c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
2447c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
2448c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
2449232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
2450232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
2451c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
2452c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
24531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
2454c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
24550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
2456232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
2457232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
2458232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
2459232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
2460c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
2461e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2462c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
2463232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
24641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2465232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
2466545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
2467f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2468e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2469c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
2470232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
2471545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
2472232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
2473ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
2474ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
2475dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
2476dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
2477e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2478c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
2479232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
2480232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
2481efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
2482efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
2483efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
2484efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
2485efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
2486e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
2487efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
2488efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
2489efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
2490efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
2491efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
24920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
2493efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
2494efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
24950953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
2496e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2497efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
2498efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
2499efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
25001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
2501f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
2502c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
2503c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
2504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
2505711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
2506711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
2507711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
2508711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
2509711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
2510711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
2511711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
2512711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2513711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
2514711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
2515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
2516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
2517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
2518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
2520711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
2521711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2523d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
25241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2525d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
2526711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!attr.getParameterName()) {
2527711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2528ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
2529711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2531ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
25320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
2533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getNumArgs() != 0) {
2534711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2535711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2536711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2537d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
2538711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getParameterName()->isStr("weak"))
25390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
2540711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  else if (attr.getParameterName()->isStr("strong"))
25410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
2542d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
2543711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
2544711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      << "objc_gc" << attr.getParameterName();
2545711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2546711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2547d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
25481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2549711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  type = S.Context.getObjCGCQualType(type, GCAttr);
2550711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
2551d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
2552d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
2553e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallnamespace {
2554e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// A helper class to unwrap a type down to a function for the
2555e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// purposes of applying attributes there.
2556e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///
2557e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// Use:
2558e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
2559e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   if (unwrapped.isFunctionType()) {
2560e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     const FunctionType *fn = unwrapped.get();
2561e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     // change fn somehow
2562e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     T = unwrapped.wrap(fn);
2563e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   }
2564e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  struct FunctionTypeUnwrapper {
2565e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    enum WrapKind {
2566e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Desugar,
2567e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Parens,
2568e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Pointer,
2569e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      BlockPointer,
2570e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Reference,
2571e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      MemberPointer
2572e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    };
2573e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2574e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType Original;
2575e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *Fn;
2576e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    llvm::SmallVector<unsigned char /*WrapKind*/, 8> Stack;
2577e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2578e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
2579e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      while (true) {
2580e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const Type *Ty = T.getTypePtr();
2581e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<FunctionType>(Ty)) {
2582e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Fn = cast<FunctionType>(Ty);
2583e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return;
2584e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ParenType>(Ty)) {
2585e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ParenType>(Ty)->getInnerType();
2586e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Parens);
2587e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<PointerType>(Ty)) {
2588e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<PointerType>(Ty)->getPointeeType();
2589e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Pointer);
2590e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<BlockPointerType>(Ty)) {
2591e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<BlockPointerType>(Ty)->getPointeeType();
2592e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(BlockPointer);
2593e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<MemberPointerType>(Ty)) {
2594e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<MemberPointerType>(Ty)->getPointeeType();
2595e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(MemberPointer);
2596e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ReferenceType>(Ty)) {
2597e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ReferenceType>(Ty)->getPointeeType();
2598e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Reference);
2599e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else {
2600e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          const Type *DTy = Ty->getUnqualifiedDesugaredType();
2601e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          if (Ty == DTy) {
2602e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            Fn = 0;
2603e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            return;
2604e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          }
2605e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2606e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = QualType(DTy, 0);
2607e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Desugar);
2608e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        }
2609e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2610e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2611e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2612e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    bool isFunctionType() const { return (Fn != 0); }
2613e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *get() const { return Fn; }
2614e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2615e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(Sema &S, const FunctionType *New) {
2616e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // If T wasn't modified from the unwrapped type, do nothing.
2617e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (New == get()) return Original;
2618e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2619e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Fn = New;
2620e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return wrap(S.Context, Original, 0);
2621e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2622e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2623e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  private:
2624e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
2625e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size())
2626e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getQualifiedType(Fn, Old.getQualifiers());
2627e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2628e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // Build up the inner type, applying the qualifiers from the old
2629e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // type to the new type.
2630e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      SplitQualType SplitOld = Old.split();
2631e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2632e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // As a special case, tail-recurse if there are no qualifiers.
2633e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (SplitOld.second.empty())
2634e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, SplitOld.first, I);
2635e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return C.getQualifiedType(wrap(C, SplitOld.first, I), SplitOld.second);
2636e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2637e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2638e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
2639e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size()) return QualType(Fn, 0);
2640e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2641e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      switch (static_cast<WrapKind>(Stack[I++])) {
2642e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Desugar:
2643e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // This is the point at which we potentially lose source
2644e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // information.
2645e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
2646e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2647e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Parens: {
2648e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
2649e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getParenType(New);
2650e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2651e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2652e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Pointer: {
2653e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
2654e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getPointerType(New);
2655e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2656e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2657e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case BlockPointer: {
2658e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
2659e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getBlockPointerType(New);
2660e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2661e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2662e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case MemberPointer: {
2663e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
2664e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldMPT->getPointeeType(), I);
2665e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getMemberPointerType(New, OldMPT->getClass());
2666e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2667e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2668e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Reference: {
2669e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const ReferenceType *OldRef = cast<ReferenceType>(Old);
2670e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldRef->getPointeeType(), I);
2671e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<LValueReferenceType>(OldRef))
2672e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
2673e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        else
2674e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getRValueReferenceType(New);
2675e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2676e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2677e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2678e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      llvm_unreachable("unknown wrapping kind");
2679e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return QualType();
2680e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2681e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  };
2682e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
2683e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2684711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
2685711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
2686711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
2687711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
2688711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
2689711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
2690e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2691711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
26922455636163fdd18581d7fdae816433f886d88213Mike Stump
2693711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getKind() == AttributeList::AT_noreturn) {
2694711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
269504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
2696e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2697e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
2698711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
2699711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
2700425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
2701425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
2702711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
2703711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2704711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2705711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
2706425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
2707711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getKind() == AttributeList::AT_regparm) {
2708711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
2709711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
2710711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
27111e030eb1194763b42c1752723be23b1515f48981John McCall
2712711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
2713711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
2714008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
27151e030eb1194763b42c1752723be23b1515f48981John McCall
2716ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Diagnose regparm with fastcall.
2717ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    const FunctionType *fn = unwrapped.get();
2718ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    CallingConv CC = fn->getCallConv();
2719ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (CC == CC_X86FastCall) {
2720ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
2721ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC)
2722ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm";
2723ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
2724ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
2725ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
2726ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
2727e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionType::ExtInfo EI =
2728711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
2729711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2730711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2731425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
2732425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
273304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
2734711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
2735711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
2736711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2737f82b4e85b1219295cad4b5851b035575bc293010John McCall
273804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Delay if the type didn't work out to a function.
2739711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!unwrapped.isFunctionType()) return false;
274004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2741711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
2742711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
2743064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis  if (S.Context.getCanonicalCallConv(CC) ==
2744e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      S.Context.getCanonicalCallConv(CCOld)) {
2745ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
2746ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2747711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2748e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  }
274904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
275004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CCOld != CC_Default) {
275104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Should we diagnose reapplications of the same convention?
2752711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
275304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CC)
275404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CCOld);
2755711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2756711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
275704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
275804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
275904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
276004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
2761711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isa<FunctionNoProtoType>(fn)) {
2762711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_knr)
276304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
2764711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
2765711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
276604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
276704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2768711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
276904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
2770711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
277104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
2772711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
2773711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
277404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
2775ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
2776ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Also diagnose fastcall with regparm.
2777ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (fn->getRegParmType()) {
2778ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
2779ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm"
2780ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC);
2781ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
2782ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
2783ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
278404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
2785f82b4e85b1219295cad4b5851b035575bc293010John McCall
2786711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
2787711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2788711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
2789f82b4e85b1219295cad4b5851b035575bc293010John McCall}
2790f82b4e85b1219295cad4b5851b035575bc293010John McCall
27916e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
27926e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
27936e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
27946e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
27956e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
27966e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
27976e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
2798788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
2799788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
280056affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
28016e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
28026e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2803e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
28046e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
28056e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
28066e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
28076e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
2808ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
2809ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
28106e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
28116e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << "vector_size" << sizeExpr->getSourceRange();
2812e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
28136e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
28146e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
28156e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
2816f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
28176e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
2818e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
28196e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
28206e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
28216e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
28226e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
28236e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
28246e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
28256e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
28266e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
28276e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
28286e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
2829e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
28306e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
28316e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
28326e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
28336e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
28346e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
2835e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
28366e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
28376e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
28386e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
28396e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
28406e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
2841788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
2842e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
28436e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
28446e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
28454211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
28464211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
28474211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
28484211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
28494211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
28504211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
28514211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
28524211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
28534211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
28544211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     VectorType::VectorKind VecKind,
28554211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const char *AttrName) {
28564211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
28574211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
28584211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
28594211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
28604211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
28614211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
28624211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
28634211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
28644211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
28654211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
28664211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
28674211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
28684211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      << AttrName << numEltsExpr->getSourceRange();
28694211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
28704211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
28714211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
28724211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
28734211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  const BuiltinType* BTy = CurType->getAs<BuiltinType>();
28744211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (!BTy ||
28754211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (VecKind == VectorType::NeonPolyVector &&
28764211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::SChar &&
28774211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short) ||
28784211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (BTy->getKind() != BuiltinType::SChar &&
28794211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UChar &&
28804211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short &&
28814211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UShort &&
28824211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Int &&
28834211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UInt &&
28844211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::LongLong &&
28854211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::ULongLong &&
28864211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Float)) {
28874211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
28884211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
28894211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
28904211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
28914211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
28924211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
28934211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
28944211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
28954211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
28964211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
28974211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
28984211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
28994211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
29004211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
29014211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
29024211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
29034211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
2904711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
2905711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             bool isDeclSpec, AttributeList *attrs) {
2906c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
2907c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
2908c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
2909c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
2910711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2911711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
2912711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
2913711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
2914711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr.getNext();
2915711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2916e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
2917711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
2918e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
2919e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
2920b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
2921b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
2922711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
2923c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
292404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2925c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
2926711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
2927c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
2928711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
2929711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
2930711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
2931d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
293204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    case AttributeList::AT_vector_size:
2933711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
2934f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
29354211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    case AttributeList::AT_neon_vector_type:
2936711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
2937711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonVector, "neon_vector_type");
29384211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
29394211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    case AttributeList::AT_neon_polyvector_type:
2940711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
2941711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonPolyVector,
29424211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                               "neon_polyvector_type");
29434211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
294404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2945711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
2946711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
2947711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
2948711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (isDeclSpec)
2949711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
2950711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2951711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
2952711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
2953711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
29546e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
2955c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
2956711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attrs = next));
2957232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
2958232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
29591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
29604ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
29614ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
29624ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
296386447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
296486447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
296586447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
296686447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
296786447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
29684ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
29694ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
29704ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
29714ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
29724ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
29734ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
29741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @param PD The partial diagnostic that will be printed out if T is not a
2975b790661a15d93941d2c33a0ea328254277b3d7e3Anders Carlsson/// complete type.
29764ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
29774ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
29784ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
297991a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
29808c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               const PartialDiagnostic &PD,
29818c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               std::pair<SourceLocation,
29828c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                                         PartialDiagnostic> Note) {
298391a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  unsigned diag = PD.getDiagID();
29841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2985573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
2986573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
2987690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
2988690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
2989690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
29901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
2991690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
2992690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
29934ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
29944ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
29954ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
29964ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
2997d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
2998923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
2999923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
3000923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
300189c49f09b0292dc7c03885f6c765d667a9837597Douglas Gregor  if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
3002923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
3003923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
30042943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
3005d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
3006972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
3007972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
3008d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
30095842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor                                                      /*Complain=*/diag != 0);
30101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
3011d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
3012d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
3013b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
3014b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        assert(MSInfo && "Missing member specialization information?");
3015357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
3016b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        if (MSInfo->getTemplateSpecializationKind()
3017972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor                                               != TSK_ExplicitSpecialization)
3018f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
3019f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
3020f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
3021f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  /*Complain=*/diag != 0);
3022d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
3023d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
3024d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
30252943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
30265842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor  if (diag == 0)
30275842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
30281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3029916c870442978db40404d51348cdf5524e506faaJohn McCall  const TagType *Tag = T->getAs<TagType>();
303001620704304f819b82ecef769ec114e541a364d7Rafael Espindola
303101620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // Avoid diagnosing invalid decls as incomplete.
303201620704304f819b82ecef769ec114e541a364d7Rafael Espindola  if (Tag && Tag->getDecl()->isInvalidDecl())
303301620704304f819b82ecef769ec114e541a364d7Rafael Espindola    return true;
303401620704304f819b82ecef769ec114e541a364d7Rafael Espindola
3035916c870442978db40404d51348cdf5524e506faaJohn McCall  // Give the external AST source a chance to complete the type.
3036916c870442978db40404d51348cdf5524e506faaJohn McCall  if (Tag && Tag->getDecl()->hasExternalLexicalStorage()) {
3037916c870442978db40404d51348cdf5524e506faaJohn McCall    Context.getExternalSource()->CompleteType(Tag->getDecl());
3038916c870442978db40404d51348cdf5524e506faaJohn McCall    if (!Tag->isIncompleteType())
3039916c870442978db40404d51348cdf5524e506faaJohn McCall      return false;
3040916c870442978db40404d51348cdf5524e506faaJohn McCall  }
3041916c870442978db40404d51348cdf5524e506faaJohn McCall
30424ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
304391a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  Diag(Loc, PD) << T;
30443c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
30458c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  // If we have a note, produce it.
30468c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  if (!Note.first.isInvalid())
30478c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson    Diag(Note.first, Note.second);
30488c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson
30494ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
305001620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
30514ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
30521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
30534ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
30544ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
30554ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
30564ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
30574ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
30584ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
3059e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
3060fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
3061fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               const PartialDiagnostic &PD) {
3062fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PD,
3063fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
3064fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
3065fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
3066fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
3067fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               unsigned DiagID) {
3068fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PDiag(DiagID),
3069fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
3070fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
3071fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
3072465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
3073465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
3074465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
3075465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
3076465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
3077e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
3078465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
3079e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
3080465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
3081465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
3082465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
3083465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
3084465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
3085465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
3086465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
3087e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
3088af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
30892a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
30902a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  ExprResult ER = CheckPlaceholderExpr(E, Loc);
30912a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
30922a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
30932a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
30942b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
30952b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
3096aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
3097aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
30982b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
3099af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
3100af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
3101af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
31022a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
31032a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  ExprResult ER = CheckPlaceholderExpr(E, Loc);
31042a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
31052a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
31064b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
3107af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getDecltypeType(E);
3108af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
3109