Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / Mono.CodeContracts / Mono.CodeContracts.Static.AST.Visitors / NodeInspector.cs
1 // 
2 // NodeInspector.cs
3 // 
4 // Authors:
5 //      Alexander Chebaturkin (chebaturkin@gmail.com)
6 // 
7 // Copyright (C) 2011 Alexander Chebaturkin
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //  
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // 
28
29 using System.Collections.Generic;
30
31 namespace Mono.CodeContracts.Static.AST.Visitors {
32         class NodeInspector {
33                 public virtual void Visit (Node node)
34                 {
35                         if (node == null)
36                                 return;
37                         switch (node.NodeType) {
38                         case NodeType.Nop:
39                                 break;
40
41                                 #region Binary
42                         case NodeType.Add:
43                         case NodeType.Sub:
44                         case NodeType.Rem:
45                         case NodeType.Clt:
46                         case NodeType.Cgt:
47                         case NodeType.Ceq:
48                         case NodeType.Box:
49                         case NodeType.Le:
50                         case NodeType.Mul:
51                         case NodeType.Div:
52                         case NodeType.Div_Un:
53                         case NodeType.Rem_Un:
54                         case NodeType.And:
55                         case NodeType.Or:
56                         case NodeType.Shr:
57                         case NodeType.Xor:
58                         case NodeType.Shl:
59                         case NodeType.Shr_Un:
60                         case NodeType.Ne:
61                         case NodeType.Ge:
62                         case NodeType.Gt:
63                         case NodeType.Lt:
64                         case NodeType.Eq:
65                                 VisitBinaryExpression ((BinaryExpression) node);
66                                 break;
67                                 #endregion
68
69                         case NodeType.Call:
70                         case NodeType.Jmp:
71                         case NodeType.MethodCall:
72                                 VisitMethodCall ((MethodCall) node);
73                                 break;
74                         case NodeType.Conv:
75                         case NodeType.Conv_I1:
76                         case NodeType.Conv_I2:
77                         case NodeType.Conv_I8:
78                         case NodeType.Conv_I4:
79                         case NodeType.Conv_R4:
80                         case NodeType.Conv_R8:
81                         case NodeType.Neg:
82                         case NodeType.Not:
83                         case NodeType.LogicalNot:
84                                 VisitUnaryExpression ((UnaryExpression) node);
85                                 break;
86                         case NodeType.Literal:
87                                 VisitLiteral ((Literal) node);
88                                 break;
89                         case NodeType.This:
90                                 VisitThis ((This) node);
91                                 break;
92                         case NodeType.Block:
93                                 VisitBlock ((Block) node);
94                                 break;
95                         case NodeType.Branch:
96                                 VisitBranch ((Branch) node);
97                                 break;
98                         case NodeType.Return:
99                                 VisitReturn ((Return) node);
100                                 break;
101                         case NodeType.AssignmentStatement:
102                                 VisitAssignmentStatement ((AssignmentStatement) node);
103                                 break;
104                         case NodeType.Local:
105                                 VisitLocal ((Local) node);
106                                 break;
107                         case NodeType.Parameter:
108                                 VisitParameter ((Parameter) node);
109                                 break;
110                         case NodeType.ExpressionStatement:
111                                 VisitExpressionStatement ((ExpressionStatement) node);
112                                 break;
113                         case NodeType.Method:
114                                 VisitMethod ((Method) node);
115                                 break;
116                         case NodeType.MethodContract:
117                                 VisitMethodContract ((MethodContract) node);
118                                 break;
119                         case NodeType.Requires:
120                                 VisitRequires ((Requires) node);
121                                 break;
122                         case NodeType.Ensures:
123                                 VisitEnsures ((Ensures) node);
124                                 break;
125                         case NodeType.TypeNode:
126                                 VisitTypeNode ((TypeNode) node);
127                                 break;
128                         case NodeType.Assembly:
129                                 VisitAssembly ((AssemblyNode) node);
130                                 break;
131                         case NodeType.Module:
132                                 VisitModule ((Module) node);
133                                 break;
134                         case NodeType.MemberBinding:
135                                 VisitMemberBinding ((MemberBinding) node);
136                                 break;
137                         case NodeType.Construct:
138                                 VisitConstruct ((Construct) node);
139                                 break;
140                         default:
141                                 VisitUnknownNodeType (node);
142                                 break;
143                         }
144                 }
145
146                 public virtual void VisitAssembly (AssemblyNode node)
147                 {
148                         if (node == null)
149                                 return;
150
151                         VisitModuleList (node.Modules);
152                 }
153
154                 public virtual void VisitModuleList (IEnumerable<Module> node)
155                 {
156                         if (node == null)
157                                 return;
158
159                         foreach (Module module in node)
160                                 VisitModule (module);
161                 }
162
163                 public virtual void VisitModule (Module node)
164                 {
165                         if (node == null)
166                                 return;
167
168                         VisitTypeNodeList (node.Types);
169                 }
170
171                 public virtual void VisitAssignmentStatement (AssignmentStatement node)
172                 {
173                         if (node == null)
174                                 return;
175                         VisitTargetExpression (node.Target);
176                         VisitExpression (node.Source);
177                 }
178
179                 public virtual void VisitBinaryExpression (BinaryExpression node)
180                 {
181                         if (node == null)
182                                 return;
183
184                         VisitExpression (node.Left);
185                         VisitExpression (node.Right);
186                 }
187
188                 public virtual void VisitBlock (Block node)
189                 {
190                         if (node == null)
191                                 return;
192
193                         VisitStatementList (node.Statements);
194                 }
195
196                 public virtual void VisitStatementList (List<Statement> node)
197                 {
198                         if (node == null)
199                                 return;
200
201                         for (int i = 0; i < node.Count; i++)
202                                 Visit (node [i]);
203                 }
204
205                 public virtual void VisitBranch (Branch node)
206                 {
207                         if (node == null)
208                                 return;
209
210                         VisitExpression (node.Condition);
211                 }
212
213                 public virtual void VisitConstruct (Construct node)
214                 {
215                         if (node == null)
216                                 return;
217
218                         VisitExpression (node.Constructor);
219                         VisitExpressionList (node.Arguments);
220                 }
221
222                 public virtual void VisitExpressionList (List<Expression> list)
223                 {
224                         if (list == null)
225                                 return;
226
227                         for (int i = 0; i < list.Count; ++i)
228                                 Visit (list [i]);
229                 }
230
231                 public virtual void VisitEnsures (Ensures node)
232                 {
233                         if (node == null)
234                                 return;
235
236                         VisitExpression (node.Assertion);
237                         VisitExpression (node.UserMessage);
238                 }
239
240                 public virtual void VisitExpression (Expression node)
241                 {
242                         if (node == null)
243                                 return;
244
245                         //todo: maybe there will be something
246                 }
247
248                 public virtual void VisitExpressionStatement (ExpressionStatement node)
249                 {
250                         if (node == null)
251                                 return;
252
253                         VisitExpression (node.Expression);
254                 }
255
256                 public virtual void VisitLiteral (Literal node)
257                 {
258                 }
259
260                 public virtual void VisitLocal (Local node)
261                 {
262                         if (node == null)
263                                 return;
264
265                         VisitTypeNode (node.Type);
266
267                         //todo: maybe there should be something else
268                 }
269
270                 public virtual void VisitMemberBinding (MemberBinding node)
271                 {
272                         if (node == null)
273                                 return;
274
275                         VisitExpression (node.TargetObject);
276                 }
277
278                 public virtual void VisitMethod (Method node)
279                 {
280                         if (node == null)
281                                 return;
282
283                         VisitTypeNode (node.ReturnType);
284                         VisitParameterList (node.Parameters);
285                         VisitMethodContract (node.MethodContract);
286                         VisitBlock (node.Body);
287                 }
288
289                 public virtual void VisitParameterList (List<Parameter> node)
290                 {
291                         if (node == null)
292                                 return;
293
294                         for (int i = 0; i < node.Count; i++)
295                                 VisitParameter (node [i]);
296                 }
297
298                 public virtual void VisitMethodCall (MethodCall node)
299                 {
300                         if (node == null)
301                                 return;
302
303                         VisitExpression (node.Callee);
304                         VisitExpressionList (node.Arguments);
305                 }
306
307                 public virtual void VisitMethodContract (MethodContract node)
308                 {
309                         if (node == null)
310                                 return;
311
312                         VisitRequiresList (node.Requires);
313                         VisitEnsuresList (node.Ensures);
314                 }
315
316                 public virtual void VisitEnsuresList (List<Ensures> node)
317                 {
318                         if (node == null)
319                                 return;
320
321                         for (int i = 0; i < node.Count; i++)
322                                 Visit (node [i]);
323                 }
324
325                 public virtual void VisitRequiresList (List<Requires> node)
326                 {
327                         if (node == null)
328                                 return;
329
330                         for (int i = 0; i < node.Count; i++)
331                                 Visit (node [i]);
332                 }
333
334                 public virtual void VisitParameter (Parameter node)
335                 {
336                         if (node == null)
337                                 return;
338
339                         VisitTypeNode (node.Type);
340
341                         //todo: there may be something else
342                 }
343
344                 public virtual void VisitRequires (Requires node)
345                 {
346                         if (node == null)
347                                 return;
348
349                         VisitExpression (node.Assertion);
350                         VisitExpression (node.UserMessage);
351                 }
352
353                 public virtual void VisitReturn (Return node)
354                 {
355                         if (node == null)
356                                 return;
357
358                         VisitExpression (node.Expression);
359                 }
360
361                 public virtual void VisitTargetExpression (Expression node)
362                 {
363                         VisitExpression (node);
364                 }
365
366                 public virtual void VisitThis (This node)
367                 {
368                         if (node == null)
369                                 return;
370
371                         VisitTypeNode (node.Type);
372                 }
373
374                 public virtual void VisitTypeNode (TypeNode node)
375                 {
376                         if (node == null)
377                                 return;
378
379                         var clazz = node as Class;
380                         if (clazz != null)
381                                 VisitTypeNode (clazz.BaseType);
382
383                         VisitPropertiesList (node.Properties);
384                         VisitMethodsList (node.Methods);
385                         VisitTypeNodeList (node.NestedTypes);
386                 }
387
388                 public virtual void VisitPropertiesList (List<Property> node)
389                 {
390                         if (node == null)
391                                 return;
392
393                         for (int i = 0; i < node.Count; i++) {
394                                 Property property = node [i];
395                                 if (property != null)
396                                         Visit (node [i]);
397                         }
398                 }
399
400                 public virtual void VisitMethodsList (List<Method> node)
401                 {
402                         if (node == null)
403                                 return;
404
405                         for (int i = 0; i < node.Count; i++) {
406                                 Method method = node [i];
407                                 if (method != null)
408                                         Visit (node [i]);
409                         }
410                 }
411
412                 public virtual void VisitTypeNodeList (List<TypeNode> node)
413                 {
414                         if (node == null)
415                                 return;
416
417                         for (int i = 0; i < node.Count; i++) {
418                                 TypeNode typeNode = node [i];
419                                 if (typeNode != null)
420                                         Visit (typeNode);
421                         }
422                 }
423
424                 public virtual void VisitUnaryExpression (UnaryExpression node)
425                 {
426                         if (node == null)
427                                 return;
428
429                         VisitExpression (node.Operand);
430                 }
431
432                 public virtual void VisitUnknownNodeType (Node node)
433                 {
434                 }
435         }
436 }