SemaType.cpp revision a08fcb8105bf53f3640ad17f61bdcde2d8ace78a
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements type-related semantic analysis.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
142d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
157cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/Sema/Template.h"
16207f4d8543529221932af82836016a2ef066c917Peter Collingbourne#include "clang/Basic/OpenCL.h"
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
1836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor#include "clang/AST/ASTMutationListener.h"
19a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor#include "clang/AST/CXXInheritance.h"
20980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
212943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor#include "clang/AST/DeclTemplate.h"
224adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
2351bd803fbdade51d674598ed45da3d54190a656cJohn McCall#include "clang/AST/TypeLocVisitor.h"
24e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
2591a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
26d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis#include "clang/Basic/TargetInfo.h"
272792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall#include "clang/Lex/Preprocessor.h"
2819510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
294994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl#include "llvm/ADT/SmallPtrSet.h"
3087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor#include "llvm/Support/ErrorHandling.h"
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
332dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// \brief Perform adjustment on the parameter type of a function.
342dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor///
352dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// This routine adjusts the given parameter type @p T to the actual
361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
382dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorQualType Sema::adjustParameterType(QualType T) {
392dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // C99 6.7.5.3p7:
40778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   A declaration of a parameter as "array of type" shall be
41778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   adjusted to "qualified pointer to type", where the type
42778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   qualifiers (if any) are those specified within the [ and ] of
43778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   the array type derivation.
44778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  if (T->isArrayType())
452dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getArrayDecayedType(T);
46778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner
47778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  // C99 6.7.5.3p8:
48778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   A declaration of a parameter as "function returning type"
49778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   shall be adjusted to "pointer to function returning type", as
50778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  //   in 6.3.2.1.
51778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner  if (T->isFunctionType())
522dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return Context.getPointerType(T);
532dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
542dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return T;
552dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
562dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
575db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
585db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
595db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// isOmittedBlockReturnType - Return true if this declarator is missing a
605db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// return type because this is a omitted return type on a block literal.
618ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic bool isOmittedBlockReturnType(const Declarator &D) {
625db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getContext() != Declarator::BlockLiteralContext ||
638ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl      D.getDeclSpec().hasTypeSpecifier())
645db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return false;
655db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
665db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 0)
67a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^{ ... }
685db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
695db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 1 &&
705db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      D.getTypeObject(0).Kind == DeclaratorChunk::Function)
71a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^(int X, float Y) { ... }
725db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
735db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  return false;
745db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner}
755db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
762792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// diagnoseBadTypeAttribute - Diagnoses a type attribute which
772792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// doesn't apply to the given type.
782792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCallstatic void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr,
792792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall                                     QualType type) {
802792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  bool useInstantiationLoc = false;
812792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
822792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  unsigned diagID = 0;
832792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  switch (attr.getKind()) {
842792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  case AttributeList::AT_objc_gc:
852792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    diagID = diag::warn_pointer_attribute_wrong_type;
862792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    useInstantiationLoc = true;
872792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    break;
882792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
892792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  default:
902792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    // Assume everything else was a function attribute.
912792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    diagID = diag::warn_function_attribute_wrong_type;
922792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    break;
932792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  }
942792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
952792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  SourceLocation loc = attr.getLoc();
962792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  llvm::StringRef name = attr.getName()->getName();
972792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
982792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  // The GC attributes are usually written with macros;  special-case them.
992792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  if (useInstantiationLoc && loc.isMacroID() && attr.getParameterName()) {
100834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    if (attr.getParameterName()->isStr("strong")) {
101834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__strong")) name = "__strong";
102834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    } else if (attr.getParameterName()->isStr("weak")) {
103834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__weak")) name = "__weak";
1042792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    }
1052792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  }
1062792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
1072792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  S.Diag(loc, diagID) << name << type;
1082792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall}
1092792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
110711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// objc_gc applies to Objective-C pointers or, otherwise, to the
111711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// smallest available pointer type (i.e. 'void*' in 'void**').
112711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
113711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_objc_gc
114711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
115711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// Function type attributes.
116711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define FUNCTION_TYPE_ATTRS_CASELIST \
117711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_noreturn: \
118711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_cdecl: \
119711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_fastcall: \
120711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_stdcall: \
121711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_thiscall: \
122711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case AttributeList::AT_pascal: \
123414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    case AttributeList::AT_regparm: \
124414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    case AttributeList::AT_pcs \
125711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
126711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace {
127711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// An object which stores processing state for the entire
128711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// GetTypeForDeclarator process.
129711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  class TypeProcessingState {
130711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &sema;
131711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
132711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The declarator being processed.
133711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &declarator;
134711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
135711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The index of the declarator chunk we're currently processing.
136711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// May be the total number of valid chunks, indicating the
137711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// DeclSpec.
138711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex;
139711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
140711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Whether there are non-trivial modifications to the decl spec.
141711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    bool trivial;
142711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1437ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    /// Whether we saved the attributes in the decl spec.
1447ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    bool hasSavedAttrs;
1457ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
146711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The original set of attributes on the DeclSpec.
147711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    llvm::SmallVector<AttributeList*, 2> savedAttrs;
148711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
149711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// A list of attributes to diagnose the uselessness of when the
150711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// processing is complete.
151711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    llvm::SmallVector<AttributeList*, 2> ignoredTypeAttrs;
152711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
153711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  public:
154711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    TypeProcessingState(Sema &sema, Declarator &declarator)
155711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : sema(sema), declarator(declarator),
156711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        chunkIndex(declarator.getNumTypeObjects()),
1577ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        trivial(true), hasSavedAttrs(false) {}
158711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
159711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &getSema() const {
160711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return sema;
161711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
163711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &getDeclarator() const {
164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator;
165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
166711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned getCurrentChunkIndex() const {
168711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return chunkIndex;
169711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
170711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void setCurrentChunkIndex(unsigned idx) {
172711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(idx <= declarator.getNumTypeObjects());
173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      chunkIndex = idx;
174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
175711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
176711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&getCurrentAttrListRef() const {
177711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(chunkIndex <= declarator.getNumTypeObjects());
178711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (chunkIndex == declarator.getNumTypeObjects())
179711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        return getMutableDeclSpec().getAttributes().getListRef();
180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator.getTypeObject(chunkIndex).getAttrListRef();
181711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
182711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
183711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Save the current set of attributes on the DeclSpec.
184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void saveDeclSpecAttrs() {
185711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Don't try to save them multiple times.
1867ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (hasSavedAttrs) return;
187711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
188711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      DeclSpec &spec = getMutableDeclSpec();
189711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (AttributeList *attr = spec.getAttributes().getList(); attr;
190711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             attr = attr->getNext())
191711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs.push_back(attr);
192711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      trivial &= savedAttrs.empty();
1937ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      hasSavedAttrs = true;
194711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
195711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
196711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Record that we had nowhere to put the given type attribute.
197711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// We will diagnose such attributes later.
198711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void addIgnoredTypeAttr(AttributeList &attr) {
199711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      ignoredTypeAttrs.push_back(&attr);
200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
201711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
202711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Diagnose all the ignored type attributes, given that the
203711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// declarator worked out to the given type.
204711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void diagnoseIgnoredTypeAttrs(QualType type) const {
205711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (llvm::SmallVectorImpl<AttributeList*>::const_iterator
206711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
2072792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall           i != e; ++i)
2082792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall        diagnoseBadTypeAttribute(getSema(), **i, type);
209711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
210711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
211711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    ~TypeProcessingState() {
212711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (trivial) return;
213711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
214711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      restoreDeclSpecAttrs();
215711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
216711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
217711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  private:
218711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclSpec &getMutableDeclSpec() const {
219711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return const_cast<DeclSpec&>(declarator.getDeclSpec());
220711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
221711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
222711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void restoreDeclSpecAttrs() {
2237ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      assert(hasSavedAttrs);
2247ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
2257ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (savedAttrs.empty()) {
2267ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        getMutableDeclSpec().getAttributes().set(0);
2277ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        return;
2287ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      }
2297ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
230711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
231711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
232711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs[i]->setNext(savedAttrs[i+1]);
233711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      savedAttrs.back()->setNext(0);
234711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
235711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
236711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
237711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// Basically std::pair except that we really want to avoid an
238711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// implicit operator= for safety concerns.  It's also a minor
239711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// link-time optimization for this to be a private type.
240711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  struct AttrAndList {
241711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The attribute.
242711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &first;
243711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
244711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The head of the list the attribute is currently in.
245711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&second;
246711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
247711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttrAndList(AttributeList &attr, AttributeList *&head)
248711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : first(attr), second(head) {}
249711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
25004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
25104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
252711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace llvm {
253711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  template <> struct isPodLike<AttrAndList> {
254711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    static const bool value = true;
255711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
256711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
257711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
258711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
259711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  attr.setNext(head);
260711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  head = &attr;
261711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
262711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
263711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
264711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (head == &attr) {
265711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    head = attr.getNext();
266711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
26704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
268711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
269711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *cur = head;
270711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  while (true) {
271711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    assert(cur && cur->getNext() && "ran out of attrs?");
272711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (cur->getNext() == &attr) {
273711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      cur->setNext(attr.getNext());
274711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
275711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
276711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    cur = cur->getNext();
277711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
278711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
279711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
280711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void moveAttrFromListToList(AttributeList &attr,
281711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&fromList,
282711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&toList) {
283711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, fromList);
284711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrIntoList(attr, toList);
285711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
286711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
287711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state,
288711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             QualType &type, bool isDeclSpec,
289711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             AttributeList *attrs);
290711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
291711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
292711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
293711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type);
294711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
295711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
296711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr, QualType &type);
297711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
298711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCPointerTypeAttr(TypeProcessingState &state,
299711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr, QualType &type) {
300711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Right now, we have exactly one of these attributes: objc_gc.
301711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(attr.getKind() == AttributeList::AT_objc_gc);
302711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return handleObjCGCTypeAttr(state, attr, type);
303711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
304711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
305711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Given that an objc_gc attribute was written somewhere on a
306711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declaration *other* than on the declarator itself (for which, use
307711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
308711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// didn't apply in whatever position it was written in, try to move
309711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// it to a more appropriate position.
310711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeObjCPointerTypeAttr(TypeProcessingState &state,
311711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          AttributeList &attr,
312711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType type) {
313711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
314711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
315711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
316711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
317711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
318711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
319711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
320711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
321711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
322711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
323711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
324711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
325711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
326711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
327711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Don't walk through these.
328711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
329711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
330711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
331711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto error;
332711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
333711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
334711c52bb20d0c69063b52a99826fb7d2835501f1John McCall error:
3352792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
3362792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
337711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
338711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
339711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Distribute an objc_gc type attribute that was written on the
340711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator.
341711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
342711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
343711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            AttributeList &attr,
344711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            QualType &declSpecType) {
345711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
346711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
347711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // objc_gc goes on the innermost pointer to something that's not a
348711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // pointer.
349711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  unsigned innermost = -1U;
350711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  bool considerDeclSpec = true;
351711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
352711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
353711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
354711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
355711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
356711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      innermost = i;
357ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      continue;
358711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
359711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
360711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
361711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
362711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
363711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
364711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
365711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
366711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      considerDeclSpec = false;
367711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto done;
368711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
369711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
370711c52bb20d0c69063b52a99826fb7d2835501f1John McCall done:
371711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
372711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // That might actually be the decl spec if we weren't blocked by
373711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // anything in the declarator.
374711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (considerDeclSpec) {
3757ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    if (handleObjCPointerTypeAttr(state, attr, declSpecType)) {
3767ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // Splice the attribute into the decl spec.  Prevents the
3777ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // attribute from being applied multiple times and gives
3787ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // the source-location-filler something to work with.
3797ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      state.saveDeclSpecAttrs();
3807ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      moveAttrFromListToList(attr, declarator.getAttrListRef(),
3817ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall               declarator.getMutableDeclSpec().getAttributes().getListRef());
382711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
3837ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    }
384711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
385711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
386711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, if we found an appropriate chunk, splice the attribute
387711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // into it.
388711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (innermost != -1U) {
389711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, declarator.getAttrListRef(),
390711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                       declarator.getTypeObject(innermost).getAttrListRef());
391711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
392711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
393711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
394711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, diagnose when we're done building the type.
395711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
396711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
397711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
398711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
399711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written somewhere in a declaration
400711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// *other* than on the declarator itself or in the decl spec.  Given
401711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// that it didn't apply in whatever position it was written in, try
402711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// to move it to a more appropriate position.
403711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeFunctionTypeAttr(TypeProcessingState &state,
404711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
405711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType type) {
406711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
407711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
408711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to push the attribute from the return type of a function to
409711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the function itself.
410711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
411711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
412711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
414711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
415711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
416711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
417711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
418711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
419711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
420711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
421711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
422711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
423711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
424711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
425711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
426711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
427711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4282792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
429711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
430711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
431711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Try to distribute a function type attribute to the innermost
432711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// function chunk or type.  Returns true if the attribute was
433711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributed, false if no location was found.
434711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool
435711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
436711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr,
437711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList *&attrList,
438711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      QualType &declSpecType) {
439711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
440711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
441711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Put it on the innermost function chunk, if there is one.
442711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
443711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (chunk.Kind != DeclaratorChunk::Function) continue;
445711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
446711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
448711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
449711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
450711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return handleFunctionTypeAttr(state, attr, declSpecType);
451711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
452711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
453711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written in the decl spec.  Try to
454711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
455711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
456711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
457711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
458711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType &declSpecType) {
459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.saveDeclSpecAttrs();
460711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
461711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
462711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
463711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            state.getCurrentAttrListRef(),
464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
465711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
466711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
467711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
468711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
469711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
470711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
471711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
472711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written on the declarator.  Try to
473711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
474711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
475711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
476711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         AttributeList &attr,
477711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         QualType &declSpecType) {
478711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
479711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
480711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
482711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declarator.getAttrListRef(),
483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
484711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
485711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
486711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
487711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
488711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
489711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
490711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
491711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
492711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// \brief Given that there are attributes written on the declarator
493711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// itself, try to distribute any type attributes to the appropriate
494711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator chunk.
495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///
496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// These are attributes like the following:
497711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f ATTR;
498711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int (f ATTR)();
499711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// but not necessarily this:
500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f() ATTR;
501711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                              QualType &declSpecType) {
503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Collect all the type attributes from the declarator itself.
504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
505711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *attr = state.getDeclarator().getAttributes();
506711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
507711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
508711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr->getNext();
509711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
510711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr->getKind()) {
511711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
512711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
513711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
514711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    default:
520711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
521711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attr = next));
523711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
524711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
525711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Add a synthetic '()' to a block-literal declarator if it is
526711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// required, given the return type.
527711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void maybeSynthesizeBlockSignature(TypeProcessingState &state,
528711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType declSpecType) {
529711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
531711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // First, check whether the declarator would produce a function,
532711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // i.e. whether the innermost semantic chunk is a function.
533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.isFunctionDeclarator()) {
534711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // If so, make that declarator a prototyped declarator.
535711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.getFunctionTypeInfo().hasPrototype = true;
536711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
537711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
538711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
539da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // If there are any type objects, the type as written won't name a
540da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // function, regardless of the decl spec type.  This is because a
541da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // block signature declarator is always an abstract-declarator, and
542da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // abstract-declarators can't just be parentheses chunks.  Therefore
543da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // we need to build a function chunk unless there are no type
544da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // objects and the decl spec type is a function.
545711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
546711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
547711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
548da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // Note that there *are* cases with invalid declarators where
549da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // declarators consist solely of parentheses.  In general, these
550da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // occur only in failed efforts to make function declarators, so
551da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // faking up the function chunk is still the right thing to do.
552711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
553711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, we need to fake up a function declarator.
554711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation loc = declarator.getSourceRange().getBegin();
555711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
556711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // ...and *prepend* it to the declarator.
557711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
558711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*proto*/ true,
559711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*variadic*/ false, SourceLocation(),
560711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*args*/ 0, 0,
561711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*type quals*/ 0,
56283f51722ed2b8134810cb178f39e44da811de7cdDouglas Gregor                             /*ref-qualifier*/true, SourceLocation(),
5636e5d319b671dbb0ecf70619834aa23c853d17621Sebastian Redl                             /*EH*/ EST_None, SourceLocation(), 0, 0, 0, 0,
564711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*parens*/ loc, loc,
565711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             declarator));
566711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
567711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // For consistency, make sure the state still has us as processing
568711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the decl spec.
569711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
570711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.setCurrentChunkIndex(declarator.getNumTypeObjects());
57104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
57204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
573930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
574930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
5755db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// \param D  the declarator containing the declaration specifier.
5765153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
5775153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
578711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic QualType ConvertDeclSpecToType(Sema &S, TypeProcessingState &state) {
5795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
5805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
581711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
582711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
583711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const DeclSpec &DS = declarator.getDeclSpec();
584711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation DeclLoc = declarator.getIdentifierLoc();
5855db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
5865db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    DeclLoc = DS.getSourceRange().getBegin();
5871564e3906cad604a42bd131e584751a75589a9c4Chris Lattner
588711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  ASTContext &Context = S.Context;
5891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5905db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
5915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
59296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
59396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
59496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
5955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
5965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
597fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
5985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
599fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
6005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
6015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
6025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
603fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
605958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
60664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
60764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
60864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
60964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
610711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
611f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
61264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
61364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
61464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
61564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
616711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
617f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
61864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
61964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
62064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
621f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
622f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
623f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
624f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
625f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
626f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
627f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
628f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
629f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
630f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
631d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
63262f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
633097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
634c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
635c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         (ObjCProtocolDecl**)PQ,
636c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         DS.getNumProtocolQualifiers());
637c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectPointerType(Result);
63862f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
63962f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
6405db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
6415db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
6425db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
643711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isOmittedBlockReturnType(declarator)) {
6445db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      Result = Context.DependentTy;
6455db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
6465db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
6471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
648d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
649d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
650d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
651d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
652d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
653d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
654d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
655711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.getLangOptions().ImplicitInt) {
65635d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
65735d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
6583f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
659711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_declspec)
6603f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
661849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int");
6623f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
6634310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
664d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
665d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
666d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
667d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
6684310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
669711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (S.getLangOptions().CPlusPlus &&
670711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          !S.getLangOptions().Microsoft) {
671711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_missing_type_specifier)
6723f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
6731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
674b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
675b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
676b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
677711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
678b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
679711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_type_specifier)
6803f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
681b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
682d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
6831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
6853cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
6865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
6875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
688fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
689fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
690fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
691311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
692311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
693311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
694311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
695711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        if (!S.getLangOptions().C99 &&
696711c52bb20d0c69063b52a99826fb7d2835501f1John McCall            !S.getLangOptions().CPlusPlus0x)
697711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
698311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
6995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
7005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
7015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
702fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
703fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
704fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
705311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
706311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
707311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner
708311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
709711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        if (!S.getLangOptions().C99 &&
710711c52bb20d0c69063b52a99826fb7d2835501f1John McCall            !S.getLangOptions().CPlusPlus0x)
711711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
712311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
7135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
7145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
715958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7163cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
717fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
718958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
719958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
720fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
721958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
722fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
72339d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne
72439d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    if (S.getLangOptions().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
72539d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
72639d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      declarator.setInvalidType(true);
72739d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    }
728958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
729fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
7305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
7315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
7325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
733711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
7348f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
735711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
7368f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
73799dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
7385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
7395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
7405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_struct: {
741b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
7426e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
7436e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
7446e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
745711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
7466e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
7476e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
7486e24726524c2b51b31bb4b622aa678a46b024f42John McCall
749a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
7500daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc());
751a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
753a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
754a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner
7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
756a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
7572191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
7580daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    // In both C and C++, make an ElaboratedType.
7590daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    ElaboratedTypeKeyword Keyword
7600daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
7610daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
7620daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara
7635153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    if (D->isInvalidDecl())
764711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
765958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7671a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
7685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
7695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
7705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
771711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
77227940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    if (Result.isNull())
773711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
77427940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    else if (DeclSpec::ProtocolQualifierListTy PQ
77527940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall               = DS.getProtocolQualifiers()) {
776c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
777c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // Silently drop any existing protocol qualifiers.
778c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // TODO: determine whether that's the right thing to do.
779c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjT->getNumProtocols())
780c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = ObjT->getBaseType();
781c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
782c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (DS.getNumProtocolQualifiers())
783c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = Context.getObjCObjectType(Result,
784c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             (ObjCProtocolDecl**) PQ,
785c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             DS.getNumProtocolQualifiers());
786c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCIdType()) {
787ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
788c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
789c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
790c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
791c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
792c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCClassType()) {
7934262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
794c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
795c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
796c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
797c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
7983f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
799711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
8003f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
801711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
8023f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
803c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
8041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
806958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
8075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
808958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
809e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
810711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
811958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
812730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian    if (!Result->isDependentType())
813730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian      if (const TagType *TT = Result->getAs<TagType>())
814711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
815d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
816fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
817958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
818d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
819b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
820d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
821d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
822711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
8234b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
8244b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
825711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
8264b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
827958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
828d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
8296fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
830b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
8316fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
8326fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
833711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
834af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
835af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
836711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
837af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
8386fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
8396fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
840ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case DeclSpec::TST_underlyingType:
841db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    Result = S.GetTypeFromParser(DS.getRepAsType());
842db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    assert(!Result.isNull() && "Didn't get a type for __underlying_type?");
843ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    Result = S.BuildUnaryTransformType(Result,
844ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::EnumUnderlyingType,
845ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       DS.getTypeSpecTypeLoc());
846ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (Result.isNull()) {
847ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Result = Context.IntTy;
848ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      declarator.setInvalidType(true);
849db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    }
850db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    break;
851db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
852e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
853e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
85434b41d939a1328f484511c6002ba2456db879a29Richard Smith    Result = Context.getAutoType(QualType());
855e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
856e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
8571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
858a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall  case DeclSpec::TST_unknown_anytype:
859a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    Result = Context.UnknownAnyTy;
860a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    break;
861a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall
862809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
8635153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
864711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
8655153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
868958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
869f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
870711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.getLangOptions().Freestanding)
871711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
872fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
87382287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
87482287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
87582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
876e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
877788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
878e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
879788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
880e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
881e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
882f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88447423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
88547423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
886711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
8871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
888711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
889711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
890711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
891711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
892711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
893711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
894711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
895711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
896711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (AttributeList *attrs = DS.getAttributes().getList())
897711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    processTypeAttrs(state, Result, true, attrs);
8981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
89996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
90096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
90196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
90296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
90396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
90496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
9050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (TypeQuals & DeclSpec::TQ_restrict) {
9062b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian      if (Result->isAnyPointerType() || Result->isReferenceType()) {
9072b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        QualType EltTy;
9082b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        if (Result->isObjCObjectPointerType())
9092b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result;
9102b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        else
9112b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result->isPointerType() ?
9122b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<PointerType>()->getPointeeType() :
9132b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<ReferenceType>()->getPointeeType();
9141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
915bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
916bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
917bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
918711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getRestrictSpecLoc(),
919d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
920d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
9210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall          TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
922bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
923bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
924711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DS.getRestrictSpecLoc(),
925711c52bb20d0c69063b52a99826fb7d2835501f1John McCall               diag::err_typecheck_invalid_restrict_not_pointer)
926d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
9270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
92896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
92996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
9301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
93196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
93296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
93396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
93496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
93596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
93696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
9370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
93896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
9390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
94096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
9410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
9420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        assert((TypeQuals & DeclSpec::TQ_restrict) &&
9430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall               "Has CVR quals but not C, V, or R?");
9440953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Loc = DS.getRestrictSpecLoc();
94596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
946711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(Loc, diag::warn_typecheck_function_qualifiers)
947d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
94896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
9491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
950f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
951f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
952f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
953f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
954f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
9551a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
9561a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
957f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
9580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
9590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
9601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
9611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
9630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Result = Context.getQualifiedType(Result, Quals);
96496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
9650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
966f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
967f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
968f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
969cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
970cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
971cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
9721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
973cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
974cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
975cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
9762865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
9772865474261a608c7873b87ba4af110d17907896dJohn McCall                                  Qualifiers Qs) {
9782865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
9792865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
9802865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
9812865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
9822865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
9832865474261a608c7873b87ba4af110d17907896dJohn McCall
9842865474261a608c7873b87ba4af110d17907896dJohn McCall    const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
9852865474261a608c7873b87ba4af110d17907896dJohn McCall    if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
9862865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
9872865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9882865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
9892865474261a608c7873b87ba4af110d17907896dJohn McCall      }
9902865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
9912865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
9922865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9932865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
9942865474261a608c7873b87ba4af110d17907896dJohn McCall      }
9952865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
9962865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
9972865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
9982865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
9992865474261a608c7873b87ba4af110d17907896dJohn McCall      }
10002865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (!Ty->isDependentType()) {
10012865474261a608c7873b87ba4af110d17907896dJohn McCall      // FIXME: this deserves a proper diagnostic
10022865474261a608c7873b87ba4af110d17907896dJohn McCall      DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
10032865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
10042865474261a608c7873b87ba4af110d17907896dJohn McCall    }
10052865474261a608c7873b87ba4af110d17907896dJohn McCall
10062865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
10072865474261a608c7873b87ba4af110d17907896dJohn McCall      Diag(Loc, DiagID) << ProblemTy;
10082865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
10092865474261a608c7873b87ba4af110d17907896dJohn McCall    }
10102865474261a608c7873b87ba4af110d17907896dJohn McCall  }
10112865474261a608c7873b87ba4af110d17907896dJohn McCall
10122865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
10132865474261a608c7873b87ba4af110d17907896dJohn McCall}
10142865474261a608c7873b87ba4af110d17907896dJohn McCall
1015075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
1016075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
1017075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
1018075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
1019075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
1020cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
1021cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1022cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
1023cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1024cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1025cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
1026cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
1027cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1028cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
1029cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1030cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1031cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
1032cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
10332865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
1034cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
1035cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
1036cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
1037cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
1038ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1039cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1040cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1041cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1042c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
104392e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
1044cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
10452865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
1046cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1047cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1048cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
1049cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1050cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
1051cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1052cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1053cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
1054cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
1055cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1056cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
1057cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1058cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1059cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
1060cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
106154e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
10622865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
106354e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
10649625e44c0252485277a340746ed8ac950686156fDouglas Gregor  assert(Context.getCanonicalType(T) != Context.OverloadTy &&
10659625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
10669625e44c0252485277a340746ed8ac950686156fDouglas Gregor
106769d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // C++0x [dcl.ref]p6:
106869d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   If a typedef (7.1.3), a type template-parameter (14.3.1), or a
106969d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
107069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   type T, an attempt to create the type "lvalue reference to cv TR" creates
107169d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   the type "lvalue reference to T", while an attempt to create the type
107269d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   "rvalue reference to cv TR" creates the type TR.
107354e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
107454e14c4db764c0636160d26c5bbf491637c83a76John McCall
107554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
107654e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
107754e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
107854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
107954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
108054e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
108154e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
108254e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
108354e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
108454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
108554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
108669d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // collapsing of references-to-references as described in C++0x.
108769d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // DR 106 and 540 introduce reference-collapsing into C++98/03.
1088cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1089cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
109033a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
1091cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
1092cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
1093cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
1094cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1095cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1096cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1097cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
10987c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
10992865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
11002865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
1101cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1102cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1103cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
1104cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1105cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
1106cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1107cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
11081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
11091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
1110cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1111cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1112cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// array type or, if there is no such entity, the location of the
1113cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have array type.
1114cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1115cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
1116cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1117cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1118cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
1119cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
1120cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1121cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
11227e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
11230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
11247e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
1125923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (getLangOptions().CPlusPlus) {
1126138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
1127138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
1128138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   type, the (possibly cv-qualified) type void, a function type or an
1129138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
1130138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
1131138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
1132138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
1133138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
1134138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
1135138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1136138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
1137138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1138923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (T->isVoidType()) {
1139923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1140923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1141923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1142138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1143138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (RequireNonAbstractType(Brackets.getBegin(), T,
1144138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
1145138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1146138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor
1147923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
1148138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1149138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
1150923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
1151923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
1152923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1153923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
1154cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1155cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
1156cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
1157ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1158cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1159cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
11601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
116134b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (T->getContainedAutoType()) {
116234b41d939a1328f484511c6002ba2456db879a29Richard Smith    Diag(Loc, diag::err_illegal_decl_array_of_auto)
116334b41d939a1328f484511c6002ba2456db879a29Richard Smith      << getPrintableNameForEntity(Entity) << T;
1164e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
1165e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
11661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11676217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
1168cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
1169cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
1170cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
1171cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
1172c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
1173c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1174c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
1175cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
11761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11775e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
1178429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ArraySize && !ArraySize->isRValue()) {
1179429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult Result = DefaultLvalueConversion(ArraySize);
1180429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Result.isInvalid())
1181429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return QualType();
1182429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
1183429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ArraySize = Result.take();
1184429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
11855e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1186cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
11875e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // TODO: in theory, if we were insane, we could allow contextual
11885e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // conversions to integer type here.
1189cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (ArraySize && !ArraySize->isTypeDependent() &&
11901274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1191cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1192cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
1193cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1194cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
11952767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
1196cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
1197f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
11987e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
1199f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
1200f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
1201ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
12027e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
1203cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
1204923d56d436f750bc1f29db50e641078725558a1bSebastian Redl             (!T->isDependentType() && !T->isIncompleteType() &&
1205923d56d436f750bc1f29db50e641078725558a1bSebastian Redl              !T->isConstantSizeType())) {
1206cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // Per C99, a variable array is an array with either a non-constant
1207cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // size or an element type that has a non-constant-size
12087e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1209cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
1210cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1211cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
1212923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
1213b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
1214b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1215b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1216b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
1217b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1218b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
1219923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1220923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1221923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
122202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
122302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
122402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      Diag(ArraySize->getLocStart(),
122502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
122602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
1227923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
12282767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
12292767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor               !T->isIncompleteType()) {
12302767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      // Is the array too large?
12312767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
12322767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
12332767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
12342767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
12352767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
12362767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
12371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
12382767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
123946a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
1240cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1241af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
1242af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  if (!getLangOptions().C99) {
12430fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
12440fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
1245204ce17e0cfd9bbe229627e1e5a20c3f2f587c8cDouglas Gregor      if (!T->isDependentType() &&
1246204ce17e0cfd9bbe229627e1e5a20c3f2f587c8cDouglas Gregor          !Context.getBaseElementType(T)->isPODType()) {
12470fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
12480fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor          << Context.getBaseElementType(T);
12490fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
12500fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      }
1251a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
1252a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
1253a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
1254a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
1255a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
12560fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
12570fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
12580fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::ext_vla);
12590fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
1260043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor      Diag(Loc,
1261043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor           getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
1262043cad21b78c6b02597cdc7b6ead32388e27ebc7Douglas Gregor                                     : diag::ext_c99_array_usage);
1263cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1264cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1265cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
1266cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
12679cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
12689cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
12699cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
12709cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
12719ae2f076ca5ab1feb3ba95629099ec2319833701John McCallQualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
12729cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
12739cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
12749cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
12751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
12769cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
12779cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
12789cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
12799cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
12809cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
12819ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
12829cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
12839ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
12849cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
12859ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        << "ext_vector_type" << ArraySize->getSourceRange();
12869cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
12879cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
12881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
12909cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
12911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
12921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12939cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
12949cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
12959ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      << ArraySize->getSourceRange();
12969cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
12979cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
12981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12999cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (!T->isDependentType())
13009cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return Context.getExtVectorType(T, vectorSize);
13011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
13021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13039ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
13049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
13051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1306724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
1307724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1308724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
1309724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
1310724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
13112943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
1312724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
1313724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1314724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
1315724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1316724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
1317724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
1318724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
1319724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1320724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
1321724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1322724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
1323724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1324724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
1325724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1326724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
1327724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
1328724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
1329724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1330724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
1331724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
1332724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1333724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
1334724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
1335724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
13361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 QualType *ParamTypes,
1337724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
1338724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 bool Variadic, unsigned Quals,
1339c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                                 RefQualifierKind RefQualifier,
1340fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
1341e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                 FunctionType::ExtInfo Info) {
1342724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
134358408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor    Diag(Loc, diag::err_func_returning_array_function)
134458408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
1345724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1346724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
13475291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
1348724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
1349724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
13502dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    QualType ParamType = adjustParameterType(ParamTypes[Idx]);
13512dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
1352724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
1353724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
1354724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
1355cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
135654e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
1357724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1358724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1359724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
1360724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1361724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1362e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
1363e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.Variadic = Variadic;
1364e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.TypeQuals = Quals;
1365c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor  EPI.RefQualifier = RefQualifier;
1366e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.ExtInfo = Info;
1367e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1368e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI);
1369724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
13701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1371949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
1372949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1373949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
1374949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
13750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR Qualifiers applied to the member pointer type
1376949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
1377949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
1378949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1379949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
1380949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
13811eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
13822865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
1383949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
1384949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
1385949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
1386949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
1387949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
1388949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1389949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
1390949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
1391949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1392949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
1393949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
1394949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
1395949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1396949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1397737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
1398949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
1399949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
14008d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
1401ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
1402949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1403949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1404949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1405949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
1406949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1407949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
1408949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1409949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1410949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1411949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
1412949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1413949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1414949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1415949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1416d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // In the Microsoft ABI, the class is allowed to be an incomplete
1417d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // type. In such cases, the compiler makes a worst-case assumption.
1418d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // We make no such assumption right now, so emit an error if the
1419d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // class isn't a complete type.
142020cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  if (Context.Target.getCXXABI() == CXXABI_Microsoft &&
1421d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis      RequireCompleteType(Loc, Class, diag::err_incomplete_type))
1422d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis    return QualType();
1423d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis
14242865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
1425949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
14261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14279a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
14289a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
14299a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
14309a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
14310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
14329a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
14339a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Loc The location of the entity whose type involves this
14349a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// block pointer type or, if there is no such entity, the location of the
14359a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type that will have block pointer type.
14369a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
14379a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
14389a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
14399a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
14409a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
14419a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
14422865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildBlockPointerType(QualType T,
14431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
14449a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
14450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
14469a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
14479a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
14489a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
14491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14502865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
14519a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
14529a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
1453b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1454b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
14553f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
1456a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
14573f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
14583f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
14593f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1460a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
1461f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
1462e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
1463a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
1464e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
14651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1466a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
1467e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
1468e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
1469e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
1470d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruthstatic void DiagnoseIgnoredQualifiers(unsigned Quals,
1471d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation ConstQualLoc,
1472d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation VolatileQualLoc,
1473d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation RestrictQualLoc,
1474d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      Sema& S) {
1475d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  std::string QualStr;
1476d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  unsigned NumQuals = 0;
1477d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  SourceLocation Loc;
1478d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1479d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint ConstFixIt;
1480d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint VolatileFixIt;
1481d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint RestrictFixIt;
1482d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1483a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg  const SourceManager &SM = S.getSourceManager();
1484a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg
1485d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to
1486d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // find a range and grow it to encompass all the qualifiers, regardless of
1487d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // the order in which they textually appear.
1488d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Const) {
1489d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc);
1490d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    QualStr = "const";
1491a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    ++NumQuals;
1492a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(ConstQualLoc, Loc))
1493a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = ConstQualLoc;
1494d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1495d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Volatile) {
1496d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc);
1497a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    QualStr += (NumQuals == 0 ? "volatile" : " volatile");
1498d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ++NumQuals;
1499a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(VolatileQualLoc, Loc))
1500a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = VolatileQualLoc;
1501d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1502d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Restrict) {
1503d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc);
1504a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    QualStr += (NumQuals == 0 ? "restrict" : " restrict");
1505d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ++NumQuals;
1506a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(RestrictQualLoc, Loc))
1507a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = RestrictQualLoc;
1508d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1509d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1510d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  assert(NumQuals > 0 && "No known qualifiers?");
1511d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1512d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  S.Diag(Loc, diag::warn_qual_return_type)
1513a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    << QualStr << NumQuals << ConstFixIt << VolatileFixIt << RestrictFixIt;
1514d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth}
1515d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
151698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump/// GetTypeForDeclarator - Convert the type for the specified
15178ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl/// declarator to Type instances.
1518402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor///
1519402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
1520402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// owns the declaration of a type (e.g., the definition of a struct
1521402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor/// type), then *OwnedDecl will receive the owned declaration.
1522bf1a028246d884a540aeafa38e89be59a269b072John McCall///
1523bf1a028246d884a540aeafa38e89be59a269b072John McCall/// The result of this call will never be null, but the associated
1524bf1a028246d884a540aeafa38e89be59a269b072John McCall/// type may be a null type if there's an unrecoverable error.
1525bf1a028246d884a540aeafa38e89be59a269b072John McCallTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
152634b41d939a1328f484511c6002ba2456db879a29Richard Smith                                           TagDecl **OwnedDecl,
152734b41d939a1328f484511c6002ba2456db879a29Richard Smith                                           bool AutoAllowedInTypeName) {
1528930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // Determine the type of the declarator. Not all forms of declarator
1529930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  // have a type.
1530930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
153105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  TypeSourceInfo *ReturnTypeInfo = 0;
1532711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  TypeProcessingState state(*this, D);
153404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
15358999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  // In C++0x, deallocation functions (normal and array operator delete)
15368999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  // are implicitly noexcept.
15378999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  bool ImplicitlyNoexcept = false;
15388999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl
15393f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
15403f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
15418999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl    if (getLangOptions().CPlusPlus0x) {
15428999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl      OverloadedOperatorKind OO = D.getName().OperatorFunctionId.Operator;
15438999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl      if (OO == OO_Delete || OO == OO_Array_Delete)
15448999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl        ImplicitlyNoexcept = true;
15458999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl    }
15468999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl    // Intentional fall-through.
15478999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  case UnqualifiedId::IK_Identifier:
15480486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
15493f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
1550711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    T = ConvertDeclSpecToType(*this, state);
15515db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
1552591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
1553b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      TagDecl* Owned = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
155415987970eeaa1842c29ec8797affd1c1dea05585Abramo Bagnara      // Owned declaration is embedded in declarator.
155515987970eeaa1842c29ec8797affd1c1dea05585Abramo Bagnara      Owned->setEmbeddedInDeclarator(true);
1556591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor      if (OwnedDecl) *OwnedDecl = Owned;
1557591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
1558930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
1559930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
15603f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
15610efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
15623f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
1563930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
156448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // "void" instead.
1565930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    T = Context.VoidTy;
1566930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
156748026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
156848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
156948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
157048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
157105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    T = GetTypeFromParser(D.getName().ConversionFunctionId,
1572bf1a028246d884a540aeafa38e89be59a269b072John McCall                          &ReturnTypeInfo);
157348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
1574930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
1575dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
1576711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
1577711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
1578711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1579e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith  // C++0x [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
15808110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // In C++0x, a function declarator using 'auto' must have a trailing return
15818110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // type (this is checked later) and we can skip this. In other languages
15828110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // using auto, we need to check regardless.
158334b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
15848110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith      (!getLangOptions().CPlusPlus0x || !D.isFunctionDeclarator())) {
1585baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
15861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1587baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
1588baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
1589baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      assert(0 && "K&R type lists aren't allowed in C++");
1590baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1591c05a94b7accd4035bf5d5897c434c445b22da855John McCall    case Declarator::ObjCPrototypeContext:
1592baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
1593baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
1594baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1595baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
1596baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      switch (cast<TagDecl>(CurContext)->getTagKind()) {
1597465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Enum: assert(0 && "unhandled tag kind"); break;
1598465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
1599465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
1600465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
16011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
1602baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1603baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
1604baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 4; // Exception declaration
1605baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1606baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
1607baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 5; // Template parameter
1608baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1609baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
161034b41d939a1328f484511c6002ba2456db879a29Richard Smith      Error = 6; // Block literal
161134b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
161234b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TemplateTypeArgContext:
161334b41d939a1328f484511c6002ba2456db879a29Richard Smith      Error = 7; // Template type argument
161434b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
1615162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
16163e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
1617162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      Error = 9; // Type alias
1618162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      break;
161934b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TypeNameContext:
162034b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!AutoAllowedInTypeName)
1621162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        Error = 11; // Generic
1622baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1623baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
1624baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
1625baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
1626baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
1627baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1628baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1629baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
1630ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith    if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1631ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith      Error = 8;
1632ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith
16338110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    // In Objective-C it is an error to use 'auto' on a function declarator.
16348110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    if (D.isFunctionDeclarator())
1635162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      Error = 10;
16368110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith
1637e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // C++0x [dcl.spec.auto]p2: 'auto' is always fine if the declarator
1638e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // contains a trailing return type. That is only legal at the outermost
1639e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // level. Check all declarator chunks (outermost first) anyway, to give
1640e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // better diagnostics.
16418110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    if (getLangOptions().CPlusPlus0x && Error != -1) {
1642e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1643e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned chunkIndex = e - i - 1;
1644e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        state.setCurrentChunkIndex(chunkIndex);
1645e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
1646e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (DeclType.Kind == DeclaratorChunk::Function) {
1647e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1648e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (FTI.TrailingReturnType) {
1649e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Error = -1;
1650e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            break;
1651e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          }
1652e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        }
1653e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
1654e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    }
1655e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
1656baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
1657baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
1658baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
1659baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      T = Context.IntTy;
1660baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
1661baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1662baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
1663e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
166434b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (T.isNull())
166534b41d939a1328f484511c6002ba2456db879a29Richard Smith    return Context.getNullTypeSourceInfo();
166634b41d939a1328f484511c6002ba2456db879a29Richard Smith
1667cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
1668cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
1669cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
1670cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
16711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1672162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  // Does this declaration declare a typedef-name?
1673162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  bool IsTypedefName =
1674162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef ||
16753e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasDeclContext ||
16763e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasTemplateContext;
1677162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
167898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
167998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
168098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
16818ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1682711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
1683711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
1684711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
16855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
16865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default: assert(0 && "Unknown decltype!");
1687075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
1688075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      T = BuildParenType(T);
1689075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
16905618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
16919af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
16929af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
16939af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner        Diag(DeclType.Loc, diag::err_blocks_disable);
16941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16952865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
16962865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
16972865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
16985618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
16995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
17006a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
17016a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
17026a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
17036a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
17046a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
17056a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
17066a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
1707c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) {
1708c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
17092865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
17102865474261a608c7873b87ba4af110d17907896dJohn McCall          T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
171114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
171214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
17132865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildPointerType(T, DeclType.Loc, Name);
17142865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
17152865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
1716711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
17175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
17180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
17196a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
17206a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
17216a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
17226a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
17236a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
17246a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
17256a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
17262865474261a608c7873b87ba4af110d17907896dJohn McCall      T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
17272865474261a608c7873b87ba4af110d17907896dJohn McCall
17282865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
17292865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
17302865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
17315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
17320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
17335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
17346a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
17356a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
17366a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
17376a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
17386a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
17396a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
17406a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
1741fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
174294f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
17435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
17445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
17455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
17465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
17475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
17485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
17495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
1750c05a94b7accd4035bf5d5897c434c445b22da855John McCall      if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
1751f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
1752f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
1753f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1754f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1755f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
1756f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
1757f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
17580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      T = BuildArrayType(T, ASM, ArraySize,
17590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                         Qualifiers::fromCVRMask(ATI.TypeQuals),
17607e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                         SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
17615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
17625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1763f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
17645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
17655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
17665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
17675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
17683cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
176934b41d939a1328f484511c6002ba2456db879a29Richard Smith      // Check for auto functions and trailing return type and adjust the
177034b41d939a1328f484511c6002ba2456db879a29Richard Smith      // return type accordingly.
177134b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!D.isInvalidType()) {
177234b41d939a1328f484511c6002ba2456db879a29Richard Smith        // trailing-return-type is only required if we're declaring a function,
177334b41d939a1328f484511c6002ba2456db879a29Richard Smith        // and not, for instance, a pointer to a function.
177434b41d939a1328f484511c6002ba2456db879a29Richard Smith        if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
177534b41d939a1328f484511c6002ba2456db879a29Richard Smith            !FTI.TrailingReturnType && chunkIndex == 0) {
177634b41d939a1328f484511c6002ba2456db879a29Richard Smith          Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
177734b41d939a1328f484511c6002ba2456db879a29Richard Smith               diag::err_auto_missing_trailing_return);
177834b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = Context.IntTy;
177934b41d939a1328f484511c6002ba2456db879a29Richard Smith          D.setInvalidType(true);
178034b41d939a1328f484511c6002ba2456db879a29Richard Smith        } else if (FTI.TrailingReturnType) {
1781e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          // T must be exactly 'auto' at this point. See CWG issue 681.
1782e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (isa<ParenType>(T)) {
1783e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1784e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith                 diag::err_trailing_return_in_parens)
1785e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith              << T << D.getDeclSpec().getSourceRange();
1786e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.setInvalidType(true);
1787e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          } else if (T.hasQualifiers() || !isa<AutoType>(T)) {
178834b41d939a1328f484511c6002ba2456db879a29Richard Smith            Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
178934b41d939a1328f484511c6002ba2456db879a29Richard Smith                 diag::err_trailing_return_without_auto)
179034b41d939a1328f484511c6002ba2456db879a29Richard Smith              << T << D.getDeclSpec().getSourceRange();
179134b41d939a1328f484511c6002ba2456db879a29Richard Smith            D.setInvalidType(true);
179234b41d939a1328f484511c6002ba2456db879a29Richard Smith          }
179334b41d939a1328f484511c6002ba2456db879a29Richard Smith
179434b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = GetTypeFromParser(
179534b41d939a1328f484511c6002ba2456db879a29Richard Smith            ParsedType::getFromOpaquePtr(FTI.TrailingReturnType),
179634b41d939a1328f484511c6002ba2456db879a29Richard Smith            &ReturnTypeInfo);
179734b41d939a1328f484511c6002ba2456db879a29Richard Smith        }
179834b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
179934b41d939a1328f484511c6002ba2456db879a29Richard Smith
1800e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // C99 6.7.5.3p1: The return type may not be a function or array type.
1801e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // For conversion functions, we'll diagnose this particular error later.
1802e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      if ((T->isArrayType() || T->isFunctionType()) &&
1803e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
1804e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned diagID = diag::err_func_returning_array_function;
1805e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // Last processing chunk in block context means this function chunk
1806e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // represents the block.
1807e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (chunkIndex == 0 &&
1808e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.getContext() == Declarator::BlockLiteralContext)
1809e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          diagID = diag::err_block_returning_array_function;
1810e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
1811e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        T = Context.IntTy;
1812e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        D.setInvalidType(true);
1813e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
1814e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
18155291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
18165291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
1817fff951371dfc309160a99d423e43a7841aeb35aaDouglas Gregor      if (isa<PointerType>(T) && T.getLocalCVRQualifiers() &&
18181e15394853bfae25112d9cc6b445504905e1f34aRafael Espindola          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId) &&
1819d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth          (!getLangOptions().CPlusPlus || !T->isDependentType())) {
1820d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?");
1821d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
1822d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer);
1823d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1824d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr;
1825d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1826d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DiagnoseIgnoredQualifiers(PTI.TypeQuals,
1827d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
1828d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
1829d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
1830d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            *this);
1831d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1832d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth      } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
18335291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor          (!getLangOptions().CPlusPlus ||
18345291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor           (!T->isDependentType() && !T->isRecordType()))) {
1835d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1836d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(),
1837d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getConstSpecLoc(),
1838d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getVolatileSpecLoc(),
1839d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getRestrictSpecLoc(),
1840d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  *this);
18415291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      }
1842d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1843402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1844402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
1845402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
1846b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
1847402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        if (Tag->isDefinition())
1848402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor          Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1849402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
1850402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
1851402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
18523cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
18533cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
1854162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (IsTypedefName && FTI.getExceptionSpecType())
1855162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef)
18563e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          << (D.getContext() == Declarator::AliasDeclContext ||
18573e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith              D.getContext() == Declarator::AliasTemplateContext);
18583cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
18592865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) {
18602865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
18612865474261a608c7873b87ba4af110d17907896dJohn McCall        T = Context.getFunctionNoProtoType(T);
18622865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
18632865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
18642865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
18652865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
18662865474261a608c7873b87ba4af110d17907896dJohn McCall        if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) {
1867965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
1868965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
1869965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
1870965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            if (Attrs->getKind() == AttributeList::AT_overloadable) {
1871965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
1872965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
1873965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
1874965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
1875965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
1876965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
1877965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1878c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
18792865474261a608c7873b87ba4af110d17907896dJohn McCall
18802865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
1881788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
1882788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
18832865474261a608c7873b87ba4af110d17907896dJohn McCall          Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
18842865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
18852865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
18862865474261a608c7873b87ba4af110d17907896dJohn McCall        }
18872865474261a608c7873b87ba4af110d17907896dJohn McCall
1888e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
1889e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
1890e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
1891c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
1892c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : FTI.RefQualifierIsLValueRef? RQ_LValue
1893c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : RQ_RValue;
1894c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
18955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
18965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
18975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        llvm::SmallVector<QualType, 16> ArgTys;
18982865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
18991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1901d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
19028123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
190378c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
19042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
19052dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
1906beb58cb83bd53b79b80fc6f9952efd985934cbfcDouglas Gregor          assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
19072dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
19085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
19095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
191072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
19112dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
19125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
19135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
19145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
19155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
19165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(DeclType.Loc, diag::err_void_only_param);
19172ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
19188123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
19192ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
19202ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
19215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Diag(FTI.ArgInfo[i].IdentLoc,
19224565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
19232ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
19248123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
19252ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
19262ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
19270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
19282ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner                Diag(DeclType.Loc, diag::err_void_param_qualified);
19291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19302ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
19312ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
19322ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
1933eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
1934eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
1935a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
1936eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              Param->setKNRPromoted(true);
1937183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
1938eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              if (BTy->getKind() == BuiltinType::Float) {
1939eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
1940eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall                Param->setKNRPromoted(true);
1941eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              }
1942eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
19435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
194456a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
194554e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
19465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
1947465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1948465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl        llvm::SmallVector<QualType, 4> Exceptions;
19498b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        EPI.ExceptionSpecType = FTI.getExceptionSpecType();
19508b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        if (FTI.getExceptionSpecType() == EST_Dynamic) {
1951e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          Exceptions.reserve(FTI.NumExceptions);
1952e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1953e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // FIXME: Preserve type source info.
1954e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1955e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // Check that the type is valid for an exception spec, and
1956e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            // drop it if not.
1957e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall            if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1958e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall              Exceptions.push_back(ET);
1959e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
1960373920bd733b1d28fe7bf209945a62eb9248d948John McCall          EPI.NumExceptions = Exceptions.size();
1961e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          EPI.Exceptions = Exceptions.data();
19628b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
196360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl          // If an error occurred, there's no expression here.
196460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl          if (Expr *NoexceptExpr = FTI.NoexceptExpr) {
196560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl            assert((NoexceptExpr->isTypeDependent() ||
196660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                    NoexceptExpr->getType()->getCanonicalTypeUnqualified() ==
196760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                        Context.BoolTy) &&
196860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                 "Parser should have made sure that the expression is boolean");
196960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl            SourceLocation ErrLoc;
197060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl            llvm::APSInt Dummy;
197160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl            if (!NoexceptExpr->isValueDependent() &&
197260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                !NoexceptExpr->isIntegerConstantExpr(Dummy, Context, &ErrLoc,
197360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                                     /*evaluated*/false))
197460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl              Diag(ErrLoc, diag::err_noexcept_needs_constant_expression)
197560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                  << NoexceptExpr->getSourceRange();
197660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl            else
197760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl              EPI.NoexceptExpr = NoexceptExpr;
197860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl          }
19798999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl        } else if (FTI.getExceptionSpecType() == EST_None &&
19808999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl                   ImplicitlyNoexcept && chunkIndex == 0) {
19818999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl          // Only the outermost chunk is marked noexcept, of course.
19828999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl          EPI.ExceptionSpecType = EST_BasicNoexcept;
1983ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl        }
1984465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1985e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI);
19865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
198704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
19885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
19895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1990f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
1991f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
19927bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
1993f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
19947bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
1995edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
1996edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
19977bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      } else if (isDependentScopeSpecifier(SS) ||
19987bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara                 dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS))) {
19991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
20007bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
200187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
200287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
200387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
20047bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
20054a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
200687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
200787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
200887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
200914aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor        case NestedNameSpecifier::NamespaceAlias:
201087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
20119f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
201287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
20137bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
201487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
201587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
201687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
201791ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Note: if the NNS has a prefix and ClsType is a nondependent
201891ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // TemplateSpecializationType, then the NNS prefix is NOT included
201991ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // in ClsType; hence we wrap ClsType into an ElaboratedType.
202091ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // NOTE: in particular, no wrap occurs if ClsType already is an
202191ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Elaborated, DependentName, or DependentTemplateSpecialization.
202291ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
20237bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
202487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
202587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
2026f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
2027949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        Diag(DeclType.Mem.Scope().getBeginLoc(),
2028949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
2029949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
2030949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
2031f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
2032f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2033f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2034949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
20352865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
2036949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
2037f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
2038949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
20392865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
20402865474261a608c7873b87ba4af110d17907896dJohn McCall        T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
2041f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2042f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
2043f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
2044f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2045cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
2046cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
2047cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
2048cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
2049cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
2050c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
2051711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
2052711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      processTypeAttrs(state, T, false, attrs);
20535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2054971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
2055971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  if (getLangOptions().CPlusPlus && T->isFunctionType()) {
2056183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
2057778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
2058971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
2059708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    // C++ 8.3.5p4:
2060708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
2061708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
2062708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
2063708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
2064683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    //
2065683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // Core issue 547 also allows cv-qualifiers on function types that are
2066683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // top-level template type arguments.
2067613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
2068613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
2069613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (D.getContext() != Declarator::MemberContext ||
2070613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall                      D.getDeclSpec().isFriendSpecified());
2071613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
2072613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      DeclContext *DC = computeDeclContext(D.getCXXScopeSpec());
2073613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
2074613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
2075613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
2076c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    // C++0x [dcl.fct]p6:
2077c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   A ref-qualifier shall only be part of the function type for a
2078c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   non-static member function, the function type to which a pointer to
2079c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   member refers, or the top-level function type of a function typedef
2080c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    //   declaration.
2081c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor    if ((FnTy->getTypeQuals() != 0 || FnTy->getRefQualifier()) &&
2082683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        !(D.getContext() == Declarator::TemplateTypeArgContext &&
2083162e1c1b487352434552147967c3dd296ebee2f7Richard Smith          !D.isFunctionDeclarator()) && !IsTypedefName &&
2084c61bb2056148891375bfa591fa2859b9b6ec2734Sebastian Redl        (FreeFunction ||
2085971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis         D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
2086683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      if (D.getContext() == Declarator::TemplateTypeArgContext) {
2087683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        // Accept qualified function types as template type arguments as a GNU
2088683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        // extension. This is also the subject of C++ core issue 547.
2089683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        std::string Quals;
2090683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        if (FnTy->getTypeQuals() != 0)
2091683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          Quals = Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
2092683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
2093683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        switch (FnTy->getRefQualifier()) {
2094683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        case RQ_None:
2095683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          break;
2096683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
2097683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        case RQ_LValue:
2098683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (!Quals.empty())
2099683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Quals += ' ';
2100683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          Quals += '&';
2101683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          break;
2102c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
2103683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        case RQ_RValue:
2104683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (!Quals.empty())
2105683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Quals += ' ';
2106683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          Quals += "&&";
2107683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          break;
2108683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
2109683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
2110683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        Diag(D.getIdentifierLoc(),
2111683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor             diag::ext_qualified_function_type_template_arg)
2112683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          << Quals;
2113683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      } else {
2114683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        if (FnTy->getTypeQuals() != 0) {
2115683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (D.isFunctionDeclarator())
2116683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Diag(D.getIdentifierLoc(),
2117683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                 diag::err_invalid_qualified_function_type);
2118683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          else
2119683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Diag(D.getIdentifierLoc(),
2120683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                 diag::err_invalid_qualified_typedef_function_type_use)
2121683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << FreeFunction;
2122683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
2123683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor
2124683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        if (FnTy->getRefQualifier()) {
2125683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          if (D.isFunctionDeclarator()) {
2126683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            SourceLocation Loc = D.getIdentifierLoc();
2127683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
2128683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              const DeclaratorChunk &Chunk = D.getTypeObject(N-I-1);
2129683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              if (Chunk.Kind == DeclaratorChunk::Function &&
2130683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                  Chunk.Fun.hasRefQualifier()) {
2131683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                Loc = Chunk.Fun.getRefQualifierLoc();
2132683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                break;
2133683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              }
2134944aa60777e6ea1015c9423107f7925f6d91f4a0Douglas Gregor            }
2135944aa60777e6ea1015c9423107f7925f6d91f4a0Douglas Gregor
2136683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Diag(Loc, diag::err_invalid_ref_qualifier_function_type)
2137683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << (FnTy->getRefQualifier() == RQ_LValue)
2138683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << FixItHint::CreateRemoval(Loc);
2139683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          } else {
2140683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor            Diag(D.getIdentifierLoc(),
2141683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                 diag::err_invalid_ref_qualifier_typedef_function_type_use)
2142683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << FreeFunction
2143683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor              << (FnTy->getRefQualifier() == RQ_LValue);
2144683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor          }
2145c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        }
2146c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
2147683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        // Strip the cv-qualifiers and ref-qualifiers from the type.
2148683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
2149683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        EPI.TypeQuals = 0;
2150683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        EPI.RefQualifier = RQ_None;
2151c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor
2152683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        T = Context.getFunctionType(FnTy->getResultType(),
2153683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                                    FnTy->arg_type_begin(),
2154683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor                                    FnTy->getNumArgs(), EPI);
2155683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      }
2156971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
2157971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
21581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2159711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
2160711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull())
2161711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = D.getAttributes())
2162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      processTypeAttrs(state, T, false, attrs);
2163711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
2165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
2166711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2167148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  // C++0x [dcl.constexpr]p9:
2168148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  //  A constexpr specifier used in an object declaration declares the object
2169148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  //  as const.
2170148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
2171737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
2172737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
2173737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
2174a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // If there was an ellipsis in the declarator, the declaration declares a
2175a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
2176a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (D.hasEllipsis() && !T.isNull()) {
2177a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
2178a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   A declarator-id or abstract-declarator containing an ellipsis shall
2179a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
2180a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
2181a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
2182a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
2183a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
2184a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   [...] When it is part of a parameter-declaration-clause, the
2185a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   parameter pack is a function parameter pack (14.5.3). The type T
2186a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
2187a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   a template parameter pack; each template parameter pack in T is
2188a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
2189a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2190a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
2191a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
2192a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
2193a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        Diag(D.getEllipsisLoc(),
2194a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
2195a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
2196a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
2197a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
2198cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
2199a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
2200a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2201a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
2202a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
2203a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
2204a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   If a template-parameter is a [...] is a parameter-declaration that
2205a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
2206a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
2207a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2208a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
2209a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
2210a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
2211a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
2212cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
221310738d36b150aa65206890c1c845cdba076e4200Douglas Gregor      else if (!getLangOptions().CPlusPlus0x)
22145ce5f5221217b64193799c2a4d5aa84432d3fba4Douglas Gregor        Diag(D.getEllipsisLoc(), diag::ext_variadic_templates);
2215a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2216a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
2217a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
2218a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
2219c05a94b7accd4035bf5d5897c434c445b22da855John McCall    case Declarator::ObjCPrototypeContext: // FIXME: special diagnostic here?
2220a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
2221162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
22223e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
2223a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
2224a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
2225a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
2226a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
2227a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
2228a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
2229683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    case Declarator::TemplateTypeArgContext:
2230a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
2231a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
2232a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
2233a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
2234a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2235a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
2236a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
2237e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2238bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
2239bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
2240bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
2241bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
2242bf1a028246d884a540aeafa38e89be59a269b072John McCall  return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo);
22435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
224514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall/// Map an AttributedType::Kind to an AttributeList::Kind.
224614aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
224714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  switch (kind) {
224814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_address_space:
224914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_address_space;
225014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_regparm:
225114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_regparm;
225214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_vector_size:
225314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_vector_size;
225414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_vector_type:
225514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_neon_vector_type;
225614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_polyvector_type:
225714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_neon_polyvector_type;
225814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_objc_gc:
225914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_objc_gc;
226014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_noreturn:
226114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_noreturn;
226214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_cdecl:
226314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_cdecl;
226414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_fastcall:
226514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_fastcall;
226614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_stdcall:
226714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_stdcall;
226814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_thiscall:
226914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_thiscall;
227014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_pascal:
227114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    return AttributeList::AT_pascal;
2272414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case AttributedType::attr_pcs:
2273414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    return AttributeList::AT_pcs;
227414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
227514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  llvm_unreachable("unexpected attribute kind!");
227614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  return AttributeList::Kind();
227714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
227814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
227914aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic void fillAttributedTypeLoc(AttributedTypeLoc TL,
228014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                  const AttributeList *attrs) {
228114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributedType::Kind kind = TL.getAttrKind();
228214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
228314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  assert(attrs && "no type attributes in the expected location!");
228414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributeList::Kind parsedKind = getAttrListKind(kind);
228514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  while (attrs->getKind() != parsedKind) {
228614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    attrs = attrs->getNext();
228714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    assert(attrs && "no matching attribute in expected location!");
228814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
228914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
229014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  TL.setAttrNameLoc(attrs->getLoc());
229114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrExprOperand())
229214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrExprOperand(attrs->getArg(0));
229314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  else if (TL.hasAttrEnumOperand())
229414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrEnumOperandLoc(attrs->getParameterLoc());
229514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
229614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // FIXME: preserve this information to here.
229714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrOperand())
229814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrOperandParensRange(SourceRange());
229914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
230014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
230151bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
230251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
2303c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    ASTContext &Context;
230451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
2305f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
230651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
2307c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
2308c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      : Context(Context), DS(DS) {}
2309f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
231014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
231114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, DS.getAttributes().getList());
231214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      Visit(TL.getModifiedLoc());
231314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
231451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
231551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
231651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
231751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
231851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
231951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
232051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
232151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
2322c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
2323c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2324c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
2325c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
2326c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
2327c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.getBaseLoc().initialize(Context, SourceLocation());
2328c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
2329c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
2330c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
2331c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
233254e14c4db764c0636160d26c5bbf491637c83a76John McCall
2333c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
233454e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
233554e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
233654e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
233754e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
233854e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
233954e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
234054e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
234154e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
234254e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
234354e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
234454e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
234554e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
234651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
234754e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
234854e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
2349c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
235051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
2351833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
2352a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
2353b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2354833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2355833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
2356833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
2357a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
23580daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara        TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
2359833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
2360833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
2361833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2362e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
2363e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
2364e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
2365e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TemplateSpecializationTypeLoc NamedTL =
2366e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
2367e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
2368e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2369e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      else
2370e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
2371833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
2372cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
2373cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
2374cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2375cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
2376cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
2377cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
2378cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
2379cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2380cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
2381b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
2382cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
2383b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2384cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
2385cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
2386ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
2387ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      // FIXME: This holds only because we only have one unary transform.
2388ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType);
2389ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setKWLoc(DS.getTypeSpecTypeLoc());
2390ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setParensRange(DS.getTypeofParensRange());
2391ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getRepAsType());
2392ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TypeSourceInfo *TInfo = 0;
2393ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2394ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setUnderlyingTInfo(TInfo);
2395ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
2396ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
2397ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
2398ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
2399ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
2400ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
2401ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
2402ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
2403ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
2404ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
2405ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
2406ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
2407ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
2408ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
2409ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
2410ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
2411e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
2412e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
2413e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
2414253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
2415e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
2416b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2417e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
2418e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
2419e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
2420e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
2421e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2422e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setKeywordLoc(Keyword != ETK_None
2423e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       ? DS.getTypeSpecTypeLoc()
2424e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       : SourceLocation());
2425e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
24269e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
2427e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
2428e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
2429e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
2430e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
2431e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
2432253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
2433e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
2434b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
2435e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
2436e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
2437e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
2438e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
2439e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
2440e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TL.setKeywordLoc(Keyword != ETK_None
2441e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       ? DS.getTypeSpecTypeLoc()
2442e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara                       : SourceLocation());
2443e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
24442494dd024b392b8def58bf067cc94b51c214cf77Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
24450daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
244633500955d731c73717af52088b7fc0e7a85681e7John McCall    }
244733500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
244833500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
244933500955d731c73717af52088b7fc0e7a85681e7John McCall      ElaboratedTypeKeyword Keyword
245033500955d731c73717af52088b7fc0e7a85681e7John McCall        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
245133500955d731c73717af52088b7fc0e7a85681e7John McCall      if (Keyword == ETK_Typename) {
245233500955d731c73717af52088b7fc0e7a85681e7John McCall        TypeSourceInfo *TInfo = 0;
2453b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
245433500955d731c73717af52088b7fc0e7a85681e7John McCall        if (TInfo) {
245533500955d731c73717af52088b7fc0e7a85681e7John McCall          TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
245633500955d731c73717af52088b7fc0e7a85681e7John McCall                    TInfo->getTypeLoc()));
245733500955d731c73717af52088b7fc0e7a85681e7John McCall          return;
245833500955d731c73717af52088b7fc0e7a85681e7John McCall        }
245933500955d731c73717af52088b7fc0e7a85681e7John McCall      }
2460c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initializeLocal(Context, SourceLocation());
246133500955d731c73717af52088b7fc0e7a85681e7John McCall      TL.setKeywordLoc(Keyword != ETK_None
246233500955d731c73717af52088b7fc0e7a85681e7John McCall                       ? DS.getTypeSpecTypeLoc()
246333500955d731c73717af52088b7fc0e7a85681e7John McCall                       : SourceLocation());
246433500955d731c73717af52088b7fc0e7a85681e7John McCall      const CXXScopeSpec& SS = DS.getTypeSpecScope();
246594fdffa4a572fc14ac296f5f1aae9db3734c72f1Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
24660daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
24670daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    }
24680daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    void VisitTagTypeLoc(TagTypeLoc TL) {
24690daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
2470e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
2471e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
247251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
247351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
2474c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initialize(Context, DS.getTypeSpecTypeLoc());
247551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
247651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
2477eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
247851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
2479b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    ASTContext &Context;
248051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
2481f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
248251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
2483b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
2484b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      : Context(Context), Chunk(Chunk) {}
24854adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
248651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
24879f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
248851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
24894adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
249051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
249151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
249251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
24934adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
249451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
249551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
249651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
24974adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
249851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
249951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
250051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
25014adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
250251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
250351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
2504b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const CXXScopeSpec& SS = Chunk.Mem.Scope();
2505b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
2506b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
2507b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const Type* ClsTy = TL.getClass();
2508b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      QualType ClsQT = QualType(ClsTy, 0);
2509b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
2510b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Now copy source location info into the type loc component.
2511b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeLoc ClsTL = ClsTInfo->getTypeLoc();
2512b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
2513b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Identifier:
2514b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
2515b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        {
2516fd9c42ed22fb4f7f865f7d8f8848df84ddf8262cAbramo Bagnara          DependentNameTypeLoc DNTLoc = cast<DependentNameTypeLoc>(ClsTL);
2517b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setKeywordLoc(SourceLocation());
2518b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
2519b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
2520b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
2521b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
2522b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
2523b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpec:
2524b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpecWithTemplate:
2525b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        if (isa<ElaboratedType>(ClsTy)) {
2526b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ElaboratedTypeLoc ETLoc = *cast<ElaboratedTypeLoc>(&ClsTL);
2527b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ETLoc.setKeywordLoc(SourceLocation());
2528b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ETLoc.setQualifierLoc(NNSLoc.getPrefix());
2529b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
2530b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
2531b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        } else {
2532b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
2533b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
2534b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
2535b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
2536b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Namespace:
2537b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::NamespaceAlias:
2538b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Global:
2539b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        llvm_unreachable("Nested-name-specifier must name a type");
2540b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
2541b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      }
2542b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
2543b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Finally fill in MemberPointerLocInfo fields.
254451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
2545b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TL.setClassTInfo(ClsTInfo);
25464adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
254751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
254851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
254954e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
255054e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
255151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
255251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
255351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
255451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
255551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
255651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
255751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
255851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
255951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
256051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
256151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
256251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
256351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
256451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
256551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
2566796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeBegin(Chunk.Loc);
2567796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeEnd(Chunk.EndLoc);
2568dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor      TL.setTrailingReturn(!!Chunk.Fun.TrailingReturnType);
256951bd803fbdade51d674598ed45da3d54190a656cJohn McCall
257051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
257154e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
2572d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
257354e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
25744adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
257551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
25764adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
2577075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
2578075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
2579075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
2580075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
2581075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
25821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
258351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
25849f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
25854adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
258651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
258751bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
25884adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
2589a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
259051bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
259151bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
259205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
259305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
259405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
259505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
259605baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
2597a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
259805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
259905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
2600a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
2601a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
260251bd803fbdade51d674598ed45da3d54190a656cJohn McCall
2603a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
2604a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
2605a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc());
2606a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
2607a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
2608a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor
26098ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
261014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    while (isa<AttributedTypeLoc>(CurrTL)) {
261114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      AttributedTypeLoc TL = cast<AttributedTypeLoc>(CurrTL);
261214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs());
261314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
261414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
261514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
2616b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
261751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
26184adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
2619f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
2620b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
2621b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
2622b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
262305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
262405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
262505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
2626b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
2627c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
262805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
262905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor
2630a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
26314adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
26324adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
2633a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
2634b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
26351bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
26361bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
26371bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
2638eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
2639eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
2640a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
26411bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
26421bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
2643b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
26441bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
26451bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
26461bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
26471bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
264835d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis  assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
264935d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
265035d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
26511bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
26521bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
2653f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
26545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
26555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
26565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
26571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2658402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  TagDecl *OwnedTag = 0;
2659bf1a028246d884a540aeafa38e89be59a269b072John McCall  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag);
2660bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
26615153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
2662809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
26635912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
2664402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  if (getLangOptions().CPlusPlus) {
2665402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
26666d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
26676d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor
2668402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // C++0x [dcl.type]p3:
26693e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    //   A type-specifier-seq shall not define a class or enumeration unless
26703e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    //   it appears in the type-id of an alias-declaration (7.1.3) that is not
26713e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    //   the declaration of a template-declaration.
26723e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (OwnedTag && OwnedTag->isDefinition()) {
26733e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      if (D.getContext() == Declarator::AliasTemplateContext)
26743e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith        Diag(OwnedTag->getLocation(), diag::err_type_defined_in_alias_template)
26753e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          << Context.getTypeDeclType(OwnedTag);
26763e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      else if (D.getContext() != Declarator::AliasDeclContext)
26773e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith        Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
26783e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          << Context.getTypeDeclType(OwnedTag);
26793e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    }
2680402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
2681402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
2682b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
26835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
26845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2685c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
2686c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
2687c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
2688c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
2689c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
2690232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
2691232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
2692c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
2693c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
26941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
2695c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
26960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
2697232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
2698232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
2699232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // for two or more different address spaces."
2700232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
2701c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
2702e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2703c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
2704232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
27051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2706232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
2707545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
2708f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2709e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2710c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
2711232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
2712545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
2713232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
2714ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
2715ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
2716dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
2717dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
2718e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2719c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
2720232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
2721232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
2722efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
2723efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
2724efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
2725efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
2726efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
2727e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
2728efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
2729efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
2730efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
2731efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
2732efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
27330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
2734efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
2735efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
27360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
2737e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
2738efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
2739efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
2740efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
27411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
2742f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
2743c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
2744c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
2745711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
2746711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
2747711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
2748711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
2749711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
2750711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
2751711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
2752711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
2753711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2754711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
2755711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
2756711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
2757711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
2758711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
2759711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2760711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
2761711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
2762711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2763711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2764d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
27651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2766d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
2767711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!attr.getParameterName()) {
2768711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2769ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
2770711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2771711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2772ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
27730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
2774711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getNumArgs() != 0) {
2775711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2776711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2777711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2778d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
2779711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getParameterName()->isStr("weak"))
27800953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
2781711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  else if (attr.getParameterName()->isStr("strong"))
27820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
2783d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
2784711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
2785711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      << "objc_gc" << attr.getParameterName();
2786711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
2787711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2788d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
27891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
279014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  QualType origType = type;
279114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  type = S.Context.getObjCGCQualType(origType, GCAttr);
279214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
279314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // Make an attributed type to preserve the source information.
279414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (attr.getLoc().isValid())
279514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
279614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                       origType, type);
279714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
2798711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
2799d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
2800d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
2801e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallnamespace {
2802e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// A helper class to unwrap a type down to a function for the
2803e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// purposes of applying attributes there.
2804e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///
2805e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// Use:
2806e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
2807e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   if (unwrapped.isFunctionType()) {
2808e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     const FunctionType *fn = unwrapped.get();
2809e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     // change fn somehow
2810e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     T = unwrapped.wrap(fn);
2811e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   }
2812e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  struct FunctionTypeUnwrapper {
2813e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    enum WrapKind {
2814e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Desugar,
2815e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Parens,
2816e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Pointer,
2817e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      BlockPointer,
2818e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Reference,
2819e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      MemberPointer
2820e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    };
2821e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2822e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType Original;
2823e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *Fn;
2824e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    llvm::SmallVector<unsigned char /*WrapKind*/, 8> Stack;
2825e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2826e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
2827e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      while (true) {
2828e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const Type *Ty = T.getTypePtr();
2829e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<FunctionType>(Ty)) {
2830e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Fn = cast<FunctionType>(Ty);
2831e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return;
2832e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ParenType>(Ty)) {
2833e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ParenType>(Ty)->getInnerType();
2834e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Parens);
2835e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<PointerType>(Ty)) {
2836e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<PointerType>(Ty)->getPointeeType();
2837e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Pointer);
2838e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<BlockPointerType>(Ty)) {
2839e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<BlockPointerType>(Ty)->getPointeeType();
2840e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(BlockPointer);
2841e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<MemberPointerType>(Ty)) {
2842e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<MemberPointerType>(Ty)->getPointeeType();
2843e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(MemberPointer);
2844e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ReferenceType>(Ty)) {
2845e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ReferenceType>(Ty)->getPointeeType();
2846e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Reference);
2847e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else {
2848e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          const Type *DTy = Ty->getUnqualifiedDesugaredType();
2849e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          if (Ty == DTy) {
2850e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            Fn = 0;
2851e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            return;
2852e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          }
2853e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2854e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = QualType(DTy, 0);
2855e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Desugar);
2856e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        }
2857e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2858e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2859e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2860e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    bool isFunctionType() const { return (Fn != 0); }
2861e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *get() const { return Fn; }
2862e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2863e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(Sema &S, const FunctionType *New) {
2864e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // If T wasn't modified from the unwrapped type, do nothing.
2865e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (New == get()) return Original;
2866e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2867e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Fn = New;
2868e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return wrap(S.Context, Original, 0);
2869e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2870e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2871e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  private:
2872e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
2873e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size())
2874e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getQualifiedType(Fn, Old.getQualifiers());
2875e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2876e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // Build up the inner type, applying the qualifiers from the old
2877e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // type to the new type.
2878e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      SplitQualType SplitOld = Old.split();
2879e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2880e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // As a special case, tail-recurse if there are no qualifiers.
2881e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (SplitOld.second.empty())
2882e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, SplitOld.first, I);
2883e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return C.getQualifiedType(wrap(C, SplitOld.first, I), SplitOld.second);
2884e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2885e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2886e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
2887e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size()) return QualType(Fn, 0);
2888e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2889e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      switch (static_cast<WrapKind>(Stack[I++])) {
2890e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Desugar:
2891e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // This is the point at which we potentially lose source
2892e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // information.
2893e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
2894e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2895e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Parens: {
2896e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
2897e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getParenType(New);
2898e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2899e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2900e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Pointer: {
2901e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
2902e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getPointerType(New);
2903e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2904e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2905e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case BlockPointer: {
2906e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
2907e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getBlockPointerType(New);
2908e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2909e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2910e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case MemberPointer: {
2911e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
2912e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldMPT->getPointeeType(), I);
2913e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getMemberPointerType(New, OldMPT->getClass());
2914e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2915e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2916e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Reference: {
2917e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const ReferenceType *OldRef = cast<ReferenceType>(Old);
2918e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldRef->getPointeeType(), I);
2919e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<LValueReferenceType>(OldRef))
2920e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
2921e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        else
2922e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getRValueReferenceType(New);
2923e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2924e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
2925e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2926e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      llvm_unreachable("unknown wrapping kind");
2927e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return QualType();
2928e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
2929e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  };
2930e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
2931e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2932711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
2933711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
2934711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
2935711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
2936711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
2937711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
2938e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2939711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
29402455636163fdd18581d7fdae816433f886d88213Mike Stump
2941711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getKind() == AttributeList::AT_noreturn) {
2942711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
294304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
2944e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2945e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
2946711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
2947711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
2948425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
2949425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
2950711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
2951711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2952711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2953711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
2954425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
2955711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getKind() == AttributeList::AT_regparm) {
2956711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
2957711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
2958711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
29591e030eb1194763b42c1752723be23b1515f48981John McCall
2960711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
2961711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
2962008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
29631e030eb1194763b42c1752723be23b1515f48981John McCall
2964ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Diagnose regparm with fastcall.
2965ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    const FunctionType *fn = unwrapped.get();
2966ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    CallingConv CC = fn->getCallConv();
2967ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (CC == CC_X86FastCall) {
2968ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
2969ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC)
2970ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm";
2971ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
2972ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
2973ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
2974ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
2975e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionType::ExtInfo EI =
2976711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
2977711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2978711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2979425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
2980425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
298104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
2982711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
2983711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
2984711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2985f82b4e85b1219295cad4b5851b035575bc293010John McCall
298604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Delay if the type didn't work out to a function.
2987711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!unwrapped.isFunctionType()) return false;
298804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
2989711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
2990711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
2991064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis  if (S.Context.getCanonicalCallConv(CC) ==
2992e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      S.Context.getCanonicalCallConv(CCOld)) {
2993ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
2994ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2995711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
2996e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  }
299704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
299804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CCOld != CC_Default) {
299904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Should we diagnose reapplications of the same convention?
3000711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
300104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CC)
300204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CCOld);
3003711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3004711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
300504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
300604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
300704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
300804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
3009711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isa<FunctionNoProtoType>(fn)) {
3010711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_knr)
301104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
3012711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
3013711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
301404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
301504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
3016711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
301704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
3018711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
301904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
3020711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
3021711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
302204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
3023ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
3024ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Also diagnose fastcall with regparm.
3025a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    if (fn->getHasRegParm()) {
3026ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
3027ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm"
3028ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC);
3029ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
3030ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
3031ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
303204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
3033f82b4e85b1219295cad4b5851b035575bc293010John McCall
3034711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
3035711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3036711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
3037f82b4e85b1219295cad4b5851b035575bc293010John McCall}
3038f82b4e85b1219295cad4b5851b035575bc293010John McCall
3039207f4d8543529221932af82836016a2ef066c917Peter Collingbourne/// Handle OpenCL image access qualifiers: read_only, write_only, read_write
3040207f4d8543529221932af82836016a2ef066c917Peter Collingbournestatic void HandleOpenCLImageAccessAttribute(QualType& CurType,
3041207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const AttributeList &Attr,
3042207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             Sema &S) {
3043207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  // Check the attribute arguments.
3044207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (Attr.getNumArgs() != 1) {
3045207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3046207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
3047207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
3048207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
3049207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
3050207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  llvm::APSInt arg(32);
3051207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
3052207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      !sizeExpr->isIntegerConstantExpr(arg, S.Context)) {
3053207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3054207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << "opencl_image_access" << sizeExpr->getSourceRange();
3055207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
3056207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
3057207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
3058207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  unsigned iarg = static_cast<unsigned>(arg.getZExtValue());
3059207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  switch (iarg) {
3060207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_only:
3061207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_write_only:
3062207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_write:
3063207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
3064207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
3065207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  default:
3066207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
3067207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
3068207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << sizeExpr->getSourceRange();
3069207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
3070207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
3071207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
3072207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
3073207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
30746e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
30756e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
30766e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
30776e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
30786e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
30796e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
30806e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
3081788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
3082788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
308356affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
30846e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
30856e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3086e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
30876e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
30886e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
30896e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
30906e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
3091ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
3092ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
30936e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
30946e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << "vector_size" << sizeExpr->getSourceRange();
3095e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
30966e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
30976e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
30986e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
3099f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
31006e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
3101e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
31026e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
31036e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
31046e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
31056e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
31066e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
31076e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
31086e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
31096e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
31106e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
31116e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
3112e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
31136e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
31146e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
31156e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
31166e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
31176e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
3118e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
31196e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
31206e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
31216e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
31226e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
31236e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
3124788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
3125e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
31266e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
31276e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
31284211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
31294211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
31304211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
31314211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
31324211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
31334211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
31344211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
31354211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
31364211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
31374211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     VectorType::VectorKind VecKind,
31384211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const char *AttrName) {
31394211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
31404211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
31414211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
31424211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
31434211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
31444211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
31454211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
31464211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
31474211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
31484211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
31494211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
31504211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
31514211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      << AttrName << numEltsExpr->getSourceRange();
31524211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
31534211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
31544211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
31554211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
31564211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  const BuiltinType* BTy = CurType->getAs<BuiltinType>();
31574211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (!BTy ||
31584211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (VecKind == VectorType::NeonPolyVector &&
31594211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::SChar &&
31604211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short) ||
31614211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (BTy->getKind() != BuiltinType::SChar &&
31624211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UChar &&
31634211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short &&
31644211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UShort &&
31654211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Int &&
31664211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UInt &&
31674211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::LongLong &&
31684211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::ULongLong &&
31694211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Float)) {
31704211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
31714211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
31724211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
31734211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
31744211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
31754211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
31764211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
31774211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
31784211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
31794211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
31804211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
31814211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
31824211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
31834211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
31844211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
31854211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
31864211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
3187711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
3188711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             bool isDeclSpec, AttributeList *attrs) {
3189c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
3190c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
3191c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
3192c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
3193711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3194711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
3195711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
3196711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
3197711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr.getNext();
3198711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3199e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
3200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
3201e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
3202e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
3203b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
3204b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
3205711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
3206c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
320704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
3208c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    case AttributeList::AT_address_space:
3209711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
3210c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
3211711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
3212711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
3213711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
3214d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
321504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    case AttributeList::AT_vector_size:
3216711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
3217f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
32184211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    case AttributeList::AT_neon_vector_type:
3219711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
3220711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonVector, "neon_vector_type");
32214211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
32224211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    case AttributeList::AT_neon_polyvector_type:
3223711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
3224711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonPolyVector,
32254211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                               "neon_polyvector_type");
32264211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
322704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
3228207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    case AttributeList::AT_opencl_image_access:
3229207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      HandleOpenCLImageAccessAttribute(type, attr, state.getSema());
3230207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      break;
3231207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
3232711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
3233711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
3234711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
3235711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (isDeclSpec)
3236711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
3237711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3238711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
3239711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
3240711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
32416e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
3242c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
3243711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attrs = next));
3244232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
3245232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3246e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \brief Ensure that the type of the given expression is complete.
3247e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
3248e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// This routine checks whether the expression \p E has a complete type. If the
3249e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// expression refers to an instantiable construct, that instantiation is
3250e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// performed as needed to complete its type. Furthermore
3251e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// Sema::RequireCompleteType is called for the expression's type (or in the
3252e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// case of a reference type, the referred-to type).
3253e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
3254e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \param E The expression whose type is required to be complete.
3255e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \param PD The partial diagnostic that will be printed out if the type cannot
3256e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// be completed.
3257e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
3258e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \returns \c true if the type of \p E is incomplete and diagnosed, \c false
3259e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// otherwise.
3260e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruthbool Sema::RequireCompleteExprType(Expr *E, const PartialDiagnostic &PD,
3261e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth                                   std::pair<SourceLocation,
3262e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth                                             PartialDiagnostic> Note) {
3263e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  QualType T = E->getType();
3264e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
3265e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Fast path the case where the type is already complete.
3266e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (!T->isIncompleteType())
3267e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    return false;
3268e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
3269e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Incomplete array types may be completed by the initializer attached to
3270e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // their definitions. For static data members of class templates we need to
3271e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // instantiate the definition to get this initializer and complete the type.
3272e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (T->isIncompleteArrayType()) {
3273e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
3274e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
3275e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        if (Var->isStaticDataMember() &&
3276e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth            Var->getInstantiatedFromStaticDataMember()) {
327736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor
327836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
327936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          assert(MSInfo && "Missing member specialization information?");
328036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          if (MSInfo->getTemplateSpecializationKind()
328136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                != TSK_ExplicitSpecialization) {
328236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // If we don't already have a point of instantiation, this is it.
328336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (MSInfo->getPointOfInstantiation().isInvalid()) {
328436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              MSInfo->setPointOfInstantiation(E->getLocStart());
328536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor
328636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              // This is a modification of an existing AST node. Notify
328736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              // listeners.
328836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              if (ASTMutationListener *L = getASTMutationListener())
328936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                L->StaticDataMemberInstantiated(Var);
329036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
329136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor
329236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            InstantiateStaticDataMemberDefinition(E->getExprLoc(), Var);
329336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor
329436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // Update the type to the newly instantiated definition's type both
329536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // here and within the expression.
329636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (VarDecl *Def = Var->getDefinition()) {
329736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setDecl(Def);
329836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              T = Def->getType();
329936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setType(T);
330036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              E->setType(T);
330136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
3302f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor          }
3303f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor
3304e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // We still go on to try to complete the type independently, as it
3305e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // may also require instantiations or diagnostics if it remains
3306e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // incomplete.
3307e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        }
3308e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      }
3309e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    }
3310e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  }
3311e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
3312e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // FIXME: Are there other cases which require instantiating something other
3313e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // than the type to complete the type of an expression?
3314e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
3315e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Look through reference types and complete the referred type.
3316e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
3317e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    T = Ref->getPointeeType();
3318e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
3319e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  return RequireCompleteType(E->getExprLoc(), T, PD, Note);
3320e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth}
3321e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
33221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
33234ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
33244ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
33254ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
332686447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
332786447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
332886447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
332986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
333086447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
33314ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
33324ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
33334ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
33344ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
33354ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
33364ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
33371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @param PD The partial diagnostic that will be printed out if T is not a
3338b790661a15d93941d2c33a0ea328254277b3d7e3Anders Carlsson/// complete type.
33394ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
33404ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
33414ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
334291a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
33438c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               const PartialDiagnostic &PD,
33448c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                               std::pair<SourceLocation,
33458c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson                                         PartialDiagnostic> Note) {
334691a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  unsigned diag = PD.getDiagID();
33471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3348573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
3349573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
3350690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
3351690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
3352690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
33531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
3354690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
3355690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
33564ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
33574ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (!T->isIncompleteType())
33584ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
33594ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
3360d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
3361923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
3362923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
3363923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
336489c49f09b0292dc7c03885f6c765d667a9837597Douglas Gregor  if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
3365923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
3366923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
33672943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
3368d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
3369972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
3370972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
3371d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
33725842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor                                                      /*Complain=*/diag != 0);
33731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
3374d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
3375d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
3376b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
3377b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        assert(MSInfo && "Missing member specialization information?");
3378357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
3379b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor        if (MSInfo->getTemplateSpecializationKind()
3380972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor                                               != TSK_ExplicitSpecialization)
3381f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
3382f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
3383f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
3384f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  /*Complain=*/diag != 0);
3385d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
3386d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
3387d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
33882943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
33895842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor  if (diag == 0)
33905842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
33911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3392916c870442978db40404d51348cdf5524e506faaJohn McCall  const TagType *Tag = T->getAs<TagType>();
339301620704304f819b82ecef769ec114e541a364d7Rafael Espindola
339401620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // Avoid diagnosing invalid decls as incomplete.
339501620704304f819b82ecef769ec114e541a364d7Rafael Espindola  if (Tag && Tag->getDecl()->isInvalidDecl())
339601620704304f819b82ecef769ec114e541a364d7Rafael Espindola    return true;
339701620704304f819b82ecef769ec114e541a364d7Rafael Espindola
3398916c870442978db40404d51348cdf5524e506faaJohn McCall  // Give the external AST source a chance to complete the type.
3399916c870442978db40404d51348cdf5524e506faaJohn McCall  if (Tag && Tag->getDecl()->hasExternalLexicalStorage()) {
3400916c870442978db40404d51348cdf5524e506faaJohn McCall    Context.getExternalSource()->CompleteType(Tag->getDecl());
3401916c870442978db40404d51348cdf5524e506faaJohn McCall    if (!Tag->isIncompleteType())
3402916c870442978db40404d51348cdf5524e506faaJohn McCall      return false;
3403916c870442978db40404d51348cdf5524e506faaJohn McCall  }
3404916c870442978db40404d51348cdf5524e506faaJohn McCall
34054ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
340691a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  Diag(Loc, PD) << T;
34073c0eb160ca1361a82b9f15b3b40a2425adc14d0fEli Friedman
34088c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  // If we have a note, produce it.
34098c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson  if (!Note.first.isInvalid())
34108c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson    Diag(Note.first, Note.second);
34118c8d91917c307dc3ba4f60661377c745f2a6bef2Anders Carlsson
34124ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
341301620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
34144ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
34151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
34164ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
34174ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
34184ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor        << QualType(Tag, 0);
34194ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
34204ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
34214ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
3422e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
3423fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
3424fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               const PartialDiagnostic &PD) {
3425fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PD,
3426fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
3427fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
3428fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
3429fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
3430fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                               unsigned DiagID) {
3431fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  return RequireCompleteType(Loc, T, PDiag(DiagID),
3432fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                             std::make_pair(SourceLocation(), PDiag(0)));
3433fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
3434fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
3435465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
3436465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
3437465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
3438465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
3439465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
3440e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
3441465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
3442e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
3443465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
3444465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
3445465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
3446465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
3447465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
3448465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
3449465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
3450e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
3451af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
34522a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
3453fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
34542a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
34552a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
34562a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
34572b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
34582b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
3459aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
3460aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
34612b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
3462af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
3463af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
3464af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
34652a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
3466fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
34672a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
34682a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
34694b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
3470af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getDecltypeType(E);
3471af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
3472ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
3473ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType Sema::BuildUnaryTransformType(QualType BaseType,
3474ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::UTTKind UKind,
3475ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       SourceLocation Loc) {
3476ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  switch (UKind) {
3477ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case UnaryTransformType::EnumUnderlyingType:
3478ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) {
3479ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Diag(Loc, diag::err_only_enums_have_underlying_types);
3480ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return QualType();
3481ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    } else {
3482ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      QualType Underlying = BaseType;
3483ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      if (!BaseType->isDependentType()) {
3484ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl();
3485ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        assert(ED && "EnumType has no EnumDecl");
3486ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        DiagnoseUseOfDecl(ED, Loc);
3487ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        Underlying = ED->getIntegerType();
3488ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      }
3489ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(!Underlying.isNull());
3490ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return Context.getUnaryTransformType(BaseType, Underlying,
3491ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                        UnaryTransformType::EnumUnderlyingType);
3492ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
3493ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  }
3494ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  llvm_unreachable("unknown unary transform type");
3495ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
3496