Skip to content

statics.stx

pdmosses/metaborg-poosl/org.metaborg.lang.poosl/trans/statics.stx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
module statics

imports
    signatures/Poosl-sig
    signatures/Common-sig
    statics-comm
    statics-expr-stat
    statics-typing
    statics-opt
    statics-names

// see README.md for details on how to switch to multi-file analysis
// TODO file-based imports (not namespace-based imports)

rules // single-file entry point

    programOk : Poosl
    programOk(Poosl(ImportList(imports), ClassList())) :- {}
        new s_poosl,
        try { selectSystemClasses(classes) == [_|[]] } | error $[There should be exactly 1 system class],
        okClasses(s_poosl, classes).

rules // multi-file entry point

    projectOk : scope
    projectOk(s).

    fileOk : scope * Poosl
    fileOk(s, Poosl(ImportList(imports), ClassList())) :- {}
        new s_poosl, s_poosl -P-> s,
        try { selectSystemClasses(classes) == [_|[]] } | error $[There should be exactly 1 system class],
        okClasses(s_poosl, classes).

rules // === Classes ( -P-> ) =======

     maps okClass(*, list(*))
     : scope * Class
    okClass(, DataClass(AnnotationList(annotations), nativeClause, , extendsClause, DeclarationOptCommaList(variables), DataMethodList(methods))) :-
        {} new s_internal, s_internal -P-> s,             // variables, methods
        {} T_extends == okExtendsClauseDataClass(s_internal, name, extendsClause), 
        declareDataClass(s, name, TypeDataClass(s_internal, T_extends)),
        okDeclarationOptCommas(s_internal, variables),
        okDataMethods(s, s_internal, name, TypeDataClass(s_internal, T_extends), methods).

    okClass(, ProcessClass(AnnotationList(annotations), name, optParameters, extendsClause, PortList(ports), MessageSignatureList(messages), DeclarationOptCommaList(variables), initMethodCall, ProcessMethodList(methods))) :-
        {} new s_external, s_external -P-> s,             // parameters, ports, messages
        {} new s_internal, s_internal -P-> s_external,    // variables, methods
        okExtendsClauseProcessClass(s_external, s_internal, extendsClause),
        declareProcessOrClusterClass(s, name, TypeProcessClass(s_external, s_internal)),
        okDeclarations(s_external, stripOptParameterList(optParameters)) == _,
        okPorts(s_external, ports),
        okMessageSignatures(s_external, messages),
        okDeclarationOptCommas(s_internal, variables),
        okProcessMethodCall(s_internal, initMethodCall),
        okProcessMethods(s_internal, methods).

    okClass(, ClusterClass(AnnotationList(annotations), name, optParameters, PortList(ports), InstanceList(instances), ChannelList(channels))) :-
        {} new s_external, s_external -P-> s,             // parameters, ports
        {} new s_internal, s_internal -P-> s_external,    // instances, channels
        declareProcessOrClusterClass(s, name, TypeClusterClass(s_external)),
        okDeclarations(s_external, stripOptParameterList(optParameters)) == _,
        okPorts(s_external, ports),
        okInstances(s_internal, instances),
        okChannels(s_internal, channels).

    okClass(s, System(AnnotationList(annotations), InstanceList(instances), ChannelList(channels))) :-
        {} new s_internal, s_internal -P-> s,             // instances, channels
        okInstances(s_internal, instances),
        okChannels(s_internal, channels).

// Note: no scopes associated to ports (for the related message signatures), because of inheritance.

rules // === Class extends clauses ( -I-> ) =======

     : scope * string * ExtendsClause -> TypeDataClass
    okExtendsClauseDataClass(s_internal, "Object", _) = TypeDataClassRoot().
    okExtendsClauseDataClass(, _, NoExtends()) = TypeDataClass(, T_extends) :-
        TypeDataClass(s2_internal, T_extends) == typeOfDataClassNoRef(s_internal, "Object"),
        s_internal -I-> s2_internal.
    okExtendsClauseDataClass(, _, Extends(name)) = TypeDataClass(, T_extends) :-
        TypeDataClass(s2_internal, T_extends) == typeOfDataClass(s_internal, name),
        s_internal -I-> s2_internal.

     : scope * scope * ExtendsClause
    okExtendsClauseProcessClass(s_external, s_internal, NoExtends()).
    okExtendsClauseProcessClass(s_external, , Extends(name)) :-
        { } TypeProcessClass(s2_external, s2_internal) == typeOfProcessOrClusterClass(s_internal, name),
        s_external -I-> s2_external,
        s_internal -I-> s2_internal.

