Variables declared at module-level with types that are class types must set a dependency on said class running its static initializer

This commit is contained in:
Tristan B. Kildaire 2021-06-06 18:38:07 +02:00
parent b2eca50f5f
commit 4aeca83d90
1 changed files with 33 additions and 11 deletions

View File

@ -71,7 +71,17 @@ public void dependancyGenerate(TypeChecker tc, Container container)
if(container == tc.getModule())
{
/**
* If it is a variable
* If we have a module-level variable declaration then we want to check
* if the type of the variable being declared is:
*
* 1. Basic (int, float, etc.)
* - Then we will do nothing, it is not dependent
* 2. Non-basic (Class-case only)
* - Then we will check if that class is accessible
* 1. If it is at the module-level then it is implied static so it would be
* 2. If at a level deeper then more care must be taken
*
* TODO: Assignments still not supported as this means more checking
*/
if(cast(Variable)entity)
{
@ -81,19 +91,31 @@ public void dependancyGenerate(TypeChecker tc, Container container)
Type variableType = tc.getType(container, variable.getType());
/**
* TODO: Here we must decide, if it is basic type then no init -> no dependence
* The only depenedence would be what comes next (a.k.a. if an assignment exists)
* then dependence from the expression must be checked
*
* Non-basic types that have static initiliazation on type reference: Class
* Check if the type is a class type
*/
if(cast(Clazz)variableType)
{
/**
* Mark the variable as dependent on the class type (`variableType`) having
* been statically initialized
*/
encounter(tc, variable, variableType);
Clazz classType = cast(Clazz)variableType;
/* If the class is defined at the module-level then it is static by default */
if(classType.parentOf() == tc.getModule())
{
/**
* Then mark the class as a dependency (the class-name/type reference)
* must cause the static initialization to go off
*
* module pp;
* Person k;
* class Person { }
*
* Above it means that because the type of `k` is `Person` and that is a
* class type therefore the Person class should have its static constructor
* run (=> all static members should be initialized)
*/
encounter(tc, variable, classType);
}
}
/* If then variable has an assignment */