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_vafu.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_class.hxx>
36 #include <ary/cpp/c_vari.hxx>
37 #include <ary/cpp/c_vfflag.hxx>
38 #include <ary/cpp/cp_ce.hxx>
39 #include <ary/cpp/inpcontx.hxx>
40 #include "pe_type.hxx"
41 #include "pe_vari.hxx"
42 #include "pe_funct.hxx"
43 #include "pe_ignor.hxx"
44 #include <x_parse.hxx>
45 
46 
47 
48 
49 namespace cpp {
50 
51 
52 
53 //***********************		PE_VarFunc		***********************//
54 
55 
56 PE_VarFunc::PE_VarFunc( Cpp_PE * i_pParent )
57 	:   Cpp_PE(i_pParent),
58         pStati( new PeStatusArray<PE_VarFunc> ),
59 	    // pSpType,
60 	    // pSpuType,
61 	    // pSpVariable,
62 	    // pSpuVariable,
63 	    // pSpFunction,
64 	    // pSpuFunctionStd,
65         // pSpuFunctionCtor,
66         // pSpuFunctionDtor,
67         // pSpuFunctionCastOperator,
68         // pSpuFunctionNormalOperator
69 	    // pSpIgnore,
70 	    // pSpuIgnore,
71         nCounter_TemplateBrackets(0),
72         bInDestructor(false),
73         // aResultIds,
74         nResultFrontType(0),
75         eResultType(result_unknown),
76         bVirtual(false),
77         bStatic(false),
78         bExtern(false),
79         bExternC(false),
80         bMutable(false),
81         bInline(false),
82         bRegister(false),
83         bExplicit(false)
84 {
85 	Setup_StatusFunctions();
86 
87     pSpType             = new SP_Type(*this);
88 	pSpuType            = new SPU_Type(*pSpType, 0, &PE_VarFunc::SpReturn_Type);
89 	pSpVariable         = new SP_Variable(*this);
90 	pSpuVariable        = new SPU_Variable(*pSpVariable, 0, &PE_VarFunc::SpReturn_Variable);
91 	pSpFunction         = new SP_Function(*this);
92 	pSpuFunctionStd     = new SPU_Function(*pSpFunction, &PE_VarFunc::SpInit_FunctionStd, &PE_VarFunc::SpReturn_FunctionStd);
93 	pSpuFunctionCtor    = new SPU_Function(*pSpFunction, &PE_VarFunc::SpInit_FunctionCtor, &PE_VarFunc::SpReturn_FunctionStd);
94 	pSpuFunctionDtor    = new SPU_Function(*pSpFunction, &PE_VarFunc::SpInit_FunctionDtor, &PE_VarFunc::SpReturn_FunctionStd);
95 	pSpuFunctionCastOperator
96                         = new SPU_Function(*pSpFunction, &PE_VarFunc::SpInit_FunctionCastOperator, &PE_VarFunc::SpReturn_FunctionStd);
97 	pSpuFunctionNormalOperator
98                         = new SPU_Function(*pSpFunction, &PE_VarFunc::SpInit_FunctionNormalOperator, &PE_VarFunc::SpReturn_FunctionStd);
99 	pSpIgnore           = new SP_Ignore(*this);
100 	pSpuIgnore          = new SPU_Ignore(*pSpIgnore, 0, &PE_VarFunc::SpReturn_Ignore);
101 }
102 
103 PE_VarFunc::~PE_VarFunc()
104 {
105 }
106 
107 void
108 PE_VarFunc::Call_Handler( const cpp::Token & i_rTok )
109 {
110 	pStati->Cur().Call_Handler(i_rTok.TypeId(), i_rTok.Text());
111 }
112 
113 void
114 PE_VarFunc::Setup_StatusFunctions()
115 {
116 	typedef CallFunction<PE_VarFunc>::F_Tok	F_Tok;
117 
118 	static F_Tok stateF_start[] =			{ &PE_VarFunc::On_start_Identifier,
119 											  &PE_VarFunc::On_start_operator,
120 											  &PE_VarFunc::On_start_TypeKey,
121 											  &PE_VarFunc::On_start_TypeKey,
122 											  &PE_VarFunc::On_start_TypeKey,
123 											  &PE_VarFunc::On_start_TypeKey,
124 											  &PE_VarFunc::On_start_virtual,
125 											  &PE_VarFunc::On_start_Tilde,
126 											  &PE_VarFunc::On_start_const,
127 											  &PE_VarFunc::On_start_volatile,
128 											  &PE_VarFunc::On_start_extern,
129 											  &PE_VarFunc::On_start_static,
130 											  &PE_VarFunc::On_start_mutable,
131 											  &PE_VarFunc::On_start_register,
132 											  &PE_VarFunc::On_start_inline,
133 											  &PE_VarFunc::On_start_explicit,
134                                               &PE_VarFunc::On_start_Bracket_Right,
135 											  &PE_VarFunc::On_start_Identifier,
136                                               &PE_VarFunc::On_start_typename,
137 											  &PE_VarFunc::On_start_Identifier,
138 											  &PE_VarFunc::On_start_Identifier };
139 	static INT16 stateT_start[] =       	{ Tid_Identifier,
140 											  Tid_operator,
141 											  Tid_class,
142 											  Tid_struct,
143 											  Tid_union,
144 											  Tid_enum,
145 											  Tid_virtual,
146 											  Tid_Tilde,
147 											  Tid_const,
148 											  Tid_volatile,
149 											  Tid_extern,
150 											  Tid_static,
151 											  Tid_mutable,
152 											  Tid_register,
153 											  Tid_inline,
154 											  Tid_explicit,
155                                               Tid_Bracket_Right,
156 											  Tid_DoubleColon,
157                                               Tid_typename,
158 											  Tid_BuiltInType,
159 											  Tid_TypeSpecializer };
160 
161 	static F_Tok stateF_expectCtor[] =		{ &PE_VarFunc::On_expectCtor_Bracket_Left };
162 	static INT16 stateT_expectCtor[] =     	{ Tid_Bracket_Left };
163 
164 	static F_Tok stateF_afterClassDecl[] =	{ &PE_VarFunc::On_afterClassDecl_Semicolon };
165 	static INT16 stateT_afterClassDecl[] =  { Tid_Semicolon };
166 
167 	static F_Tok stateF_expectName[] =		{ &PE_VarFunc::On_expectName_Identifier,
168 											  &PE_VarFunc::On_expectName_operator,
169 											  &PE_VarFunc::On_expectName_Bracket_Left };
170 	static INT16 stateT_expectName[] =     	{ Tid_Identifier,
171 											  Tid_operator,
172 											  Tid_Bracket_Left };
173 
174 	static F_Tok stateF_afterName[] =		{ &PE_VarFunc::On_afterName_ArrayBracket_Left,
175 											  &PE_VarFunc::On_afterName_Bracket_Left,
176 											  &PE_VarFunc::On_afterName_DoubleColon,
177 											  &PE_VarFunc::On_afterName_Semicolon,
178 											  &PE_VarFunc::On_afterName_Comma,
179 											  &PE_VarFunc::On_afterName_Assign,
180 											  &PE_VarFunc::On_afterName_Less };
181 	static INT16 stateT_afterName[] =     	{ Tid_ArrayBracket_Left,
182 											  Tid_Bracket_Left,
183                                               Tid_DoubleColon,
184 											  Tid_Semicolon,
185 											  Tid_Comma,
186 											  Tid_Assign,
187                                               Tid_Less };
188 
189 	static F_Tok stateF_afterName_inErraneousTemplate[] =
190                                             { &PE_VarFunc::On_afterName_inErraneousTemplate_Less,
191                                               &PE_VarFunc::On_afterName_inErraneousTemplate_Greater };
192 	static INT16 stateT_afterName_inErraneousTemplate[] =
193                                             { Tid_Less,
194                                               Tid_Greater };
195 
196 	static F_Tok stateF_finished[] =		{ &PE_VarFunc::On_finished_Semicolon,
197                                               &PE_VarFunc::On_finished_Comma };
198 	static INT16 stateT_finished[] =     	{ Tid_Semicolon,
199 											  Tid_Comma };
200 
201 	static F_Tok stateF_finishedIncludingFunctionImplementation[] =
202                                             { &PE_VarFunc::On_finishedIncludingFunctionImplementation_Default
203                                             };
204 	static INT16 stateT_finishedIncludingFunctionImplementation[] =
205                                             { Tid_BuiltInType     // Just to have one entry, but it is default handled, too.
206                                             };
207 
208 	SEMPARSE_CREATE_STATUS(PE_VarFunc, start, Hdl_UnknownToken);
209 	SEMPARSE_CREATE_STATUS(PE_VarFunc, expectCtor, Hdl_UnknownToken);
210 	SEMPARSE_CREATE_STATUS(PE_VarFunc, afterClassDecl, Hdl_UnknownToken);
211 	SEMPARSE_CREATE_STATUS(PE_VarFunc, expectName, Hdl_UnknownToken);
212 	SEMPARSE_CREATE_STATUS(PE_VarFunc, afterName, Hdl_UnknownToken);
213 	SEMPARSE_CREATE_STATUS(PE_VarFunc, afterName_inErraneousTemplate, On_afterName_inErraneousTemplate_Default);
214 	SEMPARSE_CREATE_STATUS(PE_VarFunc, finished, On_finished_Default);
215 	SEMPARSE_CREATE_STATUS(PE_VarFunc, finishedIncludingFunctionImplementation, On_finishedIncludingFunctionImplementation_Default);
216 }
217 
218 void
219 PE_VarFunc::InitData()
220 {
221 	pStati->SetCur(start);
222     csv::erase_container(aResultIds);
223 
224     nCounter_TemplateBrackets = 0;
225     bInDestructor = false;
226 
227 	nResultFrontType = 0;
228     eResultType = result_unknown;
229     sName.clear();
230     bVirtual = ary::cpp::VIRTUAL_none;
231     bStatic = false;
232     bExtern = false;
233     bExternC = false;
234     bMutable = false;
235     bInline = false;
236     bRegister = false;
237     bExplicit = false;
238 }
239 
240 void
241 PE_VarFunc::TransferData()
242 {
243 	pStati->SetCur(size_of_states);
244 }
245 
246 void
247 PE_VarFunc::Hdl_UnknownToken( const char *)
248 {
249 	pSpuIgnore->Push(not_done);
250 }
251 
252 void
253 PE_VarFunc::SpInit_FunctionStd()
254 {
255     if ( nResultFrontType.IsValid() AND sName.length() > 0 )
256     {
257         pSpuFunctionStd->Child().Init_Std(
258                                        sName,
259                                        nResultFrontType,
260                                        bVirtual,
261                                        CreateFunctionFlags() );
262     }
263     else
264     {
265     	throw X_Parser( X_Parser::x_UnexpectedToken,
266                         "",
267                         Env().CurFileName(),
268                         Env().LineCount() );
269     }
270 }
271 
272 void
273 PE_VarFunc::SpInit_FunctionCtor()
274 {
275 	ary::cpp::Class * pOwnerClass = Env().Context().CurClass();
276 	csv_assert( pOwnerClass != 0 );
277 	pSpuFunctionStd->Child().Init_Ctor( pOwnerClass->LocalName(),
278                                         CreateFunctionFlags() );
279 }
280 
281 void
282 PE_VarFunc::SpInit_FunctionDtor()
283 {
284     pSpuFunctionStd->Child().Init_Dtor( sName,
285                                         bVirtual,
286                                         CreateFunctionFlags() );
287 }
288 
289 void
290 PE_VarFunc::SpInit_FunctionCastOperator()
291 {
292     pSpuFunctionStd->Child().Init_CastOperator( bVirtual,
293                                                 CreateFunctionFlags() );
294 }
295 
296 void
297 PE_VarFunc::SpInit_FunctionNormalOperator()
298 {
299     pSpuFunctionStd->Child().Init_NormalOperator( nResultFrontType,
300                                                   bVirtual,
301                                                   CreateFunctionFlags() );
302 }
303 
304 void
305 PE_VarFunc::SpReturn_Type()
306 {
307 	switch ( pSpuType->Child().Result_KindOf() )
308 	{
309 		case PE_Type::is_type:
310 							pStati->SetCur(expectName);
311 							nResultFrontType
312 									= pSpuType->Child().Result_Type().Id();
313 							break;
314 		case PE_Type::is_constructor:
315 							pStati->SetCur(expectCtor);
316 							eResultType = result_function;
317 							break;
318 		case PE_Type::is_explicit_class_declaration:
319 		case PE_Type::is_explicit_enum_declaration:
320 							pStati->SetCur(afterClassDecl);
321 							eResultType = result_ignore;
322 							break;
323 		case PE_Type::is_class_predeclaration:
324 							pStati->SetCur(afterClassDecl);
325 							eResultType = result_ignore;
326 							break;
327         default:
328                             ;
329 	}
330 }
331 
332 void
333 PE_VarFunc::SpReturn_Variable()
334 {
335     typedef ary::cpp::VariableFlags  VarFlags;
336 
337 	if ( NOT bExtern )
338 	{
339 		VarFlags aFlags( UINT16(
340 						   ( bStatic AND Env().Context().CurClass() == 0 ? VarFlags::f_static_local : 0 )
341 						 | ( bStatic AND Env().Context().CurClass() != 0 ? VarFlags::f_static_member : 0 )
342 						 | ( bMutable  ? VarFlags::f_mutable : 0 ) )
343 					   );
344 
345 //      ary::S_InitData aData( 0, Env().CurCeSpace().Id(), i_sName, 0 );
346 		ary::cpp::Variable & rCurParsedVariable
347 				= Env().AryGate().Ces().Store_Variable(	Env().Context(),
348 													sName,
349 													nResultFrontType,
350 													aFlags,
351 													pSpuVariable->Child().Result_SizeExpression(),
352 													pSpuVariable->Child().Result_InitExpression() );
353 		Env().Event_Store_Variable(rCurParsedVariable);
354 		aResultIds.push_back( rCurParsedVariable.CeId() );
355 		eResultType = result_variable;
356 	}
357 	else if (bExtern)
358 	{
359 		eResultType = result_ignore;
360 	}
361 
362 	pStati->SetCur(finished);
363 }
364 
365 void
366 PE_VarFunc::SpReturn_FunctionStd()
367 {
368 	if ( (NOT bExtern) OR bExternC )
369 	{
370         aResultIds.push_back(pSpuFunctionStd->Child().Result_Id());
371         eResultType = result_function;
372     }
373     else
374     {
375         eResultType = result_ignore;
376     }
377 
378     if ( NOT pSpuFunctionStd->Child().Result_WithImplementation() )
379         pStati->SetCur(finished);
380     else
381         pStati->SetCur(finishedIncludingFunctionImplementation);
382 }
383 
384 void
385 PE_VarFunc::SpReturn_Ignore()
386 {
387 	pStati->SetCur(finished);
388 }
389 
390 void
391 PE_VarFunc::On_start_Identifier(const char *)
392 {
393     pSpuType->Push(not_done);
394 }
395 
396 void
397 PE_VarFunc::On_start_operator(const char *)
398 {
399     pSpuFunctionCastOperator->Push(done);
400 }
401 
402 void
403 PE_VarFunc::On_start_TypeKey(const char *)
404 {
405     pSpuType->Push(not_done);
406 }
407 
408 void
409 PE_VarFunc::On_start_virtual(const char *)
410 {
411 	SetTokenResult(done, stay);
412     bVirtual = true;
413 }
414 
415 void
416 PE_VarFunc::On_start_Tilde(const char *)
417 {
418 	SetTokenResult(done, stay);
419 	pStati->SetCur(expectName);
420 
421     bInDestructor = true;
422 }
423 
424 void
425 PE_VarFunc::On_start_const(const char *)
426 {
427     pSpuType->Push(not_done);
428 }
429 
430 void
431 PE_VarFunc::On_start_volatile(const char *)
432 {
433     pSpuType->Push(not_done);
434 }
435 
436 void
437 PE_VarFunc::On_start_extern(const char *)
438 {
439 	SetTokenResult(done, stay);
440     bExtern = true;
441 }
442 
443 void
444 PE_VarFunc::On_start_static(const char *)
445 {
446 	SetTokenResult(done, stay);
447     bStatic = true;
448 }
449 
450 void
451 PE_VarFunc::On_start_mutable(const char *)
452 {
453 	SetTokenResult(done, stay);
454     bMutable = true;
455 }
456 
457 void
458 PE_VarFunc::On_start_register(const char *)
459 {
460 	SetTokenResult(done, stay);
461     bRegister = true;
462 }
463 
464 void
465 PE_VarFunc::On_start_inline(const char *)
466 {
467    	SetTokenResult(done, stay);
468 
469     bInline = true;
470 }
471 
472 void
473 PE_VarFunc::On_start_explicit(const char *)
474 {
475 	SetTokenResult(done, stay);
476 	bExplicit = true;
477 }
478 
479 void
480 PE_VarFunc::On_start_Bracket_Right(const char *)
481 {
482 	SetTokenResult(not_done, pop_success);
483 }
484 
485 void
486 PE_VarFunc::On_start_typename(const char *)
487 {
488     pSpuType->Push(not_done);
489 }
490 
491 void
492 PE_VarFunc::On_expectCtor_Bracket_Left(const char *)
493 {
494 	pSpuFunctionCtor->Push(not_done);
495 }
496 
497 void
498 PE_VarFunc::On_afterClassDecl_Semicolon(const char *)
499 {
500 	SetTokenResult(done, pop_success);
501 }
502 
503 void
504 PE_VarFunc::On_expectName_Identifier(const char * i_sText)
505 {
506 	SetTokenResult(done, stay);
507 	pStati->SetCur(afterName);
508     sName = i_sText;
509 }
510 
511 void
512 PE_VarFunc::On_expectName_operator(const char *)
513 {
514     pSpuFunctionNormalOperator->Push(done);
515 }
516 
517 void
518 PE_VarFunc::On_expectName_Bracket_Left(const char *)
519 {
520     // Function pointer declaration
521     pSpuIgnore->Push(not_done);
522     // TODO
523 }
524 
525 
526 void
527 PE_VarFunc::On_afterName_ArrayBracket_Left(const char *)
528 {
529     pSpuVariable->Push(not_done);
530 }
531 
532 void
533 PE_VarFunc::On_afterName_Bracket_Left(const char *)
534 {
535     if ( NOT bInDestructor)
536         pSpuFunctionStd->Push(not_done);
537     else
538         pSpuFunctionDtor->Push(not_done);
539 }
540 
541 void
542 PE_VarFunc::On_afterName_DoubleColon(const char *)
543 {
544     pSpuIgnore->Push(done);   // This seems to be only an implementation.
545 
546     // This may have been a template.
547     // In that case, the declaration needs to be closed.
548     Env().Close_OpenTemplate();
549 }
550 
551 void
552 PE_VarFunc::On_afterName_Semicolon(const char *)
553 {
554     pSpuVariable->Push(not_done);
555 }
556 
557 void
558 PE_VarFunc::On_afterName_Comma(const char *)
559 {
560     pSpuVariable->Push(not_done);
561 }
562 
563 void
564 PE_VarFunc::On_afterName_Assign(const char * )
565 {
566     pSpuVariable->Push(not_done);
567 }
568 
569 void
570 PE_VarFunc::On_afterName_Less(const char * )
571 {
572 	SetTokenResult(done, stay);
573     pStati->SetCur(afterName_inErraneousTemplate);
574 
575     nCounter_TemplateBrackets = 1;
576 }
577 
578 void
579 PE_VarFunc::On_afterName_inErraneousTemplate_Less(const char * )
580 {
581 	SetTokenResult(done, stay);
582 
583     nCounter_TemplateBrackets++;
584 }
585 
586 void
587 PE_VarFunc::On_afterName_inErraneousTemplate_Greater(const char * )
588 {
589 	SetTokenResult(done, stay);
590 
591     nCounter_TemplateBrackets--;
592     if ( nCounter_TemplateBrackets == 0 )
593         pStati->SetCur(afterName);
594 }
595 
596 void
597 PE_VarFunc::On_afterName_inErraneousTemplate_Default(const char * )
598 {
599 	SetTokenResult(done, stay);
600 }
601 
602 void
603 PE_VarFunc::On_finished_Semicolon(const char * )  // Should be Semicolon !!!
604 {
605 	SetTokenResult(done, pop_success);
606 }
607 
608 void
609 PE_VarFunc::On_finished_Comma(const char * )
610 {
611 	SetTokenResult(done, stay);
612     pStati->SetCur(expectName);
613 }
614 
615 void
616 PE_VarFunc::On_finished_Default(const char * )
617 {
618     SetTokenResult(not_done, pop_success);
619 }
620 
621 void
622 PE_VarFunc::On_finishedIncludingFunctionImplementation_Default(const char * )
623 {
624     SetTokenResult(not_done, pop_success);
625 }
626 
627 ary::cpp::FunctionFlags
628 PE_VarFunc::CreateFunctionFlags()
629 {
630     typedef ary::cpp::FunctionFlags  FuncFlags;
631 
632     return FuncFlags(     UINT16(
633                             ( bStatic AND Env().Context().CurClass() == 0 ? FuncFlags::f_static_local : 0 )
634                           | ( bStatic AND Env().Context().CurClass() != 0 ? FuncFlags::f_static_member : 0 )
635                           | ( bExtern ? FuncFlags::f_extern : 0 )
636                           | ( Env().IsExternC() ? FuncFlags::f_externC : 0 )
637                           | ( bMutable  ? FuncFlags::f_mutable : 0 )
638                           | ( bInline  ? FuncFlags::f_inline : 0 )
639                           | ( bRegister  ? FuncFlags::f_register : 0 )
640                           | ( bExplicit  ? FuncFlags::f_explicit : 0 ) )
641                         );
642 
643 }
644 
645 
646 }   // namespace cpp
647 
648 
649 
650