1 using System;
2 using System.Collections;
3 using Cm.Ast;
4
5
6
7
8
9 namespace soulcm.scmpg
10 {
11 public class NonterminalInfo
12 {
13 public NonterminalInfo(const ustring& name_, bool ptrType_) :
14 name(name_), ptrType(ptrType_), weakCount(0), strongCount(0)
15 {
16 }
17 public ustring name;
18 public bool ptrType;
19 public int weakCount;
20 public int strongCount;
21 }
22 public class NonterminalCountingVisitor : Cm.Ast.Visitor
23 {
24 public NonterminalCountingVisitor(List<NonterminalInfo>& nonterminalInfos_) :
25 nonterminalInfos(nonterminalInfos_)
26 {
27 }
28 public void Visit(Cm.Ast.IdentifierNode& node)
29 {
30 for (NonterminalInfo& info : nonterminalInfos)
31 {
32 if (node.Str() == info.name)
33 {
34 ++info.weakCount;
35 if (!HasCaseStatementParent(node.Parent()))
36 {
37 ++info.strongCount;
38 }
39 }
40 }
41 }
42 public override void Visit(Cm.Ast.LabeledStatementNode& labeledStatementNode)
43 {
44 labeledStatementNode.Stmt()->Accept(*this);
45 }
46 public override void Visit(Cm.Ast.CompoundStatementNode& compoundStatementNode)
47 {
48 int n = compoundStatementNode.Statements().Count();
49 for (int i = 0; i < n; ++i;)
50 {
51 compoundStatementNode.Statements()[i]->Accept(*this);
52 }
53 }
54 public override void Visit(Cm.Ast.ReturnStatementNode& returnStatementNode)
55 {
56 returnStatementNode.Expression()->Accept(*this);
57 }
58 public override void Visit(Cm.Ast.IfStatementNode& ifStatementNode)
59 {
60 ifStatementNode.Condition()->Accept(*this);
61 ifStatementNode.ThenS()->Accept(*this);
62 if ((ifStatementNode.ElseS() != null))
63 {
64 ifStatementNode.ElseS()->Accept(*this);
65 }
66 }
67 public override void Visit(Cm.Ast.WhileStatementNode& whileStatementNode)
68 {
69 whileStatementNode.Condition()->Accept(*this);
70 whileStatementNode.Statement()->Accept(*this);
71 }
72 public override void Visit(Cm.Ast.DoStatementNode& doStatementNode)
73 {
74 doStatementNode.Statement()->Accept(*this);
75 doStatementNode.Condition()->Accept(*this);
76 }
77 public override void Visit(Cm.Ast.ForStatementNode& forStatementNode)
78 {
79 forStatementNode.InitS()->Accept(*this);
80 if ((forStatementNode.Condition() != null))
81 {
82 forStatementNode.Condition()->Accept(*this);
83 }
84 forStatementNode.LoopS()->Accept(*this);
85 forStatementNode.ActionS()->Accept(*this);
86 }
87 public override void Visit(Cm.Ast.BreakStatementNode& breakStatementNode)
88 {
89 }
90 public override void Visit(Cm.Ast.ContinueStatementNode& continueStatementNode)
91 {
92 }
93 public override void Visit(Cm.Ast.GotoStatementNode& gotoStatementNode)
94 {
95 }
96 public override void Visit(Cm.Ast.ConstructionStatementNode& constructionStatementNode)
97 {
98 constructionStatementNode.TypeExpr()->Accept(*this);
99 constructionStatementNode.Id()->Accept(*this);
100 int n = constructionStatementNode.Arguments().Count();
101 for (int i = 0; i < n; ++i;)
102 {
103 constructionStatementNode.Arguments()[i]->Accept(*this);
104 }
105 }
106 public override void Visit(Cm.Ast.DeleteStatementNode& deleteStatementNode)
107 {
108 deleteStatementNode.Expression()->Accept(*this);
109 }
110 public override void Visit(Cm.Ast.DestroyStatementNode& destroyStatementNode)
111 {
112 destroyStatementNode.Expression()->Accept(*this);
113 }
114 public override void Visit(Cm.Ast.AssignmentStatementNode& assignmentStatementNode)
115 {
116 assignmentStatementNode.SourceExpr()->Accept(*this);
117 assignmentStatementNode.TargetExpr()->Accept(*this);
118 }
119 public override void Visit(Cm.Ast.ExpressionStatementNode& expressionStatementNode)
120 {
121 expressionStatementNode.Expression()->Accept(*this);
122 }
123 public override void Visit(Cm.Ast.EmptyStatementNode& emptyStatementNode)
124 {
125 }
126 public override void Visit(Cm.Ast.RangeForStatementNode& rangeForStatementNode)
127 {
128 rangeForStatementNode.TypeExpr()->Accept(*this);
129 rangeForStatementNode.Id()->Accept(*this);
130 rangeForStatementNode.Container()->Accept(*this);
131 rangeForStatementNode.Action()->Accept(*this);
132 }
133 public override void Visit(Cm.Ast.SwitchStatementNode& switchStatementNode)
134 {
135 switchStatementNode.Condition()->Accept(*this);
136 int n = switchStatementNode.Cases().Count();
137 for (int i = 0; i < n; ++i;)
138 {
139 switchStatementNode.Cases()[i]->Accept(*this);
140 }
141 if ((switchStatementNode.Default() != null))
142 {
143 switchStatementNode.Default()->Accept(*this);
144 }
145 }
146 public override void Visit(Cm.Ast.CaseStatementNode& caseStatementNode)
147 {
148 int nc = caseStatementNode.CaseExprs().Count();
149 for (int i = 0; i < nc; ++i;)
150 {
151 caseStatementNode.CaseExprs()[i]->Accept(*this);
152 }
153 int n = caseStatementNode.Statements().Count();
154 for (int i = 0; i < n; ++i;)
155 {
156 caseStatementNode.Statements()[i]->Accept(*this);
157 }
158 }
159 public override void Visit(Cm.Ast.DefaultStatementNode& defaultStatementNode)
160 {
161 int n = defaultStatementNode.Statements().Count();
162 for (int i = 0; i < n; ++i;)
163 {
164 defaultStatementNode.Statements()[i]->Accept(*this);
165 }
166 }
167 public override void Visit(Cm.Ast.GotoCaseStatementNode& gotoCaseStatementNode)
168 {
169 gotoCaseStatementNode.CaseExpr()->Accept(*this);
170 }
171 public override void Visit(Cm.Ast.GotoDefaultStatementNode& gotoDefaultStatementNode)
172 {
173 }
174 public override void Visit(Cm.Ast.ThrowStatementNode& throwStatementNode)
175 {
176 throwStatementNode.Expression()->Accept(*this);
177 }
178 public override void Visit(Cm.Ast.TryStatementNode& tryStatementNode)
179 {
180 tryStatementNode.TryBlock()->Accept(*this);
181 int n = tryStatementNode.Catches().Count();
182 for (int i = 0; i < n; ++i;)
183 {
184 tryStatementNode.Catches()[i]->Accept(*this);
185 }
186 }
187 public override void Visit(Cm.Ast.CatchNode& catchNode)
188 {
189 catchNode.TypeExpr()->Accept(*this);
190 catchNode.Id()->Accept(*this);
191 catchNode.CatchBlock()->Accept(*this);
192 }
193 public override void Visit(Cm.Ast.AssertStatementNode& assertStatementNode)
194 {
195 assertStatementNode.AssertExpr()->Accept(*this);
196 }
197 public override void Visit(Cm.Ast.ConditionalCompilationPartNode& conditionalCompilationPartNode)
198 {
199 conditionalCompilationPartNode.Expr()->Accept(*this);
200 int n = conditionalCompilationPartNode.Statements().Count();
201 for (int i = 0; i < n; ++i;)
202 {
203 conditionalCompilationPartNode.Statements()[i]->Accept(*this);
204 }
205 }
206 public override void Visit(Cm.Ast.ConditionalCompilationDisjunctionNode& conditionalCompilationDisjunctionNode)
207 {
208 conditionalCompilationDisjunctionNode.Left()->Accept(*this);
209 conditionalCompilationDisjunctionNode.Right()->Accept(*this);
210 }
211 public override void Visit(Cm.Ast.ConditionalCompilationConjunctionNode& conditionalCompilationConjunctionNode)
212 {
213 conditionalCompilationConjunctionNode.Left()->Accept(*this);
214 conditionalCompilationConjunctionNode.Right()->Accept(*this);
215 }
216 public override void Visit(Cm.Ast.ConditionalCompilationNotNode& conditionalCompilationNotNode)
217 {
218 conditionalCompilationNotNode.Expr()->Accept(*this);
219 }
220 public override void Visit(Cm.Ast.ConditionalCompilationPrimaryNode& conditionalCompilationPrimaryNode)
221 {
222 }
223 public override void Visit(Cm.Ast.ParenthesizedConditionalCompilationExpressionNode& parenthesizeCondCompExprNode)
224 {
225 parenthesizeCondCompExprNode.Expr()->Accept(*this);
226 }
227 public override void Visit(Cm.Ast.ConditionalCompilationStatementNode& conditionalCompilationStatementNode)
228 {
229 conditionalCompilationStatementNode.IfPart()->Accept(*this);
230 int ne = conditionalCompilationStatementNode.ElifParts().Count();
231 for (int i = 0; i < ne; ++i;)
232 {
233 conditionalCompilationStatementNode.ElifParts()[i]->Accept(*this);
234 }
235 if ((conditionalCompilationStatementNode.ElsePart() != null))
236 {
237 conditionalCompilationStatementNode.ElsePart()->Accept(*this);
238 }
239 }
240 public override void Visit(Cm.Ast.ConstNode& constNode)
241 {
242 constNode.Subject()->Accept(*this);
243 }
244 public override void Visit(Cm.Ast.LValueRefNode& lvalueRefNode)
245 {
246 lvalueRefNode.Subject()->Accept(*this);
247 }
248 public override void Visit(Cm.Ast.RValueRefNode& rvalueRefNode)
249 {
250 rvalueRefNode.Subject()->Accept(*this);
251 }
252 public override void Visit(Cm.Ast.PointerNode& pointerNode)
253 {
254 pointerNode.Subject()->Accept(*this);
255 }
256 public override void Visit(Cm.Ast.ArrayNode& arrayNode)
257 {
258 arrayNode.Subject()->Accept(*this);
259 if ((arrayNode.Size() != null))
260 {
261 arrayNode.Size()->Accept(*this);
262 }
263 }
264 public override void Visit(Cm.Ast.DotNode& dotNode)
265 {
266 dotNode.Subject()->Accept(*this);
267 dotNode.MemberId()->Accept(*this);
268 }
269 public override void Visit(Cm.Ast.ArrowNode& arrowNode)
270 {
271 arrowNode.Subject()->Accept(*this);
272 arrowNode.MemberId()->Accept(*this);
273 }
274 public override void Visit(Cm.Ast.EquivalenceNode& equivalenceNode)
275 {
276 equivalenceNode.Left()->Accept(*this);
277 equivalenceNode.Right()->Accept(*this);
278 }
279 public override void Visit(Cm.Ast.ImplicationNode& implicationNode)
280 {
281 implicationNode.Left()->Accept(*this);
282 implicationNode.Right()->Accept(*this);
283 }
284 public override void Visit(Cm.Ast.DisjunctionNode& disjunctionNode)
285 {
286 disjunctionNode.Left()->Accept(*this);
287 disjunctionNode.Right()->Accept(*this);
288 }
289 public override void Visit(Cm.Ast.ConjunctionNode& conjunctionNode)
290 {
291 conjunctionNode.Left()->Accept(*this);
292 conjunctionNode.Right()->Accept(*this);
293 }
294 public override void Visit(Cm.Ast.BitOrNode& bitOrNode)
295 {
296 bitOrNode.Left()->Accept(*this);
297 bitOrNode.Right()->Accept(*this);
298 }
299 public override void Visit(Cm.Ast.BitXorNode& bitXorNode)
300 {
301 bitXorNode.Left()->Accept(*this);
302 bitXorNode.Right()->Accept(*this);
303 }
304 public override void Visit(Cm.Ast.BitAndNode& bitAndNode)
305 {
306 bitAndNode.Left()->Accept(*this);
307 bitAndNode.Right()->Accept(*this);
308 }
309 public override void Visit(Cm.Ast.EqualNode& equalNode)
310 {
311 equalNode.Left()->Accept(*this);
312 equalNode.Right()->Accept(*this);
313 }
314 public override void Visit(Cm.Ast.NotEqualNode& notEqualNode)
315 {
316 notEqualNode.Left()->Accept(*this);
317 notEqualNode.Right()->Accept(*this);
318 }
319 public override void Visit(Cm.Ast.LessNode& lessNode)
320 {
321 lessNode.Left()->Accept(*this);
322 lessNode.Right()->Accept(*this);
323 }
324 public override void Visit(Cm.Ast.GreaterNode& greaterNode)
325 {
326 greaterNode.Left()->Accept(*this);
327 greaterNode.Right()->Accept(*this);
328 }
329 public override void Visit(Cm.Ast.LessOrEqualNode& lessOrEqualNode)
330 {
331 lessOrEqualNode.Left()->Accept(*this);
332 lessOrEqualNode.Right()->Accept(*this);
333 }
334 public override void Visit(Cm.Ast.GreaterOrEqualNode& greaterOrEqualNode)
335 {
336 greaterOrEqualNode.Left()->Accept(*this);
337 greaterOrEqualNode.Right()->Accept(*this);
338 }
339 public override void Visit(Cm.Ast.ShiftLeftNode& shiftLeftNode)
340 {
341 shiftLeftNode.Left()->Accept(*this);
342 shiftLeftNode.Right()->Accept(*this);
343 }
344 public override void Visit(Cm.Ast.ShiftRightNode& shiftRightNode)
345 {
346 shiftRightNode.Left()->Accept(*this);
347 shiftRightNode.Right()->Accept(*this);
348 }
349 public override void Visit(Cm.Ast.AddNode& addNode)
350 {
351 addNode.Left()->Accept(*this);
352 addNode.Right()->Accept(*this);
353 }
354 public override void Visit(Cm.Ast.SubNode& subNode)
355 {
356 subNode.Left()->Accept(*this);
357 subNode.Right()->Accept(*this);
358 }
359 public override void Visit(Cm.Ast.MulNode& mulNode)
360 {
361 mulNode.Left()->Accept(*this);
362 mulNode.Right()->Accept(*this);
363 }
364 public override void Visit(Cm.Ast.DivNode& divNode)
365 {
366 divNode.Left()->Accept(*this);
367 divNode.Right()->Accept(*this);
368 }
369 public override void Visit(Cm.Ast.RemNode& remNode)
370 {
371 remNode.Left()->Accept(*this);
372 remNode.Right()->Accept(*this);
373 }
374 public override void Visit(Cm.Ast.NotNode& notNode)
375 {
376 notNode.Subject()->Accept(*this);
377 }
378 public override void Visit(Cm.Ast.UnaryPlusNode& unaryPlusNode)
379 {
380 unaryPlusNode.Subject()->Accept(*this);
381 }
382 public override void Visit(Cm.Ast.UnaryMinusNode& unaryMinusNode)
383 {
384 unaryMinusNode.Subject()->Accept(*this);
385 }
386 public override void Visit(Cm.Ast.PrefixIncrementNode& prefixIncrementNode)
387 {
388 prefixIncrementNode.Subject()->Accept(*this);
389 }
390 public override void Visit(Cm.Ast.PrefixDecrementNode& prefixDecrementNode)
391 {
392 prefixDecrementNode.Subject()->Accept(*this);
393 }
394 public override void Visit(Cm.Ast.DerefNode& derefNode)
395 {
396 derefNode.Subject()->Accept(*this);
397 }
398 public override void Visit(Cm.Ast.AddrOfNode& addrOfNode)
399 {
400 addrOfNode.Subject()->Accept(*this);
401 }
402 public override void Visit(Cm.Ast.ComplementNode& complementNode)
403 {
404 complementNode.Subject()->Accept(*this);
405 }
406 public override void Visit(Cm.Ast.IsNode& isNode)
407 {
408 isNode.TargetTypeExpr()->Accept(*this);
409 isNode.Expr()->Accept(*this);
410 }
411 public override void Visit(Cm.Ast.AsNode& asNode)
412 {
413 asNode.TargetTypeExpr()->Accept(*this);
414 asNode.Expr()->Accept(*this);
415 }
416 public override void Visit(Cm.Ast.IndexingNode& indexingNode)
417 {
418 indexingNode.Subject()->Accept(*this);
419 indexingNode.Index()->Accept(*this);
420 }
421 public override void Visit(Cm.Ast.InvokeNode& invokeNode)
422 {
423 invokeNode.Subject()->Accept(*this);
424 int n = invokeNode.Arguments().Count();
425 for (int i = 0; i < n; ++i;)
426 {
427 invokeNode.Arguments()[i]->Accept(*this);
428 }
429 }
430 public override void Visit(Cm.Ast.PostfixIncrementNode& postfixIncrementNode)
431 {
432 postfixIncrementNode.Subject()->Accept(*this);
433 }
434 public override void Visit(Cm.Ast.PostfixDecrementNode& postfixDecrementNode)
435 {
436 postfixDecrementNode.Subject()->Accept(*this);
437 }
438 public override void Visit(Cm.Ast.SizeOfNode& sizeOfNode)
439 {
440 sizeOfNode.Expression()->Accept(*this);
441 }
442 public override void Visit(Cm.Ast.TypeNameNode& typeNameNode)
443 {
444 typeNameNode.Expression()->Accept(*this);
445 }
446 public override void Visit(Cm.Ast.TypeIdNode& typeIdNode)
447 {
448 typeIdNode.Expression()->Accept(*this);
449 }
450 public override void Visit(Cm.Ast.CastNode& castNode)
451 {
452 castNode.TargetTypeExpr()->Accept(*this);
453 castNode.SourceExpr()->Accept(*this);
454 }
455 public override void Visit(Cm.Ast.ConstructNode& constructNode)
456 {
457 constructNode.TypeExpr()->Accept(*this);
458 int n = constructNode.Arguments().Count();
459 for (int i = 0; i < n; ++i;)
460 {
461 constructNode.Arguments()[i]->Accept(*this);
462 }
463 }
464 public override void Visit(Cm.Ast.NewNode& newNode)
465 {
466 newNode.TypeExpr()->Accept(*this);
467 int n = newNode.Arguments().Count();
468 for (int i = 0; i < n; ++i;)
469 {
470 newNode.Arguments()[i]->Accept(*this);
471 }
472 }
473 public override void Visit(Cm.Ast.ParenthesizedExpressionNode& parenthesizedExpressionNode)
474 {
475 parenthesizedExpressionNode.Subject()->Accept(*this);
476 }
477 private List<NonterminalInfo>& nonterminalInfos;
478 }
479 public bool HasCaseStatementParent(Cm.Ast.Node* node)
480 {
481 if (!(node != null)) return false;
482 if (node->GetNodeType() == NodeType.caseStatementNode)
483 {
484 return true;
485 }
486 return HasCaseStatementParent(node->Parent());
487 }
488 }