Skip to content

Commit 366a572

Browse files
committed
extend cil-source pretty printer
1 parent aee66d3 commit 366a572

19 files changed

+1084
-37
lines changed

chc/app/CAttributes.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ def is_index(self) -> bool:
113113
def is_question(self) -> bool:
114114
return False
115115

116+
def accept(self, visitor: "CVisitor") -> None:
117+
raise UF.CHCError("visitor not yet implemented for " + str(self))
118+
116119
def __str__(self) -> str:
117120
return "attrparam:" + self.tags[0]
118121

@@ -135,6 +138,9 @@ def intvalue(self) -> int:
135138
def is_int(self) -> bool:
136139
return True
137140

141+
def accept(self, visitor: "CVisitor") -> None:
142+
visitor.visit_attr_int(self)
143+
138144
def __str__(self) -> str:
139145
return "aint(" + str(self.intvalue) + ")"
140146

@@ -157,6 +163,9 @@ def stringvalue(self) -> str:
157163
def is_str(self) -> bool:
158164
return True
159165

166+
def accept(self, visitor: "CVisitor") -> None:
167+
visitor.visit_attr_str(self)
168+
160169
def __str__(self) -> str:
161170
return "astr(" + str(self.stringvalue) + ")"
162171

@@ -184,6 +193,9 @@ def params(self) -> List[CAttr]:
184193
def is_cons(self) -> bool:
185194
return True
186195

196+
def accept(self, visitor: "CVisitor") -> None:
197+
visitor.visit_attr_cons(self)
198+
187199
def __str__(self) -> str:
188200
return "acons(" + str(self.name) + ")"
189201

@@ -206,6 +218,9 @@ def typ(self) -> "CTyp":
206218
def is_sizeof(self) -> bool:
207219
return True
208220

221+
def accept(self, visitor: "CVisitor") -> None:
222+
visitor.visit_attr_sizeof(self)
223+
209224
def __str__(self) -> str:
210225
return "asizeof(" + str(self.typ) + ")"
211226

@@ -228,6 +243,9 @@ def param(self) -> CAttr:
228243
def is_sizeofe(self) -> bool:
229244
return True
230245

246+
def accept(self, visitor: "CVisitor") -> None:
247+
visitor.visit_attr_sizeofe(self)
248+
231249
def __str__(self) -> str:
232250
return "asizeofe(" + str(self.param) + ")"
233251

@@ -250,6 +268,9 @@ def typsig(self) -> "CTypsig":
250268
def is_sizeofs(self) -> bool:
251269
return True
252270

271+
def accept(self, visitor: "CVisitor") -> None:
272+
visitor.visit_attr_sizeofs(self)
273+
253274
def __str__(self) -> str:
254275
return "asizeofs(" + str(self.typsig) + ")"
255276

@@ -272,6 +293,9 @@ def typ(self) -> "CTyp":
272293
def is_alignof(self) -> bool:
273294
return True
274295

296+
def accept(self, visitor: "CVisitor") -> None:
297+
visitor.visit_attr_alignof(self)
298+
275299
def __str__(self) -> str:
276300
return "aalignof(" + str(self.typ) + ")"
277301

@@ -294,6 +318,9 @@ def param(self) -> CAttr:
294318
def is_alignofe(self) -> bool:
295319
return True
296320

321+
def accept(self, visitor: "CVisitor") -> None:
322+
visitor.visit_attr_alignofe(self)
323+
297324
def __str__(self) -> str:
298325
return "aalignofe(" + str(self.param) + ")"
299326

@@ -316,6 +343,9 @@ def typsig(self) -> "CTypsig":
316343
def is_alignofs(self) -> bool:
317344
return True
318345

346+
def accept(self, visitor: "CVisitor") -> None:
347+
visitor.visit_attr_alignofs(self)
348+
319349
def __str__(self) -> str:
320350
return "aalignofs(" + str(self.typsig) + ")"
321351

