Merge: Sys is top
[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(nmethdef.location, "Syntax error: only a method can be threaded.")
40 return
41 end
42 if nmethdef.n_signature.n_params.length != 0 then
43 toolcontext.error(nmethdef.location, "Syntax error: parametrized method not supported yet.")
44 return
45 end
46 if nmethdef.n_signature.n_type != null then
47 toolcontext.error(nmethdef.location, "Syntax error: method with a return value not supported yet.")
48 return
49 end
50
51 # Get the module associated with this method
52 var amod = nmethdef.parent.parent
53 assert amod isa AModule
54
55 # Construct the name of the generated class
56 var modulename = amod.n_moduledecl.n_name.n_id.text
57 var classname = "Threaded" + modulename
58
59 # Try to get the name of the class
60 if nmethdef.parent isa AStdClassdef then
61 classname += nmethdef.parent.as(AStdClassdef).n_id.text
62 end
63
64 # Try to get the name of the method
65 if nmethdef.n_methid isa AIdMethid then
66 classname += nmethdef.n_methid.as(AIdMethid).n_id.text
67 end
68
69 # Create a string corresponding to the threaded class
70 var s ="""
71 class {{{classname}}}
72 super Thread
73
74 redef fun main do
75 end
76 end
77 """
78
79 # Parse newly obtained classdef
80 var classdef = toolcontext.parse_classdef(s).as(AStdClassdef)
81
82 # Get the `main` fun of the class
83 var mainfun : nullable AMethPropdef = null
84 for prop in classdef.n_propdefs do
85 if prop isa AMethPropdef then mainfun = prop
86 end
87 assert mainfun != null
88
89 # Make the statements from `main` fun be the statements from the "threaded" fun
90 mainfun.n_block = nmethdef.n_block
91
92 # Add "return null" to the end of the `main` function
93 var s_nullreturn = "return null"
94 var nullreturn = toolcontext.parse_something(s_nullreturn)
95 assert nullreturn isa AExpr
96 mainfun.n_block.as(ABlockExpr).n_expr.add(nullreturn)
97
98 # Create new body for the annotated fun
99 var s_newbody ="""
100 var thread = new {{{classname}}}
101 thread.start
102 """
103
104 var newbody = toolcontext.parse_something(s_newbody)
105 nmethdef.n_block = newbody.as(ABlockExpr)
106
107 # Add the new class to the module
108 amod.n_classdefs.add(classdef)
109 end
110 end