rules // === Methods ( -P-> ) =======

    okProcessMethods maps okProcessMethod(*, list(*))
     : scope * ProcessMethod
    okProcessMethod(, ProcessMethod(AnnotationList(annotations), name, ParameterList(inParameters), ParameterList(outParameters), optLocalVariables, body)) :-
        {} new s_method, s_method -P-> s_internal,
        {} typesInputParameters == okDeclarations(s_method, inParameters),
        {} typesOutputParameters == okDeclarations(s_method, outParameters),
        declareProcessMethod(s_internal, name, TypeProcessMethod(flattenDataClasses(typesInputParameters), flattenDataClasses(typesOutputParameters))),
        okDeclarations(s_method, stripOptLocalVariablesList(optLocalVariables)) == _,
        okStatement(s_method, body).

    okDataMethods maps okDataMethod(*, *, *, *, list(*))
     : scope * scope * string * TypeDataClass * DataMethod
    okDataMethod(, s_internal, cname, , DataMethodNamed(AnnotationList(annotations), name, optParameters, returnType, optLocalVariables, )) :-
        {} new s_method, s_method -P-> s_internal,
        {} typesInputParameters == flattenDataClasses(okDeclarations(s_method, stripOptParameterList(optParameters))),
        {} typeReturnValue == typeOfDataClass(s, returnType),
        declareNamedDataMethod(s, cname, name, TypeDataMethod(typeClass, typesInputParameters, typeReturnValue)),
        okDeclarations(s_method, stripOptLocalVariablesList(optLocalVariables)) == _,
        {} type_Body == okExpression(s_method, typeClass, body),
        try { compatibleType(type_Body, typeReturnValue) } | warning $[Incompatible type] @body.
    okDataMethod(, s_internal, cname, , DataMethodUnary(AnnotationList(annotations), name, _, returnType, optLocalVariables, )) :-
        {} new s_method, s_method -P-> s_internal,
        {} typeReturnValue == typeOfDataClass(s, returnType),
        declareUnaryDataMethod(s, cname, name, TypeDataMethod(typeClass, [], typeReturnValue)),
        okDeclarations(s_method, stripOptLocalVariablesList(optLocalVariables)) == _,
        {} type_Body == okExpression(s_method, typeClass, body),
        try { compatibleType(type_Body, typeReturnValue) } | warning $[Incompatible type] @body.
    okDataMethod(, s_internal, cname, , DataMethodBinary(AnnotationList(annotations), name, , returnType, optLocalVariables, )) :-
        {} new s_method, s_method -P-> s_internal,
        {} typesInputParameters == flattenDataClasses(okDeclarations(s_method, [parameter])),
        try { [_|[]] == typesInputParameters } | error $[Expected only 1 argument] @parameter,
        {} typeReturnValue == typeOfDataClass(s, returnType),
        declareBinaryDataMethod(s, cname, name, TypeDataMethod(typeClass, typesInputParameters, typeReturnValue)),
        okDeclarations(s_method, stripOptLocalVariablesList(optLocalVariables)) == _,
        {} type_Body == okExpression(s_method, typeClass, body),
        try { compatibleType(type_Body, typeReturnValue) } | warning $[Incompatible type] @body.

    okDataMethod(, s_internal, cname, typeClass, DataMethodNamedNative(name, optParameters, returnType)) :-
        {} new s_method, s_method -P-> s_internal,
        {} typesInputParameters == flattenDataClasses(okDeclarations(s_method, stripOptParameterList(optParameters))),
        {} typeReturnValue == typeOfDataClass(s, returnType),
        declareNamedDataMethod(s, cname, name, TypeDataMethod(typeClass, typesInputParameters, typeReturnValue)).
    okDataMethod(, s_internal, cname, typeClass, DataMethodUnaryNative(name, _, returnType)) :-
        {} typeReturnValue == typeOfDataClass(s, returnType),
        declareUnaryDataMethod(s, cname, name, TypeDataMethod(typeClass, [], typeReturnValue)).
    okDataMethod(, s_internal, cname, typeClass, DataMethodBinaryNative(name, , returnType)) :-
        {} new s_method, s_method -P-> s_internal,
        {} typesInputParameters == flattenDataClasses(okDeclarations(s_method, [parameter])),
        try { [_|[]] == typesInputParameters } | error $[Expected only 1 argument] @parameter,
        {} typeReturnValue == typeOfDataClass(s, returnType),
        declareBinaryDataMethod(s, cname, name, TypeDataMethod(typeClass, typesInputParameters, typeReturnValue)).

rules

     : list(Class) -> list(Class)
    selectSystemClasses([]) = [].
    selectSystemClasses([System(AnnotationList(annotations), instances, channels)|Cs]) = [System(AnnotationList(annotations), instances, channels)|selectSystemClasses(Cs)].
    selectSystemClasses([_|Cs]) = selectSystemClasses(Cs).