SemaType.cpp revision f4c7371fb1d3cebcfb40abad4537bb82515704ea
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
489711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If there are any type objects, the type as written won't
490711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // name a function, regardless of the decl spec type.  This
491711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // is because a block signature declarator is always an
492711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // abstract-declarator, and abstract-declarators can't just
493711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // be parentheses chunks.  Therefore we need to build a function
494711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // chunk unless there are no type objects and the decl spec
495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // type is a function.
496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
497711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
498711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
499711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#ifndef NDEBUG
500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getNumTypeObjects()) {
501711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    bool isOnlyParens = true;
502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (declarator.getTypeObject(i).Kind != DeclaratorChunk::Paren) {
504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        isOnlyParens = false;
505711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        break;
506711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      }
507711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
508711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    assert(!isOnlyParens &&
509711c52bb20d0c69063b52a99826fb7d2835501f1John McCall           "non-empty abstract-declarator contained only parens!");
510711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
51107fa2fa8b9a0f7982a31e12f4164550d004543aeJohn McCall#endif
512711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
513711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, we need to fake up a function declarator.
514711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation loc = declarator.getSourceRange().getBegin();
515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // ...and *prepend* it to the declarator.
517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             ParsedAttributes(),
519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*proto*/ true,
520711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*variadic*/ false, SourceLocation(),
521711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*args*/ 0, 0,
522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*type quals*/ 0,
523711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*EH*/ false, SourceLocation(), false, 0, 0, 0,
524711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*parens*/ loc, loc,
525711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             declarator));
526711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
527711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // For consistency, make sure the state still has us as processing
528711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the decl spec.
529711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.setCurrentChunkIndex(declarator.getNumTypeObjects());
53104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
53204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
533930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
534930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
5355db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// \param D  the declarator containing the declaration specifier.
5365153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
5375153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
538711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic QualType ConvertDeclSpecToType(Sema &S, TypeProcessingState &state) {
5395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
5405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
541711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
542711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
543711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const DeclSpec &DS = declarator.getDeclSpec();
544711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation DeclLoc = declarator.getIdentifierLoc();
5455db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
5465db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    DeclLoc = DS.getSourceRange().getBegin();
5471564e3906cad604a42bd131e584751a75589a9c4Chris Lattner
548711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  ASTContext &Context = S.Context;
5491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5505db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
5515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
55296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
55396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
55496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
5555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
5565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
557fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
5585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
559fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
5605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
5615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
5625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
563fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
565958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
56664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
56764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
56864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
56964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
570711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
571f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
57264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
57364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
57464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
57564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
576711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
577f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
57864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
57964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
58064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
581f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
582f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
583f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
584f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
585f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
586f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
587f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
588f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
589f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
590f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
591d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
59262f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
593097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
594c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
595c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         (ObjCProtocolDecl**)PQ,
596c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         DS.getNumProtocolQualifiers());
597c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectPointerType(Result);
59862f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
59962f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
6005db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
6015db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
6025db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
603711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isOmittedBlockReturnType(declarator)) {
6045db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      Result = Context.DependentTy;
6055db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
6065db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
6071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
608d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
609d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
610d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
611d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
612d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
613d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
614d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
615711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.getLangOptions().ImplicitInt) {
61635d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
61735d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
6183f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
619711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_declspec)
6203f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
621849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int");
6223f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
6234310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
624d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
625d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
626d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
627d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
6284310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
629711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (S.getLangOptions().CPlusPlus &&
630711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          !S.getLangOptions().Microsoft) {
631711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_missing_type_specifier)
6323f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
6331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
634b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
635b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
636b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
637711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
638b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
639711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_type_specifier)
6403f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
641b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
642d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
6431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
6453cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
6465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
6475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
648fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
649fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
650fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
651311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
652311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
653311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
654311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
655711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        if (!S.getLangOptions().C99 &&
656711c52bb20d0c69063b52a99826fb7d2835501f1John McCall            !S.getLangOptions().CPlusPlus0x)
657711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
658311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
6595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
6605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
662fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
663fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
664fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
665311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
666311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
667311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
668311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
669711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        if (!S.getLangOptions().C99 &&
670711c52bb20d0c69063b52a99826fb7d2835501f1John McCall            !S.getLangOptions().CPlusPlus0x)
671711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
672311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
6735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
6745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
675958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
6763cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
677fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
678958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
679958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
680fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
681958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
682fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
683958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
684fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
6855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
6865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
6875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
688711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
6898f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
690711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
6918f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
69299dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
6935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
6945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
6955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
696b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
6976e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
6986e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
6996e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
700711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
7016e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
7026e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
7036e24726524c2b51b31bb4b622aa678a46b024f42John McCall
704a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
705711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
706a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
7075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
708a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
709a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
7105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
711a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
7122191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
7132191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    // In C++, make an ElaboratedType.
714711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.getLangOptions().CPlusPlus) {
715465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      ElaboratedTypeKeyword Keyword
716465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara        = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
717711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
7182191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    }
7195153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    if (D->isInvalidDecl())
720711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
721958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7231a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
7245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
7255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
7265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
727711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
72827940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    if (Result.isNull())
729711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
73027940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    else if (DeclSpec::ProtocolQualifierListTy PQ
73127940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall               = DS.getProtocolQualifiers()) {
732c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
733c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // Silently drop any existing protocol qualifiers.
734c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // TODO: determine whether that's the right thing to do.
735c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjT->getNumProtocols())
736c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = ObjT->getBaseType();
737c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
738c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (DS.getNumProtocolQualifiers())
739c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = Context.getObjCObjectType(Result,
740c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             (ObjCProtocolDecl**) PQ,
741c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             DS.getNumProtocolQualifiers());
742c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCIdType()) {
743ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
744c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
745c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
746c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
747c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
748c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCClassType()) {
7494262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
750c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
751c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
752c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
753c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
7543f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
755711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
7563f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
757711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
7583f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
759c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
762958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
764958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
765e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
766711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
767958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
768730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian    if (!Result->isDependentType())
769730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian      if (const TagType *TT = Result->getAs<TagType>())
770711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
771d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
772fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
773958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
774d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
775b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
776d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
777d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
778711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
7794b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
7804b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
781711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
7824b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
783958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
784d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
7856fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
786b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
7876fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
7886fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
789711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
790af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
791af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
792711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
793af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
7946fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
7956fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
796e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
797e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
798e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    Result = Context.UndeducedAutoTy;
799e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
800e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
8011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
802809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
8035153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
804711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
8055153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
8065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
808958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
809f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
810711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.getLangOptions().Freestanding)
811711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
812fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
81382287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
81482287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
81582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
816e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
817788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
818e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
819788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
820e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
821e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
822f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
8231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82447423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
82547423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
826711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
8271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
828711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
829711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
830711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
831711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
832711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
833711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
834711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
835711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
836711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (AttributeList *attrs = DS.getAttributes().getList())
837711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    processTypeAttrs(state, Result, true, attrs);
8381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
83996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
84096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
84196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
84296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
84396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
84496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
8450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (TypeQuals & DeclSpec::TQ_restrict) {
8462b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian      if (Result->isAnyPointerType() || Result->isReferenceType()) {
8472b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        QualType EltTy;
8482b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        if (Result->isObjCObjectPointerType())
8492b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result;
8502b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        else
8512b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result->isPointerType() ?
8522b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<PointerType>()->getPointeeType() :
8532b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<ReferenceType>()->getPointeeType();
8541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
855bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
856bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
857bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
858711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getRestrictSpecLoc(),
859d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
860d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
8610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall          TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
862bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
863bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
864711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DS.getRestrictSpecLoc(),
865711c52bb20d0c69063b52a99826fb7d2835501f1John McCall               diag::err_typecheck_invalid_restrict_not_pointer)
866d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
8670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
86896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
86996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
8701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
87296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
87396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
87496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
87596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
87696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
8770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
87896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
8790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
88096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
8810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
8820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        assert((TypeQuals & DeclSpec::TQ_restrict) &&
8830953e767ff7817f97b3ab20896b229891eeff45bJohn McCall               "Has CVR quals but not C, V, or R?");
8840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Loc = DS.getRestrictSpecLoc();
88596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
886711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(Loc, diag::warn_typecheck_function_qualifiers)
887d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
88896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
8891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
890f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
891f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
892f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
893f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
894f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
8951a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
8961a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
897f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
8980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
8990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
9001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
9011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
9030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Result = Context.getQualifiedType(Result, Quals);
90496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
9050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
906f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
907f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
908f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
909cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
910cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
911cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
9121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
913cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
914cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
915cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
9162865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
9172865474261a608c7873b87ba4af110d17907896dJohn McCall                                  Qualifiers Qs) {
9182865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
9192865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
9202865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
9212865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
9222865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
9232865474261a608c7873b87ba4af110d17907896dJohn McCall
9242865474261a608c7873b87ba4af110d17907896dJohn McCall    const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
9252865474261a608c7873b87ba4af110d17907896dJohn McCall    if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
9262865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
9272865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9282865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
9292865474261a608c7873b87ba4af110d17907896dJohn McCall      }
9302865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
9312865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
9322865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9332865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
9342865474261a608c7873b87ba4af110d17907896dJohn McCall      }
9352865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
9362865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
9372865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9382865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
9392865474261a608c7873b87ba4af110d17907896dJohn McCall      }
9402865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (!Ty->isDependentType()) {
9412865474261a608c7873b87ba4af110d17907896dJohn McCall      // FIXME: this deserves a proper diagnostic
9422865474261a608c7873b87ba4af110d17907896dJohn McCall      DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9432865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
9442865474261a608c7873b87ba4af110d17907896dJohn McCall    }
9452865474261a608c7873b87ba4af110d17907896dJohn McCall
9462865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
9472865474261a608c7873b87ba4af110d17907896dJohn McCall      Diag(Loc, DiagID) << ProblemTy;
9482865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
9492865474261a608c7873b87ba4af110d17907896dJohn McCall    }
9502865474261a608c7873b87ba4af110d17907896dJohn McCall  }
9512865474261a608c7873b87ba4af110d17907896dJohn McCall
9522865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
9532865474261a608c7873b87ba4af110d17907896dJohn McCall}
9542865474261a608c7873b87ba4af110d17907896dJohn McCall
955075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
956075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
957075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
958075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
959075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
960cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
961cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
962cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
963cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
964cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
965cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
966cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
967cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
968cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
969cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
970cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
971cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
972cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
9732865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
974cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
975cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
976cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
977cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
978ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
979cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
980cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
981cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
982c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
98392e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
984cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
9852865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
986cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
987cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
988cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
989cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
990cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
991cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
992cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
993cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
994cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
995cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
996cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
997cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
998cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
999cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
1000cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
100154e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
10022865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
100354e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
100454e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
100554e14c4db764c0636160d26c5bbf491637c83a76John McCall
100654e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
100754e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   reference to a type T, and attempt to create the type "lvalue
100854e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   reference to cv TD" creates the type "lvalue reference to T".
100954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // We use the qualifiers (restrict or none) of the original reference,
101054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // not the new ones. This is consistent with GCC.
101154e14c4db764c0636160d26c5bbf491637c83a76John McCall
101254e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
101354e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
101454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
101554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
101654e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
101754e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
101854e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
101954e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
102054e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
102154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
102254e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
102354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // collapsing of references-to-references as described in C++
102454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // DR 106 and amended by C++ DR 540.
1025cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1026cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
102733a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
1028cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
1029cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
1030cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
1031cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1032cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1033cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1034cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
10357c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
10362865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
10372865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
1038cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1039cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1040cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
1041cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1042cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
1043cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1044cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
10451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
10461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
1047cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1048cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1049cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// array type or, if there is no such entity, the location of the
1050cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have array type.
1051cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1052cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
1053cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1054cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1055cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
1056cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
1057cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1058cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
10597e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
10600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10617e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
1062923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (getLangOptions().CPlusPlus) {
1063138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
1064138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
1065138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   type, the (possibly cv-qualified) type void, a function type or an
1066138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
1067138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
1068138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
1069138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
1070138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
1071138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
1072138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1073138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
1074138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1075923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (T->isVoidType()) {
1076923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1077923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1078923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1079138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1080138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (RequireNonAbstractType(Brackets.getBegin(), T,
1081138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
1082138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1083138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1084923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
1085138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1086138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
1087923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
1088923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
1089923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1090923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
1091cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1092cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
1093cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
1094ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1095cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1096cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
10971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1098e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
10991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Loc,  diag::err_illegal_decl_array_of_auto)
1100e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson      << getPrintableNameForEntity(Entity);
1101e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
1102e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
11031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11046217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
1105cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
1106cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
1107cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
1108cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
1109c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
1110c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1111c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
1112cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
11131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11145e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
11155e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  if (ArraySize && !ArraySize->isRValue())
11165e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall    DefaultLvalueConversion(ArraySize);
11175e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1118cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
11195e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // TODO: in theory, if we were insane, we could allow contextual
11205e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // conversions to integer type here.
1121cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (ArraySize && !ArraySize->isTypeDependent() &&
11221274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1123cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1124cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
1125cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1126cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
11272767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
1128cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
1129f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
11307e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
1131f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
1132f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
1133ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
11347e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
1135cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
1136923d56d436f750bc1f29db50e641078725558a1bSebastian Redl             (!T->isDependentType() && !T->isIncompleteType() &&
1137923d56d436f750bc1f29db50e641078725558a1bSebastian Redl              !T->isConstantSizeType())) {
1138cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Per C99, a variable array is an array with either a non-constant
1139cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // size or an element type that has a non-constant-size
11407e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1141cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
1142cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1143cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
1144923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
1145b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
1146b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1147b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1148b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
1149b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1150b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
1151923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1152923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1153923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
115402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
115502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
115602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      Diag(ArraySize->getLocStart(),
115702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
115802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
1159923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
11602767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
11612767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor               !T->isIncompleteType()) {
11622767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      // Is the array too large?
11632767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
11642767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
11652767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
11662767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
11672767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
11682767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
11691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
11702767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
117146a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
1172cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1173af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
1174af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  if (!getLangOptions().C99) {
11750fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
11760fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
1177204ce17e0cfd9bbe229627e1e5a20c3f2f587c8cDouglas Gregor      if (!T->isDependentType() &&
1178204ce17e0cfd9bbe229627e1e5a20c3f2f587c8cDouglas Gregor          !Context.getBaseElementType(T)->isPODType()) {
11790fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
11800fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor          << Context.getBaseElementType(T);
11810fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
11820fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      }
1183a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
1184a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
1185a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
1186a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
1187a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
11880fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
11890fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
11900fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::ext_vla);
11910fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
1192043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      Diag(Loc,
1193043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor           getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
1194043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor                                     : diag::ext_c99_array_usage);
1195cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1196cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1197cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
1198cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
11999cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
12009cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
12019cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
12029cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
12039ae2f076ca5ab1feb3ba95629099ec2319833701John McCallQualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
12049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
12059cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
12069cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
12071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
12089cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
12099cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
12109cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
12119cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
12129cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
12139ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
12149cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
12159ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
12169cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
12179ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        << "ext_vector_type" << ArraySize->getSourceRange();
12189cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
12199cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
12229cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
12231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
12241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12259cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
12269cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
12279ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      << ArraySize->getSourceRange();
12289cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
12299cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
12301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12319cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!T->isDependentType())
12329cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return Context.getExtVectorType(T, vectorSize);
12331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
12341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12359ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
12369cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
12371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1238724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
1239724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1240724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
1241724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
1242724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
12432943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
1244724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
1245724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1246724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
1247724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1248724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
1249724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
1250724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
1251724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1252724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
1253724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1254724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
1255724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1256724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
1257724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1258724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
1259724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
1260724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
1261724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1262724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
1263724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
1264724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1265724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
1266724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
1267724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
12681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 QualType *ParamTypes,
1269724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
1270724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 bool Variadic, unsigned Quals,
1271fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
1272e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                 FunctionType::ExtInfo Info) {
1273724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
127458408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor    Diag(Loc, diag::err_func_returning_array_function)
127558408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
1276724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1277724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
12785291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
1279724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
1280724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
12812dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    QualType ParamType = adjustParameterType(ParamTypes[Idx]);
12822dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
1283724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
1284724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
1285724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
1286cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
128754e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
1288724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1289724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1290724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
1291724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1292724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1293e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
1294e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.Variadic = Variadic;
1295e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.TypeQuals = Quals;
1296e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.ExtInfo = Info;
1297e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1298e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI);
1299724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
13001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1301949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
1302949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1303949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
1304949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
13050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR Qualifiers applied to the member pointer type
1306949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
1307949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
1308949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1309949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
1310949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
13111eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
13122865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
1313949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
1314949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
1315949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
1316949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
1317949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
1318949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1319949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
1320949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
1321949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1322949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
1323949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
1324949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
1325949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1326949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1327737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
1328949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
1329949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
13308d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
1331ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
1332949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1333949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1334949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1335949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
1336949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1337949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
1338949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1339949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1340949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1341949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
1342949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1343949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1344949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1345949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1346d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // In the Microsoft ABI, the class is allowed to be an incomplete
1347d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // type. In such cases, the compiler makes a worst-case assumption.
1348d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // We make no such assumption right now, so emit an error if the
1349d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // class isn't a complete type.
135020cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  if (Context.Target.getCXXABI() == CXXABI_Microsoft &&
1351d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis      RequireCompleteType(Loc, Class, diag::err_incomplete_type))
1352d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis    return QualType();
1353d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis
13542865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
1355949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
13561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13579a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
13589a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13599a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
13609a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
13629a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13639a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Loc The location of the entity whose type involves this
13649a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// block pointer type or, if there is no such entity, the location of the
13659a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type that will have block pointer type.
13669a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13679a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
13689a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
13699a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13709a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
13719a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
13722865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildBlockPointerType(QualType T,
13731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
13749a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
13750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
13769a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
13779a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
13789a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
13791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13802865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
13819a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
13829a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
1383b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1384b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
13853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
1386a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
13873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
13883f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
13893f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1390a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
1391f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
1392e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
1393a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
1394e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
13951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1396a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
1397e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
1398e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
1399e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
140098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
14018ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl/// declarator to Type instances.
1402402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor///
1403402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
1404402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// owns the declaration of a type (e.g., the definition of a struct
1405402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// type), then *OwnedDecl will receive the owned declaration.
1406bf1a028246d884a540aeafa38e89be59a269b072John McCall///
1407bf1a028246d884a540aeafa38e89be59a269b072John McCall/// The result of this call will never be null, but the associated
1408bf1a028246d884a540aeafa38e89be59a269b072John McCall/// type may be a null type if there's an unrecoverable error.
1409bf1a028246d884a540aeafa38e89be59a269b072John McCallTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
1410bf1a028246d884a540aeafa38e89be59a269b072John McCall                                           TagDecl **OwnedDecl) {
1411930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
1412930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
1413930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
141405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  TypeSourceInfo *ReturnTypeInfo = 0;
1415711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1416711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  TypeProcessingState state(*this, D);
141704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
14183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
14193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
14203f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
14210486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
14223f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
1423711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    T = ConvertDeclSpecToType(*this, state);
14245db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
1425591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
1426b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      TagDecl* Owned = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
1427b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      // Owned is embedded if it was defined here, or if it is the
1428b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      // very first (i.e., canonical) declaration of this tag type.
1429b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      Owned->setEmbeddedInDeclarator(Owned->isDefinition() ||
1430b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor                                     Owned->isCanonicalDecl());
1431591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor      if (OwnedDecl) *OwnedDecl = Owned;
1432591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
1433930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
1434930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
14353f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
14360efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
14373f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
1438930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
143948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // "void" instead.
1440930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
1441930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
144248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
144348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
144448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
144548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
144605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    T = GetTypeFromParser(D.getName().ConversionFunctionId,
1447bf1a028246d884a540aeafa38e89be59a269b072John McCall                          &ReturnTypeInfo);
144848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
1449930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
1450dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
1451711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
1452711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
1453711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1454dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor  // Check for auto functions and trailing return type and adjust the
1455dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor  // return type accordingly.
1456dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor  if (getLangOptions().CPlusPlus0x && D.isFunctionDeclarator()) {
1457075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
1458dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor    if (T == Context.UndeducedAutoTy) {
1459dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor      if (FTI.TrailingReturnType) {
1460dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor          T = GetTypeFromParser(ParsedType::getFromOpaquePtr(FTI.TrailingReturnType),
1461dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor                                &ReturnTypeInfo);
1462dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor      }
1463dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor      else {
1464dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor          Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1465dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor               diag::err_auto_missing_trailing_return);
1466dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor          T = Context.IntTy;
1467dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor          D.setInvalidType(true);
1468dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor      }
1469dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor    }
1470dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor    else if (FTI.TrailingReturnType) {
1471dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor      Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1472dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor           diag::err_trailing_return_without_auto);
1473dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor      D.setInvalidType(true);
1474dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor    }
1475dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor  }
1476dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
14771f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor  if (T.isNull())
1478bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
14791f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
1480baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  if (T == Context.UndeducedAutoTy) {
1481baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
14821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1483baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
1484baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
1485baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      assert(0 && "K&R type lists aren't allowed in C++");
1486baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1487baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
1488baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
1489baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1490baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
1491baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      switch (cast<TagDecl>(CurContext)->getTagKind()) {
1492465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Enum: assert(0 && "unhandled tag kind"); break;
1493465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
1494465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
1495465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
14961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
1497baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1498baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
1499baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 4; // Exception declaration
1500baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1501baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
1502baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 5; // Template parameter
1503baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1504baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
1505baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 6;  // Block literal
1506baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1507baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
1508baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
1509baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
1510baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
1511baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TypeNameContext:
1512baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1513baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1514baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
1515baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
1516baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
1517baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
1518baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      T = Context.IntTy;
1519baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
1520baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1521baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
15221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1523cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
1524cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
1525cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
1526cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
15271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
152898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
152998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
153098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
15318ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1532711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
1533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
1534711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
15355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
15365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
1537075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
1538075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      T = BuildParenType(T);
1539075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
15405618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
15419af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
15429af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
15439af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner        Diag(DeclType.Loc, diag::err_blocks_disable);
15441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15452865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
15462865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
15472865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
15485618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
15495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
15506a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
15516a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
15526a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
15536a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
15546a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
15556a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
15566a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
1557c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) {
1558c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
15592865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
15602865474261a608c7873b87ba4af110d17907896dJohn McCall          T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
156114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
156214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
15632865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildPointerType(T, DeclType.Loc, Name);
15642865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
15652865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
1566711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
15675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
15680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
15696a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
15706a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
15716a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
15726a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
15736a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
15746a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
15756a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
15762865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
15772865474261a608c7873b87ba4af110d17907896dJohn McCall
15782865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
15792865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
15802865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
15815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
15820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
15835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
15846a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
15856a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
15866a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
15876a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
15886a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
15896a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
15906a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
1591fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
159294f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
15935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
15945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
15965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
15975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
15985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
1600f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      if (ASM == ArrayType::Star &&
1601f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman          D.getContext() != Declarator::PrototypeContext) {
1602f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
1603f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
1604f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1605f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1606f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
1607f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
1608f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
16090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      T = BuildArrayType(T, ASM, ArraySize,
16100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                         Qualifiers::fromCVRMask(ATI.TypeQuals),
16117e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                         SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
16125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
16135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1614f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
16155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
16165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
16175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
16185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
16193cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
1620cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      // C99 6.7.5.3p1: The return type may not be a function or array type.
162158408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      // For conversion functions, we'll diagnose this particular error later.
162248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor      if ((T->isArrayType() || T->isFunctionType()) &&
162348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
162458408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor        Diag(DeclType.Loc, diag::err_func_returning_array_function)
162558408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor          << T->isFunctionType() << T;
1626cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        T = Context.IntTy;
1627cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        D.setInvalidType(true);
1628cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      }
1629465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
16305291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
16315291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
16325291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
16335291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          (!getLangOptions().CPlusPlus ||
16345291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor           (!T->isDependentType() && !T->isRecordType()))) {
16355291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        unsigned Quals = D.getDeclSpec().getTypeQualifiers();
1636de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        std::string QualStr;
1637de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        unsigned NumQuals = 0;
16385291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        SourceLocation Loc;
1639de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        if (Quals & Qualifiers::Const) {
16405291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          Loc = D.getDeclSpec().getConstSpecLoc();
1641de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          ++NumQuals;
1642de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          QualStr = "const";
1643de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        }
1644de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        if (Quals & Qualifiers::Volatile) {
1645de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          if (NumQuals == 0) {
1646de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            Loc = D.getDeclSpec().getVolatileSpecLoc();
1647de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            QualStr = "volatile";
1648de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          } else
1649de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            QualStr += " volatile";
1650de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          ++NumQuals;
1651de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        }
1652de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        if (Quals & Qualifiers::Restrict) {
1653de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          if (NumQuals == 0) {
1654de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            Loc = D.getDeclSpec().getRestrictSpecLoc();
1655de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            QualStr = "restrict";
1656de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          } else
1657de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            QualStr += " restrict";
1658de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          ++NumQuals;
16595291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        }
1660de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        assert(NumQuals > 0 && "No known qualifiers?");
1661de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor
16625291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        SemaDiagnosticBuilder DB = Diag(Loc, diag::warn_qual_return_type);
1663de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        DB << QualStr << NumQuals;
16645291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        if (Quals & Qualifiers::Const)
16655291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          DB << FixItHint::CreateRemoval(D.getDeclSpec().getConstSpecLoc());
16665291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        if (Quals & Qualifiers::Volatile)
16675291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          DB << FixItHint::CreateRemoval(D.getDeclSpec().getVolatileSpecLoc());
16685291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        if (Quals & Qualifiers::Restrict)
16695291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          DB << FixItHint::CreateRemoval(D.getDeclSpec().getRestrictSpecLoc());
16705291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      }
16715291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
1672402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1673402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
1674402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
1675b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
1676402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        if (Tag->isDefinition())
1677402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor          Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1678402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
1679402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
1680402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
16813cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
16823cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
16833cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      if (FTI.hasExceptionSpec &&
16843cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl          D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
16853cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl        Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
16863cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
16872865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) {
16882865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
16892865474261a608c7873b87ba4af110d17907896dJohn McCall        T = Context.getFunctionNoProtoType(T);
16902865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
16912865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
16922865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
16932865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
16942865474261a608c7873b87ba4af110d17907896dJohn McCall        if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) {
1695965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
1696965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
1697965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
1698965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
1699965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
1700965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
1701965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
1702965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
1703965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
1704965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
1705965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1706c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
17072865474261a608c7873b87ba4af110d17907896dJohn McCall
17082865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
1709788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
1710788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
17112865474261a608c7873b87ba4af110d17907896dJohn McCall          Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
17122865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
17132865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
17142865474261a608c7873b87ba4af110d17907896dJohn McCall        }
17152865474261a608c7873b87ba4af110d17907896dJohn McCall
1716e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
1717e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
1718e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
1719e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
17205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
17215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
17225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
17232865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
17241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1726d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
17278123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
172878c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
17292dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
17302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
1731beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor          assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
17322dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
17335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
17345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
173572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
17362dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
17375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
17385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
17395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
17405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
17415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
17422ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
17438123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
17442ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
17452ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
17465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
17474565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
17482ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
17498123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
17502ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
17512ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
17520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
17532ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
17541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17552ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
17562ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
17572ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
1758eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
1759eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
1760a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
1761183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
1762eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
1763eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
1764eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
17655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
176656a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
176754e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
17685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
1769465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1770465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        llvm::SmallVector<QualType, 4> Exceptions;
1771e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        if (FTI.hasExceptionSpec) {
1772e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          EPI.HasExceptionSpec = FTI.hasExceptionSpec;
1773e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          EPI.HasAnyExceptionSpec = FTI.hasAnyExceptionSpec;
1774e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          Exceptions.reserve(FTI.NumExceptions);
1775e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1776e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // FIXME: Preserve type source info.
1777e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1778e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // Check that the type is valid for an exception spec, and
1779e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // drop it if not.
1780e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1781e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall              Exceptions.push_back(ET);
1782e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
1783373920bd733b1d28fe7bf209945a62eb9248d948John McCall          EPI.NumExceptions = Exceptions.size();
1784e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          EPI.Exceptions = Exceptions.data();
1785ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl        }
1786465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1787e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI);
17885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
178904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
17905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
17915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1792f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
1793f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
17947bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
1795f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
17967bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
1797edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
1798edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
17997bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      } else if (isDependentScopeSpecifier(SS) ||
18007bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara                 dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS))) {
18011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
18027bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
180387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
180487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
180587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
18067bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
18074a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
180887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
180987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
181087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
181187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
18129f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
181387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
18147bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
181587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
181687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
181787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
18187bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // Note: if NNS is dependent, then its prefix (if any) is already
18197bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // included in ClsType; this does not hold if the NNS is
18207bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // nondependent: in this case (if there is indeed a prefix)
18217bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // ClsType needs to be wrapped into an elaborated type.
18227bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          if (NNSPrefix && !NNS->isDependent())
18237bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
182487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
182587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
1826f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
1827949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        Diag(DeclType.Mem.Scope().getBeginLoc(),
1828949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
1829949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1830949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
1831f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
1832f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1833f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1834949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
18352865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
1836949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
1837f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
1838949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
18392865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
18402865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
1841f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1842f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
1843f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
1844f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1845cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
1846cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
1847cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
1848cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
1849cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1850c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
1851711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
1852711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      processTypeAttrs(state, T, false, attrs);
18535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1854971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1855971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
1856183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
1857778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
1858971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1859708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    // C++ 8.3.5p4:
1860708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
1861708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
1862708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
1863708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
1864613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
1865613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
1866613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (D.getContext() != Declarator::MemberContext ||
1867613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall                      D.getDeclSpec().isFriendSpecified());
1868613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
1869613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      DeclContext *DC = computeDeclContext(D.getCXXScopeSpec());
1870613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
1871613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
1872613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
1873971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    if (FnTy->getTypeQuals() != 0 &&
1874971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
1875c61bb2056148891375bfa591fa2859b9b6ec2734Sebastian Redl        (FreeFunction ||
1876971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
1877971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      if (D.isFunctionDeclarator())
1878971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1879971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      else
1880971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        Diag(D.getIdentifierLoc(),
1881c61bb2056148891375bfa591fa2859b9b6ec2734Sebastian Redl             diag::err_invalid_qualified_typedef_function_type_use)
1882c61bb2056148891375bfa591fa2859b9b6ec2734Sebastian Redl          << FreeFunction;
1883971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1884971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      // Strip the cv-quals from the type.
1885e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
1886e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      EPI.TypeQuals = 0;
1887e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1888971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis      T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
1889e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                  FnTy->getNumArgs(), EPI);
1890971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
1891971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
18921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1893711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
1894711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull())
1895711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = D.getAttributes())
1896711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      processTypeAttrs(state, T, false, attrs);
1897711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1898711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
1899711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
1900711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1901737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // If there's a constexpr specifier, treat it as a top-level const.
1902737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  if (D.getDeclSpec().isConstexprSpecified()) {
1903737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
1904737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
1905737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
1906a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // If there was an ellipsis in the declarator, the declaration declares a
1907a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
1908a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (D.hasEllipsis() && !T.isNull()) {
1909a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
1910a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   A declarator-id or abstract-declarator containing an ellipsis shall
1911a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
1912a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
1913a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
1914a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
1915a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
1916a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   [...] When it is part of a parameter-declaration-clause, the
1917a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   parameter pack is a function parameter pack (14.5.3). The type T
1918a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
1919a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   a template parameter pack; each template parameter pack in T is
1920a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
1921a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
1922a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
1923a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
1924a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
1925a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        Diag(D.getEllipsisLoc(),
1926a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
1927a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
1928a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
1929a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
1930cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
1931a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
1932a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
1933a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
1934a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
1935a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
1936a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   If a template-parameter is a [...] is a parameter-declaration that
1937a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
1938a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
1939a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
1940a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
1941a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
1942a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
1943a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
1944cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
194510738d36b150aa65206890c1c845cdba076e4200Douglas Gregor      else if (!getLangOptions().CPlusPlus0x)
194610738d36b150aa65206890c1c845cdba076e4200Douglas Gregor        Diag(D.getEllipsisLoc(), diag::err_variadic_templates);
1947a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
1948a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
1949a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
1950a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
1951a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
1952a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
1953a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
1954a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
1955a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
1956a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
1957a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
1958a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
1959a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
1960a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
1961a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
1962a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
1963a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
1964a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
1965a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
1966bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
1967bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
1968bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
1969bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
1970bf1a028246d884a540aeafa38e89be59a269b072John McCall  return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo);
19715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
197351bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
197451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
197551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
1976f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
197751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
197851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
1979f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
198051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
198151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
198251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
198351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
198451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
198551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
198651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
198751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
1988c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
1989c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1990c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
1991c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
1992c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
1993c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.getBaseLoc().initialize(SourceLocation());
1994c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
1995c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
1996c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
1997c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
199854e14c4db764c0636160d26c5bbf491637c83a76John McCall
1999c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
200054e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
200154e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
200254e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
200354e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
200454e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
200554e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
200654e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
200754e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
200854e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
200954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
201054e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
201154e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
201251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
201354e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
201454e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
2015c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
201651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
2017833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
2018a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
2019b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2020833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2021833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
2022833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
2023a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
2024833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        TL.initialize(DS.getTypeSpecTypeLoc());
2025833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
2026833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
2027833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2028e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
2029e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
2030e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
2031e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TemplateSpecializationTypeLoc NamedTL =
2032e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
2033e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
2034e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2035e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      else
2036e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
2037833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
2038cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
2039cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
2040cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2041cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
2042cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
2043cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
2044cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
2045cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2046cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
2047b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
2048cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
2049b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2050cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
2051cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
2052ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
2053ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
2054ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
2055ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
2056ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
2057ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
2058ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
2059ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
2060ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
2061ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
2062ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
2063ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
2064ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
2065ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
2066ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
2067e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
2068e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
2069e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
2070253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
2071e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
2072b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2073e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
2074e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
2075e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
2076e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
2077e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2078e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setKeywordLoc(Keyword != ETK_None
2079e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       ? DS.getTypeSpecTypeLoc()
2080e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       : SourceLocation());
2081e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
2082e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setQualifierRange(SS.isEmpty() ? SourceRange(): SS.getRange());
2083e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
2084e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
2085e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
2086e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
2087e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
2088253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
2089e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
2090b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2091e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
2092e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
2093e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
2094e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
2095e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2096e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setKeywordLoc(Keyword != ETK_None
2097e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       ? DS.getTypeSpecTypeLoc()
2098e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       : SourceLocation());
2099e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
2100e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
2101e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      // FIXME: load appropriate source location.
210233500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
210333500955d731c73717af52088b7fc0e7a85681e7John McCall    }
210433500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
210533500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
210633500955d731c73717af52088b7fc0e7a85681e7John McCall      ElaboratedTypeKeyword Keyword
210733500955d731c73717af52088b7fc0e7a85681e7John McCall        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
210833500955d731c73717af52088b7fc0e7a85681e7John McCall      if (Keyword == ETK_Typename) {
210933500955d731c73717af52088b7fc0e7a85681e7John McCall        TypeSourceInfo *TInfo = 0;
2110b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
211133500955d731c73717af52088b7fc0e7a85681e7John McCall        if (TInfo) {
211233500955d731c73717af52088b7fc0e7a85681e7John McCall          TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
211333500955d731c73717af52088b7fc0e7a85681e7John McCall                    TInfo->getTypeLoc()));
211433500955d731c73717af52088b7fc0e7a85681e7John McCall          return;
211533500955d731c73717af52088b7fc0e7a85681e7John McCall        }
211633500955d731c73717af52088b7fc0e7a85681e7John McCall      }
211733500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.initializeLocal(SourceLocation());
211833500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setKeywordLoc(Keyword != ETK_None
211933500955d731c73717af52088b7fc0e7a85681e7John McCall                       ? DS.getTypeSpecTypeLoc()
212033500955d731c73717af52088b7fc0e7a85681e7John McCall                       : SourceLocation());
212133500955d731c73717af52088b7fc0e7a85681e7John McCall      const CXXScopeSpec& SS = DS.getTypeSpecScope();
212233500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
212333500955d731c73717af52088b7fc0e7a85681e7John McCall      // FIXME: load appropriate source location.
2124e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeLoc());
2125e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
2126e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
212751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
212851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
212951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.initialize(DS.getTypeSpecTypeLoc());
213051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
213151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
2132eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
213351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
213451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
2135f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
213651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
213751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
21384adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
213951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
21409f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
214151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
21424adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
214351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
214451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
214551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
21464adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
214751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
214851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
214951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
21504adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
215151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
215251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
215351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
21544adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
215551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
215651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
215751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
215851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: nested name specifier
21594adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
216051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
216151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
216254e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
216354e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
216451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
216551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
216651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
216751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
216851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
216951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
217051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
217151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
217251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
217351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
217451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
217551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
217651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
217751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
217851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
217951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLParenLoc(Chunk.Loc);
218051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRParenLoc(Chunk.EndLoc);
2181dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor      TL.setTrailingReturn(!!Chunk.Fun.TrailingReturnType);
218251bd803fbdade51d674598ed45da3d54190a656cJohn McCall
218351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
218454e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
2185d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
218654e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
21874adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
218851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
21894adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
2190075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
2191075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
2192075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
2193075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
2194075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
21951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
219651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
21979f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
21984adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
219951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
220051bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
22014adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
2202a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
220351bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
220451bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
220505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
220605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
220705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
220805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
220905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
2210a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
221105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
221205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
2213a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
2214a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
221551bd803fbdade51d674598ed45da3d54190a656cJohn McCall
2216a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
2217a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
2218a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc());
2219a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
2220a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
2221a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
22228ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
222351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
222451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
22254adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
2226f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
2227b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
2228b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
2229b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
223005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
223105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
223205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
2233b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
2234b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
223505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
223605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor
2237a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
22384adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
22394adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
2240a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
2241b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
22421bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
22431bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
22441bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
2245eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
2246eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
2247a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
22481bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
22491bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
2250b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
22511bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
22521bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
22531bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
22541bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
225535d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis  assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
225635d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
225735d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
22581bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
22591bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
2260f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
22615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
22625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
22635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
22641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2265402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  TagDecl *OwnedTag = 0;
2266bf1a028246d884a540aeafa38e89be59a269b072John McCall  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag);
2267bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
22685153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
2269809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
22705912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
2271402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  if (getLangOptions().CPlusPlus) {
2272402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
22736d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
22746d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
2275402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // C++0x [dcl.type]p3:
2276402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   A type-specifier-seq shall not define a class or enumeration
2277402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   unless it appears in the type-id of an alias-declaration
2278402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   (7.1.3).
2279402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    if (OwnedTag && OwnedTag->isDefinition())
2280402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
2281402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        << Context.getTypeDeclType(OwnedTag);
2282402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
2283402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
2284b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
22855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2287c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
2288c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
2289c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
2290c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
2291c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
2292232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
2293232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
2294c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
2295c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
22961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
2297c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
22980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
2299232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
2300232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
2301232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
2302232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
2303c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
2304e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2305c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
2306232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
23071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2308232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
2309545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
2310f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2311e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2312c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
2313232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
2314545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
2315232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
2316ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
2317ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
2318dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
2319dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
2320e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2321c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
2322232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
2323232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
2324efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
2325efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
2326efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
2327efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
2328efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
2329e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
2330efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
2331efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
2332efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
2333efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
2334efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
23350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
2336efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
2337efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
23380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
2339e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2340efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
2341efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
2342efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
23431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
2344f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
2345c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
2346c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
2347711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
2348711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
2349711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
2350711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
2351711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
2352711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
2353711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
2354711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
2355711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2356711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
2357711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
2358711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
2359711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
2360711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
2361711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2362711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
2363711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
2364711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2365711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2366d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
23671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2368d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
2369711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!attr.getParameterName()) {
2370711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2371ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
2372711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2373711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2374ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
23750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
2376711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getNumArgs() != 0) {
2377711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2378711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2379711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2380d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
2381711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getParameterName()->isStr("weak"))
23820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
2383711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  else if (attr.getParameterName()->isStr("strong"))
23840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
2385d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
2386711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
2387711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      << "objc_gc" << attr.getParameterName();
2388711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2389711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2390d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
23911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2392711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  type = S.Context.getObjCGCQualType(type, GCAttr);
2393711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
2394d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
2395d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
2396e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallnamespace {
2397e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// A helper class to unwrap a type down to a function for the
2398e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// purposes of applying attributes there.
2399e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///
2400e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// Use:
2401e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
2402e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   if (unwrapped.isFunctionType()) {
2403e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     const FunctionType *fn = unwrapped.get();
2404e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     // change fn somehow
2405e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     T = unwrapped.wrap(fn);
2406e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   }
2407e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  struct FunctionTypeUnwrapper {
2408e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    enum WrapKind {
2409e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Desugar,
2410e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Parens,
2411e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Pointer,
2412e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      BlockPointer,
2413e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Reference,
2414e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      MemberPointer
2415e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    };
2416e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2417e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType Original;
2418e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *Fn;
2419e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    llvm::SmallVector<unsigned char /*WrapKind*/, 8> Stack;
2420e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2421e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
2422e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      while (true) {
2423e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const Type *Ty = T.getTypePtr();
2424e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<FunctionType>(Ty)) {
2425e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Fn = cast<FunctionType>(Ty);
2426e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return;
2427e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ParenType>(Ty)) {
2428e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ParenType>(Ty)->getInnerType();
2429e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Parens);
2430e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<PointerType>(Ty)) {
2431e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<PointerType>(Ty)->getPointeeType();
2432e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Pointer);
2433e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<BlockPointerType>(Ty)) {
2434e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<BlockPointerType>(Ty)->getPointeeType();
2435e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(BlockPointer);
2436e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<MemberPointerType>(Ty)) {
2437e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<MemberPointerType>(Ty)->getPointeeType();
2438e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(MemberPointer);
2439e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ReferenceType>(Ty)) {
2440e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ReferenceType>(Ty)->getPointeeType();
2441e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Reference);
2442e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else {
2443e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          const Type *DTy = Ty->getUnqualifiedDesugaredType();
2444e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          if (Ty == DTy) {
2445e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            Fn = 0;
2446e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            return;
2447e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          }
2448e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2449e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = QualType(DTy, 0);
2450e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Desugar);
2451e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        }
2452e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2453e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2454e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2455e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    bool isFunctionType() const { return (Fn != 0); }
2456e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *get() const { return Fn; }
2457e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2458e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(Sema &S, const FunctionType *New) {
2459e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // If T wasn't modified from the unwrapped type, do nothing.
2460e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (New == get()) return Original;
2461e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2462e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Fn = New;
2463e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return wrap(S.Context, Original, 0);
2464e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2465e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2466e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  private:
2467e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
2468e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size())
2469e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getQualifiedType(Fn, Old.getQualifiers());
2470e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2471e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // Build up the inner type, applying the qualifiers from the old
2472e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // type to the new type.
2473e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      SplitQualType SplitOld = Old.split();
2474e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2475e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // As a special case, tail-recurse if there are no qualifiers.
2476e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (SplitOld.second.empty())
2477e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, SplitOld.first, I);
2478e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return C.getQualifiedType(wrap(C, SplitOld.first, I), SplitOld.second);
2479e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2480e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2481e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
2482e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size()) return QualType(Fn, 0);
2483e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2484e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      switch (static_cast<WrapKind>(Stack[I++])) {
2485e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Desugar:
2486e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // This is the point at which we potentially lose source
2487e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // information.
2488e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
2489e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2490e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Parens: {
2491e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
2492e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getParenType(New);
2493e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2494e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2495e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Pointer: {
2496e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
2497e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getPointerType(New);
2498e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2499e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2500e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case BlockPointer: {
2501e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
2502e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getBlockPointerType(New);
2503e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2504e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2505e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case MemberPointer: {
2506e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
2507e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldMPT->getPointeeType(), I);
2508e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getMemberPointerType(New, OldMPT->getClass());
2509e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2510e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2511e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Reference: {
2512e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const ReferenceType *OldRef = cast<ReferenceType>(Old);
2513e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldRef->getPointeeType(), I);
2514e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<LValueReferenceType>(OldRef))
2515e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
2516e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        else
2517e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getRValueReferenceType(New);
2518e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2519e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2520e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2521e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      llvm_unreachable("unknown wrapping kind");
2522e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return QualType();
2523e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2524e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  };
2525e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
2526e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2527711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
2528711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
2529711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
2530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
2531711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
2532711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
2533e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2534711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
25352455636163fdd18581d7fdae816433f886d88213Mike Stump
2536711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getKind() == AttributeList::AT_noreturn) {
2537711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
253804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
2539e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2540e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
2541711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
2542711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
2543425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
2544425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
2545711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
2546711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2547711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2548711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
2549425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
2550711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getKind() == AttributeList::AT_regparm) {
2551711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
2552711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
2553711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
25541e030eb1194763b42c1752723be23b1515f48981John McCall
2555711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
2556711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
2557008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
25581e030eb1194763b42c1752723be23b1515f48981John McCall
2559e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionType::ExtInfo EI =
2560711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
2561711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2562711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2563425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
2564425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
256504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
2566711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
2567711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
2568711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2569f82b4e85b1219295cad4b5851b035575bc293010John McCall
257004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Delay if the type didn't work out to a function.
2571711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!unwrapped.isFunctionType()) return false;
257204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2573711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
2574711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
2575064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis  if (S.Context.getCanonicalCallConv(CC) ==
2576e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      S.Context.getCanonicalCallConv(CCOld)) {
2577711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2578711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2579e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  }
258004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
258104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CCOld != CC_Default) {
258204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Should we diagnose reapplications of the same convention?
2583711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
258404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CC)
258504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CCOld);
2586711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2587711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
258804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
258904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
259004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
259104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
2592711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isa<FunctionNoProtoType>(fn)) {
2593711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_knr)
259404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
2595711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
2596711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
259704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
259804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2599711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
260004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
2601711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
260204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
2603711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
2604711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
260504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
260604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
2607f82b4e85b1219295cad4b5851b035575bc293010John McCall
2608711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
2609711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2610711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
2611f82b4e85b1219295cad4b5851b035575bc293010John McCall}
2612f82b4e85b1219295cad4b5851b035575bc293010John McCall
26136e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
26146e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
26156e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
26166e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
26176e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
26186e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
26196e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
2620788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
2621788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
262256affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
26236e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
26246e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2625e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
26266e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
26276e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
26286e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
26296e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
2630ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
2631ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
26326e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
26336e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << "vector_size" << sizeExpr->getSourceRange();
2634e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
26356e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
26366e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
26376e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
2638f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
26396e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
2640e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
26416e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
26426e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
26436e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
26446e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
26456e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
26466e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
26476e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
26486e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
26496e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
26506e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
2651e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
26526e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
26536e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
26546e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
26556e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
26566e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
2657e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
26586e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
26596e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
26606e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
26616e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
26626e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
2663788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
2664e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
26656e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
26666e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
26674211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
26684211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
26694211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
26704211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
26714211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
26724211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
26734211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
26744211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
26754211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
26764211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     VectorType::VectorKind VecKind,
26774211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const char *AttrName) {
26784211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
26794211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
26804211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
26814211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
26824211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
26834211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
26844211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
26854211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
26864211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
26874211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
26884211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
26894211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
26904211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      << AttrName << numEltsExpr->getSourceRange();
26914211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
26924211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
26934211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
26944211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
26954211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  const BuiltinType* BTy = CurType->getAs<BuiltinType>();
26964211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (!BTy ||
26974211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (VecKind == VectorType::NeonPolyVector &&
26984211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::SChar &&
26994211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short) ||
27004211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (BTy->getKind() != BuiltinType::SChar &&
27014211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UChar &&
27024211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short &&
27034211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UShort &&
27044211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Int &&
27054211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UInt &&
27064211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::LongLong &&
27074211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::ULongLong &&
27084211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Float)) {
27094211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
27104211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
27114211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
27124211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
27134211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
27144211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
27154211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
27164211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
27174211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
27184211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
27194211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
27204211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
27214211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
27224211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
27234211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
27244211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
27254211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
2726711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
2727711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             bool isDeclSpec, AttributeList *attrs) {
2728c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
2729c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
2730c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
2731c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
2732711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2733711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
2734711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
2735711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
2736711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr.getNext();
2737711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2738e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
2739711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
2740e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
2741e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
2742b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
2743b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
2744711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
2745c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
274604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2747c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
2748711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
2749c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
2750711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
2751711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
2752711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
2753d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
275404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    case AttributeList::AT_vector_size:
2755711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
2756f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
27574211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    case AttributeList::AT_neon_vector_type:
2758711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
2759711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonVector, "neon_vector_type");
27604211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
27614211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    case AttributeList::AT_neon_polyvector_type:
2762711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
2763711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonPolyVector,
27644211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                               "neon_polyvector_type");
27654211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
276604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2767711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
2768711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
2769711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
2770711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (isDeclSpec)
2771711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
2772711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2773711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
2774711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
2775711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
27766e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
2777c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
2778711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attrs = next));
2779232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
2780232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
27811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
27824ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
27834ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
27844ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
278586447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
278686447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
278786447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
278886447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
278986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
27904ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
27914ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
27924ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
27934ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
27944ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
27954ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
27961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @param PD The partial diagnostic that will be printed out if T is not a
2797b790661a15d93941d2c33a0ea328254277b3d7e3Anders Carlsson/// complete type.
27984ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
27994ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
28004ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
280191a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
28028c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               const PartialDiagnostic &PD,
28038c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               std::pair<SourceLocation,
28048c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                                         PartialDiagnostic> Note) {
280591a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  unsigned diag = PD.getDiagID();
28061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2807573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
2808573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
2809690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
2810690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
2811690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
28121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
2813690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
2814690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
28154ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
28164ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
28174ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
28184ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
2819d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
2820923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
2821923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
2822923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
282389c49f09b0292dc7c03885f6c765d667a9837597Douglas Gregor  if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
2824923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
2825923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
28262943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
2827d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
2828972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
2829972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
2830d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
28315842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor                                                      /*Complain=*/diag != 0);
28321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
2833d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
2834d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
2835b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
2836b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        assert(MSInfo && "Missing member specialization information?");
2837357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
2838b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        if (MSInfo->getTemplateSpecializationKind()
2839972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor                                               != TSK_ExplicitSpecialization)
2840f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
2841f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
2842f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
2843f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  /*Complain=*/diag != 0);
2844d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
2845d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
2846d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
28472943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
28485842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor  if (diag == 0)
28495842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
28501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2851916c870442978db40404d51348cdf5524e506faaJohn McCall  const TagType *Tag = T->getAs<TagType>();
285201620704304f819b82ecef769ec114e541a364d7Rafael Espindola
285301620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // Avoid diagnosing invalid decls as incomplete.
285401620704304f819b82ecef769ec114e541a364d7Rafael Espindola  if (Tag && Tag->getDecl()->isInvalidDecl())
285501620704304f819b82ecef769ec114e541a364d7Rafael Espindola    return true;
285601620704304f819b82ecef769ec114e541a364d7Rafael Espindola
2857916c870442978db40404d51348cdf5524e506faaJohn McCall  // Give the external AST source a chance to complete the type.
2858916c870442978db40404d51348cdf5524e506faaJohn McCall  if (Tag && Tag->getDecl()->hasExternalLexicalStorage()) {
2859916c870442978db40404d51348cdf5524e506faaJohn McCall    Context.getExternalSource()->CompleteType(Tag->getDecl());
2860916c870442978db40404d51348cdf5524e506faaJohn McCall    if (!Tag->isIncompleteType())
2861916c870442978db40404d51348cdf5524e506faaJohn McCall      return false;
2862916c870442978db40404d51348cdf5524e506faaJohn McCall  }
2863916c870442978db40404d51348cdf5524e506faaJohn McCall
28644ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
286591a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  Diag(Loc, PD) << T;
28663c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
28678c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  // If we have a note, produce it.
28688c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  if (!Note.first.isInvalid())
28698c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson    Diag(Note.first, Note.second);
28708c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson
28714ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
287201620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
28734ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
28741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
28754ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
28764ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
28774ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
28784ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
28794ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
28804ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
2881e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
2882fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2883fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               const PartialDiagnostic &PD) {
2884fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PD,
2885fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
2886fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
2887fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
2888fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2889fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               unsigned DiagID) {
2890fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PDiag(DiagID),
2891fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
2892fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
2893fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
2894465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
2895465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
2896465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
2897465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
2898465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
2899e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
2900465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
2901e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
2902465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
2903465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
2904465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
2905465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
2906465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
2907465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
2908465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
2909e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
2910af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
29112a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
29122a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  ExprResult ER = CheckPlaceholderExpr(E, Loc);
29132a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
29142a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
29152a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
29162b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
29172b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
2918aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
2919aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
29202b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
2921af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
2922af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
2923af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
29242a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
29252a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  ExprResult ER = CheckPlaceholderExpr(E, Loc);
29262a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
29272a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
29284b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
2929af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getDecltypeType(E);
2930af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
2931