Type check for externalDeclarations

as long as I did not misunderstand CheckSemantic Method in class Compiler, we are supposed to do a TypeCheck for the stored ExternalDeclarations from the List in AstFactoryImplementation, with which we stored the created fuctiondefitions and functiondeclarations.

what are we supposed to do the TypeCheck method for the constructs, namely global variable, FunctionDeclaration and Function?
like globalVariable , it only has Type and Identifier as fields…what should we do here since theses two are not expression or statements…how should we do a type check for it?

for functionDeclaration there is a parameterlist, in which parameters have Type and identifer…so i only need to check whether they are void Type?

for Function(Definition) there is namedparameterlist and a block
check if it has namedparameters of vpid type and do a Typecheck for block which we did in statements?

Note that semantic analysis is not just type checking, you should read section 4.2 of the specification for some inspiration for things to do with external declarations during semantic analysis.

you mean we should also add theses externalDeclarations to the scope whenever it is possible?
when they are already in the declarationList from scope, then check if they fulfill the redeclarations conditions? if they are not in this list, then just add them to the scope?

The variables declared in the global scope must be visible to the inner scopes, so yes you have to store something. And whenever you do so you need to check that no illegal redeclaration (or any redefinition) takes place. Essentially you have to (try to) add all external declarations of variables into the global scope and you have to typecheck each one. Depending on the order of these operations you get different results. This is legal for example:

int g();
int f() {return g();}
int g() {return f();}

but becomes illegal if you remove the first declaration of g.

I don’t think a list is necessary for the scope, in fact the lecture presented an approach where you use a map instead.

1 Like

when we are adding a globalexternal Dec to a scope, should we create a new one scope or just check if the scope has no parent (which means it is a outer scope for externalDecs)?

int f() {return g();}
when we are checking for this functiondefinition:
it should have functiontype
the returntypen of a function type should be scalar type
the parameters should not be void
the body ({return g();}) should be a blockstatement

but how can i check the validity of the statements inside this block…because in a block statment it has a list of statements, i can of couse do a for each travelsal for them and check their type validity.

{
int x;
int y
int z;
int f(int x, int y);
int f(int x, int y) {
    int x; // error, already declared, but how can i know this is in the first scope for this function, it has parent , namely the globle scope, where the externalDecs are defined.
     { 
        int x = 1;  //inner scope...redeclaration is okay...but how can i check and know if this declaration statement is in this inner scope?
        x = x+ 1;
       y = y+1;  //okay, defined in the function parameterslist
      z = z + y; //also okay, z is a global variable should be in the externalDeclaration map table
    }
}

so basically, my problem is how to get and know if my current scope is the first scope of a function, since its parent is not null…

Neither.

The “global scope” is just a scope. More specifically, it should be the instance of Scope that you pass to all global declarations / function definitions.

It should never happen that you want to typecheck a global thing with a non-global scope, so you would not need to check for this.

No, object type. void-returning functions exist.

It always is, that’s assured by the grammar

By calling checkSemantics on them

Write your code such that the ofher option never happens.