SemaType.cpp revision 34b41d939a1328f484511c6002ba2456db879a29
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements type-related semantic analysis.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
142d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
157cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/Sema/Template.h"
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
17a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor#include "clang/AST/CXXInheritance.h"
18980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
192943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor#include "clang/AST/DeclTemplate.h"
204adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
2151bd803fbdade51d674598ed45da3d54190a656cJohn McCall#include "clang/AST/TypeLocVisitor.h"
22e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
2391a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
24d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis#include "clang/Basic/TargetInfo.h"
2519510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
264994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl#include "llvm/ADT/SmallPtrSet.h"
2787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor#include "llvm/Support/ErrorHandling.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// \brief Perform adjustment on the parameter type of a function.
312dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor///
322dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// This routine adjusts the given parameter type @p T to the actual
331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
352dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorQualType Sema::adjustParameterType(QualType T) {
362dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // C99 6.7.5.3p7:
37778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   A declaration of a parameter as "array of type" shall be
38778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   adjusted to "qualified pointer to type", where the type
39778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   qualifiers (if any) are those specified within the [ and ] of
40778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   the array type derivation.
41778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  if (T->isArrayType())
422dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getArrayDecayedType(T);
43778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner
44778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  // C99 6.7.5.3p8:
45778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   A declaration of a parameter as "function returning type"
46778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   shall be adjusted to "pointer to function returning type", as
47778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   in 6.3.2.1.
48778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  if (T->isFunctionType())
492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getPointerType(T);
502dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
512dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return T;
522dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
532dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
545db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
555db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
565db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// isOmittedBlockReturnType - Return true if this declarator is missing a
575db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// return type because this is a omitted return type on a block literal.
588ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic bool isOmittedBlockReturnType(const Declarator &D) {
595db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getContext() != Declarator::BlockLiteralContext ||
608ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl      D.getDeclSpec().hasTypeSpecifier())
615db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return false;
625db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
635db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 0)
64a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^{ ... }
655db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
665db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 1 &&
675db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      D.getTypeObject(0).Kind == DeclaratorChunk::Function)
68a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^(int X, float Y) { ... }
695db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
705db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  return false;
715db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner}
725db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
73711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// objc_gc applies to Objective-C pointers or, otherwise, to the
74711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// smallest available pointer type (i.e. 'void*' in 'void**').
75711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
76711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_objc_gc
77711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
78711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// Function type attributes.
79711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define FUNCTION_TYPE_ATTRS_CASELIST \
80711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_noreturn: \
81711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_cdecl: \
82711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_fastcall: \
83711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_stdcall: \
84711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_thiscall: \
85711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_pascal: \
86711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_regparm
87711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
88711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace {
89711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// An object which stores processing state for the entire
90711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// GetTypeForDeclarator process.
91711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  class TypeProcessingState {
92711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &sema;
93711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
94711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The declarator being processed.
95711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &declarator;
96711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
97711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The index of the declarator chunk we're currently processing.
98711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// May be the total number of valid chunks, indicating the
99711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// DeclSpec.
100711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex;
101711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
102711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Whether there are non-trivial modifications to the decl spec.
103711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    bool trivial;
104711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
105711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The original set of attributes on the DeclSpec.
106711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    llvm::SmallVector<AttributeList*, 2> savedAttrs;
107711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
108711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// A list of attributes to diagnose the uselessness of when the
109711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// processing is complete.
110711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    llvm::SmallVector<AttributeList*, 2> ignoredTypeAttrs;
111711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
112711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  public:
113711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    TypeProcessingState(Sema &sema, Declarator &declarator)
114711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : sema(sema), declarator(declarator),
115711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        chunkIndex(declarator.getNumTypeObjects()),
116711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        trivial(true) {}
117711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
118711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &getSema() const {
119711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return sema;
120711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
121711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
122711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &getDeclarator() const {
123711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator;
124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
125711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
126711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned getCurrentChunkIndex() const {
127711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return chunkIndex;
128711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
129711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
130711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void setCurrentChunkIndex(unsigned idx) {
131711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(idx <= declarator.getNumTypeObjects());
132711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      chunkIndex = idx;
133711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
134711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
135711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&getCurrentAttrListRef() const {
136711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(chunkIndex <= declarator.getNumTypeObjects());
137711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (chunkIndex == declarator.getNumTypeObjects())
138711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        return getMutableDeclSpec().getAttributes().getListRef();
139711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator.getTypeObject(chunkIndex).getAttrListRef();
140711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
141711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
142711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Save the current set of attributes on the DeclSpec.
143711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void saveDeclSpecAttrs() {
144711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Don't try to save them multiple times.
145711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!savedAttrs.empty()) return;
146711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
147711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      DeclSpec &spec = getMutableDeclSpec();
148711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (AttributeList *attr = spec.getAttributes().getList(); attr;
149711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             attr = attr->getNext())
150711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs.push_back(attr);
151711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      trivial &= savedAttrs.empty();
152711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
153711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
154711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Record that we had nowhere to put the given type attribute.
155711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// We will diagnose such attributes later.
156711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void addIgnoredTypeAttr(AttributeList &attr) {
157711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      ignoredTypeAttrs.push_back(&attr);
158711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
159711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
160711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Diagnose all the ignored type attributes, given that the
161711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// declarator worked out to the given type.
162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void diagnoseIgnoredTypeAttrs(QualType type) const {
163711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (llvm::SmallVectorImpl<AttributeList*>::const_iterator
164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall           i != e; ++i) {
166711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        AttributeList &attr = **i;
167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        getSema().Diag(attr.getLoc(), diag::warn_function_attribute_wrong_type)
168711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          << attr.getName() << type;
169711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      }
170711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
172711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    ~TypeProcessingState() {
173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (trivial) return;
174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
175711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      restoreDeclSpecAttrs();
176711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
177711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
178711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  private:
179711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclSpec &getMutableDeclSpec() const {
180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return const_cast<DeclSpec&>(declarator.getDeclSpec());
181711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
182711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
183711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void restoreDeclSpecAttrs() {
184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(!savedAttrs.empty());
185711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
186711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
187711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs[i]->setNext(savedAttrs[i+1]);
188711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      savedAttrs.back()->setNext(0);
189711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
190711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
191711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
192711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// Basically std::pair except that we really want to avoid an
193711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// implicit operator= for safety concerns.  It's also a minor
194711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// link-time optimization for this to be a private type.
195711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  struct AttrAndList {
196711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The attribute.
197711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &first;
198711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
199711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The head of the list the attribute is currently in.
200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&second;
201711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
202711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttrAndList(AttributeList &attr, AttributeList *&head)
203711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : first(attr), second(head) {}
204711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
20504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
20604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
207711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace llvm {
208711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  template <> struct isPodLike<AttrAndList> {
209711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    static const bool value = true;
210711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
211711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
212711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
213711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
214711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  attr.setNext(head);
215711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  head = &attr;
216711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
217711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
218711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
219711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (head == &attr) {
220711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    head = attr.getNext();
221711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
22204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
223711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
224711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *cur = head;
225711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  while (true) {
226711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    assert(cur && cur->getNext() && "ran out of attrs?");
227711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (cur->getNext() == &attr) {
228711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      cur->setNext(attr.getNext());
229711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
230711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
231711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    cur = cur->getNext();
232711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
233711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
234711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
235711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void moveAttrFromListToList(AttributeList &attr,
236711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&fromList,
237711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&toList) {
238711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, fromList);
239711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrIntoList(attr, toList);
240711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
241711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
242711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state,
243711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             QualType &type, bool isDeclSpec,
244711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             AttributeList *attrs);
245711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
246711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
247711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
248711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type);
249711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
250711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
251711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr, QualType &type);
252711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
253711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCPointerTypeAttr(TypeProcessingState &state,
254711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr, QualType &type) {
255711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Right now, we have exactly one of these attributes: objc_gc.
256711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(attr.getKind() == AttributeList::AT_objc_gc);
257711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return handleObjCGCTypeAttr(state, attr, type);
258711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
259711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
260711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Given that an objc_gc attribute was written somewhere on a
261711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declaration *other* than on the declarator itself (for which, use
262711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
263711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// didn't apply in whatever position it was written in, try to move
264711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// it to a more appropriate position.
265711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeObjCPointerTypeAttr(TypeProcessingState &state,
266711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          AttributeList &attr,
267711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType type) {
268711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
269711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
270711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
271711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
272711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
273711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
274711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
275711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
276711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
277711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
278711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
279711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
280711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
281711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
282711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Don't walk through these.
283711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
284711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
285711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
286711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto error;
287711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
288711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
289711c52bb20d0c69063b52a99826fb7d2835501f1John McCall error:
290711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
291711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.getSema().Diag(attr.getLoc(), diag::warn_function_attribute_wrong_type)
292711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    << attr.getName() << type;
293711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
294711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
295711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Distribute an objc_gc type attribute that was written on the
296711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator.
297711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
298711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
299711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            AttributeList &attr,
300711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            QualType &declSpecType) {
301711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
302711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
303711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // objc_gc goes on the innermost pointer to something that's not a
304711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // pointer.
305711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  unsigned innermost = -1U;
306711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  bool considerDeclSpec = true;
307711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
308711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
309711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
310711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
311711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
312711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      innermost = i;
313ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      continue;
314711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
315711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
316711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
317711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
318711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
319711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
320711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
321711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
322711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      considerDeclSpec = false;
323711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto done;
324711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
325711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
326711c52bb20d0c69063b52a99826fb7d2835501f1John McCall done:
327711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
328711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // That might actually be the decl spec if we weren't blocked by
329711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // anything in the declarator.
330711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (considerDeclSpec) {
331711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (handleObjCPointerTypeAttr(state, attr, declSpecType))
332711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
333711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
334711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
335711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, if we found an appropriate chunk, splice the attribute
336711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // into it.
337711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (innermost != -1U) {
338711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, declarator.getAttrListRef(),
339711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                       declarator.getTypeObject(innermost).getAttrListRef());
340711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
341711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
342711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
343711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, diagnose when we're done building the type.
344711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
345711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
346711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
347711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
348711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written somewhere in a declaration
349711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// *other* than on the declarator itself or in the decl spec.  Given
350711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// that it didn't apply in whatever position it was written in, try
351711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// to move it to a more appropriate position.
352711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeFunctionTypeAttr(TypeProcessingState &state,
353711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
354711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType type) {
355711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
356711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
357711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to push the attribute from the return type of a function to
358711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the function itself.
359711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
360711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
361711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
362711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
363711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
364711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
365711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
366711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
367711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
368711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
369711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
370711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
371711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
372711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
373711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
374711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
375711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
376711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
377711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.getSema().Diag(attr.getLoc(), diag::warn_function_attribute_wrong_type)
378711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    << attr.getName() << type;
379711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
380711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
381711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Try to distribute a function type attribute to the innermost
382711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// function chunk or type.  Returns true if the attribute was
383711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributed, false if no location was found.
384711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool
385711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
386711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr,
387711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList *&attrList,
388711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      QualType &declSpecType) {
389711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
390711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
391711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Put it on the innermost function chunk, if there is one.
392711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
393711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
394711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (chunk.Kind != DeclaratorChunk::Function) continue;
395711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
396711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
397711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
398711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
399711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
400711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return handleFunctionTypeAttr(state, attr, declSpecType);
401711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
402711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
403711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written in the decl spec.  Try to
404711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
405711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
406711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
407711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
408711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType &declSpecType) {
409711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.saveDeclSpecAttrs();
410711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
411711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
412711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            state.getCurrentAttrListRef(),
414711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
415711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
416711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
417711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
418711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
419711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
420711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
421711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
422711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written on the declarator.  Try to
423711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
424711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
425711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
426711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         AttributeList &attr,
427711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         QualType &declSpecType) {
428711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
429711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
430711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
431711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
432711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declarator.getAttrListRef(),
433711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
434711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
435711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
436711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
437711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
438711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
439711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
440711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
441711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
442711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// \brief Given that there are attributes written on the declarator
443711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// itself, try to distribute any type attributes to the appropriate
444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator chunk.
445711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///
446711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// These are attributes like the following:
447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f ATTR;
448711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int (f ATTR)();
449711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// but not necessarily this:
450711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f() ATTR;
451711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
452711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                              QualType &declSpecType) {
453711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Collect all the type attributes from the declarator itself.
454711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
455711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *attr = state.getDeclarator().getAttributes();
456711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
457711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
458711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr->getNext();
459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
460711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr->getKind()) {
461711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
462711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
463711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
465711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
466711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
467711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
468711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
469711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    default:
470711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
471711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
472711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attr = next));
473711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
474711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
475711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Add a synthetic '()' to a block-literal declarator if it is
476711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// required, given the return type.
477711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void maybeSynthesizeBlockSignature(TypeProcessingState &state,
478711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType declSpecType) {
479711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
480711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // First, check whether the declarator would produce a function,
482711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // i.e. whether the innermost semantic chunk is a function.
483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.isFunctionDeclarator()) {
484711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // If so, make that declarator a prototyped declarator.
485711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.getFunctionTypeInfo().hasPrototype = true;
486711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
487711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
488711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
489da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // If there are any type objects, the type as written won't name a
490da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // function, regardless of the decl spec type.  This is because a
491da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // block signature declarator is always an abstract-declarator, and
492da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // abstract-declarators can't just be parentheses chunks.  Therefore
493da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // we need to build a function chunk unless there are no type
494da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // objects and the decl spec type is a function.
495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
497711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
498da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // Note that there *are* cases with invalid declarators where
499da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // declarators consist solely of parentheses.  In general, these
500da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // occur only in failed efforts to make function declarators, so
501da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // faking up the function chunk is still the right thing to do.
502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, we need to fake up a function declarator.
504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation loc = declarator.getSourceRange().getBegin();
505711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
506711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // ...and *prepend* it to the declarator.
507711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
508711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             ParsedAttributes(),
509711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*proto*/ true,
510711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*variadic*/ false, SourceLocation(),
511711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*args*/ 0, 0,
512711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*type quals*/ 0,
51383f51722ed2b8134810cb178f39e44da811de7cdDouglas Gregor                             /*ref-qualifier*/true, SourceLocation(),
514711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*EH*/ false, SourceLocation(), false, 0, 0, 0,
515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*parens*/ loc, loc,
516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             declarator));
517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // For consistency, make sure the state still has us as processing
519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the decl spec.
520711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
521711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.setCurrentChunkIndex(declarator.getNumTypeObjects());
52204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
52304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
524930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
525930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
5265db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// \param D  the declarator containing the declaration specifier.
5275153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
5285153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
529711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic QualType ConvertDeclSpecToType(Sema &S, TypeProcessingState &state) {
5305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
532711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
534711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const DeclSpec &DS = declarator.getDeclSpec();
535711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation DeclLoc = declarator.getIdentifierLoc();
5365db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
5375db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    DeclLoc = DS.getSourceRange().getBegin();
5381564e3906cad604a42bd131e584751a75589a9c4Chris Lattner
539711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  ASTContext &Context = S.Context;
5401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5415db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
5425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
54396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
54496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
54596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
5465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
5475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
548fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
5495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
550fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
5515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
5535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
554fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
5555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
556958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
55764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
55864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
55964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
56064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
561711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
562f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
56364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
56464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
56564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
56664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
567711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
568f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
56964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
57064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
57164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
572f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
573f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
574f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
575f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
576f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
577f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
578f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
579f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
580f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
581f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
582d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
58362f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
584097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
585c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
586c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         (ObjCProtocolDecl**)PQ,
587c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         DS.getNumProtocolQualifiers());
588c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectPointerType(Result);
58962f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
59062f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
5915db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
5925db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
5935db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
594711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isOmittedBlockReturnType(declarator)) {
5955db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      Result = Context.DependentTy;
5965db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
5975db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
5981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
599d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
600d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
601d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
602d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
603d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
604d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
605d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
606711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.getLangOptions().ImplicitInt) {
60735d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
60835d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
6093f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
610711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_declspec)
6113f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
612849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int");
6133f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
6144310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
615d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
616d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
617d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
618d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
6194310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
620711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (S.getLangOptions().CPlusPlus &&
621711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          !S.getLangOptions().Microsoft) {
622711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_missing_type_specifier)
6233f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
625b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
626b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
627b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
628711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
629b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
630711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_type_specifier)
6313f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
632b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
633d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
6363cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
6385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
639fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
640fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
641fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
642311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
643311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
644311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
645311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
646711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        if (!S.getLangOptions().C99 &&
647711c52bb20d0c69063b52a99826fb7d2835501f1John McCall            !S.getLangOptions().CPlusPlus0x)
648711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
649311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
6505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
6515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
6525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
653fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
654fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
655fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
656311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
657311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
658311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
659311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
660711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        if (!S.getLangOptions().C99 &&
661711c52bb20d0c69063b52a99826fb7d2835501f1John McCall            !S.getLangOptions().CPlusPlus0x)
662711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
663311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
6645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
6655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
666958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
6673cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
668fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
669958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
670958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
671fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
672958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
673fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
67439d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne
67539d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    if (S.getLangOptions().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
67639d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
67739d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      declarator.setInvalidType(true);
67839d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    }
679958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
680fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
6815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
6825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
6835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
684711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
6858f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
686711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
6878f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
68899dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
6895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
6905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
6915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
692b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
6936e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
6946e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
6956e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
696711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
6976e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
6986e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
6996e24726524c2b51b31bb4b622aa678a46b024f42John McCall
700a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
701711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
702a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
7035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
704a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
705a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
7065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
707a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
7082191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
7092191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    // In C++, make an ElaboratedType.
710711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.getLangOptions().CPlusPlus) {
711465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      ElaboratedTypeKeyword Keyword
712465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara        = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
713711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
7142191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall    }
7155153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    if (D->isInvalidDecl())
716711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
717958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7191a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
7205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
7215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
7225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
723711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
72427940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    if (Result.isNull())
725711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
72627940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    else if (DeclSpec::ProtocolQualifierListTy PQ
72727940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall               = DS.getProtocolQualifiers()) {
728c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
729c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // Silently drop any existing protocol qualifiers.
730c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // TODO: determine whether that's the right thing to do.
731c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjT->getNumProtocols())
732c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = ObjT->getBaseType();
733c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
734c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (DS.getNumProtocolQualifiers())
735c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = Context.getObjCObjectType(Result,
736c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             (ObjCProtocolDecl**) PQ,
737c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             DS.getNumProtocolQualifiers());
738c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCIdType()) {
739ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
740c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
741c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
742c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
743c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
744c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCClassType()) {
7454262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
746c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
747c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
748c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
749c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
7503f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
751711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
7523f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
753711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
7543f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
755c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
7561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
758958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
760958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
761e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
762711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
763958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
764730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian    if (!Result->isDependentType())
765730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian      if (const TagType *TT = Result->getAs<TagType>())
766711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
767d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
768fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
769958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
770d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
771b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
772d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
773d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
774711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
7754b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
7764b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
777711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
7784b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
779958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
780d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
7816fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
782b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
7836fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
7846fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
785711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
786af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
787af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
788711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
789af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
7906fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
7916fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
792e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
793e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
79434b41d939a1328f484511c6002ba2456db879a29Richard Smith    Result = Context.getAutoType(QualType());
795e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
796e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
7971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
798809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
7995153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
800711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
8015153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
8025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
804958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
805f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
806711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.getLangOptions().Freestanding)
807711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
808fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
80982287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
81082287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
81182287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
812e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
813788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
814e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
815788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
816e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
817e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
818f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
8191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82047423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
82147423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
822711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
8231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
824711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
825711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
826711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
827711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
828711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
829711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
830711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
831711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
832711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (AttributeList *attrs = DS.getAttributes().getList())
833711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    processTypeAttrs(state, Result, true, attrs);
8341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
83596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
83696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
83796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
83896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
83996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
84096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
8410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (TypeQuals & DeclSpec::TQ_restrict) {
8422b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian      if (Result->isAnyPointerType() || Result->isReferenceType()) {
8432b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        QualType EltTy;
8442b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        if (Result->isObjCObjectPointerType())
8452b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result;
8462b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        else
8472b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result->isPointerType() ?
8482b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<PointerType>()->getPointeeType() :
8492b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<ReferenceType>()->getPointeeType();
8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
851bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
852bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
853bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
854711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getRestrictSpecLoc(),
855d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
856d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
8570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall          TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
858bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
859bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
860711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DS.getRestrictSpecLoc(),
861711c52bb20d0c69063b52a99826fb7d2835501f1John McCall               diag::err_typecheck_invalid_restrict_not_pointer)
862d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
8630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
86496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
86596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
8661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
86796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
86896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
86996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
87096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
87196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
87296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
8730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
87496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
8750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
87696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
8770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
8780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        assert((TypeQuals & DeclSpec::TQ_restrict) &&
8790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall               "Has CVR quals but not C, V, or R?");
8800953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Loc = DS.getRestrictSpecLoc();
88196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
882711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(Loc, diag::warn_typecheck_function_qualifiers)
883d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
88496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
8851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
886f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
887f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
888f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
889f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
890f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
8911a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
8921a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
893f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
8940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
8950953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
8961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
8971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
8990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Result = Context.getQualifiedType(Result, Quals);
90096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
9010953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
902f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
903f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
904f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
905cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
906cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
907cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
9081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
909cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
910cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
911cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
9122865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
9132865474261a608c7873b87ba4af110d17907896dJohn McCall                                  Qualifiers Qs) {
9142865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
9152865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
9162865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
9172865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
9182865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
9192865474261a608c7873b87ba4af110d17907896dJohn McCall
9202865474261a608c7873b87ba4af110d17907896dJohn McCall    const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
9212865474261a608c7873b87ba4af110d17907896dJohn McCall    if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
9222865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
9232865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9242865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
9252865474261a608c7873b87ba4af110d17907896dJohn McCall      }
9262865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
9272865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
9282865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9292865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
9302865474261a608c7873b87ba4af110d17907896dJohn McCall      }
9312865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
9322865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
9332865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9342865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
9352865474261a608c7873b87ba4af110d17907896dJohn McCall      }
9362865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (!Ty->isDependentType()) {
9372865474261a608c7873b87ba4af110d17907896dJohn McCall      // FIXME: this deserves a proper diagnostic
9382865474261a608c7873b87ba4af110d17907896dJohn McCall      DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9392865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
9402865474261a608c7873b87ba4af110d17907896dJohn McCall    }
9412865474261a608c7873b87ba4af110d17907896dJohn McCall
9422865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
9432865474261a608c7873b87ba4af110d17907896dJohn McCall      Diag(Loc, DiagID) << ProblemTy;
9442865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
9452865474261a608c7873b87ba4af110d17907896dJohn McCall    }
9462865474261a608c7873b87ba4af110d17907896dJohn McCall  }
9472865474261a608c7873b87ba4af110d17907896dJohn McCall
9482865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
9492865474261a608c7873b87ba4af110d17907896dJohn McCall}
9502865474261a608c7873b87ba4af110d17907896dJohn McCall
951075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
952075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
953075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
954075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
955075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
956cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
957cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
958cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
959cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
960cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
961cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
962cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
963cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
964cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
965cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
966cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
967cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
968cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
9692865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
970cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
971cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
972cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
973cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
974ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
975cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
976cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
977cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
978c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
97992e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
980cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
9812865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
982cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
983cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
984cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
985cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
986cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
987cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
988cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
989cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
990cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
991cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
992cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
993cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
994cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
995cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
996cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
99754e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
9982865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
99954e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
100069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // C++0x [dcl.ref]p6:
100169d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   If a typedef (7.1.3), a type template-parameter (14.3.1), or a
100269d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
100369d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   type T, an attempt to create the type "lvalue reference to cv TR" creates
100469d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   the type "lvalue reference to T", while an attempt to create the type
100569d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   "rvalue reference to cv TR" creates the type TR.
100654e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
100754e14c4db764c0636160d26c5bbf491637c83a76John McCall
100854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
100954e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
101054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
101154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
101254e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
101354e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
101454e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
101554e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
101654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
101754e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
101854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
101969d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // collapsing of references-to-references as described in C++0x.
102069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // DR 106 and 540 introduce reference-collapsing into C++98/03.
1021cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1022cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
102333a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
1024cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
1025cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
1026cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
1027cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1028cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1029cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1030cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
10317c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
10322865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
10332865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
1034cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1035cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1036cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
1037cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1038cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
1039cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1040cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
10411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
10421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
1043cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1044cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1045cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// array type or, if there is no such entity, the location of the
1046cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have array type.
1047cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1048cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
1049cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1050cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1051cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
1052cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
1053cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1054cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
10557e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
10560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10577e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
1058923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (getLangOptions().CPlusPlus) {
1059138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
1060138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
1061138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   type, the (possibly cv-qualified) type void, a function type or an
1062138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
1063138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
1064138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
1065138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
1066138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
1067138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
1068138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1069138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
1070138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1071923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (T->isVoidType()) {
1072923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1073923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1074923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1075138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1076138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (RequireNonAbstractType(Brackets.getBegin(), T,
1077138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
1078138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1079138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1080923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
1081138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1082138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
1083923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
1084923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
1085923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1086923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
1087cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1088cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
1089cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
1090ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1091cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1092cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
10931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
109434b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (T->getContainedAutoType()) {
109534b41d939a1328f484511c6002ba2456db879a29Richard Smith    Diag(Loc, diag::err_illegal_decl_array_of_auto)
109634b41d939a1328f484511c6002ba2456db879a29Richard Smith      << getPrintableNameForEntity(Entity) << T;
1097e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
1098e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
10991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11006217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
1101cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
1102cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
1103cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
1104cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
1105c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
1106c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1107c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
1108cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
11091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11105e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
11115e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  if (ArraySize && !ArraySize->isRValue())
11125e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall    DefaultLvalueConversion(ArraySize);
11135e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1114cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
11155e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // TODO: in theory, if we were insane, we could allow contextual
11165e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // conversions to integer type here.
1117cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (ArraySize && !ArraySize->isTypeDependent() &&
11181274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1119cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1120cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
1121cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1122cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
11232767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
1124cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
1125f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
11267e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
1127f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
1128f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
1129ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
11307e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
1131cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
1132923d56d436f750bc1f29db50e641078725558a1bSebastian Redl             (!T->isDependentType() && !T->isIncompleteType() &&
1133923d56d436f750bc1f29db50e641078725558a1bSebastian Redl              !T->isConstantSizeType())) {
1134cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Per C99, a variable array is an array with either a non-constant
1135cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // size or an element type that has a non-constant-size
11367e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1137cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
1138cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1139cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
1140923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
1141b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
1142b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1143b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1144b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
1145b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1146b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
1147923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1148923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1149923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
115002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
115102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
115202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      Diag(ArraySize->getLocStart(),
115302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
115402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
1155923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
11562767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
11572767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor               !T->isIncompleteType()) {
11582767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      // Is the array too large?
11592767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
11602767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
11612767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
11622767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
11632767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
11642767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
11651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
11662767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
116746a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
1168cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1169af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
1170af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  if (!getLangOptions().C99) {
11710fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
11720fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
1173204ce17e0cfd9bbe229627e1e5a20c3f2f587c8cDouglas Gregor      if (!T->isDependentType() &&
1174204ce17e0cfd9bbe229627e1e5a20c3f2f587c8cDouglas Gregor          !Context.getBaseElementType(T)->isPODType()) {
11750fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
11760fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor          << Context.getBaseElementType(T);
11770fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
11780fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      }
1179a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
1180a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
1181a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
1182a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
1183a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
11840fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
11850fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
11860fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::ext_vla);
11870fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
1188043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      Diag(Loc,
1189043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor           getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
1190043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor                                     : diag::ext_c99_array_usage);
1191cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1192cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1193cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
1194cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
11959cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
11969cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
11979cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
11989cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
11999ae2f076ca5ab1feb3ba95629099ec2319833701John McCallQualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
12009cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
12019cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
12029cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
12031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
12049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
12059cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
12069cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
12079cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
12089cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
12099ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
12109cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
12119ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
12129cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
12139ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        << "ext_vector_type" << ArraySize->getSourceRange();
12149cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
12159cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
12161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
12189cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
12191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12219cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
12229cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
12239ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      << ArraySize->getSourceRange();
12249cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
12259cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
12261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12279cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!T->isDependentType())
12289cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return Context.getExtVectorType(T, vectorSize);
12291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
12301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12319ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
12329cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
12331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1234724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
1235724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1236724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
1237724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
1238724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
12392943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
1240724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
1241724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1242724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
1243724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1244724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
1245724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
1246724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
1247724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1248724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
1249724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1250724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
1251724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1252724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
1253724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1254724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
1255724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
1256724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
1257724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1258724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
1259724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
1260724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1261724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
1262724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
1263724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
12641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 QualType *ParamTypes,
1265724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
1266724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 bool Variadic, unsigned Quals,
1267c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                                 RefQualifierKind RefQualifier,
1268fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
1269e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                 FunctionType::ExtInfo Info) {
1270724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
127158408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor    Diag(Loc, diag::err_func_returning_array_function)
127258408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
1273724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1274724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
12755291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
1276724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
1277724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
12782dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    QualType ParamType = adjustParameterType(ParamTypes[Idx]);
12792dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
1280724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
1281724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
1282724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
1283cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
128454e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
1285724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1286724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1287724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
1288724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1289724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1290e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
1291e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.Variadic = Variadic;
1292e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.TypeQuals = Quals;
1293c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor  EPI.RefQualifier = RefQualifier;
1294e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.ExtInfo = Info;
1295e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1296e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI);
1297724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
12981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1299949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
1300949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1301949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
1302949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
13030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR Qualifiers applied to the member pointer type
1304949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
1305949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
1306949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1307949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
1308949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
13091eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
13102865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
1311949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
1312949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
1313949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
1314949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
1315949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
1316949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1317949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
1318949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
1319949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1320949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
1321949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
1322949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
1323949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1324949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1325737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
1326949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
1327949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
13288d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
1329ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
1330949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1331949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1332949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1333949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
1334949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1335949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
1336949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1337949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1338949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1339949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
1340949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1341949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1342949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1343949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1344d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // In the Microsoft ABI, the class is allowed to be an incomplete
1345d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // type. In such cases, the compiler makes a worst-case assumption.
1346d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // We make no such assumption right now, so emit an error if the
1347d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // class isn't a complete type.
134820cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  if (Context.Target.getCXXABI() == CXXABI_Microsoft &&
1349d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis      RequireCompleteType(Loc, Class, diag::err_incomplete_type))
1350d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis    return QualType();
1351d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis
13522865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
1353949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
13541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13559a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
13569a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13579a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
13589a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
13609a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13619a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Loc The location of the entity whose type involves this
13629a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// block pointer type or, if there is no such entity, the location of the
13639a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type that will have block pointer type.
13649a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13659a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
13669a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
13679a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
13689a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
13699a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
13702865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildBlockPointerType(QualType T,
13711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
13729a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
13730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
13749a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
13759a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
13769a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
13771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13782865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
13799a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
13809a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
1381b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1382b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
13833f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
1384a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
13853f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
13863f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
13873f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1388a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
1389f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
1390e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
1391a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
1392e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
13931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1394a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
1395e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
1396e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
1397e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
139898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
13998ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl/// declarator to Type instances.
1400402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor///
1401402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
1402402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// owns the declaration of a type (e.g., the definition of a struct
1403402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// type), then *OwnedDecl will receive the owned declaration.
1404bf1a028246d884a540aeafa38e89be59a269b072John McCall///
1405bf1a028246d884a540aeafa38e89be59a269b072John McCall/// The result of this call will never be null, but the associated
1406bf1a028246d884a540aeafa38e89be59a269b072John McCall/// type may be a null type if there's an unrecoverable error.
1407bf1a028246d884a540aeafa38e89be59a269b072John McCallTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
140834b41d939a1328f484511c6002ba2456db879a29Richard Smith                                           TagDecl **OwnedDecl,
140934b41d939a1328f484511c6002ba2456db879a29Richard Smith                                           bool AutoAllowedInTypeName) {
1410930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
1411930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
1412930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
141305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  TypeSourceInfo *ReturnTypeInfo = 0;
1414711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1415711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  TypeProcessingState state(*this, D);
141604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
14173f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
14183f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_Identifier:
14193f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
14200486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
14213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
1422711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    T = ConvertDeclSpecToType(*this, state);
14235db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
1424591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
1425b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      TagDecl* Owned = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
1426b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      // Owned is embedded if it was defined here, or if it is the
1427b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      // very first (i.e., canonical) declaration of this tag type.
1428b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor      Owned->setEmbeddedInDeclarator(Owned->isDefinition() ||
1429b37b648b3f2bba4c557a1604ced19b526b25a372Douglas Gregor                                     Owned->isCanonicalDecl());
1430591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor      if (OwnedDecl) *OwnedDecl = Owned;
1431591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
1432930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
1433930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
14343f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
14350efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
14363f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
1437930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
143848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // "void" instead.
1439930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
1440930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
144148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
144248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
144348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
144448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
144505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    T = GetTypeFromParser(D.getName().ConversionFunctionId,
1446bf1a028246d884a540aeafa38e89be59a269b072John McCall                          &ReturnTypeInfo);
144748026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
1448930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
1449dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
1450711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
1451711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
1452711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
145334b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
145434b41d939a1328f484511c6002ba2456db879a29Richard Smith      !D.isFunctionDeclarator()) {
1455baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
14561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1457baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
1458baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
1459baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      assert(0 && "K&R type lists aren't allowed in C++");
1460baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1461baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
1462baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
1463baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1464baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
1465baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      switch (cast<TagDecl>(CurContext)->getTagKind()) {
1466465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Enum: assert(0 && "unhandled tag kind"); break;
1467465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
1468465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
1469465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
14701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
1471baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1472baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
1473baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 4; // Exception declaration
1474baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1475baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
1476baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 5; // Template parameter
1477baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1478baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
147934b41d939a1328f484511c6002ba2456db879a29Richard Smith      Error = 6; // Block literal
148034b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
148134b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TemplateTypeArgContext:
148234b41d939a1328f484511c6002ba2456db879a29Richard Smith      Error = 7; // Template type argument
148334b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
148434b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TypeNameContext:
148534b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!AutoAllowedInTypeName)
148634b41d939a1328f484511c6002ba2456db879a29Richard Smith        Error = 8; // Generic
1487baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1488baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
1489baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
1490baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
1491baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
1492baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1493baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1494baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
1495baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
1496baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
1497baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
1498baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      T = Context.IntTy;
1499baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
1500baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1501baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
15021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
150334b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (T.isNull())
150434b41d939a1328f484511c6002ba2456db879a29Richard Smith    return Context.getNullTypeSourceInfo();
150534b41d939a1328f484511c6002ba2456db879a29Richard Smith
1506cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
1507cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
1508cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
1509cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
15101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
151198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
151298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
151398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
15148ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
1516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
1517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
1520075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
1521075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      T = BuildParenType(T);
1522075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
15235618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
15249af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
15259af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
15269af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner        Diag(DeclType.Loc, diag::err_blocks_disable);
15271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15282865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
15292865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
15302865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
15315618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
15325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
15336a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
15346a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
15356a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
15366a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
15376a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
15386a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
15396a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
1540c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) {
1541c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
15422865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
15432865474261a608c7873b87ba4af110d17907896dJohn McCall          T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
154414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
154514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
15462865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildPointerType(T, DeclType.Loc, Name);
15472865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
15482865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
1549711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
15505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
15510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
15526a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
15536a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
15546a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
15556a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
15566a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
15576a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
15586a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
15592865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
15602865474261a608c7873b87ba4af110d17907896dJohn McCall
15612865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
15622865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
15632865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
15645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
15650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
15665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
15676a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
15686a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
15696a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
15706a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
15716a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
15726a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
15736a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
1574fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
157594f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
15765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
15775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
15785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
15795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
15805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
15815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
15825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
1583f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      if (ASM == ArrayType::Star &&
1584f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman          D.getContext() != Declarator::PrototypeContext) {
1585f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
1586f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
1587f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1588f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1589f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
1590f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
1591f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
15920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      T = BuildArrayType(T, ASM, ArraySize,
15930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                         Qualifiers::fromCVRMask(ATI.TypeQuals),
15947e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                         SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
15965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1597f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
15985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
16005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
16023cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
1603cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      // C99 6.7.5.3p1: The return type may not be a function or array type.
160458408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      // For conversion functions, we'll diagnose this particular error later.
160548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor      if ((T->isArrayType() || T->isFunctionType()) &&
160648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
160798650449dc769dd6217f183c846dcaf9e6f94930Argyrios Kyrtzidis        unsigned diagID = diag::err_func_returning_array_function;
1608a4356adfd4a79bd63f86e2b30878795ce7b9b0a6Argyrios Kyrtzidis        // Last processing chunk in block context means this function chunk
1609a4356adfd4a79bd63f86e2b30878795ce7b9b0a6Argyrios Kyrtzidis        // represents the block.
1610a4356adfd4a79bd63f86e2b30878795ce7b9b0a6Argyrios Kyrtzidis        if (chunkIndex == 0 &&
1611a4356adfd4a79bd63f86e2b30878795ce7b9b0a6Argyrios Kyrtzidis            D.getContext() == Declarator::BlockLiteralContext)
161298650449dc769dd6217f183c846dcaf9e6f94930Argyrios Kyrtzidis          diagID = diag::err_block_returning_array_function;
161398650449dc769dd6217f183c846dcaf9e6f94930Argyrios Kyrtzidis        Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
1614cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        T = Context.IntTy;
1615cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner        D.setInvalidType(true);
1616cd8812948bc8a65dcf10c541c1775e5ba44def6cChris Lattner      }
1617465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
161834b41d939a1328f484511c6002ba2456db879a29Richard Smith      // Check for auto functions and trailing return type and adjust the
161934b41d939a1328f484511c6002ba2456db879a29Richard Smith      // return type accordingly.
162034b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!D.isInvalidType()) {
162134b41d939a1328f484511c6002ba2456db879a29Richard Smith        // trailing-return-type is only required if we're declaring a function,
162234b41d939a1328f484511c6002ba2456db879a29Richard Smith        // and not, for instance, a pointer to a function.
162334b41d939a1328f484511c6002ba2456db879a29Richard Smith        if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
162434b41d939a1328f484511c6002ba2456db879a29Richard Smith            !FTI.TrailingReturnType && chunkIndex == 0) {
162534b41d939a1328f484511c6002ba2456db879a29Richard Smith          Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
162634b41d939a1328f484511c6002ba2456db879a29Richard Smith               diag::err_auto_missing_trailing_return);
162734b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = Context.IntTy;
162834b41d939a1328f484511c6002ba2456db879a29Richard Smith          D.setInvalidType(true);
162934b41d939a1328f484511c6002ba2456db879a29Richard Smith        } else if (FTI.TrailingReturnType) {
163034b41d939a1328f484511c6002ba2456db879a29Richard Smith          if (T.hasQualifiers() || !isa<AutoType>(T)) {
163134b41d939a1328f484511c6002ba2456db879a29Richard Smith            // T must be exactly 'auto' at this point. See CWG issue 681.
163234b41d939a1328f484511c6002ba2456db879a29Richard Smith            Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
163334b41d939a1328f484511c6002ba2456db879a29Richard Smith                 diag::err_trailing_return_without_auto)
163434b41d939a1328f484511c6002ba2456db879a29Richard Smith              << T << D.getDeclSpec().getSourceRange();
163534b41d939a1328f484511c6002ba2456db879a29Richard Smith            D.setInvalidType(true);
163634b41d939a1328f484511c6002ba2456db879a29Richard Smith          }
163734b41d939a1328f484511c6002ba2456db879a29Richard Smith
163834b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = GetTypeFromParser(
163934b41d939a1328f484511c6002ba2456db879a29Richard Smith            ParsedType::getFromOpaquePtr(FTI.TrailingReturnType),
164034b41d939a1328f484511c6002ba2456db879a29Richard Smith            &ReturnTypeInfo);
164134b41d939a1328f484511c6002ba2456db879a29Richard Smith        }
164234b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
164334b41d939a1328f484511c6002ba2456db879a29Richard Smith
16445291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
16455291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
16465291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
16475291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          (!getLangOptions().CPlusPlus ||
16485291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor           (!T->isDependentType() && !T->isRecordType()))) {
16495291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        unsigned Quals = D.getDeclSpec().getTypeQualifiers();
1650de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        std::string QualStr;
1651de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        unsigned NumQuals = 0;
16525291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        SourceLocation Loc;
1653de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        if (Quals & Qualifiers::Const) {
16545291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          Loc = D.getDeclSpec().getConstSpecLoc();
1655de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          ++NumQuals;
1656de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          QualStr = "const";
1657de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        }
1658de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        if (Quals & Qualifiers::Volatile) {
1659de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          if (NumQuals == 0) {
1660de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            Loc = D.getDeclSpec().getVolatileSpecLoc();
1661de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            QualStr = "volatile";
1662de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          } else
1663de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            QualStr += " volatile";
1664de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          ++NumQuals;
1665de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        }
1666de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        if (Quals & Qualifiers::Restrict) {
1667de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          if (NumQuals == 0) {
1668de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            Loc = D.getDeclSpec().getRestrictSpecLoc();
1669de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            QualStr = "restrict";
1670de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          } else
1671de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor            QualStr += " restrict";
1672de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor          ++NumQuals;
16735291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        }
1674de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        assert(NumQuals > 0 && "No known qualifiers?");
1675de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor
16765291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        SemaDiagnosticBuilder DB = Diag(Loc, diag::warn_qual_return_type);
1677de80ec1fa947855d2e53722a8cd71367ff513481Douglas Gregor        DB << QualStr << NumQuals;
16785291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        if (Quals & Qualifiers::Const)
16795291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          DB << FixItHint::CreateRemoval(D.getDeclSpec().getConstSpecLoc());
16805291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        if (Quals & Qualifiers::Volatile)
16815291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          DB << FixItHint::CreateRemoval(D.getDeclSpec().getVolatileSpecLoc());
16825291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor        if (Quals & Qualifiers::Restrict)
16835291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          DB << FixItHint::CreateRemoval(D.getDeclSpec().getRestrictSpecLoc());
16845291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      }
16855291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
1686402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1687402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
1688402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
1689b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
1690402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        if (Tag->isDefinition())
1691402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor          Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1692402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
1693402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
1694402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
16953cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
16963cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
16973cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      if (FTI.hasExceptionSpec &&
16983cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl          D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
16993cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl        Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
17003cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
17012865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) {
17022865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
17032865474261a608c7873b87ba4af110d17907896dJohn McCall        T = Context.getFunctionNoProtoType(T);
17042865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
17052865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
17062865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
17072865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
17082865474261a608c7873b87ba4af110d17907896dJohn McCall        if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) {
1709965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
1710965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
1711965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
1712965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
1713965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
1714965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
1715965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
1716965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
1717965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
1718965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
1719965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1720c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
17212865474261a608c7873b87ba4af110d17907896dJohn McCall
17222865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
1723788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
1724788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
17252865474261a608c7873b87ba4af110d17907896dJohn McCall          Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
17262865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
17272865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
17282865474261a608c7873b87ba4af110d17907896dJohn McCall        }
17292865474261a608c7873b87ba4af110d17907896dJohn McCall
1730e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
1731e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
1732e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
1733c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
1734c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : FTI.RefQualifierIsLValueRef? RQ_LValue
1735c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : RQ_RValue;
1736c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
17375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
17385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
17395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
17402865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
17411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1743d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
17448123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
174578c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
17462dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
17472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
1748beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor          assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
17492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
17505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
17515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
175272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
17532dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
17545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
17555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
17565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
17575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
17585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
17592ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
17608123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
17612ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
17622ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
17635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
17644565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
17652ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
17668123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
17672ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
17682ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
17690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
17702ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
17711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17722ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
17732ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
17742ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
1775eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
1776eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
1777a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
1778183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
1779eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman              if (BTy->getKind() == BuiltinType::Float)
1780eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
1781eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
17825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
178356a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
178454e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
17855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
1786465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1787465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        llvm::SmallVector<QualType, 4> Exceptions;
1788e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        if (FTI.hasExceptionSpec) {
1789e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          EPI.HasExceptionSpec = FTI.hasExceptionSpec;
1790e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          EPI.HasAnyExceptionSpec = FTI.hasAnyExceptionSpec;
1791e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          Exceptions.reserve(FTI.NumExceptions);
1792e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1793e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // FIXME: Preserve type source info.
1794e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1795e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // Check that the type is valid for an exception spec, and
1796e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // drop it if not.
1797e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1798e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall              Exceptions.push_back(ET);
1799e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
1800373920bd733b1d28fe7bf209945a62eb9248d948John McCall          EPI.NumExceptions = Exceptions.size();
1801e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          EPI.Exceptions = Exceptions.data();
1802ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl        }
1803465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1804e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI);
18055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
180604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
18075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
18085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1809f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
1810f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
18117bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
1812f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
18137bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
1814edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
1815edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
18167bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      } else if (isDependentScopeSpecifier(SS) ||
18177bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara                 dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS))) {
18181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
18197bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
182087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
182187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
182287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
18237bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
18244a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
182587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
182687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
182787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
182887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
18299f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
183087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
18317bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
183287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
183387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
183487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
18357bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // Note: if NNS is dependent, then its prefix (if any) is already
18367bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // included in ClsType; this does not hold if the NNS is
18377bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // nondependent: in this case (if there is indeed a prefix)
18387bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          // ClsType needs to be wrapped into an elaborated type.
18397bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          if (NNSPrefix && !NNS->isDependent())
18407bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
184187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
184287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
1843f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
1844949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        Diag(DeclType.Mem.Scope().getBeginLoc(),
1845949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
1846949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1847949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
1848f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
1849f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1850f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1851949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
18522865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
1853949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
1854f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
1855949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
18562865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
18572865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
1858f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
1859f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
1860f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
1861f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1862cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
1863cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
1864cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
1865cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
1866cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1867c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
1868711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
1869711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      processTypeAttrs(state, T, false, attrs);
18705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1871971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1872971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
1873183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
1874778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
1875971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
1876708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    // C++ 8.3.5p4:
1877708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
1878708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
1879708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
1880708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
1881683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    //
1882683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // Core issue 547 also allows cv-qualifiers on function types that are
1883683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // top-level template type arguments.
1884613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
1885613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
1886613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (D.getContext() != Declarator::MemberContext ||
1887613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall                      D.getDeclSpec().isFriendSpecified());
1888613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
1889613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      DeclContext *DC = computeDeclContext(D.getCXXScopeSpec());
1890613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
1891613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
1892613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
1893c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    // C++0x [dcl.fct]p6:
1894c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   A ref-qualifier shall only be part of the function type for a
1895c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   non-static member function, the function type to which a pointer to
1896c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   member refers, or the top-level function type of a function typedef
1897c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   declaration.
1898c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    if ((FnTy->getTypeQuals() != 0 || FnTy->getRefQualifier()) &&
1899683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        !(D.getContext() == Declarator::TemplateTypeArgContext &&
1900683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          !D.isFunctionDeclarator()) &&
1901971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
1902c61bb2056148891375bfa591fa2859b9b6ec2734Sebastian Redl        (FreeFunction ||
1903971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
1904683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      if (D.getContext() == Declarator::TemplateTypeArgContext) {
1905683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        // Accept qualified function types as template type arguments as a GNU
1906683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        // extension. This is also the subject of C++ core issue 547.
1907683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        std::string Quals;
1908683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        if (FnTy->getTypeQuals() != 0)
1909683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          Quals = Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
1910683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
1911683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        switch (FnTy->getRefQualifier()) {
1912683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        case RQ_None:
1913683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          break;
1914683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
1915683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        case RQ_LValue:
1916683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (!Quals.empty())
1917683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Quals += ' ';
1918683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          Quals += '&';
1919683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          break;
1920c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
1921683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        case RQ_RValue:
1922683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (!Quals.empty())
1923683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Quals += ' ';
1924683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          Quals += "&&";
1925683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          break;
1926683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
1927683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
1928683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        Diag(D.getIdentifierLoc(),
1929683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor             diag::ext_qualified_function_type_template_arg)
1930683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          << Quals;
1931683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      } else {
1932683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        if (FnTy->getTypeQuals() != 0) {
1933683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (D.isFunctionDeclarator())
1934683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Diag(D.getIdentifierLoc(),
1935683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                 diag::err_invalid_qualified_function_type);
1936683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          else
1937683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Diag(D.getIdentifierLoc(),
1938683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                 diag::err_invalid_qualified_typedef_function_type_use)
1939683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << FreeFunction;
1940683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
1941683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
1942683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        if (FnTy->getRefQualifier()) {
1943683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (D.isFunctionDeclarator()) {
1944683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            SourceLocation Loc = D.getIdentifierLoc();
1945683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
1946683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              const DeclaratorChunk &Chunk = D.getTypeObject(N-I-1);
1947683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              if (Chunk.Kind == DeclaratorChunk::Function &&
1948683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                  Chunk.Fun.hasRefQualifier()) {
1949683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                Loc = Chunk.Fun.getRefQualifierLoc();
1950683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                break;
1951683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              }
1952944aa60777e6ea1015c9423107f7925f6d91f4a0Douglas Gregor            }
1953944aa60777e6ea1015c9423107f7925f6d91f4a0Douglas Gregor
1954683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Diag(Loc, diag::err_invalid_ref_qualifier_function_type)
1955683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << (FnTy->getRefQualifier() == RQ_LValue)
1956683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << FixItHint::CreateRemoval(Loc);
1957683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          } else {
1958683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Diag(D.getIdentifierLoc(),
1959683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                 diag::err_invalid_ref_qualifier_typedef_function_type_use)
1960683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << FreeFunction
1961683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << (FnTy->getRefQualifier() == RQ_LValue);
1962683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          }
1963c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        }
1964c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
1965683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        // Strip the cv-qualifiers and ref-qualifiers from the type.
1966683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
1967683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        EPI.TypeQuals = 0;
1968683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        EPI.RefQualifier = RQ_None;
1969c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
1970683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        T = Context.getFunctionType(FnTy->getResultType(),
1971683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                                    FnTy->arg_type_begin(),
1972683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                                    FnTy->getNumArgs(), EPI);
1973683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      }
1974971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
1975971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
19761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1977711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
1978711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull())
1979711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = D.getAttributes())
1980711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      processTypeAttrs(state, T, false, attrs);
1981711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1982711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
1983711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
1984711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1985737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // If there's a constexpr specifier, treat it as a top-level const.
1986737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  if (D.getDeclSpec().isConstexprSpecified()) {
1987737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
1988737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
1989737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
1990a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // If there was an ellipsis in the declarator, the declaration declares a
1991a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
1992a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (D.hasEllipsis() && !T.isNull()) {
1993a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
1994a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   A declarator-id or abstract-declarator containing an ellipsis shall
1995a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
1996a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
1997a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
1998a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
1999a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
2000a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   [...] When it is part of a parameter-declaration-clause, the
2001a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   parameter pack is a function parameter pack (14.5.3). The type T
2002a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
2003a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   a template parameter pack; each template parameter pack in T is
2004a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
2005a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2006a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
2007a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
2008a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
2009a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        Diag(D.getEllipsisLoc(),
2010a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
2011a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
2012a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
2013a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
2014cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
2015a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
2016a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2017a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
2018a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
2019a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
2020a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   If a template-parameter is a [...] is a parameter-declaration that
2021a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
2022a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
2023a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2024a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
2025a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
2026a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
2027a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
2028cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
202910738d36b150aa65206890c1c845cdba076e4200Douglas Gregor      else if (!getLangOptions().CPlusPlus0x)
20305ce5f5221217b64193799c2a4d5aa84432d3fba4Douglas Gregor        Diag(D.getEllipsisLoc(), diag::ext_variadic_templates);
2031a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2032a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
2033a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
2034a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
2035a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
2036a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
2037a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
2038a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
2039a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
2040a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
2041a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
2042683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    case Declarator::TemplateTypeArgContext:
2043a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
2044a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
2045a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
2046a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
2047a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2048a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
2049a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
2050a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
2051bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
2052bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
2053bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
2054bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
2055bf1a028246d884a540aeafa38e89be59a269b072John McCall  return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo);
20565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
205851bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
205951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
2060c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    ASTContext &Context;
206151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
2062f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
206351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
2064c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
2065c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      : Context(Context), DS(DS) {}
2066f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
206751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
206851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
206951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
207051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
207151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
207251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
207351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
207451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
2075c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
2076c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2077c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
2078c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
2079c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
2080c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.getBaseLoc().initialize(Context, SourceLocation());
2081c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
2082c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
2083c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
2084c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
208554e14c4db764c0636160d26c5bbf491637c83a76John McCall
2086c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
208754e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
208854e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
208954e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
209054e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
209154e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
209254e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
209354e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
209454e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
209554e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
209654e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
209754e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
209854e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
209951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
210054e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
210154e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
2102c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
210351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
2104833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
2105a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
2106b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2107833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2108833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
2109833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
2110a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
2111c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.initialize(Context, DS.getTypeSpecTypeLoc());
2112833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
2113833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
2114833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2115e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
2116e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
2117e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
2118e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TemplateSpecializationTypeLoc NamedTL =
2119e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
2120e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
2121e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2122e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      else
2123e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
2124833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
2125cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
2126cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
2127cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2128cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
2129cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
2130cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
2131cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
2132cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2133cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
2134b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
2135cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
2136b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2137cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
2138cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
2139ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
2140ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
2141ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
2142ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
2143ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
2144ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
2145ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
2146ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
2147ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
2148ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
2149ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
2150ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
2151ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
2152ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
2153ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
2154e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
2155e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
2156e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
2157253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
2158e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
2159b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2160e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
2161e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
2162e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
2163e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
2164e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2165e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setKeywordLoc(Keyword != ETK_None
2166e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       ? DS.getTypeSpecTypeLoc()
2167e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       : SourceLocation());
2168e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
2169e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setQualifierRange(SS.isEmpty() ? SourceRange(): SS.getRange());
2170e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
2171e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
2172e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
2173e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
2174e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
2175253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
2176e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
2177b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2178e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
2179e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
2180e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
2181e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
2182e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2183e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setKeywordLoc(Keyword != ETK_None
2184e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       ? DS.getTypeSpecTypeLoc()
2185e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       : SourceLocation());
2186e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
2187e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
2188e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      // FIXME: load appropriate source location.
218933500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
219033500955d731c73717af52088b7fc0e7a85681e7John McCall    }
219133500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
219233500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
219333500955d731c73717af52088b7fc0e7a85681e7John McCall      ElaboratedTypeKeyword Keyword
219433500955d731c73717af52088b7fc0e7a85681e7John McCall        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
219533500955d731c73717af52088b7fc0e7a85681e7John McCall      if (Keyword == ETK_Typename) {
219633500955d731c73717af52088b7fc0e7a85681e7John McCall        TypeSourceInfo *TInfo = 0;
2197b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
219833500955d731c73717af52088b7fc0e7a85681e7John McCall        if (TInfo) {
219933500955d731c73717af52088b7fc0e7a85681e7John McCall          TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
220033500955d731c73717af52088b7fc0e7a85681e7John McCall                    TInfo->getTypeLoc()));
220133500955d731c73717af52088b7fc0e7a85681e7John McCall          return;
220233500955d731c73717af52088b7fc0e7a85681e7John McCall        }
220333500955d731c73717af52088b7fc0e7a85681e7John McCall      }
2204c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initializeLocal(Context, SourceLocation());
220533500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setKeywordLoc(Keyword != ETK_None
220633500955d731c73717af52088b7fc0e7a85681e7John McCall                       ? DS.getTypeSpecTypeLoc()
220733500955d731c73717af52088b7fc0e7a85681e7John McCall                       : SourceLocation());
220833500955d731c73717af52088b7fc0e7a85681e7John McCall      const CXXScopeSpec& SS = DS.getTypeSpecScope();
220933500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
221033500955d731c73717af52088b7fc0e7a85681e7John McCall      // FIXME: load appropriate source location.
2211e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeLoc());
2212e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
2213e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
221451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
221551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
2216c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initialize(Context, DS.getTypeSpecTypeLoc());
221751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
221851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
2219eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
222051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
222151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
2222f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
222351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
222451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
22254adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
222651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
22279f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
222851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
22294adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
223051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
223151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
223251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
22334adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
223451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
223551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
223651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
22374adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
223851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
223951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
224051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
22414adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
224251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
224351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
224451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
224551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: nested name specifier
22464adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
224751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
224851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
224954e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
225054e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
225151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
225251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
225351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
225451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
225551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
225651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
225751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
225851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
225951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
226051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
226151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
226251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
226351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
226451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
226551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
226651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLParenLoc(Chunk.Loc);
226751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRParenLoc(Chunk.EndLoc);
2268dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor      TL.setTrailingReturn(!!Chunk.Fun.TrailingReturnType);
226951bd803fbdade51d674598ed45da3d54190a656cJohn McCall
227051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
227154e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
2272d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
227354e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
22744adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
227551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
22764adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
2277075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
2278075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
2279075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
2280075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
2281075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
22821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
228351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
22849f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
22854adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
228651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
228751bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
22884adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
2289a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
229051bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
229151bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
229205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
229305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
229405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
229505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
229605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
2297a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
229805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
229905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
2300a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
2301a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
230251bd803fbdade51d674598ed45da3d54190a656cJohn McCall
2303a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
2304a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
2305a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc());
2306a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
2307a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
2308a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
23098ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
231051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
231151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
23124adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
2313f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
2314b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
2315b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
2316b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
231705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
231805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
231905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
2320b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
2321c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
232205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
232305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor
2324a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
23254adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
23264adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
2327a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
2328b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
23291bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
23301bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
23311bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
2332eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
2333eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
2334a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
23351bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
23361bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
2337b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
23381bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
23391bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
23401bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
23411bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
234235d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis  assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
234335d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
234435d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
23451bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
23461bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
2347f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
23485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
23495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
23505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
23511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2352402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  TagDecl *OwnedTag = 0;
2353bf1a028246d884a540aeafa38e89be59a269b072John McCall  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag);
2354bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
23555153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
2356809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
23575912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
2358402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  if (getLangOptions().CPlusPlus) {
2359402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
23606d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
23616d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
2362402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // C++0x [dcl.type]p3:
2363402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   A type-specifier-seq shall not define a class or enumeration
2364402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   unless it appears in the type-id of an alias-declaration
2365402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    //   (7.1.3).
2366402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    if (OwnedTag && OwnedTag->isDefinition())
2367402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
2368402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        << Context.getTypeDeclType(OwnedTag);
2369402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
2370402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
2371b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
23725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
23735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2374c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
2375c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
2376c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
2377c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
2378c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
2379232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
2380232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
2381c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
2382c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
23831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
2384c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
23850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
2386232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
2387232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
2388232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
2389232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
2390c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
2391e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2392c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
2393232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
23941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2395232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
2396545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
2397f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2398e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2399c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
2400232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
2401545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
2402232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
2403ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
2404ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
2405dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
2406dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
2407e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2408c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
2409232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
2410232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
2411efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
2412efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
2413efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
2414efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
2415efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
2416e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
2417efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
2418efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
2419efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
2420efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
2421efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
24220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
2423efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
2424efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
24250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
2426e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2427efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
2428efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
2429efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
24301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
2431f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
2432c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
2433c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
2434711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
2435711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
2436711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
2437711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
2438711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
2439711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
2440711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
2441711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
2442711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2443711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
2444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
2445711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
2446711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
2447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
2448711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2449711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
2450711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
2451711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2452711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2453d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
24541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2455d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
2456711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!attr.getParameterName()) {
2457711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2458ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
2459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2460711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2461ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
24620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
2463711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getNumArgs() != 0) {
2464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2465711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2466711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2467d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
2468711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getParameterName()->isStr("weak"))
24690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
2470711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  else if (attr.getParameterName()->isStr("strong"))
24710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
2472d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
2473711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
2474711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      << "objc_gc" << attr.getParameterName();
2475711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2476711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2477d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
24781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2479711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  type = S.Context.getObjCGCQualType(type, GCAttr);
2480711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
2481d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
2482d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
2483e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallnamespace {
2484e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// A helper class to unwrap a type down to a function for the
2485e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// purposes of applying attributes there.
2486e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///
2487e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// Use:
2488e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
2489e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   if (unwrapped.isFunctionType()) {
2490e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     const FunctionType *fn = unwrapped.get();
2491e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     // change fn somehow
2492e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     T = unwrapped.wrap(fn);
2493e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   }
2494e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  struct FunctionTypeUnwrapper {
2495e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    enum WrapKind {
2496e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Desugar,
2497e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Parens,
2498e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Pointer,
2499e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      BlockPointer,
2500e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Reference,
2501e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      MemberPointer
2502e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    };
2503e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2504e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType Original;
2505e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *Fn;
2506e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    llvm::SmallVector<unsigned char /*WrapKind*/, 8> Stack;
2507e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2508e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
2509e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      while (true) {
2510e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const Type *Ty = T.getTypePtr();
2511e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<FunctionType>(Ty)) {
2512e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Fn = cast<FunctionType>(Ty);
2513e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return;
2514e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ParenType>(Ty)) {
2515e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ParenType>(Ty)->getInnerType();
2516e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Parens);
2517e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<PointerType>(Ty)) {
2518e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<PointerType>(Ty)->getPointeeType();
2519e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Pointer);
2520e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<BlockPointerType>(Ty)) {
2521e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<BlockPointerType>(Ty)->getPointeeType();
2522e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(BlockPointer);
2523e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<MemberPointerType>(Ty)) {
2524e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<MemberPointerType>(Ty)->getPointeeType();
2525e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(MemberPointer);
2526e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ReferenceType>(Ty)) {
2527e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ReferenceType>(Ty)->getPointeeType();
2528e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Reference);
2529e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else {
2530e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          const Type *DTy = Ty->getUnqualifiedDesugaredType();
2531e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          if (Ty == DTy) {
2532e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            Fn = 0;
2533e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            return;
2534e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          }
2535e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2536e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = QualType(DTy, 0);
2537e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Desugar);
2538e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        }
2539e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2540e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2541e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2542e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    bool isFunctionType() const { return (Fn != 0); }
2543e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *get() const { return Fn; }
2544e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2545e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(Sema &S, const FunctionType *New) {
2546e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // If T wasn't modified from the unwrapped type, do nothing.
2547e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (New == get()) return Original;
2548e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2549e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Fn = New;
2550e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return wrap(S.Context, Original, 0);
2551e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2552e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2553e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  private:
2554e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
2555e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size())
2556e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getQualifiedType(Fn, Old.getQualifiers());
2557e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2558e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // Build up the inner type, applying the qualifiers from the old
2559e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // type to the new type.
2560e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      SplitQualType SplitOld = Old.split();
2561e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2562e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // As a special case, tail-recurse if there are no qualifiers.
2563e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (SplitOld.second.empty())
2564e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, SplitOld.first, I);
2565e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return C.getQualifiedType(wrap(C, SplitOld.first, I), SplitOld.second);
2566e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2567e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2568e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
2569e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size()) return QualType(Fn, 0);
2570e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2571e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      switch (static_cast<WrapKind>(Stack[I++])) {
2572e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Desugar:
2573e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // This is the point at which we potentially lose source
2574e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // information.
2575e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
2576e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2577e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Parens: {
2578e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
2579e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getParenType(New);
2580e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2581e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2582e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Pointer: {
2583e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
2584e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getPointerType(New);
2585e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2586e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2587e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case BlockPointer: {
2588e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
2589e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getBlockPointerType(New);
2590e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2591e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2592e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case MemberPointer: {
2593e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
2594e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldMPT->getPointeeType(), I);
2595e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getMemberPointerType(New, OldMPT->getClass());
2596e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2597e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2598e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Reference: {
2599e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const ReferenceType *OldRef = cast<ReferenceType>(Old);
2600e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldRef->getPointeeType(), I);
2601e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<LValueReferenceType>(OldRef))
2602e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
2603e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        else
2604e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getRValueReferenceType(New);
2605e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2606e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2607e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2608e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      llvm_unreachable("unknown wrapping kind");
2609e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return QualType();
2610e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2611e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  };
2612e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
2613e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2614711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
2615711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
2616711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
2617711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
2618711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
2619711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
2620e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2621711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
26222455636163fdd18581d7fdae816433f886d88213Mike Stump
2623711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getKind() == AttributeList::AT_noreturn) {
2624711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
262504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
2626e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2627e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
2628711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
2629711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
2630425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
2631425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
2632711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
2633711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2634711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2635711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
2636425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
2637711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getKind() == AttributeList::AT_regparm) {
2638711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
2639711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
2640711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
26411e030eb1194763b42c1752723be23b1515f48981John McCall
2642711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
2643711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
2644008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
26451e030eb1194763b42c1752723be23b1515f48981John McCall
2646ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Diagnose regparm with fastcall.
2647ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    const FunctionType *fn = unwrapped.get();
2648ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    CallingConv CC = fn->getCallConv();
2649ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (CC == CC_X86FastCall) {
2650ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
2651ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC)
2652ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm";
2653ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
2654ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
2655ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
2656ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
2657e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionType::ExtInfo EI =
2658711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
2659711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2660711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2661425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
2662425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
266304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
2664711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
2665711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
2666711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2667f82b4e85b1219295cad4b5851b035575bc293010John McCall
266804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Delay if the type didn't work out to a function.
2669711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!unwrapped.isFunctionType()) return false;
267004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2671711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
2672711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
2673064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis  if (S.Context.getCanonicalCallConv(CC) ==
2674e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      S.Context.getCanonicalCallConv(CCOld)) {
2675ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
2676ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2677711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2678e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  }
267904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
268004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CCOld != CC_Default) {
268104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Should we diagnose reapplications of the same convention?
2682711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
268304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CC)
268404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CCOld);
2685711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2686711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
268704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
268804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
268904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
269004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
2691711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isa<FunctionNoProtoType>(fn)) {
2692711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_knr)
269304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
2694711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
2695711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
269604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
269704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2698711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
269904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
2700711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
270104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
2702711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
2703711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
270404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
2705ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
2706ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Also diagnose fastcall with regparm.
2707ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (fn->getRegParmType()) {
2708ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
2709ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm"
2710ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC);
2711ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
2712ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
2713ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
271404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
2715f82b4e85b1219295cad4b5851b035575bc293010John McCall
2716711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
2717711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2718711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
2719f82b4e85b1219295cad4b5851b035575bc293010John McCall}
2720f82b4e85b1219295cad4b5851b035575bc293010John McCall
27216e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
27226e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
27236e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
27246e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
27256e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
27266e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
27276e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
2728788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
2729788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
273056affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
27316e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
27326e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2733e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
27346e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
27356e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
27366e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
27376e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
2738ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
2739ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
27406e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
27416e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << "vector_size" << sizeExpr->getSourceRange();
2742e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
27436e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
27446e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
27456e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
2746f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
27476e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
2748e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
27496e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
27506e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
27516e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
27526e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
27536e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
27546e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
27556e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
27566e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
27576e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
27586e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
2759e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
27606e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
27616e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
27626e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
27636e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
27646e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
2765e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
27666e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
27676e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
27686e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
27696e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
27706e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
2771788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
2772e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
27736e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
27746e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
27754211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
27764211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
27774211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
27784211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
27794211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
27804211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
27814211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
27824211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
27834211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
27844211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     VectorType::VectorKind VecKind,
27854211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const char *AttrName) {
27864211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
27874211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
27884211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
27894211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
27904211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
27914211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
27924211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
27934211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
27944211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
27954211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
27964211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
27974211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
27984211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      << AttrName << numEltsExpr->getSourceRange();
27994211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
28004211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
28014211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
28024211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
28034211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  const BuiltinType* BTy = CurType->getAs<BuiltinType>();
28044211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (!BTy ||
28054211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (VecKind == VectorType::NeonPolyVector &&
28064211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::SChar &&
28074211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short) ||
28084211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (BTy->getKind() != BuiltinType::SChar &&
28094211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UChar &&
28104211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short &&
28114211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UShort &&
28124211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Int &&
28134211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UInt &&
28144211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::LongLong &&
28154211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::ULongLong &&
28164211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Float)) {
28174211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
28184211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
28194211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
28204211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
28214211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
28224211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
28234211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
28244211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
28254211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
28264211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
28274211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
28284211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
28294211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
28304211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
28314211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
28324211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
28334211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
2834711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
2835711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             bool isDeclSpec, AttributeList *attrs) {
2836c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
2837c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
2838c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
2839c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
2840711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2841711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
2842711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
2843711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
2844711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr.getNext();
2845711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2846e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
2847711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
2848e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
2849e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
2850b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
2851b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
2852711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
2853c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
285404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2855c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
2856711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
2857c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
2858711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
2859711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
2860711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
2861d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
286204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    case AttributeList::AT_vector_size:
2863711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
2864f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
28654211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    case AttributeList::AT_neon_vector_type:
2866711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
2867711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonVector, "neon_vector_type");
28684211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
28694211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    case AttributeList::AT_neon_polyvector_type:
2870711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
2871711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonPolyVector,
28724211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                               "neon_polyvector_type");
28734211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
287404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2875711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
2876711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
2877711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
2878711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (isDeclSpec)
2879711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
2880711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2881711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
2882711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
2883711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
28846e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
2885c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
2886711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attrs = next));
2887232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
2888232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
28891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
28904ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
28914ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
28924ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
289386447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
289486447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
289586447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
289686447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
289786447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
28984ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
28994ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
29004ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
29014ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
29024ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
29034ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
29041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @param PD The partial diagnostic that will be printed out if T is not a
2905b790661a15d93941d2c33a0ea328254277b3d7e3Anders Carlsson/// complete type.
29064ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
29074ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
29084ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
290991a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
29108c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               const PartialDiagnostic &PD,
29118c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               std::pair<SourceLocation,
29128c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                                         PartialDiagnostic> Note) {
291391a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  unsigned diag = PD.getDiagID();
29141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2915573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
2916573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
2917690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
2918690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
2919690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
29201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
2921690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
2922690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
29234ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
29244ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
29254ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
29264ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
2927d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
2928923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
2929923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
2930923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
293189c49f09b0292dc7c03885f6c765d667a9837597Douglas Gregor  if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
2932923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
2933923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
29342943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
2935d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
2936972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
2937972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
2938d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
29395842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor                                                      /*Complain=*/diag != 0);
29401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
2941d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
2942d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
2943b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
2944b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        assert(MSInfo && "Missing member specialization information?");
2945357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
2946b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        if (MSInfo->getTemplateSpecializationKind()
2947972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor                                               != TSK_ExplicitSpecialization)
2948f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
2949f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
2950f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
2951f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  /*Complain=*/diag != 0);
2952d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
2953d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
2954d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
29552943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
29565842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor  if (diag == 0)
29575842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
29581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2959916c870442978db40404d51348cdf5524e506faaJohn McCall  const TagType *Tag = T->getAs<TagType>();
296001620704304f819b82ecef769ec114e541a364d7Rafael Espindola
296101620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // Avoid diagnosing invalid decls as incomplete.
296201620704304f819b82ecef769ec114e541a364d7Rafael Espindola  if (Tag && Tag->getDecl()->isInvalidDecl())
296301620704304f819b82ecef769ec114e541a364d7Rafael Espindola    return true;
296401620704304f819b82ecef769ec114e541a364d7Rafael Espindola
2965916c870442978db40404d51348cdf5524e506faaJohn McCall  // Give the external AST source a chance to complete the type.
2966916c870442978db40404d51348cdf5524e506faaJohn McCall  if (Tag && Tag->getDecl()->hasExternalLexicalStorage()) {
2967916c870442978db40404d51348cdf5524e506faaJohn McCall    Context.getExternalSource()->CompleteType(Tag->getDecl());
2968916c870442978db40404d51348cdf5524e506faaJohn McCall    if (!Tag->isIncompleteType())
2969916c870442978db40404d51348cdf5524e506faaJohn McCall      return false;
2970916c870442978db40404d51348cdf5524e506faaJohn McCall  }
2971916c870442978db40404d51348cdf5524e506faaJohn McCall
29724ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
297391a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  Diag(Loc, PD) << T;
29743c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
29758c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  // If we have a note, produce it.
29768c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  if (!Note.first.isInvalid())
29778c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson    Diag(Note.first, Note.second);
29788c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson
29794ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
298001620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
29814ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
29821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
29834ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
29844ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
29854ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
29864ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
29874ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
29884ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
2989e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
2990fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2991fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               const PartialDiagnostic &PD) {
2992fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PD,
2993fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
2994fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
2995fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
2996fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2997fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               unsigned DiagID) {
2998fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PDiag(DiagID),
2999fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
3000fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
3001fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
3002465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
3003465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
3004465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
3005465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
3006465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
3007e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
3008465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
3009e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
3010465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
3011465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
3012465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
3013465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
3014465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
3015465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
3016465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
3017e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
3018af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
30192a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
30202a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  ExprResult ER = CheckPlaceholderExpr(E, Loc);
30212a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
30222a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
30232a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
30242b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
30252b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
3026aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
3027aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
30282b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
3029af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
3030af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
3031af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
30322a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
30332a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  ExprResult ER = CheckPlaceholderExpr(E, Loc);
30342a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
30352a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
30364b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
3037af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getDecltypeType(E);
3038af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
3039