SemaType.cpp revision 5c52166525f8714c3e3a979b156ec23426947fd3
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements type-related semantic analysis.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
142d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
1636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor#include "clang/AST/ASTMutationListener.h"
17a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor#include "clang/AST/CXXInheritance.h"
18980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
192943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor#include "clang/AST/DeclTemplate.h"
2055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/Expr.h"
214adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
2251bd803fbdade51d674598ed45da3d54190a656cJohn McCall#include "clang/AST/TypeLocVisitor.h"
2355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/OpenCL.h"
2491a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
25d18f9f965bcfe56edcdf9b0d8375ffaad9866b3fCharles Davis#include "clang/Basic/TargetInfo.h"
262792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall#include "clang/Lex/Preprocessor.h"
27bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman#include "clang/Parse/ParseDiagnostic.h"
2819510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
29f85e193739c953358c865005855253af4f68a497John McCall#include "clang/Sema/DelayedDiagnostic.h"
30d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor#include "clang/Sema/Lookup.h"
3155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/ScopeInfo.h"
3255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Template.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: \
108263366f9241366f29ba65b703120f302490c39ffDerek Schuff    case AttributeList::AT_Pcs: \
10938980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    case AttributeList::AT_PnaclCall: \
11038980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    case AttributeList::AT_IntelOclBicc \
111711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
112711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace {
113711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// An object which stores processing state for the entire
114711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// GetTypeForDeclarator process.
115711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  class TypeProcessingState {
116711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &sema;
117711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
118711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The declarator being processed.
119711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &declarator;
120711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
121711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The index of the declarator chunk we're currently processing.
122711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// May be the total number of valid chunks, indicating the
123711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// DeclSpec.
124711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex;
125711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
126711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Whether there are non-trivial modifications to the decl spec.
127711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    bool trivial;
128711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1297ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    /// Whether we saved the attributes in the decl spec.
1307ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    bool hasSavedAttrs;
1317ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
132711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The original set of attributes on the DeclSpec.
1335f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> savedAttrs;
134711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
135711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// A list of attributes to diagnose the uselessness of when the
136711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// processing is complete.
1375f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<AttributeList*, 2> ignoredTypeAttrs;
138711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
139711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  public:
140711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    TypeProcessingState(Sema &sema, Declarator &declarator)
141711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : sema(sema), declarator(declarator),
142711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        chunkIndex(declarator.getNumTypeObjects()),
1437ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        trivial(true), hasSavedAttrs(false) {}
144711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
145711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Sema &getSema() const {
146711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return sema;
147711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
148711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
149711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Declarator &getDeclarator() const {
150711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator;
151711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
152711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
153711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned getCurrentChunkIndex() const {
154711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return chunkIndex;
155711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
156711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
157711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void setCurrentChunkIndex(unsigned idx) {
158711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(idx <= declarator.getNumTypeObjects());
159711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      chunkIndex = idx;
160711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
161711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
162711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&getCurrentAttrListRef() const {
163711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      assert(chunkIndex <= declarator.getNumTypeObjects());
164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (chunkIndex == declarator.getNumTypeObjects())
165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        return getMutableDeclSpec().getAttributes().getListRef();
166711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return declarator.getTypeObject(chunkIndex).getAttrListRef();
167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
168711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
169711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Save the current set of attributes on the DeclSpec.
170711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void saveDeclSpecAttrs() {
171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Don't try to save them multiple times.
1727ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (hasSavedAttrs) return;
173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      DeclSpec &spec = getMutableDeclSpec();
175711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (AttributeList *attr = spec.getAttributes().getList(); attr;
176711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             attr = attr->getNext())
177711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs.push_back(attr);
178711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      trivial &= savedAttrs.empty();
1797ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      hasSavedAttrs = true;
180711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
181711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
182711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Record that we had nowhere to put the given type attribute.
183711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// We will diagnose such attributes later.
184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void addIgnoredTypeAttr(AttributeList &attr) {
185711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      ignoredTypeAttrs.push_back(&attr);
186711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
187711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
188711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// Diagnose all the ignored type attributes, given that the
189711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// declarator worked out to the given type.
190711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void diagnoseIgnoredTypeAttrs(QualType type) const {
1915f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      for (SmallVectorImpl<AttributeList*>::const_iterator
192711c52bb20d0c69063b52a99826fb7d2835501f1John McCall             i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
1932792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall           i != e; ++i)
1942792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall        diagnoseBadTypeAttribute(getSema(), **i, type);
195711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
196711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
197711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    ~TypeProcessingState() {
198711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (trivial) return;
199711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
200711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      restoreDeclSpecAttrs();
201711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
202711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
203711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  private:
204711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclSpec &getMutableDeclSpec() const {
205711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return const_cast<DeclSpec&>(declarator.getDeclSpec());
206711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
207711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
208711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    void restoreDeclSpecAttrs() {
2097ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      assert(hasSavedAttrs);
2107ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
2117ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      if (savedAttrs.empty()) {
2127ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        getMutableDeclSpec().getAttributes().set(0);
2137ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall        return;
2147ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      }
2157ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall
216711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
217711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
218711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        savedAttrs[i]->setNext(savedAttrs[i+1]);
219711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      savedAttrs.back()->setNext(0);
220711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
221711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
222711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
223711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// Basically std::pair except that we really want to avoid an
224711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// implicit operator= for safety concerns.  It's also a minor
225711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  /// link-time optimization for this to be a private type.
226711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  struct AttrAndList {
227711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The attribute.
228711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &first;
229711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
230711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    /// The head of the list the attribute is currently in.
231711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList *&second;
232711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
233711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttrAndList(AttributeList &attr, AttributeList *&head)
234711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      : first(attr), second(head) {}
235711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
23604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
23704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
238711c52bb20d0c69063b52a99826fb7d2835501f1John McCallnamespace llvm {
239711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  template <> struct isPodLike<AttrAndList> {
240711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    static const bool value = true;
241711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  };
242711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
243711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
244711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
245711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  attr.setNext(head);
246711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  head = &attr;
247711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
248711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
249711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
250711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (head == &attr) {
251711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    head = attr.getNext();
252711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
25304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
254711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
255711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *cur = head;
256711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  while (true) {
257711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    assert(cur && cur->getNext() && "ran out of attrs?");
258711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (cur->getNext() == &attr) {
259711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      cur->setNext(attr.getNext());
260711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
261711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
262711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    cur = cur->getNext();
263711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
264711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
265711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
266711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void moveAttrFromListToList(AttributeList &attr,
267711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&fromList,
268711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList *&toList) {
269711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, fromList);
270711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrIntoList(attr, toList);
271711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
272711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
273f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith/// The location of a type attribute.
274f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smithenum TypeAttrLocation {
275f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is in the decl-specifier-seq.
276f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclSpec,
277f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is part of a DeclaratorChunk.
278f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclChunk,
279f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  /// The attribute is immediately after the declaration's name.
280f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith  TAL_DeclName
281f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith};
282f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
283711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state,
284f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             QualType &type, TypeAttrLocation TAL,
285711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             AttributeList *attrs);
286711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
287711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
288711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
289711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type);
290711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
291711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
292711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr, QualType &type);
293711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
294b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
295f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr, QualType &type);
296f85e193739c953358c865005855253af4f68a497John McCall
297711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCPointerTypeAttr(TypeProcessingState &state,
298711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr, QualType &type) {
2998e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_ObjCGC)
300f85e193739c953358c865005855253af4f68a497John McCall    return handleObjCGCTypeAttr(state, attr, type);
3018e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  assert(attr.getKind() == AttributeList::AT_ObjCOwnership);
302b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  return handleObjCOwnershipTypeAttr(state, attr, type);
303711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
304711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
305711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Given that an objc_gc attribute was written somewhere on a
306711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declaration *other* than on the declarator itself (for which, use
307711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
308711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// didn't apply in whatever position it was written in, try to move
309711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// it to a more appropriate position.
310711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeObjCPointerTypeAttr(TypeProcessingState &state,
311711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          AttributeList &attr,
312711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType type) {
313711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
314711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
315711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
316711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
317711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
318711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
319711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
320711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
321711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
322711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
323711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
324711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
325711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
326711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
327711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Don't walk through these.
328711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
329711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
330711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
331711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto error;
332711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
333711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
334711c52bb20d0c69063b52a99826fb7d2835501f1John McCall error:
3352792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall
3362792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
337711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
338711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
339711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Distribute an objc_gc type attribute that was written on the
340711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator.
341711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
342711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
343711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            AttributeList &attr,
344711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            QualType &declSpecType) {
345711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
346711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
347711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // objc_gc goes on the innermost pointer to something that's not a
348711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // pointer.
349711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  unsigned innermost = -1U;
350711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  bool considerDeclSpec = true;
351711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
352711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
353711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
354711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
355711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
356711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      innermost = i;
357ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      continue;
358711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
359711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
360711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
361711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
362711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
363711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
364711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
365711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
366711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      considerDeclSpec = false;
367711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      goto done;
368711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
369711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
370711c52bb20d0c69063b52a99826fb7d2835501f1John McCall done:
371711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
372711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // That might actually be the decl spec if we weren't blocked by
373711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // anything in the declarator.
374711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (considerDeclSpec) {
3757ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    if (handleObjCPointerTypeAttr(state, attr, declSpecType)) {
3767ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // Splice the attribute into the decl spec.  Prevents the
3777ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // attribute from being applied multiple times and gives
3787ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      // the source-location-filler something to work with.
3797ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      state.saveDeclSpecAttrs();
3807ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall      moveAttrFromListToList(attr, declarator.getAttrListRef(),
3817ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall               declarator.getMutableDeclSpec().getAttributes().getListRef());
382711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
3837ea21937de6f849a7f44f10549c3d69c5a8cb3f3John McCall    }
384711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
385711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
386711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, if we found an appropriate chunk, splice the attribute
387711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // into it.
388711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (innermost != -1U) {
389711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, declarator.getAttrListRef(),
390711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                       declarator.getTypeObject(innermost).getAttrListRef());
391711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
392711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
393711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
394711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, diagnose when we're done building the type.
395711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
396711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
397711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
398711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
399711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written somewhere in a declaration
400711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// *other* than on the declarator itself or in the decl spec.  Given
401711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// that it didn't apply in whatever position it was written in, try
402711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// to move it to a more appropriate position.
403711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeFunctionTypeAttr(TypeProcessingState &state,
404711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
405711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType type) {
406711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
407711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
408711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to push the attribute from the return type of a function to
409711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the function itself.
410711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
411711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
412711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (chunk.Kind) {
413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Function:
414711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
415711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                             chunk.getAttrListRef());
416711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return;
417711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
418711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Paren:
419711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Pointer:
420711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::BlockPointer:
421711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Array:
422711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::Reference:
423711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    case DeclaratorChunk::MemberPointer:
424711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      continue;
425711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
426711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
42791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4282792fa5115c5de7cbe11d99d23663c569bfb4caeJohn McCall  diagnoseBadTypeAttribute(state.getSema(), attr, type);
429711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
430711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
431711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Try to distribute a function type attribute to the innermost
432711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// function chunk or type.  Returns true if the attribute was
433711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// distributed, false if no location was found.
434711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool
435711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
436711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList &attr,
437711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      AttributeList *&attrList,
438711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      QualType &declSpecType) {
439711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
440711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
441711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Put it on the innermost function chunk, if there is one.
442711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
443711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(i);
444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (chunk.Kind != DeclaratorChunk::Function) continue;
445711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
446711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
447711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
448711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
449711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
450f85e193739c953358c865005855253af4f68a497John McCall  if (handleFunctionTypeAttr(state, attr, declSpecType)) {
451f85e193739c953358c865005855253af4f68a497John McCall    spliceAttrOutOfList(attr, attrList);
452f85e193739c953358c865005855253af4f68a497John McCall    return true;
453f85e193739c953358c865005855253af4f68a497John McCall  }
454f85e193739c953358c865005855253af4f68a497John McCall
455f85e193739c953358c865005855253af4f68a497John McCall  return false;
456711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
457711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
458711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written in the decl spec.  Try to
459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
460711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
461711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
462711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       AttributeList &attr,
463711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       QualType &declSpecType) {
464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.saveDeclSpecAttrs();
465711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4665c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // C++11 attributes before the decl specifiers actually appertain to
4675c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // the declarators. Move them straight there. We don't support the
4685c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  // 'put them wherever you like' semantics we allow for GNU attributes.
4695c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  if (attr.isCXX11Attribute()) {
4705c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
4715c52166525f8714c3e3a979b156ec23426947fd3Richard Smith                           state.getDeclarator().getAttrListRef());
4725c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    return;
4735c52166525f8714c3e3a979b156ec23426947fd3Richard Smith  }
4745c52166525f8714c3e3a979b156ec23426947fd3Richard Smith
475711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
476711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
477711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            state.getCurrentAttrListRef(),
478711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
479711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
480711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
482711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
484711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
485711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
486711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// A function type attribute was written on the declarator.  Try to
487711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// apply it somewhere.
488711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void
489711c52bb20d0c69063b52a99826fb7d2835501f1John McCalldistributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
490711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         AttributeList &attr,
491711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                         QualType &declSpecType) {
492711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
493711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
494711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Try to distribute to the innermost.
495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (distributeFunctionTypeAttrToInnermost(state, attr,
496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declarator.getAttrListRef(),
497711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                            declSpecType))
498711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
499711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If that failed, diagnose the bad attribute when the declarator is
501711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // fully built.
502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  spliceAttrOutOfList(attr, declarator.getAttrListRef());
503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.addIgnoredTypeAttr(attr);
504711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
505711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
506711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// \brief Given that there are attributes written on the declarator
507711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// itself, try to distribute any type attributes to the appropriate
508711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// declarator chunk.
509711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///
510711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// These are attributes like the following:
511711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f ATTR;
512711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int (f ATTR)();
513711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// but not necessarily this:
514711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///   int f() ATTR;
515711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
516711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                              QualType &declSpecType) {
517711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Collect all the type attributes from the declarator itself.
518711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
519711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *attr = state.getDeclarator().getAttributes();
520711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
521711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
522711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr->getNext();
523711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5245c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    // Do not distribute C++11 attributes. They have strict rules for what
5255c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    // they appertain to.
5265c52166525f8714c3e3a979b156ec23426947fd3Richard Smith    if (attr->isCXX11Attribute())
5275c52166525f8714c3e3a979b156ec23426947fd3Richard Smith      continue;
5285c52166525f8714c3e3a979b156ec23426947fd3Richard Smith
529711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr->getKind()) {
530711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
531711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
532711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
533711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
5348e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
5354e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
536f85e193739c953358c865005855253af4f68a497John McCall        break;
537f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough
538f85e193739c953358c865005855253af4f68a497John McCall
539711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
540711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
541711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
542711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
543711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    default:
544711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      break;
545711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
546711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attr = next));
547711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
548711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
549711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Add a synthetic '()' to a block-literal declarator if it is
550711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// required, given the return type.
551711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void maybeSynthesizeBlockSignature(TypeProcessingState &state,
552711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                          QualType declSpecType) {
553711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
554711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
555711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // First, check whether the declarator would produce a function,
556711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // i.e. whether the innermost semantic chunk is a function.
557711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.isFunctionDeclarator()) {
558711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // If so, make that declarator a prototyped declarator.
559711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.getFunctionTypeInfo().hasPrototype = true;
560711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
561711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
562711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
563da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // If there are any type objects, the type as written won't name a
564da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // function, regardless of the decl spec type.  This is because a
565da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // block signature declarator is always an abstract-declarator, and
566da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // abstract-declarators can't just be parentheses chunks.  Therefore
567da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // we need to build a function chunk unless there are no type
568da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // objects and the decl spec type is a function.
569711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
570711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
571711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
572da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // Note that there *are* cases with invalid declarators where
573da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // declarators consist solely of parentheses.  In general, these
574da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // occur only in failed efforts to make function declarators, so
575da263795abd39437d73d23fcf34dcd3afc1d7df3John McCall  // faking up the function chunk is still the right thing to do.
576711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
577711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Otherwise, we need to fake up a function declarator.
57896a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar  SourceLocation loc = declarator.getLocStart();
579711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
580711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // ...and *prepend* it to the declarator.
58159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara  SourceLocation NoLoc;
582711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
58359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*HasProto=*/true,
58459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*IsAmbiguous=*/false,
58559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*LParenLoc=*/NoLoc,
58659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ArgInfo=*/0,
58759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NumArgs=*/0,
58859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*EllipsisLoc=*/NoLoc,
58959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RParenLoc=*/NoLoc,
59059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*TypeQuals=*/0,
59159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RefQualifierIsLvalueRef=*/true,
59259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*RefQualifierLoc=*/NoLoc,
59359c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ConstQualifierLoc=*/NoLoc,
59459c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*VolatileQualifierLoc=*/NoLoc,
59559c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*MutableLoc=*/NoLoc,
59659c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             EST_None,
59759c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ESpecLoc=*/NoLoc,
59859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*Exceptions=*/0,
59959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*ExceptionRanges=*/0,
60059c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NumExceptions=*/0,
60159c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             /*NoexceptExpr=*/0,
60259c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara                             loc, loc, declarator));
603711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
604711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // For consistency, make sure the state still has us as processing
605711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // the decl spec.
606711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
607711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  state.setCurrentChunkIndex(declarator.getNumTypeObjects());
60804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
60904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
610930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// \brief Convert the specified declspec to the appropriate type
611930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor/// object.
6121dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// \param state Specifies the declarator containing the declaration specifier
6131dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// to be converted, along with other associated processing state.
6145153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// \returns The type described by the declaration specifiers.  This function
6155153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner/// never returns null.
6168cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType ConvertDeclSpecToType(TypeProcessingState &state) {
6175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
6185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // checking.
619711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
6208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
621711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Declarator &declarator = state.getDeclarator();
622711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const DeclSpec &DS = declarator.getDeclSpec();
623711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  SourceLocation DeclLoc = declarator.getIdentifierLoc();
6245db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  if (DeclLoc.isInvalid())
62596a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar    DeclLoc = DS.getLocStart();
62691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
627711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  ASTContext &Context = S.Context;
6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6295db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner  QualType Result;
6305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (DS.getTypeSpecType()) {
63196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  case DeclSpec::TST_void:
63296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    Result = Context.VoidTy;
63396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    break;
6345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_char:
6355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
636fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.CharTy;
6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
638fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.SignedCharTy;
6395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else {
6405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
6415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             "Unknown TSS value");
642fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.UnsignedCharTy;
6435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
644958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
64564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  case DeclSpec::TST_wchar:
64664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
64764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.WCharTy;
64864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
649711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
650f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
65164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getSignedWCharType();
65264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    } else {
65364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
65464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis        "Unknown TSS value");
655711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
656f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner        << DS.getSpecifierName(DS.getTypeSpecType());
65764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Result = Context.getUnsignedWCharType();
65864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    }
65964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    break;
660f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char16:
661f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
662f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
663f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char16Ty;
664f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
665f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case DeclSpec::TST_char32:
666f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
667f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith        "Unknown TSS value");
668f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Result = Context.Char32Ty;
669f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    break;
670d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner  case DeclSpec::TST_unspecified:
67162f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    // "<proto1,proto2>" is an objc qualified ID with a missing id.
672097e916b617bb4a069a03764024c310ed42a6424Chris Lattner    if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
673c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
67431ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                         (ObjCProtocolDecl*const*)PQ,
675c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         DS.getNumProtocolQualifiers());
676c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = Context.getObjCObjectPointerType(Result);
67762f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner      break;
67862f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner    }
67991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
6805db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // If this is a missing declspec in a block literal return context, then it
6815db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    // is inferred from the return statements inside the block.
682f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // The declspec is always missing in a lambda expr context; it is either
683f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    // specified with a trailing return type or inferred.
684f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    if (declarator.getContext() == Declarator::LambdaExprContext ||
685f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman        isOmittedBlockReturnType(declarator)) {
6865db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      Result = Context.DependentTy;
6875db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner      break;
6885db2bb1cb0c040dcbca1b5000f091d6d225b4bfeChris Lattner    }
6891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
690d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
691d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
692d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
693d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // Note that the one exception to this is function definitions, which are
694d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // allowed to be completely missing a declspec.  This is handled in the
695d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // parser already though by it pretending to have seen an 'int' in this
696d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    // case.
6974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().ImplicitInt) {
69835d276f443462249b436951c1c663820569e1768Chris Lattner      // In C89 mode, we only warn if there is a completely missing declspec
69935d276f443462249b436951c1c663820569e1768Chris Lattner      // when one is not allowed.
7003f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      if (DS.isEmpty()) {
701711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_declspec)
7023f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange()
70396a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        << FixItHint::CreateInsertion(DS.getLocStart(), "int");
7043f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
7054310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor    } else if (!DS.hasTypeSpecifier()) {
706d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
707d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // "At least one type specifier shall be given in the declaration
708d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // specifiers in each declaration, and in the specifier-qualifier list in
709d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner      // each struct declaration and type name."
7104310f4ee260e6c7ceeaf299e240f4d789ecc730dDouglas Gregor      // FIXME: Does Microsoft really have the implicit int extension in C++?
7114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (S.getLangOpts().CPlusPlus &&
7124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          !S.getLangOpts().MicrosoftExt) {
713711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_missing_type_specifier)
7143f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
7151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
716b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // When this occurs in C++ code, often something is very broken with the
717b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // value being declared, poison it as invalid so we don't get chains of
718b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner        // errors.
719711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
720b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      } else {
721711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::ext_missing_type_specifier)
7223f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
723b78d833b12f7c4baab138f305f72efd49455a3f9Chris Lattner      }
724d658b562e80d6ef7a1118e34ff12802c6e2fccedChris Lattner    }
7251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FALL THROUGH.
7273cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  case DeclSpec::TST_int: {
7285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
730fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
731fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
732fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
733311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
734311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.LongLongTy;
73591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
736e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
737e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
738e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
739e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
74080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
741e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
742e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
743e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
744e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
745311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
7485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (DS.getTypeSpecWidth()) {
749fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
750fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
751fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
752311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner      case DeclSpec::TSW_longlong:
753311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        Result = Context.UnsignedLongLongTy;
75491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
755e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        // 'long long' is a C99 or C++11 feature.
756e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        if (!S.getLangOpts().C99) {
757e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          if (S.getLangOpts().CPlusPlus)
758e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(),
75980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                   S.getLangOpts().CPlusPlus11 ?
760e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko                   diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
761e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko          else
762e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko            S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
763e3b136bd873508c9ac0ee6eba98c2a810a177ebaDmitri Gribenko        }
764311157fa6be96e2769bf317390dc9fb85087d5faChris Lattner        break;
7655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
7665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
767958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
7683cbc38bd3569d37f53bd76fa89d24803f48f5036Chris Lattner  }
7695a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith  case DeclSpec::TST_int128:
77084268904947ada7e251932a6f5b0f4364df7a2c7Richard Smith    if (!S.PP.getTargetInfo().hasInt128Type())
77184268904947ada7e251932a6f5b0f4364df7a2c7Richard Smith      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_int128_unsupported);
7725a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    if (DS.getTypeSpecSign() == DeclSpec::TSS_unsigned)
7735a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.UnsignedInt128Ty;
7745a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    else
7755a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith      Result = Context.Int128Ty;
7765a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith    break;
777aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case DeclSpec::TST_half: Result = Context.HalfTy; break;
778fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_float: Result = Context.FloatTy; break;
779958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_double:
780958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
781fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.LongDoubleTy;
782958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    else
783fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner      Result = Context.DoubleTy;
78439d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne
7854e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
78639d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
78739d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne      declarator.setInvalidType(true);
78839d3e7a26c1969fcb76bceb4ee0a410c60ea5954Peter Collingbourne    }
789958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
790fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
7915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal32:    // _Decimal32
7925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal64:    // _Decimal64
7935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_decimal128:   // _Decimal128
794711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
7958f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    Result = Context.IntTy;
796711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
7978f12f65fad7bfbbdbd4234efe0d484f68c3924b6Chris Lattner    break;
79899dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  case DeclSpec::TST_class:
7995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_enum:
8005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case DeclSpec::TST_union:
8016666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_struct:
8026666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case DeclSpec::TST_interface: {
803b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
8046e24726524c2b51b31bb4b622aa678a46b024f42John McCall    if (!D) {
8056e24726524c2b51b31bb4b622aa678a46b024f42John McCall      // This can happen in C++ with ambiguous lookups.
8066e24726524c2b51b31bb4b622aa678a46b024f42John McCall      Result = Context.IntTy;
807711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
8086e24726524c2b51b31bb4b622aa678a46b024f42John McCall      break;
8096e24726524c2b51b31bb4b622aa678a46b024f42John McCall    }
8106e24726524c2b51b31bb4b622aa678a46b024f42John McCall
811a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    // If the type is deprecated or unavailable, diagnose it.
8120daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc());
81391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
8145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
815a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner           DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
81691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
8175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
818a64ef0ab5cb6ac9cfb7d40661a9152c4aa488386Chris Lattner    Result = Context.getTypeDeclType(D);
8192191b20bfb31fc0e22a158f6b4204cd0b7dbd0fdJohn McCall
8200daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    // In both C and C++, make an ElaboratedType.
8210daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    ElaboratedTypeKeyword Keyword
8220daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
8230daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
824958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
8251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
8261a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor  case DeclSpec::TST_typename: {
8275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
8285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           DS.getTypeSpecSign() == 0 &&
8295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "Can't handle qualifiers on typedef names yet!");
830711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
83127940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    if (Result.isNull())
832711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
83327940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall    else if (DeclSpec::ProtocolQualifierListTy PQ
83427940d2fb346325d6001a7661e4ada099cd8e59cJohn McCall               = DS.getProtocolQualifiers()) {
835c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
836c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // Silently drop any existing protocol qualifiers.
837c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        // TODO: determine whether that's the right thing to do.
838c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjT->getNumProtocols())
839c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = ObjT->getBaseType();
840c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
841c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (DS.getNumProtocolQualifiers())
842c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          Result = Context.getObjCObjectType(Result,
84331ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                             (ObjCProtocolDecl*const*) PQ,
844c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                             DS.getNumProtocolQualifiers());
845c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCIdType()) {
846ae4da6150bb837311a2f0f958b01a2989066ba90Chris Lattner        // id<protocol-list>
847c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
84831ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                           (ObjCProtocolDecl*const*) PQ,
849c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
850c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
851c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else if (Result->isObjCClassType()) {
8524262a07621043c19292f5fd90b1e426d65cd366cSteve Naroff        // Class<protocol-list>
853c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
85431ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                           (ObjCProtocolDecl*const*) PQ,
855c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                           DS.getNumProtocolQualifiers());
856c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = Context.getObjCObjectPointerType(Result);
8573f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      } else {
858711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
8593f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner          << DS.getSourceRange();
860711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        declarator.setInvalidType(true);
8613f84ad22acc25353a47ee88f55ab05dffef5d9a9Chris Lattner      }
862c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    }
8631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // TypeQuals handled by caller.
865958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
867958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  case DeclSpec::TST_typeofType:
868e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    // FIXME: Preserve type source info.
869711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.GetTypeFromParser(DS.getRepAsType());
870958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    assert(!Result.isNull() && "Didn't get a type for typeof?");
871730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian    if (!Result->isDependentType())
872730e175910936eae49e65caea8b2ba81c67edff7Fariborz Jahanian      if (const TagType *TT = Result->getAs<TagType>())
873711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
874d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
875fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getTypeOfType(Result);
876958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
877d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  case DeclSpec::TST_typeofExpr: {
878b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
879d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    assert(E && "Didn't get an expression for typeof?");
880d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff    // TypeQuals handled by caller.
881711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
8824b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (Result.isNull()) {
8834b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      Result = Context.IntTy;
884711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
8854b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    }
886958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner    break;
887d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  }
8886fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  case DeclSpec::TST_decltype: {
889b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    Expr *E = DS.getRepAsExpr();
8906fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    assert(E && "Didn't get an expression for decltype?");
8916fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    // TypeQuals handled by caller.
892711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
893af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    if (Result.isNull()) {
894af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson      Result = Context.IntTy;
895711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      declarator.setInvalidType(true);
896af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson    }
8976fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson    break;
8986fd634f4ac59f5923cffadadb99d19f23c18707aAnders Carlsson  }
899ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case DeclSpec::TST_underlyingType:
900db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    Result = S.GetTypeFromParser(DS.getRepAsType());
901db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    assert(!Result.isNull() && "Didn't get a type for __underlying_type?");
902ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    Result = S.BuildUnaryTransformType(Result,
903ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::EnumUnderlyingType,
904ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       DS.getTypeSpecTypeLoc());
905ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (Result.isNull()) {
906ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Result = Context.IntTy;
907ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      declarator.setInvalidType(true);
908db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt    }
90991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
910db5d44b775c60166074acd184ca9f1981c10c2a7Sean Hunt
911e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  case DeclSpec::TST_auto: {
912e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    // TypeQuals handled by caller.
91334b41d939a1328f484511c6002ba2456db879a29Richard Smith    Result = Context.getAutoType(QualType());
914e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson    break;
915e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  }
9161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
917a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall  case DeclSpec::TST_unknown_anytype:
918a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    Result = Context.UnknownAnyTy;
919a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall    break;
920a5fc472b353b88be3b4981da946fb01f5a5cc0c6John McCall
921b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case DeclSpec::TST_atomic:
922b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.GetTypeFromParser(DS.getRepAsType());
923b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(!Result.isNull() && "Didn't get a type for _Atomic?");
924b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc());
925b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Result.isNull()) {
926b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Result = Context.IntTy;
927b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      declarator.setInvalidType(true);
928b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
92991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
930b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
931b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_t:
932b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dTy;
933b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
934b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
935b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_array_t:
936b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dArrayTy;
937b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
938b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
939b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image1d_buffer_t:
940b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage1dBufferTy;
941b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
942b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
943b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_t:
944b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dTy;
945b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
946b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
947b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image2d_array_t:
948b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage2dArrayTy;
949b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
950b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
951b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case DeclSpec::TST_image3d_t:
952b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    Result = Context.OCLImage3dTy;
953b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    break;
954b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
955809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  case DeclSpec::TST_error:
9565153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    Result = Context.IntTy;
957711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    declarator.setInvalidType(true);
9585153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner    break;
9595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
961958858e04e9f98a42031ba69779e49c21f01ca6cChris Lattner  // Handle complex types.
962f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
9634e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (S.getLangOpts().Freestanding)
964711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
965fab5b45729db4e24ba43bb94d1bce5f73106be78Chris Lattner    Result = Context.getComplexType(Result);
96682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  } else if (DS.isTypeAltiVecVector()) {
96782287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
96882287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
969e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType::VectorKind VecKind = VectorType::AltiVecVector;
970788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (DS.isTypeAltiVecPixel())
971e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecPixel;
972788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    else if (DS.isTypeAltiVecBool())
973e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson      VecKind = VectorType::AltiVecBool;
974e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    Result = Context.getVectorType(Result, 128/typeSize, VecKind);
975f244cd7e54753caf6edb76df430dea2f43bb82a8Douglas Gregor  }
9761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
97747423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  // FIXME: Imaginary.
97847423bdaa06a3b9c2a859b57c17fc570094dad1cArgyrios Kyrtzidis  if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
979711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
9801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
981711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Before we process any type attributes, synthesize a block literal
982711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // function declarator if necessary.
983711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (declarator.getContext() == Declarator::BlockLiteralContext)
984711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    maybeSynthesizeBlockSignature(state, Result);
985711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
986711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any type attributes from the decl spec.  This may cause the
987711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // list of type attributes to be temporarily saved while the type
988711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // attributes are pushed around.
989711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (AttributeList *attrs = DS.getAttributes().getList())
990f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    processTypeAttrs(state, Result, TAL_DeclSpec, attrs);
9911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
99296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  // Apply const/volatile/restrict qualifiers to T.
99396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
99496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner
99596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
99696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // or incomplete types shall not be restrict-qualified."  C++ also allows
99796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // restrict-qualified references.
9980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (TypeQuals & DeclSpec::TQ_restrict) {
9992b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian      if (Result->isAnyPointerType() || Result->isReferenceType()) {
10002b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        QualType EltTy;
10012b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        if (Result->isObjCObjectPointerType())
10022b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result;
10032b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian        else
10042b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian          EltTy = Result->isPointerType() ?
10052b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<PointerType>()->getPointeeType() :
10062b5ff1a1471819192ae805b51b888030ecb52914Fariborz Jahanian                    Result->getAs<ReferenceType>()->getPointeeType();
10071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1008bad0e656c3732e3539a9cd6525de721d7e47408bDouglas Gregor        // If we have a pointer or reference, the pointee must have an object
1009bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        // incomplete type.
1010bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        if (!EltTy->isIncompleteOrObjectType()) {
1011711c52bb20d0c69063b52a99826fb7d2835501f1John McCall          S.Diag(DS.getRestrictSpecLoc(),
1012d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner               diag::err_typecheck_invalid_restrict_invalid_pointee)
1013d162584991885ab004a02573a73ce06422b921fcChris Lattner            << EltTy << DS.getSourceRange();
10140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall          TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
1015bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner        }
1016bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner      } else {
1017711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        S.Diag(DS.getRestrictSpecLoc(),
1018711c52bb20d0c69063b52a99826fb7d2835501f1John McCall               diag::err_typecheck_invalid_restrict_not_pointer)
1019d162584991885ab004a02573a73ce06422b921fcChris Lattner          << Result << DS.getSourceRange();
10200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
102196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
102296b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
10231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
102496b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
102596b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // of a function type includes any type qualifiers, the behavior is
102696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    // undefined."
102796b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    if (Result->isFunctionType() && TypeQuals) {
102896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      // Get some location to point at, either the C or V location.
102996b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      SourceLocation Loc;
10300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (TypeQuals & DeclSpec::TQ_const)
103196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getConstSpecLoc();
10320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else if (TypeQuals & DeclSpec::TQ_volatile)
103396b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner        Loc = DS.getVolatileSpecLoc();
10340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      else {
10350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        assert((TypeQuals & DeclSpec::TQ_restrict) &&
10360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall               "Has CVR quals but not C, V, or R?");
10370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Loc = DS.getRestrictSpecLoc();
103896b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner      }
1039711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(Loc, diag::warn_typecheck_function_qualifiers)
1040d162584991885ab004a02573a73ce06422b921fcChris Lattner        << Result << DS.getSourceRange();
104196b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner    }
10421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1043f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    // C++ [dcl.ref]p1:
1044f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   Cv-qualified references are ill-formed except when the
1045f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   cv-qualifiers are introduced through the use of a typedef
1046f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   (7.1.3) or of a template type argument (14.3), in which
1047f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor    //   case the cv-qualifiers are ignored.
10481a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    // FIXME: Shouldn't we be checking SCS_typedef here?
10491a51b4a11b7db25cac2134249711ecaaf9d1c0a8Douglas Gregor    if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
1050f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor        TypeQuals && Result->isReferenceType()) {
10510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_const;
10520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      TypeQuals &= ~DeclSpec::TQ_volatile;
10531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
10541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1055bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // C90 6.5.3 constraints: "The same type qualifier shall not appear more
1056bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // than once in the same specifier-list or qualifier-list, either directly
1057bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    // or via one or more typedefs."
105891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus
1059bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman        && TypeQuals & Result.getCVRQualifiers()) {
1060bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()) {
106191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec)
1062bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "const";
1063bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1064bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
1065bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()) {
106691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec)
1067bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman          << "volatile";
1068bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      }
1069bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
1070bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      // C90 doesn't have restrict, so it doesn't force us to produce a warning
1071bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman      // in this case.
1072bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman    }
1073bc1029b4a47282cb4ce27c7014acb864b10a4043Eli Friedman
10740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
10750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Result = Context.getQualifiedType(Result, Quals);
107696b77fc05ed4a052a9e614f72b0e83572408ce48Chris Lattner  }
10770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1078f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner  return Result;
1079f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner}
1080f1d705c3e2276f7f5b97b8b3394b9b3068fdf25bChris Lattner
1081cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregorstatic std::string getPrintableNameForEntity(DeclarationName Entity) {
1082cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (Entity)
1083cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return Entity.getAsString();
10841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1085cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return "type name";
1086cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1087cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
10882865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
10892865474261a608c7873b87ba4af110d17907896dJohn McCall                                  Qualifiers Qs) {
10902865474261a608c7873b87ba4af110d17907896dJohn McCall  // Enforce C99 6.7.3p2: "Types other than pointer types derived from
10912865474261a608c7873b87ba4af110d17907896dJohn McCall  // object or incomplete types shall not be restrict-qualified."
10922865474261a608c7873b87ba4af110d17907896dJohn McCall  if (Qs.hasRestrict()) {
10932865474261a608c7873b87ba4af110d17907896dJohn McCall    unsigned DiagID = 0;
10942865474261a608c7873b87ba4af110d17907896dJohn McCall    QualType ProblemTy;
10952865474261a608c7873b87ba4af110d17907896dJohn McCall
10962865474261a608c7873b87ba4af110d17907896dJohn McCall    const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
10972865474261a608c7873b87ba4af110d17907896dJohn McCall    if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
10982865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
10992865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
11002865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
11012865474261a608c7873b87ba4af110d17907896dJohn McCall      }
11022865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
11032865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
11042865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
11052865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
11062865474261a608c7873b87ba4af110d17907896dJohn McCall      }
11072865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
11082865474261a608c7873b87ba4af110d17907896dJohn McCall      if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
11092865474261a608c7873b87ba4af110d17907896dJohn McCall        DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
11102865474261a608c7873b87ba4af110d17907896dJohn McCall        ProblemTy = T->getAs<PointerType>()->getPointeeType();
111191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
11122865474261a608c7873b87ba4af110d17907896dJohn McCall    } else if (!Ty->isDependentType()) {
11132865474261a608c7873b87ba4af110d17907896dJohn McCall      // FIXME: this deserves a proper diagnostic
11142865474261a608c7873b87ba4af110d17907896dJohn McCall      DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
11152865474261a608c7873b87ba4af110d17907896dJohn McCall      ProblemTy = T;
11162865474261a608c7873b87ba4af110d17907896dJohn McCall    }
11172865474261a608c7873b87ba4af110d17907896dJohn McCall
11182865474261a608c7873b87ba4af110d17907896dJohn McCall    if (DiagID) {
11192865474261a608c7873b87ba4af110d17907896dJohn McCall      Diag(Loc, DiagID) << ProblemTy;
11202865474261a608c7873b87ba4af110d17907896dJohn McCall      Qs.removeRestrict();
11212865474261a608c7873b87ba4af110d17907896dJohn McCall    }
11222865474261a608c7873b87ba4af110d17907896dJohn McCall  }
11232865474261a608c7873b87ba4af110d17907896dJohn McCall
11242865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getQualifiedType(T, Qs);
11252865474261a608c7873b87ba4af110d17907896dJohn McCall}
11262865474261a608c7873b87ba4af110d17907896dJohn McCall
1127075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara/// \brief Build a paren type including \p T.
1128075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType Sema::BuildParenType(QualType T) {
1129075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return Context.getParenType(T);
1130075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
1131075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
1132f85e193739c953358c865005855253af4f68a497John McCall/// Given that we're building a pointer or reference to the given
1133f85e193739c953358c865005855253af4f68a497John McCallstatic QualType inferARCLifetimeForPointee(Sema &S, QualType type,
1134f85e193739c953358c865005855253af4f68a497John McCall                                           SourceLocation loc,
1135f85e193739c953358c865005855253af4f68a497John McCall                                           bool isReference) {
1136f85e193739c953358c865005855253af4f68a497John McCall  // Bail out if retention is unrequired or already specified.
1137f85e193739c953358c865005855253af4f68a497John McCall  if (!type->isObjCLifetimeType() ||
1138f85e193739c953358c865005855253af4f68a497John McCall      type.getObjCLifetime() != Qualifiers::OCL_None)
1139f85e193739c953358c865005855253af4f68a497John McCall    return type;
1140f85e193739c953358c865005855253af4f68a497John McCall
1141f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None;
1142f85e193739c953358c865005855253af4f68a497John McCall
1143f85e193739c953358c865005855253af4f68a497John McCall  // If the object type is const-qualified, we can safely use
1144f85e193739c953358c865005855253af4f68a497John McCall  // __unsafe_unretained.  This is safe (because there are no read
1145f85e193739c953358c865005855253af4f68a497John McCall  // barriers), and it'll be safe to coerce anything but __weak* to
1146f85e193739c953358c865005855253af4f68a497John McCall  // the resulting type.
1147f85e193739c953358c865005855253af4f68a497John McCall  if (type.isConstQualified()) {
1148f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
1149f85e193739c953358c865005855253af4f68a497John McCall
1150f85e193739c953358c865005855253af4f68a497John McCall  // Otherwise, check whether the static type does not require
1151f85e193739c953358c865005855253af4f68a497John McCall  // retaining.  This currently only triggers for Class (possibly
1152f85e193739c953358c865005855253af4f68a497John McCall  // protocol-qualifed, and arrays thereof).
1153f85e193739c953358c865005855253af4f68a497John McCall  } else if (type->isObjCARCImplicitlyUnretainedType()) {
1154f85e193739c953358c865005855253af4f68a497John McCall    implicitLifetime = Qualifiers::OCL_ExplicitNone;
11555b76f373d23cc3b292ecf523349aaaa388eea375Argyrios Kyrtzidis
1156ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // If we are in an unevaluated context, like sizeof, skip adding a
1157ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // qualification.
115871f55f771794674a410171dbf3cb5dbedf95d033David Blaikie  } else if (S.isUnevaluatedContext()) {
1159ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    return type;
1160f85e193739c953358c865005855253af4f68a497John McCall
1161e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // If that failed, give an error and recover using __strong.  __strong
1162e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // is the option most likely to prevent spurious second-order diagnostics,
1163e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall  // like when binding a reference to a field.
1164f85e193739c953358c865005855253af4f68a497John McCall  } else {
1165f85e193739c953358c865005855253af4f68a497John McCall    // These types can show up in private ivars in system headers, so
1166f85e193739c953358c865005855253af4f68a497John McCall    // we need this to not be an error in those cases.  Instead we
1167f85e193739c953358c865005855253af4f68a497John McCall    // want to delay.
1168f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
1169ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.DelayedDiagnostics.add(
1170ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman          sema::DelayedDiagnostic::makeForbiddenType(loc,
1171ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman              diag::err_arc_indirect_no_ownership, type, isReference));
1172f85e193739c953358c865005855253af4f68a497John McCall    } else {
1173ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman      S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference;
1174f85e193739c953358c865005855253af4f68a497John McCall    }
1175e8c904ff343f440e213b88e6963f5ebfbec7ae60John McCall    implicitLifetime = Qualifiers::OCL_Strong;
1176f85e193739c953358c865005855253af4f68a497John McCall  }
1177f85e193739c953358c865005855253af4f68a497John McCall  assert(implicitLifetime && "didn't infer any lifetime!");
1178f85e193739c953358c865005855253af4f68a497John McCall
1179f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers qs;
1180f85e193739c953358c865005855253af4f68a497John McCall  qs.addObjCLifetime(implicitLifetime);
1181f85e193739c953358c865005855253af4f68a497John McCall  return S.Context.getQualifiedType(type, qs);
1182f85e193739c953358c865005855253af4f68a497John McCall}
1183f85e193739c953358c865005855253af4f68a497John McCall
1184cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a pointer type.
1185cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1186cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a pointer.
1187cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1188cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1189cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// pointer type or, if there is no such entity, the location of the
1190cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have pointer type.
1191cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1192cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the pointer
1193cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1194cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1195cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable pointer type, if there are no
1196cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
11972865474261a608c7873b87ba4af110d17907896dJohn McCallQualType Sema::BuildPointerType(QualType T,
1198cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                                SourceLocation Loc, DeclarationName Entity) {
1199cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isReferenceType()) {
1200cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C++ 8.3.2p4: There shall be no ... pointers to references ...
1201cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
1202ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1203cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1204cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1205cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1206c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
120792e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
1208f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build pointers to unqualified pointers.
12094e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1210f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false);
1211f85e193739c953358c865005855253af4f68a497John McCall
1212cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Build the pointer type.
12132865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getPointerType(T);
1214cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1215cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1216cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build a reference type.
1217cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1218cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type to which we'll be building a reference.
1219cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1220cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Loc The location of the entity whose type involves this
1221cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// reference type or, if there is no such entity, the location of the
1222cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type that will have reference type.
1223cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1224cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the reference
1225cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1226cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1227cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable reference type, if there are no
1228cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// errors. Otherwise, returns a NULL type.
122954e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
12302865474261a608c7873b87ba4af110d17907896dJohn McCall                                  SourceLocation Loc,
123154e14c4db764c0636160d26c5bbf491637c83a76John McCall                                  DeclarationName Entity) {
123291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  assert(Context.getCanonicalType(T) != Context.OverloadTy &&
12339625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
123491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
123569d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // C++0x [dcl.ref]p6:
123691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   If a typedef (7.1.3), a type template-parameter (14.3.1), or a
123791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
123891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   type T, an attempt to create the type "lvalue reference to cv TR" creates
123991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //   the type "lvalue reference to T", while an attempt to create the type
124069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //   "rvalue reference to cv TR" creates the type TR.
124154e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
124254e14c4db764c0636160d26c5bbf491637c83a76John McCall
124354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // C++ [dcl.ref]p4: There shall be no references to references.
124454e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
124554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // According to C++ DR 106, references to references are only
124654e14c4db764c0636160d26c5bbf491637c83a76John McCall  // diagnosed when they are written directly (e.g., "int & &"),
124754e14c4db764c0636160d26c5bbf491637c83a76John McCall  // but not when they happen via a typedef:
124854e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
124954e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef int& intref;
125054e14c4db764c0636160d26c5bbf491637c83a76John McCall  //   typedef intref& intref2;
125154e14c4db764c0636160d26c5bbf491637c83a76John McCall  //
125254e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Parser::ParseDeclaratorInternal diagnoses the case where
125354e14c4db764c0636160d26c5bbf491637c83a76John McCall  // references are written directly; here, we handle the
125469d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // collapsing of references-to-references as described in C++0x.
125569d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  // DR 106 and 540 introduce reference-collapsing into C++98/03.
1256cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1257cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C++ [dcl.ref]p1:
125833a3138a0862cafdd9ff1332b834454a79cd2cdcEli Friedman  //   A declarator that specifies the type "reference to cv void"
1259cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  //   is ill-formed.
1260cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isVoidType()) {
1261cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_reference_to_void);
1262cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1263cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1264cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1265f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, it is forbidden to build references to unqualified pointers.
12664e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
1267f85e193739c953358c865005855253af4f68a497John McCall    T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true);
1268f85e193739c953358c865005855253af4f68a497John McCall
1269cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // Handle restrict on references.
12707c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueRef)
12712865474261a608c7873b87ba4af110d17907896dJohn McCall    return Context.getLValueReferenceType(T, SpelledAsLValue);
12722865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getRValueReferenceType(T);
1273cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
1274cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1275e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// Check whether the specified array size makes the array type a VLA.  If so,
1276e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner/// return true, if not, return the size of the array in SizeVal.
1277282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smithstatic bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal) {
1278282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode
1279282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // (like gnu99, but not c99) accept any evaluatable value as an extension.
1280ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  class VLADiagnoser : public Sema::VerifyICEDiagnoser {
1281ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  public:
1282ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    VLADiagnoser() : Sema::VerifyICEDiagnoser(true) {}
128391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1284ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) {
1285ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
128691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1287ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    virtual void diagnoseFold(Sema &S, SourceLocation Loc, SourceRange SR) {
1288ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor      S.Diag(Loc, diag::ext_vla_folded_to_constant) << SR;
1289ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor    }
1290ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  } Diagnoser;
129191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1292ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor  return S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser,
1293ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor                                           S.LangOpts.GNUMode).isInvalid();
1294e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner}
1295e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1296e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner
1297cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \brief Build an array type.
1298cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1299cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param T The type of each element in the array.
1300cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1301cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param ASM C99 array size modifier (e.g., '*', 'static').
13021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
13031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ArraySize Expression describing the size of the array.
1304cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1305efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Brackets The range from the opening '[' to the closing ']'.
1306cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1307cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \param Entity The name of the entity that involves the array
1308cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// type, if known.
1309cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor///
1310cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// \returns A suitable array type, if there are no errors. Otherwise,
1311cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor/// returns a NULL type.
1312cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas GregorQualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1313cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor                              Expr *ArraySize, unsigned Quals,
13147e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              SourceRange Brackets, DeclarationName Entity) {
13150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
13167e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  SourceLocation Loc = Brackets.getBegin();
13174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
1318138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C++ [dcl.array]p1:
1319138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   T is called the array element type; this type shall not be a reference
132091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   type, the (possibly cv-qualified) type void, a function type or an
1321138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //   abstract class type.
1322138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    //
1323bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    // C++ [dcl.array]p3:
1324bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   When several "array of" specifications are adjacent, [...] only the
1325bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   first of the constant expressions that specify the bounds of the arrays
1326bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //   may be omitted.
1327bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    //
1328138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // Note: function types are handled in the common path with C.
1329138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    if (T->isReferenceType()) {
1330138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      Diag(Loc, diag::err_illegal_decl_array_of_references)
1331138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      << getPrintableNameForEntity(Entity) << T;
1332138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
1333138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    }
133491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1335bb35151a166db2b4fee043bc90e60858ac2b7a89Richard Smith    if (T->isVoidType() || T->isIncompleteArrayType()) {
1336923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1337923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1338923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
133991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
134091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    if (RequireNonAbstractType(Brackets.getBegin(), T,
1341138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor                               diag::err_array_of_abstract_type))
1342138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor      return QualType();
134391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1344923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  } else {
1345138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1346138bb2366baa3856088bae94f36f2d96b2c995b9Douglas Gregor    // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
1347923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (RequireCompleteType(Loc, T,
1348923d56d436f750bc1f29db50e641078725558a1bSebastian Redl                            diag::err_illegal_decl_array_incomplete_type))
1349923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1350923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  }
1351cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1352cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (T->isFunctionType()) {
1353cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(Loc, diag::err_illegal_decl_array_of_functions)
1354ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << getPrintableNameForEntity(Entity) << T;
1355cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1356cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
13571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
135834b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (T->getContainedAutoType()) {
135934b41d939a1328f484511c6002ba2456db879a29Richard Smith    Diag(Loc, diag::err_illegal_decl_array_of_auto)
136034b41d939a1328f484511c6002ba2456db879a29Richard Smith      << getPrintableNameForEntity(Entity) << T;
1361e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson    return QualType();
1362e7cf07d8df83e083505c7105c50b2797493008a6Anders Carlsson  }
13631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13646217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *EltTy = T->getAs<RecordType>()) {
1365cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // If the element type is a struct or union that contains a variadic
1366cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // array, accept it as a GNU extension: C99 6.7.2.1p2.
1367cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (EltTy->getDecl()->hasFlexibleArrayMember())
1368cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      Diag(Loc, diag::ext_flexible_array_in_array) << T;
1369c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (T->isObjCObjectType()) {
1370c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1371c7c11b1ba6a110f2416889cc3576fe33277b2a33Chris Lattner    return QualType();
1372cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
13731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1374806054db6653d29cb0d9692df3612cbcd03d0530John McCall  // Do placeholder conversions on the array size expression.
1375806054db6653d29cb0d9692df3612cbcd03d0530John McCall  if (ArraySize && ArraySize->hasPlaceholderType()) {
1376806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ExprResult Result = CheckPlaceholderExpr(ArraySize);
1377806054db6653d29cb0d9692df3612cbcd03d0530John McCall    if (Result.isInvalid()) return QualType();
1378806054db6653d29cb0d9692df3612cbcd03d0530John McCall    ArraySize = Result.take();
1379806054db6653d29cb0d9692df3612cbcd03d0530John McCall  }
1380806054db6653d29cb0d9692df3612cbcd03d0530John McCall
13815e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  // Do lvalue-to-rvalue conversions on the array size expression.
1382429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ArraySize && !ArraySize->isRValue()) {
1383429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult Result = DefaultLvalueConversion(ArraySize);
1384429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Result.isInvalid())
1385429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return QualType();
1386429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
1387429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ArraySize = Result.take();
1388429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  }
13895e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1390cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // C99 6.7.5.2p1: The size expression shall have integer type.
1391282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  // C++11 allows contextual conversions to such types.
139280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith  if (!getLangOpts().CPlusPlus11 &&
1393282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      ArraySize && !ArraySize->isTypeDependent() &&
13941274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1395cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1396cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      << ArraySize->getType() << ArraySize->getSourceRange();
1397cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    return QualType();
1398cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1399282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
14002767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
1401cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (!ArraySize) {
1402f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    if (ASM == ArrayType::Star)
14037e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor      T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
1404f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman    else
1405f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      T = Context.getIncompleteArrayType(T, ASM, Quals);
1406ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
14077e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
1408282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  } else if ((!T->isDependentType() && !T->isIncompleteType() &&
1409282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith              !T->isConstantSizeType()) ||
1410282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith             isArraySizeVLA(*this, ArraySize, ConstVal)) {
1411282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // Even in C++11, don't allow contextual conversions in the array bound
1412282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    // of a VLA.
141380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (getLangOpts().CPlusPlus11 &&
1414282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1415282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1416282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        << ArraySize->getType() << ArraySize->getSourceRange();
1417282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      return QualType();
1418282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    }
1419282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
1420e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with an element type that has a non-constant-size is a VLA.
1421e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // C99: an array with a non-ICE size is a VLA.  We accept any expression
1422e1eed38733ed47d44f9d8c7731817c411eaf4141Chris Lattner    // that we can fold to a non-zero positive value as an extension.
14237e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
1424cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  } else {
1425cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1426cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    // have a value greater than zero.
1427923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal.isSigned() && ConstVal.isNegative()) {
1428b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      if (Entity)
1429b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1430b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1431b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth      else
1432b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth        Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1433b2b5cc0cf908d516a107d373db963f692449a8a8Chandler Carruth          << ArraySize->getSourceRange();
1434923d56d436f750bc1f29db50e641078725558a1bSebastian Redl      return QualType();
1435923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    }
1436923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    if (ConstVal == 0) {
143702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // GCC accepts zero sized static arrays. We allow them when
143802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // we're not in a SFINAE context.
143991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      Diag(ArraySize->getLocStart(),
144002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor           isSFINAEContext()? diag::err_typecheck_zero_array_size
144102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            : diag::ext_typecheck_zero_array_size)
1442923d56d436f750bc1f29db50e641078725558a1bSebastian Redl        << ArraySize->getSourceRange();
144320cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne
144420cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      if (ASM == ArrayType::Static) {
144520cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        Diag(ArraySize->getLocStart(),
144620cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne             diag::warn_typecheck_zero_static_array_size)
144720cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne          << ArraySize->getSourceRange();
144820cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne        ASM = ArrayType::Normal;
144920cdbeb8f36576f469db195b4140c293c7281718Peter Collingbourne      }
145091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
14512767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor               !T->isIncompleteType()) {
145291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // Is the array too large?
14532767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      unsigned ActiveSizeBits
14542767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
14552767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
14562767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor        Diag(ArraySize->getLocStart(), diag::err_array_too_large)
14572767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ConstVal.toString(10)
14582767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor          << ArraySize->getSourceRange();
14591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
146091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
146146a617a792bfab0d9b1e057371ea3b9540802226John McCall    T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
1462cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1463af40776922bc5c28e740adb0342faa09f35b0068David Chisnall  // If this is not C99, extwarn about VLA's and C99 array size modifiers.
14644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().C99) {
14650fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    if (T->isVariableArrayType()) {
14660fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Prohibit the use of non-POD types in VLAs.
1467f85e193739c953358c865005855253af4f68a497John McCall      QualType BaseT = Context.getBaseElementType(T);
146891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      if (!T->isDependentType() &&
14693c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT.isPODType(Context) &&
14703c7236e01dfb69b370857ccd71c7bcf5ce80b36fDouglas Gregor          !BaseT->isObjCLifetimeType()) {
14710fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::err_vla_non_pod)
1472f85e193739c953358c865005855253af4f68a497John McCall          << BaseT;
14730fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        return QualType();
147491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      }
1475a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      // Prohibit the use of VLAs during template argument deduction.
1476a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      else if (isSFINAEContext()) {
1477a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        Diag(Loc, diag::err_vla_in_sfinae);
1478a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor        return QualType();
1479a481ec4150ad203440852a2bfee0883dd26f7530Douglas Gregor      }
14800fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      // Just extwarn about VLAs.
14810fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor      else
14820fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor        Diag(Loc, diag::ext_vla);
14830fddb97901dbe36a8253dee29961cba8e0a87cf6Douglas Gregor    } else if (ASM != ArrayType::Normal || Quals != 0)
1484d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith      Diag(Loc,
14854e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie           getLangOpts().CPlusPlus? diag::err_c99_array_usage_cxx
1486d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith                                     : diag::ext_c99_array_usage) << ASM;
1487cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  }
1488cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
1489cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  return T;
1490cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor}
14919cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
14929cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \brief Build an ext-vector type.
14939cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor///
14949cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// Run the required checks for the extended vector type.
14959ae2f076ca5ab1feb3ba95629099ec2319833701John McCallQualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
14969cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                  SourceLocation AttrLoc) {
14979cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // unlike gcc's vector_size attribute, we do not allow vectors to be defined
14989cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  // in conjunction with complex types (pointers, arrays, functions, etc.).
14991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!T->isDependentType() &&
15009cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      !T->isIntegerType() && !T->isRealFloatingType()) {
15019cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
15029cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return QualType();
15039cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
15049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
15059ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
15069cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    llvm::APSInt vecSize(32);
15079ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
15089cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_argument_not_int)
15099ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        << "ext_vector_type" << ArraySize->getSourceRange();
15109cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
15119cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
15121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // unlike gcc's vector_size attribute, the size is specified as the
15149cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    // number of elements, not the number of bytes.
15151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
15161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15179cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    if (vectorSize == 0) {
15189cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      Diag(AttrLoc, diag::err_attribute_zero_size)
15199ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      << ArraySize->getSourceRange();
15209cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      return QualType();
15219cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    }
15221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15234ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    return Context.getExtVectorType(T, vectorSize);
15241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
15251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15269ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
15279cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
15281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1529724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \brief Build a function type.
1530724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1531724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// This routine checks the function type according to C++ rules and
1532724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// under the assumption that the result type and parameter types have
1533724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// just been instantiated from a template. It therefore duplicates
15342943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor/// some of the behavior of GetTypeForDeclarator, but in a much
1535724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// simpler form that is only suitable for this narrow use case.
1536724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1537724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param T The return type of the function.
1538724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1539724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param ParamTypes The parameter types of the function. This array
1540724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// will be modified to account for adjustments to the types of the
1541724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function parameters.
1542724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1543724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param NumParamTypes The number of parameter types in ParamTypes.
1544724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1545724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Variadic Whether this is a variadic function type.
1546724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1547eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith/// \param HasTrailingReturn Whether this function has a trailing return type.
1548eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith///
1549724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Quals The cvr-qualifiers to be applied to the function type.
1550724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1551724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Loc The location of the entity whose type involves this
1552724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// function type or, if there is no such entity, the location of the
1553724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type that will have function type.
1554724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1555724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \param Entity The name of the entity that involves the function
1556724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// type, if known.
1557724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor///
1558724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// \returns A suitable function type, if there are no
1559724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor/// errors. Otherwise, returns a NULL type.
1560724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas GregorQualType Sema::BuildFunctionType(QualType T,
15611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 QualType *ParamTypes,
1562724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor                                 unsigned NumParamTypes,
1563eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith                                 bool Variadic, bool HasTrailingReturn,
1564eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith                                 unsigned Quals,
1565c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                                 RefQualifierKind RefQualifier,
1566fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                 SourceLocation Loc, DeclarationName Entity,
1567e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                 FunctionType::ExtInfo Info) {
1568724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (T->isArrayType() || T->isFunctionType()) {
156991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    Diag(Loc, diag::err_func_returning_array_function)
157058408bc4ead86b08af56cd06fc966fd858b48b2dDouglas Gregor      << T->isFunctionType() << T;
1571724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1572724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1573aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1574aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // Functions cannot return half FP.
1575aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  if (T->isHalfType()) {
1576aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 <<
1577aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      FixItHint::CreateInsertion(Loc, "*");
1578aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    return QualType();
1579aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  }
1580aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
1581724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  bool Invalid = false;
1582724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
1583aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    // FIXME: Loc is too inprecise here, should use proper locations for args.
158479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]);
15852dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    if (ParamType->isVoidType()) {
1586724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Diag(Loc, diag::err_param_with_void_type);
1587724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor      Invalid = true;
1588aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    } else if (ParamType->isHalfType()) {
1589aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Disallow half FP arguments.
1590aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 <<
1591aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        FixItHint::CreateInsertion(Loc, "*");
1592aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Invalid = true;
1593724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    }
1594cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
159554e14c4db764c0636160d26c5bbf491637c83a76John McCall    ParamTypes[Idx] = ParamType;
1596724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  }
1597724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1598724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor  if (Invalid)
1599724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor    return QualType();
1600724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor
1601e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
1602e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.Variadic = Variadic;
1603eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith  EPI.HasTrailingReturn = HasTrailingReturn;
1604e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.TypeQuals = Quals;
1605c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor  EPI.RefQualifier = RefQualifier;
1606e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.ExtInfo = Info;
1607e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1608e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI);
1609724651c3523e25fbf2f6cd0419bc3466e0afdb07Douglas Gregor}
16101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1611949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \brief Build a member pointer type \c T Class::*.
1612949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1613949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param T the type to which the member pointer refers.
1614949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Class the class type into which the member pointer points.
1615949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Loc the location where this type begins
1616949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \param Entity the name of the entity that will have this member pointer type
1617949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor///
1618949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// \returns a member pointer type, if successful, or a NULL type if there was
1619949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor/// an error.
16201eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType Sema::BuildMemberPointerType(QualType T, QualType Class,
16212865474261a608c7873b87ba4af110d17907896dJohn McCall                                      SourceLocation Loc,
1622949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor                                      DeclarationName Entity) {
1623949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // Verify that we're not building a pointer to pointer to function with
1624949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  // exception specification.
1625949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (CheckDistantExceptionSpec(T)) {
1626949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_distant_exception_spec);
1627949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1628949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // FIXME: If we're doing this as part of template instantiation,
1629949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // we should return immediately.
1630949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1631949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // Build the type anyway, but use the canonical type so that the
1632949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    // exception specifiers are stripped off.
1633949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    T = Context.getCanonicalType(T);
1634949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1635949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1636737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  // C++ 8.3.3p3: A pointer to member shall not point to ... a member
1637949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  //   with reference type, or "cv void."
1638949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isReferenceType()) {
16398d4655d3b966da02fe0588767160448594cddd61Anders Carlsson    Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
1640ac406052f7b980f8caa6b07b4a8d0867d53852c4John McCall      << (Entity? Entity.getAsString() : "type name") << T;
1641949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1642949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1643949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1644949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (T->isVoidType()) {
1645949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1646949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      << (Entity? Entity.getAsString() : "type name");
1647949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1648949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1649949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1650949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  if (!Class->isDependentType() && !Class->isRecordType()) {
1651949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1652949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor    return QualType();
1653949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor  }
1654949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor
1655679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos  // In the Microsoft ABI, the class is allowed to be an incomplete
1656679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos  // type. In such cases, the compiler makes a worst-case assumption.
1657679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos  // We make no such assumption right now, so emit an error if the
1658679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos  // class isn't a complete type.
1659679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos  if (Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft &&
1660679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos      RequireCompleteType(Loc, Class, diag::err_incomplete_type))
1661679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos    return QualType();
1662679fc9314c2bde5eb6bea33c790d1a035461e618Joao Matos
16632865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getMemberPointerType(T, Class.getTypePtr());
1664949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor}
16651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16669a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \brief Build a block pointer type.
16679a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
16689a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param T The type to which we'll be building a block pointer.
16699a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
1670efce31f51d6e7e31e125f96c20f6cdab3ead0a47James Dennett/// \param Loc The source location, used for diagnostics.
16719a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
16729a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \param Entity The name of the entity that involves the block pointer
16739a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// type, if known.
16749a917e4fac79aba20fbd25983c78396475078918Anders Carlsson///
16759a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// \returns A suitable block pointer type, if there are no
16769a917e4fac79aba20fbd25983c78396475078918Anders Carlsson/// errors. Otherwise, returns a NULL type.
167791cbbbf506c892a26d4301e2b3ccd377b0938817Chad RosierQualType Sema::BuildBlockPointerType(QualType T,
16781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     SourceLocation Loc,
16799a917e4fac79aba20fbd25983c78396475078918Anders Carlsson                                     DeclarationName Entity) {
16800953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T->isFunctionType()) {
16819a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    Diag(Loc, diag::err_nonfunction_block_type);
16829a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    return QualType();
16839a917e4fac79aba20fbd25983c78396475078918Anders Carlsson  }
16841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16852865474261a608c7873b87ba4af110d17907896dJohn McCall  return Context.getBlockPointerType(T);
16869a917e4fac79aba20fbd25983c78396475078918Anders Carlsson}
16879a917e4fac79aba20fbd25983c78396475078918Anders Carlsson
1688b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallQualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1689b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  QualType QT = Ty.get();
16903f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  if (QT.isNull()) {
1691a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    if (TInfo) *TInfo = 0;
16923f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor    return QualType();
16933f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  }
16943f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor
1695a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = 0;
1696f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
1697e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis    QT = LIT->getType();
1698a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    DI = LIT->getTypeSourceInfo();
1699e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  }
17001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1701a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo) *TInfo = DI;
1702e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis  return QT;
1703e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis}
1704e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis
1705a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
1706a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
1707a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                            unsigned chunkIndex);
1708a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis
1709f85e193739c953358c865005855253af4f68a497John McCall/// Given that this is the declaration of a parameter under ARC,
1710f85e193739c953358c865005855253af4f68a497John McCall/// attempt to infer attributes and such for pointer-to-whatever
1711f85e193739c953358c865005855253af4f68a497John McCall/// types.
1712f85e193739c953358c865005855253af4f68a497John McCallstatic void inferARCWriteback(TypeProcessingState &state,
1713f85e193739c953358c865005855253af4f68a497John McCall                              QualType &declSpecType) {
1714f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
1715f85e193739c953358c865005855253af4f68a497John McCall  Declarator &declarator = state.getDeclarator();
1716f85e193739c953358c865005855253af4f68a497John McCall
1717f85e193739c953358c865005855253af4f68a497John McCall  // TODO: should we care about decl qualifiers?
1718f85e193739c953358c865005855253af4f68a497John McCall
1719f85e193739c953358c865005855253af4f68a497John McCall  // Check whether the declarator has the expected form.  We walk
1720f85e193739c953358c865005855253af4f68a497John McCall  // from the inside out in order to make the block logic work.
1721f85e193739c953358c865005855253af4f68a497John McCall  unsigned outermostPointerIndex = 0;
1722f85e193739c953358c865005855253af4f68a497John McCall  bool isBlockPointer = false;
1723f85e193739c953358c865005855253af4f68a497John McCall  unsigned numPointers = 0;
1724f85e193739c953358c865005855253af4f68a497John McCall  for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
1725f85e193739c953358c865005855253af4f68a497John McCall    unsigned chunkIndex = i;
1726f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex);
1727f85e193739c953358c865005855253af4f68a497John McCall    switch (chunk.Kind) {
1728f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Paren:
1729f85e193739c953358c865005855253af4f68a497John McCall      // Ignore parens.
1730f85e193739c953358c865005855253af4f68a497John McCall      break;
1731f85e193739c953358c865005855253af4f68a497John McCall
1732f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Reference:
1733f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Pointer:
1734f85e193739c953358c865005855253af4f68a497John McCall      // Count the number of pointers.  Treat references
1735f85e193739c953358c865005855253af4f68a497John McCall      // interchangeably as pointers; if they're mis-ordered, normal
1736f85e193739c953358c865005855253af4f68a497John McCall      // type building will discover that.
1737f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1738f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1739f85e193739c953358c865005855253af4f68a497John McCall      break;
1740f85e193739c953358c865005855253af4f68a497John McCall
1741f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::BlockPointer:
1742f85e193739c953358c865005855253af4f68a497John McCall      // If we have a pointer to block pointer, that's an acceptable
1743f85e193739c953358c865005855253af4f68a497John McCall      // indirect reference; anything else is not an application of
1744f85e193739c953358c865005855253af4f68a497John McCall      // the rules.
1745f85e193739c953358c865005855253af4f68a497John McCall      if (numPointers != 1) return;
1746f85e193739c953358c865005855253af4f68a497John McCall      numPointers++;
1747f85e193739c953358c865005855253af4f68a497John McCall      outermostPointerIndex = chunkIndex;
1748f85e193739c953358c865005855253af4f68a497John McCall      isBlockPointer = true;
1749f85e193739c953358c865005855253af4f68a497John McCall
1750f85e193739c953358c865005855253af4f68a497John McCall      // We don't care about pointer structure in return values here.
1751f85e193739c953358c865005855253af4f68a497John McCall      goto done;
1752f85e193739c953358c865005855253af4f68a497John McCall
1753f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Array: // suppress if written (id[])?
1754f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::Function:
1755f85e193739c953358c865005855253af4f68a497John McCall    case DeclaratorChunk::MemberPointer:
1756f85e193739c953358c865005855253af4f68a497John McCall      return;
1757f85e193739c953358c865005855253af4f68a497John McCall    }
1758f85e193739c953358c865005855253af4f68a497John McCall  }
1759f85e193739c953358c865005855253af4f68a497John McCall done:
1760f85e193739c953358c865005855253af4f68a497John McCall
1761f85e193739c953358c865005855253af4f68a497John McCall  // If we have *one* pointer, then we want to throw the qualifier on
1762f85e193739c953358c865005855253af4f68a497John McCall  // the declaration-specifiers, which means that it needs to be a
1763f85e193739c953358c865005855253af4f68a497John McCall  // retainable object type.
1764f85e193739c953358c865005855253af4f68a497John McCall  if (numPointers == 1) {
1765f85e193739c953358c865005855253af4f68a497John McCall    // If it's not a retainable object type, the rule doesn't apply.
1766f85e193739c953358c865005855253af4f68a497John McCall    if (!declSpecType->isObjCRetainableType()) return;
1767f85e193739c953358c865005855253af4f68a497John McCall
1768f85e193739c953358c865005855253af4f68a497John McCall    // If it already has lifetime, don't do anything.
1769f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType.getObjCLifetime()) return;
1770f85e193739c953358c865005855253af4f68a497John McCall
1771f85e193739c953358c865005855253af4f68a497John McCall    // Otherwise, modify the type in-place.
1772f85e193739c953358c865005855253af4f68a497John McCall    Qualifiers qs;
177391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1774f85e193739c953358c865005855253af4f68a497John McCall    if (declSpecType->isObjCARCImplicitlyUnretainedType())
1775f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
1776f85e193739c953358c865005855253af4f68a497John McCall    else
1777f85e193739c953358c865005855253af4f68a497John McCall      qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing);
1778f85e193739c953358c865005855253af4f68a497John McCall    declSpecType = S.Context.getQualifiedType(declSpecType, qs);
1779f85e193739c953358c865005855253af4f68a497John McCall
1780f85e193739c953358c865005855253af4f68a497John McCall  // If we have *two* pointers, then we want to throw the qualifier on
1781f85e193739c953358c865005855253af4f68a497John McCall  // the outermost pointer.
1782f85e193739c953358c865005855253af4f68a497John McCall  } else if (numPointers == 2) {
1783f85e193739c953358c865005855253af4f68a497John McCall    // If we don't have a block pointer, we need to check whether the
1784f85e193739c953358c865005855253af4f68a497John McCall    // declaration-specifiers gave us something that will turn into a
1785f85e193739c953358c865005855253af4f68a497John McCall    // retainable object pointer after we slap the first pointer on it.
1786f85e193739c953358c865005855253af4f68a497John McCall    if (!isBlockPointer && !declSpecType->isObjCObjectType())
1787f85e193739c953358c865005855253af4f68a497John McCall      return;
1788f85e193739c953358c865005855253af4f68a497John McCall
1789f85e193739c953358c865005855253af4f68a497John McCall    // Look for an explicit lifetime attribute there.
1790f85e193739c953358c865005855253af4f68a497John McCall    DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex);
17911c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis    if (chunk.Kind != DeclaratorChunk::Pointer &&
17921c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis        chunk.Kind != DeclaratorChunk::BlockPointer)
17931c73dcbe1f1921bad8311cfb5089d30b4bd75b66Argyrios Kyrtzidis      return;
1794f85e193739c953358c865005855253af4f68a497John McCall    for (const AttributeList *attr = chunk.getAttrs(); attr;
1795f85e193739c953358c865005855253af4f68a497John McCall           attr = attr->getNext())
17968e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt      if (attr->getKind() == AttributeList::AT_ObjCOwnership)
1797f85e193739c953358c865005855253af4f68a497John McCall        return;
1798f85e193739c953358c865005855253af4f68a497John McCall
1799a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis    transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing,
1800a8349f5e60d1b5b0e658195a60d385b56ed440ecArgyrios Kyrtzidis                                          outermostPointerIndex);
1801f85e193739c953358c865005855253af4f68a497John McCall
1802f85e193739c953358c865005855253af4f68a497John McCall  // Any other number of pointers/references does not trigger the rule.
1803f85e193739c953358c865005855253af4f68a497John McCall  } else return;
1804f85e193739c953358c865005855253af4f68a497John McCall
1805f85e193739c953358c865005855253af4f68a497John McCall  // TODO: mark whether we did this inference?
1806f85e193739c953358c865005855253af4f68a497John McCall}
1807f85e193739c953358c865005855253af4f68a497John McCall
1808d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruthstatic void DiagnoseIgnoredQualifiers(unsigned Quals,
1809d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation ConstQualLoc,
1810d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation VolatileQualLoc,
1811d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      SourceLocation RestrictQualLoc,
1812d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                      Sema& S) {
1813d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  std::string QualStr;
1814d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  unsigned NumQuals = 0;
1815d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  SourceLocation Loc;
1816d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1817d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint ConstFixIt;
1818d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint VolatileFixIt;
1819d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  FixItHint RestrictFixIt;
1820d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1821a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg  const SourceManager &SM = S.getSourceManager();
1822a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg
1823d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to
1824d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // find a range and grow it to encompass all the qualifiers, regardless of
1825d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  // the order in which they textually appear.
1826d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Const) {
1827d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc);
1828d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    QualStr = "const";
1829a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    ++NumQuals;
1830a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(ConstQualLoc, Loc))
1831a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = ConstQualLoc;
1832d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1833d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Volatile) {
1834d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc);
1835a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    QualStr += (NumQuals == 0 ? "volatile" : " volatile");
1836d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ++NumQuals;
1837a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(VolatileQualLoc, Loc))
1838a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = VolatileQualLoc;
1839d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1840d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  if (Quals & Qualifiers::Restrict) {
1841d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc);
1842a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    QualStr += (NumQuals == 0 ? "restrict" : " restrict");
1843d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth    ++NumQuals;
1844a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    if (!Loc.isValid() || SM.isBeforeInTranslationUnit(RestrictQualLoc, Loc))
1845a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg      Loc = RestrictQualLoc;
1846d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  }
1847d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1848d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  assert(NumQuals > 0 && "No known qualifiers?");
1849d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
1850d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth  S.Diag(Loc, diag::warn_qual_return_type)
1851a08fcb8105bf53f3640ad17f61bdcde2d8ace78aHans Wennborg    << QualStr << NumQuals << ConstFixIt << VolatileFixIt << RestrictFixIt;
1852d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth}
1853d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
18548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
18558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                             TypeSourceInfo *&ReturnTypeInfo) {
18568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &SemaRef = state.getSema();
18578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
1858930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  QualType T;
18598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ReturnTypeInfo = 0;
1860711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
18618cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // The TagDecl owned by the DeclSpec.
18628cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TagDecl *OwnedTagDecl = 0;
18638999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl
18643f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  switch (D.getName().getKind()) {
186598a5403ecf1d2b60ae8cbf43e54194bd762cacaaFariborz Jahanian  case UnqualifiedId::IK_ImplicitSelfParam:
18663f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_OperatorFunctionId:
18678999fe1bc367b3ecc878d135c7b31e3479da56f4Sebastian Redl  case UnqualifiedId::IK_Identifier:
18680486d746019f8310589b1f0d92edcc4bb3916b33Sean Hunt  case UnqualifiedId::IK_LiteralOperatorId:
18693f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_TemplateId:
18708cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = ConvertDeclSpecToType(state);
187191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
1872591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
1873d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
187415987970eeaa1842c29ec8797affd1c1dea05585Abramo Bagnara      // Owned declaration is embedded in declarator.
1875d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis      OwnedTagDecl->setEmbeddedInDeclarator(true);
1876591bd3cb605f1f0229b4b1d8a4b8183377064ec5Douglas Gregor    }
1877930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
1878930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
18793f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_ConstructorName:
18800efc2c1716be4f1c5f1343cad3b047e74861f030Douglas Gregor  case UnqualifiedId::IK_ConstructorTemplateId:
18813f9a0566e6793151b99a65ab936220971cf96c1bDouglas Gregor  case UnqualifiedId::IK_DestructorName:
1882930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // Constructors and destructors don't have return types. Use
188391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // "void" instead.
18848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    T = SemaRef.Context.VoidTy;
1885a78a640c5f59720f2c2b8034eca4fbf8525d9026Rafael Espindola    if (AttributeList *attrs = D.getDeclSpec().getAttributes().getList())
1886f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclSpec, attrs);
1887930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    break;
188848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor
188948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor  case UnqualifiedId::IK_ConversionFunctionId:
189048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // The result type of a conversion function is the type that it
189148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    // converts to.
189291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId,
18938cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  &ReturnTypeInfo);
189448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor    break;
1895930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
1896dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor
1897711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (D.getAttributes())
1898711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    distributeTypeAttrsFromDeclarator(state, T);
1899711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
1900d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
1901d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // In C++11, a function declarator using 'auto' must have a trailing return
19028110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // type (this is checked later) and we can skip this. In other languages
19038110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith  // using auto, we need to check regardless.
190434b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
190580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      (!SemaRef.getLangOpts().CPlusPlus11 || !D.isFunctionDeclarator())) {
1906baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    int Error = -1;
19071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1908baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    switch (D.getContext()) {
1909baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::KNRTypeListContext:
1910b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("K&R type lists aren't allowed in C++");
1911f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
1912f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman      llvm_unreachable("Can't specify a type specifier in lambda grammar");
1913cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
1914cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
1915baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::PrototypeContext:
1916baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      Error = 0; // Function prototype
1917baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1918baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::MemberContext:
19197a614d8380297fcd2bc23986241905d97222948cRichard Smith      if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)
19207a614d8380297fcd2bc23986241905d97222948cRichard Smith        break;
19218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) {
1922eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie      case TTK_Enum: llvm_unreachable("unhandled tag kind");
1923465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Struct: Error = 1; /* Struct member */ break;
1924465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Union:  Error = 2; /* Union member */ break;
1925465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      case TTK_Class:  Error = 3; /* Class member */ break;
19266666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      case TTK_Interface: Error = 4; /* Interface member */ break;
19271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
1928baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1929baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::CXXCatchContext:
193017b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
19316666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 5; // Exception declaration
1932baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1933baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::TemplateParamContext:
19346666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 6; // Template parameter
1935baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1936baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockLiteralContext:
19376666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 7; // Block literal
193834b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
193934b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TemplateTypeArgContext:
19406666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 8; // Template type argument
194134b41d939a1328f484511c6002ba2456db879a29Richard Smith      break;
1942162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
19433e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
19446666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 10; // Type alias
1945162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      break;
19467796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
19476666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11; // Function return type
19487796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
194934b41d939a1328f484511c6002ba2456db879a29Richard Smith    case Declarator::TypeNameContext:
19506666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 12; // Generic
1951baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1952baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::FileContext:
1953baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::BlockContext:
1954baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ForContext:
1955baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    case Declarator::ConditionContext:
19560b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
1957baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      break;
1958baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    }
1959baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson
1960ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith    if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
19616666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 9;
1962ddc83f9255834217f0559b09ff75a1c50b8ce457Richard Smith
19638110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    // In Objective-C it is an error to use 'auto' on a function declarator.
19648110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith    if (D.isFunctionDeclarator())
19656666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      Error = 11;
19668110f04b39fd028496dd6bf9e3a78278c3e0a6adRichard Smith
1967d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.spec.auto]p2: 'auto' is always fine if the declarator
1968e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // contains a trailing return type. That is only legal at the outermost
1969e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // level. Check all declarator chunks (outermost first) anyway, to give
1970e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    // better diagnostics.
197180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (SemaRef.getLangOpts().CPlusPlus11 && Error != -1) {
1972e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1973e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned chunkIndex = e - i - 1;
1974e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        state.setCurrentChunkIndex(chunkIndex);
1975e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
1976e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (DeclType.Kind == DeclaratorChunk::Function) {
1977e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
197854655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (FTI.hasTrailingReturnType()) {
1979e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            Error = -1;
1980e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            break;
1981e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          }
1982e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        }
1983e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
1984e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith    }
1985e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
1986baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson    if (Error != -1) {
19878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
19888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_auto_not_allowed)
1989baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson        << Error;
19908cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = SemaRef.Context.IntTy;
1991baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson      D.setInvalidType(true);
19920aa86c0463a881be85fd34e04c7de3379997621dRichard Smith    } else
19930aa86c0463a881be85fd34e04c7de3379997621dRichard Smith      SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
19940aa86c0463a881be85fd34e04c7de3379997621dRichard Smith                   diag::warn_cxx98_compat_auto_type_specifier);
1995baf45d31f18e6d5b3d2a33695c2af6e6cbc4ee29Anders Carlsson  }
19968cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
19974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().CPlusPlus &&
19985e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall      OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) {
19998cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // Check the contexts where C++ forbids the declaration of a new class
20008cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    // or enumeration in a type-specifier-seq.
20018cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    switch (D.getContext()) {
20027796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
20037796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // Class and enumeration definitions are syntactically not allowed in
20047796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      // trailing return types.
20057796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      llvm_unreachable("parser should not have allowed this");
20067796eb5643244f3134834253ce5ea89107ac21c1Richard Smith      break;
20078cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::FileContext:
20088cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::MemberContext:
20098cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockContext:
20108cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ForContext:
20118cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::BlockLiteralContext:
2012f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
2013d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // C++11 [dcl.type]p3:
20148cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   A type-specifier-seq shall not define a class or enumeration unless
20158cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   it appears in the type-id of an alias-declaration (7.1.3) that is not
20168cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   the declaration of a template-declaration.
20178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasDeclContext:
20188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
20198cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::AliasTemplateContext:
20208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
20218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_alias_template)
20228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2023601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
20248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
20258cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TypeNameContext:
20268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateParamContext:
20278cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXNewContext:
20288cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::CXXCatchContext:
20298cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ObjCCatchContext:
20308cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::TemplateTypeArgContext:
20318cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
20328cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis             diag::err_type_defined_in_type_specifier)
20338cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2034601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
20358cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
20368cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::PrototypeContext:
2037cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:
2038cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:
20398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::KNRTypeListContext:
20408cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ [dcl.fct]p6:
20418cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      //   Types shall not be defined in return or parameter types.
20428cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
20438cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_param_type)
20448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2045601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
20468cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
20478cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    case Declarator::ConditionContext:
20488cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // C++ 6.4p2:
20498cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // The type-specifier-seq shall not contain typedef and shall not declare
20508cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      // a new class or enumeration.
20518cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      SemaRef.Diag(OwnedTagDecl->getLocation(),
20528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                   diag::err_type_defined_in_condition);
2053601e6e894c2a38243588b375bed0b9a9e60060bbEnea Zaffanella      D.setInvalidType(true);
20548cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      break;
20558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    }
20568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  }
20578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
20588cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return T;
20598cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
20608cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2061a08c2fb74ef823c185619ecc532f8fced6a1982fBenjamin Kramerstatic std::string getFunctionQualifiersAsString(const FunctionProtoType *FnTy){
2062d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  std::string Quals =
2063d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
2064d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2065d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (FnTy->getRefQualifier()) {
2066d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_None:
2067d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2068d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2069d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_LValue:
2070d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2071d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2072d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += '&';
2073d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2074d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2075d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case RQ_RValue:
2076d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (!Quals.empty())
2077d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      Quals += ' ';
2078d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    Quals += "&&";
2079d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2080d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2081d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2082d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  return Quals;
2083d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2084d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2085d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// Check that the function type T, which has a cv-qualifier or a ref-qualifier,
2086d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// can be contained within the declarator chunk DeclType, and produce an
2087d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith/// appropriate diagnostic if not.
2088d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smithstatic void checkQualifiedFunction(Sema &S, QualType T,
2089d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                   DeclaratorChunk &DeclType) {
2090d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // C++98 [dcl.fct]p4 / C++11 [dcl.fct]p6: a function type with a
2091d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // cv-qualifier or a ref-qualifier can only appear at the topmost level
2092d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // of a type.
2093d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  int DiagKind = -1;
2094d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  switch (DeclType.Kind) {
2095d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Paren:
2096d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::MemberPointer:
2097d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases are permitted.
2098d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2099d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Array:
2100d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Function:
2101d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // These cases don't allow function types at all; no need to diagnose the
2102d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // qualifiers separately.
2103d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    return;
2104d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::BlockPointer:
2105d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 0;
2106d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2107d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Pointer:
2108d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 1;
2109d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2110d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  case DeclaratorChunk::Reference:
2111d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    DiagKind = 2;
2112d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    break;
2113d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  }
2114d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2115d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  assert(DiagKind != -1);
2116d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  S.Diag(DeclType.Loc, diag::err_compound_qualified_function_type)
2117d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << DiagKind << isa<FunctionType>(T.IgnoreParens()) << T
2118d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    << getFunctionQualifiersAsString(T->castAs<FunctionProtoType>());
2119d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith}
2120d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2121b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// Produce an approprioate diagnostic for an ambiguity between a function
2122b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith/// declarator and a C++ direct-initializer.
2123b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smithstatic void warnAboutAmbiguousFunction(Sema &S, Declarator &D,
2124b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith                                       DeclaratorChunk &DeclType, QualType RT) {
2125b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2126b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  assert(FTI.isAmbiguous && "no direct-initializer / function ambiguity");
2127b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2128b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // If the return type is void there is no ambiguity.
2129b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isVoidType())
2130b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2131b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2132b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a non-class type can have at most one argument.
2133b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!RT->isRecordType() && FTI.NumArgs > 1)
2134b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2135b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2136b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // An initializer for a reference must have exactly one argument.
2137b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (RT->isReferenceType() && FTI.NumArgs != 1)
2138b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2139b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2140b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Only warn if this declarator is declaring a function at block scope, and
2141b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // doesn't have a storage class (such as 'extern') specified.
2142b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (!D.isFunctionDeclarator() ||
2143b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      D.getFunctionDefinitionKind() != FDK_Declaration ||
2144b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      !S.CurContext->isFunctionOrMethod() ||
2145b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      D.getDeclSpec().getStorageClassSpecAsWritten()
2146b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        != DeclSpec::SCS_unspecified)
2147b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2148b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2149b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // Inside a condition, a direct initializer is not permitted. We allow one to
2150b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  // be parsed in order to give better diagnostics in condition parsing.
2151b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  if (D.getContext() == Declarator::ConditionContext)
2152b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    return;
2153b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2154b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  SourceRange ParenRange(DeclType.Loc, DeclType.EndLoc);
2155b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2156d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  S.Diag(DeclType.Loc,
2157d64effc4e31044c05d6e4400150edb26e914983aRichard Smith         FTI.NumArgs ? diag::warn_parens_disambiguated_as_function_declaration
2158d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                     : diag::warn_empty_parens_are_function_decl)
2159d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    << ParenRange;
2160d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2161d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // If the declaration looks like:
2162d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   T var1,
2163d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  //   f();
2164d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // and name lookup finds a function named 'f', then the ',' was
2165d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  // probably intended to be a ';'.
2166d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (!D.isFirstDeclarator() && D.getIdentifier()) {
2167d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Comma(D.getCommaLoc(), S.SourceMgr);
2168d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    FullSourceLoc Name(D.getIdentifierLoc(), S.SourceMgr);
2169d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    if (Comma.getFileID() != Name.getFileID() ||
2170d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) {
2171d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      LookupResult Result(S, D.getIdentifier(), SourceLocation(),
2172d64effc4e31044c05d6e4400150edb26e914983aRichard Smith                          Sema::LookupOrdinaryName);
2173d64effc4e31044c05d6e4400150edb26e914983aRichard Smith      if (S.LookupName(Result, S.getCurScope()))
2174d64effc4e31044c05d6e4400150edb26e914983aRichard Smith        S.Diag(D.getCommaLoc(), diag::note_empty_parens_function_call)
2175d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << FixItHint::CreateReplacement(D.getCommaLoc(), ";")
2176d64effc4e31044c05d6e4400150edb26e914983aRichard Smith          << D.getIdentifier();
2177d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    }
2178d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  }
2179b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
2180d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  if (FTI.NumArgs > 0) {
2181d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration with parameters, eg. "T var(T());", suggest adding parens
2182d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // around the first parameter to turn the declaration into a variable
2183d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2184b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceRange Range = FTI.ArgInfo[0].Param->getSourceRange();
2185b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation B = Range.getBegin();
2186b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    SourceLocation E = S.PP.getLocForEndOfToken(Range.getEnd());
2187b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // FIXME: Maybe we should suggest adding braces instead of parens
2188b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // in C++11 for classes that don't have an initializer_list constructor.
2189b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    S.Diag(B, diag::note_additional_parens_for_variable_declaration)
2190b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(B, "(")
2191b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      << FixItHint::CreateInsertion(E, ")");
2192d64effc4e31044c05d6e4400150edb26e914983aRichard Smith  } else {
2193d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // For a declaration without parameters, eg. "T var();", suggest replacing the
2194d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // parens with an initializer to turn the declaration into a variable
2195d64effc4e31044c05d6e4400150edb26e914983aRichard Smith    // declaration.
2196b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
2197d64effc4e31044c05d6e4400150edb26e914983aRichard Smith
2198b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // Empty parens mean value-initialization, and no parens mean
2199b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // default initialization. These are equivalent if the default
2200b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // constructor is user-provided or if zero-initialization is a
2201b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    // no-op.
2202b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    if (RD && RD->hasDefinition() &&
2203b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        (RD->isEmpty() || RD->hasUserProvidedDefaultConstructor()))
2204b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor)
2205b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        << FixItHint::CreateRemoval(ParenRange);
2206b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    else {
2207b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      std::string Init = S.getFixItZeroInitializerForType(RT);
220880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      if (Init.empty() && S.LangOpts.CPlusPlus11)
2209b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        Init = "{}";
2210b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (!Init.empty())
2211b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        S.Diag(DeclType.Loc, diag::note_empty_parens_zero_initialize)
2212b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith          << FixItHint::CreateReplacement(ParenRange, Init);
2213b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith    }
2214b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith  }
2215b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith}
2216b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
22178cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidisstatic TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
22188cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                QualType declSpecType,
22198cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                                TypeSourceInfo *TInfo) {
22208cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
22218cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = declSpecType;
22228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Declarator &D = state.getDeclarator();
22238cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  Sema &S = state.getSema();
22248cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  ASTContext &Context = S.Context;
22254e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &LangOpts = S.getLangOpts();
22268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
2227cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  // The name we're declaring, if any.
2228cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  DeclarationName Name;
2229cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor  if (D.getIdentifier())
2230cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    Name = D.getIdentifier();
22311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2232162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  // Does this declaration declare a typedef-name?
2233162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  bool IsTypedefName =
2234162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef ||
22353e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasDeclContext ||
22363e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    D.getContext() == Declarator::AliasTemplateContext;
2237162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2238d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  // Does T refer to a function type with a cv-qualifier or a ref-qualifier?
2239d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith  bool IsQualifiedFunction = T->isFunctionProtoType() &&
2240d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      (T->castAs<FunctionProtoType>()->getTypeQuals() != 0 ||
2241d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith       T->castAs<FunctionProtoType>()->getRefQualifier() != RQ_None);
2242d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
224398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // Walk the DeclTypeInfo, building the recursive type as we go.
224498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // DeclTypeInfos are ordered from the identifier out, which is
224598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  // opposite of what we want :).
22468ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2247711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned chunkIndex = e - i - 1;
2248711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    state.setCurrentChunkIndex(chunkIndex);
2249711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
2250d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction) {
2251d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      checkQualifiedFunction(S, T, DeclType);
2252d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = DeclType.Kind == DeclaratorChunk::Paren;
2253d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    }
22545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (DeclType.Kind) {
2255075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    case DeclaratorChunk::Paren:
22568cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildParenType(T);
2257075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      break;
22585618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    case DeclaratorChunk::BlockPointer:
22599af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      // If blocks are disabled, emit an error.
22609af5500f3f132f9a2f9abbe82113a7c7bb751472Chris Lattner      if (!LangOpts.Blocks)
22618cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_blocks_disable);
22621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22638cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
22642865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Cls.TypeQuals)
22658cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
22665618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      break;
22675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Pointer:
22686a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a pointer to pointer to function with
22696a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
22708cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
22718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
22726a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
22736a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
22746a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
22758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.ObjC1 && T->getAs<ObjCObjectType>()) {
2276c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        T = Context.getObjCObjectPointerType(T);
22772865474261a608c7873b87ba4af110d17907896dJohn McCall        if (DeclType.Ptr.TypeQuals)
22788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
227914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        break;
228014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
22818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildPointerType(T, DeclType.Loc, Name);
22822865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ptr.TypeQuals)
22838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
2284711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
22855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
22860953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    case DeclaratorChunk::Reference: {
22876a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building a reference to pointer to function with
22886a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
22898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
22908cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
22916a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
22926a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
22936a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
22948cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
22952865474261a608c7873b87ba4af110d17907896dJohn McCall
22962865474261a608c7873b87ba4af110d17907896dJohn McCall      Qualifiers Quals;
22972865474261a608c7873b87ba4af110d17907896dJohn McCall      if (DeclType.Ref.HasRestrict)
22988cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
22995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
23000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
23015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case DeclaratorChunk::Array: {
23026a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // Verify that we're not building an array of pointers to function with
23036a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      // exception specification.
23048cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
23058cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
23066a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        D.setInvalidType(true);
23076a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl        // Build the type anyway.
23086a7330c20cabf1cf1cd46f5dfc183ec3a72add66Sebastian Redl      }
2309fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
231094f81fd0b0f81a99d215b225c8c5616295b063f6Chris Lattner      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
23115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ArrayType::ArraySizeModifier ASM;
23125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (ATI.isStar)
23135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Star;
23145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else if (ATI.hasStatic)
23155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Static;
23165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      else
23175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ASM = ArrayType::Normal;
2318c05a94b7accd4035bf5d5897c434c445b22da855John McCall      if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
2319f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // FIXME: This check isn't quite right: it allows star in prototypes
2320f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // for function definitions, and disallows some edge cases detailed
2321f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
23228cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
2323f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        ASM = ArrayType::Normal;
2324f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman        D.setInvalidType(true);
2325f91f5c8a66ffd812f61819836529f8ad437f7e2bEli Friedman      }
23267f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
23277f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // C99 6.7.5.2p1: The optional type qualifiers and the keyword static
23287f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // shall appear only in a declaration of a function parameter with an
23297f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      // array type, ...
23307f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      if (ASM == ArrayType::Static || ATI.TypeQuals) {
233199570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay        if (!(D.isPrototypeContext() ||
233299570a58b09fca5d0b328733ab8b6717a1a04f4aMatt Beaumont-Gay              D.getContext() == Declarator::KNRTypeListContext)) {
23337f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          S.Diag(DeclType.Loc, diag::err_array_static_outside_prototype) <<
23347f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
23357f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Remove the 'static' and the type qualifiers.
23367f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          if (ASM == ArrayType::Static)
23377f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ASM = ArrayType::Normal;
23387f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          ATI.TypeQuals = 0;
23397f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          D.setInvalidType(true);
23407f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
23417f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
23427f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // C99 6.7.5.2p1: ... and then only in the outermost array type
23437f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        // derivation.
23447f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        unsigned x = chunkIndex;
23457f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        while (x != 0) {
23467f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          // Walk outwards along the declarator chunks.
23477f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          x--;
23487f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          const DeclaratorChunk &DC = D.getTypeObject(x);
23497f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          switch (DC.Kind) {
23507f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Paren:
23517f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            continue;
23527f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Array:
23537f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Pointer:
23547f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Reference:
23557f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::MemberPointer:
23567f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            S.Diag(DeclType.Loc, diag::err_array_static_not_outermost) <<
23577f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
23587f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            if (ASM == ArrayType::Static)
23597f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg              ASM = ArrayType::Normal;
23607f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            ATI.TypeQuals = 0;
23617f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            D.setInvalidType(true);
23627f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
23637f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::Function:
23647f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          case DeclaratorChunk::BlockPointer:
23657f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            // These are invalid anyway, so just ignore.
23667f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg            break;
23677f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg          }
23687f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg        }
23697f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg      }
23707f397c5d97fc0c11333d939f2c07bc3b230956e2Hans Wennborg
23718ac2c66a1442985091c5ec2b33ce6d3df3bcb529Eli Friedman      T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
23728cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                           SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
23735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
23745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2375f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::Function: {
23765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If the function declarator has a prototype (i.e. it is not () and
23775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // does not have a K&R-style identifier list), then the arguments are part
23785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // of the type, otherwise the argument list is ().
23795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2380d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      IsQualifiedFunction = FTI.TypeQuals || FTI.hasRefQualifier();
23813cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
238234b41d939a1328f484511c6002ba2456db879a29Richard Smith      // Check for auto functions and trailing return type and adjust the
238334b41d939a1328f484511c6002ba2456db879a29Richard Smith      // return type accordingly.
238434b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!D.isInvalidType()) {
238534b41d939a1328f484511c6002ba2456db879a29Richard Smith        // trailing-return-type is only required if we're declaring a function,
238634b41d939a1328f484511c6002ba2456db879a29Richard Smith        // and not, for instance, a pointer to a function.
238734b41d939a1328f484511c6002ba2456db879a29Richard Smith        if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
238854655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            !FTI.hasTrailingReturnType() && chunkIndex == 0) {
23898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
239034b41d939a1328f484511c6002ba2456db879a29Richard Smith               diag::err_auto_missing_trailing_return);
239134b41d939a1328f484511c6002ba2456db879a29Richard Smith          T = Context.IntTy;
239234b41d939a1328f484511c6002ba2456db879a29Richard Smith          D.setInvalidType(true);
239354655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        } else if (FTI.hasTrailingReturnType()) {
2394e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          // T must be exactly 'auto' at this point. See CWG issue 681.
2395e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          if (isa<ParenType>(T)) {
23968cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
2397e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith                 diag::err_trailing_return_in_parens)
2398e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith              << T << D.getDeclSpec().getSourceRange();
2399e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.setInvalidType(true);
2400f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman          } else if (D.getContext() != Declarator::LambdaExprContext &&
2401f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman                     (T.hasQualifiers() || !isa<AutoType>(T))) {
24028cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
240334b41d939a1328f484511c6002ba2456db879a29Richard Smith                 diag::err_trailing_return_without_auto)
240434b41d939a1328f484511c6002ba2456db879a29Richard Smith              << T << D.getDeclSpec().getSourceRange();
240534b41d939a1328f484511c6002ba2456db879a29Richard Smith            D.setInvalidType(true);
240634b41d939a1328f484511c6002ba2456db879a29Richard Smith          }
240754655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          T = S.GetTypeFromParser(FTI.getTrailingReturnType(), &TInfo);
240854655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          if (T.isNull()) {
240954655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            // An error occurred parsing the trailing return type.
241054655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            T = Context.IntTy;
241154655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith            D.setInvalidType(true);
241254655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith          }
241334b41d939a1328f484511c6002ba2456db879a29Richard Smith        }
241434b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
241534b41d939a1328f484511c6002ba2456db879a29Richard Smith
2416e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // C99 6.7.5.3p1: The return type may not be a function or array type.
2417e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      // For conversion functions, we'll diagnose this particular error later.
2418e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      if ((T->isArrayType() || T->isFunctionType()) &&
2419e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
2420e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        unsigned diagID = diag::err_func_returning_array_function;
2421e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // Last processing chunk in block context means this function chunk
2422e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        // represents the block.
2423e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        if (chunkIndex == 0 &&
2424e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith            D.getContext() == Declarator::BlockLiteralContext)
2425e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith          diagID = diag::err_block_returning_array_function;
24268cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
2427e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        T = Context.IntTy;
2428e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith        D.setInvalidType(true);
2429e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith      }
2430e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2431aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Do not allow returning half FP value.
2432aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // FIXME: This really should be in BuildFunctionType.
2433aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      if (T->isHalfType()) {
2434aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        S.Diag(D.getIdentifierLoc(),
2435aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov             diag::err_parameters_retval_cannot_have_fp16_type) << 1
2436aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov          << FixItHint::CreateInsertion(D.getIdentifierLoc(), "*");
2437aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov        D.setInvalidType(true);
2438aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      }
2439aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
24405291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // cv-qualifiers on return types are pointless except when the type is a
24415291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      // class type in C++.
2442fff951371dfc309160a99d423e43a7841aeb35aaDouglas Gregor      if (isa<PointerType>(T) && T.getLocalCVRQualifiers() &&
24431e15394853bfae25112d9cc6b445504905e1f34aRafael Espindola          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId) &&
24448cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          (!LangOpts.CPlusPlus || !T->isDependentType())) {
2445d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?");
2446d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
2447d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer);
2448d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2449d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr;
2450d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2451d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DiagnoseIgnoredQualifiers(PTI.TypeQuals,
2452d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
2453d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
2454d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth            SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
24558cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S);
2456d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2457d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth      } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
24588cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          (!LangOpts.CPlusPlus ||
24595291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor           (!T->isDependentType() && !T->isRecordType()))) {
2460d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
2461d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth        DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(),
2462d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getConstSpecLoc(),
2463d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getVolatileSpecLoc(),
2464d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth                                  D.getDeclSpec().getRestrictSpecLoc(),
24658cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                                  S);
24665291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor      }
2467d067c07c6cbf099b25aba38bcb66f38e79d0c420Chandler Carruth
24688cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (LangOpts.CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
2469402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        // C++ [dcl.fct]p6:
2470402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor        //   Types shall not be defined in return or parameter types.
2471b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
24725e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall        if (Tag->isCompleteDefinition())
24738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
2474402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor            << Context.getTypeDeclType(Tag);
2475402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor      }
2476402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
24773cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // Exception specs are not allowed in typedefs. Complain, but add it
24783cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl      // anyway.
2479162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (IsTypedefName && FTI.getExceptionSpecType())
24808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef)
24813e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          << (D.getContext() == Declarator::AliasDeclContext ||
24823e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith              D.getContext() == Declarator::AliasTemplateContext);
24833cc9726a493d90bd8faf094986a59352fd3461cbSebastian Redl
2484b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // If we see "T var();" or "T var(T());" at block scope, it is probably
2485b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      // an attempt to initialize a variable, not a function declaration.
2486b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith      if (FTI.isAmbiguous)
2487b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith        warnAboutAmbiguousFunction(S, D, DeclType, T);
2488b9c6261d02f688d0a9a36b736ad5956fbc737854Richard Smith
24898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      if (!FTI.NumArgs && !FTI.isVariadic && !LangOpts.CPlusPlus) {
24902865474261a608c7873b87ba4af110d17907896dJohn McCall        // Simple void foo(), where the incoming T is the result type.
24912865474261a608c7873b87ba4af110d17907896dJohn McCall        T = Context.getFunctionNoProtoType(T);
24922865474261a608c7873b87ba4af110d17907896dJohn McCall      } else {
24932865474261a608c7873b87ba4af110d17907896dJohn McCall        // We allow a zero-parameter variadic function in C if the
24942865474261a608c7873b87ba4af110d17907896dJohn McCall        // function is marked with the "overloadable" attribute. Scan
24952865474261a608c7873b87ba4af110d17907896dJohn McCall        // for this attribute now.
24968cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        if (!FTI.NumArgs && FTI.isVariadic && !LangOpts.CPlusPlus) {
2497965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          bool Overloadable = false;
2498965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          for (const AttributeList *Attrs = D.getAttributes();
2499965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor               Attrs; Attrs = Attrs->getNext()) {
25008e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt            if (Attrs->getKind() == AttributeList::AT_Overloadable) {
2501965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              Overloadable = true;
2502965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor              break;
2503965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor            }
2504965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          }
2505965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor
2506965acbb321e94e36aa5365126eee46b97745fdbbDouglas Gregor          if (!Overloadable)
25078cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis            S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
2508c6f7345e44e079f373d6bdecaa06c7e06574dc27Argyrios Kyrtzidis        }
25092865474261a608c7873b87ba4af110d17907896dJohn McCall
25102865474261a608c7873b87ba4af110d17907896dJohn McCall        if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
2511788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
2512788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner          // definition.
25138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          S.Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
25142865474261a608c7873b87ba4af110d17907896dJohn McCall          D.setInvalidType(true);
25152865474261a608c7873b87ba4af110d17907896dJohn McCall          break;
25162865474261a608c7873b87ba4af110d17907896dJohn McCall        }
25172865474261a608c7873b87ba4af110d17907896dJohn McCall
2518e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI;
2519e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.Variadic = FTI.isVariadic;
252054655be65585ed6618fdd7a19fa6c70efc321d3aRichard Smith        EPI.HasTrailingReturn = FTI.hasTrailingReturnType();
2521e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.TypeQuals = FTI.TypeQuals;
2522c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
2523c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : FTI.RefQualifierIsLValueRef? RQ_LValue
2524c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                    : RQ_RValue;
252591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
25265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Otherwise, we have a function with an argument list that is
25275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // potentially variadic.
25285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 16> ArgTys;
25292865474261a608c7873b87ba4af110d17907896dJohn McCall        ArgTys.reserve(FTI.NumArgs);
25301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25315f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<bool, 16> ConsumedArguments;
2532f85e193739c953358c865005855253af4f68a497John McCall        ConsumedArguments.reserve(FTI.NumArgs);
2533f85e193739c953358c865005855253af4f68a497John McCall        bool HasAnyConsumedArguments = false;
2534f85e193739c953358c865005855253af4f68a497John McCall
25355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
2536d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall          ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
25378123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner          QualType ArgTy = Param->getType();
253878c75fb3d275079c5fab30eeb33077958f2b0265Chris Lattner          assert(!ArgTy.isNull() && "Couldn't parse type?");
25392dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
25402dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          // Adjust the parameter type.
254191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier          assert((ArgTy == Context.getAdjustedParameterType(ArgTy)) &&
254279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor                 "Unadjusted type?");
25432dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
25445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Look for 'void'.  void is allowed only as a single argument to a
25455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // function with no other parameters (C99 6.7.5.3p10).  We record
254672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor          // int(void) as a FunctionProtoType with an empty argument list.
25472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor          if (ArgTy->isVoidType()) {
25485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // If this is something like 'float(int, void)', reject it.  'void'
25495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // is an incomplete type (C99 6.2.5p19) and function decls cannot
25505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // have arguments of incomplete type.
25515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (FTI.NumArgs != 1 || FTI.isVariadic) {
25528cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(DeclType.Loc, diag::err_void_only_param);
25532ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
25548123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
25552ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else if (FTI.ArgInfo[i].Ident) {
25562ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'int(void abc)'.
25578cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis              S.Diag(FTI.ArgInfo[i].IdentLoc,
25584565d4e83cec55356fe9c75929579eacced9da36Chris Lattner                   diag::err_param_with_void_type);
25592ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              ArgTy = Context.IntTy;
25608123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner              Param->setType(ArgTy);
25612ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            } else {
25622ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Reject, but continue to parse 'float(const void)'.
25630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall              if (ArgTy.hasQualifiers())
25648cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                S.Diag(DeclType.Loc, diag::err_void_param_qualified);
25651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25662ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              // Do not add 'void' to the ArgTys list.
25672ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner              break;
25682ff5426cd83ae02378efacdfeb70d6785eb09a30Chris Lattner            }
2569aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov          } else if (ArgTy->isHalfType()) {
2570aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // Disallow half FP arguments.
2571aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            // FIXME: This really should be in BuildFunctionType.
2572aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            S.Diag(Param->getLocation(),
2573aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov               diag::err_parameters_retval_cannot_have_fp16_type) << 0
2574aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            << FixItHint::CreateInsertion(Param->getLocation(), "*");
2575aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov            D.setInvalidType();
2576eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman          } else if (!FTI.hasPrototype) {
2577eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            if (ArgTy->isPromotableIntegerType()) {
2578a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman              ArgTy = Context.getPromotedIntegerType(ArgTy);
2579eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              Param->setKNRPromoted(true);
2580183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall            } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
2581eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              if (BTy->getKind() == BuiltinType::Float) {
2582eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman                ArgTy = Context.DoubleTy;
2583eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall                Param->setKNRPromoted(true);
2584eecf5fa12d5426637c47d7072f0c193a8d7ff68bJohn McCall              }
2585eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman            }
25865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
258756a965c0f77c9e6bffd65cc8f8796442a8527381Fariborz Jahanian
25888cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis          if (LangOpts.ObjCAutoRefCount) {
2589f85e193739c953358c865005855253af4f68a497John McCall            bool Consumed = Param->hasAttr<NSConsumedAttr>();
2590f85e193739c953358c865005855253af4f68a497John McCall            ConsumedArguments.push_back(Consumed);
2591f85e193739c953358c865005855253af4f68a497John McCall            HasAnyConsumedArguments |= Consumed;
2592f85e193739c953358c865005855253af4f68a497John McCall          }
2593f85e193739c953358c865005855253af4f68a497John McCall
259454e14c4db764c0636160d26c5bbf491637c83a76John McCall          ArgTys.push_back(ArgTy);
25955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2596465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2597f85e193739c953358c865005855253af4f68a497John McCall        if (HasAnyConsumedArguments)
2598f85e193739c953358c865005855253af4f68a497John McCall          EPI.ConsumedArguments = ConsumedArguments.data();
2599f85e193739c953358c865005855253af4f68a497John McCall
26005f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 4> Exceptions;
260174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<ParsedType, 2> DynamicExceptions;
260274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        SmallVector<SourceRange, 2> DynamicExceptionRanges;
260374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        Expr *NoexceptExpr = 0;
260491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
26058b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        if (FTI.getExceptionSpecType() == EST_Dynamic) {
260674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // FIXME: It's rather inefficient to have to split into two vectors
260774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          // here.
260874e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          unsigned N = FTI.NumExceptions;
260974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptions.reserve(N);
261074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          DynamicExceptionRanges.reserve(N);
261174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          for (unsigned I = 0; I != N; ++I) {
261274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptions.push_back(FTI.Exceptions[I].Ty);
261374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor            DynamicExceptionRanges.push_back(FTI.Exceptions[I].Range);
2614e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall          }
26158b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl        } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
261674e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor          NoexceptExpr = FTI.NoexceptExpr;
261774e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        }
261891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
261974e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor        S.checkExceptionSpecification(FTI.getExceptionSpecType(),
262074e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptions,
262174e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      DynamicExceptionRanges,
262274e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      NoexceptExpr,
262374e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      Exceptions,
262474e2fc332e07c76d4e69ccbd0e9e47a0bafd3908Douglas Gregor                                      EPI);
262591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2626e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI);
26275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
262804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
26295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
26305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2631f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    case DeclaratorChunk::MemberPointer:
2632f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      // The scope spec must refer to a class, or be dependent.
26337bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      CXXScopeSpec &SS = DeclType.Mem.Scope();
2634f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      QualType ClsType;
26357bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara      if (SS.isInvalid()) {
2636edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        // Avoid emitting extra errors if we already errored on the scope.
2637edc287751a4b05e3b4d8ff2b38fa30c5b59a548bJeffrey Yasskin        D.setInvalidType(true);
26388cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      } else if (S.isDependentScopeSpecifier(SS) ||
26398cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis                 dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) {
26401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        NestedNameSpecifier *NNS
26417bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
264287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
264387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        switch (NNS->getKind()) {
264487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Identifier:
26457bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara          ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
26464a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                                 NNS->getAsIdentifier());
264787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
264887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor
264987c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Namespace:
265014aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor        case NestedNameSpecifier::NamespaceAlias:
265187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::Global:
26529f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin          llvm_unreachable("Nested-name-specifier must name a type");
26537bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara
265487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpec:
265587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        case NestedNameSpecifier::TypeSpecWithTemplate:
265687c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          ClsType = QualType(NNS->getAsType(), 0);
265791ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Note: if the NNS has a prefix and ClsType is a nondependent
265891ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // TemplateSpecializationType, then the NNS prefix is NOT included
265991ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // in ClsType; hence we wrap ClsType into an ElaboratedType.
266091ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // NOTE: in particular, no wrap occurs if ClsType already is an
266191ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          // Elaborated, DependentName, or DependentTemplateSpecialization.
266291ce2c4484e56cdc8068cebaaf2bb42362b0e1a6Abramo Bagnara          if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
26637bd067635df79f6ce4b9ffee394ea6d9e86e4290Abramo Bagnara            ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
266487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor          break;
266587c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor        }
2666f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      } else {
26678cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        S.Diag(DeclType.Mem.Scope().getBeginLoc(),
2668949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor             diag::err_illegal_decl_mempointer_in_nonclass)
2669949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
2670949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor          << DeclType.Mem.Scope().getRange();
2671f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        D.setInvalidType(true);
2672f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2673f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2674949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (!ClsType.isNull())
26758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
2676949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor      if (T.isNull()) {
2677f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl        T = Context.IntTy;
2678949bf69136e07fb7968d84bc21d9272ff343ffdbDouglas Gregor        D.setInvalidType(true);
26792865474261a608c7873b87ba4af110d17907896dJohn McCall      } else if (DeclType.Mem.TypeQuals) {
26808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis        T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
2681f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      }
2682f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      break;
2683f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    }
2684f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2685cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    if (T.isNull()) {
2686cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      D.setInvalidType(true);
2687cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor      T = Context.IntTy;
2688cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor    }
2689cd281c3ded486ced5aad29dd7c3fa22b7514c3d8Douglas Gregor
2690c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    // See if there are any attributes on this declarator chunk.
2691711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
2692f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclChunk, attrs);
26935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2694971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
26958cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (LangOpts.CPlusPlus && T->isFunctionType()) {
2696183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
2697778ed741de8ada0049b89608af0abdb5ae6e106eChris Lattner    assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
2698971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis
269991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    // C++ 8.3.5p4:
2700708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   A cv-qualifier-seq shall only be part of the function type
2701708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   for a nonstatic member function, the function type to which a pointer
2702708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   to member refers, or the top-level function type of a function typedef
2703708f3b8e350a5c0605889a4f32b26686864495caDouglas Gregor    //   declaration.
2704683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    //
2705683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // Core issue 547 also allows cv-qualifiers on function types that are
2706683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    // top-level template type arguments.
2707613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    bool FreeFunction;
2708613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    if (!D.getCXXScopeSpec().isSet()) {
2709906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman      FreeFunction = ((D.getContext() != Declarator::MemberContext &&
2710906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman                       D.getContext() != Declarator::LambdaExprContext) ||
2711613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall                      D.getDeclSpec().isFriendSpecified());
2712613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    } else {
27138cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec());
2714613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall      FreeFunction = (DC && !DC->isRecord());
2715613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall    }
2716613ef3d4c144f8c35224daf28a187426d2044aeeJohn McCall
2717d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // C++11 [dcl.fct]p6 (w/DR1417):
2718d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // An attempt to specify a function type with a cv-qualifier-seq or a
2719d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    // ref-qualifier (including by typedef-name) is ill-formed unless it is:
2720d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type for a non-static member function,
2721d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the function type to which a pointer to member refers,
2722d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the top-level function type of a function typedef declaration or
2723d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //    alias-declaration,
2724d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id in the default argument of a type-parameter, or
2725d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    //  - the type-id of a template-argument for a type-parameter
2726d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith    if (IsQualifiedFunction &&
2727d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !(!FreeFunction &&
2728d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) &&
2729d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        !IsTypedefName &&
2730d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        D.getContext() != Declarator::TemplateTypeArgContext) {
273196a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      SourceLocation Loc = D.getLocStart();
2732d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      SourceRange RemovalRange;
2733d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      unsigned I;
2734d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      if (D.isFunctionDeclarator(I)) {
2735d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        SmallVector<SourceLocation, 4> RemovalLocs;
2736d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        const DeclaratorChunk &Chunk = D.getTypeObject(I);
2737d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        assert(Chunk.Kind == DeclaratorChunk::Function);
2738d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.hasRefQualifier())
2739d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getRefQualifierLoc());
2740d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Const)
2741d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getConstQualifierLoc());
2742d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (Chunk.Fun.TypeQuals & Qualifiers::Volatile)
2743d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalLocs.push_back(Chunk.Fun.getVolatileQualifierLoc());
2744d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        // FIXME: We do not track the location of the __restrict qualifier.
2745d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //if (Chunk.Fun.TypeQuals & Qualifiers::Restrict)
2746d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        //  RemovalLocs.push_back(Chunk.Fun.getRestrictQualifierLoc());
2747d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        if (!RemovalLocs.empty()) {
2748d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          std::sort(RemovalLocs.begin(), RemovalLocs.end(),
2749aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                    BeforeThanCompare<SourceLocation>(S.getSourceManager()));
2750d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          RemovalRange = SourceRange(RemovalLocs.front(), RemovalLocs.back());
2751d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith          Loc = RemovalLocs.front();
2752683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor        }
2753683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor      }
2754d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2755d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      S.Diag(Loc, diag::err_invalid_qualified_function_type)
2756d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FreeFunction << D.isFunctionDeclarator() << T
2757d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << getFunctionQualifiersAsString(FnTy)
2758d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith        << FixItHint::CreateRemoval(RemovalRange);
2759d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
2760d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      // Strip the cv-qualifiers and ref-qualifiers from the type.
2761d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
2762d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.TypeQuals = 0;
2763d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith      EPI.RefQualifier = RQ_None;
2764d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith
276591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      T = Context.getFunctionType(FnTy->getResultType(),
2766d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                  FnTy->arg_type_begin(),
2767d37b360bf9f954af119c9805fdc79ab9d30e06c6Richard Smith                                  FnTy->getNumArgs(), EPI);
2768e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      // Rebuild any parens around the identifier in the function type.
2769e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2770e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        if (D.getTypeObject(i).Kind != DeclaratorChunk::Paren)
2771e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith          break;
2772e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith        T = S.BuildParenType(T);
2773e925322569cb4aad26cc62036a13e2d3daed862dRichard Smith      }
2774971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis    }
2775971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis  }
27761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2777711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Apply any undistributed attributes from the declarator.
2778711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull())
2779711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (AttributeList *attrs = D.getAttributes())
2780f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      processTypeAttrs(state, T, TAL_DeclName, attrs);
2781711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2782711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Diagnose any ignored type attributes.
2783711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
2784711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2785148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  // C++0x [dcl.constexpr]p9:
2786148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  //  A constexpr specifier used in an object declaration declares the object
278791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //  as const.
2788148f1f7936afd718bac7be95089e77673e43f16fPeter Collingbourne  if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
2789737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl    T.addConst();
2790737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl  }
2791737801257f795632175517ffce4a80c62fc7bff7Sebastian Redl
279291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // If there was an ellipsis in the declarator, the declaration declares a
2793a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // parameter pack whose type may be a pack expansion type.
2794a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (D.hasEllipsis() && !T.isNull()) {
2795a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    // C++0x [dcl.fct]p13:
279691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    //   A declarator-id or abstract-declarator containing an ellipsis shall
2797a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   only be used in a parameter-declaration. Such a parameter-declaration
2798a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    //   is a parameter pack (14.5.3). [...]
2799a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    switch (D.getContext()) {
2800a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::PrototypeContext:
2801a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [dcl.fct]p13:
280291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   [...] When it is part of a parameter-declaration-clause, the
280391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   parameter pack is a function parameter pack (14.5.3). The type T
2804a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   of the declarator-id of the function parameter pack shall contain
280591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   a template parameter pack; each template parameter pack in T is
2806a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   expanded by the function parameter pack.
2807a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2808a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // We represent function parameter packs as function parameters whose
2809a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // type is a pack expansion.
2810a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (!T->containsUnexpandedParameterPack()) {
281191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier        S.Diag(D.getEllipsisLoc(),
2812a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor             diag::err_function_parameter_pack_without_parameter_packs)
2813a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor          << T <<  D.getSourceRange();
2814a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor        D.setEllipsisLoc(SourceLocation());
2815a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      } else {
2816cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
2817a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      }
2818a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
281991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2820a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TemplateParamContext:
2821a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // C++0x [temp.param]p15:
282291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      //   If a template-parameter is a [...] is a parameter-declaration that
2823a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   declares a parameter pack (8.3.5), then the template-parameter is a
2824a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //   template parameter pack (14.5.3).
2825a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      //
2826a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // Note: core issue 778 clarifies that, if there are any unexpanded
2827a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // parameter packs in the type of the non-type template parameter, then
2828a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // it expands those parameter packs.
2829a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      if (T->containsUnexpandedParameterPack())
2830cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
2831e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith      else
2832e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith        S.Diag(D.getEllipsisLoc(),
283380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith               LangOpts.CPlusPlus11
2834e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 ? diag::warn_cxx98_compat_variadic_templates
2835e5acd13f885ac95d0f2dafda245625b8190235acRichard Smith                 : diag::ext_variadic_templates);
2836a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
283791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
2838a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::FileContext:
2839a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::KNRTypeListContext:
2840cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCParameterContext:  // FIXME: special diagnostic here?
2841cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    case Declarator::ObjCResultContext:     // FIXME: special diagnostic here?
2842a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::TypeNameContext:
28430b8c98f3ddf83adcb9e9d98b68ce38e970cdee73Argyrios Kyrtzidis    case Declarator::CXXNewContext:
2844162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case Declarator::AliasDeclContext:
28453e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case Declarator::AliasTemplateContext:
2846a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::MemberContext:
2847a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockContext:
2848a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ForContext:
2849a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::ConditionContext:
2850a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::CXXCatchContext:
285117b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis    case Declarator::ObjCCatchContext:
2852a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    case Declarator::BlockLiteralContext:
2853f88c400085eac7068399d0a01dbad89f8c579f07Eli Friedman    case Declarator::LambdaExprContext:
28547796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    case Declarator::TrailingReturnContext:
2855683a81f4373cf1fa9d41a751dca6f7c36125b058Douglas Gregor    case Declarator::TemplateTypeArgContext:
2856a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // FIXME: We may want to allow parameter packs in block-literal contexts
2857a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      // in the future.
28588cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis      S.Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
2859a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      D.setEllipsisLoc(SourceLocation());
2860a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor      break;
2861a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    }
2862a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
2863e7397c6a1bb2b205c5fe678e26199eb26d22e38eRichard Smith
2864bf1a028246d884a540aeafa38e89be59a269b072John McCall  if (T.isNull())
2865bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getNullTypeSourceInfo();
2866bf1a028246d884a540aeafa38e89be59a269b072John McCall  else if (D.isInvalidType())
2867bf1a028246d884a540aeafa38e89be59a269b072John McCall    return Context.getTrivialTypeSourceInfo(T);
2868db7abf78dedc2ef6ccb42b3dac6ab330fe2ea469Argyrios Kyrtzidis
28698cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return S.GetTypeSourceInfoForDeclarator(D, T, TInfo);
28708cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis}
28718cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
28728cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// GetTypeForDeclarator - Convert the type for the specified
28738cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// declarator to Type instances.
28748cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis///
28758cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// The result of this call will never be null, but the associated
28768cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis/// type may be a null type if there's an unrecoverable error.
28778cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
28788cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // Determine the type of the declarator. Not all forms of declarator
28798cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  // have a type.
28808cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
28818cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeProcessingState state(*this, D);
28828cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
28838cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
28848cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
28858cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  if (T.isNull())
28868cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
28878cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis
28884e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (D.isPrototypeContext() && getLangOpts().ObjCAutoRefCount)
28898cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis    inferARCWriteback(state, T);
289091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
28918cfa57b348d4d5a58d92764a60280bf88e4e49aeArgyrios Kyrtzidis  return GetFullTypeForDeclarator(state, T, ReturnTypeInfo);
28925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
28935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
289431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclSpec(Sema &S,
289531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           QualType &declSpecTy,
289631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                           Qualifiers::ObjCLifetime ownership) {
289731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy->isObjCRetainableType() &&
289831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) {
289931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers qs;
290031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    qs.addObjCLifetime(ownership);
290131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    declSpecTy = S.Context.getQualifiedType(declSpecTy, qs);
290231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
290331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
290431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
290531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
290631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            Qualifiers::ObjCLifetime ownership,
290731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                            unsigned chunkIndex) {
290831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
290931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
291031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
291131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // Look for an explicit lifetime attribute.
291231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  DeclaratorChunk &chunk = D.getTypeObject(chunkIndex);
291331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (const AttributeList *attr = chunk.getAttrs(); attr;
291431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis         attr = attr->getNext())
29158e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    if (attr->getKind() == AttributeList::AT_ObjCOwnership)
291631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
291731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
291831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  const char *attrStr = 0;
291931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  switch (ownership) {
29203026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
292131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break;
292231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Strong: attrStr = "strong"; break;
292331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Weak: attrStr = "weak"; break;
292431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break;
292531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
292631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
292731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // If there wasn't one, add one (with an invalid source location
292831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // so that we don't make an AttributedType for it).
292931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  AttributeList *attr = D.getAttributePool()
293031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    .create(&S.Context.Idents.get("objc_ownership"), SourceLocation(),
293131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            /*scope*/ 0, SourceLocation(),
293231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis            &S.Context.Idents.get(attrStr), SourceLocation(),
293393f95f2a2cbb6bb3d17bfb5fc74ce1cccea751b6Sean Hunt            /*args*/ 0, 0, AttributeList::AS_GNU);
293431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  spliceAttrIntoList(*attr, chunk.getAttrListRef());
293531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
293631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  // TODO: mark whether we did this inference?
293731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
293831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
293948d798ce32447607144db70a484cdb99c1180663Benjamin Kramer/// \brief Used for transferring ownership in casts resulting in l-values.
294031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidisstatic void transferARCOwnership(TypeProcessingState &state,
294131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 QualType &declSpecTy,
294231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis                                 Qualifiers::ObjCLifetime ownership) {
294331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Sema &S = state.getSema();
294431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  Declarator &D = state.getDeclarator();
294531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
294631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  int inner = -1;
29476ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis  bool hasIndirection = false;
294831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
294931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    DeclaratorChunk &chunk = D.getTypeObject(i);
295031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    switch (chunk.Kind) {
295131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Paren:
295231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      // Ignore parens.
295331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
295431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
295531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Array:
295631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Reference:
295731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Pointer:
29586ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
29596ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        hasIndirection = true;
296031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      inner = i;
296131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
296231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
296331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::BlockPointer:
29646ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      if (inner != -1)
29656ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis        transferARCOwnershipToDeclaratorChunk(state, ownership, i);
29666ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis      return;
296731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
296831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::Function:
296931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    case DeclaratorChunk::MemberPointer:
297031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return;
297131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    }
297231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
297331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
297431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (inner == -1)
29756ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    return;
297631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
297791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  DeclaratorChunk &chunk = D.getTypeObject(inner);
297831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (chunk.Kind == DeclaratorChunk::Pointer) {
297931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (declSpecTy->isObjCRetainableType())
298031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
29816ee54925f709176a33aab4727bc35bea2d05aca4Argyrios Kyrtzidis    if (declSpecTy->isObjCObjectType() && hasIndirection)
298231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return transferARCOwnershipToDeclaratorChunk(state, ownership, inner);
298331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  } else {
298431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    assert(chunk.Kind == DeclaratorChunk::Array ||
298531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis           chunk.Kind == DeclaratorChunk::Reference);
298631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
298731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
298831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
298931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
299031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisTypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) {
299131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeProcessingState state(*this, D);
299231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
299331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  TypeSourceInfo *ReturnTypeInfo = 0;
299431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
299531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  if (declSpecTy.isNull())
299631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    return Context.getNullTypeSourceInfo();
299731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
29984e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount) {
299931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy);
300031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (ownership != Qualifiers::OCL_None)
300131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      transferARCOwnership(state, declSpecTy, ownership);
300231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
300331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
300431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo);
300531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
300631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
300714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall/// Map an AttributedType::Kind to an AttributeList::Kind.
300814aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
300914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  switch (kind) {
301014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_address_space:
30118e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_AddressSpace;
301214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_regparm:
30138e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Regparm;
301414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_vector_size:
30158e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_VectorSize;
301614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_vector_type:
30178e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonVectorType;
301814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_neon_polyvector_type:
30198e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NeonPolyVectorType;
302014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_objc_gc:
30218e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCGC;
3022b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis  case AttributedType::attr_objc_ownership:
30238e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ObjCOwnership;
302414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_noreturn:
30258e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_NoReturn;
302614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_cdecl:
30278e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_CDecl;
302814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_fastcall:
30298e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_FastCall;
303014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_stdcall:
30318e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_StdCall;
303214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_thiscall:
30338e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_ThisCall;
303414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  case AttributedType::attr_pascal:
30358e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pascal;
3036414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case AttributedType::attr_pcs:
30378e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    return AttributeList::AT_Pcs;
3038263366f9241366f29ba65b703120f302490c39ffDerek Schuff  case AttributedType::attr_pnaclcall:
3039263366f9241366f29ba65b703120f302490c39ffDerek Schuff    return AttributeList::AT_PnaclCall;
304038980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei  case AttributedType::attr_inteloclbicc:
304138980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    return AttributeList::AT_IntelOclBicc;
304214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
304314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  llvm_unreachable("unexpected attribute kind!");
304414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
304514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
304614aa2175416f79ef17811282afbf425f87d54ebfJohn McCallstatic void fillAttributedTypeLoc(AttributedTypeLoc TL,
304714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                  const AttributeList *attrs) {
304814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributedType::Kind kind = TL.getAttrKind();
304914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
305014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  assert(attrs && "no type attributes in the expected location!");
305114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  AttributeList::Kind parsedKind = getAttrListKind(kind);
305214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  while (attrs->getKind() != parsedKind) {
305314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    attrs = attrs->getNext();
305414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    assert(attrs && "no matching attribute in expected location!");
305514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  }
305614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
305714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  TL.setAttrNameLoc(attrs->getLoc());
305814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrExprOperand())
305914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrExprOperand(attrs->getArg(0));
306014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  else if (TL.hasAttrEnumOperand())
306114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrEnumOperandLoc(attrs->getParameterLoc());
306214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
306314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // FIXME: preserve this information to here.
306414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (TL.hasAttrOperand())
306514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    TL.setAttrOperandParensRange(SourceRange());
306614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall}
306714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
306851bd803fbdade51d674598ed45da3d54190a656cJohn McCallnamespace {
306951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
3070c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    ASTContext &Context;
307151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclSpec &DS;
3072f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
307351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
307491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
3075c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      : Context(Context), DS(DS) {}
3076f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
307714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
307814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, DS.getAttributes().getList());
307914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      Visit(TL.getModifiedLoc());
308014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
308151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
308251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      Visit(TL.getUnqualifiedLoc());
308351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
308451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
308551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
308651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
308751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
308851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setNameLoc(DS.getTypeSpecTypeLoc());
30891de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // FIXME. We should have DS.getTypeSpecTypeEndLoc(). But, it requires
30901de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // addition field. What we have is good enough for dispay of location
30911de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      // of 'fixit' on interface name.
30921de6a6cb485fb58b4fb100282bb3cf298eedacd9Fariborz Jahanian      TL.setNameEndLoc(DS.getLocEnd());
3093c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    }
3094c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3095c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Handle the base type, which might not have been written explicitly.
3096c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
3097c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(false);
3098c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor        TL.getBaseLoc().initialize(Context, SourceLocation());
3099c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      } else {
3100c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        TL.setHasBaseTypeAsWritten(true);
3101c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Visit(TL.getBaseLoc());
3102c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      }
310354e14c4db764c0636160d26c5bbf491637c83a76John McCall
3104c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      // Protocol qualifiers.
310554e14c4db764c0636160d26c5bbf491637c83a76John McCall      if (DS.getProtocolQualifiers()) {
310654e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() > 0);
310754e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
310854e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(DS.getProtocolLAngleLoc());
310954e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(DS.getSourceRange().getEnd());
311054e14c4db764c0636160d26c5bbf491637c83a76John McCall        for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
311154e14c4db764c0636160d26c5bbf491637c83a76John McCall          TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
311254e14c4db764c0636160d26c5bbf491637c83a76John McCall      } else {
311354e14c4db764c0636160d26c5bbf491637c83a76John McCall        assert(TL.getNumProtocols() == 0);
311454e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setLAngleLoc(SourceLocation());
311554e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setRAngleLoc(SourceLocation());
311654e14c4db764c0636160d26c5bbf491637c83a76John McCall      }
311751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
311854e14c4db764c0636160d26c5bbf491637c83a76John McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
311954e14c4db764c0636160d26c5bbf491637c83a76John McCall      TL.setStarLoc(SourceLocation());
3120c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Visit(TL.getPointeeLoc());
312151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
3122833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
3123a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *TInfo = 0;
3124b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3125833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3126833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If we got no declarator info from previous Sema routines,
3127833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // just fill with the typespec loc.
3128a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!TInfo) {
31290daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara        TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
3130833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        return;
3131833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      }
3132833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3133e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      TypeLoc OldTL = TInfo->getTypeLoc();
3134e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      if (TInfo->getType()->getAs<ElaboratedType>()) {
3135e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
3136e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TemplateSpecializationTypeLoc NamedTL =
3137e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
3138e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(NamedTL);
3139e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
3140e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      else
3141e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
3142833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    }
3143cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3144cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
3145cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3146cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3147cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3148cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3149cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
3150cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3151cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setParensRange(DS.getTypeofParensRange());
3152b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      assert(DS.getRepAsType());
3153cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TypeSourceInfo *TInfo = 0;
3154b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3155cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall      TL.setUnderlyingTInfo(TInfo);
3156cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall    }
3157ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3158ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      // FIXME: This holds only because we only have one unary transform.
3159ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType);
3160ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setKWLoc(DS.getTypeSpecTypeLoc());
3161ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setParensRange(DS.getTypeofParensRange());
3162ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(DS.getRepAsType());
3163ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TypeSourceInfo *TInfo = 0;
3164ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3165ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      TL.setUnderlyingTInfo(TInfo);
3166ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
3167ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3168ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      // By default, use the source location of the type specifier.
3169ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
3170ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      if (TL.needsExtraLocalData()) {
3171ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Set info for the written builtin specifiers.
3172ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
3173ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        // Try to have a meaningful source location.
3174ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        if (TL.getWrittenSignSpec() != TSS_unspecified)
3175ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Sign spec loc overrides the others (e.g., 'unsigned long').
3176ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
3177ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        else if (TL.getWrittenWidthSpec() != TSW_unspecified)
3178ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          // Width spec loc overrides type spec loc (e.g., 'short int').
3179ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
3180ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
3181ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
3182e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3183e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      ElaboratedTypeKeyword Keyword
3184e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
3185253e80b019727451edb4cbcad71277fcbe05ff0eNico Weber      if (DS.getTypeSpecType() == TST_typename) {
3186e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        TypeSourceInfo *TInfo = 0;
3187b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3188e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        if (TInfo) {
3189e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
3190e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara          return;
3191e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara        }
3192e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      }
319338a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara      TL.setElaboratedKeywordLoc(Keyword != ETK_None
319438a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 ? DS.getTypeSpecTypeLoc()
319538a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara                                 : SourceLocation());
3196e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      const CXXScopeSpec& SS = DS.getTypeSpecScope();
31979e876876afc13aa671cc11a17c19907c599b9ab9Douglas Gregor      TL.setQualifierLoc(SS.getWithLocInContext(Context));
3198e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara      Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
3199e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3200e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
320166581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
320266581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
320366581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
320466581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
320566581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
320633500955d731c73717af52088b7fc0e7a85681e7John McCall    }
320733500955d731c73717af52088b7fc0e7a85681e7John McCall    void VisitDependentTemplateSpecializationTypeLoc(
320833500955d731c73717af52088b7fc0e7a85681e7John McCall                                 DependentTemplateSpecializationTypeLoc TL) {
320966581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(DS.getTypeSpecType() == TST_typename);
321066581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TypeSourceInfo *TInfo = 0;
321166581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
321266581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      assert(TInfo);
321366581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara      TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
321466581d41527628d4b37f7b05c288f77be7415d7dAbramo Bagnara                TInfo->getTypeLoc()));
32150daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    }
32160daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara    void VisitTagTypeLoc(TagTypeLoc TL) {
32170daaf32723ac78549c507c2a68a5300502703673Abramo Bagnara      TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
3218e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara    }
3219b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
3220b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      TL.setKWLoc(DS.getTypeSpecTypeLoc());
3221b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      TL.setParensRange(DS.getTypeofParensRange());
322291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
322343fe245b37c3cd36d837aab9eb98551328d30141Douglas Gregor      TypeSourceInfo *TInfo = 0;
322443fe245b37c3cd36d837aab9eb98551328d30141Douglas Gregor      Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
322543fe245b37c3cd36d837aab9eb98551328d30141Douglas Gregor      TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
3226b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
3227e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
322851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
322951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: add other typespec types and change this to an assert.
3230c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      TL.initialize(Context, DS.getTypeSpecTypeLoc());
323151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
323251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
3233eb66759e9a1d7c041354d132a14674b2d948059bArgyrios Kyrtzidis
323451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
3235b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    ASTContext &Context;
323651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    const DeclaratorChunk &Chunk;
3237f352bddf015e537350416c296dd2963524f554f9Argyrios Kyrtzidis
323851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  public:
3239b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
3240b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      : Context(Context), Chunk(Chunk) {}
32414adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
324251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
32439f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("qualified type locs not expected here!");
324451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
32454adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3246f85e193739c953358c865005855253af4f68a497John McCall    void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3247f85e193739c953358c865005855253af4f68a497John McCall      fillAttributedTypeLoc(TL, Chunk.getAttrs());
3248f85e193739c953358c865005855253af4f68a497John McCall    }
324951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
325051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
325151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setCaretLoc(Chunk.Loc);
32524adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
325351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitPointerTypeLoc(PointerTypeLoc TL) {
325451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
325551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
32564adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
325751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
325851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Pointer);
325951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
32604adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
326151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
326251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
3263b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const CXXScopeSpec& SS = Chunk.Mem.Scope();
3264b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
3265b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3266b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      const Type* ClsTy = TL.getClass();
3267b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      QualType ClsQT = QualType(ClsTy, 0);
3268b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
3269b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Now copy source location info into the type loc component.
3270b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TypeLoc ClsTL = ClsTInfo->getTypeLoc();
3271b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
3272b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Identifier:
3273b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
3274b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        {
3275fd9c42ed22fb4f7f865f7d8f8848df84ddf8262cAbramo Bagnara          DependentNameTypeLoc DNTLoc = cast<DependentNameTypeLoc>(ClsTL);
327638a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          DNTLoc.setElaboratedKeywordLoc(SourceLocation());
3277b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
3278b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
3279b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3280b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3281b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3282b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpec:
3283b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::TypeSpecWithTemplate:
3284b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        if (isa<ElaboratedType>(ClsTy)) {
3285b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ElaboratedTypeLoc ETLoc = *cast<ElaboratedTypeLoc>(&ClsTL);
328638a42916cb07fd368d9e2ae1e7915fa896f9ec06Abramo Bagnara          ETLoc.setElaboratedKeywordLoc(SourceLocation());
3287b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ETLoc.setQualifierLoc(NNSLoc.getPrefix());
3288b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
3289b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
3290b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        } else {
3291b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara          ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
3292b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        }
3293b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        break;
3294b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3295b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Namespace:
3296b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::NamespaceAlias:
3297b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      case NestedNameSpecifier::Global:
3298b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara        llvm_unreachable("Nested-name-specifier must name a type");
3299b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      }
3300b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara
3301b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      // Finally fill in MemberPointerLocInfo fields.
330251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setStarLoc(Chunk.Loc);
3303b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara      TL.setClassTInfo(ClsTInfo);
33044adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
330551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
330651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
330754e14c4db764c0636160d26c5bbf491637c83a76John McCall      // 'Amp' is misleading: this might have been originally
330854e14c4db764c0636160d26c5bbf491637c83a76John McCall      /// spelled with AmpAmp.
330951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpLoc(Chunk.Loc);
331051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
331151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
331251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Reference);
331351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(!Chunk.Ref.LValueRef);
331451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setAmpAmpLoc(Chunk.Loc);
331551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
331651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitArrayTypeLoc(ArrayTypeLoc TL) {
331751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Array);
331851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setLBracketLoc(Chunk.Loc);
331951bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setRBracketLoc(Chunk.EndLoc);
332051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
332151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
332251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
332351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      assert(Chunk.Kind == DeclaratorChunk::Function);
3324796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeBegin(Chunk.Loc);
3325796aa443ab5ed036f42ef33fed629e1b4b34871bAbramo Bagnara      TL.setLocalRangeEnd(Chunk.EndLoc);
332651bd803fbdade51d674598ed45da3d54190a656cJohn McCall
332751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
332859c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setLParenLoc(FTI.getLParenLoc());
332959c0a818a79be850f7ae8fdafd57a1710e5b809aAbramo Bagnara      TL.setRParenLoc(FTI.getRParenLoc());
333054e14c4db764c0636160d26c5bbf491637c83a76John McCall      for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
3331d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
333254e14c4db764c0636160d26c5bbf491637c83a76John McCall        TL.setArg(tpi++, Param);
33334adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis      }
333451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // FIXME: exception specs
33354adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
3336075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    void VisitParenTypeLoc(ParenTypeLoc TL) {
3337075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      assert(Chunk.Kind == DeclaratorChunk::Paren);
3338075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setLParenLoc(Chunk.Loc);
3339075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      TL.setRParenLoc(Chunk.EndLoc);
3340075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    }
33411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
334251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void VisitTypeLoc(TypeLoc TL) {
33439f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("unsupported TypeLoc kind in declarator!");
33444adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis    }
334551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  };
334651bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
33474adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3348a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create and instantiate a TypeSourceInfo with type source information.
334951bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
335051bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// \param T QualType referring to the type as written in source code.
335105baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor///
335205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// \param ReturnTypeInfo For declarators whose return type does not show
335305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// up in the normal place in the declaration specifiers (such as a C++
335405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// conversion function), this pointer will refer to a type source information
335505baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor/// for that return type.
3356a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *
335705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas GregorSema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
335805baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor                                     TypeSourceInfo *ReturnTypeInfo) {
3359a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
3360a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
336151bd803fbdade51d674598ed45da3d54190a656cJohn McCall
3362a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  // Handle parameter packs whose type is a pack expansion.
3363a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  if (isa<PackExpansionType>(T)) {
3364a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor    cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc());
336591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
3366a8bc8c9e9ba5bffebde00340786fe8542469c435Douglas Gregor  }
336791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
33688ce35b095e8fca45e04c1bda14ed0548ce7536adSebastian Redl  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
336914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    while (isa<AttributedTypeLoc>(CurrTL)) {
337014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      AttributedTypeLoc TL = cast<AttributedTypeLoc>(CurrTL);
337114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs());
337214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall      CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
337314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    }
337414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3375b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara    DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
337651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
33774adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis  }
337891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3379b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // If we have different source information for the return type, use
3380b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  // that.  This really only applies to C++ conversion functions.
3381b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  if (ReturnTypeInfo) {
338205baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    TypeLoc TL = ReturnTypeInfo->getTypeLoc();
338305baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
338405baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor    memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
3385b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  } else {
3386c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor    TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
338705baacbfd67017b2724f3e0503fd23609f5d32bcDouglas Gregor  }
338891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3389a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
33904adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis}
33914adab7fcb4cb1e23622f4849f7ef7981ff169616Argyrios Kyrtzidis
3392a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
3393b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
33941bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
33951bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // and Sema during declaration parsing. Try deallocating/caching them when
33961bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  // it's appropriate, instead of allocating them and keeping them around.
339791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
3398eb0eb49ce5f5294902769702b9322e42e89e972eDouglas Gregor                                                       TypeAlignment);
3399a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (LocT) LocInfoType(T, TInfo);
34001bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis  assert(LocT->getTypeClass() != T->getTypeClass() &&
34011bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis         "LocInfoType's TypeClass conflicts with an existing Type class");
3402b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParsedType::make(QualType(LocT, 0));
34031bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
34041bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
34051bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidisvoid LocInfoType::getAsStringInternal(std::string &Str,
34061bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis                                      const PrintingPolicy &Policy) const {
3407b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*"
340835d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " was used directly instead of getting the QualType through"
340935d44e5673e772d1cc7eab66818de8d9796b89caArgyrios Kyrtzidis         " GetTypeFromParser");
34101bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis}
34111bb8a45f7386a23871598d05141a07af03067925Argyrios Kyrtzidis
3412f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallTypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
34135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.7.6: Type names have no identifier.  This is already validated by
34145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the parser.
34155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
34161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3417d3880f8458bb6a03818ee01f758c32f945de3eaaArgyrios Kyrtzidis  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
3418bf1a028246d884a540aeafa38e89be59a269b072John McCall  QualType T = TInfo->getType();
34195153ee66d6d4fb37b02f85df38e48dc8b46660dfChris Lattner  if (D.isInvalidType())
3420809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return true;
34215912a3544e438a92832b8c52c13f48d4f54795dcSteve Naroff
3422e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall  // Make sure there are no unused decl attributes on the declarator.
3423cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // We don't want to do this for ObjC parameters because we're going
3424cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // to apply them to the actual parameter declaration.
3425cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  if (D.getContext() != Declarator::ObjCParameterContext)
3426cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    checkUnusedDeclAttributes(D);
3427e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
34284e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
3429402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor    // Check that there are no default arguments (C++ only).
34306d6eb57225b53fb627c565861d1d0e90645400d1Douglas Gregor    CheckExtraCXXDefaultArguments(D);
3431402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor  }
3432402abb55fc2e0cdda5fb1ac90009b1f5f6774906Douglas Gregor
3433b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return CreateParsedType(T, TInfo);
34345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
34355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3436e97179c675b341927807c718be215c8d1aab8acbDouglas GregorParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) {
3437e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  QualType T = Context.getObjCInstanceType();
3438e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc);
3439e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return CreateParsedType(T, TInfo);
3440e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
3441e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3442e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3443c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3444c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner// Type Attribute Processing
3445c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner//===----------------------------------------------------------------------===//
3446232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3447232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
3448c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// specified type.  The attribute contains 1 argument, the id of the address
3449c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner/// space for the type.
34501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void HandleAddressSpaceTypeAttribute(QualType &Type,
3451c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner                                            const AttributeList &Attr, Sema &S){
34520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
3453232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // If this type is already address space qualified, reject it.
345429e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified by
345529e3ef8df84da298e7553a84276af4909ff6e9ebPeter Collingbourne  // qualifiers for two or more different address spaces."
3456232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  if (Type.getAddressSpace()) {
3457c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
3458e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3459c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3460232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
34611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3462020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be
3463020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  // qualified by an address-space qualifier."
3464020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  if (Type->isFunctionType()) {
3465020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type);
3466020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    Attr.setInvalid();
3467020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne    return;
3468020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne  }
3469020972d5d6dc1f3c49839cfbadcccf4cbefb2f4dPeter Collingbourne
3470232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  // Check the attribute arguments.
3471545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (Attr.getNumArgs() != 1) {
3472f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3473e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3474c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3475232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3476545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
3477232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  llvm::APSInt addrSpace(32);
3478ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
3479ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
3480dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
3481dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      << ASArgExpr->getSourceRange();
3482e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3483c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    return;
3484232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner  }
3485232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
3486efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  // Bounds checking.
3487efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace.isSigned()) {
3488efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    if (addrSpace.isNegative()) {
3489efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
3490efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall        << ASArgExpr->getSourceRange();
3491e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      Attr.setInvalid();
3492efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall      return;
3493efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    }
3494efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    addrSpace.setIsSigned(false);
3495efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3496efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  llvm::APSInt max(addrSpace.getBitWidth());
34970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  max = Qualifiers::MaxAddressSpace;
3498efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  if (addrSpace > max) {
3499efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
35000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
3501e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
3502efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall    return;
3503efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall  }
3504efadb7768e7c7418185f5a4010ecd8b21ca9731bJohn McCall
35051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
3506f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
3507c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner}
3508c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner
3509d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// Does this type have a "direct" ownership qualifier?  That is,
3510d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// is it written like "__strong id", as opposed to something like
3511d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall/// "typeof(foo)", where that happens to be strong?
3512d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCallstatic bool hasDirectOwnershipQualifier(QualType type) {
3513d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Fast path: no qualifier at all.
3514d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  assert(type.getQualifiers().hasObjCLifetime());
3515d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3516d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  while (true) {
3517d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // __strong id
3518d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (const AttributedType *attr = dyn_cast<AttributedType>(type)) {
3519d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      if (attr->getAttrKind() == AttributedType::attr_objc_ownership)
3520d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        return true;
3521d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3522d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = attr->getModifiedType();
3523d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3524d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // X *__strong (...)
3525d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else if (const ParenType *paren = dyn_cast<ParenType>(type)) {
3526d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      type = paren->getInnerType();
352791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3528d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // That's it for things we want to complain about.  In particular,
3529d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // we do not want to look through typedefs, typeof(expr),
3530d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // typeof(type), or any other way that the type is somehow
3531d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // abstracted.
3532d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    } else {
353391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3534d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return false;
3535d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3536d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
3537d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall}
3538d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3539b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis/// handleObjCOwnershipTypeAttr - Process an objc_ownership
3540f85e193739c953358c865005855253af4f68a497John McCall/// attribute on the specified type.
3541f85e193739c953358c865005855253af4f68a497John McCall///
3542f85e193739c953358c865005855253af4f68a497John McCall/// Returns 'true' if the attribute was handled.
3543b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidisstatic bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
3544f85e193739c953358c865005855253af4f68a497John McCall                                       AttributeList &attr,
3545f85e193739c953358c865005855253af4f68a497John McCall                                       QualType &type) {
3546e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  bool NonObjCPointer = false;
3547e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3548e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!type->isDependentType()) {
3549e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    if (const PointerType *ptr = type->getAs<PointerType>()) {
3550e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      QualType pointee = ptr->getPointeeType();
3551e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      if (pointee->isObjCRetainableType() || pointee->isPointerType())
3552e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis        return false;
3553e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // It is important not to lose the source info that there was an attribute
3554e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // applied to non-objc pointer. We will create an attributed type but
3555e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      // its type will be the same as the original type.
3556e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      NonObjCPointer = true;
3557e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    } else if (!type->isObjCRetainableType()) {
3558e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      return false;
3559e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3560e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3561f85e193739c953358c865005855253af4f68a497John McCall
3562f85e193739c953358c865005855253af4f68a497John McCall  Sema &S = state.getSema();
3563440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  SourceLocation AttrLoc = attr.getLoc();
3564440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isMacroID())
3565440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    AttrLoc = S.getSourceManager().getImmediateExpansionRange(AttrLoc).first;
3566f85e193739c953358c865005855253af4f68a497John McCall
3567f85e193739c953358c865005855253af4f68a497John McCall  if (!attr.getParameterName()) {
3568440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::err_attribute_argument_n_not_string)
3569b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      << "objc_ownership" << 1;
3570f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3571f85e193739c953358c865005855253af4f68a497John McCall    return true;
3572f85e193739c953358c865005855253af4f68a497John McCall  }
3573f85e193739c953358c865005855253af4f68a497John McCall
3574d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Consume lifetime attributes without further comment outside of
3575d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // ARC mode.
35764e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!S.getLangOpts().ObjCAutoRefCount)
3577d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    return true;
3578d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3579f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime lifetime;
3580f85e193739c953358c865005855253af4f68a497John McCall  if (attr.getParameterName()->isStr("none"))
3581f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_ExplicitNone;
3582f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("strong"))
3583f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Strong;
3584f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("weak"))
3585f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Weak;
3586f85e193739c953358c865005855253af4f68a497John McCall  else if (attr.getParameterName()->isStr("autoreleasing"))
3587f85e193739c953358c865005855253af4f68a497John McCall    lifetime = Qualifiers::OCL_Autoreleasing;
3588f85e193739c953358c865005855253af4f68a497John McCall  else {
3589440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_attribute_type_not_supported)
3590b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis      << "objc_ownership" << attr.getParameterName();
3591f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3592f85e193739c953358c865005855253af4f68a497John McCall    return true;
3593f85e193739c953358c865005855253af4f68a497John McCall  }
3594f85e193739c953358c865005855253af4f68a497John McCall
3595d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  SplitQualType underlyingType = type.split();
3596d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3597d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  // Check for redundant/conflicting ownership qualifiers.
3598d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  if (Qualifiers::ObjCLifetime previousLifetime
3599d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        = type.getQualifiers().getObjCLifetime()) {
3600d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // If it's written directly, that's an error.
3601d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (hasDirectOwnershipQualifier(type)) {
3602d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant)
3603d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        << type;
3604d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      return true;
3605d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3606d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3607d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // Otherwise, if the qualifiers actually conflict, pull sugar off
3608d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    // until we reach a type that is directly qualified.
3609d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    if (previousLifetime != lifetime) {
3610d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // This should always terminate: the canonical type is
3611d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      // qualified, so some bit of sugar must be hiding it.
3612d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      while (!underlyingType.Quals.hasObjCLifetime()) {
3613d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall        underlyingType = underlyingType.getSingleStepDesugaredType();
3614d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      }
3615d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall      underlyingType.Quals.removeObjCLifetime();
3616d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    }
3617d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  }
3618d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall
3619d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall  underlyingType.Quals.addObjCLifetime(lifetime);
3620f85e193739c953358c865005855253af4f68a497John McCall
3621e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (NonObjCPointer) {
3622e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    StringRef name = attr.getName()->getName();
3623e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    switch (lifetime) {
3624e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_None:
3625e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_ExplicitNone:
3626e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      break;
3627e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Strong: name = "__strong"; break;
3628e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Weak: name = "__weak"; break;
3629e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break;
3630e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    }
3631e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis    S.Diag(AttrLoc, diag::warn_objc_object_attribute_wrong_type)
3632e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis      << name << type;
3633e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  }
3634e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis
3635f85e193739c953358c865005855253af4f68a497John McCall  QualType origType = type;
3636e71202efccdead44c8a3d4a2296d866d0e89799bArgyrios Kyrtzidis  if (!NonObjCPointer)
3637d85bf9dd23920f5400bb8d6a41c8ebb1aecfdee9John McCall    type = S.Context.getQualifiedType(underlyingType);
3638f85e193739c953358c865005855253af4f68a497John McCall
3639f85e193739c953358c865005855253af4f68a497John McCall  // If we have a valid source location for the attribute, use an
3640f85e193739c953358c865005855253af4f68a497John McCall  // AttributedType instead.
3641440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis  if (AttrLoc.isValid())
3642b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis    type = S.Context.getAttributedType(AttributedType::attr_objc_ownership,
3643f85e193739c953358c865005855253af4f68a497John McCall                                       origType, type);
3644f85e193739c953358c865005855253af4f68a497John McCall
36459f084a3166b684573ba49df28fc5792bc37d92e1John McCall  // Forbid __weak if the runtime doesn't support it.
3646f85e193739c953358c865005855253af4f68a497John McCall  if (lifetime == Qualifiers::OCL_Weak &&
36470a7dd788dbef975f35f273c7ab913f480f7edd60John McCall      !S.getLangOpts().ObjCARCWeak && !NonObjCPointer) {
3648f85e193739c953358c865005855253af4f68a497John McCall
3649f85e193739c953358c865005855253af4f68a497John McCall    // Actually, delay this until we know what we're parsing.
3650f85e193739c953358c865005855253af4f68a497John McCall    if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
3651f85e193739c953358c865005855253af4f68a497John McCall      S.DelayedDiagnostics.add(
3652440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis          sema::DelayedDiagnostic::makeForbiddenType(
3653440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis              S.getSourceManager().getExpansionLoc(AttrLoc),
3654f85e193739c953358c865005855253af4f68a497John McCall              diag::err_arc_weak_no_runtime, type, /*ignored*/ 0));
3655f85e193739c953358c865005855253af4f68a497John McCall    } else {
3656440ec2ebbe2e7aa2a5f733a41cf845d354d16e23Argyrios Kyrtzidis      S.Diag(AttrLoc, diag::err_arc_weak_no_runtime);
3657f85e193739c953358c865005855253af4f68a497John McCall    }
3658f85e193739c953358c865005855253af4f68a497John McCall
3659f85e193739c953358c865005855253af4f68a497John McCall    attr.setInvalid();
3660f85e193739c953358c865005855253af4f68a497John McCall    return true;
3661f85e193739c953358c865005855253af4f68a497John McCall  }
366291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
366391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  // Forbid __weak for class objects marked as
3664742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  // objc_arc_weak_reference_unavailable
3665742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  if (lifetime == Qualifiers::OCL_Weak) {
3666742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    QualType T = type;
3667742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    while (const PointerType *ptr = T->getAs<PointerType>())
3668742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      T = ptr->getPointeeType();
3669742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
36704e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith      if (ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl()) {
36714e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        if (Class->isArcWeakrefUnavailable()) {
36724e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class);
36734e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith            S.Diag(ObjT->getInterfaceDecl()->getLocation(),
36744e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith                   diag::note_class_declared);
36754e90bc39f052ea0046d40aebbb42732ad1f21f50Richard Smith        }
3676742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian      }
3677742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian    }
3678742352a3984aeef9ecf911be23e673e97b34595fFariborz Jahanian  }
367991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
3680f85e193739c953358c865005855253af4f68a497John McCall  return true;
3681f85e193739c953358c865005855253af4f68a497John McCall}
3682f85e193739c953358c865005855253af4f68a497John McCall
3683711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
3684711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// attribute on the specified type.  Returns true to indicate that
3685711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// the attribute was handled, false to indicate that the type does
3686711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// not permit the attribute.
3687711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleObjCGCTypeAttr(TypeProcessingState &state,
3688711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 AttributeList &attr,
3689711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                 QualType &type) {
3690711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
3691711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3692711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Delay if this isn't some kind of pointer.
3693711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!type->isPointerType() &&
3694711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isObjCObjectPointerType() &&
3695711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      !type->isBlockPointerType())
3696711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return false;
3697711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
3698711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (type.getObjCGCAttr() != Qualifiers::GCNone) {
3699711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
3700711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3701711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3702d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
37031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3704d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check the attribute arguments.
3705711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!attr.getParameterName()) {
3706711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3707ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian      << "objc_gc" << 1;
3708711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3709711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3710ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian  }
37110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttr;
3712711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getNumArgs() != 0) {
3713711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3714711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3715711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3716d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
3717711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (attr.getParameterName()->isStr("weak"))
37180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Weak;
3719711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  else if (attr.getParameterName()->isStr("strong"))
37200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    GCAttr = Qualifiers::Strong;
3721d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  else {
3722711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
3723711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      << "objc_gc" << attr.getParameterName();
3724711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3725711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3726d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
37271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
372814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  QualType origType = type;
372914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  type = S.Context.getObjCGCQualType(origType, GCAttr);
373014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
373114aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  // Make an attributed type to preserve the source information.
373214aa2175416f79ef17811282afbf425f87d54ebfJohn McCall  if (attr.getLoc().isValid())
373314aa2175416f79ef17811282afbf425f87d54ebfJohn McCall    type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
373414aa2175416f79ef17811282afbf425f87d54ebfJohn McCall                                       origType, type);
373514aa2175416f79ef17811282afbf425f87d54ebfJohn McCall
3736711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
3737d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
3738d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
3739e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallnamespace {
3740e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// A helper class to unwrap a type down to a function for the
3741e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// purposes of applying attributes there.
3742e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///
3743e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  /// Use:
3744e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   FunctionTypeUnwrapper unwrapped(SemaRef, T);
3745e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   if (unwrapped.isFunctionType()) {
3746e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     const FunctionType *fn = unwrapped.get();
3747e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     // change fn somehow
3748e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///     T = unwrapped.wrap(fn);
3749e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  ///   }
3750e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  struct FunctionTypeUnwrapper {
3751e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    enum WrapKind {
3752e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Desugar,
3753e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Parens,
3754e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Pointer,
3755e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      BlockPointer,
3756e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Reference,
3757e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      MemberPointer
3758e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    };
3759e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3760e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType Original;
3761e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *Fn;
37625f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<unsigned char /*WrapKind*/, 8> Stack;
3763e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3764e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
3765e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      while (true) {
3766e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const Type *Ty = T.getTypePtr();
3767e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<FunctionType>(Ty)) {
3768e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Fn = cast<FunctionType>(Ty);
3769e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return;
3770e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ParenType>(Ty)) {
3771e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ParenType>(Ty)->getInnerType();
3772e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Parens);
3773e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<PointerType>(Ty)) {
3774e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<PointerType>(Ty)->getPointeeType();
3775e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Pointer);
3776e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<BlockPointerType>(Ty)) {
3777e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<BlockPointerType>(Ty)->getPointeeType();
3778e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(BlockPointer);
3779e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<MemberPointerType>(Ty)) {
3780e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<MemberPointerType>(Ty)->getPointeeType();
3781e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(MemberPointer);
3782e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else if (isa<ReferenceType>(Ty)) {
3783e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = cast<ReferenceType>(Ty)->getPointeeType();
3784e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Reference);
3785e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        } else {
3786e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          const Type *DTy = Ty->getUnqualifiedDesugaredType();
3787e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          if (Ty == DTy) {
3788e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            Fn = 0;
3789e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall            return;
3790e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          }
3791e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3792e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          T = QualType(DTy, 0);
3793e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          Stack.push_back(Desugar);
3794e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        }
3795e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3796e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3797e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3798e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    bool isFunctionType() const { return (Fn != 0); }
3799e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionType *get() const { return Fn; }
3800e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3801e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(Sema &S, const FunctionType *New) {
3802e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // If T wasn't modified from the unwrapped type, do nothing.
3803e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (New == get()) return Original;
3804e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3805e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      Fn = New;
3806e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      return wrap(S.Context, Original, 0);
3807e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3808e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3809e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  private:
3810e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, QualType Old, unsigned I) {
3811e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size())
3812e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getQualifiedType(Fn, Old.getQualifiers());
3813e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3814e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // Build up the inner type, applying the qualifiers from the old
3815e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // type to the new type.
3816e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      SplitQualType SplitOld = Old.split();
3817e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3818e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      // As a special case, tail-recurse if there are no qualifiers.
3819200fa53fd420aa8369586f569dbece04930ad6a3John McCall      if (SplitOld.Quals.empty())
3820200fa53fd420aa8369586f569dbece04930ad6a3John McCall        return wrap(C, SplitOld.Ty, I);
3821200fa53fd420aa8369586f569dbece04930ad6a3John McCall      return C.getQualifiedType(wrap(C, SplitOld.Ty, I), SplitOld.Quals);
3822e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3823e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3824e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
3825e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      if (I == Stack.size()) return QualType(Fn, 0);
3826e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3827e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      switch (static_cast<WrapKind>(Stack[I++])) {
3828e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Desugar:
3829e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // This is the point at which we potentially lose source
3830e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        // information.
3831e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return wrap(C, Old->getUnqualifiedDesugaredType(), I);
3832e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3833e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Parens: {
3834e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
3835e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getParenType(New);
3836e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3837e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3838e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Pointer: {
3839e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
3840e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getPointerType(New);
3841e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3842e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3843e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case BlockPointer: {
3844e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
3845e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getBlockPointerType(New);
3846e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3847e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3848e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case MemberPointer: {
3849e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
3850e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldMPT->getPointeeType(), I);
3851e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        return C.getMemberPointerType(New, OldMPT->getClass());
3852e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3853e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3854e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      case Reference: {
3855e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        const ReferenceType *OldRef = cast<ReferenceType>(Old);
3856e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        QualType New = wrap(C, OldRef->getPointeeType(), I);
3857e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        if (isa<LValueReferenceType>(OldRef))
3858e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
3859e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall        else
3860e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall          return C.getRValueReferenceType(New);
3861e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3862e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      }
3863e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3864e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall      llvm_unreachable("unknown wrapping kind");
3865e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    }
3866e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  };
3867e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
3868e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3869711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// Process an individual function attribute.  Returns true to
3870711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// indicate that the attribute was handled, false if it wasn't.
3871711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic bool handleFunctionTypeAttr(TypeProcessingState &state,
3872711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   AttributeList &attr,
3873711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   QualType &type) {
3874711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Sema &S = state.getSema();
3875e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3876711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionTypeUnwrapper unwrapped(S, type);
38772455636163fdd18581d7fdae816433f886d88213Mike Stump
38788e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NoReturn) {
3879711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckNoReturnAttr(attr))
388004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      return true;
3881e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
3882e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    // Delay if this is not a function type.
3883711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
3884711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return false;
3885425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
3886425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    // Otherwise we can process right away.
3887711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
3888711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3889711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3890711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
3891425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
3892f85e193739c953358c865005855253af4f68a497John McCall  // ns_returns_retained is not always a type attribute, but if we got
3893f85e193739c953358c865005855253af4f68a497John McCall  // here, we're treating it as one right now.
38948e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_NSReturnsRetained) {
38954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(S.getLangOpts().ObjCAutoRefCount &&
3896f85e193739c953358c865005855253af4f68a497John McCall           "ns_returns_retained treated as type attribute in non-ARC");
3897f85e193739c953358c865005855253af4f68a497John McCall    if (attr.getNumArgs()) return true;
3898f85e193739c953358c865005855253af4f68a497John McCall
3899f85e193739c953358c865005855253af4f68a497John McCall    // Delay if this is not a function type.
3900f85e193739c953358c865005855253af4f68a497John McCall    if (!unwrapped.isFunctionType())
3901f85e193739c953358c865005855253af4f68a497John McCall      return false;
3902f85e193739c953358c865005855253af4f68a497John McCall
3903f85e193739c953358c865005855253af4f68a497John McCall    FunctionType::ExtInfo EI
3904f85e193739c953358c865005855253af4f68a497John McCall      = unwrapped.get()->getExtInfo().withProducesResult(true);
3905f85e193739c953358c865005855253af4f68a497John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3906f85e193739c953358c865005855253af4f68a497John McCall    return true;
3907f85e193739c953358c865005855253af4f68a497John McCall  }
3908f85e193739c953358c865005855253af4f68a497John McCall
39098e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt  if (attr.getKind() == AttributeList::AT_Regparm) {
3910711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    unsigned value;
3911711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (S.CheckRegparmAttr(attr, value))
3912711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
39131e030eb1194763b42c1752723be23b1515f48981John McCall
3914711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Delay if this is not a function type.
3915711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (!unwrapped.isFunctionType())
3916008df5dce3938456ae7ea2e7ab3b2d12391ebf3eJohn McCall      return false;
39171e030eb1194763b42c1752723be23b1515f48981John McCall
3918ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Diagnose regparm with fastcall.
3919ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    const FunctionType *fn = unwrapped.get();
3920ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    CallingConv CC = fn->getCallConv();
3921ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    if (CC == CC_X86FastCall) {
3922ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
3923ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC)
3924ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm";
3925ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
3926ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
3927ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
3928ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
392991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    FunctionType::ExtInfo EI =
3930711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      unwrapped.get()->getExtInfo().withRegParm(value);
3931711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3932711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3933425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  }
3934425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
393582bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  // Delay if the type didn't work out to a function.
393682bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman  if (!unwrapped.isFunctionType()) return false;
393782bfa19fe3be324b13fdbcda46304b52c500f0d4Aaron Ballman
393804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Otherwise, a calling convention.
3939711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CC;
3940711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (S.CheckCallingConvAttr(attr, CC))
3941711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3942f82b4e85b1219295cad4b5851b035575bc293010John McCall
3943711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  const FunctionType *fn = unwrapped.get();
3944711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  CallingConv CCOld = fn->getCallConv();
3945064f7db69def9299f5f4d9a32114afc10b6a6420Charles Davis  if (S.Context.getCanonicalCallConv(CC) ==
3946e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      S.Context.getCanonicalCallConv(CCOld)) {
3947ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
3948ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3949711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
3950e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  }
395104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
39528e68f1c8a2919ea83c2053731d6011074f1062e1Roman Divacky  if (CCOld != (S.LangOpts.MRTD ? CC_X86StdCall : CC_Default)) {
395304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    // Should we diagnose reapplications of the same convention?
3954711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
395504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CC)
395604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      << FunctionType::getNameForCallConv(CCOld);
3957711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    attr.setInvalid();
3958711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return true;
395904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
396004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
396104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
396204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (CC == CC_X86FastCall) {
3963711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (isa<FunctionNoProtoType>(fn)) {
3964711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_knr)
396504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
3966711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
3967711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
396804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
396904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
3970711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
397104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FnP->isVariadic()) {
3972711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      S.Diag(attr.getLoc(), diag::err_cconv_varargs)
397304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        << FunctionType::getNameForCallConv(CC);
3974711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      attr.setInvalid();
3975711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      return true;
397604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    }
3977ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis
3978ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    // Also diagnose fastcall with regparm.
3979a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    if (fn->getHasRegParm()) {
3980ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
3981ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << "regparm"
3982ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis        << FunctionType::getNameForCallConv(CC);
3983ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      attr.setInvalid();
3984ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis      return true;
3985ce95566b36a4ff16e90507633dad8b7a76572999Argyrios Kyrtzidis    }
398604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
3987f82b4e85b1219295cad4b5851b035575bc293010John McCall
3988711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
3989711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3990711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return true;
3991f82b4e85b1219295cad4b5851b035575bc293010John McCall}
3992f82b4e85b1219295cad4b5851b035575bc293010John McCall
3993207f4d8543529221932af82836016a2ef066c917Peter Collingbourne/// Handle OpenCL image access qualifiers: read_only, write_only, read_write
3994207f4d8543529221932af82836016a2ef066c917Peter Collingbournestatic void HandleOpenCLImageAccessAttribute(QualType& CurType,
3995207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const AttributeList &Attr,
3996207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             Sema &S) {
3997207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  // Check the attribute arguments.
3998207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (Attr.getNumArgs() != 1) {
3999207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4000207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4001207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4002207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4003207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
4004207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  llvm::APSInt arg(32);
4005207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4006207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      !sizeExpr->isIntegerConstantExpr(arg, S.Context)) {
4007207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4008207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << "opencl_image_access" << sizeExpr->getSourceRange();
4009207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4010207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return;
4011207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4012207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  unsigned iarg = static_cast<unsigned>(arg.getZExtValue());
4013207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  switch (iarg) {
4014207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_only:
4015207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_write_only:
4016207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  case CLIA_read_write:
4017207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4018207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4019207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  default:
4020207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // Implemented in a separate patch
4021207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
4022207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      << sizeExpr->getSourceRange();
4023207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    Attr.setInvalid();
4024207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    break;
4025207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
4026207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
4027207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
40286e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// HandleVectorSizeAttribute - this attribute is only applicable to integral
40296e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// and float scalars, although arrays, pointers, and function return values are
40306e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// allowed in conjunction with this construct. Aggregates with this attribute
40316e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// are invalid, even if they are of the same size as a corresponding scalar.
40326e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// The raw attribute should contain precisely 1 argument, the vector size for
40336e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// the variable, measured in bytes. If curType and rawAttr are well formed,
40346e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson/// this routine will return a new vector type.
4035788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattnerstatic void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
4036788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                 Sema &S) {
403756affbcaeff9a01caa70b2a237f7e6ac31c8ded6Bob Wilson  // Check the attribute arguments.
40386e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (Attr.getNumArgs() != 1) {
40396e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4040e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
40416e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
40426e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
40436e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
40446e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  llvm::APSInt vecSize(32);
4045ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4046ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
40476e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
40486e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << "vector_size" << sizeExpr->getSourceRange();
4049e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
40506e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
40516e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
40526e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the base type must be integer or float, and can't already be a vector.
4053f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
40546e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
4055e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
40566e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
40576e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
40586e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
40596e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // vecSize is specified in bytes - convert to bits.
40606e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
40616e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
40626e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // the vector size needs to be an integral multiple of the type size.
40636e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize % typeSize) {
40646e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
40656e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4066e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
40676e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
40686e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
40696e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  if (vectorSize == 0) {
40706e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
40716e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      << sizeExpr->getSourceRange();
4072e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    Attr.setInvalid();
40736e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson    return;
40746e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  }
40756e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
40766e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // Success! Instantiate the vector type, the number of elements is > 0, and
40776e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson  // not required to be a power of 2, unlike GCC.
4078788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
4079e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                    VectorType::GenericVector);
40806e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson}
40816e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson
40824ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// \brief Process the OpenCL-like ext_vector_type attribute when it occurs on
40834ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor/// a type.
408491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierstatic void HandleExtVectorTypeAttr(QualType &CurType,
408591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                                    const AttributeList &Attr,
40864ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                    Sema &S) {
40874ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  Expr *sizeExpr;
408891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
40894ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Special case where the argument is a template id.
40904ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (Attr.getParameterName()) {
40914ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CXXScopeSpec SS;
4092e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    SourceLocation TemplateKWLoc;
40934ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    UnqualifiedId id;
40944ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
4095e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
4096e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    ExprResult Size = S.ActOnIdExpression(S.getCurScope(), SS, TemplateKWLoc,
4097e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                          id, false, false);
40984ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Size.isInvalid())
40994ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
410091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
41014ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Size.get();
41024ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  } else {
41034ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    // check the attribute arguments.
41044ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    if (Attr.getNumArgs() != 1) {
41054ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
41064ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return;
41074ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    }
41084ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    sizeExpr = Attr.getArg(0);
41094ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  }
411091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
41114ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  // Create the vector type.
41124ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  QualType T = S.BuildExtVectorType(CurType, sizeExpr, Attr.getLoc());
41134ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  if (!T.isNull())
41144ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    CurType = T;
41154ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor}
41164ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
41174211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
41184211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// "neon_polyvector_type" attributes are used to create vector types that
41194211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// are mangled according to ARM's ABI.  Otherwise, these types are identical
41204211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// to those created with the "vector_size" attribute.  Unlike "vector_size"
41214211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// the argument to these Neon attributes is the number of vector elements,
41224211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// not the vector size in bytes.  The vector width and element type must
41234211bb68cff1f310be280f66a59520548ef99d8fBob Wilson/// match one of the standard Neon vector types.
41244211bb68cff1f310be280f66a59520548ef99d8fBob Wilsonstatic void HandleNeonVectorTypeAttr(QualType& CurType,
41254211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const AttributeList &Attr, Sema &S,
41264211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     VectorType::VectorKind VecKind,
41274211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                                     const char *AttrName) {
41284211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Check the attribute arguments.
41294211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (Attr.getNumArgs() != 1) {
41304211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
41314211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
41324211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
41334211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
41344211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The number of elements must be an ICE.
41354211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
41364211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  llvm::APSInt numEltsInt(32);
41374211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
41384211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
41394211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
41404211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      << AttrName << numEltsExpr->getSourceRange();
41414211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
41424211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
41434211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
41444211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // Only certain element types are supported for Neon vectors.
41454211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  const BuiltinType* BTy = CurType->getAs<BuiltinType>();
41464211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (!BTy ||
41474211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (VecKind == VectorType::NeonPolyVector &&
41484211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::SChar &&
41494211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short) ||
41504211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      (BTy->getKind() != BuiltinType::SChar &&
41514211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UChar &&
41524211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Short &&
41534211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UShort &&
41544211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Int &&
41554211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::UInt &&
41564211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::LongLong &&
41574211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::ULongLong &&
41584211bb68cff1f310be280f66a59520548ef99d8fBob Wilson       BTy->getKind() != BuiltinType::Float)) {
41594211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
41604211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
41614211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
41624211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
41634211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  // The total size of the vector must be 64 or 128 bits.
41644211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
41654211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
41664211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  unsigned vecSize = typeSize * numElts;
41674211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  if (vecSize != 64 && vecSize != 128) {
41684211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
41694211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    Attr.setInvalid();
41704211bb68cff1f310be280f66a59520548ef99d8fBob Wilson    return;
41714211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  }
41724211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
41734211bb68cff1f310be280f66a59520548ef99d8fBob Wilson  CurType = S.Context.getVectorType(CurType, numElts, VecKind);
41744211bb68cff1f310be280f66a59520548ef99d8fBob Wilson}
41754211bb68cff1f310be280f66a59520548ef99d8fBob Wilson
4176711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstatic void processTypeAttrs(TypeProcessingState &state, QualType &type,
4177f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                             TypeAttrLocation TAL, AttributeList *attrs) {
4178c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // Scan through and apply attributes to this type where it makes sense.  Some
4179c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // attributes (such as __address_space__, __vector_size__, etc) apply to the
4180c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // type, but others can be present in the type specifiers even though they
4181c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner  // apply to the decl.  Here we apply type attributes and ignore the rest.
4182711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4183711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  AttributeList *next;
4184711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  do {
4185711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    AttributeList &attr = *attrs;
4186711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    next = attr.getNext();
4187711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4188e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Skip attributes that were marked to be invalid.
4189711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (attr.isInvalid())
4190e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara      continue;
4191e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara
4192f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    // [[gnu::...]] attributes are treated as declaration attributes, so may
4193f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    // not appertain to a DeclaratorChunk, even if we handle them as type
4194f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    // attributes.
4195f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    // FIXME: All other C++11 type attributes may *only* appertain to a type,
4196f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    // and should only be considered here if they appertain to a
4197f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    // DeclaratorChunk.
4198f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk &&
4199f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith        attr.getScopeName() && attr.getScopeName()->isStr("gnu")) {
4200f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      state.getSema().Diag(attr.getLoc(),
4201f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith                           diag::warn_cxx11_gnu_attribute_on_type)
4202f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith        << attr.getName();
4203f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      continue;
4204f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith    }
4205f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith
4206b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // If this is an attribute we can handle, do so now,
4207b1f1b267351be74013f966f4834cde1eddbe0233Abramo Bagnara    // otherwise, add it to the FnAttrs list for rechaining.
4208711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    switch (attr.getKind()) {
4209c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    default: break;
421004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
42118e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_MayAlias:
4212682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // FIXME: This attribute needs to actually be handled, but if we ignore
4213682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      // it it breaks large amounts of Linux software.
4214682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      attr.setUsedAsTypeAttr();
4215682eae243ae3d96fe3dc302091034e08c414db10Chandler Carruth      break;
42168e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_AddressSpace:
4217711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
4218e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4219c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner      break;
4220711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    OBJC_POINTER_TYPE_ATTRS_CASELIST:
4221711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (!handleObjCPointerTypeAttr(state, attr, type))
4222711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeObjCPointerTypeAttr(state, attr, type);
4223e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4224d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian      break;
42258e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_VectorSize:
4226711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleVectorSizeAttr(type, attr, state.getSema());
4227e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4228f82b4e85b1219295cad4b5851b035575bc293010John McCall      break;
42298e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_ExtVectorType:
4230a4fa9008988985f9cf01712a99ddd923aea278a0Richard Smith      HandleExtVectorTypeAttr(type, attr, state.getSema());
4231e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
42324ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      break;
42338e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonVectorType:
4234711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
4235711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonVector, "neon_vector_type");
4236e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
42374211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
42388e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NeonPolyVectorType:
4239711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      HandleNeonVectorTypeAttr(type, attr, state.getSema(),
4240711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                               VectorType::NeonPolyVector,
42414211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                               "neon_polyvector_type");
4242e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
42434211bb68cff1f310be280f66a59520548ef99d8fBob Wilson      break;
42448e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_OpenCLImageAccess:
4245207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      HandleOpenCLImageAccessAttribute(type, attr, state.getSema());
4246e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4247207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      break;
4248207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
42498e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Win64:
42508e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Ptr32:
42518e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_Ptr64:
4252c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall      // FIXME: don't ignore these
4253c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall      attr.setUsedAsTypeAttr();
4254c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall      break;
4255c052dbb2d8fe0e23e90d81236aab0f864f712b45John McCall
42568e083e71d48f7f4d6ef40c00531c2e14df745486Sean Hunt    case AttributeList::AT_NSReturnsRetained:
42574e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!state.getSema().getLangOpts().ObjCAutoRefCount)
425891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier    break;
4259f85e193739c953358c865005855253af4f68a497John McCall      // fallthrough into the function attrs
4260f85e193739c953358c865005855253af4f68a497John McCall
4261711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    FUNCTION_TYPE_ATTRS_CASELIST:
4262e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall      attr.setUsedAsTypeAttr();
4263e82247a71a1a76e78f3b979b64d5f6412ab40266John McCall
4264711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Never process function type attributes as part of the
4265711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // declaration-specifiers.
4266f7a052732c2b6c82f74708038f75fa92c9b4dba0Richard Smith      if (TAL == TAL_DeclSpec)
4267711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
4268711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
4269711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      // Otherwise, handle the possible delays.
4270711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      else if (!handleFunctionTypeAttr(state, attr, type))
4271711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        distributeFunctionTypeAttr(state, attr, type);
42726e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompson      break;
4273c9b346d7b3b24f8bf940735cc812893dfcef1d4bChris Lattner    }
4274711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  } while ((attrs = next));
4275232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner}
4276232e882226aa116807ee08a700dfc2350fbfabb1Chris Lattner
4277e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \brief Ensure that the type of the given expression is complete.
4278e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4279e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// This routine checks whether the expression \p E has a complete type. If the
4280e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// expression refers to an instantiable construct, that instantiation is
4281e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// performed as needed to complete its type. Furthermore
4282e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// Sema::RequireCompleteType is called for the expression's type (or in the
4283e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// case of a reference type, the referred-to type).
4284e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4285e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \param E The expression whose type is required to be complete.
4286d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// \param Diagnoser The object that will emit a diagnostic if the type is
4287d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor/// incomplete.
4288e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth///
4289e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// \returns \c true if the type of \p E is incomplete and diagnosed, \c false
4290e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth/// otherwise.
4291f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, TypeDiagnoser &Diagnoser){
4292e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  QualType T = E->getType();
4293e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4294e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Fast path the case where the type is already complete.
4295e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (!T->isIncompleteType())
4296e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    return false;
4297e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4298e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Incomplete array types may be completed by the initializer attached to
4299e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // their definitions. For static data members of class templates we need to
4300e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // instantiate the definition to get this initializer and complete the type.
4301e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (T->isIncompleteArrayType()) {
4302e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4303e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
4304e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        if (Var->isStaticDataMember() &&
4305e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth            Var->getInstantiatedFromStaticDataMember()) {
430691cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
430736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
430836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          assert(MSInfo && "Missing member specialization information?");
430936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor          if (MSInfo->getTemplateSpecializationKind()
431036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                != TSK_ExplicitSpecialization) {
431136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // If we don't already have a point of instantiation, this is it.
431236f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (MSInfo->getPointOfInstantiation().isInvalid()) {
431336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              MSInfo->setPointOfInstantiation(E->getLocStart());
431491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
431591cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier              // This is a modification of an existing AST node. Notify
431636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              // listeners.
431736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              if (ASTMutationListener *L = getASTMutationListener())
431836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor                L->StaticDataMemberInstantiated(Var);
431936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
432091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
432136f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            InstantiateStaticDataMemberDefinition(E->getExprLoc(), Var);
432291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
432336f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // Update the type to the newly instantiated definition's type both
432436f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            // here and within the expression.
432536f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            if (VarDecl *Def = Var->getDefinition()) {
432636f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setDecl(Def);
432736f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              T = Def->getType();
432836f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              DRE->setType(T);
432936f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor              E->setType(T);
433036f255c324d37dd8e0e5ab2e026814e8396a05aaDouglas Gregor            }
4331f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor          }
433291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4333e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // We still go on to try to complete the type independently, as it
4334e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // may also require instantiations or diagnostics if it remains
4335e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth          // incomplete.
4336e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth        }
4337e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth      }
4338e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    }
4339e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  }
4340e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4341e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // FIXME: Are there other cases which require instantiating something other
4342e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // than the type to complete the type of an expression?
4343e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4344e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  // Look through reference types and complete the referred type.
4345e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
4346e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth    T = Ref->getPointeeType();
4347e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
4348d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(E->getExprLoc(), T, Diagnoser);
4349d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4350d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4351d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregornamespace {
4352f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  struct TypeDiagnoserDiag : Sema::TypeDiagnoser {
4353d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    unsigned DiagID;
435491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4355f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor    TypeDiagnoserDiag(unsigned DiagID)
4356f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor      : Sema::TypeDiagnoser(DiagID == 0), DiagID(DiagID) {}
435791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4358d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) {
4359d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      if (Suppressed) return;
4360d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor      S.Diag(Loc, DiagID) << T;
4361d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    }
4362d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  };
4363d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor}
4364d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
4365d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregorbool Sema::RequireCompleteExprType(Expr *E, unsigned DiagID) {
4366f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4367d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteExprType(E, Diagnoser);
4368e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth}
4369e4d645cbe073042d8abc1a4eb600af4ff7a8dffbChandler Carruth
43701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// @brief Ensure that the type T is a complete type.
43714ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
43724ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// This routine checks whether the type @p T is complete in any
43734ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// context where a complete type is required. If @p T is a complete
437486447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// type, returns false. If @p T is a class template specialization,
437586447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// this routine then attempts to perform class template
437686447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// instantiation. If instantiation fails, or if @p T is incomplete
437786447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// and cannot be completed, issues the diagnostic @p diag (giving it
437886447ec25fa34aa3c2f48ebc49ec09bc1f03f002Douglas Gregor/// the type @p T) and returns true.
43794ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
43804ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param Loc  The location in the source that the incomplete type
43814ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// diagnostic should refer to.
43824ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
43834ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @param T  The type that this routine is examining for completeness.
43844ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor///
43854ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
43864ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor/// @c false otherwise.
438791a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlssonbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
4388f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                               TypeDiagnoser &Diagnoser) {
4389573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  // FIXME: Add this assertion to make sure we always get instantiation points.
4390573d9c325279b6e156c7fde163ffe3629c62d596Douglas Gregor  //  assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
4391690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // FIXME: Add this assertion to help us flush out problems with
4392690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  // checking for dependent types and type-dependent expressions.
4393690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //
43941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //  assert(!T->isDependentType() &&
4395690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor  //         "Can't ask whether a dependent type is complete");
4396690dc7f4f2c0fe87409839b7560c19dee7832195Douglas Gregor
43974ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If we have a complete type, we're done.
4398d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  NamedDecl *Def = 0;
4399d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  if (!T->isIncompleteType(&Def)) {
4400d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    // If we know about the definition but it is not visible, complain.
4401d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (!Diagnoser.Suppressed && Def && !LookupResult::isVisible(Def)) {
4402d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // Suppress this error outside of a SFINAE context if we've already
440391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier      // emitted the error once for this type. There's no usefulness in
4404d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // repeating the diagnostic.
4405d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // FIXME: Add a Fix-It that imports the corresponding module or includes
4406d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      // the header.
4407ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Module *Owner = Def->getOwningModule();
4408ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Loc, diag::err_module_private_definition)
4409ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        << T << Owner->getFullModuleName();
4410ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      Diag(Def->getLocation(), diag::note_previous_definition);
4411ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
4412ca2ab45341c448284cf93770018c717810575f86Douglas Gregor      if (!isSFINAEContext()) {
4413ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        // Recover by implicitly importing this module.
4414ca2ab45341c448284cf93770018c717810575f86Douglas Gregor        createImplicitModuleImport(Loc, Owner);
4415d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      }
4416d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    }
441791cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
44184ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor    return false;
4419d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
44204ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
4421bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const TagType *Tag = T->getAs<TagType>();
4422bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  const ObjCInterfaceType *IFace = 0;
442391cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4424bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  if (Tag) {
4425bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4426bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->isInvalidDecl())
4427bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
4428bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan
4429bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4430bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (Tag->getDecl()->hasExternalLexicalStorage()) {
4431bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(Tag->getDecl());
4432bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!Tag->isIncompleteType())
4433bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4434bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4435bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
4436bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  else if ((IFace = T->getAs<ObjCInterfaceType>())) {
4437bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Avoid diagnosing invalid decls as incomplete.
4438bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->isInvalidDecl())
4439bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      return true;
444091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4441bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    // Give the external AST source a chance to complete the type.
4442bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    if (IFace->getDecl()->hasExternalLexicalStorage()) {
4443bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      Context.getExternalSource()->CompleteType(IFace->getDecl());
4444bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan      if (!IFace->isIncompleteType())
4445bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan        return false;
4446bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan    }
4447bd79119a50172db92ad3ce77ec3ac3c51e42a126Sean Callanan  }
444891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4449d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // If we have a class template specialization or a class member of a
4450923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // class template specialization, or an array with known size of such,
4451923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  // try to instantiate it.
4452923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  QualType MaybeTemplate = T;
4453e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor  while (const ConstantArrayType *Array
4454e656b8397f05fd1b7c4a735372f79a52f4e32be5Douglas Gregor           = Context.getAsConstantArrayType(MaybeTemplate))
4455923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    MaybeTemplate = Array->getElementType();
4456923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
44572943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor    if (ClassTemplateSpecializationDecl *ClassTemplateSpec
4458d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
4459972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor      if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
4460972e6ce33c7e307f4b0da12bd6079bbd6ef76948Douglas Gregor        return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
4461d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                                                      TSK_ImplicitInstantiation,
4462d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                            /*Complain=*/!Diagnoser.Suppressed);
44631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (CXXRecordDecl *Rec
4464d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
4465564f4c5664f552becbd05407611a92754c40e641Richard Smith      CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass();
4466564f4c5664f552becbd05407611a92754c40e641Richard Smith      if (!Rec->isBeingDefined() && Pattern) {
4467564f4c5664f552becbd05407611a92754c40e641Richard Smith        MemberSpecializationInfo *MSI = Rec->getMemberSpecializationInfo();
4468564f4c5664f552becbd05407611a92754c40e641Richard Smith        assert(MSI && "Missing member specialization information?");
4469357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor        // This record was instantiated from a class within a template.
4470564f4c5664f552becbd05407611a92754c40e641Richard Smith        if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
4471f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor          return InstantiateClass(Loc, Rec, Pattern,
4472f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  getTemplateInstantiationArgs(Rec),
4473f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor                                  TSK_ImplicitInstantiation,
4474d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                  /*Complain=*/!Diagnoser.Suppressed);
4475d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      }
4476d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    }
4477d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  }
44782943aed177b33ae3f14273b11a7b398e5276ec62Douglas Gregor
4479d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  if (Diagnoser.Suppressed)
44805842ba9fd482bb2fe5198b32c2ae549cd5474e6dDouglas Gregor    return true;
4481d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor
44824ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // We have an incomplete type. Produce a diagnostic.
4483d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
448491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
44854ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  // If the type was a forward declaration of a class/struct/union
448601620704304f819b82ecef769ec114e541a364d7Rafael Espindola  // type, produce a note.
44874ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  if (Tag && !Tag->getDecl()->isInvalidDecl())
44881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diag(Tag->getDecl()->getLocation(),
44894ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor         Tag->isBeingDefined() ? diag::note_type_being_defined
44904ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor                               : diag::note_forward_declaration)
4491b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor      << QualType(Tag, 0);
449291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4493b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  // If the Objective-C class was a forward declaration, produce a note.
4494b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor  if (IFace && !IFace->getDecl()->isInvalidDecl())
4495b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor    Diag(IFace->getDecl()->getLocation(), diag::note_forward_class);
44964ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor
44974ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor  return true;
44984ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor}
4499e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor
4500fe6b2d481d91140923f4541f273b253291884214Douglas Gregorbool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
450191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier                               unsigned DiagID) {
4502f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4503d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  return RequireCompleteType(Loc, T, Diagnoser);
4504fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
4505fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
45066666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \brief Get diagnostic %select index for tag kind for
45076666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// literal type diagnostic message.
45086666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// WARNING: Indexes apply to particular diagnostics only!
45096666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos///
45106666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos/// \returns diagnostic %select index.
4511f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matosstatic unsigned getLiteralDiagFromTagKind(TagTypeKind Tag) {
45126666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  switch (Tag) {
4513f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Struct: return 0;
4514f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Interface: return 1;
4515f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  case TTK_Class:  return 2;
4516f143ae9b68cdd40dfb120094baaa702b810eb52cJoao Matos  default: llvm_unreachable("Invalid tag kind for literal type diagnostic!");
45176666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  }
45186666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos}
45196666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos
45209f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @brief Ensure that the type T is a literal type.
45219f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
45229f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// This routine checks whether the type @p T is a literal type. If @p T is an
45239f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// incomplete type, an attempt is made to complete it. If @p T is a literal
45249f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// type, or @p AllowIncompleteType is true and @p T is an incomplete type,
45259f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// returns false. Otherwise, this routine issues the diagnostic @p PD (giving
45269f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// it the type @p T), along with notes explaining why the type is not a
45279f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// literal type, and returns true.
45289f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
45299f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param Loc  The location in the source that the non-literal type
45309f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// diagnostic should refer to.
45319f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
45329f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @param T  The type that this routine is examining for literalness.
45339f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
4534f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor/// @param Diagnoser Emits a diagnostic if T is not a literal type.
45359f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith///
45369f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @returns @c true if @p T is not a literal type and a diagnostic was emitted,
45379f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith/// @c false otherwise.
45389f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithbool Sema::RequireLiteralType(SourceLocation Loc, QualType T,
4539f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor                              TypeDiagnoser &Diagnoser) {
45409f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  assert(!T->isDependentType() && "type should not be dependent");
45419f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4542ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  QualType ElemType = Context.getBaseElementType(T);
4543ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  RequireCompleteType(Loc, ElemType, 0);
4544ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
454586c3ae46250cdcc57778c27826060779a92f3815Richard Smith  if (T->isLiteralType())
45469f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return false;
45479f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4548f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  if (Diagnoser.Suppressed)
45499f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
45509f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4551f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  Diagnoser.diagnose(*this, Loc, T);
45529f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
45539f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (T->isVariableArrayType())
45549f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
45559f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4556ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman  const RecordType *RT = ElemType->getAs<RecordType>();
45579f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (!RT)
45589f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    return true;
45599f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
45609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
45619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
4562c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // A partially-defined class type can't be a literal type, because a literal
4563c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // class type must have a trivial destructor (which can't be checked until
4564c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  // the class definition is complete).
4565c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  if (!RD->isCompleteDefinition()) {
4566d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T);
4567ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman    return true;
4568c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smith  }
4569ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman
45709f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // If the class has virtual base classes, then it's not an aggregate, and
457186c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // cannot have any constexpr constructors or a trivial default constructor,
457286c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // so is non-literal. This is better to diagnose than the resulting absence
457386c3ae46250cdcc57778c27826060779a92f3815Richard Smith  // of constexpr constructors.
45749f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (RD->getNumVBases()) {
45759f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_virtual_base)
45766666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      << getLiteralDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases();
45779f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
45789f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           E = RD->vbases_end(); I != E; ++I)
457996a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      Diag(I->getLocStart(),
45809f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith           diag::note_constexpr_virtual_base_here) << I->getSourceRange();
458186c3ae46250cdcc57778c27826060779a92f3815Richard Smith  } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor() &&
458286c3ae46250cdcc57778c27826060779a92f3815Richard Smith             !RD->hasTrivialDefaultConstructor()) {
45839f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD;
45849f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (RD->hasNonLiteralTypeFieldsOrBases()) {
45859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
45869f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->bases_end(); I != E; ++I) {
45879f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      if (!I->getType()->isLiteralType()) {
458896a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        Diag(I->getLocStart(),
45899f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith             diag::note_non_literal_base_class)
45909f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith          << RD << I->getType() << I->getSourceRange();
45919f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
45929f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
45939f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
45949f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    for (CXXRecordDecl::field_iterator I = RD->field_begin(),
45959f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         E = RD->field_end(); I != E; ++I) {
4596262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      if (!I->getType()->isLiteralType() ||
4597262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          I->getType().isVolatileQualified()) {
4598262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        Diag(I->getLocation(), diag::note_non_literal_field)
4599581deb3da481053c4993c7600f97acf7768caac5David Blaikie          << RD << *I << I->getType()
4600262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          << I->getType().isVolatileQualified();
46019f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        return true;
46029f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      }
46039f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    }
46049f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  } else if (!RD->hasTrivialDestructor()) {
46059f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // All fields and bases are of literal types, so have trivial destructors.
46069f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // If this class's destructor is non-trivial it must be user-declared.
46079f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    CXXDestructorDecl *Dtor = RD->getDestructor();
46089f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    assert(Dtor && "class has literal fields and bases but no dtor?");
46099f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    if (!Dtor)
46109f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      return true;
46119f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
46129f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    Diag(Dtor->getLocation(), Dtor->isUserProvided() ?
46139f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_user_provided_dtor :
46149f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith         diag::note_non_literal_nontrivial_dtor) << RD;
4615ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    if (!Dtor->isUserProvided())
4616ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith      SpecialMemberIsTrivial(Dtor, CXXDestructor, /*Diagnose*/true);
46179f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  }
46189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
46199f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  return true;
46209f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}
46219f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
462291cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosierbool Sema::RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID) {
4623f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  TypeDiagnoserDiag Diagnoser(DiagID);
4624f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor  return RequireLiteralType(Loc, T, Diagnoser);
4625f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor}
4626f502d8ec9b43b259db9e37e9622279df46070fedDouglas Gregor
4627465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
4628465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara/// and qualified by the nested-name-specifier contained in SS.
4629465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraQualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
4630465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                 const CXXScopeSpec &SS, QualType T) {
4631465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T.isNull())
4632e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor    return T;
4633465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *NNS;
4634e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  if (SS.isValid())
4635465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
4636465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else {
4637465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Keyword == ETK_None)
4638465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return T;
4639465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    NNS = 0;
4640465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
4641465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Context.getElaboratedType(Keyword, NNS, T);
4642e6258936178b4c52b43b3b9dbec13552961cd645Douglas Gregor}
4643af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
46442a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
4645fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
46462a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
46472a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
46482a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
46492b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  if (!E->isTypeDependent()) {
46502b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian    QualType T = E->getType();
4651aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian    if (const TagType *TT = T->getAs<TagType>())
4652aca7f7bab0102341863a0d1bdb048d69213ae362Fariborz Jahanian      DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
46532b1d51bcf891f8887759aebb4b9e78dee8542e6dFariborz Jahanian  }
4654af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson  return Context.getTypeOfExprType(E);
4655af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
4656af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson
4657f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// getDecltypeForExpr - Given an expr, will return the decltype for
4658f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// that expression, according to the rules in C++11
4659f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor/// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18.
4660f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregorstatic QualType getDecltypeForExpr(Sema &S, Expr *E) {
4661f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (E->isTypeDependent())
4662f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    return S.Context.DependentTy;
4663f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
46646d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
46656d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   The type denoted by decltype(e) is defined as follows:
46666d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //
46676d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //     - if e is an unparenthesized id-expression or an unparenthesized class
466891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       member access (5.2.5), decltype(e) is the type of the entity named
466991cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //       by e. If there is no such entity, or if e names a set of overloaded
46706d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       functions, the program is ill-formed;
467184dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  //
467284dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  // We apply the same rules for Objective-C ivar and property references.
4673f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
4674f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
4675f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return VD->getType();
467684dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
4677f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
4678f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      return FD->getType();
467984dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCIvarRefExpr *IR = dyn_cast<ObjCIvarRefExpr>(E)) {
468084dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    return IR->getDecl()->getType();
468184dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor  } else if (const ObjCPropertyRefExpr *PR = dyn_cast<ObjCPropertyRefExpr>(E)) {
468284dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor    if (PR->isExplicitProperty())
468384dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor      return PR->getExplicitProperty()->getType();
4684f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
468584dd82e2088b1ea629f54f62a816f1155c78bb94Douglas Gregor
4686f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  // C++11 [expr.lambda.prim]p18:
4687f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   Every occurrence of decltype((x)) where x is a possibly
4688f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   parenthesized id-expression that names an entity of automatic
4689f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   storage duration is treated as if x were transformed into an
4690f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   access to a corresponding data member of the closure type that
4691f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   would have been declared if x were an odr-use of the denoted
4692f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  //   entity.
4693f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  using namespace sema;
4694f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  if (S.getCurLambda()) {
4695f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    if (isa<ParenExpr>(E)) {
4696f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4697f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
469868932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          QualType T = S.getCapturedDeclRefType(Var, DRE->getLocation());
469968932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor          if (!T.isNull())
470068932845a432d2a1dbbc57a84fd85bbb37c90487Douglas Gregor            return S.Context.getLValueReferenceType(T);
4701f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor        }
4702f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor      }
4703f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    }
4704f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  }
4705f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
4706f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
47076d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  // C++11 [dcl.type.simple]p4:
47086d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //   [...]
47096d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  QualType T = E->getType();
47106d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  switch (E->getValueKind()) {
471191cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the
47126d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
47136d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_XValue: T = S.Context.getRValueReferenceType(T); break;
471491cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier  //     - otherwise, if e is an lvalue, decltype(e) is T&, where T is the
47156d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //       type of e;
47166d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_LValue: T = S.Context.getLValueReferenceType(T); break;
47176d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  //  - otherwise, decltype(e) is the type of e.
47186d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  case VK_RValue: break;
47196d9ef30c5026e80fb398ed32bcdf69e4d714f033Douglas Gregor  }
472091cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4721f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return T;
4722f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor}
4723f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor
47242a984cad5ac3fdceeff2bd99daa7b90979313475John McCallQualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
4725fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall  ExprResult ER = CheckPlaceholderExpr(E);
47262a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  if (ER.isInvalid()) return QualType();
47272a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  E = ER.take();
472891cbbbf506c892a26d4301e2b3ccd377b0938817Chad Rosier
4729f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor  return Context.getDecltypeType(E, getDecltypeForExpr(*this, E));
4730af017e682918f7a1a95ff08d9ab7ae3426436ca3Anders Carlsson}
4731ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
4732ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType Sema::BuildUnaryTransformType(QualType BaseType,
4733ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UnaryTransformType::UTTKind UKind,
4734ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       SourceLocation Loc) {
4735ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  switch (UKind) {
4736ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case UnaryTransformType::EnumUnderlyingType:
4737ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) {
4738ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      Diag(Loc, diag::err_only_enums_have_underlying_types);
4739ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return QualType();
4740ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    } else {
4741ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      QualType Underlying = BaseType;
4742ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      if (!BaseType->isDependentType()) {
4743ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl();
4744ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        assert(ED && "EnumType has no EnumDecl");
4745ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        DiagnoseUseOfDecl(ED, Loc);
4746ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt        Underlying = ED->getIntegerType();
4747ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      }
4748ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      assert(!Underlying.isNull());
4749ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return Context.getUnaryTransformType(BaseType, Underlying,
4750ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                        UnaryTransformType::EnumUnderlyingType);
4751ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    }
4752ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  }
4753ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  llvm_unreachable("unknown unary transform type");
4754ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
4755b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4756b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) {
4757b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T->isDependentType()) {
47588327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // FIXME: It isn't entirely clear whether incomplete atomic types
47598327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    // are allowed or not; for simplicity, ban them for the moment.
4760d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (RequireCompleteType(Loc, T, diag::err_atomic_specifier_bad_type, 0))
47618327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith      return QualType();
47628327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith
4763b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    int DisallowedKind = -1;
47648327118ff60cd9c4812fba1e5ba4eb3cb5ed3401Richard Smith    if (T->isArrayType())
4765b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 1;
4766b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isFunctionType())
4767b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 2;
4768b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isReferenceType())
4769b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 3;
4770b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T->isAtomicType())
4771b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 4;
4772b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (T.hasQualifiers())
4773b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 5;
4774b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    else if (!T.isTriviallyCopyableType(Context))
4775b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      // Some other non-trivially-copyable type (probably a C++ class)
4776b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      DisallowedKind = 6;
4777b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4778b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (DisallowedKind != -1) {
4779b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T;
4780b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return QualType();
4781b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
4782b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4783b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // FIXME: Do we need any handling for ARC here?
4784b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
4785b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
4786b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Build the pointer type.
4787b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return Context.getAtomicType(T);
4788b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
4789