Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ScopeDeclaration.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 
5 using System.Linq;
6 using SiliconStudio.Shaders.Ast;
7 
8 namespace SiliconStudio.Shaders.Visitor
9 {
10  /// <summary>
11  /// A Scope declaration provides a way to retrieve all scope declaration (variable, methods...etc.)
12  /// and attached nodes.
13  /// </summary>
14  public class ScopeDeclaration
15  {
16  #region Constructors and Destructors
17 
18  /// <summary>
19  /// Initializes a new instance of the <see cref="ScopeDeclaration"/> class.
20  /// </summary>
21  public ScopeDeclaration() : this(null)
22  {
23  }
24 
25  /// <summary>
26  /// Initializes a new instance of the <see cref="ScopeDeclaration"/> class.
27  /// </summary>
28  /// <param name="scopeContainer">The scope container.</param>
29  public ScopeDeclaration(IScopeContainer scopeContainer)
30  {
31  ScopeContainer = scopeContainer;
32  Declarations = new Dictionary<string, List<IDeclaration>>();
33  Generics = new Dictionary<string, List<GenericDeclaration>>();
34  }
35 
36  #endregion
37 
38  #region Public Properties
39 
40  /// <summary>
41  /// Gets or sets the scope container.
42  /// </summary>
43  /// <value>
44  /// The scope container.
45  /// </value>
46  public IScopeContainer ScopeContainer { get; set; }
47 
48  /// <summary>
49  /// Gets or sets the declarations.
50  /// </summary>
51  /// <value>
52  /// The declarations.
53  /// </value>
54  public IDictionary<string, List<IDeclaration>> Declarations { get; private set; }
55 
56  public IDictionary<string, List<GenericDeclaration>> Generics { get; private set; }
57 
59  {
60  List<IDeclaration> declarationList;
61  if (Declarations.TryGetValue(name, out declarationList))
62  return declarationList;
63 
64  return Enumerable.Empty<IDeclaration>();
65  }
66 
68  {
69  List<GenericDeclaration> declarationList;
70  if (Generics.TryGetValue(name, out declarationList))
71  return declarationList;
72 
73  return Enumerable.Empty<GenericDeclaration>();
74  }
75 
76  public void AddDeclaration(IDeclaration declaration)
77  {
78  List<IDeclaration> declarations;
79  if (declaration.Name != null)
80  {
81 
82  string name = declaration.Name.Text;
83  if (string.IsNullOrEmpty(name))
84  return;
85 
86  if (!Declarations.TryGetValue(name, out declarations))
87  {
88  declarations = new List<IDeclaration>();
89  Declarations.Add(name, declarations);
90  }
91  }
92  else
93  {
94  declarations = new List<IDeclaration>();
95  }
96 
97  var genericsDeclarator = declaration as IGenerics;
98  if (genericsDeclarator != null)
99  {
100  for (int i = 0; i < genericsDeclarator.GenericParameters.Count; i++)
101  {
102  var genericArgument = genericsDeclarator.GenericParameters[i];
103  var genericName = genericArgument.Name.Text;
104  List<GenericDeclaration> generics;
105  if (!Generics.TryGetValue(genericName, out generics))
106  {
107  generics = new List<GenericDeclaration>();
108  Generics.Add(genericName, generics);
109  }
110 
111  generics.Add(new GenericDeclaration(genericArgument.Name, genericsDeclarator, i, false) { Span = genericArgument.Span });
112  }
113  }
114 
115  if (!declarations.Contains(declaration))
116  declarations.Add(declaration);
117  }
118 
119  public void AddDeclarations(IEnumerable<IDeclaration> declarations)
120  {
121  foreach (var declaration in declarations)
122  AddDeclaration(declaration);
123  }
124 
125  public void RemoveDeclaration(IDeclaration declaration)
126  {
127  string name = declaration.Name.Text;
128  if (string.IsNullOrEmpty(name))
129  return;
130 
131  List<IDeclaration> declarations;
132  if (Declarations.TryGetValue(name, out declarations))
133  declarations.Remove(declaration);
134  }
135 
136  #endregion
137 
138  public class DeclarationList
139  {
141  {
142  Standard = new List<IDeclaration>();
143  Generics = new List<IGenerics>();
144  }
145 
146  public List<IDeclaration> Standard;
147 
148  public List<IGenerics> Generics;
149 
150  public void Add(IDeclaration declaration)
151  {
152  Standard.Add(declaration);
153  var genDecl = declaration as IGenerics;
154  if (genDecl != null && genDecl.GenericParameters.Count > 0)
155  {
156  Generics.Add((IGenerics)declaration);
157  }
158  }
159 
160  public void Remove(IDeclaration declaration)
161  {
162  Standard.Remove(declaration);
163  if (declaration is IGenerics)
164  Generics.Remove((IGenerics)declaration);
165  }
166  }
167  }
168 }
void AddDeclarations(IEnumerable< IDeclaration > declarations)
A tag interface to identify a container for scope declarations.
Identifier Name
Gets or sets the name of this declaration
Definition: IDeclaration.cs:16
A generic declaration. This is used internally to identify a generic declaration. ...
A Scope declaration provides a way to retrieve all scope declaration (variable, methods...etc.) and attached nodes.
ScopeDeclaration()
Initializes a new instance of the ScopeDeclaration class.
IEnumerable< GenericDeclaration > FindGenerics(string name)
void RemoveDeclaration(IDeclaration declaration)
An interface used by generic definitions and instance.
Definition: IGenerics.cs:10
ScopeDeclaration(IScopeContainer scopeContainer)
Initializes a new instance of the ScopeDeclaration class.
void AddDeclaration(IDeclaration declaration)
IEnumerable< IDeclaration > FindDeclaration(string name)
Toplevel interface for a declaration.
Definition: IDeclaration.cs:8