119510856727e0e14a3696b2a72c35163bff2a71fJohn McCall//===--- ParsedTemplate.h - Template Parsing Data Types -------------------===//
2314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor//
3314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor//                     The LLVM Compiler Infrastructure
4314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor//
5314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor// This file is distributed under the University of Illinois Open Source
6314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor// License. See LICENSE.TXT for details.
7314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor//
8314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor//===----------------------------------------------------------------------===//
9314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor//
10314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor//  This file provides data structures that store the parsed representation of
11314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor//  templates.
12314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor//
13314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor//===----------------------------------------------------------------------===//
1419510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#ifndef LLVM_CLANG_SEMA_PARSEDTEMPLATE_H
1519510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#define LLVM_CLANG_SEMA_PARSEDTEMPLATE_H
16314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
1719510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
1819510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/Ownership.h"
19314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor#include <cassert>
20314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
21314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregornamespace clang {
22314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  /// \brief Represents the parsed form of a C++ template argument.
23314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  class ParsedTemplateArgument {
24314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  public:
25314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \brief Describes the kind of template argument that was parsed.
26314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    enum KindType {
27314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      /// \brief A template type parameter, stored as a type.
28314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      Type,
29314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      /// \brief A non-type template parameter, stored as an expression.
30314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      NonType,
31314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      /// \brief A template template argument, stored as a template name.
32314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      Template
33314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    };
34314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
357536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    /// \brief Build an empty template argument.
367536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    ///
377536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    /// This template argument is invalid.
38314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ParsedTemplateArgument() : Kind(Type), Arg(0) { }
39314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
40314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \brief Create a template type argument or non-type template argument.
41314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ///
42314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \param Arg the template type argument or non-type template argument.
43314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \param Loc the location of the type.
44314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ParsedTemplateArgument(KindType Kind, void *Arg, SourceLocation Loc)
45314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      : Kind(Kind), Arg(Arg), Loc(Loc) { }
46314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
47314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \brief Create a template template argument.
48314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ///
49314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \param SS the C++ scope specifier that precedes the template name, if
50314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// any.
51314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ///
52314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \param Template the template to which this template template
53314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// argument refers.
54314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ///
55314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \param TemplateLoc the location of the template name.
56314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ParsedTemplateArgument(const CXXScopeSpec &SS,
57b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                           ParsedTemplateTy Template,
58314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor                           SourceLocation TemplateLoc)
592b5289b6fd7e3d9899868410a498c081c9595662John McCall      : Kind(ParsedTemplateArgument::Template),
602b5289b6fd7e3d9899868410a498c081c9595662John McCall        Arg(Template.getAsOpaquePtr()),
61db3d68f188eb0883407a25ef09c1d85e217b97efBenjamin Kramer        SS(SS), Loc(TemplateLoc), EllipsisLoc() { }
62314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
63314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \brief Determine whether the given template argument is invalid.
64bb3310a5fd9c08741b5272ed2665bdeab76a99a6Douglas Gregor    bool isInvalid() const { return Arg == 0; }
65314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
66314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \brief Determine what kind of template argument we have.
67314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    KindType getKind() const { return Kind; }
68314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
69314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \brief Retrieve the template type argument's type.
70b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType getAsType() const {
71314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      assert(Kind == Type && "Not a template type argument");
72b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParsedType::getFromOpaquePtr(Arg);
73314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    }
74314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
75314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \brief Retrieve the non-type template argument's expression.
76ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    Expr *getAsExpr() const {
77314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      assert(Kind == NonType && "Not a non-type template argument");
78ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall      return static_cast<Expr*>(Arg);
79314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    }
80314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
81314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \brief Retrieve the template template argument's template name.
82b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedTemplateTy getAsTemplate() const {
83314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      assert(Kind == Template && "Not a template template argument");
84b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParsedTemplateTy::getFromOpaquePtr(Arg);
85314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    }
86314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
87314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \brief Retrieve the location of the template argument.
88314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    SourceLocation getLocation() const { return Loc; }
89314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
90314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \brief Retrieve the nested-name-specifier that precedes the template
91314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// name in a template template argument.
92314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    const CXXScopeSpec &getScopeSpec() const {
93314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      assert(Kind == Template &&
94314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor             "Only template template arguments can have a scope specifier");
95314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      return SS;
96314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    }
97314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
98ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor    /// \brief Retrieve the location of the ellipsis that makes a template
99ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor    /// template argument into a pack expansion.
100ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor    SourceLocation getEllipsisLoc() const {
101ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor      assert(Kind == Template &&
102ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor             "Only template template arguments can have an ellipsis");
103ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor      return EllipsisLoc;
104ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor    }
105ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor
106ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor    /// \brief Retrieve a pack expansion of the given template template
107ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor    /// argument.
108ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor    ///
109ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor    /// \param EllipsisLoc The location of the ellipsis.
110ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor    ParsedTemplateArgument getTemplatePackExpansion(
111ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor                                              SourceLocation EllipsisLoc) const;
112ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor
113314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  private:
114314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    KindType Kind;
115314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
116314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \brief The actual template argument representation, which may be
117f81e5a9e3f3ff80c56e4afb4fe6311a8735f36e8Richard Trieu    /// an \c ActionBase::TypeTy* (for a type), an Expr* (for an
118314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// expression), or an ActionBase::TemplateTy (for a template).
119314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    void *Arg;
120314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
121314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \brief The nested-name-specifier that can accompany a template template
122314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// argument.
123314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    CXXScopeSpec SS;
124db3d68f188eb0883407a25ef09c1d85e217b97efBenjamin Kramer
125db3d68f188eb0883407a25ef09c1d85e217b97efBenjamin Kramer    /// \brief the location of the template argument.
126db3d68f188eb0883407a25ef09c1d85e217b97efBenjamin Kramer    SourceLocation Loc;
127db3d68f188eb0883407a25ef09c1d85e217b97efBenjamin Kramer
128ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor    /// \brief The ellipsis location that can accompany a template template
129ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor    /// argument (turning it into a template template argument expansion).
130ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor    SourceLocation EllipsisLoc;
131314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  };
132314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
133314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  /// \brief Information about a template-id annotation
134314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  /// token.
135314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  ///
136314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  /// A template-id annotation token contains the template declaration,
137314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  /// template arguments, whether those template arguments were types,
138314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  /// expressions, or template names, and the source locations for important
139314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  /// tokens. All of the information about template arguments is allocated
140314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  /// directly after this structure.
141314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  struct TemplateIdAnnotation {
142059101f922de6eb765601459925f4c8914420b23Douglas Gregor    /// \brief The nested-name-specifier that precedes the template name.
143059101f922de6eb765601459925f4c8914420b23Douglas Gregor    CXXScopeSpec SS;
144e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
145e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    /// TemplateKWLoc - The location of the template keyword within the
146e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    /// source.
147e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    SourceLocation TemplateKWLoc;
148e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara
149314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// TemplateNameLoc - The location of the template name within the
150314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// source.
151314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    SourceLocation TemplateNameLoc;
152314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
153314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// FIXME: Temporarily stores the name of a specialization
154314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    IdentifierInfo *Name;
155314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
156314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// FIXME: Temporarily stores the overloaded operator kind.
157314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    OverloadedOperatorKind Operator;
158314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
159314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// The declaration of the template corresponding to the
1602b5289b6fd7e3d9899868410a498c081c9595662John McCall    /// template-name.
1612b5289b6fd7e3d9899868410a498c081c9595662John McCall    ParsedTemplateTy Template;
162314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
163314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// The kind of template that Template refers to.
164314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    TemplateNameKind Kind;
165314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
166314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// The location of the '<' before the template argument
167314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// list.
168314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    SourceLocation LAngleLoc;
169314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
170314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// The location of the '>' after the template argument
171314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// list.
172314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    SourceLocation RAngleLoc;
173314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
174314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// NumArgs - The number of template arguments.
175314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    unsigned NumArgs;
176314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
177314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    /// \brief Retrieves a pointer to the template arguments
178314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    ParsedTemplateArgument *getTemplateArgs() {
179314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      return reinterpret_cast<ParsedTemplateArgument *>(this + 1);
180314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    }
18113bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer
18213bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    /// \brief Creates a new TemplateIdAnnotation with NumArgs arguments and
18313bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    /// appends it to List.
18413bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    static TemplateIdAnnotation *
18513bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer    Allocate(unsigned NumArgs, SmallVectorImpl<TemplateIdAnnotation*> &List) {
186314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      TemplateIdAnnotation *TemplateId
187059101f922de6eb765601459925f4c8914420b23Douglas Gregor        = (TemplateIdAnnotation *)std::malloc(sizeof(TemplateIdAnnotation) +
188314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor                                      sizeof(ParsedTemplateArgument) * NumArgs);
189314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      TemplateId->NumArgs = NumArgs;
190c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
191059101f922de6eb765601459925f4c8914420b23Douglas Gregor      // Default-construct nested-name-specifier.
192059101f922de6eb765601459925f4c8914420b23Douglas Gregor      new (&TemplateId->SS) CXXScopeSpec();
193059101f922de6eb765601459925f4c8914420b23Douglas Gregor
194c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor      // Default-construct parsed template arguments.
195c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor      ParsedTemplateArgument *TemplateArgs = TemplateId->getTemplateArgs();
196c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor      for (unsigned I = 0; I != NumArgs; ++I)
197c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor        new (TemplateArgs + I) ParsedTemplateArgument();
198c34348a7ef1a6b3f92a644a227953800cd1f9947Douglas Gregor
19913bb701f2f876356400a34b0917a417c66b5d70dBenjamin Kramer      List.push_back(TemplateId);
200314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor      return TemplateId;
201314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor    }
202314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
203059101f922de6eb765601459925f4c8914420b23Douglas Gregor    void Destroy() {
204059101f922de6eb765601459925f4c8914420b23Douglas Gregor      SS.~CXXScopeSpec();
205059101f922de6eb765601459925f4c8914420b23Douglas Gregor      free(this);
206059101f922de6eb765601459925f4c8914420b23Douglas Gregor    }
207314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor  };
20878b810559d89e996e00684335407443936ce34a1John McCall
20978b810559d89e996e00684335407443936ce34a1John McCall  /// Retrieves the range of the given template parameter lists.
21078b810559d89e996e00684335407443936ce34a1John McCall  SourceRange getTemplateParamsRange(TemplateParameterList const *const *Params,
21178b810559d89e996e00684335407443936ce34a1John McCall                                     unsigned NumParams);
212314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor}
213314b97f8c564b465af605efaee23f91ec18a982bDouglas Gregor
214c1643de31b9acff14b88354c0b13025407db9e8aBenjamin Kramer#endif
215