winIDEA SDK
show_type_hierarchy.py
1# This script is licensed under BSD License, see file LICENSE.txt.
2#
3# (c) TASKING Germany GmbH, 2023
4
5import isystem.connect as ic
6
7winidea_id = ''
8
9
10class CVariable:
11 """
12 This class contains type information of C variable.
13 """
14
15 def __init__(self, ivar : ic.IVariable):
16 # Variable name without any decoration (module, partition, ...),
17 # for example 'iCounter'.
18 self.name = ivar.Name()
19 # Variable qualified name.
20 self.qName = ivar.QualifiedName()
21 self.typeName = ivar.TypeName()
22 # Variable type, as enumeration type IVariable::EType.
23 self.ctype = ivar.Type()
24 # Array dimension for arrays, 0 for all other variables.
25 self.arrayDim = ivar.ArrayDimension()
26 # Index of the module, where the variable is located.
27 self.moduleIdx = ivar.Module()
28 # Variable scope - e.g. class name.
29 self.scope = ivar.Scope()
30 self.memArea = ivar.MemArea()
31 self.address = ivar.Address()
32 self.size = ivar.Size()
33 itype = ivar.GetIType().Type()
34 # Type of variable, see SType::EType for possible values.
35 self.stype = itype.m_byType
36 # Size of variable in bits, for primitive types only.
37 self.bitSize = itype.m_byBitSize
38
39 self.children = []
40
41
42 def addChild(self, cvar):
43 self.children.append(cvar)
44
45
46 def toString(self, indent: int):
47
48 istr = ' ' * indent
49
50 return istr + '- Name: ' + self.name + \
51 '\n' + istr + ' QualifiedName: ' + self.qName + \
52 '\n' + istr + ' TypeName: ' + self.typeName + \
53 '\n' + istr + ' Type: ' + str(self.ctype) + \
54 '\n' + istr + ' rArrayDimension: ' + str(self.arrayDim) + \
55 '\n' + istr + ' Module: ' + str(self.moduleIdx) + \
56 '\n' + istr + ' Scope: ' + self.scope + \
57 '\n' + istr + ' MemArea: ' + str(self.memArea) + \
58 '\n' + istr + ' Address: ' + hex(self.address) + \
59 '\n' + istr + ' Size: ' + str(self.size) + \
60 '\n' + istr + ' SType: ' + str(self.stype) + \
61 '\n' + istr + ' BitSize: ' + str(self.bitSize)
62
63
64def getTypeInfo(dataCtrl2 : ic.CDataController2,
65 expressionOrVarName : str,
66 maxArrayLen : int):
67 """
68 Returns type information for the given string as hierarchical data
69 structure.
70 """
71 typeInfo = dataCtrl2.getExpressionType(0, expressionOrVarName)
72
73 exprInfo = CVariable(typeInfo.Expression())
74 children = typeInfo.Children()
75
76 numArrayElements = min(exprInfo.arrayDim, maxArrayLen);
77
78 # add struct children
79 numChildren = children.size()
80 for childIdx in range(numChildren):
81 ivarChild = children.at(childIdx)
82 child = getTypeInfo(dataCtrl2, ivarChild.QualifiedName(), maxArrayLen)
83 exprInfo.addChild(child)
84
85 # add array elements
86 for idx in range(numArrayElements):
87
88 elementType = exprInfo.typeName;
89 endOfNameIdx = elementType.find('[');
90 elementType = elementType[:endOfNameIdx].strip();
91 elementIdx = "[" + str(idx) + "]";
92
93 child = getTypeInfo(dataCtrl2, exprInfo.name + elementIdx, maxArrayLen);
94 exprInfo.addChild(child)
95
96 dataCtrl2.release(typeInfo)
97
98 return exprInfo;
99
100
101def printTree(cvar : CVariable, dataCtrl2, indent = 0):
102 """
103 Prints the given cvar and all of its children. If dataCtrl2 != None,
104 values of primtive variables are also printed.
105 """
106
107 print(cvar.toString(indent))
108
109 if dataCtrl2 and cvar.ctype == ic.IVariable.tSimple:
110 cval = dataCtrl2.evaluate(ic.IConnectDebug.fMonitor, cvar.qName)
111 print(' ' * indent + ' Value: ' + cval.getResult())
112
113
114 for child in cvar.children:
115 printTree(child, dataCtrl2, indent + 2)
116
117
118
119print('isystem.connect version: ' + ic.getModuleVersion())
120
121cmgr = ic.ConnectionMgr()
122cmgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
123
124dataCtrl2 = ic.CDataController2(cmgr)
125
126info = getTypeInfo(dataCtrl2, 'g_complexStruct', 100)
127
128printTree(info, dataCtrl2)