Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2074 lines
55 KiB

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Copyright (c) 1989-1999 Microsoft Corporation
  3. Module Name:
  4. expr.cxx
  5. Abstract:
  6. expression evaluator routines implementation.
  7. Notes:
  8. History:
  9. VibhasC Aug-05-1993 Created
  10. ----------------------------------------------------------------------------*/
  11. #pragma warning ( disable : 4514 )
  12. /****************************************************************************
  13. * include files
  14. ***************************************************************************/
  15. #include "nulldefs.h"
  16. extern "C"
  17. {
  18. #include <stdio.h>
  19. #include <string.h>
  20. }
  21. #include "expr.hxx"
  22. #include "listhndl.hxx"
  23. #include "nodeskl.hxx"
  24. #include "semantic.hxx"
  25. #include "symtable.hxx"
  26. #include "cmdana.hxx"
  27. #include <excpt.h>
  28. #include <float.h>
  29. /****************************************************************************
  30. * extern definitions
  31. ***************************************************************************/
  32. /****************************************************************************
  33. * extern data
  34. ***************************************************************************/
  35. extern SymTable * pBaseSymTbl;
  36. extern CMD_ARG * pCommand;
  37. /****************************************************************************
  38. * local definitions
  39. ***************************************************************************/
  40. /****************************************************************************
  41. * local data
  42. ***************************************************************************/
  43. /***************************************************************************/
  44. short
  45. expr_node::MakeListOfVars(
  46. ITERATOR &
  47. )
  48. {
  49. return( 0 );
  50. }
  51. short
  52. expr_named_constant::MakeListOfVars(
  53. ITERATOR &
  54. )
  55. {
  56. return( 0 );
  57. }
  58. short
  59. expr_variable::MakeListOfVars(
  60. ITERATOR & pList
  61. )
  62. {
  63. pList.Insert( this );
  64. return( 1 );
  65. }
  66. short
  67. expr_op_unary::MakeListOfVars(
  68. ITERATOR & pList
  69. )
  70. {
  71. short VarCount = 0;
  72. if ( GetLeft() )
  73. VarCount = GetLeft()->MakeListOfVars( pList );
  74. return( VarCount );
  75. }
  76. short
  77. expr_sizeof::MakeListOfVars(
  78. ITERATOR &
  79. )
  80. /*++
  81. expr_sizeof is a unary_op but it doesn't have a child!
  82. --*/
  83. {
  84. return( 0 );
  85. }
  86. short
  87. expr_alignof::MakeListOfVars(
  88. ITERATOR &
  89. )
  90. /*++
  91. expr_alignof is a unary_op but it doesn't have a child!
  92. --*/
  93. {
  94. return( 0 );
  95. }
  96. short
  97. expr_op_binary::MakeListOfVars(
  98. ITERATOR & pList
  99. )
  100. {
  101. short VarCount;
  102. VarCount = GetLeft()->MakeListOfVars( pList );
  103. if ( GetRight() )
  104. {
  105. VarCount = short( VarCount + GetRight()->MakeListOfVars( pList ) );
  106. }
  107. return( VarCount );
  108. }
  109. short
  110. expr_ternary::MakeListOfVars(
  111. ITERATOR & pList
  112. )
  113. {
  114. short VarCount;
  115. VarCount = GetLeft()->MakeListOfVars( pList );
  116. if ( GetRight() )
  117. {
  118. VarCount = short( VarCount + GetRight()->MakeListOfVars( pList ) );
  119. }
  120. if ( GetRelational() )
  121. {
  122. VarCount = short( VarCount + GetRelational()->MakeListOfVars( pList ) );
  123. }
  124. return( VarCount );
  125. }
  126. /***************************************************************************/
  127. void
  128. expr_node::DecorateWithPrefix(
  129. char * pPrefix )
  130. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  131. Routine Description:
  132. This routine decorates all the variable nodes in the expression
  133. with a prefix.
  134. Arguments:
  135. pPrefix - the prefix to be prepended to each variable
  136. Return Value:
  137. Notes:
  138. ----------------------------------------------------------------------------*/
  139. {
  140. ITERATOR VarList;
  141. expr_variable * pVarNode = 0;
  142. short VarCount = MakeListOfVars( VarList );
  143. if ( VarCount )
  144. {
  145. VarList.Init();
  146. while ( ITERATOR_GETNEXT( VarList, pVarNode ) )
  147. pVarNode->SetPrefix( pPrefix );
  148. }
  149. return;
  150. }
  151. expr_index::expr_index(
  152. expr_node * pL,
  153. expr_node * pR ) : expr_op_binary( OP_INDEX, pL, pR )
  154. {
  155. SetType( pL->GetType() );
  156. }
  157. expr_param *
  158. expr_proc_call::SetParam(
  159. expr_param * pParam )
  160. {
  161. expr_param * p = GetFirstParam();
  162. IncrNoOfParams();
  163. if( p )
  164. {
  165. return p->SetLastPeerParam( pParam );
  166. }
  167. else
  168. return SetFirstParam( pParam );
  169. }
  170. expr_param *
  171. expr_proc_call::SetParam(
  172. expr_node * pExpr )
  173. {
  174. return SetParam( new expr_param( pExpr ));
  175. }
  176. expr_param *
  177. expr_param::SetLastPeerParam(
  178. expr_param * pParam )
  179. {
  180. expr_param * p = GetNextParam();
  181. if( p )
  182. {
  183. return p->SetLastPeerParam( pParam );
  184. }
  185. else
  186. {
  187. return SetNextParam( pParam );
  188. }
  189. }
  190. /**
  191. ** This routine attempts to recreate the string to be eaten by c compilers.
  192. ** If the user specified a string with a quote inside, substitute with an
  193. ** escape character. The new string with possible escapes will ALWAYS be
  194. ** smaller than the older one, so allocating the same amount of space is
  195. ** enough.
  196. **/
  197. char *
  198. MakeNewStringWithProperQuoting(
  199. char * pSrc )
  200. {
  201. char * pResult = new char [ strlen( pSrc ) + 1];
  202. char ch;
  203. char * pDest = pResult;
  204. while( ( ch = *pSrc++ ) != 0 )
  205. {
  206. *pDest = ch;
  207. if( ch == '\\')
  208. {
  209. if( ( ch = *pSrc++ ) != 0 )
  210. {
  211. if( (ch == '"') || ( ch == '\\') )
  212. {
  213. *pDest = ch;
  214. }
  215. else
  216. {
  217. *pDest++ = '\\';
  218. *pDest = ch;
  219. }
  220. }
  221. else
  222. break;
  223. }
  224. pDest++;
  225. }
  226. *pDest = '\0';
  227. return pResult;
  228. }
  229. // routines for expr_variable...
  230. // constructor - see if the type is constant
  231. expr_variable::expr_variable( PNAME p, node_skl * pT )
  232. {
  233. SetName( p );
  234. SetType( pT );
  235. SetConstant( FALSE );
  236. SetPrefix( NULL );
  237. }
  238. // resolve forwards on GetType
  239. node_skl *
  240. expr_variable::GetType( void )
  241. {
  242. if ( pType )
  243. {
  244. if ( pType->NodeKind() == NODE_FORWARD )
  245. {
  246. node_forward* pFwd = (node_forward *) pType;
  247. // (permanently) resolve the forward
  248. pType = pFwd->ResolveFDecl();
  249. // if it couldn't be resolved, put back the forward
  250. if ( !pType )
  251. {
  252. pType = pFwd;
  253. }
  254. }
  255. }
  256. return pType;
  257. }
  258. // named constant routines...
  259. // evaluate the variable if it is a constant
  260. EXPR_VALUE
  261. expr_named_constant::GetValue()
  262. {
  263. node_skl * pT = GetType();
  264. NODE_T Kind = ( pT ) ? (NODE_T) pT->NodeKind()
  265. : (NODE_T) NODE_ILLEGAL;
  266. if ( !IsConstant() )
  267. return 0;
  268. // identifiers may be const... Forwards are assumed NOT const
  269. if ( Kind == NODE_ID )
  270. {
  271. node_id * pId = (node_id *) pT;
  272. node_skl * pType = pId->GetBasicType();
  273. EXPR_VALUE Result = (pId->GetExpr()->GetValue() );
  274. return (pType) ? pType->ConvertMyKindOfValueToEXPR_VALUE(Result) : Result;
  275. }
  276. else if ( Kind == NODE_LABEL )
  277. {
  278. return ( (node_label *) pT)->GetValue();
  279. }
  280. MIDL_ASSERT(FALSE);
  281. return 0; // for the compiler...
  282. }
  283. // evaluate the variable if it is a constant
  284. expr_node *
  285. expr_named_constant::GetExpr()
  286. {
  287. node_skl * pT = GetType();
  288. NODE_T Kind = ( pT ) ? (NODE_T) pT->NodeKind()
  289. : (NODE_T) NODE_ILLEGAL;
  290. if ( !IsConstant() )
  291. return 0;
  292. // identifiers may be const... Forwards are assumed NOT const
  293. if ( Kind == NODE_ID )
  294. {
  295. node_id * pId = (node_id *) pT;
  296. return pId->GetExpr();
  297. }
  298. else if ( Kind == NODE_LABEL )
  299. {
  300. return ( (node_label *) pT)->pExpr;
  301. }
  302. MIDL_ASSERT(FALSE);
  303. return 0; // for the compiler...
  304. }
  305. /******************************************************************************
  306. * expression list handler
  307. *****************************************************************************/
  308. expr_list::expr_list()
  309. {
  310. SetConstant( TRUE );
  311. }
  312. STATUS_T
  313. expr_list::GetPeer(
  314. expr_node ** ppExpr )
  315. {
  316. return (GetNext( (void **)ppExpr ) );
  317. }
  318. STATUS_T
  319. expr_list::SetPeer(
  320. expr_node * pExpr )
  321. {
  322. if ( pExpr && !pExpr->IsConstant() )
  323. SetConstant( FALSE );
  324. return Insert( (void *) pExpr );
  325. }
  326. EXPR_VALUE
  327. expr_u_arithmetic::GetValue()
  328. {
  329. EXPR_VALUE LeftValue = GetLeft()->GetValue();
  330. EXPR_VALUE Result;
  331. switch ( GetOperator() )
  332. {
  333. case OP_UNARY_PLUS:
  334. {
  335. Result = LeftValue;
  336. break;
  337. }
  338. case OP_UNARY_MINUS:
  339. {
  340. if (IsStringConstant())
  341. {
  342. char * szVal = (char *)LeftValue;
  343. char * szNewVal = new char[strlen(szVal)+2];
  344. szNewVal[0] = '-';
  345. strcpy(szNewVal + 1, szVal);
  346. Result = (EXPR_VALUE) szNewVal;
  347. }
  348. else
  349. {
  350. Result = -LeftValue;
  351. }
  352. break;
  353. }
  354. default:
  355. {
  356. Result = 0;
  357. break;
  358. }
  359. }
  360. return (GetType()) ? GetType()->ConvertMyKindOfValueToEXPR_VALUE(Result) : Result;
  361. }
  362. EXPR_VALUE
  363. expr_u_not::GetValue()
  364. {
  365. EXPR_VALUE Result = !GetLeft()->GetValue();
  366. return (GetType()) ? GetType()->ConvertMyKindOfValueToEXPR_VALUE(Result) : Result;
  367. }
  368. EXPR_VALUE
  369. expr_u_complement::GetValue()
  370. {
  371. EXPR_VALUE Result = ~ GetLeft()->GetValue();
  372. return (GetType()) ? GetType()->ConvertMyKindOfValueToEXPR_VALUE(Result) : Result;
  373. }
  374. EXPR_VALUE
  375. expr_cast::GetValue()
  376. {
  377. return GetType()->
  378. ConvertMyKindOfValueToEXPR_VALUE(GetLeft()->GetValue());
  379. }
  380. EXPR_VALUE
  381. expr_constant::GetValue()
  382. {
  383. return (GetType()) ? GetType()->ConvertMyKindOfValueToEXPR_VALUE(Value.I64)
  384. : Value.I64;
  385. }
  386. EXPR_VALUE
  387. expr_sizeof::GetValue()
  388. {
  389. return (pType) ? pType->GetSize() : 0;
  390. }
  391. EXPR_VALUE
  392. expr_alignof::GetValue()
  393. {
  394. return (pType) ? pType->GetAlign() : 0;
  395. }
  396. EXPR_VALUE
  397. expr_b_arithmetic::GetValue()
  398. {
  399. EXPR_VALUE LeftValue = GetLeft()->GetValue();
  400. EXPR_VALUE RightValue = GetRight()->GetValue();
  401. EXPR_VALUE Result;
  402. switch ( GetOperator() )
  403. {
  404. case OP_PLUS:
  405. {
  406. Result = LeftValue + RightValue;
  407. break;
  408. }
  409. case OP_MINUS:
  410. {
  411. Result = LeftValue - RightValue;
  412. break;
  413. }
  414. case OP_STAR:
  415. {
  416. Result = LeftValue * RightValue;
  417. break;
  418. }
  419. case OP_SLASH:
  420. {
  421. if (RightValue == 0)
  422. Result = 1;
  423. else
  424. Result = LeftValue / RightValue;
  425. break;
  426. }
  427. case OP_MOD:
  428. {
  429. if ( 0 == RightValue )
  430. Result = 1;
  431. else
  432. Result = LeftValue % RightValue;
  433. break;
  434. }
  435. default:
  436. {
  437. Result = 0;
  438. break;
  439. }
  440. }
  441. return (GetType()) ? GetType()->ConvertMyKindOfValueToEXPR_VALUE(Result) : Result ;
  442. }
  443. EXPR_VALUE
  444. expr_b_logical::GetValue()
  445. {
  446. EXPR_VALUE LeftValue = GetLeft()->GetValue();
  447. EXPR_VALUE RightValue = GetRight()->GetValue();
  448. EXPR_VALUE Result;
  449. switch ( GetOperator() )
  450. {
  451. case OP_LOGICAL_AND:
  452. {
  453. Result = LeftValue && RightValue;
  454. break;
  455. }
  456. case OP_LOGICAL_OR:
  457. {
  458. Result = LeftValue || RightValue;
  459. break;
  460. }
  461. default:
  462. {
  463. Result = 0;
  464. break;
  465. }
  466. }
  467. return (GetType()) ? GetType()->ConvertMyKindOfValueToEXPR_VALUE(Result) : Result ;
  468. }
  469. EXPR_VALUE
  470. expr_relational::GetValue()
  471. {
  472. EXPR_VALUE LeftValue = GetLeft()->GetValue();
  473. EXPR_VALUE RightValue = GetRight()->GetValue();
  474. EXPR_VALUE Result;
  475. switch ( GetOperator() )
  476. {
  477. // gaj - we implicitly assume signed types
  478. case OP_LESS:
  479. {
  480. Result = LeftValue < RightValue;
  481. break;
  482. }
  483. case OP_LESS_EQUAL:
  484. {
  485. Result = LeftValue <= RightValue;
  486. break;
  487. }
  488. case OP_GREATER_EQUAL:
  489. {
  490. Result = LeftValue >= RightValue;
  491. break;
  492. }
  493. case OP_GREATER:
  494. {
  495. Result = LeftValue > RightValue;
  496. break;
  497. }
  498. case OP_EQUAL:
  499. {
  500. Result = LeftValue == RightValue;
  501. break;
  502. }
  503. case OP_NOT_EQUAL:
  504. {
  505. Result = LeftValue != RightValue;
  506. break;
  507. }
  508. default:
  509. {
  510. Result = 0;
  511. break;
  512. }
  513. }
  514. return (GetType()) ? GetType()->ConvertMyKindOfValueToEXPR_VALUE(Result) : Result;
  515. }
  516. EXPR_VALUE
  517. expr_shift::GetValue()
  518. {
  519. EXPR_VALUE LeftValue = GetLeft()->GetValue();
  520. EXPR_VALUE RightValue = GetRight()->GetValue();
  521. EXPR_VALUE Result;
  522. switch ( GetOperator() )
  523. {
  524. // gaj - we implicitly assume signed types
  525. case OP_LEFT_SHIFT:
  526. {
  527. Result = LeftValue << RightValue;
  528. break;
  529. }
  530. case OP_RIGHT_SHIFT:
  531. {
  532. Result = LeftValue >> RightValue;
  533. break;
  534. }
  535. default:
  536. {
  537. Result = 0;
  538. break;
  539. }
  540. }
  541. return (GetType()) ? GetType()->ConvertMyKindOfValueToEXPR_VALUE(Result) : Result;
  542. }
  543. EXPR_VALUE
  544. expr_bitwise::GetValue()
  545. {
  546. EXPR_VALUE LeftValue = GetLeft()->GetValue();
  547. EXPR_VALUE RightValue = GetRight()->GetValue();
  548. EXPR_VALUE Result;
  549. switch ( GetOperator() )
  550. {
  551. // gaj - we implicitly assume signed types
  552. case OP_AND:
  553. {
  554. Result = LeftValue & RightValue;
  555. break;
  556. }
  557. case OP_OR:
  558. {
  559. Result = LeftValue | RightValue;
  560. break;
  561. }
  562. case OP_XOR:
  563. {
  564. Result = LeftValue ^ RightValue;
  565. break;
  566. }
  567. default:
  568. {
  569. Result = 0;
  570. break;
  571. }
  572. }
  573. return (GetType()) ? GetType()->ConvertMyKindOfValueToEXPR_VALUE(Result) : Result ;
  574. }
  575. EXPR_VALUE
  576. expr_ternary::GetValue()
  577. {
  578. EXPR_VALUE RelValue = GetRelational()->GetValue();
  579. EXPR_VALUE Result;
  580. if ( RelValue )
  581. Result = GetLeft()->GetValue();
  582. else
  583. Result = GetRight()->GetValue();
  584. return (GetType()) ? GetType()->ConvertMyKindOfValueToEXPR_VALUE(Result) : Result;
  585. }
  586. ////////////////////////////////////////////////////////////////////////
  587. // analysis of expressions
  588. //
  589. void
  590. expr_node::ExprAnalyze( EXPR_CTXT * pExprCtxt )
  591. {
  592. pExprCtxt->SetUpFlags( EX_VALUE_INVALID );
  593. }
  594. //
  595. // determine the type of the expression
  596. //
  597. void
  598. expr_op_unary::DetermineType()
  599. {
  600. node_skl * pLeftType;
  601. if ( !GetLeft() )
  602. {
  603. return;
  604. }
  605. pLeftType = GetLeft()->AlwaysGetType();
  606. SetConstant( GetLeft()->IsConstant() );
  607. switch ( GetOperator() )
  608. {
  609. case OP_UNARY_PLUS:
  610. case OP_UNARY_COMPLEMENT:
  611. case OP_UNARY_MINUS:
  612. case OP_PRE_INCR:
  613. case OP_PRE_DECR:
  614. case OP_POST_INCR:
  615. case OP_POST_DECR:
  616. {
  617. // same type
  618. SetType( pLeftType );
  619. break;
  620. }
  621. case OP_UNARY_NOT:
  622. {
  623. node_skl * pTmpType;
  624. GetBaseTypeNode( &pTmpType, SIGN_UNDEF, SIZE_UNDEF, TYPE_BOOLEAN );
  625. SetType( pTmpType );
  626. break;
  627. }
  628. case OP_UNARY_CAST:
  629. {
  630. SetType( GetType() );
  631. break;
  632. }
  633. case OP_UNARY_SIZEOF:
  634. case OP_UNARY_ALIGNOF:
  635. {
  636. node_skl * pTmpType;
  637. GetBaseTypeNode( &pTmpType, SIGN_SIGNED, SIZE_UNDEF, TYPE_INT );
  638. SetType( pTmpType );
  639. SetConstant( TRUE );
  640. break;
  641. }
  642. case OP_UNARY_INDIRECTION:
  643. {
  644. node_skl * pNode = pLeftType;
  645. if ( pNode->NodeKind() != NODE_POINTER )
  646. {
  647. // probably the param or field; look down
  648. pNode = pNode->GetBasicType();
  649. }
  650. // now get the pointee
  651. pNode = pNode->GetBasicType();
  652. SetType( pNode );
  653. break;
  654. }
  655. case OP_UNARY_AND:
  656. {
  657. node_pointer * pPtr = new node_pointer;
  658. pPtr->SetChild( pLeftType );
  659. SetType( pPtr );
  660. break;
  661. }
  662. default:
  663. {
  664. break;
  665. }
  666. } // end of switch
  667. }
  668. void
  669. expr_op_unary::ExprAnalyze( EXPR_CTXT * pExprCtxt )
  670. {
  671. EXPR_CTXT LeftCtxt( pExprCtxt );
  672. if ( GetLeft() )
  673. {
  674. GetLeft()->ExprAnalyze( &LeftCtxt );
  675. pExprCtxt->MergeUpFlags( &LeftCtxt );
  676. SetConstant( GetLeft()->IsConstant() );
  677. }
  678. else if ( GetOperator() != OP_UNARY_SIZEOF && GetOperator() != OP_UNARY_ALIGNOF)
  679. pExprCtxt->SetUpFlags( EX_VALUE_INVALID );
  680. // do type compatibility stuff
  681. switch ( GetOperator() )
  682. {
  683. case OP_UNARY_PLUS:
  684. case OP_UNARY_COMPLEMENT:
  685. {
  686. // same type
  687. pExprCtxt->pType = LeftCtxt.pType;
  688. pExprCtxt->TypeInfo = LeftCtxt.TypeInfo;
  689. pExprCtxt->fIntegral = LeftCtxt.fIntegral;
  690. SetType( pExprCtxt->pType );
  691. break;
  692. }
  693. case OP_UNARY_MINUS:
  694. {
  695. node_skl * pTmp;
  696. pExprCtxt->pType = LeftCtxt.pType;
  697. pExprCtxt->TypeInfo = LeftCtxt.TypeInfo;
  698. pExprCtxt->TypeInfo.TypeSign = SIGN_SIGNED;
  699. pExprCtxt->fIntegral = LeftCtxt.fIntegral;
  700. GetBaseTypeNode( &pTmp, pExprCtxt->TypeInfo );
  701. if ( pTmp )
  702. pExprCtxt->pType = pTmp;
  703. SetType( pExprCtxt->pType );
  704. break;
  705. }
  706. case OP_UNARY_NOT:
  707. {
  708. pExprCtxt->TypeInfo.TypeSize = SIZE_UNDEF;
  709. pExprCtxt->TypeInfo.BaseType = TYPE_BOOLEAN;
  710. pExprCtxt->TypeInfo.TypeSign = SIGN_UNDEF;
  711. GetBaseTypeNode( &(pExprCtxt->pType ), pExprCtxt->TypeInfo );
  712. pExprCtxt->fIntegral = TRUE;
  713. SetType( pExprCtxt->pType );
  714. break;
  715. }
  716. case OP_UNARY_CAST:
  717. {
  718. SetType( GetType() );
  719. break;
  720. }
  721. case OP_UNARY_SIZEOF:
  722. case OP_UNARY_ALIGNOF:
  723. {
  724. pExprCtxt->TypeInfo.TypeSize = SIZE_UNDEF;
  725. pExprCtxt->TypeInfo.BaseType = TYPE_INT;
  726. pExprCtxt->TypeInfo.TypeSign = SIGN_SIGNED;
  727. GetBaseTypeNode( &(pExprCtxt->pType ), pExprCtxt->TypeInfo);
  728. pExprCtxt->fIntegral = TRUE;
  729. SetType( pExprCtxt->pType );
  730. break;
  731. }
  732. case OP_UNARY_INDIRECTION:
  733. {
  734. node_skl * pNode = GetLeft()->GetType();
  735. node_skl * pOrigNode = pNode;
  736. node_ptr_attr * pPtrAttr;
  737. PTRTYPE PtrKind = PTR_UNKNOWN;
  738. if ( pNode->FInSummary( ATTR_PTR_KIND ) )
  739. {
  740. pPtrAttr = (node_ptr_attr *)
  741. ((named_node *)pNode)->GetAttribute( ATTR_PTR_KIND );
  742. PtrKind = pPtrAttr->GetPtrKind();
  743. }
  744. if ( pNode->FInSummary( ATTR_IGNORE ) )
  745. {
  746. SEM_ANALYSIS_CTXT * pCtxt = pExprCtxt->GetCtxt();
  747. // Only complain about this on the actual type, not every
  748. // single use of the type.
  749. if ( !pCtxt->AnyAncestorBits( IN_PARAM_LIST ) )
  750. {
  751. SemError(
  752. pNode,
  753. *pCtxt,
  754. CORRELATION_DERIVES_FROM_IGNORE_POINTER,
  755. NULL );
  756. }
  757. }
  758. if ( pNode->NodeKind() != NODE_POINTER )
  759. {
  760. // probably the param or field; look down
  761. pNode = pNode->GetBasicType();
  762. }
  763. if ( pNode->NodeKind() != NODE_POINTER )
  764. {
  765. // error
  766. pNode = NULL;
  767. }
  768. else
  769. {
  770. // check things about type of pointer
  771. if ( PtrKind == PTR_UNKNOWN )
  772. {
  773. node_interface * pIntf = pOrigNode->GetMyInterfaceNode();
  774. if ( pOrigNode->NodeKind() == NODE_PARAM )
  775. PtrKind = PTR_REF;
  776. else if ( pIntf->FInSummary( ATTR_PTR_KIND ) )
  777. {
  778. pPtrAttr = (node_ptr_attr *)
  779. pIntf->GetAttribute( ATTR_PTR_KIND );
  780. PtrKind = pPtrAttr->GetPtrKind();
  781. }
  782. else
  783. {
  784. pIntf = pExprCtxt->GetCtxt()->GetInterfaceNode();
  785. if ( pIntf->FInSummary( ATTR_PTR_KIND ) )
  786. {
  787. pPtrAttr = (node_ptr_attr *)
  788. pIntf->GetAttribute( ATTR_PTR_KIND );
  789. PtrKind = pPtrAttr->GetPtrKind();
  790. }
  791. else
  792. {
  793. if (pCommand->IsSwitchDefined( SWITCH_MS_EXT ) )
  794. {
  795. PtrKind = PTR_UNIQUE;
  796. }
  797. else
  798. {
  799. PtrKind = PTR_FULL;
  800. }
  801. }
  802. }
  803. }
  804. if ( ( PtrKind == PTR_FULL ) ||
  805. ( PtrKind == PTR_UNIQUE ) )
  806. {
  807. SEM_ANALYSIS_CTXT * pCtxt = pExprCtxt->GetCtxt();
  808. pExprCtxt->SetUpFlags( EX_PTR_FULL_UNIQUE );
  809. SemError( pCtxt->GetParent(),
  810. *pCtxt,
  811. SIZE_LENGTH_SW_UNIQUE_OR_FULL,
  812. NULL );
  813. }
  814. pNode = pNode->GetBasicType();
  815. }
  816. SetType( pNode );
  817. break;
  818. }
  819. case OP_UNARY_AND:
  820. {
  821. if ( GetType() )
  822. break;
  823. node_pointer * pPtr = new node_pointer;
  824. pPtr->SetChild( GetLeft()->GetType() );
  825. SetType( pPtr );
  826. break;
  827. }
  828. case OP_PRE_INCR:
  829. case OP_PRE_DECR:
  830. case OP_POST_INCR:
  831. case OP_POST_DECR:
  832. {
  833. SetType( GetLeft()->GetType() );
  834. break;
  835. }
  836. default:
  837. {
  838. break;
  839. }
  840. } // end of switch
  841. pExprCtxt->pType = GetType();
  842. // compute the value
  843. if ( !pExprCtxt->AnyUpFlags( EX_VALUE_INVALID ) )
  844. {
  845. // tbd - update value from LeftCtxt
  846. EXPR_VALUE & LeftValue = LeftCtxt.Value();
  847. EXPR_VALUE & Result = pExprCtxt->Value();
  848. switch ( GetOperator() )
  849. {
  850. case OP_UNARY_PLUS:
  851. {
  852. Result = LeftValue;
  853. break;
  854. }
  855. case OP_UNARY_MINUS:
  856. {
  857. Result = -LeftValue;
  858. break;
  859. }
  860. case OP_UNARY_NOT:
  861. {
  862. Result = !LeftValue;
  863. break;
  864. }
  865. case OP_UNARY_COMPLEMENT:
  866. {
  867. Result = ~LeftValue;
  868. break;
  869. }
  870. case OP_UNARY_CAST:
  871. {
  872. Result = LeftValue;
  873. break;
  874. }
  875. case OP_UNARY_SIZEOF:
  876. case OP_UNARY_ALIGNOF:
  877. {
  878. Result = GetValue();
  879. break;
  880. }
  881. case OP_UNARY_INDIRECTION:
  882. case OP_UNARY_AND:
  883. case OP_PRE_INCR:
  884. case OP_PRE_DECR:
  885. case OP_POST_INCR:
  886. case OP_POST_DECR:
  887. {
  888. SetConstant( FALSE );
  889. pExprCtxt->SetUpFlags( EX_VALUE_INVALID );
  890. break;
  891. }
  892. default:
  893. {
  894. SetConstant( FALSE );
  895. Result = 0;
  896. break;
  897. }
  898. } // end of case
  899. } // end of if valid
  900. }
  901. //
  902. // determine the type of the expression
  903. //
  904. void
  905. expr_op_binary::DetermineType()
  906. {
  907. node_skl * pLeftType = NULL;
  908. node_skl * pRightType = NULL;
  909. if ( GetLeft() )
  910. pLeftType = GetLeft()->AlwaysGetType();
  911. if ( GetRight() )
  912. pRightType = GetRight()->AlwaysGetType();
  913. SetConstant( GetRight()->IsConstant() && GetLeft()->IsConstant() );
  914. ////////////////////////////////////////////////////////////////////////
  915. // do type compatibility stuff
  916. switch ( GetOperator() )
  917. {
  918. case OP_PLUS:
  919. case OP_MINUS:
  920. case OP_STAR:
  921. case OP_MOD:
  922. // gaj - we implicitly assume signed types
  923. case OP_LEFT_SHIFT:
  924. case OP_RIGHT_SHIFT:
  925. case OP_AND:
  926. case OP_OR:
  927. case OP_XOR:
  928. case OP_SLASH:
  929. {
  930. // tbd - for now, just grab one of the types
  931. if ( !pRightType ||
  932. (pLeftType &&
  933. (pLeftType->GetSize() >= pRightType->GetSize() ) ) )
  934. {
  935. SetType( pLeftType );
  936. }
  937. else if ( pRightType )
  938. {
  939. SetType( pRightType );
  940. }
  941. break;
  942. }
  943. case OP_LOGICAL_AND:
  944. case OP_LOGICAL_OR:
  945. // gaj - we implicitly assume signed types
  946. case OP_LESS:
  947. case OP_LESS_EQUAL:
  948. case OP_GREATER_EQUAL:
  949. case OP_GREATER:
  950. case OP_EQUAL:
  951. case OP_NOT_EQUAL:
  952. {
  953. node_skl * pTmpType;
  954. GetBaseTypeNode( &pTmpType, SIGN_UNDEF, SIZE_UNDEF, TYPE_BOOLEAN );
  955. SetType( pTmpType );
  956. break;
  957. }
  958. default:
  959. {
  960. break;
  961. }
  962. }
  963. }
  964. void
  965. expr_op_binary::ExprAnalyze( EXPR_CTXT * pExprCtxt )
  966. {
  967. EXPR_CTXT LeftCtxt( pExprCtxt );
  968. EXPR_CTXT RightCtxt( pExprCtxt );
  969. if ( GetLeft() )
  970. {
  971. GetLeft()->ExprAnalyze( &LeftCtxt );
  972. pExprCtxt->MergeUpFlags( &LeftCtxt );
  973. SetConstant( IsConstant() && GetLeft()->IsConstant() );
  974. }
  975. else
  976. pExprCtxt->SetUpFlags( EX_VALUE_INVALID );
  977. if ( GetRight() )
  978. {
  979. GetRight()->ExprAnalyze( &RightCtxt );
  980. pExprCtxt->MergeUpFlags( &RightCtxt );
  981. SetConstant( IsConstant() && GetRight()->IsConstant() );
  982. // check here for div by zero ( could be var / 0 )
  983. if ( !RightCtxt.AnyUpFlags( EX_VALUE_INVALID ) &&
  984. ( GetOperator() == OP_SLASH ) &&
  985. ( RightCtxt.Value() == 0 ) )
  986. {
  987. SemError( pExprCtxt->GetNode(),
  988. *(pExprCtxt->GetCtxt()),
  989. EXPR_DIV_BY_ZERO,
  990. NULL );
  991. pExprCtxt->SetUpFlags( EX_VALUE_INVALID );
  992. }
  993. }
  994. else
  995. pExprCtxt->SetUpFlags( EX_VALUE_INVALID );
  996. ////////////////////////////////////////////////////////////////////////
  997. // do type compatibility stuff
  998. switch ( GetOperator() )
  999. {
  1000. case OP_PLUS:
  1001. case OP_MINUS:
  1002. case OP_STAR:
  1003. case OP_MOD:
  1004. // gaj - we implicitly assume signed types
  1005. case OP_LEFT_SHIFT:
  1006. case OP_RIGHT_SHIFT:
  1007. case OP_AND:
  1008. case OP_OR:
  1009. case OP_XOR:
  1010. case OP_SLASH:
  1011. {
  1012. // tbd - for now, just grab the bigger of the types
  1013. if ( !RightCtxt.pType ||
  1014. (LeftCtxt.pType &&
  1015. (LeftCtxt.pType->GetSize() >= RightCtxt.pType->GetSize() ) ) )
  1016. {
  1017. pExprCtxt->pType = LeftCtxt.pType;
  1018. pExprCtxt->TypeInfo = LeftCtxt.TypeInfo;
  1019. pExprCtxt->fIntegral = LeftCtxt.fIntegral;
  1020. SetType( pExprCtxt->pType );
  1021. }
  1022. else if ( RightCtxt.pType )
  1023. {
  1024. pExprCtxt->pType = RightCtxt.pType;
  1025. pExprCtxt->TypeInfo = RightCtxt.TypeInfo;
  1026. pExprCtxt->fIntegral = RightCtxt.fIntegral;
  1027. SetType( pExprCtxt->pType );
  1028. }
  1029. break;
  1030. }
  1031. case OP_LOGICAL_AND:
  1032. case OP_LOGICAL_OR:
  1033. // gaj - we implicitly assume signed types
  1034. case OP_LESS:
  1035. case OP_LESS_EQUAL:
  1036. case OP_GREATER_EQUAL:
  1037. case OP_GREATER:
  1038. case OP_EQUAL:
  1039. case OP_NOT_EQUAL:
  1040. {
  1041. pExprCtxt->TypeInfo.TypeSize = SIZE_UNDEF;
  1042. pExprCtxt->TypeInfo.BaseType = TYPE_BOOLEAN;
  1043. pExprCtxt->TypeInfo.TypeSign = SIGN_UNDEF;
  1044. GetBaseTypeNode( &(pExprCtxt->pType ), pExprCtxt->TypeInfo );
  1045. pExprCtxt->fIntegral = TRUE;
  1046. SetType( pExprCtxt->pType );
  1047. break;
  1048. }
  1049. default:
  1050. {
  1051. break;
  1052. }
  1053. }
  1054. ////////////////////////////////////////////////////////////////////////
  1055. // compute the value
  1056. if ( !pExprCtxt->AnyUpFlags( EX_VALUE_INVALID ) )
  1057. {
  1058. // update value directly from LeftCtxt and RightCtxt
  1059. EXPR_VALUE & LeftValue = LeftCtxt.Value();
  1060. EXPR_VALUE & RightValue = RightCtxt.Value();
  1061. EXPR_VALUE & Result = pExprCtxt->Value();
  1062. switch ( GetOperator() )
  1063. {
  1064. case OP_PLUS:
  1065. {
  1066. Result = LeftValue + RightValue;
  1067. break;
  1068. }
  1069. case OP_MINUS:
  1070. {
  1071. Result = LeftValue - RightValue;
  1072. break;
  1073. }
  1074. case OP_STAR:
  1075. {
  1076. Result = LeftValue * RightValue;
  1077. break;
  1078. }
  1079. case OP_SLASH:
  1080. {
  1081. if (RightValue == 0)
  1082. Result = 0;
  1083. else
  1084. Result = LeftValue / RightValue;
  1085. break;
  1086. }
  1087. case OP_MOD:
  1088. {
  1089. Result = LeftValue % RightValue;
  1090. break;
  1091. }
  1092. case OP_LOGICAL_AND:
  1093. {
  1094. Result = LeftValue && RightValue;
  1095. break;
  1096. }
  1097. case OP_LOGICAL_OR:
  1098. {
  1099. Result = LeftValue || RightValue;
  1100. break;
  1101. }
  1102. // gaj - we implicitly assume signed types
  1103. case OP_LESS:
  1104. {
  1105. Result = LeftValue < RightValue;
  1106. break;
  1107. }
  1108. case OP_LESS_EQUAL:
  1109. {
  1110. Result = LeftValue <= RightValue;
  1111. break;
  1112. }
  1113. case OP_GREATER_EQUAL:
  1114. {
  1115. Result = LeftValue >= RightValue;
  1116. break;
  1117. }
  1118. case OP_GREATER:
  1119. {
  1120. Result = LeftValue > RightValue;
  1121. break;
  1122. }
  1123. case OP_EQUAL:
  1124. {
  1125. Result = LeftValue == RightValue;
  1126. break;
  1127. }
  1128. case OP_NOT_EQUAL:
  1129. {
  1130. Result = LeftValue != RightValue;
  1131. break;
  1132. }
  1133. // gaj - we implicitly assume signed types
  1134. case OP_LEFT_SHIFT:
  1135. {
  1136. Result = LeftValue << RightValue;
  1137. break;
  1138. }
  1139. case OP_RIGHT_SHIFT:
  1140. {
  1141. Result = LeftValue >> RightValue;
  1142. break;
  1143. }
  1144. case OP_AND:
  1145. {
  1146. Result = LeftValue & RightValue;
  1147. break;
  1148. }
  1149. case OP_OR:
  1150. {
  1151. Result = LeftValue | RightValue;
  1152. break;
  1153. }
  1154. case OP_XOR:
  1155. {
  1156. Result = LeftValue ^ RightValue;
  1157. break;
  1158. }
  1159. default:
  1160. {
  1161. Result = 0;
  1162. break;
  1163. }
  1164. }
  1165. }
  1166. }
  1167. //
  1168. // determine the type of the expression
  1169. //
  1170. void
  1171. expr_ternary::DetermineType()
  1172. {
  1173. node_skl * pLeftType = NULL;
  1174. node_skl * pRightType = NULL;
  1175. if ( GetLeft() )
  1176. pLeftType = GetLeft()->AlwaysGetType();
  1177. if ( GetRight() )
  1178. pRightType = GetRight()->AlwaysGetType();
  1179. SetConstant( GetRight()->IsConstant() && GetLeft()->IsConstant() );
  1180. if ( pLeftType )
  1181. SetType( pLeftType );
  1182. else if ( pRightType )
  1183. SetType( pRightType );
  1184. }
  1185. void
  1186. expr_ternary::ExprAnalyze( EXPR_CTXT * pExprCtxt )
  1187. {
  1188. EXPR_CTXT LeftCtxt( pExprCtxt );
  1189. EXPR_CTXT RightCtxt( pExprCtxt );
  1190. EXPR_CTXT RelCtxt( pExprCtxt );
  1191. if ( GetLeft() )
  1192. {
  1193. GetLeft()->ExprAnalyze( &LeftCtxt );
  1194. pExprCtxt->MergeUpFlags( &LeftCtxt );
  1195. SetConstant( IsConstant() && GetLeft()->IsConstant() );
  1196. }
  1197. else
  1198. pExprCtxt->SetUpFlags( EX_VALUE_INVALID );
  1199. if ( GetRight() )
  1200. {
  1201. GetRight()->ExprAnalyze( &RightCtxt );
  1202. pExprCtxt->MergeUpFlags( &RightCtxt );
  1203. SetConstant( IsConstant() && GetRight()->IsConstant() );
  1204. }
  1205. else
  1206. pExprCtxt->SetUpFlags( EX_VALUE_INVALID );
  1207. if ( GetRelational() )
  1208. {
  1209. GetRelational()->ExprAnalyze( &RelCtxt );
  1210. pExprCtxt->MergeUpFlags( &RelCtxt );
  1211. SetConstant( IsConstant() && GetRelational()->IsConstant() );
  1212. }
  1213. else
  1214. pExprCtxt->SetUpFlags( EX_VALUE_INVALID );
  1215. // tbd - get the type from the left or right of the ':'
  1216. if ( LeftCtxt.pType )
  1217. {
  1218. pExprCtxt->pType = LeftCtxt.pType;
  1219. pExprCtxt->TypeInfo = LeftCtxt.TypeInfo;
  1220. pExprCtxt->fIntegral = LeftCtxt.fIntegral;
  1221. SetType( pExprCtxt->pType );
  1222. }
  1223. else if ( RightCtxt.pType )
  1224. {
  1225. pExprCtxt->pType = RightCtxt.pType;
  1226. pExprCtxt->TypeInfo = RightCtxt.TypeInfo;
  1227. pExprCtxt->fIntegral = RightCtxt.fIntegral;
  1228. SetType( pExprCtxt->pType );
  1229. }
  1230. else
  1231. SetType( NULL );
  1232. ////////////////////////////////////////////////////////////////////////
  1233. // compute the value
  1234. if ( !pExprCtxt->AnyUpFlags( EX_VALUE_INVALID ) )
  1235. {
  1236. // update value directly from LeftCtxt and RightCtxt
  1237. EXPR_VALUE & LeftValue = LeftCtxt.Value();
  1238. EXPR_VALUE & RightValue = RightCtxt.Value();
  1239. EXPR_VALUE & RelValue = RelCtxt.Value();
  1240. EXPR_VALUE & Result = pExprCtxt->Value();
  1241. Result = (RelValue) ? LeftValue : RightValue;
  1242. }
  1243. }
  1244. //
  1245. // determine the type of the expression
  1246. //
  1247. void
  1248. expr_variable::DetermineType()
  1249. {
  1250. if ( pType->NodeKind() == NODE_FORWARD )
  1251. {
  1252. node_forward * pFwd = (node_forward *) pType;
  1253. // (permanently) resolve the forward
  1254. pType = pFwd->ResolveFDecl();
  1255. // This appears to be a better fix for the problem of unresolved
  1256. // variables in async size expressions than the NULL type pointer check
  1257. // in Fixup[Begin|Finish]ProcExpr in front\copyto.cxx. However, at this
  1258. // time we don't fully understand the implications of this change while
  1259. // the other is localized to the async_uuid interfaces only.
  1260. // e.g. ([in] long size, [in, size_is(huh)] long *p)
  1261. // 03-May-99 MikeW
  1262. /*
  1263. // if it couldn't be resolved, put back the forward
  1264. if ( !pType )
  1265. {
  1266. pType = pFwd;
  1267. }
  1268. */
  1269. }
  1270. }
  1271. void
  1272. expr_variable::ExprAnalyze( EXPR_CTXT * pExprCtxt )
  1273. {
  1274. pExprCtxt->SetUpFlags( EX_VALUE_INVALID );
  1275. pExprCtxt->SetUpFlags( EX_NON_NUMERIC );
  1276. if ( !pType )
  1277. {
  1278. pExprCtxt->SetUpFlags( EX_UNSAT_FWD );
  1279. return;
  1280. }
  1281. if ( pType->NodeKind() == NODE_FORWARD )
  1282. {
  1283. node_forward * pFwd = (node_forward *) pType;
  1284. // (permanently) resolve the forward
  1285. pType = pFwd->ResolveFDecl();
  1286. // if it couldn't be resolved, put back the forward
  1287. if ( !pType )
  1288. {
  1289. pExprCtxt->SetUpFlags( EX_UNSAT_FWD );
  1290. pExprCtxt->TypeInfo.TypeSize = SIZE_UNDEF;
  1291. pExprCtxt->TypeInfo.BaseType = TYPE_INT;
  1292. pExprCtxt->TypeInfo.TypeSign = SIGN_SIGNED;
  1293. GetBaseTypeNode( &pType, pExprCtxt->TypeInfo);
  1294. pExprCtxt->pType = pType;
  1295. return;
  1296. }
  1297. }
  1298. // do type compatibility stuff
  1299. pExprCtxt->pType = pType;
  1300. pExprCtxt->TypeInfo.TypeSize = SIZE_UNDEF;
  1301. pExprCtxt->TypeInfo.BaseType = TYPE_UNDEF;
  1302. pExprCtxt->TypeInfo.TypeSign = SIGN_UNDEF;
  1303. pExprCtxt->fIntegral = FALSE; // for now...
  1304. if ( ( pType->NodeKind() == NODE_PARAM ) &&
  1305. pType->FInSummary( ATTR_OUT ) &&
  1306. !pType->FInSummary( ATTR_IN ) )
  1307. pExprCtxt->SetUpFlags( EX_OUT_ONLY_PARAM );
  1308. }
  1309. //
  1310. // determine the type of the expression
  1311. //
  1312. void
  1313. expr_named_constant::DetermineType()
  1314. {
  1315. // do type compatibility stuff
  1316. if ( !GetType() )
  1317. {
  1318. SetType( GetExpr()->AlwaysGetType() );
  1319. }
  1320. }
  1321. void
  1322. expr_named_constant::ExprAnalyze( EXPR_CTXT * pExprCtxt )
  1323. {
  1324. // named constants shouldn't be folded away
  1325. pExprCtxt->SetUpFlags( EX_NON_NUMERIC );
  1326. // update value
  1327. pExprCtxt->Value() = GetValue();
  1328. // do type compatibility stuff
  1329. if ( GetType() )
  1330. {
  1331. pExprCtxt->pType = GetType();
  1332. pExprCtxt->TypeInfo.TypeSize = SIZE_UNDEF;
  1333. pExprCtxt->TypeInfo.BaseType = TYPE_UNDEF;
  1334. pExprCtxt->TypeInfo.TypeSign = SIGN_UNDEF;
  1335. pExprCtxt->fIntegral = FALSE; // for now...
  1336. }
  1337. else
  1338. {
  1339. EXPR_CTXT LeftCtxt( pExprCtxt );
  1340. expr_node * pExpr = GetExpr();
  1341. pExpr->ExprAnalyze( &LeftCtxt );
  1342. pExprCtxt->pType = LeftCtxt.pType;
  1343. pExprCtxt->TypeInfo = LeftCtxt.TypeInfo;
  1344. pExprCtxt->fIntegral = LeftCtxt.fIntegral;
  1345. SetType( pExprCtxt->pType );
  1346. }
  1347. }
  1348. //
  1349. // determine the type of the expression
  1350. //
  1351. void
  1352. expr_constant::DetermineType()
  1353. {
  1354. node_skl* pNewType = 0;
  1355. // do type compatibility stuff
  1356. if ( GetType() )
  1357. return;
  1358. // do type compatibility stuff
  1359. switch (Format)
  1360. {
  1361. case VALUE_TYPE_STRING:
  1362. {
  1363. node_skl * pBottomType;
  1364. GetBaseTypeNode( &pBottomType,
  1365. SIGN_SIGNED,
  1366. SIZE_CHAR,
  1367. TYPE_INT );
  1368. pNewType = new node_pointer;
  1369. pNewType->SetChild( pBottomType );
  1370. break;
  1371. }
  1372. case VALUE_TYPE_WSTRING:
  1373. {
  1374. node_skl * pBottomType;
  1375. SymKey SKey( "wchar_t", NAME_DEF );
  1376. pBottomType = pBaseSymTbl->SymSearch( SKey );
  1377. pNewType = new node_pointer;
  1378. pNewType->SetChild( pBottomType );
  1379. break;
  1380. }
  1381. case VALUE_TYPE_CHAR:
  1382. {
  1383. GetBaseTypeNode( &pNewType,
  1384. SIGN_SIGNED,
  1385. SIZE_CHAR,
  1386. TYPE_INT );
  1387. break;
  1388. }
  1389. case VALUE_TYPE_WCHAR:
  1390. {
  1391. SymKey SKey( "wchar_t", NAME_DEF );
  1392. pNewType = pBaseSymTbl->SymSearch( SKey );
  1393. break;
  1394. }
  1395. case VALUE_TYPE_NUMERIC:
  1396. case VALUE_TYPE_HEX:
  1397. case VALUE_TYPE_OCTAL:
  1398. {
  1399. GetBaseTypeNode( &pNewType,
  1400. SIGN_SIGNED,
  1401. SIZE_UNDEF,
  1402. TYPE_INT );
  1403. break;
  1404. }
  1405. case VALUE_TYPE_NUMERIC_U:
  1406. case VALUE_TYPE_HEX_U:
  1407. case VALUE_TYPE_OCTAL_U:
  1408. {
  1409. GetBaseTypeNode( &pNewType,
  1410. SIGN_UNSIGNED,
  1411. SIZE_SHORT,
  1412. TYPE_INT );
  1413. break;
  1414. }
  1415. case VALUE_TYPE_NUMERIC_LONG:
  1416. case VALUE_TYPE_HEX_LONG:
  1417. case VALUE_TYPE_OCTAL_LONG:
  1418. {
  1419. GetBaseTypeNode( &pNewType,
  1420. SIGN_SIGNED,
  1421. SIZE_LONG,
  1422. TYPE_INT );
  1423. break;
  1424. }
  1425. case VALUE_TYPE_NUMERIC_ULONG:
  1426. case VALUE_TYPE_HEX_ULONG:
  1427. case VALUE_TYPE_OCTAL_ULONG:
  1428. {
  1429. GetBaseTypeNode( &pNewType,
  1430. SIGN_UNSIGNED,
  1431. SIZE_LONG,
  1432. TYPE_INT );
  1433. break;
  1434. }
  1435. case VALUE_TYPE_BOOL:
  1436. {
  1437. GetBaseTypeNode( &pNewType,
  1438. SIGN_UNDEF,
  1439. SIZE_UNDEF,
  1440. TYPE_BOOLEAN );
  1441. break;
  1442. }
  1443. case VALUE_TYPE_FLOAT:
  1444. {
  1445. GetBaseTypeNode( &pNewType,
  1446. SIGN_UNDEF,
  1447. SIZE_UNDEF,
  1448. TYPE_FLOAT );
  1449. break;
  1450. }
  1451. case VALUE_TYPE_DOUBLE:
  1452. {
  1453. GetBaseTypeNode( &pNewType,
  1454. SIGN_UNDEF,
  1455. SIZE_UNDEF,
  1456. TYPE_DOUBLE );
  1457. break;
  1458. }
  1459. default:
  1460. break;
  1461. }
  1462. SetType( pNewType );
  1463. }
  1464. void
  1465. expr_constant::ExprAnalyze( EXPR_CTXT * pExprCtxt )
  1466. {
  1467. // update value
  1468. pExprCtxt->Value() = GetValue();
  1469. if ( GetType() )
  1470. {
  1471. pExprCtxt->pType = GetType();
  1472. pExprCtxt->TypeInfo.TypeSize = SIZE_UNDEF;
  1473. pExprCtxt->TypeInfo.BaseType = TYPE_UNDEF;
  1474. pExprCtxt->TypeInfo.TypeSign = SIGN_UNDEF;
  1475. pExprCtxt->fIntegral = FALSE; // for now...
  1476. return;
  1477. }
  1478. // do type compatibility stuff
  1479. switch (Format)
  1480. {
  1481. case VALUE_TYPE_STRING:
  1482. {
  1483. node_skl * pBottomType;
  1484. GetBaseTypeNode( &pBottomType,
  1485. SIGN_SIGNED,
  1486. SIZE_CHAR,
  1487. TYPE_INT );
  1488. pExprCtxt->pType = new node_pointer;
  1489. pExprCtxt->pType->SetChild( pBottomType );
  1490. break;
  1491. }
  1492. case VALUE_TYPE_WSTRING:
  1493. {
  1494. node_skl * pBottomType;
  1495. SymKey SKey( "wchar_t", NAME_DEF );
  1496. pBottomType = pBaseSymTbl->SymSearch( SKey );
  1497. pExprCtxt->pType = new node_pointer;
  1498. pExprCtxt->pType->SetChild( pBottomType );
  1499. break;
  1500. }
  1501. case VALUE_TYPE_CHAR:
  1502. {
  1503. GetBaseTypeNode( &pExprCtxt->pType,
  1504. SIGN_SIGNED,
  1505. SIZE_CHAR,
  1506. TYPE_INT );
  1507. break;
  1508. }
  1509. case VALUE_TYPE_WCHAR:
  1510. {
  1511. SymKey SKey( "wchar_t", NAME_DEF );
  1512. pExprCtxt->pType = pBaseSymTbl->SymSearch( SKey );
  1513. break;
  1514. }
  1515. case VALUE_TYPE_NUMERIC:
  1516. case VALUE_TYPE_HEX:
  1517. case VALUE_TYPE_OCTAL:
  1518. {
  1519. short RealSize = SIZE_HYPER;
  1520. __int64 val = GetValue();
  1521. if ( (val <= 127) && (val >= -128 ) )
  1522. RealSize = SIZE_CHAR;
  1523. else if ( (val <= _I16_MAX) && (val >= _I16_MIN ) )
  1524. RealSize = SIZE_SHORT;
  1525. else if ( (val <= _I32_MAX) && (val >= _I32_MIN ) )
  1526. RealSize = SIZE_LONG;
  1527. GetBaseTypeNode( &pExprCtxt->pType,
  1528. SIGN_SIGNED,
  1529. RealSize,
  1530. TYPE_INT );
  1531. break;
  1532. }
  1533. case VALUE_TYPE_NUMERIC_U:
  1534. case VALUE_TYPE_HEX_U:
  1535. case VALUE_TYPE_OCTAL_U:
  1536. {
  1537. short RealSize = SIZE_LONG;
  1538. unsigned long val = (unsigned long) GetValue();
  1539. if ( val <= 255 )
  1540. RealSize = SIZE_CHAR;
  1541. else if ( val <= 65536 )
  1542. RealSize = SIZE_SHORT;
  1543. GetBaseTypeNode( &pExprCtxt->pType,
  1544. SIGN_UNSIGNED,
  1545. RealSize,
  1546. TYPE_INT );
  1547. break;
  1548. }
  1549. case VALUE_TYPE_NUMERIC_LONG:
  1550. case VALUE_TYPE_HEX_LONG:
  1551. case VALUE_TYPE_OCTAL_LONG:
  1552. {
  1553. GetBaseTypeNode( &pExprCtxt->pType,
  1554. SIGN_SIGNED,
  1555. SIZE_LONG,
  1556. TYPE_INT );
  1557. break;
  1558. }
  1559. case VALUE_TYPE_NUMERIC_ULONG:
  1560. case VALUE_TYPE_HEX_ULONG:
  1561. case VALUE_TYPE_OCTAL_ULONG:
  1562. {
  1563. GetBaseTypeNode( &pExprCtxt->pType,
  1564. SIGN_UNSIGNED,
  1565. SIZE_LONG,
  1566. TYPE_INT );
  1567. break;
  1568. }
  1569. case VALUE_TYPE_BOOL:
  1570. {
  1571. GetBaseTypeNode( &pExprCtxt->pType,
  1572. SIGN_UNDEF,
  1573. SIZE_UNDEF,
  1574. TYPE_BOOLEAN );
  1575. break;
  1576. }
  1577. case VALUE_TYPE_FLOAT:
  1578. {
  1579. GetBaseTypeNode( &pExprCtxt->pType,
  1580. SIGN_UNDEF,
  1581. SIZE_UNDEF,
  1582. TYPE_FLOAT );
  1583. break;
  1584. }
  1585. case VALUE_TYPE_DOUBLE:
  1586. {
  1587. GetBaseTypeNode( &pExprCtxt->pType,
  1588. SIGN_UNDEF,
  1589. SIZE_UNDEF,
  1590. TYPE_DOUBLE );
  1591. break;
  1592. }
  1593. default:
  1594. break;
  1595. }
  1596. SetType( pExprCtxt->pType );
  1597. }
  1598. //
  1599. // determine the type of the expression
  1600. //
  1601. void
  1602. expr_init_list::DetermineType()
  1603. {
  1604. SetType( pExpr->AlwaysGetType() );
  1605. }
  1606. void
  1607. expr_init_list::ExprAnalyze( EXPR_CTXT * pExprCtxt )
  1608. {
  1609. // tbd - for now only process first element
  1610. pExpr->ExprAnalyze( pExprCtxt );
  1611. SetConstant( pExpr->IsConstant() );
  1612. SetType( pExpr->GetType() );
  1613. }
  1614. short
  1615. expr_op_binary::MakeListOfDerefedVars( ITERATOR& List )
  1616. {
  1617. if( GetLeft() )
  1618. GetLeft()->MakeListOfDerefedVars( List );
  1619. if( GetRight() )
  1620. GetRight()->MakeListOfDerefedVars( List );
  1621. return (short) ITERATOR_GETCOUNT( List );
  1622. }
  1623. short
  1624. expr_u_deref::MakeListOfDerefedVars( ITERATOR& List )
  1625. {
  1626. expr_node * pLeft = GetLeft();
  1627. if( !pLeft ) return 0;
  1628. if( pLeft->IsAVariable() )
  1629. ITERATOR_INSERT( List, pLeft );
  1630. else if( pLeft->GetOperator() == OP_UNARY_INDIRECTION )
  1631. pLeft->MakeListOfDerefedVars( List );
  1632. return ITERATOR_GETCOUNT( List );
  1633. }
  1634. BOOL
  1635. expr_b_arithmetic::GetExprValue( SExprValue& v )
  1636. {
  1637. SExprValue LeftValue = {VALUE_TYPE_UNDEFINED, 0};
  1638. SExprValue RightValue = {VALUE_TYPE_UNDEFINED, 0};
  1639. BOOL fSuccess;
  1640. fSuccess = GetLeft()->GetExprValue( LeftValue ) &&
  1641. GetRight()->GetExprValue( RightValue ) &&
  1642. (LeftValue.format == VALUE_TYPE_DOUBLE || LeftValue.format == VALUE_TYPE_DOUBLE) &&
  1643. (RightValue.format == VALUE_TYPE_DOUBLE || RightValue.format == VALUE_TYPE_DOUBLE);
  1644. if (fSuccess)
  1645. {
  1646. v.format = LeftValue.format == VALUE_TYPE_DOUBLE || RightValue.format == VALUE_TYPE_DOUBLE ?
  1647. VALUE_TYPE_DOUBLE : VALUE_TYPE_FLOAT ;
  1648. double l = LeftValue.format == VALUE_TYPE_DOUBLE ? LeftValue.d : LeftValue.f ;
  1649. double r = RightValue.format == VALUE_TYPE_DOUBLE ? RightValue.d : RightValue.f ;
  1650. switch ( GetOperator() )
  1651. {
  1652. case OP_PLUS:
  1653. l += r;
  1654. break;
  1655. case OP_MINUS:
  1656. l -= r;
  1657. break;
  1658. case OP_STAR:
  1659. l *= r;
  1660. break;
  1661. case OP_SLASH:
  1662. __try
  1663. {
  1664. l /= r;
  1665. }
  1666. __except (EXCEPTION_EXECUTE_HANDLER)
  1667. {
  1668. fSuccess = FALSE;
  1669. }
  1670. break;
  1671. }
  1672. if (v.format == VALUE_TYPE_FLOAT)
  1673. {
  1674. v.f = (float) l;
  1675. }
  1676. else if (v.format == VALUE_TYPE_DOUBLE)
  1677. {
  1678. v.d = l;
  1679. }
  1680. }
  1681. return fSuccess;
  1682. }
  1683. BOOL
  1684. expr_u_arithmetic::GetExprValue( SExprValue& v )
  1685. {
  1686. SExprValue LeftValue = {VALUE_TYPE_UNDEFINED, 0};
  1687. BOOL fSuccess = GetLeft()->GetExprValue( LeftValue );
  1688. if (fSuccess)
  1689. {
  1690. v = LeftValue;
  1691. if (GetOperator() == OP_UNARY_MINUS)
  1692. {
  1693. switch ( v.format )
  1694. {
  1695. case VALUE_TYPE_FLOAT:
  1696. v.f = -LeftValue.f;
  1697. break;
  1698. case VALUE_TYPE_DOUBLE:
  1699. v.d = -LeftValue.d;
  1700. break;
  1701. default:
  1702. fSuccess = FALSE;
  1703. break;
  1704. }
  1705. }
  1706. }
  1707. return fSuccess;
  1708. }
  1709. void
  1710. expr_node::CopyTo( expr_node* lhs )
  1711. {
  1712. DetermineType();
  1713. lhs->pType = pType;
  1714. lhs->fFloatExpr = fFloatExpr;
  1715. lhs->fConstant = fConstant;
  1716. }
  1717. void
  1718. expr_constant::CopyTo( expr_node* pExpr )
  1719. {
  1720. expr_constant* lhs = (expr_constant*) pExpr;
  1721. expr_node::CopyTo( lhs );
  1722. lhs->Format = Format;
  1723. lhs->Value = Value;
  1724. }
  1725. void
  1726. expr_init_list::CopyTo( expr_node* pExpr )
  1727. {
  1728. expr_init_list* lhs = (expr_init_list*) pExpr;
  1729. expr_node::CopyTo( lhs );
  1730. lhs->pSibling = (expr_init_list*)pSibling->Clone();
  1731. pSibling->CopyTo(lhs->pSibling);
  1732. lhs->pExpr = pExpr->Clone();
  1733. pExpr->CopyTo(lhs->pExpr);
  1734. }
  1735. void
  1736. expr_operator::CopyTo( expr_node* pExpr )
  1737. {
  1738. expr_operator* lhs = (expr_operator*) pExpr;
  1739. expr_node::CopyTo( lhs );
  1740. lhs->Operator = Operator;
  1741. }
  1742. void
  1743. expr_op_binary::CopyTo( expr_node* pExpr )
  1744. {
  1745. expr_op_binary* lhs = (expr_op_binary*) pExpr;
  1746. expr_operator::CopyTo( lhs );
  1747. lhs->pLeft = pLeft->Clone();
  1748. pLeft->CopyTo(lhs->pLeft);
  1749. lhs->pRight = pRight->Clone();
  1750. pRight->CopyTo(lhs->pRight);
  1751. }
  1752. void
  1753. expr_param::CopyTo( expr_node* pExpr )
  1754. {
  1755. expr_param* lhs = (expr_param*) pExpr;
  1756. expr_op_binary::CopyTo( lhs );
  1757. _STRDUP( lhs->pName, pName );
  1758. }
  1759. void
  1760. expr_op_unary::CopyTo( expr_node* pExpr )
  1761. {
  1762. expr_op_unary* lhs = (expr_op_unary*) pExpr;
  1763. expr_operator::CopyTo( lhs );
  1764. lhs->pLeft = pLeft->Clone();
  1765. pLeft->CopyTo(lhs->pLeft);
  1766. }
  1767. void
  1768. expr_cast::CopyTo( expr_node* pExpr )
  1769. {
  1770. expr_cast* lhs = (expr_cast*) pExpr;
  1771. expr_op_unary::CopyTo( lhs );
  1772. lhs->fEmitModifiers = fEmitModifiers;
  1773. lhs->pCastType = pCastType;
  1774. }
  1775. void
  1776. expr_proc_call::CopyTo( expr_node* pExpr )
  1777. {
  1778. expr_proc_call* lhs = (expr_proc_call*) pExpr;
  1779. expr_op_unary::CopyTo( lhs );
  1780. _STRDUP( lhs->pName, pName );
  1781. lhs->NoOfParams = NoOfParams;
  1782. }
  1783. void
  1784. expr_sizeof::CopyTo( expr_node* pExpr )
  1785. {
  1786. expr_sizeof* lhs = (expr_sizeof*) pExpr;
  1787. expr_op_unary::CopyTo( lhs );
  1788. lhs->pType = pType;
  1789. }
  1790. void
  1791. expr_alignof::CopyTo( expr_node* pExpr )
  1792. {
  1793. expr_alignof* lhs = (expr_alignof*) pExpr;
  1794. expr_op_unary::CopyTo( lhs );
  1795. lhs->pType = pType;
  1796. }
  1797. void
  1798. expr_ternary::CopyTo( expr_node* pExpr )
  1799. {
  1800. expr_ternary* lhs = (expr_ternary*) pExpr;
  1801. expr_operator::CopyTo( lhs );
  1802. lhs->pLeft = pLeft->Clone();
  1803. pLeft->CopyTo(lhs->pLeft);
  1804. lhs->pRight = pRight->Clone();
  1805. pRight->CopyTo(lhs->pRight);
  1806. lhs->pRelational = pRelational->Clone();
  1807. pRelational->CopyTo(lhs->pRelational);
  1808. }
  1809. void
  1810. expr_variable::CopyTo( expr_node* pExpr )
  1811. {
  1812. expr_variable* lhs = (expr_variable*) pExpr;
  1813. expr_node::CopyTo( lhs );
  1814. _STRDUP( lhs->pIDName, pIDName );
  1815. _STRDUP( lhs->pPrefix, pPrefix );
  1816. }
  1817. expr_node*
  1818. expr_ternary::Clone()
  1819. {
  1820. return new expr_ternary(OP_ILLEGAL,0,0,0);
  1821. }