src: improve messages (and sometime location) of errors and warnings
[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 if nmethdef.n_signature.n_type != null then
46 toolcontext.error(nat.location, "Error: functions not supported yet.")
47 return
48 end
49
50 # Get the module associated with this method
51 var amod = nmethdef.parent.parent
52 assert amod isa AModule
53
54 # Construct the name of the generated class
55 var modulename = amod.n_moduledecl.n_name.n_id.text
56 var classname = "Threaded" + modulename
57
58 # Try to get the name of the class
59 if nmethdef.parent isa AStdClassdef then
60 classname += nmethdef.parent.as(AStdClassdef).n_id.text
61 end
62
63 # Try to get the name of the method
64 if nmethdef.n_methid isa AIdMethid then
65 classname += nmethdef.n_methid.as(AIdMethid).n_id.text
66 end
67
68
69 var params = new Array[String]
70 # case if the method has parameters
71 if nmethdef.n_signature.n_params.not_empty then
72 for param in nmethdef.n_signature.n_params do
73 params.add("""
74 var {{{param.n_id.text}}} : {{{param.n_type.n_id.text}}}
75
76 """)
77 end
78 end
79
80 # String corresponding to the generated class
81 var s="""
82 class {{{classname}}}
83 super Thread
84
85 {{{params.join("\n")}}}
86 redef fun main do
87 end
88 end
89 """
90
91 # Parse newly obtained classdef
92 var classdef = toolcontext.parse_classdef(s).as(AStdClassdef)
93
94 # Get the `main` fun of the class
95 var mainfun : nullable AMethPropdef = null
96 for prop in classdef.n_propdefs do
97 if prop isa AMethPropdef then mainfun = prop
98 end
99 assert mainfun != null
100
101 # Make the statements from `main` fun be the statements from the "threaded" fun
102 mainfun.n_block = nmethdef.n_block
103
104 # Add "return null" to the end of the `main` function
105 var s_nullreturn = "return null"
106 var nullreturn = toolcontext.parse_something(s_nullreturn)
107 assert nullreturn isa AExpr
108 mainfun.n_block.as(ABlockExpr).n_expr.add(nullreturn)
109
110
111 # Create new body for the annotated fun
112 var s_newbody : String
113 if nmethdef.n_signature.n_params.not_empty then
114 var init_params = new Array[String]
115 for param in nmethdef.n_signature.n_params do
116 init_params.add(param.n_id.text)
117 end
118 s_newbody ="""
119 var thread = new {{{classname}}}({{{init_params.join(",")}}})
120 thread.start
121 """
122 else
123 s_newbody = """
124 var thread = new {{{classname}}}
125 thread.start
126 """
127 end
128
129 var newbody = toolcontext.parse_something(s_newbody)
130 nmethdef.n_block = newbody.as(ABlockExpr)
131
132 # Add the new class to the module
133 amod.n_classdefs.add(classdef)
134 end
135 end