9613c5311846f882499de7bb038bd4de8470cb3f
[nit.git] / src / frontend / parallelization_phase.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2015 Romain Chanoir <romain.chanoir@viacesi.fr>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17
18 # Phase generating threads for functions annotated with `threaded` annotation
19 module parallelization_phase
20
21 private import parser_util
22 import modelize
23 import astbuilder
24 private import annotation
25
26 redef class ToolContext
27 # Transforms a function annotated with "threaded"
28 var parallelization_phase: Phase = new ParallelizationPhase(self, null)
29 end
30
31 private class ParallelizationPhase
32 super Phase
33
34 redef fun process_annotated_node(nmethdef, nat)
35 do
36 if nat.n_atid.n_id.text != "threaded" then return
37
38 if not nmethdef isa AMethPropdef then
39 toolcontext.error(nat.location, "Syntax Error: only a method can be threaded.")
40 return
41 end
42
43 #TODO: check for self calls
44
45 # Get the module associated with this method
46 var amod = nmethdef.parent.parent
47 assert amod isa AModule
48
49 # Construct the name of the generated class
50 var classname = "Threaded"
51
52 # Try to get the name of the class
53 if nmethdef.parent isa AStdClassdef then
54 classname += nmethdef.parent.as(AStdClassdef).n_id.text
55 end
56
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
60 end
61
62 # Handle methods with a return value
63 var has_rvalue = nmethdef.n_signature.n_type != null
64 var vtype = ""
65 if has_rvalue then
66 vtype = "redef type E: " + nmethdef.n_signature.n_type.n_id.text
67 end
68 # create a return type
69 var n_id = new TClassid
70 n_id.text = classname
71 var n_type = new AType
72 n_type.n_id = n_id
73 nmethdef.n_signature.n_type = n_type
74
75 var params = new Array[String]
76 for param in nmethdef.n_signature.n_params do
77 var typ = param.n_type.n_id.text
78 if param.n_type.n_kwnullable != null then typ = "nullable {typ}"
79 params.add """
80 var {{{param.n_id.text}}}: {{{typ}}}
81 """
82 end
83
84 # String corresponding to the generated class
85 var s="""
86 class {{{classname}}}
87 super Thread
88
89 {{{vtype}}}
90
91 {{{params.join("\n")}}}
92 redef fun main do
93 end
94 end
95 """
96
97 # Parse newly obtained classdef
98 var classdef = toolcontext.parse_classdef(s).as(AStdClassdef)
99
100 # Get the `main` fun of the class
101 var mainfun : nullable AMethPropdef = null
102 for prop in classdef.n_propdefs do
103 if prop isa AMethPropdef then mainfun = prop
104 end
105 assert mainfun != null
106
107 # Make the statements from `main` fun be the statements from the "threaded" fun
108 mainfun.n_block = nmethdef.n_block
109
110 # Add "return null" to the end of the `main` function
111 if not has_rvalue then
112 var s_nullreturn = "return null"
113 var nullreturn = toolcontext.parse_something(s_nullreturn)
114 assert nullreturn isa AExpr
115 mainfun.n_block.as(ABlockExpr).n_expr.add(nullreturn)
116 end
117
118 # Create new body for the annotated fun
119 var s_newbody : String
120 if nmethdef.n_signature.n_params.not_empty then
121 var init_params = new Array[String]
122 for param in nmethdef.n_signature.n_params do
123 init_params.add(param.n_id.text)
124 end
125 s_newbody ="""
126 var thread = new {{{classname}}}({{{init_params.join(",")}}})
127 thread.start
128 return thread
129 """
130 else
131 s_newbody = """
132 var thread = new {{{classname}}}
133 thread.start
134 return thread
135 """
136 end
137
138 var newbody = toolcontext.parse_something(s_newbody)
139 nmethdef.n_block = newbody.as(ABlockExpr)
140
141 # Add the new class to the module
142 amod.n_classdefs.add(classdef)
143 end
144 end