src/parellalization_phase: support nullable parameters in threaded methods
[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 modulename = amod.n_moduledecl.n_name.n_id.text
51 var classname = "Threaded" + modulename
52
53 # Try to get the name of the class
54 if nmethdef.parent isa AStdClassdef then
55 classname += nmethdef.parent.as(AStdClassdef).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_id.text
68 end
69 # create a return type
70 var n_id = new TClassid
71 n_id.text = classname
72 var n_type = new AType
73 n_type.n_id = n_id
74 nmethdef.n_signature.n_type = n_type
75
76 var params = new Array[String]
77 for param in nmethdef.n_signature.n_params do
78 var typ = param.n_type.n_id.text
79 if param.n_type.n_kwnullable != null then typ = "nullable {typ}"
80 params.add """
81 var {{{param.n_id.text}}}: {{{typ}}}
82 """
83 end
84
85 # String corresponding to the generated class
86 var s="""
87 class {{{classname}}}
88 super Thread
89
90 {{{vtype}}}
91
92 {{{params.join("\n")}}}
93 redef fun main do
94 end
95 end
96 """
97
98 # Parse newly obtained classdef
99 var classdef = toolcontext.parse_classdef(s).as(AStdClassdef)
100
101 # Get the `main` fun of the class
102 var mainfun : nullable AMethPropdef = null
103 for prop in classdef.n_propdefs do
104 if prop isa AMethPropdef then mainfun = prop
105 end
106 assert mainfun != null
107
108 # Make the statements from `main` fun be the statements from the "threaded" fun
109 mainfun.n_block = nmethdef.n_block
110
111 # Add "return null" to the end of the `main` function
112 if not has_rvalue then
113 var s_nullreturn = "return null"
114 var nullreturn = toolcontext.parse_something(s_nullreturn)
115 assert nullreturn isa AExpr
116 mainfun.n_block.as(ABlockExpr).n_expr.add(nullreturn)
117 end
118
119 # Create new body for the annotated fun
120 var s_newbody : String
121 if nmethdef.n_signature.n_params.not_empty then
122 var init_params = new Array[String]
123 for param in nmethdef.n_signature.n_params do
124 init_params.add(param.n_id.text)
125 end
126 s_newbody ="""
127 var thread = new {{{classname}}}({{{init_params.join(",")}}})
128 thread.start
129 return thread
130 """
131 else
132 s_newbody = """
133 var thread = new {{{classname}}}
134 thread.start
135 return thread
136 """
137 end
138
139 var newbody = toolcontext.parse_something(s_newbody)
140 nmethdef.n_block = newbody.as(ABlockExpr)
141
142 # Add the new class to the module
143 amod.n_classdefs.add(classdef)
144 end
145 end