Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ShaderVisitor.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System.Collections.Generic;
4 using System.Linq;
5 using SiliconStudio.Shaders.Ast;
6 
7 namespace SiliconStudio.Shaders.Visitor
8 {
9  /// <summary>
10  /// A Generic Visitor.
11  /// </summary>
12  /// <remarks>
13  /// An derived classs need to set the Iterator with this instance.
14  /// </remarks>
15  public abstract class ShaderVisitor : VisitorBase
16  {
17  private readonly NodeProcessor nodeProcessor;
18 
19  #region Constructors and Destructors
20 
21  /// <summary>
22  /// Initializes a new instance of the <see cref="ShaderVisitor"/> class.
23  /// </summary>
24  /// <param name="buildScopeDeclaration">if set to <c>true</c> [build scope declaration].</param>
25  /// <param name="useNodeStack">if set to <c>true</c> [use node stack].</param>
26  protected ShaderVisitor(bool buildScopeDeclaration, bool useNodeStack) : base(useNodeStack)
27  {
28  nodeProcessor = OnNodeProcessor;
29  if (buildScopeDeclaration)
30  {
31  ScopeStack = new Stack<ScopeDeclaration>();
32  ScopeStack.Push(this.NewScope());
33  }
34  }
35 
36  #endregion
37 
38  #region Properties
39 
40  protected virtual ScopeDeclaration NewScope(IScopeContainer container = null)
41  {
42  return new ScopeDeclaration(container);
43  }
44 
45  /// <summary>
46  /// Gets the parent node or null if no parents
47  /// </summary>
48  public Node ParentNode
49  {
50  get
51  {
52  return NodeStack.Count > 1 ? NodeStack[NodeStack.Count - 2] : null;
53  }
54  }
55 
56  /// <summary>
57  /// Gets the scope stack.
58  /// </summary>
59  protected Stack<ScopeDeclaration> ScopeStack { get; private set; }
60 
61  #endregion
62 
63  #region Public Methods
64 
65  [Visit]
66  protected virtual Node Visit(Node node)
67  {
68  return node.Childrens(nodeProcessor);
69  }
70 
71  private Node OnNodeProcessor(Node nodeArg, ref NodeProcessorContext explorer)
72  {
73  return VisitDynamic(nodeArg);
74  }
75 
76  /// <summary>
77  /// Finds a list of declaration by its name.
78  /// </summary>
79  /// <param name="name">The name.</param>
80  /// <returns>A list of declaration</returns>
81  protected virtual IEnumerable<IDeclaration> FindDeclarations(string name)
82  {
83  return ScopeStack.SelectMany(scopeDecl => scopeDecl.FindDeclaration(name));
84  }
85 
86  /// <summary>
87  /// Finds a declaration by its name.
88  /// </summary>
89  /// <param name="name">The name.</param>
90  /// <returns>A declaration</returns>
91  protected IDeclaration FindDeclaration(string name)
92  {
93  return FindDeclarations(name).FirstOrDefault();
94  }
95 
96  protected override bool PreVisitNode(Node node)
97  {
98  if (ScopeStack != null)
99  {
100  var declaration = node as IDeclaration;
101  if (declaration != null)
102  {
103  // If this is a variable, add only instance variables
104  if (declaration is Variable)
105  {
106  foreach (var variable in ((Variable)declaration).Instances())
107  ScopeStack.Peek().AddDeclaration(variable);
108  }
109  else
110  {
111  ScopeStack.Peek().AddDeclaration(declaration);
112  }
113  }
114 
115  var scopeContainer = node as IScopeContainer;
116  if (scopeContainer != null)
117  {
118  ScopeStack.Push(this.NewScope(scopeContainer));
119  }
120  }
121  return true;
122  }
123 
124  protected override void PostVisitNode(Node node, bool nodeVisited)
125  {
126  if (ScopeStack != null && node is IScopeContainer)
127  ScopeStack.Pop();
128  }
129 
130  #endregion
131  }
132 }
delegate Node NodeProcessor(Node node, ref NodeProcessorContext nodeProcessorContext)
Processor for a single node.
virtual ScopeDeclaration NewScope(IScopeContainer container=null)
A tag interface to identify a container for scope declarations.
A Scope declaration provides a way to retrieve all scope declaration (variable, methods...etc.) and attached nodes.
Abstract node.
Definition: Node.cs:15
ShaderVisitor(bool buildScopeDeclaration, bool useNodeStack)
Initializes a new instance of the ShaderVisitor class.
A variable declaration.
Definition: Variable.cs:11
override bool PreVisitNode(Node node)
Called before visiting the node.
IDeclaration FindDeclaration(string name)
Finds a declaration by its name.
virtual IEnumerable< IDeclaration > FindDeclarations(string name)
Finds a list of declaration by its name.
Toplevel interface for a declaration.
Definition: IDeclaration.cs:8
override void PostVisitNode(Node node, bool nodeVisited)
Called after visiting the node.