Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Variable.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;
4 using System.Collections.Generic;
5 
6 namespace SiliconStudio.Shaders.Ast
7 {
8  /// <summary>
9  /// A variable declaration.
10  /// </summary>
12  {
13  #region Constructors and Destructors
14 
15  /// <summary>
16  /// Initializes a new instance of the <see cref = "Variable" /> class.
17  /// </summary>
18  public Variable()
19  {
20  Attributes = new List<AttributeBase>();
21  Qualifiers = Qualifier.None;
22  }
23 
24  /// <summary>
25  /// Initializes a new instance of the <see cref="Variable"/> class.
26  /// </summary>
27  /// <param name="type">The type.</param>
28  /// <param name="name">The name.</param>
29  /// <param name="initialValue">The initial value.</param>
30  public Variable(TypeBase type, string name, Expression initialValue = null)
31  {
32  Type = type;
33  Name = new Identifier(name);
34  InitialValue = initialValue;
35  Attributes = new List<AttributeBase>();
36  Qualifiers = Qualifier.None;
37  }
38 
39  #endregion
40 
41  #region Public Properties
42 
43 
44  /// <inheritdoc />
45  public List<AttributeBase> Attributes { get; set; }
46 
47  /// <summary>
48  /// Gets or sets the qualifiers.
49  /// </summary>
50  /// <value>
51  /// The qualifiers.
52  /// </value>
53  public Qualifier Qualifiers { get; set; }
54 
55  /// <summary>
56  /// Gets or sets the type.
57  /// </summary>
58  /// <value>
59  /// The type.
60  /// </value>
61  public TypeBase Type { get; set; }
62 
63  /// <summary>
64  /// Gets or sets the initial value.
65  /// </summary>
66  /// <value>
67  /// The initial value.
68  /// </value>
69  public Expression InitialValue { get; set; }
70 
71  /// <summary>
72  /// Gets or sets the name.
73  /// </summary>
74  /// <value>
75  /// The name.
76  /// </value>
77  public Identifier Name { get; set; }
78 
79  /// <summary>
80  /// Gets or sets the sub variables (used only for variable group)
81  /// </summary>
82  /// <value>
83  /// The sub variables inside this group.
84  /// </value>
85  public List<Variable> SubVariables { get; set; }
86 
87  /// <summary>
88  /// Gets a value indicating whether this instance is group.
89  /// </summary>
90  /// <value>
91  /// <c>true</c> if this instance is group; otherwise, <c>false</c>.
92  /// </value>
93  public bool IsGroup
94  {
95  get
96  {
97  return SubVariables != null && SubVariables.Count > 0;
98  }
99  }
100 
101  /// <summary>
102  /// Returns single variable instances.
103  /// </summary>
104  /// <returns>An enumeration of single variable instances</returns>
106  {
107  if (IsGroup)
108  {
109  foreach (var subVariable in SubVariables)
110  {
111  yield return subVariable;
112  }
113  }
114  else
115  {
116  yield return this;
117  }
118  }
119 
120  /// <summary>
121  /// Merges attributes and qualifiers from another variable.
122  /// </summary>
123  /// <param name="from">The variable to merge attribute from.</param>
124  public void MergeFrom(Variable from)
125  {
126  Qualifiers |= from.Qualifiers;
127  Attributes.AddRange(from.Attributes);
128  }
129 
130  #endregion
131 
132  #region Public Methods
133 
134  /// <inheritdoc />
135  public override IEnumerable<Node> Childrens()
136  {
137  ChildrenList.Clear();
138  ChildrenList.Add(Type);
139  ChildrenList.Add(Name);
140  if (Qualifiers != Qualifier.None) ChildrenList.Add(Qualifiers);
141  if (InitialValue != null) ChildrenList.Add(InitialValue);
142  if (SubVariables != null)
143  ChildrenList.AddRange(SubVariables);
144  return ChildrenList;
145  }
146 
147  /// <inheritdoc />
148  public override string ToString()
149  {
150  return string.Format(
151  "{0}{1} {2}{3}{4}",
152  Qualifiers.ToString(false),
153  Type,
154  Name,
155  Qualifiers.ToString(true),
156  InitialValue != null ? " = " + InitialValue : string.Empty);
157  }
158 
159  /// <inheritdoc />
160  public override int GetHashCode()
161  {
162  unchecked
163  {
164  return ((Name != null ? Name.GetHashCode() : 0) * 397) ^ (Type != null ? Type.GetHashCode() : 0);
165  }
166  }
167  #endregion
168  }
169 }
Variable()
Initializes a new instance of the Variable class.
Definition: Variable.cs:18
override IEnumerable< Node > Childrens()
Gets the child nodes. An enumeration of child nodes
Definition: Variable.cs:135
void MergeFrom(Variable from)
Merges attributes and qualifiers from another variable.
Definition: Variable.cs:124
Base interface for all node providing qualifiers.
Definition: IQualifiers.cs:8
Abstract node.
Definition: Node.cs:15
A variable declaration.
Definition: Variable.cs:11
Base type for all types.
Definition: TypeBase.cs:11
IEnumerable< Variable > Instances()
Returns single variable instances.
Definition: Variable.cs:105
Variable(TypeBase type, string name, Expression initialValue=null)
Initializes a new instance of the Variable class.
Definition: Variable.cs:30
Toplevel interface for a declaration.
Definition: IDeclaration.cs:8
static readonly Qualifier None
None Enum.
Definition: Qualifier.cs:18