@@ -343,6 +373,9 @@ def param(self) -> CAttr:
343373
def is_unop(self) -> bool:
344374
return True
345375

376+
def acecpt(self, visitor: "CVisitor") -> None:
377+
visitor.visit_attr_unop(self)
378+
346379
def __str__(self) -> str:
347380
return "aunop(" + self.op + "," + str(self.param) + ")"
348381

@@ -375,6 +408,9 @@ def param2(self) -> CAttr:
375408
def is_binop(self) -> bool:
376409
return True
377410

411+
def accept(self, visitor: "CVisitor") -> None:
412+
visitor.visit_attr_binop(self)
413+
378414
def __str__(self) -> str:
379415
return (
380416
"abinop("
@@ -410,6 +446,9 @@ def param(self) -> CAttr:
410446
def is_dot(self) -> bool:
411447
return True
412448

449+
def accept(self, visitor: "CVisitor") -> None:
450+
visitor.visit_attr_dot(self)
451+
413452
def __str__(self) -> str:
414453
return "adot(" + str(self.param) + "." + self.suffix + ")"
415454

@@ -436,6 +475,9 @@ def param(self) -> CAttr:
436475
def is_star(self) -> bool:
437476
return True
438477

478+
def accept(self, visitor: "CVisitor") -> None:
479+
visitor.visit_attr_star(self)
480+
439481
def __str__(self) -> str:
440482
if self.index == self.args[0]:
441483
return "astar()"
@@ -461,6 +503,9 @@ def param(self) -> CAttr:
461503
def is_addrof(self) -> bool:
462504
return True
463505

506+
def accept(self, visitor: "CVisitor") -> None:
507+
visitor.visit_attr_addrof(self)
508+
464509
def __str__(self) -> str:
465510
return "aaddrof(" + str(self.param) + ")"
466511

@@ -488,6 +533,9 @@ def param2(self) -> CAttr:
488533
def is_index(self) -> bool:
489534
return True
490535

536+
def accept(self, visitor: "CVisitor") -> None:
537+
visitor.visit_attr_index(self)
538+
491539
def __str__(self) -> str:
492540
return "aindex(" + str(self.param1) + "," + str(self.param2) + ")"
493541

@@ -516,6 +564,9 @@ def param2(self) -> CAttr:
516564
def param3(self) -> CAttr:
517565
return self.cd.get_attrparam(int(self.args[2]))
518566

567+
def accept(self, visitor: "CVisitor") -> None:
568+
visitor.visit_attr_question(self)
569+
519570
def __str__(self) -> str:
520571
return (
521572
"aquestion("
@@ -541,6 +592,9 @@ def name(self) -> str:
541592
def params(self) -> List[CAttr]:
542593
return [self.cd.get_attrparam(int(i)) for i in self.args]
543594

595+
def accept(self, visitor: "CVisitor") -> None:
596+
visitor.visit_attribute(self)
597+
544598
def __str__(self) -> str:
545599
return self.name + ": " + ",".join([str(p) for p in self.params])
546600

chc/app/CCompInfo.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
# Copyright (c) 2017-2020 Kestrel Technology LLC
88
# Copyright (c) 2020-2022 Henny B. Sipma
9-
# Copyright (c) 2023-2024 Aarno Labs LLC
9+
# Copyright (c) 2023-2025 Aarno Labs LLC
1010
#
1111
# Permission is hereby granted, free of charge, to any person obtaining a copy
1212
# of this software and associated documentation files (the "Software"), to deal
@@ -39,6 +39,7 @@
3939
from chc.app.CDeclarations import CDeclarations
4040
from chc.app.CFieldInfo import CFieldInfo
4141
from chc.app.CGlobalDeclarations import CGlobalDeclarations
42+
from chc.app.CVisitor import CVisitor
4243

4344

4445
class CCompInfo(CDeclarationsRecord):
@@ -100,6 +101,9 @@ def name(self) -> str:
100101
def field_strings(self) -> str:
101102
return ":".join([f.fname for f in self.fields])
102103

104+
def accept(self, visitor: "CVisitor") -> None:
105+
visitor.visit_compinfo(self)
106+
103107
def __str__(self) -> str:
104108
lines = []
105109
lines.append("struct " + self.name)

chc/app/CExp.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
from chc.app.CDictionaryRecord import CDictionaryRecord, cdregistry
3434

35+
import chc.util.fileutil as UF
3536
import chc.util.IndexedTable as IT
3637

3738
if TYPE_CHECKING:
@@ -148,15 +149,15 @@ def get_strings(self) -> List[str]:
148149
def get_variable_uses(self, vid: int) -> int:
149150
return 0
150151

151-
def accept(self, visitor: "CVisitor") -> None:
152-
raise Exception("CExp.accept: " + str(self))
153-
154152
def to_dict(self) -> Dict[str, Any]:
155153
return {"base": "exp"}
156154

157155
def to_idict(self) -> Dict[str, Any]:
158156
return {"t": self.tags, "a": self.args}
159157

158+
def accept(self, visitor: "CVisitor") -> None:
159+
raise UF.CHCError("visitor not yet implemented for: " + str(self))
160+
160161
def __str__(self) -> str:
161162
return "baseexp:" + self.tags[0]
162163

@@ -223,6 +224,9 @@ def get_variable_uses(self, vid: int) -> int:
223224
def to_dict(self) -> Dict[str, Any]:
224225
return {"base": "lval", "lval": self.lval.to_dict()}
225226

227+
def accept(self, visitor: "CVisitor") -> None:
228+
visitor.visit_explval(self)
229+
226230
def __str__(self) -> str:
227231
return str(self.lval)
228232

@@ -248,6 +252,9 @@ def is_sizeof(self) -> bool:
248252
def to_dict(self) -> Dict[str, Any]:
249253
return {"base": "sizeof", "type": self.typ.to_dict()}
250254

255+
def accept(self, visitor: "CVisitor") -> None:
256+
visitor.visit_sizeof(self)
257+
251258
def __str__(self) -> str:
252259
return "sizeof(" + str(self.typ) + ")"
253260

@@ -279,6 +286,9 @@ def get_variable_uses(self, vid: int) -> int:
279286
def to_dict(self) -> Dict[str, Any]:
280287
return {"base": "sizeofe", "exp": self.exp.to_dict()}
281288

289+
def accept(self, visitor: "CVisitor") -> None:
290+
visitor.visit_sizeofe(self)
291+
282292
def __str__(self) -> str:
283293
return "sizeofe(" + str(self.exp) + ")"
284294

@@ -307,6 +317,9 @@ def is_sizeofstr(self) -> bool:
307317
def to_dict(self) -> Dict[str, Any]:
308318
return {"base": "sizeofstr", "string": self.stringvalue}
309319

320+
def accept(self, visitor: "CVisitor") -> None:
321+
visitor.visit_size_of_str(self)
322+
310323
def __str__(self) -> str:
311324
return "sizeofstr(" + str(self.stringvalue) + ")"
312325

@@ -332,6 +345,9 @@ def is_alignof(self) -> bool:
332345
def to_dict(self) -> Dict[str, Any]:
333346
return {"base": "alignof", "type": self.typ.to_dict()}
334347

348+
def accept(self, visitor: "CVisitor") -> None:
349+
visitor.visit_alignof(self)
350+
335351
def __str__(self) -> str:
336352
return "alignof(" + str(self.typ) + ")"
337353

@@ -366,6 +382,9 @@ def get_variable_uses(self, vid: int) -> int:
366382
def to_dict(self) -> Dict[str, Any]:
367383
return {"base": "alignofe", "exp": self.exp.to_dict()}
368384

385+
def accept(self, visitor: "CVisitor") -> None:
386+
visitor.visit_alignofe(self)
387+
369388
def __str__(self) -> str:
370389
return "alignofe(" + str(self.exp) + ")"
371390

@@ -411,6 +430,9 @@ def get_variable_uses(self, vid: int) -> int:
411430
def to_dict(self) -> Dict[str, Any]:
412431
return {"base": "unop", "op": self.op, "exp": self.exp.to_dict()}
413432

433+
def accept(self, visitor: "CVisitor") -> None:
434+
visitor.visit_unop(self)
435+
414436
def __str__(self) -> str:
415437
return "(" + unoperatorstrings[self.op] + " " + str(self.exp) + ")"
416438

@@ -474,6 +496,9 @@ def to_dict(self) -> Dict[str, Any]:
474496
"exp2": self.exp2.to_dict(),
475497
}
476498

499+
def accept(self, visitor: "CVisitor") -> None:
500+
visitor.visit_binop(self)
501+
477502
def __str__(self) -> str:
478503
return (
479504
"("
@@ -549,6 +574,9 @@ def to_dict(self) -> Dict[str, object]:
549574
"type": self.typ.to_dict(),
550575
}
551576

577+
def accept(self, visitor: "CVisitor") -> None:
578+
visitor.visit_question(self)
579+
552580
def __str__(self) -> str:
553581
return (
554582
"("
@@ -600,6 +628,9 @@ def to_dict(self) -> Dict[str, object]:
600628
"type": self.typ.to_dict(),
601629
}
602630

631+
def accept(self, visitor: "CVisitor") -> None:
632+
visitor.visit_cast(self)
633+
603634
def __str__(self) -> str:
604635
return "caste(" + str(self.typ) + "," + str(self.exp) + ")"
605636

@@ -634,6 +665,9 @@ def get_variable_uses(self, vid: int) -> int:
634665
def to_dict(self) -> Dict[str, object]:
635666
return {"base": "addrof", "lval": self.lval.to_dict()}
636667

668+
def accept(self, visitor: "CVisitor") -> None:
669+
visitor.visit_addrof(self)
670+
637671
def __str__(self) -> str:
638672
return "&(" + str(self.lval) + ")"
639673

@@ -655,6 +689,9 @@ def label_sid(self) -> int:
655689
def to_dict(self) -> Dict[str, object]:
656690
return {"base": "addroflabel", "label": self.label_sid}
657691

692+
def accept(self, visitor: "CVisitor") -> None:
693+
visitor.visit_addr_of_label(self)
694+
658695
def __str__(self) -> str:
659696
return "addroflabel(" + str(self.label_sid) + ")"
660697

@@ -689,6 +726,9 @@ def get_variable_uses(self, vid: int) -> int:
689726
def to_dict(self) -> Dict[str, Any]:
690727
return {"base": "startof", "lval": self.lval.to_dict()}
691728

729+
def accept(self, visitor: "CVisitor") -> None:
730+
visitor.visit_startof(self)
731+
692732
def __str__(self) -> str:
693733
return "&(" + str(self.lval) + ")"
694734

@@ -722,6 +762,9 @@ def arguments(self) -> List[Optional[CExp]]:
722762
def has_variable(self, vid: int) -> bool:
723763
return any([a.has_variable(vid) for a in self.arguments if a])
724764

765+
def accept(self, visitor: "CVisitor") -> None:
766+
visitor.visit_fn_app(self)
767+
725768
def __str__(self) -> str:
726769
return (
727770
"fnapp("
@@ -762,6 +805,9 @@ def arguments(self) -> List[Optional[CExp]]:
762805
def has_variable(self, vid: int) -> bool:
763806
return any([a.has_variable(vid) for a in self.arguments if a])
764807

808+
def accept(self, visitor: "CVisitor") -> None:
809+
visitor.visit_cn_app(self)
810+
765811
def __str__(self) -> str:
766812
return (
767813
"cnapp("

0 commit comments

Comments
 (0)