1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <precomp.h>
29 #include "pe_funct.hxx"
30 
31 
32 // NOT FULLY DEFINED SERVICES
33 #include <cosv/tpl/tpltools.hxx>
34 #include <ary/cpp/c_gate.hxx>
35 #include <ary/cpp/c_funct.hxx>
36 #include <ary/cpp/c_type.hxx>
37 #include <ary/cpp/cp_ce.hxx>
38 #include <ary/cpp/cp_type.hxx>
39 #include <ary/cpp/inpcontx.hxx>
40 #include "pe_type.hxx"
41 #include "pe_param.hxx"
42 
43 
44 
45 
46 namespace cpp
47 {
48 
49 
50 inline void
51 PE_Function::PerformFinishingPunctuation()
52 {
53     SetTokenResult(not_done,pop_success);
54 }
55 
56 
57 PE_Function::PE_Function( Cpp_PE * i_pParent )
58 	:   Cpp_PE(i_pParent),
59 		pStati( new PeStatusArray<PE_Function> ),
60 		// pSpParameter,
61 		// pSpuParameter,
62         // pSpType,
63         // pSpuException,
64         // pSpuCastOperatorType,
65 		nResult(0),
66         bResult_WithImplementation(false),
67         aName(60),
68         eVirtuality(ary::cpp::VIRTUAL_none),
69         eConVol(ary::cpp::CONVOL_none),
70         // aFlags,
71         nReturnType(0),
72         // aParameters
73         // aExceptions,
74         bThrow(false),
75         nBracketCounterInImplementation(0)
76 {
77 	Setup_StatusFunctions();
78 
79 	pSpParameter     = new SP_Parameter(*this);
80 	pSpType          = new SP_Type(*this);
81 
82 	pSpuParameter	        = new SPU_Parameter(*pSpParameter, 0, &PE_Function::SpReturn_Parameter);
83 	pSpuException	        = new SPU_Type(*pSpType, 0, &PE_Function::SpReturn_Exception);
84 	pSpuCastOperatorType	= new SPU_Type(*pSpType, &PE_Function::SpInit_CastOperatorType, &PE_Function::SpReturn_CastOperatorType);
85 }
86 
87 PE_Function::~PE_Function()
88 {
89 
90 }
91 
92 void
93 PE_Function::Init_Std( const String  &          i_sName,
94                        ary::cpp::Type_id        i_nReturnType,
95 	                   bool                     i_bVirtual,
96                        ary::cpp::FunctionFlags  i_aFlags )
97 {
98     aName << i_sName;
99     eVirtuality = i_bVirtual ? ary::cpp::VIRTUAL_virtual : ary::cpp::VIRTUAL_none;
100     aFlags = i_aFlags;
101     nReturnType = i_nReturnType;
102     pStati->SetCur(afterName);
103 }
104 
105 void
106 PE_Function::Init_Ctor( const String  &         i_sName,
107                         ary::cpp::FunctionFlags i_aFlags )
108 {
109     aName << i_sName;
110     eVirtuality = ary::cpp::VIRTUAL_none;
111     aFlags = i_aFlags;
112     nReturnType = 0;
113     pStati->SetCur(afterName);
114 }
115 
116 void
117 PE_Function::Init_Dtor( const String  &         i_sName,
118                         bool                    i_bVirtual,
119                         ary::cpp::FunctionFlags i_aFlags )
120 {
121     aName << "~" << i_sName;
122     eVirtuality = i_bVirtual ? ary::cpp::VIRTUAL_virtual : ary::cpp::VIRTUAL_none;
123     aFlags = i_aFlags;
124     nReturnType = 0;
125     pStati->SetCur(afterName);
126 }
127 
128 void
129 PE_Function::Init_CastOperator( bool                    i_bVirtual,
130                                 ary::cpp::FunctionFlags i_aFlags )
131 {
132     aName << "operator ";
133     eVirtuality = i_bVirtual ? ary::cpp::VIRTUAL_virtual : ary::cpp::VIRTUAL_none;
134     aFlags = i_aFlags;
135     nReturnType = 0;
136     pStati->SetCur(afterCastOperator);
137 }
138 
139 void
140 PE_Function::Init_NormalOperator( ary::cpp::Type_id                  i_nReturnType,
141 	                              bool                      i_bVirtual,
142                                   ary::cpp::FunctionFlags   i_aFlags )
143 {
144     aName << "operator";
145     eVirtuality = i_bVirtual ? ary::cpp::VIRTUAL_virtual : ary::cpp::VIRTUAL_none;
146     aFlags = i_aFlags;
147     nReturnType = i_nReturnType;
148     pStati->SetCur(afterStdOperator);
149 }
150 
151 ary::cpp::Ce_id
152 PE_Function::Result_Id() const
153 {
154 	return nResult;
155 }
156 
157 void
158 PE_Function::Call_Handler( const cpp::Token & i_rTok )
159 {
160 	pStati->Cur().Call_Handler(i_rTok.TypeId(), i_rTok.Text());
161 }
162 
163 void
164 PE_Function::Setup_StatusFunctions()
165 {
166 	typedef CallFunction<PE_Function>::F_Tok	F_Tok;
167 
168 	static F_Tok stateF_afterStdOperator[] =
169                                             { &PE_Function::On_afterOperator_Std_Operator,
170 											  &PE_Function::On_afterOperator_Std_LeftBracket,
171 											  &PE_Function::On_afterOperator_Std_LeftBracket,
172 											  &PE_Function::On_afterOperator_Std_Operator,
173 											  &PE_Function::On_afterOperator_Std_Operator,
174 											  &PE_Function::On_afterOperator_Std_Operator,
175 											  &PE_Function::On_afterOperator_Std_Operator,
176 											  &PE_Function::On_afterOperator_Std_Operator,
177 											  &PE_Function::On_afterOperator_Std_Operator };
178 	static INT16 stateT_afterStdOperator[] =
179                                             { Tid_Operator,
180 											  Tid_ArrayBracket_Left,
181 											  Tid_Bracket_Left,
182 											  Tid_Comma,
183 											  Tid_Assign,
184 											  Tid_Less,
185 											  Tid_Greater,
186 											  Tid_Asterix,
187 											  Tid_AmpersAnd };
188 
189 	static F_Tok stateF_afterStdOperatorLeftBracket[] =
190                                             { &PE_Function::On_afterStdOperatorLeftBracket_RightBracket,
191                                               &PE_Function::On_afterStdOperatorLeftBracket_RightBracket };
192 	static INT16 stateT_afterStdOperatorLeftBracket[] =
193                                             { Tid_ArrayBracket_Right,
194 											  Tid_Bracket_Right };
195 
196 	static F_Tok stateF_afterCastOperator[] =
197                                             { &PE_Function::On_afterOperator_Cast_Type,
198 											  &PE_Function::On_afterOperator_Cast_Type,
199 											  &PE_Function::On_afterOperator_Cast_Type,
200 											  &PE_Function::On_afterOperator_Cast_Type,
201 											  &PE_Function::On_afterOperator_Cast_Type,
202 											  &PE_Function::On_afterOperator_Cast_Type,
203 											  &PE_Function::On_afterOperator_Cast_Type,
204 											  &PE_Function::On_afterOperator_Cast_Type,
205 											  &PE_Function::On_afterOperator_Cast_Type,
206 											  &PE_Function::On_afterOperator_Cast_Type,
207 											  &PE_Function::On_afterOperator_Cast_Type };
208 	static INT16 stateT_afterCastOperator[] =
209                                             { Tid_Identifier,
210 											  Tid_class,
211 											  Tid_struct,
212 											  Tid_union,
213 											  Tid_enum,
214 											  Tid_const,
215 											  Tid_volatile,
216 											  Tid_DoubleColon,
217                                               Tid_typename,
218 											  Tid_BuiltInType,
219 											  Tid_TypeSpecializer };
220 
221 	static F_Tok stateF_afterName[] =		{ &PE_Function::On_afterName_Bracket_Left };
222 	static INT16 stateT_afterName[] =     	{ Tid_Bracket_Left };
223 
224 	static F_Tok stateF_expectParameterSeparator[] =
225 											{ &PE_Function::On_expectParameterSeparator_BracketRight,
226 											  &PE_Function::On_expectParameterSeparator_Comma };
227 	static INT16 stateT_expectParameterSeparator[] =
228 											{ Tid_Bracket_Right,
229 											  Tid_Comma };
230 
231 	static F_Tok stateF_afterParameters[] =	{ &PE_Function::On_afterParameters_const,
232 											  &PE_Function::On_afterParameters_volatile,
233 											  &PE_Function::On_afterParameters_throw,
234 											  &PE_Function::On_afterParameters_SwBracket_Left,
235 											  &PE_Function::On_afterParameters_Semicolon,
236 											  &PE_Function::On_afterParameters_Comma,
237 											  &PE_Function::On_afterParameters_Colon,
238 											  &PE_Function::On_afterParameters_Assign };
239 	static INT16 stateT_afterParameters[] = { Tid_const,
240 											  Tid_volatile,
241 											  Tid_throw,
242                                               Tid_SwBracket_Left,
243 											  Tid_Semicolon,
244 											  Tid_Comma,
245                                               Tid_Colon,
246 											  Tid_Assign };
247 
248 	static F_Tok stateF_afterThrow[] =		{ &PE_Function::On_afterThrow_Bracket_Left };
249 	static INT16 stateT_afterThrow[] =     	{ Tid_Bracket_Left };
250 
251 	static F_Tok stateF_expectExceptionSeparator[] =
252 											{ &PE_Function::On_expectExceptionSeparator_BracketRight,
253 											  &PE_Function::On_expectExceptionSeparator_Comma };
254 	static INT16 stateT_expectExceptionSeparator[] =
255 											{ Tid_Bracket_Right,
256 											  Tid_Comma };
257 
258 	static F_Tok stateF_afterExceptions[] =	{ &PE_Function::On_afterExceptions_SwBracket_Left,
259                                               &PE_Function::On_afterExceptions_Semicolon,
260 											  &PE_Function::On_afterExceptions_Comma,
261 											  &PE_Function::On_afterExceptions_Colon,
262 											  &PE_Function::On_afterExceptions_Assign };
263 	static INT16 stateT_afterExceptions[] = { Tid_SwBracket_Left,
264                                               Tid_Semicolon,
265 											  Tid_Comma,
266 											  Tid_Colon,
267 											  Tid_Assign };
268 
269 	static F_Tok stateF_expectZero[] =		{ &PE_Function::On_expectZero_Constant };
270 	static INT16 stateT_expectZero[] =     	{ Tid_Constant };
271 
272 	static F_Tok stateF_inImplementation[] =
273                                             { &PE_Function::On_inImplementation_SwBracket_Left,
274                                               &PE_Function::On_inImplementation_SwBracket_Right };
275 	static INT16 stateT_inImplementation[] =
276                                             { Tid_SwBracket_Left,
277                                               Tid_SwBracket_Right };
278 
279 	SEMPARSE_CREATE_STATUS(PE_Function, afterStdOperator, Hdl_SyntaxError);
280 	SEMPARSE_CREATE_STATUS(PE_Function, afterStdOperatorLeftBracket, Hdl_SyntaxError);
281 	SEMPARSE_CREATE_STATUS(PE_Function, afterCastOperator, Hdl_SyntaxError);
282 	SEMPARSE_CREATE_STATUS(PE_Function, afterName, Hdl_SyntaxError);
283 	SEMPARSE_CREATE_STATUS(PE_Function, expectParameterSeparator, Hdl_SyntaxError);
284 	SEMPARSE_CREATE_STATUS(PE_Function, afterParameters, Hdl_SyntaxError);
285 	SEMPARSE_CREATE_STATUS(PE_Function, afterThrow, Hdl_SyntaxError);
286 	SEMPARSE_CREATE_STATUS(PE_Function, expectExceptionSeparator, Hdl_SyntaxError);
287 	SEMPARSE_CREATE_STATUS(PE_Function, afterExceptions, Hdl_SyntaxError);
288 	SEMPARSE_CREATE_STATUS(PE_Function, expectZero, Hdl_SyntaxError);
289 	SEMPARSE_CREATE_STATUS(PE_Function, inImplementation, On_inImplementation_Default );
290 }
291 
292 void
293 PE_Function::InitData()
294 {
295     pStati->SetCur( afterName ),
296 	nResult = 0;
297     bResult_WithImplementation = false;
298     aName.seekp(0);
299     eVirtuality = ary::cpp::VIRTUAL_none;
300     eConVol = ary::cpp::CONVOL_none;
301     aFlags.Reset();
302     nReturnType = 0;
303     csv::erase_container(aParameters);
304     csv::erase_container(aExceptions);
305     bThrow = false;
306 }
307 
308 void
309 PE_Function::TransferData()
310 {
311     String  sName( aName.c_str() );
312     ary::cpp::Function *
313         pFunction = Env().AryGate().Ces().Store_Operation(
314                                                 Env().Context(),
315                                                 sName,
316                                                 nReturnType,
317                                                 aParameters,
318                                                 eVirtuality,
319                                                 eConVol,
320                                                 aFlags,
321                                                 bThrow,
322                                                 aExceptions );
323     if (pFunction != 0)
324     {
325         // KORR_FUTURE: How to handle differing documentation?
326 
327         Dyn< StringVector >
328                 pTplParams ( Env().Get_CurTemplateParameters() );
329         if ( pTplParams )
330         {
331           	for ( StringVector::const_iterator it = pTplParams->begin();
332     	    	  it !=  pTplParams->end();
333     		      ++it )
334         	{
335     	    	pFunction->Add_TemplateParameterType( *it, ary::cpp::Type_id(0) );
336         	}  // end for
337         }
338 
339         Env().Event_Store_Function(*pFunction);
340     }
341 
342     pStati->SetCur(size_of_states);
343 }
344 
345 void
346 PE_Function::Hdl_SyntaxError(const char * i_sText)
347 {
348     StdHandlingOfSyntaxError(i_sText);
349 }
350 
351 void
352 PE_Function::SpInit_CastOperatorType()
353 {
354     pSpuCastOperatorType->Child().Init_AsCastOperatorType();
355 }
356 
357 void
358 PE_Function::SpReturn_Parameter()
359 {
360     pStati->SetCur(expectParameterSeparator);
361 
362 	ary::cpp::Type_id nParamType = pSpuParameter->Child().Result_FrontType();
363 	if ( nParamType.IsValid() )      // Check, if there was a parameter, or only the closing ')'.
364 	{
365 	    aParameters.push_back( pSpuParameter->Child().Result_ParamInfo() );
366 	}
367 }
368 
369 void
370 PE_Function::SpReturn_Exception()
371 {
372     pStati->SetCur(expectExceptionSeparator);
373 
374     ary::cpp::Type_id
375         nException = pSpuException->Child().Result_Type().TypeId();
376     if ( nException.IsValid() AND pSpuException->Child().Result_KindOf() == PE_Type::is_type )
377     {
378         aExceptions.push_back( nException );
379     }
380 }
381 
382 void
383 PE_Function::SpReturn_CastOperatorType()
384 {
385     pStati->SetCur(afterName);
386 
387     Env().AryGate().Types().Get_TypeText(
388             aName, pSpuCastOperatorType->Child().Result_Type().TypeId() );
389 }
390 
391 void
392 PE_Function::On_afterOperator_Std_Operator(const char * i_sText)
393 {
394     SetTokenResult(done,stay);
395     pStati->SetCur(afterName);
396 
397     if ( 'a' <= *i_sText AND *i_sText <= 'z' )
398         aName << ' ';
399     aName << i_sText;
400 }
401 
402 void
403 PE_Function::On_afterOperator_Std_LeftBracket(const char * i_sText)
404 {
405     SetTokenResult(done,stay);
406     pStati->SetCur(afterStdOperatorLeftBracket);
407 
408     aName << i_sText;
409 }
410 
411 void
412 PE_Function::On_afterStdOperatorLeftBracket_RightBracket(const char * i_sText)
413 {
414     SetTokenResult(done,stay);
415     pStati->SetCur(afterName);
416 
417     aName << i_sText;
418 }
419 
420 void
421 PE_Function::On_afterOperator_Cast_Type(const char *)
422 {
423     pSpuCastOperatorType->Push(not_done);
424 }
425 
426 void
427 PE_Function::On_afterName_Bracket_Left(const char *)
428 {
429     pSpuParameter->Push(done);
430 }
431 
432 void
433 PE_Function::On_expectParameterSeparator_BracketRight(const char *)
434 {
435     SetTokenResult(done,stay);
436     pStati->SetCur(afterParameters);
437 }
438 
439 void
440 PE_Function::On_expectParameterSeparator_Comma(const char *)
441 {
442     pSpuParameter->Push(done);
443 }
444 
445 void
446 PE_Function::On_afterParameters_const(const char *)
447 {
448     SetTokenResult(done,stay);
449     eConVol = static_cast<E_ConVol>(
450                     static_cast<int>(eConVol) | static_cast<int>(ary::cpp::CONVOL_const) );
451 }
452 
453 void
454 PE_Function::On_afterParameters_volatile(const char *)
455 {
456     SetTokenResult(done,stay);
457     eConVol = static_cast<E_ConVol>(
458                     static_cast<int>(eConVol) | static_cast<int>(ary::cpp::CONVOL_volatile) );
459 }
460 
461 void
462 PE_Function::On_afterParameters_throw(const char *)
463 {
464     SetTokenResult(done,stay);
465     pStati->SetCur(afterThrow);
466     bThrow = true;
467 }
468 
469 void
470 PE_Function::On_afterParameters_SwBracket_Left(const char *)
471 {
472     EnterImplementation(1);
473 }
474 
475 void
476 PE_Function::On_afterParameters_Semicolon(const char *)
477 {
478     PerformFinishingPunctuation();
479 }
480 
481 void
482 PE_Function::On_afterParameters_Comma(const char *)
483 {
484     PerformFinishingPunctuation();
485 }
486 
487 void
488 PE_Function::On_afterParameters_Colon(const char *)
489 {
490     EnterImplementation(0);
491 }
492 
493 void
494 PE_Function::On_afterParameters_Assign(const char *)
495 {
496     SetTokenResult(done,stay);
497     pStati->SetCur(expectZero);
498 }
499 
500 void
501 PE_Function::On_afterThrow_Bracket_Left(const char *)
502 {
503     pSpuException->Push(done);
504 }
505 
506 void
507 PE_Function::On_expectExceptionSeparator_BracketRight(const char *)
508 {
509     SetTokenResult(done,stay);
510     pStati->SetCur(afterExceptions);
511 }
512 
513 void
514 PE_Function::On_expectExceptionSeparator_Comma(const char *)
515 {
516     pSpuException->Push(done);
517 }
518 
519 void
520 PE_Function::On_afterExceptions_SwBracket_Left(const char *)
521 {
522     EnterImplementation(1);
523 }
524 
525 void
526 PE_Function::On_afterExceptions_Semicolon(const char *)
527 {
528     PerformFinishingPunctuation();
529 }
530 
531 void
532 PE_Function::On_afterExceptions_Comma(const char *)
533 {
534     PerformFinishingPunctuation();
535 }
536 
537 void
538 PE_Function::On_afterExceptions_Colon(const char *)
539 {
540     EnterImplementation(0);
541 }
542 
543 void
544 PE_Function::On_afterExceptions_Assign(const char *)
545 {
546     SetTokenResult(done,stay);
547     pStati->SetCur(expectZero);
548 }
549 
550 void
551 PE_Function::On_expectZero_Constant(const char * i_sText)
552 {
553     if ( strcmp(i_sText,"0") != 0 )
554         Hdl_SyntaxError(i_sText);
555 
556     SetTokenResult(done,stay);
557     pStati->SetCur(afterExceptions);
558 
559     eVirtuality = ary::cpp::VIRTUAL_abstract;
560 }
561 
562 void
563 PE_Function::On_inImplementation_SwBracket_Left(const char *)
564 {
565     SetTokenResult(done,stay);
566     nBracketCounterInImplementation++;
567 }
568 
569 void
570 PE_Function::On_inImplementation_SwBracket_Right(const char *)
571 {
572     nBracketCounterInImplementation--;
573     if (nBracketCounterInImplementation == 0)
574     {
575         SetTokenResult(done,pop_success);
576     }
577     else
578     {
579         SetTokenResult(done,stay);
580     }
581 }
582 
583 void
584 PE_Function::On_inImplementation_Default(const char *)
585 {
586     SetTokenResult(done,stay);
587 }
588 
589 void
590 PE_Function::EnterImplementation( intt i_nBracketCountStart )
591 {
592     SetTokenResult(done,stay);
593     pStati->SetCur(inImplementation);
594 
595     bResult_WithImplementation = true;
596     nBracketCounterInImplementation = i_nBracketCountStart;
597     if ( Env().Context().CurClass() != 0 )
598     {
599         aFlags.SetInline();
600     }
601 }
602 
603 
604 
605 }   // namespace cpp
606 
607 
608 
609 
610 
611