1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2015 Romain Chanoir <romain.chanoir@viacesi.fr>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 # Phase generating threads for functions annotated with `threaded` annotation
19 module parallelization_phase
21 private import parser_util
24 private import annotation
26 redef class ToolContext
27 # Transforms a function annotated with "threaded"
28 var parallelization_phase
: Phase = new ParallelizationPhase(self, null)
31 private class ParallelizationPhase
34 redef fun process_annotated_node
(nmethdef
, nat
)
36 if nat
.n_atid
.n_id
.text
!= "threaded" then return
38 if not nmethdef
isa AMethPropdef then
39 toolcontext
.error
(nat
.location
, "Syntax Error: only a method can be threaded.")
43 #TODO: check for self calls
45 # Get the module associated with this method
46 var amod
= nmethdef
.parent
.parent
47 assert amod
isa AModule
49 # Construct the name of the generated class
50 var classname
= "Threaded"
52 # Try to get the name of the class
53 if nmethdef
.parent
isa AStdClassdef then
54 classname
+= nmethdef
.parent
.as(AStdClassdef).n_qid
.n_id
.text
57 # Try to get the name of the method
58 if nmethdef
.n_methid
isa AIdMethid then
59 classname
+= nmethdef
.n_methid
.as(AIdMethid).n_id
.text
62 # Handle methods with a return value
63 var has_rvalue
= nmethdef
.n_signature
.n_type
!= null
66 vtype
= "redef type E: " + nmethdef
.n_signature
.n_type
.n_qid
.n_id
.text
69 # create a return type
70 var n_id
= new TClassid
72 var n_qid
= new AQclassid
74 var n_type
= new AType
76 nmethdef
.n_signature
.n_type
= n_type
78 var params
= new Array[String]
79 for param
in nmethdef
.n_signature
.n_params
do
80 var typ
= param
.n_type
.n_qid
.n_id
.text
81 if param
.n_type
.n_kwnullable
!= null then typ
= "nullable {typ}"
83 var {{{param.n_id.text}}}: {{{typ}}}
87 # String corresponding to the generated class
88 var classdef_source
= """
94 {{{params.join("\n")}}}
100 # Parse newly obtained classdef
101 var classdef
= toolcontext
.parse_classdef
(classdef_source
)
102 assert classdef
isa AStdClassdef
104 # Get the `main` fun of the class
105 var mainfun
: nullable AMethPropdef = null
106 for prop
in classdef
.n_propdefs
do
107 if prop
isa AMethPropdef then mainfun
= prop
109 assert mainfun
!= null
111 # Make the statements from `main` fun be the statements from the "threaded" fun
112 mainfun
.n_block
= nmethdef
.n_block
114 # Add "return null" to the end of the `main` function
115 if not has_rvalue
then
116 var s_nullreturn
= "return null"
117 var nullreturn
= toolcontext
.parse_something
(s_nullreturn
)
118 assert nullreturn
isa AExpr
119 mainfun
.n_block
.as(ABlockExpr).n_expr
.add
(nullreturn
)
122 # Create new body for the annotated fun
123 var s_newbody
: String
124 if nmethdef
.n_signature
.n_params
.not_empty
then
125 var init_params
= new Array[String]
126 for param
in nmethdef
.n_signature
.n_params
do
127 init_params
.add
(param
.n_id
.text
)
130 var thread = new {{{classname}}}({{{init_params.join(",")}}})
136 var thread = new {{{classname}}}
142 var newbody
= toolcontext
.parse_something
(s_newbody
)
143 nmethdef
.n_block
= newbody
.as(ABlockExpr)
147 # Add the new class to the module
148 amod
.n_classdefs
.add
(classdef
)