Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
MixinVirtualTable.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.Ast;
7 using SiliconStudio.Paradox.Shaders.Parser.Utility;
8 using SiliconStudio.Shaders.Ast;
9 using SiliconStudio.Shaders.Ast.Hlsl;
10 using SiliconStudio.Shaders.Utility;
11 
12 namespace SiliconStudio.Paradox.Shaders.Parser.Mixins
13 {
14  internal class MixinVirtualTable : ShaderVirtualTable
15  {
16  #region Public properties
17 
18  /// <summary>
19  /// List of all declared methods
20  /// </summary>
21  public HashSet<MethodDeclarationShaderCouple> Methods { get; private set; }
22 
23  /// <summary>
24  /// List of all declared Variables
25  /// </summary>
26  public HashSet<VariableShaderCouple> Variables { get; private set; }
27 
28  /// <summary>
29  /// List of all the structure definitions
30  /// </summary>
31  public List<StructType> StructureTypes { get; private set; } // list instead of hashset because order can be important
32 
33  /// <summary>
34  /// List of all the Typedefs
35  /// </summary>
36  public List<Typedef> Typedefs { get; private set; } // list instead of hashset because order can be important
37 
38  #endregion
39 
40  #region Constructor
41 
42  public MixinVirtualTable()
43  {
44  Methods = new HashSet<MethodDeclarationShaderCouple>();
45  Variables = new HashSet<VariableShaderCouple>();
46  StructureTypes = new List<StructType>();
47  Typedefs = new List<Typedef>();
48  }
49 
50  #endregion
51 
52  #region Public methods
53 
54  /// <summary>
55  /// Merge with a local virtual table = need to check override keywords
56  /// </summary>
57  /// <param name="virtualTable">the virtual table to add</param>
58  /// <param name="mixinName">the name of the mixin</param>
59  /// <param name="log">the error logger</param>
60  public void MergeWithLocalVirtualTable(MixinVirtualTable virtualTable, string mixinName, LoggerResult log)
61  {
62  foreach (var method in virtualTable.Methods)
63  {
64  var methodDecl = Methods.LastOrDefault(x => x.Method.IsSameSignature(method.Method));
65  if (methodDecl != null)
66  {
67  var isBaseMethod = method.Shader.BaseClasses.Any(x => x.Name.Text == methodDecl.Shader.Name.Text);
68 
69  if (isBaseMethod)
70  {
71  if (methodDecl.Method is MethodDefinition)
72  {
73  if (!method.Method.Qualifiers.Contains(ParadoxStorageQualifier.Override))
74  {
75  log.Error(ParadoxMessageCode.ErrorMissingOverride, method.Method.Span, method.Method, mixinName);
76  continue;
77  }
78  }
79  else if (method.Method.Qualifiers.Contains(ParadoxStorageQualifier.Override))
80  {
81  log.Error(ParadoxMessageCode.ErrorOverrideDeclaration, method.Method.Span, method.Method, mixinName);
82  continue;
83  }
84  }
85 
86  Methods.Remove(methodDecl);
87  }
88  else
89  {
90  if (method.Method.Qualifiers.Contains(ParadoxStorageQualifier.Override))
91  {
92  log.Error(ParadoxMessageCode.ErrorNoMethodToOverride, method.Method.Span, method.Method, mixinName);
93  continue;
94  }
95  }
96 
97  Methods.Add(method);
98 
99  // TODO: handle declarations vs definitions
100  }
101 
102  Variables.UnionWith(virtualTable.Variables.Where(x => !Variables.Contains(x)));
103  StructureTypes.AddRange(virtualTable.StructureTypes.Where(x => !StructureTypes.Contains(x)));
104  Typedefs.AddRange(virtualTable.Typedefs.Where(x => !Typedefs.Contains(x)));
105  }
106 
107  /// <summary>
108  /// Check the name conflict between the two virtual tables
109  /// </summary>
110  public bool CheckNameConflict(MixinVirtualTable virtualTable, LoggerResult log)
111  {
112  var conflict = false;
113 
114  foreach (var variable in virtualTable.Variables.Where(variable => Variables.Any(x => x.Variable.Name.Text == variable.Variable.Name.Text)))
115  {
116  log.Error(ParadoxMessageCode.ErrorVariableNameConflict, variable.Variable.Span, variable.Variable, "");
117  conflict = true;
118  }
119 
120  return conflict;
121  }
122 
123  #endregion
124  }
125 }
A class to collect parsing/expression messages.
Definition: LoggerResult.cs:13
static readonly SiliconStudio.Shaders.Ast.StorageQualifier Override
Override keyword (override).
A method definition with a body of statements.