Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ReferencesPool.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 
6 using SiliconStudio.Paradox.Shaders.Parser.Analysis;
7 using SiliconStudio.Shaders.Ast;
8 
9 namespace SiliconStudio.Paradox.Shaders.Parser.Mixins
10 {
11  internal class ReferencesPool
12  {
13  /// <summary>
14  /// List of all the variable references
15  /// </summary>
16  public Dictionary<Variable, HashSet<ExpressionNodeCouple>> VariablesReferences { get; private set; }
17 
18  /// <summary>
19  /// List of all the variable references
20  /// </summary>
21  public Dictionary<MethodDeclaration, HashSet<MethodInvocationExpression>> MethodsReferences { get; private set; }
22 
23  public ReferencesPool()
24  {
25  VariablesReferences = new Dictionary<Variable, HashSet<ExpressionNodeCouple>>();
26  MethodsReferences = new Dictionary<MethodDeclaration, HashSet<MethodInvocationExpression>>();
27  }
28 
29  /// <summary>
30  /// Merge the argument references into this one
31  /// </summary>
32  /// <param name="pool">the ReferencePool</param>
33  public void Merge(ReferencesPool pool)
34  {
35  // merge the VariablesReferences
36  foreach (var variableReference in pool.VariablesReferences)
37  {
38  if (!VariablesReferences.ContainsKey(variableReference.Key))
39  VariablesReferences.Add(variableReference.Key, new HashSet<ExpressionNodeCouple>());
40 
41  VariablesReferences[variableReference.Key].UnionWith(variableReference.Value);
42  }
43  // merge the MethodsReferences
44  foreach (var methodReference in pool.MethodsReferences)
45  {
46  if (!MethodsReferences.ContainsKey(methodReference.Key))
47  MethodsReferences.Add(methodReference.Key, new HashSet<MethodInvocationExpression>());
48 
49  MethodsReferences[methodReference.Key].UnionWith(methodReference.Value);
50  }
51  }
52 
53  /// <summary>
54  /// Regen the keys bacause they could have been modified
55  /// </summary>
56  public void RegenKeys()
57  {
58  VariablesReferences = VariablesReferences.ToDictionary(variable => variable.Key, variable => variable.Value);
59  MethodsReferences = MethodsReferences.ToDictionary(method => method.Key, variable => variable.Value);
60  }
61 
62  /// <summary>
63  /// Insert a variable reference
64  /// </summary>
65  /// <param name="variable">the variable</param>
66  /// <param name="expression">the reference</param>
67  public void InsertVariable(Variable variable, ExpressionNodeCouple expression)
68  {
69  if (!VariablesReferences.ContainsKey(variable))
70  VariablesReferences.Add(variable, new HashSet<ExpressionNodeCouple>());
71  VariablesReferences[variable].Add(expression);
72  }
73 
74  /// <summary>
75  /// Insert a method reference
76  /// </summary>
77  /// <param name="methodDeclaration">the method</param>
78  /// <param name="expression">the reference</param>
79  public void InsertMethod(MethodDeclaration methodDeclaration, MethodInvocationExpression expression)
80  {
81  if (!MethodsReferences.ContainsKey(methodDeclaration))
82  MethodsReferences.Add(methodDeclaration, new HashSet<MethodInvocationExpression>());
83  MethodsReferences[methodDeclaration].Add(expression);
84  }
85  }
86 }
A variable declaration.
Definition: Variable.cs:11