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