SemaType.cpp revision 6666ed4ed2e2bc13da5ac5d0a4947019137d45be
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
14f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor#include "clang/Sema/ScopeInfo.h"
152d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
167cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/Sema/Template.h"
17207f4d8543529221932af82836016a2ef066c917Peter Collingbourne#include "clang/Basic/OpenCL.h"
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
1936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor#include "clang/AST/ASTMutationListener.h"
20a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor#include "clang/AST/CXXInheritance.h"
21980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
222943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor#include "clang/AST/DeclTemplate.h"
234adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
2451bd803fbdade51d674598ed45da3d54190a656cJohn McCall#include "clang/AST/TypeLocVisitor.h"
25e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
2691a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
27d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis#include "clang/Basic/TargetInfo.h"
282792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall#include "clang/Lex/Preprocessor.h"
29bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman#include "clang/Parse/ParseDiagnostic.h"
3019510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
31f85e193739c953358c865005855253af4f68a497John McCall#include "clang/Sema/DelayedDiagnostic.h"
32d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor#include "clang/Sema/Lookup.h"
334994d2d50ceacdc8908f750c55589c0a20942a0aSebastian Redl#include "llvm/ADT/SmallPtrSet.h"
3487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor#include "llvm/Support/ErrorHandling.h"
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
375db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner/// isOmittedBlockReturnType - Return true if this declarator is missing a
3891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier/// return type because this is a omitted return type on a block literal.
398ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redlstatic bool isOmittedBlockReturnType(const Declarator &D) {
405db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getContext() != Declarator::BlockLiteralContext ||
418ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl      D.getDeclSpec().hasTypeSpecifier())
425db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    return false;
4391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
445db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 0)
45a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^{ ... }
4691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
475db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (D.getNumTypeObjects() == 1 &&
485db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      D.getTypeObject(0).Kind == DeclaratorChunk::Function)
49a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    return true;   // ^(int X, float Y) { ... }
5091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
515db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  return false;
525db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner}
535db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner
542792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// diagnoseBadTypeAttribute - Diagnoses a type attribute which
552792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall/// doesn't apply to the given type.
562792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCallstatic void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr,
572792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall                                     QualType type) {
58108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth  bool useExpansionLoc = false;
592792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
602792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  unsigned diagID = 0;
612792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  switch (attr.getKind()) {
628e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  case AttributeList::AT_ObjCGC:
632792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    diagID = diag::warn_pointer_attribute_wrong_type;
64108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth    useExpansionLoc = true;
652792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    break;
662792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
678e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  case AttributeList::AT_ObjCOwnership:
6805d4876a64865e34366b58fc8a6848c3cde895d9Argyrios Kyrtzidis    diagID = diag::warn_objc_object_attribute_wrong_type;
69108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth    useExpansionLoc = true;
7005d4876a64865e34366b58fc8a6848c3cde895d9Argyrios Kyrtzidis    break;
7105d4876a64865e34366b58fc8a6848c3cde895d9Argyrios Kyrtzidis
722792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  default:
732792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    // Assume everything else was a function attribute.
742792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    diagID = diag::warn_function_attribute_wrong_type;
752792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    break;
762792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  }
772792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
782792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  SourceLocation loc = attr.getLoc();
795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef name = attr.getName()->getName();
802792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
812792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  // The GC attributes are usually written with macros;  special-case them.
82108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth  if (useExpansionLoc && loc.isMacroID() && attr.getParameterName()) {
83834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    if (attr.getParameterName()->isStr("strong")) {
84834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__strong")) name = "__strong";
85834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    } else if (attr.getParameterName()->isStr("weak")) {
86834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall      if (S.findMacroSpelling(loc, "__weak")) name = "__weak";
872792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall    }
882792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  }
892792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
902792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  S.Diag(loc, diagID) << name << type;
912792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall}
922792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
93711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// objc_gc applies to Objective-C pointers or, otherwise, to the
94711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// smallest available pointer type (i.e. 'void*' in 'void**').
95711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
968e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ObjCGC: \
978e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ObjCOwnership
98711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
99711c52bb20d0c69063b52a99826fb7d2835501f1John McCall// Function type attributes.
100711c52bb20d0c69063b52a99826fb7d2835501f1John McCall#define FUNCTION_TYPE_ATTRS_CASELIST \
1018e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NoReturn: \
1028e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_CDecl: \
1038e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_FastCall: \
1048e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_StdCall: \
1058e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ThisCall: \
1068e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Pascal: \
1078e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Regparm: \
1088e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Pcs \
109711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
110711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace {
111711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// An object which stores processing state for the entire
112711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// GetTypeForDeclarator process.
113711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  class TypeProcessingState {
114711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &sema;
115711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
116711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The declarator being processed.
117711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &declarator;
118711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
119711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The index of the declarator chunk we're currently processing.
120711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// May be the total number of valid chunks, indicating the
121711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// DeclSpec.
122711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex;
123711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Whether there are non-trivial modifications to the decl spec.
125711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    bool trivial;
126711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1277ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    /// Whether we saved the attributes in the decl spec.
1287ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    bool hasSavedAttrs;
1297ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
130711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The original set of attributes on the DeclSpec.
1315f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> savedAttrs;
132711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
133711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// A list of attributes to diagnose the uselessness of when the
134711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// processing is complete.
1355f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> ignoredTypeAttrs;
136711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
137711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  public:
138711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    TypeProcessingState(Sema &sema, Declarator &declarator)
139711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : sema(sema), declarator(declarator),
140711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        chunkIndex(declarator.getNumTypeObjects()),
1417ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        trivial(true), hasSavedAttrs(false) {}
142711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
143711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &getSema() const {
144711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return sema;
145711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
146711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
147711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &getDeclarator() const {
148711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator;
149711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
150711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
151711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned getCurrentChunkIndex() const {
152711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return chunkIndex;
153711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
154711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
155711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void setCurrentChunkIndex(unsigned idx) {
156711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(idx <= declarator.getNumTypeObjects());
157711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      chunkIndex = idx;
158711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
159711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
160711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&getCurrentAttrListRef() const {
161711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(chunkIndex <= declarator.getNumTypeObjects());
162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (chunkIndex == declarator.getNumTypeObjects())
163711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        return getMutableDeclSpec().getAttributes().getListRef();
164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator.getTypeObject(chunkIndex).getAttrListRef();
165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
166711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Save the current set of attributes on the DeclSpec.
168711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void saveDeclSpecAttrs() {
169711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Don't try to save them multiple times.
1707ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (hasSavedAttrs) return;
171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
172711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      DeclSpec &spec = getMutableDeclSpec();
173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (AttributeList *attr = spec.getAttributes().getList(); attr;
174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             attr = attr->getNext())
175711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs.push_back(attr);
176711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      trivial &= savedAttrs.empty();
1777ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      hasSavedAttrs = true;
178711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
179711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Record that we had nowhere to put the given type attribute.
181711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// We will diagnose such attributes later.
182711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void addIgnoredTypeAttr(AttributeList &attr) {
183711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      ignoredTypeAttrs.push_back(&attr);
184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
185711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
186711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Diagnose all the ignored type attributes, given that the
187711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// declarator worked out to the given type.
188711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void diagnoseIgnoredTypeAttrs(QualType type) const {
1895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      for (SmallVectorImpl<AttributeList*>::const_iterator
190711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
1912792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall           i != e; ++i)
1922792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall        diagnoseBadTypeAttribute(getSema(), **i, type);
193711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
194711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
195711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    ~TypeProcessingState() {
196711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (trivial) return;
197711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
198711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      restoreDeclSpecAttrs();
199711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
201711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  private:
202711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclSpec &getMutableDeclSpec() const {
203711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return const_cast<DeclSpec&>(declarator.getDeclSpec());
204711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
205711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
206711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void restoreDeclSpecAttrs() {
2077ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      assert(hasSavedAttrs);
2087ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
2097ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (savedAttrs.empty()) {
2107ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        getMutableDeclSpec().getAttributes().set(0);
2117ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        return;
2127ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      }
2137ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
214711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
215711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
216711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs[i]->setNext(savedAttrs[i+1]);
217711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      savedAttrs.back()->setNext(0);
218711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
219711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
220711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
221711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// Basically std::pair except that we really want to avoid an
222711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// implicit operator= for safety concerns.  It's also a minor
223711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// link-time optimization for this to be a private type.
224711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  struct AttrAndList {
225711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The attribute.
226711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &first;
227711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
228711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The head of the list the attribute is currently in.
229711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&second;
230711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
231711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttrAndList(AttributeList &attr, AttributeList *&head)
232711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : first(attr), second(head) {}
233711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
23404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
23504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
236711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace llvm {
237711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  template <> struct isPodLike<AttrAndList> {
238711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    static const bool value = true;
239711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
240711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
241711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
242711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
243711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  attr.setNext(head);
244711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  head = &attr;
245711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
246711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
247711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
248711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (head == &attr) {
249711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    head = attr.getNext();
250711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
25104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
252711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
253711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *cur = head;
254711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  while (true) {
255711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    assert(cur && cur->getNext() && "ran out of attrs?");
256711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (cur->getNext() == &attr) {
257711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      cur->setNext(attr.getNext());
258711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
259711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
260711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    cur = cur->getNext();
261711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
262711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
263711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
264711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void moveAttrFromListToList(AttributeList &attr,
265711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&fromList,
266711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&toList) {
267711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, fromList);
268711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrIntoList(attr, toList);
269711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
270711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
271711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state,
272711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             QualType &type, bool isDeclSpec,
273711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             AttributeList *attrs);
274711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
275711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
276711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
277711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type);
278711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
279711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
280711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr, QualType &type);
281711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
282b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
283f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr, QualType &type);
284f85e193739c953358c865005855253af4f68a497John McCall
285711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCPointerTypeAttr(TypeProcessingState &state,
286711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr, QualType &type) {
2878e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_ObjCGC)
288f85e193739c953358c865005855253af4f68a497John McCall    return handleObjCGCTypeAttr(state, attr, type);
2898e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  assert(attr.getKind() == AttributeList::AT_ObjCOwnership);
290b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  return handleObjCOwnershipTypeAttr(state, attr, type);
291711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
292711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
293711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Given that an objc_gc attribute was written somewhere on a
294711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declaration *other* than on the declarator itself (for which, use
295711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
296711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// didn't apply in whatever position it was written in, try to move
297711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// it to a more appropriate position.
298711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeObjCPointerTypeAttr(TypeProcessingState &state,
299711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          AttributeList &attr,
300711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType type) {
301711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
302711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
303711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
304711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
305711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
306711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
307711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
308711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
309711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
310711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
311711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
312711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
313711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
314711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
315711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Don't walk through these.
316711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
317711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
318711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
319711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto error;
320711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
321711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
322711c52bb20d0c69063b52a99826fb7d2835501f1John McCall error:
3232792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
3242792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
325711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
326711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
327711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Distribute an objc_gc type attribute that was written on the
328711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator.
329711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
330711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
331711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            AttributeList &attr,
332711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            QualType &declSpecType) {
333711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
334711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
335711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // objc_gc goes on the innermost pointer to something that's not a
336711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // pointer.
337711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  unsigned innermost = -1U;
338711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  bool considerDeclSpec = true;
339711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
340711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
341711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
342711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
343711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
344711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      innermost = i;
345ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      continue;
346711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
347711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
348711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
349711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
350711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
351711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
352711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
353711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
354711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      considerDeclSpec = false;
355711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto done;
356711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
357711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
358711c52bb20d0c69063b52a99826fb7d2835501f1John McCall done:
359711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
360711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // That might actually be the decl spec if we weren't blocked by
361711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // anything in the declarator.
362711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (considerDeclSpec) {
3637ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    if (handleObjCPointerTypeAttr(state, attr, declSpecType)) {
3647ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // Splice the attribute into the decl spec.  Prevents the
3657ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // attribute from being applied multiple times and gives
3667ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // the source-location-filler something to work with.
3677ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      state.saveDeclSpecAttrs();
3687ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      moveAttrFromListToList(attr, declarator.getAttrListRef(),
3697ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall               declarator.getMutableDeclSpec().getAttributes().getListRef());
370711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
3717ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    }
372711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
373711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
374711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, if we found an appropriate chunk, splice the attribute
375711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // into it.
376711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (innermost != -1U) {
377711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, declarator.getAttrListRef(),
378711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                       declarator.getTypeObject(innermost).getAttrListRef());
379711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
380711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
381711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
382711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, diagnose when we're done building the type.
383711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
384711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
385711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
386711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
387711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written somewhere in a declaration
388711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// *other* than on the declarator itself or in the decl spec.  Given
389711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// that it didn't apply in whatever position it was written in, try
390711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// to move it to a more appropriate position.
391711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeFunctionTypeAttr(TypeProcessingState &state,
392711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
393711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType type) {
394711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
395711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
396711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to push the attribute from the return type of a function to
397711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the function itself.
398711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
399711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
400711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
401711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
402711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
403711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
404711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
405711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
406711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
407711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
408711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
409711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
410711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
411711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
412711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
414711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
41591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4162792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
417711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
418711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
419711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Try to distribute a function type attribute to the innermost
420711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// function chunk or type.  Returns true if the attribute was
421711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributed, false if no location was found.
422711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool
423711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
424711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr,
425711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList *&attrList,
426711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      QualType &declSpecType) {
427711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
428711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
429711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Put it on the innermost function chunk, if there is one.
430711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
431711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
432711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (chunk.Kind != DeclaratorChunk::Function) continue;
433711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
434711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
435711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
436711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
437711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
438f85e193739c953358c865005855253af4f68a497John McCall  if (handleFunctionTypeAttr(state, attr, declSpecType)) {
439f85e193739c953358c865005855253af4f68a497John McCall    spliceAttrOutOfList(attr, attrList);
440f85e193739c953358c865005855253af4f68a497John McCall    return true;
441f85e193739c953358c865005855253af4f68a497John McCall  }
442f85e193739c953358c865005855253af4f68a497John McCall
443f85e193739c953358c865005855253af4f68a497John McCall  return false;
444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
445711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
446711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written in the decl spec.  Try to
447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
448711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
449711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
450711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
451711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType &declSpecType) {
452711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.saveDeclSpecAttrs();
453711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
454711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
455711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
456711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            state.getCurrentAttrListRef(),
457711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
458711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
460711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
461711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
462711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
463711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
465711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written on the declarator.  Try to
466711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
467711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
468711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
469711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         AttributeList &attr,
470711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         QualType &declSpecType) {
471711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
472711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
473711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
474711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
475711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declarator.getAttrListRef(),
476711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
477711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
478711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
479711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
480711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
482711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
484711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
485711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// \brief Given that there are attributes written on the declarator
486711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// itself, try to distribute any type attributes to the appropriate
487711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator chunk.
488711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///
489711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// These are attributes like the following:
490711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f ATTR;
491711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int (f ATTR)();
492711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// but not necessarily this:
493711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f() ATTR;
494711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                              QualType &declSpecType) {
496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Collect all the type attributes from the declarator itself.
497711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
498711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *attr = state.getDeclarator().getAttributes();
499711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
501711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr->getNext();
502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr->getKind()) {
504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
505711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
506711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
507711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5088e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
5094e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
510f85e193739c953358c865005855253af4f68a497John McCall        break;
511f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough
512f85e193739c953358c865005855253af4f68a497John McCall
513711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
514711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
515711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    default:
518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
520711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attr = next));
521711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
523711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Add a synthetic '()' to a block-literal declarator if it is
524711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// required, given the return type.
525711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void maybeSynthesizeBlockSignature(TypeProcessingState &state,
526711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType declSpecType) {
527711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
528711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
529711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // First, check whether the declarator would produce a function,
530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // i.e. whether the innermost semantic chunk is a function.
531711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.isFunctionDeclarator()) {
532711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // If so, make that declarator a prototyped declarator.
533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.getFunctionTypeInfo().hasPrototype = true;
534711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
535711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
536711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
537da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // If there are any type objects, the type as written won't name a
538da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // function, regardless of the decl spec type.  This is because a
539da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // block signature declarator is always an abstract-declarator, and
540da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // abstract-declarators can't just be parentheses chunks.  Therefore
541da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // we need to build a function chunk unless there are no type
542da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // objects and the decl spec type is a function.
543711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
544711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
545711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
546da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // Note that there *are* cases with invalid declarators where
547da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // declarators consist solely of parentheses.  In general, these
548da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // occur only in failed efforts to make function declarators, so
549da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // faking up the function chunk is still the right thing to do.
550711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
551711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, we need to fake up a function declarator.
55296a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar  SourceLocation loc = declarator.getLocStart();
553711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
554711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // ...and *prepend* it to the declarator.
555711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
556711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*proto*/ true,
557b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith                             /*variadic*/ false,
558b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith                             /*ambiguous*/ false, SourceLocation(),
559711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*args*/ 0, 0,
560711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*type quals*/ 0,
56183f51722ed2b8134810cb178f39e44da811de7cdDouglas Gregor                             /*ref-qualifier*/true, SourceLocation(),
56243f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                             /*const qualifier*/SourceLocation(),
56343f5103f8051bbac19022e6edaf7d9138b0f3c0fDouglas Gregor                             /*volatile qualifier*/SourceLocation(),
56490ebed0734fac9b464c9bdff53fbf85a86b27f32Douglas Gregor                             /*mutable qualifier*/SourceLocation(),
565a058fd4f0a944174295f77169b438510dad389f8Richard Smith                             /*EH*/ EST_None, SourceLocation(), 0, 0, 0, 0,
566711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             /*parens*/ loc, loc,
567711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             declarator));
568711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
569711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // For consistency, make sure the state still has us as processing
570711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the decl spec.
571711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
572711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.setCurrentChunkIndex(declarator.getNumTypeObjects());
57304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
57404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
575930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
576930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
5771dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// \param state Specifies the declarator containing the declaration specifier
5781dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// to be converted, along with other associated processing state.
5795153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
5805153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
5818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType ConvertDeclSpecToType(TypeProcessingState &state) {
5825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
5835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
584711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
586711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
587711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const DeclSpec &DS = declarator.getDeclSpec();
588711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation DeclLoc = declarator.getIdentifierLoc();
5895db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
59096a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar    DeclLoc = DS.getLocStart();
59191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
592711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  ASTContext &Context = S.Context;
5931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5945db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
5955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
59696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
59796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
59896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
5995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
6005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
601fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
6025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
603fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
6055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
6065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
607fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
6085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
609958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
61064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
61164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
61264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
61364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
614711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
615f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
61664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
61764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
61864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
61964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
620711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
621f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
62264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
62364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
62464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
625f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
626f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
627f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
628f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
629f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
630f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
631f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
632f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
633f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
634f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
635d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
63662f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
637097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
638c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
639c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         (ObjCProtocolDecl**)PQ,
640c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         DS.getNumProtocolQualifiers());
641c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectPointerType(Result);
64262f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
64362f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
64491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
6455db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
6465db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
647f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // The declspec is always missing in a lambda expr context; it is either
648f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // specified with a trailing return type or inferred.
649f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    if (declarator.getContext() == Declarator::LambdaExprContext ||
650f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman        isOmittedBlockReturnType(declarator)) {
6515db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      Result = Context.DependentTy;
6525db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
6535db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
6541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
655d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
656d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
657d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
658d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
659d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
660d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
661d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
6624e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().ImplicitInt) {
66335d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
66435d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
6653f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
666711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_declspec)
6673f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
66896a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        << FixItHint::CreateInsertion(DS.getLocStart(), "int");
6693f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
6704310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
671d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
672d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
673d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
674d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
6754310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
6764e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (S.getLangOpts().CPlusPlus &&
6774e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          !S.getLangOpts().MicrosoftExt) {
678711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_missing_type_specifier)
6793f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
681b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
682b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
683b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
684711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
685b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
686711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_type_specifier)
6873f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
688b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
689d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
6901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
6923cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
6935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
6945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
695fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
696fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
697fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
698311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
699311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
70091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
701311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
7024e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (!S.getLangOpts().C99)
703ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith          S.Diag(DS.getTypeSpecWidthLoc(),
7044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                 S.getLangOpts().CPlusPlus0x ?
705ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith                   diag::warn_cxx98_compat_longlong : diag::ext_longlong);
706311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
7075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
7085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
7095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
710fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
711fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
712fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
713311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
714311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
71591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
716311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        // long long is a C99 feature.
7174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (!S.getLangOpts().C99)
718ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith          S.Diag(DS.getTypeSpecWidthLoc(),
7194e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                 S.getLangOpts().CPlusPlus0x ?
720ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith                   diag::warn_cxx98_compat_longlong : diag::ext_longlong);
721311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
7225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
7235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
724958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7253cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
7265a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith  case DeclSpec::TST_int128:
7275a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    if (DS.getTypeSpecSign() == DeclSpec::TSS_unsigned)
7285a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.UnsignedInt128Ty;
7295a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    else
7305a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.Int128Ty;
7315a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    break;
732aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case DeclSpec::TST_half: Result = Context.HalfTy; break;
733fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
734958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
735958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
736fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
737958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
738fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
73939d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne
7404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
74139d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
74239d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      declarator.setInvalidType(true);
74339d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    }
744958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
745fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
7485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
749711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
7508f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
751711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
7528f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
75399dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
7566666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_struct:
7576666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_interface: {
758b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
7596e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
7606e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
7616e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
762711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
7636e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
7646e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
7656e24726524c2b51b31bb4b622aa678a46b024f42John McCall
766a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
7670daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc());
76891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
7695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
770a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
77191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
7725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
773a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
7742191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
7750daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    // In both C and C++, make an ElaboratedType.
7760daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    ElaboratedTypeKeyword Keyword
7770daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
7780daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
779958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7811a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
7825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
7845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
785711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
78627940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    if (Result.isNull())
787711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
78827940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    else if (DeclSpec::ProtocolQualifierListTy PQ
78927940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall               = DS.getProtocolQualifiers()) {
790c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
791c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // Silently drop any existing protocol qualifiers.
792c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // TODO: determine whether that's the right thing to do.
793c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjT->getNumProtocols())
794c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = ObjT->getBaseType();
795c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
796c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (DS.getNumProtocolQualifiers())
797c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = Context.getObjCObjectType(Result,
798c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             (ObjCProtocolDecl**) PQ,
799c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             DS.getNumProtocolQualifiers());
800c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCIdType()) {
801ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
802c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
803c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
804c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
805c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
806c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCClassType()) {
8074262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
808c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
809c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           (ObjCProtocolDecl**) PQ,
810c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
811c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
8123f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
813711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
8143f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
815711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
8163f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
817c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
8181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
820958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
8215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
822958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
823e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
824711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
825958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
826730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian    if (!Result->isDependentType())
827730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian      if (const TagType *TT = Result->getAs<TagType>())
828711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
829d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
830fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
831958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
832d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
833b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
834d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
835d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
836711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
8374b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
8384b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
839711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
8404b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
841958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
842d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
8436fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
844b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
8456fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
8466fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
847711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
848af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
849af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
850711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
851af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
8526fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
8536fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
854ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case DeclSpec::TST_underlyingType:
855db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    Result = S.GetTypeFromParser(DS.getRepAsType());
856db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    assert(!Result.isNull() && "Didn't get a type for __underlying_type?");
857ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    Result = S.BuildUnaryTransformType(Result,
858ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::EnumUnderlyingType,
859ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       DS.getTypeSpecTypeLoc());
860ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (Result.isNull()) {
861ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Result = Context.IntTy;
862ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      declarator.setInvalidType(true);
863db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    }
86491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
865db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
866e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
867e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
86834b41d939a1328f484511c6002ba2456db879a29Richard Smith    Result = Context.getAutoType(QualType());
869e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
870e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
8711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
872a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall  case DeclSpec::TST_unknown_anytype:
873a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    Result = Context.UnknownAnyTy;
874a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    break;
875a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall
876b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case DeclSpec::TST_atomic:
877b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.GetTypeFromParser(DS.getRepAsType());
878b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(!Result.isNull() && "Didn't get a type for _Atomic?");
879b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc());
880b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Result.isNull()) {
881b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Result = Context.IntTy;
882b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      declarator.setInvalidType(true);
883b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
88491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
885b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
886809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
8875153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
888711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
8895153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
8905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
892958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
893f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
8944e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().Freestanding)
895711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
896fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
89782287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
89882287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
89982287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
900e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
901788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
902e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
903788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
904e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
905e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
906f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
9071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90847423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
90947423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
910711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
9111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
912711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
913711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
914711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
915711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
916711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
917711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
918711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
919711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
920711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (AttributeList *attrs = DS.getAttributes().getList())
921711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    processTypeAttrs(state, Result, true, attrs);
9221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
92396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
92496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
92596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
92696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
92796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
92896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
9290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (TypeQuals & DeclSpec::TQ_restrict) {
9302b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian      if (Result->isAnyPointerType() || Result->isReferenceType()) {
9312b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        QualType EltTy;
9322b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        if (Result->isObjCObjectPointerType())
9332b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result;
9342b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        else
9352b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result->isPointerType() ?
9362b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<PointerType>()->getPointeeType() :
9372b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<ReferenceType>()->getPointeeType();
9381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
939bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
940bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
941bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
942711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getRestrictSpecLoc(),
943d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
944d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
9450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall          TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
946bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
947bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
948711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DS.getRestrictSpecLoc(),
949711c52bb20d0c69063b52a99826fb7d2835501f1John McCall               diag::err_typecheck_invalid_restrict_not_pointer)
950d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
9510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
95296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
95396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
9541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
95596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
95696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
95796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
95896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
95996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
96096b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
9610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
96296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
9630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
96496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
9650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
9660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        assert((TypeQuals & DeclSpec::TQ_restrict) &&
9670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall               "Has CVR quals but not C, V, or R?");
9680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Loc = DS.getRestrictSpecLoc();
96996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
970711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(Loc, diag::warn_typecheck_function_qualifiers)
971d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
97296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
9731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
974f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
975f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
976f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
977f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
978f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
9791a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
9801a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
981f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
9820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
9830953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
9841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
9851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
986bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // C90 6.5.3 constraints: "The same type qualifier shall not appear more
987bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // than once in the same specifier-list or qualifier-list, either directly
988bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // or via one or more typedefs."
98991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus
990bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman        && TypeQuals & Result.getCVRQualifiers()) {
991bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()) {
99291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec)
993bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "const";
994bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
995bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
996bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()) {
99791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec)
998bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "volatile";
999bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1000bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
1001bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      // C90 doesn't have restrict, so it doesn't force us to produce a warning
1002bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      // in this case.
1003bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    }
1004bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
10050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
10060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Result = Context.getQualifiedType(Result, Quals);
100796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
10080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1009f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
1010f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
1011f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
1012cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
1013cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
1014cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
10151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1016cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
1017cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1018cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
10192865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
10202865474261a608c7873b87ba4af110d17907896dJohn McCall                                  Qualifiers Qs) {
10212865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
10222865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
10232865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
10242865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
10252865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
10262865474261a608c7873b87ba4af110d17907896dJohn McCall
10272865474261a608c7873b87ba4af110d17907896dJohn McCall    const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
10282865474261a608c7873b87ba4af110d17907896dJohn McCall    if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
10292865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
10302865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
10312865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
10322865474261a608c7873b87ba4af110d17907896dJohn McCall      }
10332865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
10342865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
10352865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
10362865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
10372865474261a608c7873b87ba4af110d17907896dJohn McCall      }
10382865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
10392865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
10402865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
10412865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
104291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
10432865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (!Ty->isDependentType()) {
10442865474261a608c7873b87ba4af110d17907896dJohn McCall      // FIXME: this deserves a proper diagnostic
10452865474261a608c7873b87ba4af110d17907896dJohn McCall      DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
10462865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
10472865474261a608c7873b87ba4af110d17907896dJohn McCall    }
10482865474261a608c7873b87ba4af110d17907896dJohn McCall
10492865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
10502865474261a608c7873b87ba4af110d17907896dJohn McCall      Diag(Loc, DiagID) << ProblemTy;
10512865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
10522865474261a608c7873b87ba4af110d17907896dJohn McCall    }
10532865474261a608c7873b87ba4af110d17907896dJohn McCall  }
10542865474261a608c7873b87ba4af110d17907896dJohn McCall
10552865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
10562865474261a608c7873b87ba4af110d17907896dJohn McCall}
10572865474261a608c7873b87ba4af110d17907896dJohn McCall
1058075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
1059075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
1060075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
1061075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
1062075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
1063f85e193739c953358c865005855253af4f68a497John McCall/// Given that we're building a pointer or reference to the given
1064f85e193739c953358c865005855253af4f68a497John McCallstatic QualType inferARCLifetimeForPointee(Sema &S, QualType type,
1065f85e193739c953358c865005855253af4f68a497John McCall                                           SourceLocation loc,
1066f85e193739c953358c865005855253af4f68a497John McCall                                           bool isReference) {
1067f85e193739c953358c865005855253af4f68a497John McCall  // Bail out if retention is unrequired or already specified.
1068f85e193739c953358c865005855253af4f68a497John McCall  if (!type->isObjCLifetimeType() ||
1069f85e193739c953358c865005855253af4f68a497John McCall      type.getObjCLifetime() != Qualifiers::OCL_None)
1070f85e193739c953358c865005855253af4f68a497John McCall    return type;
1071f85e193739c953358c865005855253af4f68a497John McCall
1072f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None;
1073f85e193739c953358c865005855253af4f68a497John McCall
1074f85e193739c953358c865005855253af4f68a497John McCall  // If the object type is const-qualified, we can safely use
1075f85e193739c953358c865005855253af4f68a497John McCall  // __unsafe_unretained.  This is safe (because there are no read
1076f85e193739c953358c865005855253af4f68a497John McCall  // barriers), and it'll be safe to coerce anything but __weak* to
1077f85e193739c953358c865005855253af4f68a497John McCall  // the resulting type.
1078f85e193739c953358c865005855253af4f68a497John McCall  if (type.isConstQualified()) {
1079f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
1080f85e193739c953358c865005855253af4f68a497John McCall
1081f85e193739c953358c865005855253af4f68a497John McCall  // Otherwise, check whether the static type does not require
1082f85e193739c953358c865005855253af4f68a497John McCall  // retaining.  This currently only triggers for Class (possibly
1083f85e193739c953358c865005855253af4f68a497John McCall  // protocol-qualifed, and arrays thereof).
1084f85e193739c953358c865005855253af4f68a497John McCall  } else if (type->isObjCARCImplicitlyUnretainedType()) {
1085f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
10865b76f373d23cc3b292ecf523349aaaa388eea375Argyrios Kyrtzidis
1087ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // If we are in an unevaluated context, like sizeof, skip adding a
1088ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // qualification.
108971f55f771794674a410171dbf3cb5dbedf95d033David Blaikie  } else if (S.isUnevaluatedContext()) {
1090ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    return type;
1091f85e193739c953358c865005855253af4f68a497John McCall
1092e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // If that failed, give an error and recover using __strong.  __strong
1093e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // is the option most likely to prevent spurious second-order diagnostics,
1094e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // like when binding a reference to a field.
1095f85e193739c953358c865005855253af4f68a497John McCall  } else {
1096f85e193739c953358c865005855253af4f68a497John McCall    // These types can show up in private ivars in system headers, so
1097f85e193739c953358c865005855253af4f68a497John McCall    // we need this to not be an error in those cases.  Instead we
1098f85e193739c953358c865005855253af4f68a497John McCall    // want to delay.
1099f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1100ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.DelayedDiagnostics.add(
1101ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman          sema::DelayedDiagnostic::makeForbiddenType(loc,
1102ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman              diag::err_arc_indirect_no_ownership, type, isReference));
1103f85e193739c953358c865005855253af4f68a497John McCall    } else {
1104ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference;
1105f85e193739c953358c865005855253af4f68a497John McCall    }
1106e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall    implicitLifetime = Qualifiers::OCL_Strong;
1107f85e193739c953358c865005855253af4f68a497John McCall  }
1108f85e193739c953358c865005855253af4f68a497John McCall  assert(implicitLifetime && "didn't infer any lifetime!");
1109f85e193739c953358c865005855253af4f68a497John McCall
1110f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers qs;
1111f85e193739c953358c865005855253af4f68a497John McCall  qs.addObjCLifetime(implicitLifetime);
1112f85e193739c953358c865005855253af4f68a497John McCall  return S.Context.getQualifiedType(type, qs);
1113f85e193739c953358c865005855253af4f68a497John McCall}
1114f85e193739c953358c865005855253af4f68a497John McCall
1115cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
1116cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1117cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
1118cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1119cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1120cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
1121cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
1122cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1123cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
1124cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1125cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1126cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
1127cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
11282865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
1129cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
1130cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
1131cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
1132cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
1133ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1134cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1135cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1136cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1137c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
113892e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
1139f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build pointers to unqualified pointers.
11404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1141f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false);
1142f85e193739c953358c865005855253af4f68a497John McCall
1143cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
11442865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
1145cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1146cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1147cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
1148cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1149cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
1150cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1151cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1152cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
1153cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
1154cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1155cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
1156cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1157cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1158cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
1159cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
116054e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
11612865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
116254e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
116391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  assert(Context.getCanonicalType(T) != Context.OverloadTy &&
11649625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
116591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
116669d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // C++0x [dcl.ref]p6:
116791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   If a typedef (7.1.3), a type template-parameter (14.3.1), or a
116891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
116991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   type T, an attempt to create the type "lvalue reference to cv TR" creates
117091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   the type "lvalue reference to T", while an attempt to create the type
117169d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   "rvalue reference to cv TR" creates the type TR.
117254e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
117354e14c4db764c0636160d26c5bbf491637c83a76John McCall
117454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
117554e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
117654e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
117754e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
117854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
117954e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
118054e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
118154e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
118254e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
118354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
118454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
118569d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // collapsing of references-to-references as described in C++0x.
118669d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // DR 106 and 540 introduce reference-collapsing into C++98/03.
1187cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1188cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
118933a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
1190cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
1191cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
1192cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
1193cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1194cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1195cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1196f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build references to unqualified pointers.
11974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1198f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true);
1199f85e193739c953358c865005855253af4f68a497John McCall
1200cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
12017c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
12022865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
12032865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
1204cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1205cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1206e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// Check whether the specified array size makes the array type a VLA.  If so,
1207e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// return true, if not, return the size of the array in SizeVal.
1208282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smithstatic bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal) {
1209282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode
1210282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // (like gnu99, but not c99) accept any evaluatable value as an extension.
1211ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  class VLADiagnoser : public Sema::VerifyICEDiagnoser {
1212ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  public:
1213ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    VLADiagnoser() : Sema::VerifyICEDiagnoser(true) {}
121491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1215ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) {
1216ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
121791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1218ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseFold(Sema &S, SourceLocation Loc, SourceRange SR) {
1219ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      S.Diag(Loc, diag::ext_vla_folded_to_constant) << SR;
1220ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
1221ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  } Diagnoser;
122291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1223ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  return S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser,
1224ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                           S.LangOpts.GNUMode).isInvalid();
1225e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner}
1226e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1227e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1228cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
1229cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1230cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
1231cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1232cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
12331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
12341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
1235cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1236efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Brackets The range from the opening '[' to the closing ']'.
1237cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1238cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
1239cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1240cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1241cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
1242cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
1243cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1244cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
12457e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
12460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
12477e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
12484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
1249138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
1250138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
125191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   type, the (possibly cv-qualified) type void, a function type or an
1252138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
1253138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
1254bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    // C++ [dcl.array]p3:
1255bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   When several "array of" specifications are adjacent, [...] only the
1256bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   first of the constant expressions that specify the bounds of the arrays
1257bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   may be omitted.
1258bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //
1259138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
1260138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
1261138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
1262138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
1263138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1264138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
126591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1266bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    if (T->isVoidType() || T->isIncompleteArrayType()) {
1267923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1268923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1269923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
127091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
127191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (RequireNonAbstractType(Brackets.getBegin(), T,
1272138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
1273138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
127491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1275923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
1276138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1277138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
1278923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
1279923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
1280923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1281923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
1282cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1283cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
1284cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
1285ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1286cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1287cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
12881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
128934b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (T->getContainedAutoType()) {
129034b41d939a1328f484511c6002ba2456db879a29Richard Smith    Diag(Loc, diag::err_illegal_decl_array_of_auto)
129134b41d939a1328f484511c6002ba2456db879a29Richard Smith      << getPrintableNameForEntity(Entity) << T;
1292e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
1293e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
12941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12956217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
1296cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
1297cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
1298cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
1299cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
1300c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
1301c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1302c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
1303cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
13041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1305806054db6653d29cb0d9692df3612cbcd03d0530John McCall  // Do placeholder conversions on the array size expression.
1306806054db6653d29cb0d9692df3612cbcd03d0530John McCall  if (ArraySize && ArraySize->hasPlaceholderType()) {
1307806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ExprResult Result = CheckPlaceholderExpr(ArraySize);
1308806054db6653d29cb0d9692df3612cbcd03d0530John McCall    if (Result.isInvalid()) return QualType();
1309806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ArraySize = Result.take();
1310806054db6653d29cb0d9692df3612cbcd03d0530John McCall  }
1311806054db6653d29cb0d9692df3612cbcd03d0530John McCall
13125e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
1313429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ArraySize && !ArraySize->isRValue()) {
1314429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult Result = DefaultLvalueConversion(ArraySize);
1315429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Result.isInvalid())
1316429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return QualType();
1317429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
1318429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ArraySize = Result.take();
1319429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
13205e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1321cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
1322282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // C++11 allows contextual conversions to such types.
13234e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().CPlusPlus0x &&
1324282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      ArraySize && !ArraySize->isTypeDependent() &&
13251274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1326cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1327cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
1328cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1329cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1330282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
13312767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
1332cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
1333f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
13347e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
1335f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
1336f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
1337ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
13387e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
1339282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  } else if ((!T->isDependentType() && !T->isIncompleteType() &&
1340282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith              !T->isConstantSizeType()) ||
1341282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith             isArraySizeVLA(*this, ArraySize, ConstVal)) {
1342282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // Even in C++11, don't allow contextual conversions in the array bound
1343282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // of a VLA.
13444e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().CPlusPlus0x &&
1345282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1346282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1347282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        << ArraySize->getType() << ArraySize->getSourceRange();
1348282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      return QualType();
1349282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    }
1350282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
1351e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with an element type that has a non-constant-size is a VLA.
1352e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with a non-ICE size is a VLA.  We accept any expression
1353e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // that we can fold to a non-zero positive value as an extension.
13547e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1355cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
1356cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1357cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
1358923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
1359b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
1360b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1361b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1362b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
1363b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1364b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
1365923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1366923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1367923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
136802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
136902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
137091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      Diag(ArraySize->getLocStart(),
137102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
137202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
1373923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
137420cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne
137520cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      if (ASM == ArrayType::Static) {
137620cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        Diag(ArraySize->getLocStart(),
137720cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne             diag::warn_typecheck_zero_static_array_size)
137820cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne          << ArraySize->getSourceRange();
137920cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        ASM = ArrayType::Normal;
138020cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      }
138191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
13822767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor               !T->isIncompleteType()) {
138391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // Is the array too large?
13842767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
13852767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
13862767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
13872767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
13882767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
13892767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
13901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
139191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
139246a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
1393cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1394af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
13954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().C99) {
13960fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
13970fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
1398f85e193739c953358c865005855253af4f68a497John McCall      QualType BaseT = Context.getBaseElementType(T);
139991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      if (!T->isDependentType() &&
1400f85e193739c953358c865005855253af4f68a497John McCall          !BaseT.isPODType(Context) &&
1401f85e193739c953358c865005855253af4f68a497John McCall          !BaseT->isObjCLifetimeType()) {
14020fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
1403f85e193739c953358c865005855253af4f68a497John McCall          << BaseT;
14040fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
140591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
1406a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
1407a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
1408a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
1409a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
1410a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
14110fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
14120fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
14130fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::ext_vla);
14140fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
1415d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith      Diag(Loc,
14164e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie           getLangOpts().CPlusPlus? diag::err_c99_array_usage_cxx
1417d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith                                     : diag::ext_c99_array_usage) << ASM;
1418cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1419cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1420cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
1421cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
14229cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
14239cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
14249cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
14259cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
14269ae2f076ca5ab1feb3ba95629099ec2319833701John McCallQualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
14279cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
14289cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
14299cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
14301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
14319cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
14329cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
14339cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
14349cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
14359cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
14369ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
14379cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
14389ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
14399cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
14409ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        << "ext_vector_type" << ArraySize->getSourceRange();
14419cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
14429cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
14431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
14459cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
14461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
14471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14489cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
14499cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
14509ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      << ArraySize->getSourceRange();
14519cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
14529cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
14531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14544ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    return Context.getExtVectorType(T, vectorSize);
14551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
14561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14579ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
14589cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
14591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1460724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
1461724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1462724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
1463724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
1464724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
14652943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
1466724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
1467724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1468724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
1469724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1470724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
1471724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
1472724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
1473724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1474724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
1475724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1476724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
1477724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1478eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith/// \param HasTrailingReturn Whether this function has a trailing return type.
1479eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith///
1480724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
1481724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1482724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
1483724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
1484724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
1485724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1486724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
1487724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
1488724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1489724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
1490724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
1491724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
14921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 QualType *ParamTypes,
1493724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
1494eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith                                 bool Variadic, bool HasTrailingReturn,
1495eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith                                 unsigned Quals,
1496c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                                 RefQualifierKind RefQualifier,
1497fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
1498e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                 FunctionType::ExtInfo Info) {
1499724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
150091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    Diag(Loc, diag::err_func_returning_array_function)
150158408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
1502724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1503724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1504aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1505aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // Functions cannot return half FP.
1506aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  if (T->isHalfType()) {
1507aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 <<
1508aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      FixItHint::CreateInsertion(Loc, "*");
1509aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    return QualType();
1510aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  }
1511aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1512724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
1513724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
1514aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    // FIXME: Loc is too inprecise here, should use proper locations for args.
151579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]);
15162dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
1517724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
1518724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
1519aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    } else if (ParamType->isHalfType()) {
1520aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Disallow half FP arguments.
1521aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 <<
1522aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        FixItHint::CreateInsertion(Loc, "*");
1523aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Invalid = true;
1524724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
1525cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
152654e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
1527724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1528724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1529724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
1530724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1531724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1532e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
1533e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.Variadic = Variadic;
1534eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith  EPI.HasTrailingReturn = HasTrailingReturn;
1535e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.TypeQuals = Quals;
1536c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor  EPI.RefQualifier = RefQualifier;
1537e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.ExtInfo = Info;
1538e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1539e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI);
1540724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
15411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1542949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
1543949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1544949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
1545949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
1546949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
1547949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
1548949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1549949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
1550949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
15511eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
15522865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
1553949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
1554949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
1555949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
1556949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
1557949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
1558949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1559949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
1560949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
1561949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1562949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
1563949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
1564949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
1565949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1566949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1567737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
1568949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
1569949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
15708d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
1571ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
1572949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1573949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1574949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1575949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
1576949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1577949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
1578949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1579949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1580949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1581949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
1582949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1583949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1584949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1585949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1586d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // In the Microsoft ABI, the class is allowed to be an incomplete
1587d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // type. In such cases, the compiler makes a worst-case assumption.
1588d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // We make no such assumption right now, so emit an error if the
1589d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis  // class isn't a complete type.
1590bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  if (Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft &&
1591d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis      RequireCompleteType(Loc, Class, diag::err_incomplete_type))
1592d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis    return QualType();
1593d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis
15942865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
1595949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
15961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15979a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
15989a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
15999a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
16009a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
1601efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Loc The source location, used for diagnostics.
16029a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
16039a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
16049a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
16059a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
16069a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
16079a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
160891cbbbf506c892a26d4301e2b3ccd377b0938817Chad RosierQualType Sema::BuildBlockPointerType(QualType T,
16091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
16109a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
16110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
16129a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
16139a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
16149a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
16151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16162865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
16179a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
16189a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
1619b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1620b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
16213f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
1622a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
16233f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
16243f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
16253f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1626a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
1627f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
1628e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
1629a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
1630e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
16311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1632a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
1633e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
1634e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
1635e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
1636a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
1637a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
1638a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            unsigned chunkIndex);
1639a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis
1640f85e193739c953358c865005855253af4f68a497John McCall/// Given that this is the declaration of a parameter under ARC,
1641f85e193739c953358c865005855253af4f68a497John McCall/// attempt to infer attributes and such for pointer-to-whatever
1642f85e193739c953358c865005855253af4f68a497John McCall/// types.
1643f85e193739c953358c865005855253af4f68a497John McCallstatic void inferARCWriteback(TypeProcessingState &state,
1644f85e193739c953358c865005855253af4f68a497John McCall                              QualType &declSpecType) {
1645f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
1646f85e193739c953358c865005855253af4f68a497John McCall  Declarator &declarator = state.getDeclarator();
1647f85e193739c953358c865005855253af4f68a497John McCall
1648f85e193739c953358c865005855253af4f68a497John McCall  // TODO: should we care about decl qualifiers?
1649f85e193739c953358c865005855253af4f68a497John McCall
1650f85e193739c953358c865005855253af4f68a497John McCall  // Check whether the declarator has the expected form.  We walk
1651f85e193739c953358c865005855253af4f68a497John McCall  // from the inside out in order to make the block logic work.
1652f85e193739c953358c865005855253af4f68a497John McCall  unsigned outermostPointerIndex = 0;
1653f85e193739c953358c865005855253af4f68a497John McCall  bool isBlockPointer = false;
1654f85e193739c953358c865005855253af4f68a497John McCall  unsigned numPointers = 0;
1655f85e193739c953358c865005855253af4f68a497John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
1656f85e193739c953358c865005855253af4f68a497John McCall    unsigned chunkIndex = i;
1657f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex);
1658f85e193739c953358c865005855253af4f68a497John McCall    switch (chunk.Kind) {
1659f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Paren:
1660f85e193739c953358c865005855253af4f68a497John McCall      // Ignore parens.
1661f85e193739c953358c865005855253af4f68a497John McCall      break;
1662f85e193739c953358c865005855253af4f68a497John McCall
1663f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Reference:
1664f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Pointer:
1665f85e193739c953358c865005855253af4f68a497John McCall      // Count the number of pointers.  Treat references
1666f85e193739c953358c865005855253af4f68a497John McCall      // interchangeably as pointers; if they're mis-ordered, normal
1667f85e193739c953358c865005855253af4f68a497John McCall      // type building will discover that.
1668f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1669f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1670f85e193739c953358c865005855253af4f68a497John McCall      break;
1671f85e193739c953358c865005855253af4f68a497John McCall
1672f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::BlockPointer:
1673f85e193739c953358c865005855253af4f68a497John McCall      // If we have a pointer to block pointer, that's an acceptable
1674f85e193739c953358c865005855253af4f68a497John McCall      // indirect reference; anything else is not an application of
1675f85e193739c953358c865005855253af4f68a497John McCall      // the rules.
1676f85e193739c953358c865005855253af4f68a497John McCall      if (numPointers != 1) return;
1677f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1678f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1679f85e193739c953358c865005855253af4f68a497John McCall      isBlockPointer = true;
1680f85e193739c953358c865005855253af4f68a497John McCall
1681f85e193739c953358c865005855253af4f68a497John McCall      // We don't care about pointer structure in return values here.
1682f85e193739c953358c865005855253af4f68a497John McCall      goto done;
1683f85e193739c953358c865005855253af4f68a497John McCall
1684f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Array: // suppress if written (id[])?
1685f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Function:
1686f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::MemberPointer:
1687f85e193739c953358c865005855253af4f68a497John McCall      return;
1688f85e193739c953358c865005855253af4f68a497John McCall    }
1689f85e193739c953358c865005855253af4f68a497John McCall  }
1690f85e193739c953358c865005855253af4f68a497John McCall done:
1691f85e193739c953358c865005855253af4f68a497John McCall
1692f85e193739c953358c865005855253af4f68a497John McCall  // If we have *one* pointer, then we want to throw the qualifier on
1693f85e193739c953358c865005855253af4f68a497John McCall  // the declaration-specifiers, which means that it needs to be a
1694f85e193739c953358c865005855253af4f68a497John McCall  // retainable object type.
1695f85e193739c953358c865005855253af4f68a497John McCall  if (numPointers == 1) {
1696f85e193739c953358c865005855253af4f68a497John McCall    // If it's not a retainable object type, the rule doesn't apply.
1697f85e193739c953358c865005855253af4f68a497John McCall    if (!declSpecType->isObjCRetainableType()) return;
1698f85e193739c953358c865005855253af4f68a497John McCall
1699f85e193739c953358c865005855253af4f68a497John McCall    // If it already has lifetime, don't do anything.
1700f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType.getObjCLifetime()) return;
1701f85e193739c953358c865005855253af4f68a497John McCall
1702f85e193739c953358c865005855253af4f68a497John McCall    // Otherwise, modify the type in-place.
1703f85e193739c953358c865005855253af4f68a497John McCall    Qualifiers qs;
170491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1705f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType->isObjCARCImplicitlyUnretainedType())
1706f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
1707f85e193739c953358c865005855253af4f68a497John McCall    else
1708f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing);
1709f85e193739c953358c865005855253af4f68a497John McCall    declSpecType = S.Context.getQualifiedType(declSpecType, qs);
1710f85e193739c953358c865005855253af4f68a497John McCall
1711f85e193739c953358c865005855253af4f68a497John McCall  // If we have *two* pointers, then we want to throw the qualifier on
1712f85e193739c953358c865005855253af4f68a497John McCall  // the outermost pointer.
1713f85e193739c953358c865005855253af4f68a497John McCall  } else if (numPointers == 2) {
1714f85e193739c953358c865005855253af4f68a497John McCall    // If we don't have a block pointer, we need to check whether the
1715f85e193739c953358c865005855253af4f68a497John McCall    // declaration-specifiers gave us something that will turn into a
1716f85e193739c953358c865005855253af4f68a497John McCall    // retainable object pointer after we slap the first pointer on it.
1717f85e193739c953358c865005855253af4f68a497John McCall    if (!isBlockPointer && !declSpecType->isObjCObjectType())
1718f85e193739c953358c865005855253af4f68a497John McCall      return;
1719f85e193739c953358c865005855253af4f68a497John McCall
1720f85e193739c953358c865005855253af4f68a497John McCall    // Look for an explicit lifetime attribute there.
1721f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex);
17221c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis    if (chunk.Kind != DeclaratorChunk::Pointer &&
17231c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis        chunk.Kind != DeclaratorChunk::BlockPointer)
17241c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis      return;
1725f85e193739c953358c865005855253af4f68a497John McCall    for (const AttributeList *attr = chunk.getAttrs(); attr;
1726f85e193739c953358c865005855253af4f68a497John McCall           attr = attr->getNext())
17278e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt      if (attr->getKind() == AttributeList::AT_ObjCOwnership)
1728f85e193739c953358c865005855253af4f68a497John McCall        return;
1729f85e193739c953358c865005855253af4f68a497John McCall
1730a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis    transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing,
1731a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                          outermostPointerIndex);
1732f85e193739c953358c865005855253af4f68a497John McCall
1733f85e193739c953358c865005855253af4f68a497John McCall  // Any other number of pointers/references does not trigger the rule.
1734f85e193739c953358c865005855253af4f68a497John McCall  } else return;
1735f85e193739c953358c865005855253af4f68a497John McCall
1736f85e193739c953358c865005855253af4f68a497John McCall  // TODO: mark whether we did this inference?
1737f85e193739c953358c865005855253af4f68a497John McCall}
1738f85e193739c953358c865005855253af4f68a497John McCall
1739d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruthstatic void DiagnoseIgnoredQualifiers(unsigned Quals,
1740d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation ConstQualLoc,
1741d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation VolatileQualLoc,
1742d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation RestrictQualLoc,
1743d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      Sema& S) {
1744d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  std::string QualStr;
1745d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  unsigned NumQuals = 0;
1746d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  SourceLocation Loc;
1747d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1748d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint ConstFixIt;
1749d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint VolatileFixIt;
1750d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint RestrictFixIt;
1751d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1752a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg  const SourceManager &SM = S.getSourceManager();
1753a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg
1754d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to
1755d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // find a range and grow it to encompass all the qualifiers, regardless of
1756d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // the order in which they textually appear.
1757d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Const) {
1758d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc);
1759d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    QualStr = "const";
1760a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    ++NumQuals;
1761a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(ConstQualLoc, Loc))
1762a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = ConstQualLoc;
1763d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1764d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Volatile) {
1765d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc);
1766a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    QualStr += (NumQuals == 0 ? "volatile" : " volatile");
1767d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ++NumQuals;
1768a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(VolatileQualLoc, Loc))
1769a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = VolatileQualLoc;
1770d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1771d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Restrict) {
1772d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc);
1773a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    QualStr += (NumQuals == 0 ? "restrict" : " restrict");
1774d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ++NumQuals;
1775a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(RestrictQualLoc, Loc))
1776a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = RestrictQualLoc;
1777d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1778d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1779d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  assert(NumQuals > 0 && "No known qualifiers?");
1780d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1781d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  S.Diag(Loc, diag::warn_qual_return_type)
1782a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    << QualStr << NumQuals << ConstFixIt << VolatileFixIt << RestrictFixIt;
1783d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth}
1784d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
17858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
17868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                             TypeSourceInfo *&ReturnTypeInfo) {
17878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &SemaRef = state.getSema();
17888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
1789930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
17908cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ReturnTypeInfo = 0;
1791711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
17928cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // The TagDecl owned by the DeclSpec.
17938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TagDecl *OwnedTagDecl = 0;
17948999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl
17953f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
179698a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian  case UnqualifiedId::IK_ImplicitSelfParam:
17973f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
17988999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  case UnqualifiedId::IK_Identifier:
17990486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
18003f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
18018cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = ConvertDeclSpecToType(state);
180291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1803591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
1804d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
180515987970eeaa1842c29ec8797affd1c1dea05585Abramo Bagnara      // Owned declaration is embedded in declarator.
1806d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl->setEmbeddedInDeclarator(true);
1807591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
1808930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
1809930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
18103f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
18110efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
18123f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
1813930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
181491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // "void" instead.
18158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = SemaRef.Context.VoidTy;
1816a78a640c5f59720f2c2b8034eca4fbf8525d9026Rafael Espindola    if (AttributeList *attrs = D.getDeclSpec().getAttributes().getList())
1817a78a640c5f59720f2c2b8034eca4fbf8525d9026Rafael Espindola      processTypeAttrs(state, T, true, attrs);
1818930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
181948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
182048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
182148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
182248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
182391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId,
18248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  &ReturnTypeInfo);
182548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
1826930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
1827dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
1828711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
1829711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
1830711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1831d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
1832d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // In C++11, a function declarator using 'auto' must have a trailing return
18338110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // type (this is checked later) and we can skip this. In other languages
18348110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // using auto, we need to check regardless.
183534b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
18364e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      (!SemaRef.getLangOpts().CPlusPlus0x || !D.isFunctionDeclarator())) {
1837baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
18381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1839baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
1840baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
1841b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("K&R type lists aren't allowed in C++");
1842f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
1843f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman      llvm_unreachable("Can't specify a type specifier in lambda grammar");
1844cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
1845cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
1846baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
1847baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
1848baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1849baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
18507a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)
18517a614d8380297fcd2bc23986241905d97222948cRichard Smith        break;
18528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) {
1853eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie      case TTK_Enum: llvm_unreachable("unhandled tag kind");
1854465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
1855465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
1856465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
18576666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      case TTK_Interface: Error = 4; /* Interface member */ break;
18581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
1859baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1860baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
186117b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
18626666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 5; // Exception declaration
1863baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1864baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
18656666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 6; // Template parameter
1866baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1867baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
18686666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 7; // Block literal
186934b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
187034b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TemplateTypeArgContext:
18716666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 8; // Template type argument
187234b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
1873162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
18743e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
18756666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 10; // Type alias
1876162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      break;
18777796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
18786666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11; // Function return type
18797796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
188034b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TypeNameContext:
18816666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 12; // Generic
1882baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1883baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
1884baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
1885baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
1886baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
18870b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
1888baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1889baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1890baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
1891ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith    if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
18926666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 9;
1893ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith
18948110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    // In Objective-C it is an error to use 'auto' on a function declarator.
18958110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    if (D.isFunctionDeclarator())
18966666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11;
18978110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith
1898d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.spec.auto]p2: 'auto' is always fine if the declarator
1899e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // contains a trailing return type. That is only legal at the outermost
1900e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // level. Check all declarator chunks (outermost first) anyway, to give
1901e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // better diagnostics.
19024e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (SemaRef.getLangOpts().CPlusPlus0x && Error != -1) {
1903e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1904e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned chunkIndex = e - i - 1;
1905e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        state.setCurrentChunkIndex(chunkIndex);
1906e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
1907e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (DeclType.Kind == DeclaratorChunk::Function) {
1908e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
190954655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (FTI.hasTrailingReturnType()) {
1910e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Error = -1;
1911e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            break;
1912e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          }
1913e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        }
1914e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
1915e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    }
1916e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
1917baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
19188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
19198cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_auto_not_allowed)
1920baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
19218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = SemaRef.Context.IntTy;
1922baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
19230aa86c0463a881be85fd34e04c7de3379997621dRichard Smith    } else
19240aa86c0463a881be85fd34e04c7de3379997621dRichard Smith      SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
19250aa86c0463a881be85fd34e04c7de3379997621dRichard Smith                   diag::warn_cxx98_compat_auto_type_specifier);
1926baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
19278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
19284e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().CPlusPlus &&
19295e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall      OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) {
19308cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // Check the contexts where C++ forbids the declaration of a new class
19318cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // or enumeration in a type-specifier-seq.
19328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    switch (D.getContext()) {
19337796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
19347796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // Class and enumeration definitions are syntactically not allowed in
19357796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // trailing return types.
19367796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      llvm_unreachable("parser should not have allowed this");
19377796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
19388cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::FileContext:
19398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::MemberContext:
19408cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockContext:
19418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ForContext:
19428cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockLiteralContext:
1943f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
1944d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // C++11 [dcl.type]p3:
19458cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   A type-specifier-seq shall not define a class or enumeration unless
19468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   it appears in the type-id of an alias-declaration (7.1.3) that is not
19478cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   the declaration of a template-declaration.
19488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasDeclContext:
19498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
19508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasTemplateContext:
19518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
19528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_alias_template)
19538cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
19548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
19558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TypeNameContext:
19568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateParamContext:
19578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXNewContext:
19588cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXCatchContext:
19598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ObjCCatchContext:
19608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateTypeArgContext:
19618cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
19628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_type_specifier)
19638cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
19648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
19658cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::PrototypeContext:
1966cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
1967cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
19688cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::KNRTypeListContext:
19698cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ [dcl.fct]p6:
19708cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   Types shall not be defined in return or parameter types.
19718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
19728cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_param_type)
19738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
19748cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
19758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ConditionContext:
19768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ 6.4p2:
19778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // The type-specifier-seq shall not contain typedef and shall not declare
19788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // a new class or enumeration.
19798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
19808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_condition);
19818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
19828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    }
19838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  }
19848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
19858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return T;
19868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
19878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
1988a08c2fb74ef823c185619ecc532f8fced6a1982fBenjamin Kramerstatic std::string getFunctionQualifiersAsString(const FunctionProtoType *FnTy){
1989d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  std::string Quals =
1990d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
1991d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
1992d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (FnTy->getRefQualifier()) {
1993d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_None:
1994d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
1995d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
1996d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_LValue:
1997d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
1998d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
1999d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += '&';
2000d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2001d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2002d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_RValue:
2003d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2004d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2005d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += "&&";
2006d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2007d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2008d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2009d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  return Quals;
2010d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2011d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2012d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// Check that the function type T, which has a cv-qualifier or a ref-qualifier,
2013d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// can be contained within the declarator chunk DeclType, and produce an
2014d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// appropriate diagnostic if not.
2015d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smithstatic void checkQualifiedFunction(Sema &S, QualType T,
2016d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                   DeclaratorChunk &DeclType) {
2017d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++98 [dcl.fct]p4 / C++11 [dcl.fct]p6: a function type with a
2018d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // cv-qualifier or a ref-qualifier can only appear at the topmost level
2019d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // of a type.
2020d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  int DiagKind = -1;
2021d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (DeclType.Kind) {
2022d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Paren:
2023d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::MemberPointer:
2024d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases are permitted.
2025d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2026d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Array:
2027d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Function:
2028d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases don't allow function types at all; no need to diagnose the
2029d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // qualifiers separately.
2030d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2031d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::BlockPointer:
2032d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 0;
2033d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2034d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Pointer:
2035d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 1;
2036d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2037d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Reference:
2038d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 2;
2039d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2040d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2041d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2042d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  assert(DiagKind != -1);
2043d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  S.Diag(DeclType.Loc, diag::err_compound_qualified_function_type)
2044d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << DiagKind << isa<FunctionType>(T.IgnoreParens()) << T
2045d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << getFunctionQualifiersAsString(T->castAs<FunctionProtoType>());
2046d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2047d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2048b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// Produce an approprioate diagnostic for an ambiguity between a function
2049b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// declarator and a C++ direct-initializer.
2050b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smithstatic void warnAboutAmbiguousFunction(Sema &S, Declarator &D,
2051b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith                                       DeclaratorChunk &DeclType, QualType RT) {
2052b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2053b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  assert(FTI.isAmbiguous && "no direct-initializer / function ambiguity");
2054b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2055b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // If the return type is void there is no ambiguity.
2056b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isVoidType())
2057b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2058b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2059b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a non-class type can have at most one argument.
2060b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!RT->isRecordType() && FTI.NumArgs > 1)
2061b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2062b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2063b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a reference must have exactly one argument.
2064b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isReferenceType() && FTI.NumArgs != 1)
2065b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2066b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2067b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Only warn if this declarator is declaring a function at block scope, and
2068b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // doesn't have a storage class (such as 'extern') specified.
2069b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!D.isFunctionDeclarator() ||
2070b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      D.getFunctionDefinitionKind() != FDK_Declaration ||
2071b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      !S.CurContext->isFunctionOrMethod() ||
2072b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      D.getDeclSpec().getStorageClassSpecAsWritten()
2073b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        != DeclSpec::SCS_unspecified)
2074b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2075b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2076b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Inside a condition, a direct initializer is not permitted. We allow one to
2077b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // be parsed in order to give better diagnostics in condition parsing.
2078b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (D.getContext() == Declarator::ConditionContext)
2079b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2080b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2081b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  SourceRange ParenRange(DeclType.Loc, DeclType.EndLoc);
2082b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2083d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  S.Diag(DeclType.Loc,
2084d64effc4e31044c05d6e4400150edb26e914983aRichard Smith         FTI.NumArgs ? diag::warn_parens_disambiguated_as_function_declaration
2085d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                     : diag::warn_empty_parens_are_function_decl)
2086d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    << ParenRange;
2087d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2088d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // If the declaration looks like:
2089d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   T var1,
2090d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   f();
2091d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // and name lookup finds a function named 'f', then the ',' was
2092d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // probably intended to be a ';'.
2093d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (!D.isFirstDeclarator() && D.getIdentifier()) {
2094d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Comma(D.getCommaLoc(), S.SourceMgr);
2095d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Name(D.getIdentifierLoc(), S.SourceMgr);
2096d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    if (Comma.getFileID() != Name.getFileID() ||
2097d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) {
2098d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      LookupResult Result(S, D.getIdentifier(), SourceLocation(),
2099d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                          Sema::LookupOrdinaryName);
2100d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      if (S.LookupName(Result, S.getCurScope()))
2101d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        S.Diag(D.getCommaLoc(), diag::note_empty_parens_function_call)
2102d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << FixItHint::CreateReplacement(D.getCommaLoc(), ";")
2103d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << D.getIdentifier();
2104d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    }
2105d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  }
2106b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2107d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (FTI.NumArgs > 0) {
2108d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration with parameters, eg. "T var(T());", suggest adding parens
2109d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // around the first parameter to turn the declaration into a variable
2110d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2111b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceRange Range = FTI.ArgInfo[0].Param->getSourceRange();
2112b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation B = Range.getBegin();
2113b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation E = S.PP.getLocForEndOfToken(Range.getEnd());
2114b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // FIXME: Maybe we should suggest adding braces instead of parens
2115b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // in C++11 for classes that don't have an initializer_list constructor.
2116b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    S.Diag(B, diag::note_additional_parens_for_variable_declaration)
2117b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(B, "(")
2118b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(E, ")");
2119d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  } else {
2120d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration without parameters, eg. "T var();", suggest replacing the
2121d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // parens with an initializer to turn the declaration into a variable
2122d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2123b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
2124d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2125b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // Empty parens mean value-initialization, and no parens mean
2126b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // default initialization. These are equivalent if the default
2127b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // constructor is user-provided or if zero-initialization is a
2128b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // no-op.
2129b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    if (RD && RD->hasDefinition() &&
2130b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        (RD->isEmpty() || RD->hasUserProvidedDefaultConstructor()))
2131b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor)
2132b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        << FixItHint::CreateRemoval(ParenRange);
2133b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    else {
2134b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      std::string Init = S.getFixItZeroInitializerForType(RT);
2135b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (Init.empty() && S.LangOpts.CPlusPlus0x)
2136b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        Init = "{}";
2137b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (!Init.empty())
2138b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        S.Diag(DeclType.Loc, diag::note_empty_parens_zero_initialize)
2139b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith          << FixItHint::CreateReplacement(ParenRange, Init);
2140b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    }
2141b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  }
2142b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith}
2143b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
21448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
21458cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                QualType declSpecType,
21468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                TypeSourceInfo *TInfo) {
21478cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
21488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = declSpecType;
21498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
21508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
21518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ASTContext &Context = S.Context;
21524e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &LangOpts = S.getLangOpts();
21538cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
21548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  bool ImplicitlyNoexcept = false;
21558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (D.getName().getKind() == UnqualifiedId::IK_OperatorFunctionId &&
21568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      LangOpts.CPlusPlus0x) {
21578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    OverloadedOperatorKind OO = D.getName().OperatorFunctionId.Operator;
21588cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    /// In C++0x, deallocation functions (normal and array operator delete)
21598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    /// are implicitly noexcept.
21608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    if (OO == OO_Delete || OO == OO_Array_Delete)
21618cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      ImplicitlyNoexcept = true;
21628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  }
216334b41d939a1328f484511c6002ba2456db879a29Richard Smith
2164cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
2165cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
2166cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
2167cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
21681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2169162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  // Does this declaration declare a typedef-name?
2170162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  bool IsTypedefName =
2171162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef ||
21723e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasDeclContext ||
21733e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasTemplateContext;
2174162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2175d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // Does T refer to a function type with a cv-qualifier or a ref-qualifier?
2176d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  bool IsQualifiedFunction = T->isFunctionProtoType() &&
2177d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      (T->castAs<FunctionProtoType>()->getTypeQuals() != 0 ||
2178d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith       T->castAs<FunctionProtoType>()->getRefQualifier() != RQ_None);
2179d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
218098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
218198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
218298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
21838ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
2185711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
2186711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2187d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction) {
2188d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      checkQualifiedFunction(S, T, DeclType);
2189d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = DeclType.Kind == DeclaratorChunk::Paren;
2190d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    }
21915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
2192075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
21938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildParenType(T);
2194075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
21955618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
21969af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
21979af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
21988cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_blocks_disable);
21991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22008cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
22012865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
22028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
22035618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
22045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
22056a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
22066a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
22078cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
22088cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
22096a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
22106a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
22116a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
22128cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.ObjC1 && T->getAs<ObjCObjectType>()) {
2213c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
22142865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
22158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
221614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
221714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
22188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildPointerType(T, DeclType.Loc, Name);
22192865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
22208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
2221711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
22225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
22230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
22246a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
22256a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
22268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
22278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
22286a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
22296a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
22306a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
22318cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
22322865474261a608c7873b87ba4af110d17907896dJohn McCall
22332865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
22342865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
22358cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
22365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
22370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
22385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
22396a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
22406a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
22418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
22428cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
22436a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
22446a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
22456a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
2246fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
224794f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
22485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
22495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
22505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
22515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
22525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
22535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
22545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
2255c05a94b7accd4035bf5d5897c434c445b22da855John McCall      if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
2256f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
2257f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
2258f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
22598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
2260f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
2261f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
2262f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
22637f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
22647f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // C99 6.7.5.2p1: The optional type qualifiers and the keyword static
22657f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // shall appear only in a declaration of a function parameter with an
22667f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // array type, ...
22677f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      if (ASM == ArrayType::Static || ATI.TypeQuals) {
226899570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay        if (!(D.isPrototypeContext() ||
226999570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay              D.getContext() == Declarator::KNRTypeListContext)) {
22707f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          S.Diag(DeclType.Loc, diag::err_array_static_outside_prototype) <<
22717f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
22727f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Remove the 'static' and the type qualifiers.
22737f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          if (ASM == ArrayType::Static)
22747f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ASM = ArrayType::Normal;
22757f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          ATI.TypeQuals = 0;
22767f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          D.setInvalidType(true);
22777f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
22787f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
22797f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // C99 6.7.5.2p1: ... and then only in the outermost array type
22807f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // derivation.
22817f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        unsigned x = chunkIndex;
22827f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        while (x != 0) {
22837f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Walk outwards along the declarator chunks.
22847f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          x--;
22857f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          const DeclaratorChunk &DC = D.getTypeObject(x);
22867f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          switch (DC.Kind) {
22877f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Paren:
22887f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            continue;
22897f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Array:
22907f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Pointer:
22917f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Reference:
22927f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::MemberPointer:
22937f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            S.Diag(DeclType.Loc, diag::err_array_static_not_outermost) <<
22947f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
22957f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            if (ASM == ArrayType::Static)
22967f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              ASM = ArrayType::Normal;
22977f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ATI.TypeQuals = 0;
22987f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            D.setInvalidType(true);
22997f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
23007f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Function:
23017f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::BlockPointer:
23027f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            // These are invalid anyway, so just ignore.
23037f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
23047f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          }
23057f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
23067f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      }
23077f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
23088ac2c66a1442985091c5ec2b33ce6d3df3bcb529Eli Friedman      T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
23098cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                           SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
23105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
23115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2312f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
23135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
23145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
23155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
23165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2317d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = FTI.TypeQuals || FTI.hasRefQualifier();
23183cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
231934b41d939a1328f484511c6002ba2456db879a29Richard Smith      // Check for auto functions and trailing return type and adjust the
232034b41d939a1328f484511c6002ba2456db879a29Richard Smith      // return type accordingly.
232134b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!D.isInvalidType()) {
232234b41d939a1328f484511c6002ba2456db879a29Richard Smith        // trailing-return-type is only required if we're declaring a function,
232334b41d939a1328f484511c6002ba2456db879a29Richard Smith        // and not, for instance, a pointer to a function.
232434b41d939a1328f484511c6002ba2456db879a29Richard Smith        if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
232554655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            !FTI.hasTrailingReturnType() && chunkIndex == 0) {
23268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
232734b41d939a1328f484511c6002ba2456db879a29Richard Smith               diag::err_auto_missing_trailing_return);
232834b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = Context.IntTy;
232934b41d939a1328f484511c6002ba2456db879a29Richard Smith          D.setInvalidType(true);
233054655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        } else if (FTI.hasTrailingReturnType()) {
2331e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          // T must be exactly 'auto' at this point. See CWG issue 681.
2332e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (isa<ParenType>(T)) {
23338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
2334e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith                 diag::err_trailing_return_in_parens)
2335e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith              << T << D.getDeclSpec().getSourceRange();
2336e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.setInvalidType(true);
2337f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman          } else if (D.getContext() != Declarator::LambdaExprContext &&
2338f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman                     (T.hasQualifiers() || !isa<AutoType>(T))) {
23398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
234034b41d939a1328f484511c6002ba2456db879a29Richard Smith                 diag::err_trailing_return_without_auto)
234134b41d939a1328f484511c6002ba2456db879a29Richard Smith              << T << D.getDeclSpec().getSourceRange();
234234b41d939a1328f484511c6002ba2456db879a29Richard Smith            D.setInvalidType(true);
234334b41d939a1328f484511c6002ba2456db879a29Richard Smith          }
234454655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          T = S.GetTypeFromParser(FTI.getTrailingReturnType(), &TInfo);
234554655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (T.isNull()) {
234654655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            // An error occurred parsing the trailing return type.
234754655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            T = Context.IntTy;
234854655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            D.setInvalidType(true);
234954655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          }
235034b41d939a1328f484511c6002ba2456db879a29Richard Smith        }
235134b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
235234b41d939a1328f484511c6002ba2456db879a29Richard Smith
2353e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // C99 6.7.5.3p1: The return type may not be a function or array type.
2354e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // For conversion functions, we'll diagnose this particular error later.
2355e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      if ((T->isArrayType() || T->isFunctionType()) &&
2356e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
2357e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned diagID = diag::err_func_returning_array_function;
2358e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // Last processing chunk in block context means this function chunk
2359e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // represents the block.
2360e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (chunkIndex == 0 &&
2361e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.getContext() == Declarator::BlockLiteralContext)
2362e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          diagID = diag::err_block_returning_array_function;
23638cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
2364e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        T = Context.IntTy;
2365e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        D.setInvalidType(true);
2366e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2367e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2368aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Do not allow returning half FP value.
2369aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // FIXME: This really should be in BuildFunctionType.
2370aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      if (T->isHalfType()) {
2371aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        S.Diag(D.getIdentifierLoc(),
2372aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov             diag::err_parameters_retval_cannot_have_fp16_type) << 1
2373aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov          << FixItHint::CreateInsertion(D.getIdentifierLoc(), "*");
2374aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        D.setInvalidType(true);
2375aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      }
2376aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
23775291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
23785291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
2379fff951371dfc309160a99d423e43a7841aeb35aaDouglas Gregor      if (isa<PointerType>(T) && T.getLocalCVRQualifiers() &&
23801e15394853bfae25112d9cc6b445504905e1f34aRafael Espindola          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId) &&
23818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          (!LangOpts.CPlusPlus || !T->isDependentType())) {
2382d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?");
2383d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
2384d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer);
2385d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2386d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr;
2387d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2388d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DiagnoseIgnoredQualifiers(PTI.TypeQuals,
2389d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
2390d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
2391d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
23928cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S);
2393d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2394d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth      } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
23958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          (!LangOpts.CPlusPlus ||
23965291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor           (!T->isDependentType() && !T->isRecordType()))) {
2397d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2398d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(),
2399d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getConstSpecLoc(),
2400d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getVolatileSpecLoc(),
2401d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getRestrictSpecLoc(),
24028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  S);
24035291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      }
2404d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
24058cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
2406402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
2407402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
2408b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
24095e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall        if (Tag->isCompleteDefinition())
24108cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
2411402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
2412402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
2413402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
24143cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
24153cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
2416162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (IsTypedefName && FTI.getExceptionSpecType())
24178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef)
24183e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          << (D.getContext() == Declarator::AliasDeclContext ||
24193e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith              D.getContext() == Declarator::AliasTemplateContext);
24203cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
2421b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // If we see "T var();" or "T var(T());" at block scope, it is probably
2422b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // an attempt to initialize a variable, not a function declaration.
2423b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (FTI.isAmbiguous)
2424b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        warnAboutAmbiguousFunction(S, D, DeclType, T);
2425b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
24268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (!FTI.NumArgs && !FTI.isVariadic && !LangOpts.CPlusPlus) {
24272865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
24282865474261a608c7873b87ba4af110d17907896dJohn McCall        T = Context.getFunctionNoProtoType(T);
24292865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
24302865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
24312865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
24322865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
24338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        if (!FTI.NumArgs && FTI.isVariadic && !LangOpts.CPlusPlus) {
2434965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
2435965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
2436965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
24378e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt            if (Attrs->getKind() == AttributeList::AT_Overloadable) {
2438965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
2439965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
2440965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
2441965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
2442965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
2443965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
24448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
2445c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
24462865474261a608c7873b87ba4af110d17907896dJohn McCall
24472865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
2448788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
2449788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
24508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
24512865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
24522865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
24532865474261a608c7873b87ba4af110d17907896dJohn McCall        }
24542865474261a608c7873b87ba4af110d17907896dJohn McCall
2455e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
2456e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
245754655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        EPI.HasTrailingReturn = FTI.hasTrailingReturnType();
2458e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
2459c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
2460c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : FTI.RefQualifierIsLValueRef? RQ_LValue
2461c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : RQ_RValue;
246291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
24635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
24645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
24655f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 16> ArgTys;
24662865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
24671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<bool, 16> ConsumedArguments;
2469f85e193739c953358c865005855253af4f68a497John McCall        ConsumedArguments.reserve(FTI.NumArgs);
2470f85e193739c953358c865005855253af4f68a497John McCall        bool HasAnyConsumedArguments = false;
2471f85e193739c953358c865005855253af4f68a497John McCall
24725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
2473d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
24748123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
247578c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
24762dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
24772dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
247891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier          assert((ArgTy == Context.getAdjustedParameterType(ArgTy)) &&
247979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor                 "Unadjusted type?");
24802dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
24815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
24825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
248372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
24842dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
24855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
24865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
24875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
24885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
24898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(DeclType.Loc, diag::err_void_only_param);
24902ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
24918123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
24922ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
24932ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
24948cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(FTI.ArgInfo[i].IdentLoc,
24954565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
24962ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
24978123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
24982ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
24992ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
25000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
25018cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                S.Diag(DeclType.Loc, diag::err_void_param_qualified);
25021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25032ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
25042ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
25052ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
2506aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov          } else if (ArgTy->isHalfType()) {
2507aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // Disallow half FP arguments.
2508aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // FIXME: This really should be in BuildFunctionType.
2509aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            S.Diag(Param->getLocation(),
2510aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov               diag::err_parameters_retval_cannot_have_fp16_type) << 0
2511aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            << FixItHint::CreateInsertion(Param->getLocation(), "*");
2512aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            D.setInvalidType();
2513eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
2514eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
2515a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
2516eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              Param->setKNRPromoted(true);
2517183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
2518eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              if (BTy->getKind() == BuiltinType::Float) {
2519eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
2520eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall                Param->setKNRPromoted(true);
2521eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              }
2522eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
25235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
252456a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
25258cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          if (LangOpts.ObjCAutoRefCount) {
2526f85e193739c953358c865005855253af4f68a497John McCall            bool Consumed = Param->hasAttr<NSConsumedAttr>();
2527f85e193739c953358c865005855253af4f68a497John McCall            ConsumedArguments.push_back(Consumed);
2528f85e193739c953358c865005855253af4f68a497John McCall            HasAnyConsumedArguments |= Consumed;
2529f85e193739c953358c865005855253af4f68a497John McCall          }
2530f85e193739c953358c865005855253af4f68a497John McCall
253154e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
25325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2533465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2534f85e193739c953358c865005855253af4f68a497John McCall        if (HasAnyConsumedArguments)
2535f85e193739c953358c865005855253af4f68a497John McCall          EPI.ConsumedArguments = ConsumedArguments.data();
2536f85e193739c953358c865005855253af4f68a497John McCall
25375f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 4> Exceptions;
253874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<ParsedType, 2> DynamicExceptions;
253974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<SourceRange, 2> DynamicExceptionRanges;
254074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        Expr *NoexceptExpr = 0;
254191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
25428b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        if (FTI.getExceptionSpecType() == EST_Dynamic) {
254374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // FIXME: It's rather inefficient to have to split into two vectors
254474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // here.
254574e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          unsigned N = FTI.NumExceptions;
254674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptions.reserve(N);
254774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptionRanges.reserve(N);
254874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          for (unsigned I = 0; I != N; ++I) {
254974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptions.push_back(FTI.Exceptions[I].Ty);
255074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptionRanges.push_back(FTI.Exceptions[I].Range);
2551e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
25528b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
255374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          NoexceptExpr = FTI.NoexceptExpr;
255474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        }
255591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
255674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        S.checkExceptionSpecification(FTI.getExceptionSpecType(),
255774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptions,
255874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptionRanges,
255974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      NoexceptExpr,
256074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      Exceptions,
256174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      EPI);
256291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
256374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        if (FTI.getExceptionSpecType() == EST_None &&
256474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            ImplicitlyNoexcept && chunkIndex == 0) {
25658999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl          // Only the outermost chunk is marked noexcept, of course.
25668999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl          EPI.ExceptionSpecType = EST_BasicNoexcept;
2567ef65f06e8e440aec541442cfd73a8a836e9bc842Sebastian Redl        }
2568465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2569e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI);
25705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
257104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
25725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
25735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2574f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
2575f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
25767bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
2577f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
25787bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
2579edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
2580edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
25818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      } else if (S.isDependentScopeSpecifier(SS) ||
25828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                 dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) {
25831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
25847bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
258587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
258687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
258787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
25887bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
25894a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
259087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
259187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
259287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
259314aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor        case NestedNameSpecifier::NamespaceAlias:
259487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
25959f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
25967bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
259787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
259887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
259987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
260091ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Note: if the NNS has a prefix and ClsType is a nondependent
260191ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // TemplateSpecializationType, then the NNS prefix is NOT included
260291ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // in ClsType; hence we wrap ClsType into an ElaboratedType.
260391ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // NOTE: in particular, no wrap occurs if ClsType already is an
260491ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Elaborated, DependentName, or DependentTemplateSpecialization.
260591ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
26067bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
260787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
260887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
2609f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
26108cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Mem.Scope().getBeginLoc(),
2611949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
2612949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
2613949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
2614f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
2615f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2616f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2617949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
26188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
2619949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
2620f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
2621949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
26222865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
26238cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
2624f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2625f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
2626f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
2627f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2628cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
2629cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
2630cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
2631cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
2632cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
2633c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
2634711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
2635711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      processTypeAttrs(state, T, false, attrs);
26365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2637971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
26388cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (LangOpts.CPlusPlus && T->isFunctionType()) {
2639183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
2640778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
2641971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
264291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // C++ 8.3.5p4:
2643708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
2644708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
2645708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
2646708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
2647683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    //
2648683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // Core issue 547 also allows cv-qualifiers on function types that are
2649683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // top-level template type arguments.
2650613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
2651613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
2652906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman      FreeFunction = ((D.getContext() != Declarator::MemberContext &&
2653906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman                       D.getContext() != Declarator::LambdaExprContext) ||
2654613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall                      D.getDeclSpec().isFriendSpecified());
2655613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
26568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec());
2657613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
2658613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
2659613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
266055dec868977ccb89cab0286122f9345f63bb5de7Richard Smith    // C++0x [dcl.constexpr]p8: A constexpr specifier for a non-static member
266155dec868977ccb89cab0286122f9345f63bb5de7Richard Smith    // function that is not a constructor declares that function to be const.
266255dec868977ccb89cab0286122f9345f63bb5de7Richard Smith    if (D.getDeclSpec().isConstexprSpecified() && !FreeFunction &&
26631bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith        D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static &&
266455dec868977ccb89cab0286122f9345f63bb5de7Richard Smith        D.getName().getKind() != UnqualifiedId::IK_ConstructorName &&
266555dec868977ccb89cab0286122f9345f63bb5de7Richard Smith        D.getName().getKind() != UnqualifiedId::IK_ConstructorTemplateId &&
266655dec868977ccb89cab0286122f9345f63bb5de7Richard Smith        !(FnTy->getTypeQuals() & DeclSpec::TQ_const)) {
266755dec868977ccb89cab0286122f9345f63bb5de7Richard Smith      // Rebuild function type adding a 'const' qualifier.
266855dec868977ccb89cab0286122f9345f63bb5de7Richard Smith      FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
266955dec868977ccb89cab0286122f9345f63bb5de7Richard Smith      EPI.TypeQuals |= DeclSpec::TQ_const;
267091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      T = Context.getFunctionType(FnTy->getResultType(),
267155dec868977ccb89cab0286122f9345f63bb5de7Richard Smith                                  FnTy->arg_type_begin(),
267255dec868977ccb89cab0286122f9345f63bb5de7Richard Smith                                  FnTy->getNumArgs(), EPI);
267355dec868977ccb89cab0286122f9345f63bb5de7Richard Smith    }
267455dec868977ccb89cab0286122f9345f63bb5de7Richard Smith
2675d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.fct]p6 (w/DR1417):
2676d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // An attempt to specify a function type with a cv-qualifier-seq or a
2677d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // ref-qualifier (including by typedef-name) is ill-formed unless it is:
2678d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type for a non-static member function,
2679d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type to which a pointer to member refers,
2680d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the top-level function type of a function typedef declaration or
2681d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //    alias-declaration,
2682d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id in the default argument of a type-parameter, or
2683d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id of a template-argument for a type-parameter
2684d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction &&
2685d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !(!FreeFunction &&
2686d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) &&
2687d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !IsTypedefName &&
2688d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        D.getContext() != Declarator::TemplateTypeArgContext) {
268996a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      SourceLocation Loc = D.getLocStart();
2690d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      SourceRange RemovalRange;
2691d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      unsigned I;
2692d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      if (D.isFunctionDeclarator(I)) {
2693d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        SmallVector<SourceLocation, 4> RemovalLocs;
2694d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        const DeclaratorChunk &Chunk = D.getTypeObject(I);
2695d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        assert(Chunk.Kind == DeclaratorChunk::Function);
2696d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.hasRefQualifier())
2697d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getRefQualifierLoc());
2698d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Const)
2699d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getConstQualifierLoc());
2700d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Volatile)
2701d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getVolatileQualifierLoc());
2702d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        // FIXME: We do not track the location of the __restrict qualifier.
2703d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //if (Chunk.Fun.TypeQuals & Qualifiers::Restrict)
2704d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //  RemovalLocs.push_back(Chunk.Fun.getRestrictQualifierLoc());
2705d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (!RemovalLocs.empty()) {
2706d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          std::sort(RemovalLocs.begin(), RemovalLocs.end(),
2707aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                    BeforeThanCompare<SourceLocation>(S.getSourceManager()));
2708d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalRange = SourceRange(RemovalLocs.front(), RemovalLocs.back());
2709d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          Loc = RemovalLocs.front();
2710683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
2711683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      }
2712d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2713d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      S.Diag(Loc, diag::err_invalid_qualified_function_type)
2714d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FreeFunction << D.isFunctionDeclarator() << T
2715d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << getFunctionQualifiersAsString(FnTy)
2716d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FixItHint::CreateRemoval(RemovalRange);
2717d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2718d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // Strip the cv-qualifiers and ref-qualifiers from the type.
2719d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
2720d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.TypeQuals = 0;
2721d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.RefQualifier = RQ_None;
2722d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
272391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      T = Context.getFunctionType(FnTy->getResultType(),
2724d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                  FnTy->arg_type_begin(),
2725d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                  FnTy->getNumArgs(), EPI);
2726971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
2727971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
27281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2729711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
2730711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull())
2731711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = D.getAttributes())
2732711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      processTypeAttrs(state, T, false, attrs);
2733711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2734711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
2735711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
2736711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2737148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  // C++0x [dcl.constexpr]p9:
2738148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  //  A constexpr specifier used in an object declaration declares the object
273991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //  as const.
2740148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
2741737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
2742737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
2743737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
274491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // If there was an ellipsis in the declarator, the declaration declares a
2745a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
2746a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (D.hasEllipsis() && !T.isNull()) {
2747a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
274891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   A declarator-id or abstract-declarator containing an ellipsis shall
2749a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
2750a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
2751a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
2752a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
2753a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
275491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   [...] When it is part of a parameter-declaration-clause, the
275591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   parameter pack is a function parameter pack (14.5.3). The type T
2756a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
275791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   a template parameter pack; each template parameter pack in T is
2758a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
2759a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2760a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
2761a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
2762a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
276391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(D.getEllipsisLoc(),
2764a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
2765a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
2766a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
2767a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
2768cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
2769a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
2770a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
277191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2772a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
2773a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
277491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   If a template-parameter is a [...] is a parameter-declaration that
2775a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
2776a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
2777a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2778a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
2779a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
2780a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
2781a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
2782cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
2783e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith      else
2784e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith        S.Diag(D.getEllipsisLoc(),
2785e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith               LangOpts.CPlusPlus0x
2786e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 ? diag::warn_cxx98_compat_variadic_templates
2787e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 : diag::ext_variadic_templates);
2788a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
278991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2790a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
2791a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
2792cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:  // FIXME: special diagnostic here?
2793cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:     // FIXME: special diagnostic here?
2794a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
27950b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
2796162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
27973e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
2798a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
2799a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
2800a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
2801a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
2802a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
280317b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
2804a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
2805f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
28067796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
2807683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    case Declarator::TemplateTypeArgContext:
2808a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
2809a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
28108cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      S.Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
2811a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
2812a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2813a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
2814a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
2815e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2816bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
2817bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
2818bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
2819bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
2820db7abf78dedc2ef6ccb42b3dac6ab330fe2ea469Argyrios Kyrtzidis
28218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return S.GetTypeSourceInfoForDeclarator(D, T, TInfo);
28228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
28238cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
28248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// GetTypeForDeclarator - Convert the type for the specified
28258cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// declarator to Type instances.
28268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis///
28278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// The result of this call will never be null, but the associated
28288cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// type may be a null type if there's an unrecoverable error.
28298cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
28308cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // Determine the type of the declarator. Not all forms of declarator
28318cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // have a type.
28328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
28338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeProcessingState state(*this, D);
28348cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
28358cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
28368cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
28378cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (T.isNull())
28388cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
28398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
28404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (D.isPrototypeContext() && getLangOpts().ObjCAutoRefCount)
28418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    inferARCWriteback(state, T);
284291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
28438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return GetFullTypeForDeclarator(state, T, ReturnTypeInfo);
28445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
28455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
284631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclSpec(Sema &S,
284731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           QualType &declSpecTy,
284831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           Qualifiers::ObjCLifetime ownership) {
284931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy->isObjCRetainableType() &&
285031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) {
285131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers qs;
285231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    qs.addObjCLifetime(ownership);
285331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    declSpecTy = S.Context.getQualifiedType(declSpecTy, qs);
285431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
285531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
285631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
285731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
285831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
285931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            unsigned chunkIndex) {
286031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
286131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
286231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
286331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Look for an explicit lifetime attribute.
286431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclaratorChunk &chunk = D.getTypeObject(chunkIndex);
286531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (const AttributeList *attr = chunk.getAttrs(); attr;
286631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis         attr = attr->getNext())
28678e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    if (attr->getKind() == AttributeList::AT_ObjCOwnership)
286831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
286931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
287031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  const char *attrStr = 0;
287131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  switch (ownership) {
28723026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
287331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break;
287431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Strong: attrStr = "strong"; break;
287531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Weak: attrStr = "weak"; break;
287631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break;
287731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
287831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
287931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // If there wasn't one, add one (with an invalid source location
288031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // so that we don't make an AttributedType for it).
288131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  AttributeList *attr = D.getAttributePool()
288231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    .create(&S.Context.Idents.get("objc_ownership"), SourceLocation(),
288331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            /*scope*/ 0, SourceLocation(),
288431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            &S.Context.Idents.get(attrStr), SourceLocation(),
288593f95f2a2cbb6bb3d17bfb5fc74ce1cccea751b6Sean Hunt            /*args*/ 0, 0, AttributeList::AS_GNU);
288631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  spliceAttrIntoList(*attr, chunk.getAttrListRef());
288731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
288831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // TODO: mark whether we did this inference?
288931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
289031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
289148d798ce32447607144db70a484cdb99c1180663Benjamin Kramer/// \brief Used for transferring ownership in casts resulting in l-values.
289231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnership(TypeProcessingState &state,
289331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 QualType &declSpecTy,
289431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 Qualifiers::ObjCLifetime ownership) {
289531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
289631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
289731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
289831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  int inner = -1;
28996ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis  bool hasIndirection = false;
290031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
290131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    DeclaratorChunk &chunk = D.getTypeObject(i);
290231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    switch (chunk.Kind) {
290331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Paren:
290431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      // Ignore parens.
290531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
290631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
290731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Array:
290831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Reference:
290931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Pointer:
29106ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
29116ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        hasIndirection = true;
291231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      inner = i;
291331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
291431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
291531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::BlockPointer:
29166ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
29176ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        transferARCOwnershipToDeclaratorChunk(state, ownership, i);
29186ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      return;
291931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
292031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Function:
292131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::MemberPointer:
292231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
292331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    }
292431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
292531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
292631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (inner == -1)
29276ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    return;
292831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
292991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  DeclaratorChunk &chunk = D.getTypeObject(inner);
293031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (chunk.Kind == DeclaratorChunk::Pointer) {
293131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (declSpecTy->isObjCRetainableType())
293231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
29336ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    if (declSpecTy->isObjCObjectType() && hasIndirection)
293431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclaratorChunk(state, ownership, inner);
293531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  } else {
293631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    assert(chunk.Kind == DeclaratorChunk::Array ||
293731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis           chunk.Kind == DeclaratorChunk::Reference);
293831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
293931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
294031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
294131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
294231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) {
294331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeProcessingState state(*this, D);
294431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
294531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
294631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
294731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy.isNull())
294831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
294931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
29504e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount) {
295131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy);
295231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (ownership != Qualifiers::OCL_None)
295331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      transferARCOwnership(state, declSpecTy, ownership);
295431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
295531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
295631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo);
295731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
295831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
295914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall/// Map an AttributedType::Kind to an AttributeList::Kind.
296014aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
296114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  switch (kind) {
296214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_address_space:
29638e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_AddressSpace;
296414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_regparm:
29658e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Regparm;
296614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_vector_size:
29678e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_VectorSize;
296814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_vector_type:
29698e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonVectorType;
297014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_polyvector_type:
29718e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonPolyVectorType;
297214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_objc_gc:
29738e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCGC;
2974b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  case AttributedType::attr_objc_ownership:
29758e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCOwnership;
297614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_noreturn:
29778e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NoReturn;
297814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_cdecl:
29798e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_CDecl;
298014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_fastcall:
29818e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_FastCall;
298214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_stdcall:
29838e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_StdCall;
298414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_thiscall:
29858e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ThisCall;
298614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_pascal:
29878e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pascal;
2988414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case AttributedType::attr_pcs:
29898e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pcs;
299014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
299114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  llvm_unreachable("unexpected attribute kind!");
299214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
299314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
299414aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic void fillAttributedTypeLoc(AttributedTypeLoc TL,
299514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                  const AttributeList *attrs) {
299614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributedType::Kind kind = TL.getAttrKind();
299714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
299814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  assert(attrs && "no type attributes in the expected location!");
299914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributeList::Kind parsedKind = getAttrListKind(kind);
300014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  while (attrs->getKind() != parsedKind) {
300114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    attrs = attrs->getNext();
300214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    assert(attrs && "no matching attribute in expected location!");
300314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
300414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
300514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  TL.setAttrNameLoc(attrs->getLoc());
300614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrExprOperand())
300714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrExprOperand(attrs->getArg(0));
300814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  else if (TL.hasAttrEnumOperand())
300914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrEnumOperandLoc(attrs->getParameterLoc());
301014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
301114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // FIXME: preserve this information to here.
301214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrOperand())
301314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrOperandParensRange(SourceRange());
301414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
301514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
301651bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
301751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
3018c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    ASTContext &Context;
301951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
3020f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
302151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
302291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
3023c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      : Context(Context), DS(DS) {}
3024f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
302514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
302614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, DS.getAttributes().getList());
302714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      Visit(TL.getModifiedLoc());
302814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
302951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
303051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
303151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
303251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
303351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
303451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
303551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
303651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
30371de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // FIXME. We should have DS.getTypeSpecTypeEndLoc(). But, it requires
30381de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // addition field. What we have is good enough for dispay of location
30391de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // of 'fixit' on interface name.
30401de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      TL.setNameEndLoc(DS.getLocEnd());
3041c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
3042c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3043c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
3044c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
3045c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
3046c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.getBaseLoc().initialize(Context, SourceLocation());
3047c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
3048c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
3049c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
3050c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
305154e14c4db764c0636160d26c5bbf491637c83a76John McCall
3052c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
305354e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
305454e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
305554e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
305654e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
305754e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
305854e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
305954e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
306054e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
306154e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
306254e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
306354e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
306454e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
306551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
306654e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
306754e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
3068c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
306951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
3070833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
3071a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
3072b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3073833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3074833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
3075833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
3076a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
30770daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara        TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
3078833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
3079833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
3080833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3081e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
3082e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
3083e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
3084e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TemplateSpecializationTypeLoc NamedTL =
3085e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
3086e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
3087e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
3088e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      else
3089e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
3090833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
3091cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3092cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
3093cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3094cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3095cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3096cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3097cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
3098cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3099cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3100b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
3101cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
3102b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3103cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
3104cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3105ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3106ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      // FIXME: This holds only because we only have one unary transform.
3107ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType);
3108ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setKWLoc(DS.getTypeSpecTypeLoc());
3109ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setParensRange(DS.getTypeofParensRange());
3110ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getRepAsType());
3111ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TypeSourceInfo *TInfo = 0;
3112ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3113ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setUnderlyingTInfo(TInfo);
3114ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
3115ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3116ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
3117ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
3118ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
3119ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
3120ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
3121ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
3122ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
3123ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
3124ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
3125ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
3126ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
3127ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
3128ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
3129ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
3130e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3131e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
3132e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
3133253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
3134e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
3135b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3136e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
3137e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
3138e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
3139e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
3140e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
314138a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara      TL.setElaboratedKeywordLoc(Keyword != ETK_None
314238a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 ? DS.getTypeSpecTypeLoc()
314338a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 : SourceLocation());
3144e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
31459e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
3146e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
3147e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3148e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
314966581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
315066581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
315166581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
315266581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
315366581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
315433500955d731c73717af52088b7fc0e7a85681e7John McCall    }
315533500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
315633500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
315766581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
315866581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
315966581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
316066581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
316166581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
316266581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara                TInfo->getTypeLoc()));
31630daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    }
31640daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    void VisitTagTypeLoc(TagTypeLoc TL) {
31650daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
3166e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3167b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
3168b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      TL.setKWLoc(DS.getTypeSpecTypeLoc());
3169b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      TL.setParensRange(DS.getTypeofParensRange());
317091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
317143fe245b37c3cd36d837aab9eb98551328d30141Douglas Gregor      TypeSourceInfo *TInfo = 0;
317243fe245b37c3cd36d837aab9eb98551328d30141Douglas Gregor      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
317343fe245b37c3cd36d837aab9eb98551328d30141Douglas Gregor      TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
3174b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
3175e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
317651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
317751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
3178c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initialize(Context, DS.getTypeSpecTypeLoc());
317951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
318051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
3181eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
318251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
3183b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    ASTContext &Context;
318451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
3185f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
318651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
3187b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
3188b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      : Context(Context), Chunk(Chunk) {}
31894adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
319051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
31919f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
319251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
31934adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3194f85e193739c953358c865005855253af4f68a497John McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3195f85e193739c953358c865005855253af4f68a497John McCall      fillAttributedTypeLoc(TL, Chunk.getAttrs());
3196f85e193739c953358c865005855253af4f68a497John McCall    }
319751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
319851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
319951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
32004adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
320151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
320251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
320351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
32044adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
320551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
320651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
320751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
32084adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
320951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
321051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
3211b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const CXXScopeSpec& SS = Chunk.Mem.Scope();
3212b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
3213b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3214b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const Type* ClsTy = TL.getClass();
3215b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      QualType ClsQT = QualType(ClsTy, 0);
3216b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
3217b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Now copy source location info into the type loc component.
3218b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeLoc ClsTL = ClsTInfo->getTypeLoc();
3219b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
3220b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Identifier:
3221b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
3222b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        {
3223fd9c42ed22fb4f7f865f7d8f8848df84ddf8262cAbramo Bagnara          DependentNameTypeLoc DNTLoc = cast<DependentNameTypeLoc>(ClsTL);
322438a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          DNTLoc.setElaboratedKeywordLoc(SourceLocation());
3225b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
3226b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
3227b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3228b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3229b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3230b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpec:
3231b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpecWithTemplate:
3232b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        if (isa<ElaboratedType>(ClsTy)) {
3233b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ElaboratedTypeLoc ETLoc = *cast<ElaboratedTypeLoc>(&ClsTL);
323438a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          ETLoc.setElaboratedKeywordLoc(SourceLocation());
3235b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ETLoc.setQualifierLoc(NNSLoc.getPrefix());
3236b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
3237b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
3238b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        } else {
3239b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
3240b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3241b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3242b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3243b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Namespace:
3244b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::NamespaceAlias:
3245b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Global:
3246b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        llvm_unreachable("Nested-name-specifier must name a type");
3247b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      }
3248b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3249b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Finally fill in MemberPointerLocInfo fields.
325051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
3251b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TL.setClassTInfo(ClsTInfo);
32524adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
325351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
325451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
325554e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
325654e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
325751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
325851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
325951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
326051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
326151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
326251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
326351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
326451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
326551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
326651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
326751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
326851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
326951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
327051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
327151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
3272796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeBegin(Chunk.Loc);
3273796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeEnd(Chunk.EndLoc);
327451bd803fbdade51d674598ed45da3d54190a656cJohn McCall
327551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
327654e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
3277d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
327854e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
32794adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
328051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
32814adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
3282075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
3283075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
3284075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
3285075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
3286075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
32871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
328851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
32899f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
32904adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
329151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
329251bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
32934adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3294a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
329551bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
329651bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
329705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
329805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
329905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
330005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
330105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
3302a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
330305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
330405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
3305a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
3306a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
330751bd803fbdade51d674598ed45da3d54190a656cJohn McCall
3308a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
3309a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
3310a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc());
331191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
3312a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
331391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
33148ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
331514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    while (isa<AttributedTypeLoc>(CurrTL)) {
331614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      AttributedTypeLoc TL = cast<AttributedTypeLoc>(CurrTL);
331714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs());
331814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
331914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
332014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3321b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
332251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
33234adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
332491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3325b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
3326b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
3327b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
332805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
332905baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
333005baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
3331b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
3332c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
333305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
333491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3335a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
33364adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
33374adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3338a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
3339b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
33401bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
33411bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
33421bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
334391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
3344eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
3345a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
33461bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
33471bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
3348b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
33491bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
33501bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
33511bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
33521bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
3353b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*"
335435d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
335535d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
33561bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
33571bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
3358f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
33595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
33605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
33615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
33621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3363d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
3364bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
33655153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
3366809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
33675912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
3368e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall  // Make sure there are no unused decl attributes on the declarator.
3369cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // We don't want to do this for ObjC parameters because we're going
3370cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // to apply them to the actual parameter declaration.
3371cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  if (D.getContext() != Declarator::ObjCParameterContext)
3372cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    checkUnusedDeclAttributes(D);
3373e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
33744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
3375402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
33766d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
3377402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
3378402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
3379b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
33805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
33815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3382e97179c675b341927807c718be215c8d1aab8acbDouglas GregorParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) {
3383e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  QualType T = Context.getObjCInstanceType();
3384e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc);
3385e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return CreateParsedType(T, TInfo);
3386e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
3387e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3388e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3389c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3390c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
3391c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3392232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3393232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
3394c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
3395c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
33961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
3397c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
33980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
3399232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
340029e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified by
340129e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // qualifiers for two or more different address spaces."
3402232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
3403c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
3404e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3405c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3406232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
34071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3408020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be
3409020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // qualified by an address-space qualifier."
3410020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  if (Type->isFunctionType()) {
3411020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type);
3412020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    Attr.setInvalid();
3413020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    return;
3414020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  }
3415020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne
3416232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
3417545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
3418f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3419e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3420c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3421232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3422545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
3423232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
3424ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
3425ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
3426dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
3427dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
3428e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3429c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3430232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3431232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3432efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
3433efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
3434efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
3435efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
3436efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
3437e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
3438efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
3439efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
3440efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
3441efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3442efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
34430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
3444efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
3445efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
34460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
3447e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3448efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
3449efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3450efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
34511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
3452f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
3453c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
3454c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
3455d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// Does this type have a "direct" ownership qualifier?  That is,
3456d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// is it written like "__strong id", as opposed to something like
3457d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// "typeof(foo)", where that happens to be strong?
3458d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCallstatic bool hasDirectOwnershipQualifier(QualType type) {
3459d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Fast path: no qualifier at all.
3460d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  assert(type.getQualifiers().hasObjCLifetime());
3461d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3462d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  while (true) {
3463d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // __strong id
3464d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (const AttributedType *attr = dyn_cast<AttributedType>(type)) {
3465d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      if (attr->getAttrKind() == AttributedType::attr_objc_ownership)
3466d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        return true;
3467d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3468d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = attr->getModifiedType();
3469d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3470d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // X *__strong (...)
3471d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else if (const ParenType *paren = dyn_cast<ParenType>(type)) {
3472d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = paren->getInnerType();
347391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3474d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // That's it for things we want to complain about.  In particular,
3475d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // we do not want to look through typedefs, typeof(expr),
3476d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // typeof(type), or any other way that the type is somehow
3477d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // abstracted.
3478d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else {
347991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3480d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return false;
3481d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3482d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
3483d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall}
3484d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3485b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis/// handleObjCOwnershipTypeAttr - Process an objc_ownership
3486f85e193739c953358c865005855253af4f68a497John McCall/// attribute on the specified type.
3487f85e193739c953358c865005855253af4f68a497John McCall///
3488f85e193739c953358c865005855253af4f68a497John McCall/// Returns 'true' if the attribute was handled.
3489b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
3490f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr,
3491f85e193739c953358c865005855253af4f68a497John McCall                                       QualType &type) {
3492e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  bool NonObjCPointer = false;
3493e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3494e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!type->isDependentType()) {
3495e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    if (const PointerType *ptr = type->getAs<PointerType>()) {
3496e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      QualType pointee = ptr->getPointeeType();
3497e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      if (pointee->isObjCRetainableType() || pointee->isPointerType())
3498e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis        return false;
3499e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // It is important not to lose the source info that there was an attribute
3500e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // applied to non-objc pointer. We will create an attributed type but
3501e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // its type will be the same as the original type.
3502e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      NonObjCPointer = true;
3503e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    } else if (!type->isObjCRetainableType()) {
3504e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      return false;
3505e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3506e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3507f85e193739c953358c865005855253af4f68a497John McCall
3508f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
3509440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  SourceLocation AttrLoc = attr.getLoc();
3510440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isMacroID())
3511440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    AttrLoc = S.getSourceManager().getImmediateExpansionRange(AttrLoc).first;
3512f85e193739c953358c865005855253af4f68a497John McCall
3513f85e193739c953358c865005855253af4f68a497John McCall  if (!attr.getParameterName()) {
3514440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::err_attribute_argument_n_not_string)
3515b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      << "objc_ownership" << 1;
3516f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3517f85e193739c953358c865005855253af4f68a497John McCall    return true;
3518f85e193739c953358c865005855253af4f68a497John McCall  }
3519f85e193739c953358c865005855253af4f68a497John McCall
3520d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Consume lifetime attributes without further comment outside of
3521d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // ARC mode.
35224e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!S.getLangOpts().ObjCAutoRefCount)
3523d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    return true;
3524d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3525f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime lifetime;
3526f85e193739c953358c865005855253af4f68a497John McCall  if (attr.getParameterName()->isStr("none"))
3527f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_ExplicitNone;
3528f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("strong"))
3529f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Strong;
3530f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("weak"))
3531f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Weak;
3532f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("autoreleasing"))
3533f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Autoreleasing;
3534f85e193739c953358c865005855253af4f68a497John McCall  else {
3535440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_attribute_type_not_supported)
3536b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      << "objc_ownership" << attr.getParameterName();
3537f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3538f85e193739c953358c865005855253af4f68a497John McCall    return true;
3539f85e193739c953358c865005855253af4f68a497John McCall  }
3540f85e193739c953358c865005855253af4f68a497John McCall
3541d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  SplitQualType underlyingType = type.split();
3542d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3543d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Check for redundant/conflicting ownership qualifiers.
3544d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  if (Qualifiers::ObjCLifetime previousLifetime
3545d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        = type.getQualifiers().getObjCLifetime()) {
3546d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // If it's written directly, that's an error.
3547d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (hasDirectOwnershipQualifier(type)) {
3548d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant)
3549d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        << type;
3550d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return true;
3551d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3552d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3553d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // Otherwise, if the qualifiers actually conflict, pull sugar off
3554d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // until we reach a type that is directly qualified.
3555d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (previousLifetime != lifetime) {
3556d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // This should always terminate: the canonical type is
3557d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // qualified, so some bit of sugar must be hiding it.
3558d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      while (!underlyingType.Quals.hasObjCLifetime()) {
3559d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        underlyingType = underlyingType.getSingleStepDesugaredType();
3560d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      }
3561d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      underlyingType.Quals.removeObjCLifetime();
3562d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3563d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
3564d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3565d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  underlyingType.Quals.addObjCLifetime(lifetime);
3566f85e193739c953358c865005855253af4f68a497John McCall
3567e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (NonObjCPointer) {
3568e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    StringRef name = attr.getName()->getName();
3569e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    switch (lifetime) {
3570e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_None:
3571e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_ExplicitNone:
3572e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      break;
3573e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Strong: name = "__strong"; break;
3574e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Weak: name = "__weak"; break;
3575e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break;
3576e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3577e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_objc_object_attribute_wrong_type)
3578e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      << name << type;
3579e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3580e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3581f85e193739c953358c865005855253af4f68a497John McCall  QualType origType = type;
3582e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!NonObjCPointer)
3583d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    type = S.Context.getQualifiedType(underlyingType);
3584f85e193739c953358c865005855253af4f68a497John McCall
3585f85e193739c953358c865005855253af4f68a497John McCall  // If we have a valid source location for the attribute, use an
3586f85e193739c953358c865005855253af4f68a497John McCall  // AttributedType instead.
3587440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isValid())
3588b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis    type = S.Context.getAttributedType(AttributedType::attr_objc_ownership,
3589f85e193739c953358c865005855253af4f68a497John McCall                                       origType, type);
3590f85e193739c953358c865005855253af4f68a497John McCall
35919f084a3166b684573ba49df28fc5792bc37d92e1John McCall  // Forbid __weak if the runtime doesn't support it.
3592f85e193739c953358c865005855253af4f68a497John McCall  if (lifetime == Qualifiers::OCL_Weak &&
35930a7dd788dbef975f35f273c7ab913f480f7edd60John McCall      !S.getLangOpts().ObjCARCWeak && !NonObjCPointer) {
3594f85e193739c953358c865005855253af4f68a497John McCall
3595f85e193739c953358c865005855253af4f68a497John McCall    // Actually, delay this until we know what we're parsing.
3596f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
3597f85e193739c953358c865005855253af4f68a497John McCall      S.DelayedDiagnostics.add(
3598440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis          sema::DelayedDiagnostic::makeForbiddenType(
3599440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis              S.getSourceManager().getExpansionLoc(AttrLoc),
3600f85e193739c953358c865005855253af4f68a497John McCall              diag::err_arc_weak_no_runtime, type, /*ignored*/ 0));
3601f85e193739c953358c865005855253af4f68a497John McCall    } else {
3602440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis      S.Diag(AttrLoc, diag::err_arc_weak_no_runtime);
3603f85e193739c953358c865005855253af4f68a497John McCall    }
3604f85e193739c953358c865005855253af4f68a497John McCall
3605f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3606f85e193739c953358c865005855253af4f68a497John McCall    return true;
3607f85e193739c953358c865005855253af4f68a497John McCall  }
360891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
360991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // Forbid __weak for class objects marked as
3610742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  // objc_arc_weak_reference_unavailable
3611742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  if (lifetime == Qualifiers::OCL_Weak) {
3612742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    QualType T = type;
3613742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    while (const PointerType *ptr = T->getAs<PointerType>())
3614742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      T = ptr->getPointeeType();
3615742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
36164e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith      if (ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl()) {
36174e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        if (Class->isArcWeakrefUnavailable()) {
36184e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class);
36194e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(ObjT->getInterfaceDecl()->getLocation(),
36204e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith                   diag::note_class_declared);
36214e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        }
3622742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      }
3623742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    }
3624742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  }
362591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3626f85e193739c953358c865005855253af4f68a497John McCall  return true;
3627f85e193739c953358c865005855253af4f68a497John McCall}
3628f85e193739c953358c865005855253af4f68a497John McCall
3629711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
3630711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
3631711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
3632711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
3633711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
3634711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
3635711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
3636711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
3637711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3638711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
3639711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
3640711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
3641711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
3642711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
3643711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3644711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
3645711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
3646711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3647711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3648d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
36491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3650d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
3651711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!attr.getParameterName()) {
3652711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3653ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
3654711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3655711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3656ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
36570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
3658711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getNumArgs() != 0) {
3659711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3660711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3661711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3662d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
3663711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getParameterName()->isStr("weak"))
36640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
3665711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  else if (attr.getParameterName()->isStr("strong"))
36660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
3667d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
3668711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
3669711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      << "objc_gc" << attr.getParameterName();
3670711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3671711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3672d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
36731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
367414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  QualType origType = type;
367514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  type = S.Context.getObjCGCQualType(origType, GCAttr);
367614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
367714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // Make an attributed type to preserve the source information.
367814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (attr.getLoc().isValid())
367914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
368014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                       origType, type);
368114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3682711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
3683d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
3684d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
3685e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallnamespace {
3686e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// A helper class to unwrap a type down to a function for the
3687e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// purposes of applying attributes there.
3688e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///
3689e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// Use:
3690e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
3691e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   if (unwrapped.isFunctionType()) {
3692e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     const FunctionType *fn = unwrapped.get();
3693e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     // change fn somehow
3694e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     T = unwrapped.wrap(fn);
3695e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   }
3696e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  struct FunctionTypeUnwrapper {
3697e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    enum WrapKind {
3698e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Desugar,
3699e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Parens,
3700e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Pointer,
3701e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      BlockPointer,
3702e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Reference,
3703e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      MemberPointer
3704e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    };
3705e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3706e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType Original;
3707e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *Fn;
37085f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<unsigned char /*WrapKind*/, 8> Stack;
3709e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3710e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
3711e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      while (true) {
3712e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const Type *Ty = T.getTypePtr();
3713e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<FunctionType>(Ty)) {
3714e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Fn = cast<FunctionType>(Ty);
3715e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return;
3716e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ParenType>(Ty)) {
3717e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ParenType>(Ty)->getInnerType();
3718e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Parens);
3719e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<PointerType>(Ty)) {
3720e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<PointerType>(Ty)->getPointeeType();
3721e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Pointer);
3722e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<BlockPointerType>(Ty)) {
3723e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<BlockPointerType>(Ty)->getPointeeType();
3724e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(BlockPointer);
3725e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<MemberPointerType>(Ty)) {
3726e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<MemberPointerType>(Ty)->getPointeeType();
3727e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(MemberPointer);
3728e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ReferenceType>(Ty)) {
3729e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ReferenceType>(Ty)->getPointeeType();
3730e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Reference);
3731e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else {
3732e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          const Type *DTy = Ty->getUnqualifiedDesugaredType();
3733e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          if (Ty == DTy) {
3734e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            Fn = 0;
3735e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            return;
3736e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          }
3737e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3738e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = QualType(DTy, 0);
3739e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Desugar);
3740e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        }
3741e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3742e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3743e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3744e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    bool isFunctionType() const { return (Fn != 0); }
3745e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *get() const { return Fn; }
3746e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3747e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(Sema &S, const FunctionType *New) {
3748e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // If T wasn't modified from the unwrapped type, do nothing.
3749e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (New == get()) return Original;
3750e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3751e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Fn = New;
3752e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return wrap(S.Context, Original, 0);
3753e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3754e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3755e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  private:
3756e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
3757e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size())
3758e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getQualifiedType(Fn, Old.getQualifiers());
3759e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3760e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // Build up the inner type, applying the qualifiers from the old
3761e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // type to the new type.
3762e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      SplitQualType SplitOld = Old.split();
3763e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3764e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // As a special case, tail-recurse if there are no qualifiers.
3765200fa53fd420aa8369586f569dbece04930ad6a3John McCall      if (SplitOld.Quals.empty())
3766200fa53fd420aa8369586f569dbece04930ad6a3John McCall        return wrap(C, SplitOld.Ty, I);
3767200fa53fd420aa8369586f569dbece04930ad6a3John McCall      return C.getQualifiedType(wrap(C, SplitOld.Ty, I), SplitOld.Quals);
3768e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3769e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3770e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
3771e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size()) return QualType(Fn, 0);
3772e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3773e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      switch (static_cast<WrapKind>(Stack[I++])) {
3774e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Desugar:
3775e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // This is the point at which we potentially lose source
3776e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // information.
3777e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
3778e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3779e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Parens: {
3780e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
3781e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getParenType(New);
3782e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3783e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3784e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Pointer: {
3785e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
3786e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getPointerType(New);
3787e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3788e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3789e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case BlockPointer: {
3790e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
3791e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getBlockPointerType(New);
3792e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3793e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3794e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case MemberPointer: {
3795e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
3796e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldMPT->getPointeeType(), I);
3797e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getMemberPointerType(New, OldMPT->getClass());
3798e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3799e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3800e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Reference: {
3801e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const ReferenceType *OldRef = cast<ReferenceType>(Old);
3802e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldRef->getPointeeType(), I);
3803e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<LValueReferenceType>(OldRef))
3804e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
3805e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        else
3806e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getRValueReferenceType(New);
3807e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3808e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3809e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3810e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      llvm_unreachable("unknown wrapping kind");
3811e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3812e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  };
3813e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
3814e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3815711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
3816711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
3817711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
3818711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
3819711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
3820711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
3821e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3822711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
38232455636163fdd18581d7fdae816433f886d88213Mike Stump
38248e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NoReturn) {
3825711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
382604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
3827e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3828e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
3829711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
3830711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
3831425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
3832425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
3833711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
3834711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3835711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3836711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
3837425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
3838f85e193739c953358c865005855253af4f68a497John McCall  // ns_returns_retained is not always a type attribute, but if we got
3839f85e193739c953358c865005855253af4f68a497John McCall  // here, we're treating it as one right now.
38408e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NSReturnsRetained) {
38414e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(S.getLangOpts().ObjCAutoRefCount &&
3842f85e193739c953358c865005855253af4f68a497John McCall           "ns_returns_retained treated as type attribute in non-ARC");
3843f85e193739c953358c865005855253af4f68a497John McCall    if (attr.getNumArgs()) return true;
3844f85e193739c953358c865005855253af4f68a497John McCall
3845f85e193739c953358c865005855253af4f68a497John McCall    // Delay if this is not a function type.
3846f85e193739c953358c865005855253af4f68a497John McCall    if (!unwrapped.isFunctionType())
3847f85e193739c953358c865005855253af4f68a497John McCall      return false;
3848f85e193739c953358c865005855253af4f68a497John McCall
3849f85e193739c953358c865005855253af4f68a497John McCall    FunctionType::ExtInfo EI
3850f85e193739c953358c865005855253af4f68a497John McCall      = unwrapped.get()->getExtInfo().withProducesResult(true);
3851f85e193739c953358c865005855253af4f68a497John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3852f85e193739c953358c865005855253af4f68a497John McCall    return true;
3853f85e193739c953358c865005855253af4f68a497John McCall  }
3854f85e193739c953358c865005855253af4f68a497John McCall
38558e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_Regparm) {
3856711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
3857711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
3858711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
38591e030eb1194763b42c1752723be23b1515f48981John McCall
3860711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
3861711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
3862008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
38631e030eb1194763b42c1752723be23b1515f48981John McCall
3864ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Diagnose regparm with fastcall.
3865ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    const FunctionType *fn = unwrapped.get();
3866ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    CallingConv CC = fn->getCallConv();
3867ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (CC == CC_X86FastCall) {
3868ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
3869ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC)
3870ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm";
3871ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
3872ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
3873ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
3874ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
387591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    FunctionType::ExtInfo EI =
3876711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
3877711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3878711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3879425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
3880425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
388104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
3882711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
3883711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
3884711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3885f82b4e85b1219295cad4b5851b035575bc293010John McCall
388604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Delay if the type didn't work out to a function.
3887711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!unwrapped.isFunctionType()) return false;
388804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
3889711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
3890711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
3891064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis  if (S.Context.getCanonicalCallConv(CC) ==
3892e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      S.Context.getCanonicalCallConv(CCOld)) {
3893ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
3894ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3895711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3896e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  }
389704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
38988e68f1c8a2919ea83c2053731d6011074f1062e1Roman Divacky  if (CCOld != (S.LangOpts.MRTD ? CC_X86StdCall : CC_Default)) {
389904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Should we diagnose reapplications of the same convention?
3900711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
390104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CC)
390204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CCOld);
3903711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3904711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
390504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
390604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
390704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
390804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
3909711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isa<FunctionNoProtoType>(fn)) {
3910711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_knr)
391104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
3912711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
3913711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
391404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
391504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
3916711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
391704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
3918711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
391904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
3920711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
3921711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
392204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
3923ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
3924ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Also diagnose fastcall with regparm.
3925a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    if (fn->getHasRegParm()) {
3926ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
3927ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm"
3928ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC);
3929ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
3930ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
3931ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
393204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
3933f82b4e85b1219295cad4b5851b035575bc293010John McCall
3934711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
3935711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3936711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
3937f82b4e85b1219295cad4b5851b035575bc293010John McCall}
3938f82b4e85b1219295cad4b5851b035575bc293010John McCall
3939207f4d8543529221932af82836016a2ef066c917Peter Collingbourne/// Handle OpenCL image access qualifiers: read_only, write_only, read_write
3940207f4d8543529221932af82836016a2ef066c917Peter Collingbournestatic void HandleOpenCLImageAccessAttribute(QualType& CurType,
3941207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const AttributeList &Attr,
3942207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             Sema &S) {
3943207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  // Check the attribute arguments.
3944207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (Attr.getNumArgs() != 1) {
3945207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3946207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
3947207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
3948207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
3949207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
3950207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  llvm::APSInt arg(32);
3951207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
3952207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      !sizeExpr->isIntegerConstantExpr(arg, S.Context)) {
3953207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3954207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << "opencl_image_access" << sizeExpr->getSourceRange();
3955207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
3956207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
3957207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
3958207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  unsigned iarg = static_cast<unsigned>(arg.getZExtValue());
3959207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  switch (iarg) {
3960207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_only:
3961207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_write_only:
3962207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_write:
3963207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
3964207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
3965207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  default:
3966207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
3967207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
3968207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << sizeExpr->getSourceRange();
3969207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
3970207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
3971207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
3972207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
3973207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
39746e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
39756e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
39766e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
39776e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
39786e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
39796e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
39806e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
3981788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
3982788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
398356affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
39846e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
39856e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3986e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
39876e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
39886e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
39896e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
39906e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
3991ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
3992ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
39936e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
39946e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << "vector_size" << sizeExpr->getSourceRange();
3995e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
39966e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
39976e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
39986e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
3999f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
40006e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
4001e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
40026e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
40036e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
40046e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
40056e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
40066e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
40076e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
40086e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
40096e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
40106e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
40116e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4012e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
40136e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
40146e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
40156e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
40166e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
40176e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4018e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
40196e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
40206e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
40216e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
40226e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
40236e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
4024788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
4025e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
40266e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
40276e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
40284ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// \brief Process the OpenCL-like ext_vector_type attribute when it occurs on
40294ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// a type.
403091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierstatic void HandleExtVectorTypeAttr(QualType &CurType,
403191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                                    const AttributeList &Attr,
40324ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                    Sema &S) {
40334ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  Expr *sizeExpr;
403491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
40354ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Special case where the argument is a template id.
40364ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (Attr.getParameterName()) {
40374ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CXXScopeSpec SS;
4038e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    SourceLocation TemplateKWLoc;
40394ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    UnqualifiedId id;
40404ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
4041e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
4042e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    ExprResult Size = S.ActOnIdExpression(S.getCurScope(), SS, TemplateKWLoc,
4043e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          id, false, false);
40444ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Size.isInvalid())
40454ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
404691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
40474ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Size.get();
40484ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  } else {
40494ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    // check the attribute arguments.
40504ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Attr.getNumArgs() != 1) {
40514ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
40524ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
40534ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    }
40544ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Attr.getArg(0);
40554ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  }
405691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
40574ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Create the vector type.
40584ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  QualType T = S.BuildExtVectorType(CurType, sizeExpr, Attr.getLoc());
40594ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (!T.isNull())
40604ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CurType = T;
40614ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor}
40624ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
40634211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
40644211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
40654211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
40664211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
40674211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
40684211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
40694211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
40704211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
40714211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
40724211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     VectorType::VectorKind VecKind,
40734211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const char *AttrName) {
40744211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
40754211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
40764211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
40774211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
40784211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
40794211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
40804211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
40814211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
40824211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
40834211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
40844211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
40854211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
40864211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      << AttrName << numEltsExpr->getSourceRange();
40874211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
40884211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
40894211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
40904211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
40914211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  const BuiltinType* BTy = CurType->getAs<BuiltinType>();
40924211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (!BTy ||
40934211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (VecKind == VectorType::NeonPolyVector &&
40944211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::SChar &&
40954211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short) ||
40964211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (BTy->getKind() != BuiltinType::SChar &&
40974211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UChar &&
40984211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short &&
40994211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UShort &&
41004211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Int &&
41014211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UInt &&
41024211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::LongLong &&
41034211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::ULongLong &&
41044211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Float)) {
41054211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
41064211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
41074211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
41084211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
41094211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
41104211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
41114211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
41124211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
41134211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
41144211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
41154211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
41164211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
41174211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
41184211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
41194211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
41204211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
41214211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
4122711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
4123711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             bool isDeclSpec, AttributeList *attrs) {
4124c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
4125c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
4126c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
4127c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
4128711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4129711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
4130711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
4131711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
4132711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr.getNext();
4133711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4134e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
4135711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
4136e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
4137e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
4138b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
4139b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
4140711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
4141c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
414204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
41438e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_MayAlias:
4144682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // FIXME: This attribute needs to actually be handled, but if we ignore
4145682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // it it breaks large amounts of Linux software.
4146682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      attr.setUsedAsTypeAttr();
4147682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      break;
41488e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_AddressSpace:
4149711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
4150e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4151c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
4152711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
4153711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
4154711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
4155e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4156d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
41578e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_VectorSize:
4158711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
4159e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4160f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
41618e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ExtVectorType:
41624ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      if (state.getDeclarator().getDeclSpec().getStorageClassSpec()
41634ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor            != DeclSpec::SCS_typedef)
41644ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        HandleExtVectorTypeAttr(type, attr, state.getSema());
4165e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
41664ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      break;
41678e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonVectorType:
4168711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
4169711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonVector, "neon_vector_type");
4170e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
41714211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
41728e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonPolyVectorType:
4173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
4174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonPolyVector,
41754211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                               "neon_polyvector_type");
4176e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
41774211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
41788e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_OpenCLImageAccess:
4179207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      HandleOpenCLImageAccessAttribute(type, attr, state.getSema());
4180e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4181207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      break;
4182207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
41838e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Win64:
41848e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Ptr32:
41858e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Ptr64:
4186c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall      // FIXME: don't ignore these
4187c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall      attr.setUsedAsTypeAttr();
4188c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall      break;
4189c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall
41908e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
41914e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
419291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
4193f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough into the function attrs
4194f85e193739c953358c865005855253af4f68a497John McCall
4195711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
4196e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4197e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
4198711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
4199711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
4200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (isDeclSpec)
4201711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
4202711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4203711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
4204711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
4205711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
42066e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
4207c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
4208711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attrs = next));
4209232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
4210232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
4211e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \brief Ensure that the type of the given expression is complete.
4212e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4213e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// This routine checks whether the expression \p E has a complete type. If the
4214e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// expression refers to an instantiable construct, that instantiation is
4215e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// performed as needed to complete its type. Furthermore
4216e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// Sema::RequireCompleteType is called for the expression's type (or in the
4217e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// case of a reference type, the referred-to type).
4218e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4219e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \param E The expression whose type is required to be complete.
4220d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// \param Diagnoser The object that will emit a diagnostic if the type is
4221d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// incomplete.
4222e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4223e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \returns \c true if the type of \p E is incomplete and diagnosed, \c false
4224e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// otherwise.
4225f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, TypeDiagnoser &Diagnoser){
4226e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  QualType T = E->getType();
4227e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4228e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Fast path the case where the type is already complete.
4229e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (!T->isIncompleteType())
4230e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    return false;
4231e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4232e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Incomplete array types may be completed by the initializer attached to
4233e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // their definitions. For static data members of class templates we need to
4234e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // instantiate the definition to get this initializer and complete the type.
4235e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (T->isIncompleteArrayType()) {
4236e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4237e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
4238e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        if (Var->isStaticDataMember() &&
4239e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth            Var->getInstantiatedFromStaticDataMember()) {
424091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
424136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
424236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          assert(MSInfo && "Missing member specialization information?");
424336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          if (MSInfo->getTemplateSpecializationKind()
424436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                != TSK_ExplicitSpecialization) {
424536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // If we don't already have a point of instantiation, this is it.
424636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (MSInfo->getPointOfInstantiation().isInvalid()) {
424736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              MSInfo->setPointOfInstantiation(E->getLocStart());
424891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
424991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier              // This is a modification of an existing AST node. Notify
425036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              // listeners.
425136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              if (ASTMutationListener *L = getASTMutationListener())
425236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                L->StaticDataMemberInstantiated(Var);
425336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
425491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
425536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            InstantiateStaticDataMemberDefinition(E->getExprLoc(), Var);
425691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
425736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // Update the type to the newly instantiated definition's type both
425836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // here and within the expression.
425936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (VarDecl *Def = Var->getDefinition()) {
426036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setDecl(Def);
426136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              T = Def->getType();
426236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setType(T);
426336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              E->setType(T);
426436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
4265f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor          }
426691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4267e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // We still go on to try to complete the type independently, as it
4268e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // may also require instantiations or diagnostics if it remains
4269e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // incomplete.
4270e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        }
4271e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      }
4272e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    }
4273e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  }
4274e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4275e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // FIXME: Are there other cases which require instantiating something other
4276e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // than the type to complete the type of an expression?
4277e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4278e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Look through reference types and complete the referred type.
4279e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
4280e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    T = Ref->getPointeeType();
4281e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4282d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(E->getExprLoc(), T, Diagnoser);
4283d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4284d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4285d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregornamespace {
4286f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  struct TypeDiagnoserDiag : Sema::TypeDiagnoser {
4287d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    unsigned DiagID;
428891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4289f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor    TypeDiagnoserDiag(unsigned DiagID)
4290f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor      : Sema::TypeDiagnoser(DiagID == 0), DiagID(DiagID) {}
429191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4292d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) {
4293d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      if (Suppressed) return;
4294d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      S.Diag(Loc, DiagID) << T;
4295d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    }
4296d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  };
4297d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4298d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4299d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, unsigned DiagID) {
4300f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4301d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteExprType(E, Diagnoser);
4302e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth}
4303e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
43041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
43054ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
43064ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
43074ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
430886447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
430986447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
431086447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
431186447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
431286447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
43134ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
43144ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
43154ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
43164ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
43174ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
43184ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
43194ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
43204ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
432191a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
4322f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                               TypeDiagnoser &Diagnoser) {
4323573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
4324573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
4325690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
4326690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
4327690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
43281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
4329690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
4330690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
43314ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
4332d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  NamedDecl *Def = 0;
4333d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  if (!T->isIncompleteType(&Def)) {
4334d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    // If we know about the definition but it is not visible, complain.
4335d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (!Diagnoser.Suppressed && Def && !LookupResult::isVisible(Def)) {
4336d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // Suppress this error outside of a SFINAE context if we've already
433791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // emitted the error once for this type. There's no usefulness in
4338d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // repeating the diagnostic.
4339d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // FIXME: Add a Fix-It that imports the corresponding module or includes
4340d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // the header.
4341d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      if (isSFINAEContext() || HiddenDefinitions.insert(Def)) {
4342d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor        Diag(Loc, diag::err_module_private_definition) << T;
4343d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor        Diag(Def->getLocation(), diag::note_previous_definition);
4344d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      }
4345d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    }
434691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
43474ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
4348d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
43494ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
4350bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const TagType *Tag = T->getAs<TagType>();
4351bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const ObjCInterfaceType *IFace = 0;
435291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4353bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  if (Tag) {
4354bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4355bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->isInvalidDecl())
4356bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
4357bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan
4358bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4359bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->hasExternalLexicalStorage()) {
4360bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(Tag->getDecl());
4361bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!Tag->isIncompleteType())
4362bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4363bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4364bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
4365bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  else if ((IFace = T->getAs<ObjCInterfaceType>())) {
4366bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4367bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->isInvalidDecl())
4368bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
436991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4370bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4371bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->hasExternalLexicalStorage()) {
4372bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(IFace->getDecl());
4373bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!IFace->isIncompleteType())
4374bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4375bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4376bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
437791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4378d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
4379923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
4380923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
4381923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
4382e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor  while (const ConstantArrayType *Array
4383e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor           = Context.getAsConstantArrayType(MaybeTemplate))
4384923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
4385923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
43862943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
4387d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
4388972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
4389972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
4390d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
4391d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                            /*Complain=*/!Diagnoser.Suppressed);
43921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
4393d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
4394564f4c5664f552becbd05407611a92754c40e641Richard Smith      CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass();
4395564f4c5664f552becbd05407611a92754c40e641Richard Smith      if (!Rec->isBeingDefined() && Pattern) {
4396564f4c5664f552becbd05407611a92754c40e641Richard Smith        MemberSpecializationInfo *MSI = Rec->getMemberSpecializationInfo();
4397564f4c5664f552becbd05407611a92754c40e641Richard Smith        assert(MSI && "Missing member specialization information?");
4398357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
4399564f4c5664f552becbd05407611a92754c40e641Richard Smith        if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
4400f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
4401f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
4402f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
4403d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                  /*Complain=*/!Diagnoser.Suppressed);
4404d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
4405d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
4406d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
44072943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
4408d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  if (Diagnoser.Suppressed)
44095842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
4410d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
44114ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
4412d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
441391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
44144ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
441501620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
44164ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
44171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
44184ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
44194ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
4420b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor      << QualType(Tag, 0);
442191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4422b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  // If the Objective-C class was a forward declaration, produce a note.
4423b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  if (IFace && !IFace->getDecl()->isInvalidDecl())
4424b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor    Diag(IFace->getDecl()->getLocation(), diag::note_forward_class);
44254ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
44264ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
44274ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
4428e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
4429fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
443091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                               unsigned DiagID) {
4431f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4432d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(Loc, T, Diagnoser);
4433fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
4434fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
44356666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \brief Get diagnostic %select index for tag kind for
44366666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// literal type diagnostic message.
44376666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// WARNING: Indexes apply to particular diagnostics only!
44386666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos///
44396666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \returns diagnostic %select index.
44406666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matosstatic unsigned getLiteralDiagFromTagKind(TagTypeKind Tag)
44416666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos{
44426666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  switch (Tag) {
44436666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos    case TTK_Struct: return 0;
44446666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos    case TTK_Interface: return 1;
44456666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos    case TTK_Class:  return 2;
44466666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos    default: assert("Invalid tag kind for literal type diagnostic!");
44476666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  }
44486666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  return -1;
44496666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos}
44506666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos
44519f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @brief Ensure that the type T is a literal type.
44529f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
44539f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// This routine checks whether the type @p T is a literal type. If @p T is an
44549f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// incomplete type, an attempt is made to complete it. If @p T is a literal
44559f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// type, or @p AllowIncompleteType is true and @p T is an incomplete type,
44569f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// returns false. Otherwise, this routine issues the diagnostic @p PD (giving
44579f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// it the type @p T), along with notes explaining why the type is not a
44589f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// literal type, and returns true.
44599f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
44609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param Loc  The location in the source that the non-literal type
44619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// diagnostic should refer to.
44629f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
44639f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param T  The type that this routine is examining for literalness.
44649f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
4465f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor/// @param Diagnoser Emits a diagnostic if T is not a literal type.
44669f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
44679f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @returns @c true if @p T is not a literal type and a diagnostic was emitted,
44689f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @c false otherwise.
44699f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithbool Sema::RequireLiteralType(SourceLocation Loc, QualType T,
4470f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                              TypeDiagnoser &Diagnoser) {
44719f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  assert(!T->isDependentType() && "type should not be dependent");
44729f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4473ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  QualType ElemType = Context.getBaseElementType(T);
4474ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  RequireCompleteType(Loc, ElemType, 0);
4475ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
447686c3ae46250cdcc57778c27826060779a92f3815Richard Smith  if (T->isLiteralType())
44779f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return false;
44789f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4479f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  if (Diagnoser.Suppressed)
44809f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
44819f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4482f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
44839f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
44849f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (T->isVariableArrayType())
44859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
44869f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4487ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  const RecordType *RT = ElemType->getAs<RecordType>();
44889f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (!RT)
44899f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
44909f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
44919f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
44929f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4493c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // A partially-defined class type can't be a literal type, because a literal
4494c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // class type must have a trivial destructor (which can't be checked until
4495c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // the class definition is complete).
4496c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  if (!RD->isCompleteDefinition()) {
4497d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T);
4498ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman    return true;
4499c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  }
4500ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
45019f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // If the class has virtual base classes, then it's not an aggregate, and
450286c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // cannot have any constexpr constructors or a trivial default constructor,
450386c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // so is non-literal. This is better to diagnose than the resulting absence
450486c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // of constexpr constructors.
45059f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (RD->getNumVBases()) {
45069f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_virtual_base)
45076666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      << getLiteralDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases();
45089f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
45099f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           E = RD->vbases_end(); I != E; ++I)
451096a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      Diag(I->getLocStart(),
45119f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           diag::note_constexpr_virtual_base_here) << I->getSourceRange();
451286c3ae46250cdcc57778c27826060779a92f3815Richard Smith  } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor() &&
451386c3ae46250cdcc57778c27826060779a92f3815Richard Smith             !RD->hasTrivialDefaultConstructor()) {
45149f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD;
45159f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (RD->hasNonLiteralTypeFieldsOrBases()) {
45169f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
45179f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->bases_end(); I != E; ++I) {
45189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      if (!I->getType()->isLiteralType()) {
451996a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        Diag(I->getLocStart(),
45209f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith             diag::note_non_literal_base_class)
45219f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith          << RD << I->getType() << I->getSourceRange();
45229f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
45239f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
45249f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
45259f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::field_iterator I = RD->field_begin(),
45269f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->field_end(); I != E; ++I) {
4527262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      if (!I->getType()->isLiteralType() ||
4528262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          I->getType().isVolatileQualified()) {
4529262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        Diag(I->getLocation(), diag::note_non_literal_field)
4530581deb3da481053c4993c7600f97acf7768caac5David Blaikie          << RD << *I << I->getType()
4531262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          << I->getType().isVolatileQualified();
45329f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
45339f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
45349f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
45359f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (!RD->hasTrivialDestructor()) {
45369f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // All fields and bases are of literal types, so have trivial destructors.
45379f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // If this class's destructor is non-trivial it must be user-declared.
45389f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    CXXDestructorDecl *Dtor = RD->getDestructor();
45399f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    assert(Dtor && "class has literal fields and bases but no dtor?");
45409f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    if (!Dtor)
45419f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      return true;
45429f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
45439f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(Dtor->getLocation(), Dtor->isUserProvided() ?
45449f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_user_provided_dtor :
45459f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_nontrivial_dtor) << RD;
45469f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  }
45479f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
45489f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return true;
45499f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
45509f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
455191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierbool Sema::RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID) {
4552f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4553f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  return RequireLiteralType(Loc, T, Diagnoser);
4554f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor}
4555f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor
4556465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
4557465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
4558465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
4559465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
4560465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
4561e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
4562465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
4563e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
4564465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
4565465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
4566465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
4567465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
4568465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
4569465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
4570465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
4571e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
4572af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
45732a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
4574fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
45752a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
45762a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
45772a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
45782b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
45792b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
4580aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
4581aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
45822b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
4583af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
4584af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
4585af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
4586f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// getDecltypeForExpr - Given an expr, will return the decltype for
4587f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// that expression, according to the rules in C++11
4588f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18.
4589f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregorstatic QualType getDecltypeForExpr(Sema &S, Expr *E) {
4590f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (E->isTypeDependent())
4591f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    return S.Context.DependentTy;
4592f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
45936d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
45946d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   The type denoted by decltype(e) is defined as follows:
45956d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //
45966d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //     - if e is an unparenthesized id-expression or an unparenthesized class
459791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       member access (5.2.5), decltype(e) is the type of the entity named
459891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       by e. If there is no such entity, or if e names a set of overloaded
45996d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       functions, the program is ill-formed;
4600f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
4601f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
4602f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return VD->getType();
4603f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
4604f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
4605f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
4606f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return FD->getType();
4607f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
460891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4609f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  // C++11 [expr.lambda.prim]p18:
4610f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   Every occurrence of decltype((x)) where x is a possibly
4611f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   parenthesized id-expression that names an entity of automatic
4612f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   storage duration is treated as if x were transformed into an
4613f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   access to a corresponding data member of the closure type that
4614f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   would have been declared if x were an odr-use of the denoted
4615f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   entity.
4616f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  using namespace sema;
4617f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (S.getCurLambda()) {
4618f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (isa<ParenExpr>(E)) {
4619f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4620f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
462168932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          QualType T = S.getCapturedDeclRefType(Var, DRE->getLocation());
462268932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          if (!T.isNull())
462368932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor            return S.Context.getLValueReferenceType(T);
4624f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        }
4625f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      }
4626f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    }
4627f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
4628f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
4629f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
46306d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
46316d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   [...]
46326d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  QualType T = E->getType();
46336d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  switch (E->getValueKind()) {
463491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the
46356d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
46366d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_XValue: T = S.Context.getRValueReferenceType(T); break;
463791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an lvalue, decltype(e) is T&, where T is the
46386d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
46396d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_LValue: T = S.Context.getLValueReferenceType(T); break;
46406d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //  - otherwise, decltype(e) is the type of e.
46416d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_RValue: break;
46426d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  }
464391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4644f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return T;
4645f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor}
4646f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
46472a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
4648fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
46492a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
46502a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
465191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4652f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return Context.getDecltypeType(E, getDecltypeForExpr(*this, E));
4653af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
4654ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
4655ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType Sema::BuildUnaryTransformType(QualType BaseType,
4656ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::UTTKind UKind,
4657ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       SourceLocation Loc) {
4658ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  switch (UKind) {
4659ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case UnaryTransformType::EnumUnderlyingType:
4660ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) {
4661ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Diag(Loc, diag::err_only_enums_have_underlying_types);
4662ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return QualType();
4663ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    } else {
4664ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      QualType Underlying = BaseType;
4665ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      if (!BaseType->isDependentType()) {
4666ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl();
4667ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        assert(ED && "EnumType has no EnumDecl");
4668ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        DiagnoseUseOfDecl(ED, Loc);
4669ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        Underlying = ED->getIntegerType();
4670ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      }
4671ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(!Underlying.isNull());
4672ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return Context.getUnaryTransformType(BaseType, Underlying,
4673ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                        UnaryTransformType::EnumUnderlyingType);
4674ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
4675ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  }
4676ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  llvm_unreachable("unknown unary transform type");
4677ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
4678b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4679b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) {
4680b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T->isDependentType()) {
46818327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // FIXME: It isn't entirely clear whether incomplete atomic types
46828327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // are allowed or not; for simplicity, ban them for the moment.
4683d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (RequireCompleteType(Loc, T, diag::err_atomic_specifier_bad_type, 0))
46848327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith      return QualType();
46858327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith
4686b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    int DisallowedKind = -1;
46878327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    if (T->isArrayType())
4688b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 1;
4689b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isFunctionType())
4690b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 2;
4691b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isReferenceType())
4692b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 3;
4693b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isAtomicType())
4694b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 4;
4695b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T.hasQualifiers())
4696b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 5;
4697b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (!T.isTriviallyCopyableType(Context))
4698b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      // Some other non-trivially-copyable type (probably a C++ class)
4699b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 6;
4700b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4701b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (DisallowedKind != -1) {
4702b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T;
4703b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return QualType();
4704b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
4705b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4706b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // FIXME: Do we need any handling for ARC here?
4707b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
4708b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4709b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Build the pointer type.
4710b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return Context.getAtomicType(T);
4711b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
4712