1 /***************************************************************************
2 gdlexception.cpp - exception handling
3 -------------------
4 begin : July 22 2002
5 copyright : (C) 2002 by Marc Schellens
6 email : m_schellens@users.sf.net
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include "includefirst.hpp"
19
20 #include "gdlexception.hpp"
21 #include "dnode.hpp"
22 #include "initsysvar.hpp"
23 #include "gdljournal.hpp"
24 #include "dinterpreter.hpp"
25
26 //#define GDL_DEBUG
27 //#undef GDL_DEBUG
28
29 using namespace std;
30
31 DInterpreter* GDLException::interpreter = NULL;
32
Name(BaseGDL * b)33 string GDLException::Name( BaseGDL* b)
34 {
35 if(interpreter!=NULL && interpreter->CallStack().size()>0)
36 return interpreter->Name(b);
37 return "";
38 }
39
GDLException(DLong eC,const string & s,bool pre,bool decorate)40 GDLException::GDLException(DLong eC, const string& s, bool pre, bool decorate):
41 ANTLRException(s),
42 errorNode(static_cast<RefDNode>(antlr::nullAST)),
43 errorNodeP( NULL),
44 errorCode(eC),
45 line( 0), col( 0), prefix( pre),
46 arrayexprIndexeeFailed(false),
47 ioException( false),
48 targetEnv( NULL)
49 {
50 if(decorate && interpreter!=NULL && interpreter->CallStack().size()>0)
51 {
52 EnvBaseT* e = interpreter->CallStack().back();
53 errorNodeP = e->CallingNode();
54 msg = e->GetProName();
55 if( msg != "$MAIN$") msg += ": "+ s; else msg = s;
56 }
57 else
58 {
59 msg = s;
60 }
61 // note: This is for cases, when form a destructor is thrown
62 // in these cases, program aborts
63 #ifdef GDL_DEBUG
64 cerr << s << endl;
65 #endif
66 }
GDLException(const string & s,bool pre,bool decorate)67 GDLException::GDLException(const string& s, bool pre, bool decorate):
68 ANTLRException(s),
69 errorNode(static_cast<RefDNode>(antlr::nullAST)),
70 errorNodeP( NULL),
71 errorCode(-1),
72 line( 0), col( 0), prefix( pre),
73 ioException( false),
74 targetEnv( NULL)
75 {
76 if(decorate && interpreter!=NULL && interpreter->CallStack().size()>0)
77 {
78 EnvBaseT* e = interpreter->CallStack().back();
79 errorNodeP = e->CallingNode();
80 msg = e->GetProName();
81 if( msg != "$MAIN$") msg += ": "+ s; else msg = s;
82 }
83 else
84 {
85 msg = s;
86 }
87 // note: This is for cases, when form a destructor is thrown
88 // in these cases, program aborts
89 #ifdef GDL_DEBUG
90 cerr << s << endl;
91 #endif
92 }
93
GDLException(const RefDNode eN,const string & s)94 GDLException::GDLException(const RefDNode eN, const string& s):
95 ANTLRException(s),
96 errorNode(eN),
97 errorNodeP( NULL),
98 errorCode(-1),
99 line( 0), col( 0), prefix( true),
100 arrayexprIndexeeFailed(false),
101 ioException( false),
102 targetEnv( NULL)
103 {
104 if(interpreter!=NULL && interpreter->CallStack().size()>0)
105 {
106 EnvBaseT* e = interpreter->CallStack().back();
107 errorNodeP = e->CallingNode();
108 msg = e->GetProName();
109 if( msg != "$MAIN$") msg += ": "+ s; else msg = s;
110 }
111 else
112 {
113 msg = s;
114 }
115 #ifdef GDL_DEBUG
116 cerr << s << endl;
117 #endif
118 }
GDLException(DLong eC,const RefDNode eN,const string & s)119 GDLException::GDLException(DLong eC, const RefDNode eN, const string& s):
120 ANTLRException(s),
121 errorNode(eN),
122 errorNodeP( NULL),
123 errorCode(eC),
124 line( 0), col( 0), prefix( true),
125 arrayexprIndexeeFailed(false),
126 ioException( false),
127 targetEnv( NULL)
128 {
129 if(interpreter!=NULL && interpreter->CallStack().size()>0)
130 {
131 EnvBaseT* e = interpreter->CallStack().back();
132 errorNodeP = e->CallingNode();
133 msg = e->GetProName();
134 if( msg != "$MAIN$") msg += ": "+ s; else msg = s;
135 }
136 else
137 {
138 msg = s;
139 }
140 #ifdef GDL_DEBUG
141 cerr << s << endl;
142 #endif
143 }
144
GDLException(const ProgNodeP eN,const string & s,bool decorate,bool overWriteNode)145 GDLException::GDLException(const ProgNodeP eN, const string& s, bool decorate, bool overWriteNode):
146 ANTLRException(s),
147 errorNode(static_cast<RefDNode>(antlr::nullAST)),
148 errorNodeP( eN),
149 errorCode(-1),
150 line( 0), col( 0), prefix( true),
151 arrayexprIndexeeFailed(false),
152 ioException( false),
153 targetEnv( NULL)
154 {
155 if( overWriteNode && interpreter!=NULL && interpreter->CallStack().size()>0)
156 {
157 EnvBaseT* e = interpreter->CallStack().back();
158 errorNodeP = e->CallingNode();
159 }
160 if( decorate && interpreter!=NULL && interpreter->CallStack().size()>0)
161 {
162 EnvBaseT* e = interpreter->CallStack().back();
163 msg = e->GetProName();
164 if( msg != "$MAIN$") msg += ": "+ s; else msg = s;
165 }
166 else
167 {
168 msg = s;
169 }
170 #ifdef GDL_DEBUG
171 cerr << s << endl;
172 #endif
173 }
GDLException(DLong eC,const ProgNodeP eN,const string & s,bool decorate,bool overWriteNode)174 GDLException::GDLException(DLong eC, const ProgNodeP eN, const string& s, bool decorate, bool overWriteNode):
175 ANTLRException(s),
176 errorNode(static_cast<RefDNode>(antlr::nullAST)),
177 errorNodeP( eN),
178 errorCode(eC),
179 line( 0), col( 0), prefix( true),
180 arrayexprIndexeeFailed(false),
181 ioException( false),
182 targetEnv( NULL)
183 {
184 if( overWriteNode && interpreter!=NULL && interpreter->CallStack().size()>0)
185 {
186 EnvBaseT* e = interpreter->CallStack().back();
187 errorNodeP = e->CallingNode();
188 }
189 if( decorate && interpreter!=NULL && interpreter->CallStack().size()>0)
190 {
191 EnvBaseT* e = interpreter->CallStack().back();
192 msg = e->GetProName();
193 if( msg != "$MAIN$") msg += ": "+ s; else msg = s;
194 }
195 else
196 {
197 msg = s;
198 }
199 #ifdef GDL_DEBUG
200 cerr << s << endl;
201 #endif
202 }
203
GDLException(SizeT l,SizeT c,const string & s)204 GDLException::GDLException(SizeT l, SizeT c, const string& s):
205 ANTLRException(s),
206 errorNode(static_cast<RefDNode>(antlr::nullAST)),
207 errorNodeP( NULL),
208 errorCode(-1),
209 line( l), col( c), prefix( true),
210 arrayexprIndexeeFailed(false),
211 ioException( false),
212 targetEnv( NULL)
213 {
214 if(interpreter!=NULL && interpreter->CallStack().size()>0)
215 {
216 EnvBaseT* e = interpreter->CallStack().back();
217 errorNodeP = e->CallingNode();
218 msg = e->GetProName();
219 if( msg != "$MAIN$") msg += ": "+ s; else msg = s;
220 }
221 else
222 {
223 msg = s;
224 }
225 #ifdef GDL_DEBUG
226 cerr << s << endl;
227 #endif
228 }
GDLException(DLong eC,SizeT l,SizeT c,const string & s)229 GDLException::GDLException(DLong eC, SizeT l, SizeT c, const string& s):
230 ANTLRException(s),
231 errorNode(static_cast<RefDNode>(antlr::nullAST)),
232 errorNodeP( NULL),
233 errorCode(eC),
234 line( l), col( c), prefix( true),
235 arrayexprIndexeeFailed(false),
236 targetEnv( NULL)
237 {
238 if(interpreter!=NULL && interpreter->CallStack().size()>0)
239 {
240 EnvBaseT* e = interpreter->CallStack().back();
241 errorNodeP = e->CallingNode();
242 msg = e->GetProName();
243 if( msg != "$MAIN$") msg += ": "+ s; else msg = s;
244 }
245 else
246 {
247 msg = s;
248 }
249 #ifdef GDL_DEBUG
250 cerr << s << endl;
251 #endif
252 }
253
Message(const string & s)254 void Message(const string& s)
255 {
256 if( SysVar::Quiet() == 0)
257 {
258 cerr << SysVar::MsgPrefix() << s << endl;
259 lib::write_journal_comment( SysVar::MsgPrefix() + s);
260 }
261 }
262
Warning(const std::string & s)263 void Warning(const std::string& s)
264 {
265 cerr << SysVar::MsgPrefix() << s << endl;
266 lib::write_journal_comment( SysVar::MsgPrefix() + s);
267 }
268
ThrowGDLException(const std::string & str)269 void ThrowGDLException( const std::string& str)
270 {
271 throw GDLException( str);
272 }
273
WarnAboutObsoleteRoutine(const string & name)274 void WarnAboutObsoleteRoutine(const string& name)
275 {
276 // no static here due to .RESET_SESSION
277 DStructGDL* warnStruct = SysVar::Warn();
278 // this static is ok as it will evaluate always to the same value
279 static unsigned obs_routinesTag = warnStruct->Desc()->TagIndex( "OBS_ROUTINES");
280 if (((static_cast<DByteGDL*>( warnStruct->GetTag(obs_routinesTag, 0)))[0]).LogTrue())
281 Message("Routine compiled from an obsolete library: " + name);
282 // TODO: journal / !QUIET??
283 }
284
WarnAboutObsoleteRoutine(const RefDNode eN,const string & name)285 void WarnAboutObsoleteRoutine(const RefDNode eN, const string& name)
286 {
287 // TODO: journal?
288 // no static here due to .RESET_SESSION
289 DStructGDL* warnStruct = SysVar::Warn();
290 // this static is ok as it will evaluate always to the same value
291 static unsigned obs_routinesTag = warnStruct->Desc()->TagIndex( "OBS_ROUTINES");
292 if (((static_cast<DByteGDL*>( warnStruct->GetTag(obs_routinesTag, 0)))[0]).LogTrue())
293 {
294 GDLException* e = new GDLException(eN,
295 "Routine compiled from an obsolete library: " + name
296 );
297 Guard<GDLException> eGuard(e);
298 GDLInterpreter::ReportCompileError(*e, "");
299 // TODO: file
300 }
301 }
302