Create identifiers for anonymous types #36059
Replies: 2 comments 5 replies
-
When creating identifiers for anonymous types, it is useless to have the type of node. i.e. So when implementing this my plan is to create a stack and push relevant suffixes. We should start this from a public node identifier. |
Beta Was this translation helpful? Give feedback.
-
I think we need to improve case 3 and case 7 right. They have the same existing logic. imagine what happens if these are inside a loop. Can we still identify them uniquely?
|
Beta Was this translation helpful? Give feedback.
-
Introduction
According to the current implementation, anonymous types are named using the format
$anonType$_x
wherex
is an integer.x
is just an incrementing value. Because of this naming format, we have to face some issues as mentioned below.Original problem
The original problem can be explained using an example. Let’s say we are representing an anonymous type using
$anonType$_1
in langlib Lib. Also, we have a moduleMod
, which uses Lib. We have built Mod on language versionv1
which is an older version. Assume that there is a cast to$anonType$_1
in Mod. Now we build Lib in language versionv2
which is a new version. In new Lib,$anonType$_1
points to a different anonymous type. Still,Mod
is built onv1
. Therefore we can see a runtime cast error.Current solutions
In runtime
Generate a hash for each type based on its shape. Create a lookup method called
getAnonType
which accepts a hash and returns a type. This method can be found in module init. When an anonymous type that is defined inMod1
is used in a different moduleMod2
, generate the hash and get the anonymous type usingMod1.getAnonType
method. Please check #29792In compile time
Let's say, there is an anonymous type $anonType$_0 which is defined in Mod1 and there is a reference to $anonType$_0 from Mod2.
Initially, both modules are built on older version v1. Now, Mod1 is built on newer version v2 and $anonType$_0 does not represent the previous type. When the symbols are entered in BIRPackageSymbolEnter for Mod2, it is unable to lookup the correct type for $anonType$_0 from Mod1.
In order to solve this problem, a method called
getType
was introduced inBIRTypeReader
. This method reads the type from the byte stream and looks up the corresponding type fromMod2
and checks whether those shapes are the same. Please check #35870.Use of anonymous types
Anonymous types are used to create a type node when the type name is not specified. The current format of anonymous type names is
$anonType$_x
(x is an integer which increases by 1). At runtime there is a class for each anonymous type.Anonymous types creation
Anonymous types are created when the type is not defined explicitly. Please check the following examples.
An anonymous type is created for
record {| int i; |}
.An anonymous type is created for the type of
address
.An anonymous type is created for the type of
rec
.Four anonymous types are created for each type cast.
An anonymous type is created for type of
XX
.An anonymous type is created for
rec
.var
declarationAn anonymous type is created for the mapping value
{ i: 2 }
.Two anonymous types are created for two enum members.
Goal
The ultimate goal is to create anonymous types to be able to uniquely identify them.
Approach
Create the name of the anonymous type based on the place where it located.
Let's go through the each case that mentioned in
Anonymous types creation
.Case 1
Since the anonymous type created for the return type of function we can create the name of anonymous type like
$anonType$FUNC_RETURN$foo$
.Case 2
The anonymous type of address can be created with a name like
$anonType$FIELD$address$Person$
.Case 3
The name of the anonymous type can be created using the local variable. But if the local variable is a wildcard, it cannot be used to create anonymous type name because the same statement can be repeated several times with a single block. Since the local variable are not referred outside it’s scope, it is not need to uniquely identify this anonymous type. Therefore a name like
$anonType$x
can be used. (x is an integer)Case 4
This is same as
Case 3
.Case 5
The anonymous type of XX can be created with a name like
$anonType$CONST$XX$
.Case 6
The anonymous type of rec can be created with a name like
$anonType$MODULE_VAR$rec$
.Case 7
This is same as Case 3.
Case 8
The two anonymous types for RED and GREEN can be created with names like
$anonType$ENUM$RED$
and$anonType$ENUM$GREEN$
.Note: The org name and module name can be appended additionally.
Beta Was this translation helpful? Give feedback.
All reactions