Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
MethodDeclaration.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;
5 using System.Collections.Generic;
6 
7 namespace SiliconStudio.Shaders.Ast
8 {
9  /// <summary>
10  /// Declaration of a method.
11  /// </summary>
13  {
14  #region Constructors and Destructors
15 
16  /// <summary>
17  /// Initializes a new instance of the <see cref = "MethodDeclaration" /> class.
18  /// </summary>
20  {
21  Attributes = new List<AttributeBase>();
22  Parameters = new List<Parameter>();
23  Qualifiers = Qualifier.None;
24  ParameterConstraints = new List<GenericParameterConstraint>();
25  }
26 
27  #endregion
28 
29  #region Public Properties
30 
31  /// <summary>
32  /// Gets or sets the attributes.
33  /// </summary>
34  /// <value>
35  /// The attributes.
36  /// </value>
37  /// <inhericdoc />
38  public List<AttributeBase> Attributes { get; set; }
39 
40  /// <summary>
41  /// Gets or sets the name.
42  /// </summary>
43  /// <value>
44  /// The name.
45  /// </value>
46  public Identifier Name { get; set; }
47 
48  /// <summary>
49  /// Gets or sets the parameter constraints.
50  /// </summary>
51  /// <value>
52  /// The parameter constraints.
53  /// </value>
54  public List<GenericParameterConstraint> ParameterConstraints { get; set; }
55 
56  /// <summary>
57  /// Gets or sets the parameters.
58  /// </summary>
59  /// <value>
60  /// The parameters.
61  /// </value>
62  public List<Parameter> Parameters { get; set; }
63 
64  /// <summary>
65  /// Gets or sets the storage class.
66  /// </summary>
67  /// <value>
68  /// The storage class.
69  /// </value>
70  public Qualifier Qualifiers { get; set; }
71 
72  /// <summary>
73  /// Gets or sets the type of the return.
74  /// </summary>
75  /// <value>
76  /// The type of the return.
77  /// </value>
78  public TypeBase ReturnType { get; set; }
79 
80  /// <summary>
81  /// Gets or sets a value indicating whether this instance is builtin.
82  /// </summary>
83  /// <value>
84  /// <c>true</c> if this instance is builtin; otherwise, <c>false</c>.
85  /// </value>
86  public bool IsBuiltin { get; set; }
87 
88  #endregion
89 
90  #region Public Methods
91 
92  /// <summary>
93  /// Checks the constraint.
94  /// </summary>
95  /// <param name="parameterType">Type of the parameter.</param>
96  /// <param name="typeToCheck">The type to check.</param>
97  /// <returns></returns>
98  public bool CheckConstraint(GenericParameterType parameterType, TypeBase typeToCheck)
99  {
100  foreach (var genericParameterConstraint in ParameterConstraints)
101  {
102  if (genericParameterConstraint.Name == parameterType.Name)
103  {
104  return genericParameterConstraint.Constraint(typeToCheck);
105  }
106  }
107  return false;
108  }
109 
110  /// <summary>
111  /// Test if a method declaration has the same signature.
112  /// </summary>
113  /// <param name="methodDeclaration">The method declaration.</param>
114  /// <returns>True if the method passed has the same signature</returns>
115  public bool IsSameSignature(MethodDeclaration methodDeclaration)
116  {
117  if (methodDeclaration == null)
118  return false;
119 
120  if (Name != methodDeclaration.Name)
121  return false;
122  if (Parameters.Count != methodDeclaration.Parameters.Count)
123  return false;
124  for (int i = 0; i < Parameters.Count; i++)
125  {
126  var parameter = Parameters[i];
127  var parameterAgainst = methodDeclaration.Parameters[i];
128  var parameterType = parameter.Type.ResolveType();
129  var parameterAgainstType = parameterAgainst.Type.ResolveType();
130  if (parameterType != parameterAgainstType)
131  {
132  return false;
133  }
134  }
135  return true;
136  }
137 
138  /// <summary>
139  /// Test if a method invocation expression has the same signature.
140  /// </summary>
141  /// <param name="methodInvocationExpression">The method invocation expression.</param>
142  /// <returns>True if the method passed has the same signature</returns>
143  public bool IsSameSignature(MethodInvocationExpression methodInvocationExpression)
144  {
145  if (methodInvocationExpression == null)
146  return false;
147 
148  Identifier methodName;
149  var target = methodInvocationExpression.Target as MemberReferenceExpression;
150  if (target != null)
151  methodName = target.Member;
152  else
153  {
154  var vre = methodInvocationExpression.Target as VariableReferenceExpression;
155  if (vre == null)
156  return false;
157  methodName = vre.Name;
158  }
159 
160  if (Name != methodName)
161  return false;
162  if (Parameters.Count != methodInvocationExpression.Arguments.Count)
163  return false;
164  for (int i = 0; i < Parameters.Count; i++)
165  {
166  var parameter = Parameters[i];
167  var parameterAgainst = methodInvocationExpression.Arguments[i];
168  var parameterType = parameter.Type.ResolveType();
169 
170  if (parameterAgainst.TypeInference.TargetType == null)
171  return false;
172 
173  var parameterAgainstType = parameterAgainst.TypeInference.TargetType.ResolveType();
174  if (parameterType != parameterAgainstType)
175  return false;
176  }
177  return true;
178  }
179 
180  /// <summary>
181  /// Copies declartion to another instance.
182  /// </summary>
183  /// <param name="target">The target instance.</param>
184  public void CopyTo(MethodDeclaration target)
185  {
186  target.Attributes = Attributes;
187  target.Name = Name;
188  target.Parameters = Parameters;
189  target.Qualifiers = Qualifiers;
190  target.ReturnType = ReturnType;
191  }
192 
193  /// <inheritdoc />
194  public override IEnumerable<Node> Childrens()
195  {
196  ChildrenList.Clear();
197  ChildrenList.Add(ReturnType);
198  ChildrenList.Add(Name);
199  foreach (var variableDeclarator in Parameters)
200  {
201  ChildrenList.Add(variableDeclarator);
202  }
203  if (Qualifiers != Qualifier.None)
204  {
205  ChildrenList.Add(Qualifiers);
206  }
207 
208  return ChildrenList;
209  }
210 
211  /// <inheritdoc />
212  public override string ToString()
213  {
214  return string.Format(
215  "{0}{1} {2}({3}){4}",
216  Qualifiers == Qualifier.None ? string.Empty : Qualifiers + " ",
217  ReturnType,
218  Name,
219  string.Join(",", Parameters),
220  GetType() == typeof(MethodDeclaration) ? ";" : " {...}");
221  }
222 
223  /// <inheritdoc />
224  /*public override int GetHashCode()
225  {
226  unchecked
227  {
228  return ((Name != null ? Name.GetHashCode() : 0) * 397) ^ (ReturnType != null ? ReturnType.GetHashCode() : 0);
229  }
230  }*/
231 
232  #endregion
233 
234  #region Methods
235 
236  internal void UpdateParameters()
237  {
238  foreach (var parameter in Parameters)
239  {
240  parameter.DeclaringMethod = this;
241  }
242  }
243 
244  #endregion
245  }
246 }
bool IsSameSignature(MethodInvocationExpression methodInvocationExpression)
Test if a method invocation expression has the same signature.
override IEnumerable< Node > Childrens()
Gets the child nodes. An enumeration of child nodes
A tag interface to identify a container for scope declarations.
Identifier Name
Gets or sets the name.
void CopyTo(MethodDeclaration target)
Copies declartion to another instance.
Base interface for all node providing qualifiers.
Definition: IQualifiers.cs:8
Abstract node.
Definition: Node.cs:15
bool CheckConstraint(GenericParameterType parameterType, TypeBase typeToCheck)
Checks the constraint.
List< Expression > Arguments
Gets or sets the arguments.
bool IsSameSignature(MethodDeclaration methodDeclaration)
Test if a method declaration has the same signature.
A member reference in the form {this}.{Name}
Base type for all types.
Definition: TypeBase.cs:11
List< Parameter > Parameters
Gets or sets the parameters.
MethodDeclaration()
Initializes a new instance of the MethodDeclaration class.
Toplevel interface for a declaration.
Definition: IDeclaration.cs:8
static readonly Qualifier None
None Enum.
Definition: Qualifier.cs:18