080efe67e83b923bd53925e30a53d6867c6aa43e
[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 private import astvalidation
26
27 redef class ToolContext
28 # Transforms a function annotated with "threaded"
29 var parallelization_phase: Phase = new ParallelizationPhase(self, null)
30 end
31
32 private class ParallelizationPhase
33 super Phase
34
35 redef fun process_annotated_node(nmethdef, nat)
36 do
37 if nat.n_atid.n_id.text != "threaded" then return
38
39 if not nmethdef isa AMethPropdef then
40 toolcontext.error(nat.location, "Syntax Error: only a method can be threaded.")
41 return
42 end
43
44 #TODO: check for self calls
45
46 # Get the module associated with this method
47 var amod = nmethdef.parent.parent
48 assert amod isa AModule
49
50 # Construct the name of the generated class
51 var classname = "Threaded"
52
53 # Try to get the name of the class
54 if nmethdef.parent isa AStdClassdef then
55 classname += nmethdef.parent.as(AStdClassdef).n_qid.n_id.text
56 end
57
58 # Try to get the name of the method
59 if nmethdef.n_methid isa AIdMethid then
60 classname += nmethdef.n_methid.as(AIdMethid).n_id.text
61 end
62
63 # Handle methods with a return value
64 var has_rvalue = nmethdef.n_signature.n_type != null
65 var vtype = ""
66 if has_rvalue then
67 vtype = "redef type E: " + nmethdef.n_signature.n_type.n_qid.n_id.text
68 end
69
70 # create a return type
71 var n_id = new TClassid
72 n_id.text = classname
73 var n_qid = new AQclassid
74 n_qid.n_id = n_id
75 var n_type = new AType
76 n_type.n_qid = n_qid
77 nmethdef.n_signature.n_type = n_type
78
79 var params = new Array[String]
80 for param in nmethdef.n_signature.n_params do
81 var typ = param.n_type.n_qid.n_id.text
82 if param.n_type.n_kwnullable != null then typ = "nullable {typ}"
83 params.add """
84 var {{{param.n_id.text}}}: {{{typ}}}
85 """
86 end
87
88 # String corresponding to the generated class
89 var classdef_source = """
90 class {{{classname}}}
91 super Thread
92
93 {{{vtype}}}
94
95 {{{params.join("\n")}}}
96 redef fun main do
97 end
98 end
99 """
100
101 # Parse newly obtained classdef
102 var classdef = toolcontext.parse_classdef(classdef_source)
103 assert classdef isa AStdClassdef
104
105 # Get the `main` fun of the class
106 var mainfun: nullable AMethPropdef = null
107 for prop in classdef.n_propdefs do
108 if prop isa AMethPropdef then mainfun = prop
109 end
110 assert mainfun != null
111
112 # Make the statements from `main` fun be the statements from the "threaded" fun
113 mainfun.n_block = nmethdef.n_block
114
115 # Add "return null" to the end of the `main` function
116 if not has_rvalue then
117 var s_nullreturn = "return null"
118 var nullreturn = toolcontext.parse_something(s_nullreturn)
119 assert nullreturn isa AExpr
120 mainfun.n_block.as(ABlockExpr).n_expr.add(nullreturn)
121 end
122
123 # Create new body for the annotated fun
124 var s_newbody : String
125 if nmethdef.n_signature.n_params.not_empty then
126 var init_params = new Array[String]
127 for param in nmethdef.n_signature.n_params do
128 init_params.add(param.n_id.text)
129 end
130 s_newbody ="""
131 var thread = new {{{classname}}}({{{init_params.join(",")}}})
132 thread.start
133 return thread
134 """
135 else
136 s_newbody = """
137 var thread = new {{{classname}}}
138 thread.start
139 return thread
140 """
141 end
142
143 var newbody = toolcontext.parse_something(s_newbody)
144 nmethdef.n_block = newbody.as(ABlockExpr)
145
146 nmethdef.validate
147
148 # Add the new class to the module
149 amod.n_classdefs.add(classdef)
150 classdef.validate
151 end
152 end