-
I want to calculate the size of type defined in Golang programs and I fail to find libraries to do that. Therefore I try to do it myself. There exists types whose size can be unknown, so I mark them as -1 to indicate they are unknown. But problem occurs when I meet struct type. I first check its fields to see if the size is unknown. If it is, I will set The program is as follows: language[monotonicAggregates]
int typeSize(Type type) {
exists(Type t | type.getUnderlyingType() = t |
...
else if t instanceof StructType then
if exists(Field f| f = t.getField(_) and -1 = typeSize(f.getType())) then
result = -1
else
result = sum(Field f| f = t.getField(_)|typeSize(f.getType()))
...
) I add |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Can you give the full code for typeSize and all the predicates it depends on? |
Beta Was this translation helpful? Give feedback.
-
I think your problem is caused by the use of
|
Beta Was this translation helpful? Give feedback.
-
Another note: a more idiomatic way of representing "unknown" is to have the predicate not hold in those cases. Like this:
|
Beta Was this translation helpful? Give feedback.
I think your problem is caused by the use of
if A then B else C
. That desugars toA and B or not A and C
. This introduces lots ofnot
s, and I guess an odd number of them are getting into the recursion. It's a bit nicer to rewrite it withoutif then else
, since most of your conditions are mutually exclusive. This works for me: