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.

1146 lines
28 KiB

  1. /*****************************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1987-1999 **/
  4. /*****************************************************************************/
  5. /*****************************************************************************
  6. File : gramutil.cxx
  7. Title : grammar utility routines
  8. Description : contains associated routines for the grammar (pass 1)
  9. History :
  10. 05-Aug-1991 VibhasC Created
  11. *****************************************************************************/
  12. #pragma warning ( disable : 4514 4710 )
  13. /****************************************************************************
  14. * include files
  15. ***************************************************************************/
  16. #include "nulldefs.h"
  17. extern "C" {
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <string.h>
  21. }
  22. #include "lextable.hxx"
  23. #include "allnodes.hxx"
  24. #include "gramutil.hxx"
  25. #include "filehndl.hxx"
  26. #include "idict.hxx"
  27. #include "control.hxx"
  28. #include "cmdana.hxx"
  29. /****************************************************************************
  30. * external data
  31. ***************************************************************************/
  32. extern CMD_ARG * pCommand;
  33. extern class _nfa_info * pImportCntrl;
  34. extern pre_type_db * pPreAllocTypes;
  35. extern SymTable * pBaseSymTbl;
  36. extern node_error * pErrorTypeNode;
  37. extern short CompileMode;
  38. extern node_e_attr * pErrorAttrNode;
  39. extern CCONTROL * pCompiler;
  40. extern LexTable * pMidlLexTable;
  41. extern IINFODICT * pInterfaceInfoDict;
  42. extern short ImportLevel;
  43. extern node_e_status_t * pError_status_t;
  44. extern IDICT * pInterfaceDict;
  45. /****************************************************************************
  46. * external functions
  47. ***************************************************************************/
  48. extern char * GetTypeName( short );
  49. extern STATUS_T GetBaseTypeNode( node_skl **, short, short, short);
  50. extern ATTRLIST GenerateFieldAttribute(ATTR_T, expr_list *);
  51. extern char * GetExpectedSyntax( char *, short );
  52. extern int GetExpectedChar( short );
  53. extern void CheckGlobalNamesClash( SymKey );
  54. extern void NormalizeString( char* szSrc, char* szNrm );
  55. /****************************************************************************
  56. * local functions
  57. ***************************************************************************/
  58. /****************************************************************************
  59. * local definitions
  60. ***************************************************************************/
  61. //
  62. // the parse stack
  63. //
  64. lextype_t yyv[YYMAXDEPTH]; /* where the values are stored */
  65. short yys[YYMAXDEPTH]; /* the parse stack */
  66. struct pre_init
  67. {
  68. unsigned short TypeSpec;
  69. NODE_T NodeType;
  70. ATTR_T Attr; // signed or unsigned
  71. ATTR_T Attr2; // __w64
  72. char * pSymName;
  73. };
  74. struct pre_init PreInitArray[ PRE_TYPE_DB_SIZE ] =
  75. {
  76. { /** float **/
  77. MAKE_TYPE_SPEC( SIGN_UNDEF, SIZE_UNDEF, TYPE_FLOAT, ATT_NONE )
  78. ,NODE_FLOAT
  79. ,(ATTR_T)ATTR_NONE
  80. ,(ATTR_T)ATTR_NONE
  81. ,"float"
  82. }
  83. ,{ /** double **/
  84. MAKE_TYPE_SPEC( SIGN_UNDEF, SIZE_UNDEF, TYPE_DOUBLE, ATT_NONE )
  85. ,NODE_DOUBLE
  86. ,(ATTR_T)ATTR_NONE
  87. ,(ATTR_T)ATTR_NONE
  88. ,"double"
  89. }
  90. ,{ /** __float80 **/
  91. MAKE_TYPE_SPEC( SIGN_UNDEF, SIZE_UNDEF, TYPE_FLOAT80, ATT_NONE )
  92. ,NODE_FLOAT80
  93. ,(ATTR_T)ATTR_NONE
  94. ,(ATTR_T)ATTR_NONE
  95. ,"__float80"
  96. }
  97. ,{ /** __float128 **/
  98. MAKE_TYPE_SPEC( SIGN_UNDEF, SIZE_UNDEF, TYPE_FLOAT128, ATT_NONE )
  99. ,NODE_FLOAT128
  100. ,(ATTR_T)ATTR_NONE
  101. ,(ATTR_T)ATTR_NONE
  102. ,"__float128"
  103. }
  104. ,{ /** signed hyper **/
  105. MAKE_TYPE_SPEC( SIGN_SIGNED, SIZE_HYPER, TYPE_INT, ATT_NONE )
  106. ,NODE_HYPER
  107. ,(ATTR_T)ATTR_NONE
  108. ,(ATTR_T)ATTR_NONE
  109. ,"hyper"
  110. }
  111. ,{ /** unsigned hyper **/
  112. MAKE_TYPE_SPEC( SIGN_UNSIGNED, SIZE_HYPER, TYPE_INT, ATT_NONE )
  113. ,NODE_HYPER
  114. ,(ATTR_T)ATTR_UNSIGNED
  115. ,(ATTR_T)ATTR_NONE
  116. ,"hyper"
  117. }
  118. ,{ /** signed int32 **/
  119. MAKE_TYPE_SPEC( SIGN_SIGNED, SIZE_INT32, TYPE_INT, ATT_NONE )
  120. ,NODE_INT32
  121. ,(ATTR_T)ATTR_NONE
  122. ,(ATTR_T)ATTR_NONE
  123. ,"__int32"
  124. }
  125. ,{ /** unsigned int32 **/
  126. MAKE_TYPE_SPEC( SIGN_UNSIGNED, SIZE_INT32, TYPE_INT, ATT_NONE )
  127. ,NODE_INT32
  128. ,(ATTR_T)ATTR_UNSIGNED
  129. ,(ATTR_T)ATTR_NONE
  130. ,"__int32"
  131. }
  132. ,{ /** signed int3264 **/
  133. MAKE_TYPE_SPEC( SIGN_SIGNED, SIZE_INT3264, TYPE_INT, ATT_NONE )
  134. ,NODE_INT3264
  135. ,(ATTR_T)ATTR_NONE
  136. ,(ATTR_T)ATTR_NONE
  137. ,"__int3264"
  138. }
  139. ,{ /** unsigned int3264 **/
  140. MAKE_TYPE_SPEC( SIGN_UNSIGNED, SIZE_INT3264, TYPE_INT, ATT_NONE )
  141. ,NODE_INT3264
  142. ,(ATTR_T)ATTR_UNSIGNED
  143. ,(ATTR_T)ATTR_NONE
  144. ,"__int3264"
  145. }
  146. ,{ /** signed int64 **/
  147. MAKE_TYPE_SPEC( SIGN_SIGNED, SIZE_INT64, TYPE_INT, ATT_NONE )
  148. ,NODE_INT64
  149. ,(ATTR_T)ATTR_NONE
  150. ,(ATTR_T)ATTR_NONE
  151. ,"__int64"
  152. }
  153. ,{ /** unsigned int64 **/
  154. MAKE_TYPE_SPEC( SIGN_UNSIGNED, SIZE_INT64, TYPE_INT, ATT_NONE )
  155. ,NODE_INT64
  156. ,(ATTR_T)ATTR_UNSIGNED
  157. ,(ATTR_T)ATTR_NONE
  158. ,"__int64"
  159. }
  160. ,{ /** signed int128 **/
  161. MAKE_TYPE_SPEC( SIGN_SIGNED, SIZE_INT128, TYPE_INT, ATT_NONE )
  162. ,NODE_INT128
  163. ,(ATTR_T)ATTR_NONE
  164. ,(ATTR_T)ATTR_NONE
  165. ,"__int128"
  166. }
  167. ,{ /** unsigned int128 **/
  168. MAKE_TYPE_SPEC( SIGN_UNSIGNED, SIZE_INT128, TYPE_INT, ATT_NONE )
  169. ,NODE_INT128
  170. ,(ATTR_T)ATTR_UNSIGNED
  171. ,(ATTR_T)ATTR_NONE
  172. ,"__int128"
  173. }
  174. ,{ /** signed long **/
  175. MAKE_TYPE_SPEC( SIGN_SIGNED, SIZE_LONG, TYPE_INT, ATT_NONE )
  176. ,NODE_LONG
  177. ,(ATTR_T)ATTR_NONE
  178. ,(ATTR_T)ATTR_NONE
  179. ,"long"
  180. }
  181. ,{ /** unsigned long **/
  182. MAKE_TYPE_SPEC( SIGN_UNSIGNED, SIZE_LONG, TYPE_INT, ATT_NONE )
  183. ,NODE_LONG
  184. ,(ATTR_T)ATTR_UNSIGNED
  185. ,(ATTR_T)ATTR_NONE
  186. ,"long"
  187. }
  188. ,{ /** signed int **/
  189. MAKE_TYPE_SPEC( SIGN_SIGNED, SIZE_UNDEF, TYPE_INT, ATT_NONE )
  190. ,NODE_INT
  191. ,(ATTR_T)ATTR_NONE
  192. ,(ATTR_T)ATTR_NONE
  193. ,"int"
  194. }
  195. ,{ /** unsigned int **/
  196. MAKE_TYPE_SPEC( SIGN_UNSIGNED, SIZE_UNDEF, TYPE_INT, ATT_NONE )
  197. ,NODE_INT
  198. ,(ATTR_T)ATTR_UNSIGNED
  199. ,(ATTR_T)ATTR_NONE
  200. ,"int"
  201. }
  202. ,{ /** __w64 signed long **/
  203. MAKE_TYPE_SPEC( SIGN_SIGNED, SIZE_LONG, TYPE_INT, ATT_W64 )
  204. ,NODE_LONG
  205. ,(ATTR_T)ATTR_NONE
  206. ,(ATTR_T)ATTR_W64
  207. ,"long"
  208. }
  209. ,{ /** __w64 unsigned long **/
  210. MAKE_TYPE_SPEC( SIGN_UNSIGNED, SIZE_LONG, TYPE_INT, ATT_W64 )
  211. ,NODE_LONG
  212. ,(ATTR_T)ATTR_UNSIGNED
  213. ,(ATTR_T)ATTR_W64
  214. ,"long"
  215. }
  216. ,{ /** __w64 signed int **/
  217. MAKE_TYPE_SPEC( SIGN_SIGNED, SIZE_UNDEF, TYPE_INT, ATT_W64 )
  218. ,NODE_INT
  219. ,(ATTR_T)ATTR_NONE
  220. ,(ATTR_T)ATTR_W64
  221. ,"int"
  222. }
  223. ,{ /** __w64 unsigned int **/
  224. MAKE_TYPE_SPEC( SIGN_UNSIGNED, SIZE_UNDEF, TYPE_INT, ATT_W64 )
  225. ,NODE_INT
  226. ,(ATTR_T)ATTR_UNSIGNED
  227. ,(ATTR_T)ATTR_W64
  228. ,"int"
  229. }
  230. ,{ /** signed short **/
  231. MAKE_TYPE_SPEC( SIGN_SIGNED, SIZE_SHORT, TYPE_INT, ATT_NONE )
  232. ,NODE_SHORT
  233. ,(ATTR_T)ATTR_NONE
  234. ,(ATTR_T)ATTR_NONE
  235. ,"short"
  236. }
  237. ,{ /** unsigned short **/
  238. MAKE_TYPE_SPEC( SIGN_UNSIGNED, SIZE_SHORT, TYPE_INT, ATT_NONE )
  239. ,NODE_SHORT
  240. ,(ATTR_T)ATTR_UNSIGNED
  241. ,(ATTR_T)ATTR_NONE
  242. ,"short"
  243. }
  244. ,{ /** signed small **/
  245. MAKE_TYPE_SPEC( SIGN_SIGNED, SIZE_SMALL, TYPE_INT, ATT_NONE )
  246. ,NODE_SMALL
  247. ,(ATTR_T)ATTR_SIGNED
  248. ,(ATTR_T)ATTR_NONE
  249. ,"small"
  250. }
  251. ,{ /** small **//** NOTE : SMALL W/O SIGN IS A SPECIAL HACK FOR THE BACKEND **/
  252. MAKE_TYPE_SPEC( SIGN_UNDEF, SIZE_SMALL, TYPE_INT, ATT_NONE )
  253. ,NODE_SMALL
  254. ,(ATTR_T)ATTR_NONE
  255. ,(ATTR_T)ATTR_NONE
  256. ,"small"
  257. }
  258. ,{ /** unsigned small **/
  259. MAKE_TYPE_SPEC( SIGN_UNSIGNED, SIZE_SMALL, TYPE_INT, ATT_NONE )
  260. ,NODE_SMALL
  261. ,(ATTR_T)ATTR_UNSIGNED
  262. ,(ATTR_T)ATTR_NONE
  263. ,"small"
  264. }
  265. ,{ /** signed char **/
  266. MAKE_TYPE_SPEC( SIGN_SIGNED, SIZE_CHAR, TYPE_INT, ATT_NONE )
  267. ,NODE_CHAR
  268. ,(ATTR_T)ATTR_SIGNED
  269. ,(ATTR_T)ATTR_NONE
  270. ,"char"
  271. }
  272. ,{ /** plain char **/
  273. MAKE_TYPE_SPEC( SIGN_UNDEF, SIZE_CHAR, TYPE_INT, ATT_NONE )
  274. ,NODE_CHAR
  275. ,(ATTR_T)ATTR_NONE
  276. ,(ATTR_T)ATTR_NONE
  277. ,"char"
  278. }
  279. ,{ /** unsigned char **/
  280. MAKE_TYPE_SPEC( SIGN_UNSIGNED, SIZE_CHAR, TYPE_INT, ATT_NONE )
  281. ,NODE_CHAR
  282. ,(ATTR_T)ATTR_UNSIGNED
  283. ,(ATTR_T)ATTR_NONE
  284. ,"char"
  285. }
  286. ,{ /** boolean **/
  287. MAKE_TYPE_SPEC( SIGN_UNDEF, SIZE_UNDEF, TYPE_BOOLEAN, ATT_NONE )
  288. ,NODE_BOOLEAN
  289. ,(ATTR_T)ATTR_NONE
  290. ,(ATTR_T)ATTR_NONE
  291. ,"boolean"
  292. }
  293. ,{ /** byte **/
  294. MAKE_TYPE_SPEC( SIGN_UNDEF, SIZE_UNDEF, TYPE_BYTE, ATT_NONE )
  295. ,NODE_BYTE
  296. ,(ATTR_T)ATTR_NONE
  297. ,(ATTR_T)ATTR_NONE
  298. ,"byte"
  299. }
  300. ,{ /** void **/
  301. MAKE_TYPE_SPEC( SIGN_UNDEF, SIZE_UNDEF, TYPE_VOID, ATT_NONE )
  302. ,NODE_VOID
  303. ,(ATTR_T)ATTR_NONE
  304. ,(ATTR_T)ATTR_NONE
  305. ,"void"
  306. }
  307. ,{ /** handle_t **/
  308. MAKE_TYPE_SPEC( SIGN_UNDEF, SIZE_UNDEF, TYPE_HANDLE_T, ATT_NONE )
  309. ,NODE_HANDLE_T
  310. ,(ATTR_T)ATTR_NONE
  311. ,(ATTR_T)ATTR_NONE
  312. ,"handle_t"
  313. }
  314. ,{ /** signed long long **/
  315. MAKE_TYPE_SPEC( SIGN_SIGNED, SIZE_LONGLONG, TYPE_INT, ATT_NONE )
  316. ,NODE_LONGLONG
  317. ,(ATTR_T)ATTR_NONE
  318. ,(ATTR_T)ATTR_NONE
  319. ,"long long"
  320. }
  321. ,{ /** unsigned long **/
  322. MAKE_TYPE_SPEC( SIGN_UNSIGNED, SIZE_LONGLONG, TYPE_INT, ATT_NONE )
  323. ,NODE_LONGLONG
  324. ,(ATTR_T)ATTR_UNSIGNED
  325. ,(ATTR_T)ATTR_NONE
  326. ,"long long"
  327. }
  328. };
  329. /*****************************************************************************/
  330. SIBLING_LIST
  331. SIBLING_LIST::Add( named_node * pNewNode )
  332. {
  333. if (pTail == NULL)
  334. {
  335. return Init( pNewNode );
  336. };
  337. if ( pNewNode )
  338. {
  339. named_node * pHead = (named_node *) pTail->GetSibling();
  340. pNewNode->SetSibling( pHead );
  341. pTail->SetSibling(pNewNode);
  342. pTail = pNewNode;
  343. };
  344. return *this;
  345. };
  346. SIBLING_LIST
  347. SIBLING_LIST::Merge( SIBLING_LIST & NewList )
  348. {
  349. if (NewList.pTail == NULL)
  350. {
  351. return *this;
  352. };
  353. if ( pTail != NULL )
  354. {
  355. named_node * tmp = (named_node *)
  356. NewList.pTail->GetSibling();
  357. NewList.pTail->SetSibling( pTail->GetSibling());
  358. pTail->SetSibling(tmp);
  359. };
  360. pTail = NewList.pTail;
  361. return *this;
  362. };
  363. named_node *
  364. SIBLING_LIST::Linearize()
  365. {
  366. named_node * pHead;
  367. if (pTail == NULL)
  368. {
  369. pHead = NULL;
  370. }
  371. else
  372. {
  373. pHead = (named_node *) pTail->GetSibling();
  374. pTail->SetSibling( (named_node *) NULL);
  375. };
  376. return pHead;
  377. };
  378. void
  379. SIBLING_LIST::AddAttributes( ATTRLIST & AList )
  380. {
  381. named_node * pCur;
  382. ATTRLIST pClone;
  383. // for each of the node_skl nodes, apply the ATTRLIST
  384. if ( pTail == NULL) return;
  385. pCur = pTail->GetSibling();
  386. // this traversal skips the last node on the list;
  387. // that one gets the original list added.
  388. while (pCur != pTail)
  389. {
  390. // clone attr list
  391. // apply to pCur
  392. pClone = AList.Clone();
  393. pCur->AddAttributes( AList );
  394. pCur = pCur->GetSibling();
  395. }
  396. pCur->AddAttributes( AList );
  397. }
  398. /*****************************************************************************/
  399. /*** ParseError ************************************************************
  400. * Purpose : format and report parsing errors
  401. * Input :
  402. * Output :
  403. * Notes : errors will be reported many times. This is one localised place
  404. * : for the RpcError Call
  405. ***************************************************************************/
  406. void
  407. ParseError(
  408. STATUS_T Err,
  409. char * pSuffix )
  410. {
  411. char * pFileName;
  412. short Line;
  413. short Col;
  414. char TempBuf[512 + 50];
  415. char * pBuffer;
  416. ErrorInfo ErrStats( Err );
  417. if ( !ErrStats.IsRelevant() )
  418. return;
  419. pImportCntrl->GetCurrentInputDetails( &pFileName, &Line, &Col);
  420. if (pSuffix)
  421. {
  422. strcpy( TempBuf, ": " );
  423. strcat( TempBuf, pSuffix );
  424. pBuffer = TempBuf;
  425. }
  426. else
  427. {
  428. pBuffer = "";
  429. }
  430. ErrStats.ReportError(pFileName, Line, pBuffer);
  431. }
  432. void
  433. SyntaxError(
  434. STATUS_T Err,
  435. short State )
  436. {
  437. #define NEAR_STRING (" near ")
  438. #define STRLEN_OF_NEAR_STRING (6)
  439. extern char *tokptr_G;
  440. char * pTemp;
  441. short len = (short) strlen( tokptr_G );
  442. char * pBuffer = new char[
  443. 512 +
  444. STRLEN_OF_NEAR_STRING +
  445. len + 2 +
  446. 1 ];
  447. #ifndef NO_GOOD_ERRORS
  448. if( Err == BENIGN_SYNTAX_ERROR )
  449. {
  450. GetExpectedSyntax( pBuffer, State );
  451. strcat( pBuffer, NEAR_STRING );
  452. strcat( pBuffer, "\"" );
  453. strcat( pBuffer, tokptr_G );
  454. strcat( pBuffer, "\"" );
  455. pTemp = pBuffer;
  456. }
  457. else
  458. pTemp = (char *)0;
  459. ParseError( Err, pTemp );
  460. #else // NO_GOOD_ERRORS
  461. strcpy( pBuffer, "syntax error" );
  462. ParseError( Err, pBuffer );
  463. #endif // NO_GOOD_ERRORS
  464. delete pBuffer;
  465. }
  466. /*** BaseTypeSpecAnalysis *************************************************
  467. * Purpose : to check for valid base type specification
  468. * Input : pointer to already collected specs, new base type spec
  469. * Output : modified collected specs
  470. * Notes :
  471. ***************************************************************************/
  472. void
  473. BaseTypeSpecAnalysis(
  474. struct _type_ana *pType,
  475. short NewBaseType )
  476. {
  477. char TempBuf[ 50 ];
  478. if( pType->BaseType == TYPE_PIPE )
  479. return;
  480. if( (pType->BaseType != TYPE_UNDEF) && (NewBaseType != TYPE_UNDEF) )
  481. {
  482. sprintf(TempBuf,", ignoring %s", GetTypeName(NewBaseType));
  483. ParseError(BENIGN_SYNTAX_ERROR, TempBuf);
  484. }
  485. if(NewBaseType != TYPE_UNDEF)
  486. pType->BaseType = NewBaseType;
  487. }
  488. /**************************************************************************
  489. * routines for the pre_type_db class
  490. **************************************************************************/
  491. /*** pre_type_db *********************************************************
  492. * Purpose : constructor for pre-allocated type data base
  493. * Input : nothing
  494. * Output :
  495. * Notes : inits the prellocated types data base. This routine exist mainly
  496. * : because static preallocation was giving a problem. If that is
  497. * : solved, remove this
  498. **************************************************************************/
  499. pre_type_db::pre_type_db (void)
  500. {
  501. named_node * pNode;
  502. int i = 0;
  503. struct _pre_type * pPreType = &TypeDB[ 0 ];
  504. struct pre_init * pInitCur = PreInitArray;
  505. while( i < PRE_TYPE_DB_SIZE )
  506. {
  507. pPreType->TypeSpec = pInitCur->TypeSpec;
  508. pPreType->pPreAlloc = pNode = new node_base_type(
  509. pInitCur->NodeType,
  510. pInitCur->Attr );
  511. if ( pInitCur->Attr2 == ATTR_W64 )
  512. {
  513. pNode->GetModifiers().SetModifier( pInitCur->Attr2 );
  514. }
  515. pNode->SetSymName( pInitCur->pSymName );
  516. pInitCur++;
  517. pPreType++;
  518. ++i;
  519. }
  520. }
  521. /*** GetPreAllocType ******************************************************
  522. * Purpose : to search for a preallocated base type node, whose type
  523. * : spec is provided
  524. * Input : pointer to the resultant base node
  525. * Output : STATUS_OK if all is well, error otherwise
  526. * Notes :
  527. **************************************************************************/
  528. STATUS_T
  529. pre_type_db::GetPreAllocType(
  530. node_skl ** ppNode,
  531. unsigned short TypeSpec )
  532. {
  533. int i = 0;
  534. if( GET_TYPE( TypeSpec ) == TYPE_PIPE )
  535. {
  536. (*ppNode) = pErrorTypeNode;
  537. return STATUS_OK;
  538. }
  539. while(i < sizeof(TypeDB) / sizeof(struct _pre_type) )
  540. {
  541. if( TypeDB[i].TypeSpec == TypeSpec )
  542. {
  543. (*ppNode) = TypeDB[i].pPreAlloc;
  544. return STATUS_OK;
  545. }
  546. ++i;
  547. }
  548. return SYNTAX_ERROR;
  549. }
  550. /****************************************************************************
  551. * nested symbol table access support
  552. ***************************************************************************/
  553. nsa::nsa( void )
  554. {
  555. SymTable * pSymTable = new GlobalSymTable;
  556. CurrentLevel = 0;
  557. InsertHead( (void *)pSymTable );
  558. }
  559. STATUS_T
  560. nsa::PushSymLevel(
  561. SymTable **ppSymTable )
  562. {
  563. STATUS_T Status;
  564. SymTable *pSymTable = new SymTable;
  565. CurrentLevel++;
  566. Status = InsertHead( (void *)pSymTable);
  567. *ppSymTable = pSymTable;
  568. return Status;
  569. }
  570. STATUS_T
  571. nsa::PopSymLevel(
  572. SymTable **ppSymTable )
  573. {
  574. if(CurrentLevel == 0)
  575. return I_ERR_SYMTABLE_UNDERFLOW;
  576. CurrentLevel--;
  577. RemoveHead();
  578. return GetCurrent( (void **)ppSymTable );
  579. }
  580. short
  581. nsa::GetCurrentLevel( void )
  582. {
  583. return CurrentLevel;
  584. }
  585. SymTable *
  586. nsa::GetCurrentSymbolTable()
  587. {
  588. SymTable *pSymbolTable;
  589. GetCurrent( (void **)&pSymbolTable );
  590. return pSymbolTable;
  591. }
  592. /****************************************************************************
  593. * nested symbol table access support
  594. ***************************************************************************/
  595. void
  596. IINFODICT::StartNewInterface()
  597. {
  598. IINFO * pInfo = new IINFO;
  599. pInfo->fLocal = FALSE;
  600. pInfo->InterfacePtrAttribute = ATTR_NONE;
  601. pInfo->pInterfaceNode = NULL;
  602. pInfo->CurrentTagNumber = 1;
  603. pInfo->fPtrDefErrorReported = 0;
  604. pInfo->fPtrWarningIssued = FALSE;
  605. pInfo->IntfKey = CurrentIntfKey;
  606. Push( (IDICTELEMENT) pInfo );
  607. }
  608. BOOL
  609. IINFODICT::IsPtrWarningIssued()
  610. {
  611. IINFO * pInfo = (IINFO *)GetTop();
  612. return pInfo->fPtrWarningIssued;
  613. }
  614. void
  615. IINFODICT::SetPtrWarningIssued()
  616. {
  617. IINFO * pInfo = (IINFO *)GetTop();
  618. pInfo->fPtrWarningIssued = TRUE;
  619. }
  620. void
  621. IINFODICT::EndNewInterface()
  622. {
  623. IINFO * pInfo = (IINFO *)GetTop();
  624. delete pInfo;
  625. Pop();
  626. pInfo = (IINFO *) GetTop();
  627. if ( pInfo )
  628. CurrentIntfKey = pInfo->IntfKey;
  629. }
  630. void
  631. IINFODICT::SetInterfacePtrAttribute(
  632. ATTR_T A )
  633. {
  634. IINFO * pInfo = (IINFO *)GetTop();
  635. pInfo->InterfacePtrAttribute = A;
  636. if( ImportLevel == 0 ) BaseInterfacePtrAttribute = A;
  637. }
  638. void
  639. IINFODICT::SetInterfaceLocal()
  640. {
  641. IINFO * pInfo = (IINFO *)GetTop();
  642. pInfo->fLocal = TRUE;
  643. if( ImportLevel == 0 ) fBaseLocal = TRUE;
  644. }
  645. void
  646. IINFODICT::SetInterfaceNode(
  647. node_interface * p )
  648. {
  649. IINFO * pInfo = (IINFO *)GetTop();
  650. pInfo->pInterfaceNode = p;
  651. if( ImportLevel == 0 ) pBaseInterfaceNode = p;
  652. CurrentIntfKey = (unsigned short) pInterfaceDict->AddElement( p );
  653. pInfo->IntfKey = CurrentIntfKey;
  654. }
  655. void
  656. IINFODICT::IncrementCurrentTagNumber()
  657. {
  658. IINFO * pInfo = (IINFO *)GetTop();
  659. pInfo->CurrentTagNumber++;
  660. }
  661. ATTR_T
  662. IINFODICT::GetInterfacePtrAttribute()
  663. {
  664. IINFO * pInfo = (IINFO *)GetTop();
  665. return pInfo->InterfacePtrAttribute;
  666. }
  667. ATTR_T
  668. IINFODICT::GetBaseInterfacePtrAttribute()
  669. {
  670. return BaseInterfacePtrAttribute;
  671. }
  672. BOOL
  673. IINFODICT::IsInterfaceLocal()
  674. {
  675. IINFO * pInfo = (IINFO *)GetTop();
  676. return pInfo->fLocal;
  677. }
  678. node_interface *
  679. IINFODICT::GetInterfaceNode()
  680. {
  681. IINFO * pInfo = (IINFO *)GetTop();
  682. return pInfo->pInterfaceNode;
  683. }
  684. short
  685. IINFODICT::GetCurrentTagNumber()
  686. {
  687. IINFO * pInfo = (IINFO *)GetTop();
  688. return pInfo->CurrentTagNumber;
  689. }
  690. void
  691. IINFODICT::SetPtrDefErrorReported()
  692. {
  693. IINFO * pInfo = (IINFO *) GetTop();
  694. pInfo->fPtrDefErrorReported = 1;
  695. }
  696. BOOL
  697. IINFODICT::IsPtrDefErrorReported()
  698. {
  699. IINFO * pInfo = (IINFO *) GetTop();
  700. return (BOOL) (pInfo->fPtrDefErrorReported == 1);
  701. }
  702. /****************************************************************************
  703. * utility functions
  704. ***************************************************************************/
  705. char *
  706. GetTypeName(
  707. short Type )
  708. {
  709. char *p;
  710. switch(Type)
  711. {
  712. case TYPE_INT: p = "\"int\""; break;
  713. case TYPE_FLOAT: p = "\"float\""; break;
  714. case TYPE_DOUBLE: p = "\"double\""; break;
  715. case TYPE_FLOAT80: p = "\"__float80\""; break;
  716. case TYPE_FLOAT128: p = "\"__float128\""; break;
  717. case TYPE_VOID: p = "\"void\""; break;
  718. case TYPE_BOOLEAN: p = "\"boolean\""; break;
  719. case TYPE_HANDLE_T: p = "\"handle_t\""; break;
  720. default: p = ""; break;
  721. }
  722. return p;
  723. }
  724. STATUS_T
  725. GetBaseTypeNode(
  726. node_skl ** ppNode,
  727. short TypeSign,
  728. short TypeSize,
  729. short BaseType,
  730. short TypeAttrib)
  731. {
  732. STATUS_T uStatus = STATUS_OK;
  733. if( pPreAllocTypes->GetPreAllocType(
  734. ppNode,
  735. (unsigned short)MAKE_TYPE_SPEC(TypeSign,TypeSize,BaseType,TypeAttrib)) != STATUS_OK)
  736. {
  737. // this should never happen
  738. MIDL_ASSERT( FALSE );
  739. *ppNode = (node_skl *)new node_error;
  740. }
  741. return uStatus;
  742. }
  743. STATUS_T
  744. GetBaseTypeNode(
  745. node_skl ** ppNode,
  746. struct _type_ana Type)
  747. {
  748. STATUS_T uStatus = STATUS_OK;
  749. if( pPreAllocTypes->GetPreAllocType(
  750. ppNode,
  751. (unsigned short)MAKE_TYPE_SPEC(Type.TypeSign,Type.TypeSize,Type.BaseType, Type.TypeAttrib)) != STATUS_OK)
  752. {
  753. // this should never happen
  754. *ppNode = (node_skl *) NULL;
  755. }
  756. return uStatus;
  757. }
  758. #define TEMPNAME ("__MIDL_")
  759. #define TEMP_NAME_LENGTH (7)
  760. #define INTF_ADDITION ("itf_")
  761. #define INTF_ADDITION_LENGTH (4)
  762. #define LENGTH_OF_1_UNDERSCORE (1)
  763. char *
  764. GenTempName()
  765. {
  766. static short NameCounter = 0;
  767. char TempBuf[ TEMP_NAME_LENGTH + 4 + 1 ];
  768. sprintf(TempBuf, "%s%.4d", TEMPNAME, NameCounter++);
  769. return pMidlLexTable->LexInsert( TempBuf );
  770. }
  771. char *
  772. GenIntfName()
  773. {
  774. static short NameCounter = 0;
  775. char TempBuf[ TEMP_NAME_LENGTH + INTF_ADDITION_LENGTH + 4 + 1 + _MAX_PATH ];
  776. char FileBase[_MAX_PATH];
  777. char dbcsFileBase[_MAX_PATH*2];
  778. pCommand->GetInputFileNameComponents(NULL, NULL, FileBase, NULL );
  779. // account for spaces and DBCS chars.
  780. NormalizeString( FileBase, dbcsFileBase );
  781. sprintf(TempBuf, "%s%s%s_%.4d",
  782. TEMPNAME,
  783. INTF_ADDITION,
  784. dbcsFileBase,
  785. NameCounter++);
  786. return pMidlLexTable->LexInsert( TempBuf );
  787. }
  788. char *
  789. GenCompName()
  790. {
  791. char* pCurrentInterfaceName = pInterfaceInfoDict->GetInterfaceName();
  792. short Length = (short) strlen(pCurrentInterfaceName);
  793. char* pBuffer;
  794. char* pTemp;
  795. short CurrentTagNumber;
  796. pBuffer = new char[ TEMP_NAME_LENGTH + // __MIDL_
  797. Length + // intface name
  798. LENGTH_OF_1_UNDERSCORE + // _
  799. 4 + // temp number
  800. 1 ]; // term. zero.
  801. CurrentTagNumber = pInterfaceInfoDict->GetCurrentTagNumber();
  802. sprintf( pBuffer, "%s%s_%.4d", TEMPNAME, pCurrentInterfaceName, CurrentTagNumber );
  803. pInterfaceInfoDict->IncrementCurrentTagNumber();
  804. pTemp = pMidlLexTable->LexInsert( pBuffer );
  805. delete pBuffer;
  806. return pTemp;
  807. }
  808. BOOL
  809. IsTempName(
  810. char *pName )
  811. {
  812. return !(strncmp( pName, TEMPNAME , TEMP_NAME_LENGTH ) );
  813. }
  814. void
  815. ApplyAttributes(
  816. named_node * pNode,
  817. ATTRLIST & attrs )
  818. {
  819. if( attrs )
  820. {
  821. pNode->SetAttributes(attrs);
  822. }
  823. }
  824. /****************************************************************************
  825. This routine exists to share code for setting up the field attribute nodes
  826. ****************************************************************************/
  827. ATTRLIST
  828. GenerateFieldAttribute(
  829. ATTR_T NodeType,
  830. expr_list * pExprList )
  831. {
  832. node_base_attr * pAttr = 0;
  833. expr_node * pExpr = 0;
  834. ATTRLIST AList;
  835. AList.MakeAttrList();
  836. /**
  837. ** we delibrately dont set the bits in the summary attribute 'cause
  838. ** these bits will get set in the set attribute anyways for the
  839. ** field attributes
  840. **/
  841. if(pExprList != (expr_list *)NULL)
  842. {
  843. pExprList->Init();
  844. while( pExprList->GetNext( (void **)&pExpr ) == STATUS_OK)
  845. {
  846. switch(NodeType)
  847. {
  848. case ATTR_FIRST:
  849. case ATTR_LAST:
  850. case ATTR_LENGTH:
  851. case ATTR_SIZE:
  852. case ATTR_MIN:
  853. case ATTR_MAX:
  854. pAttr = new size_attr( pExpr, NodeType );
  855. break;
  856. default:
  857. //this should never happen
  858. MIDL_ASSERT(FALSE && "Attribute not supported");
  859. break;
  860. }
  861. AList.SetPeer( pAttr );
  862. }
  863. }
  864. delete pExprList;
  865. AList.Reverse();
  866. return AList;
  867. }
  868. /****************************************************************************
  869. SearchTag:
  870. This routine provides a means of searching the global symbol space for
  871. struct/union tags, and enums. These share the same name space but we want to
  872. keep the symbol table identity of enums, struct tags etc separate. so
  873. we need to search for all of these separately when verifying that a tag
  874. has really not been seen before.
  875. This routine returns:
  876. 1. (node_skl *)NULL if NO struct/union/enum was defined by that name
  877. 2. node_skl * if the a definition was found for what you are looking
  878. for.
  879. 3. (node_skl *) error type node if a definition was found, but it is
  880. not what you are looking for.
  881. ****************************************************************************/
  882. node_skl *
  883. SearchTag(
  884. char * pName,
  885. NAME_T Tag )
  886. {
  887. node_skl * pNode;
  888. NAME_T MyTag;
  889. SymKey SKey( pName, MyTag = NAME_TAG );
  890. /**
  891. ** Has it been declared as a struct ?
  892. **/
  893. if ( (pNode = pBaseSymTbl->SymSearch(SKey) ) == 0 )
  894. {
  895. /**
  896. ** not a tag - maybe enum / union
  897. **/
  898. SKey.SetKind( MyTag = NAME_ENUM );
  899. if ( (pNode = pBaseSymTbl->SymSearch(SKey) ) == 0 )
  900. {
  901. /**
  902. ** not a enum maybe union
  903. **/
  904. SKey.SetKind( MyTag = NAME_UNION );
  905. if ( (pNode = pBaseSymTbl->SymSearch(SKey) ) == 0 )
  906. return (node_skl *)NULL;
  907. }
  908. }
  909. /**
  910. ** search was sucessful. Check whether this was what you were looking
  911. ** for. If it is , it means we found a definition of the symbol. If not
  912. ** then we found a definition all right, but it is of a different entity.
  913. ** The routine can find this out by verifying that the typenode returned
  914. ** was an error type node or not
  915. **/
  916. return (MyTag == Tag ) ? pNode : pErrorTypeNode;
  917. }
  918. /****************************************************************************
  919. SetPredefinedTypes:
  920. Set up predefined types for the midl compiler. The predefined types
  921. are error_status_t and wchar_t( the latter dependent on compile mode )
  922. ****************************************************************************/
  923. void
  924. SetPredefinedTypes()
  925. {
  926. char * pName = 0;
  927. node_e_status_t * pNew = new node_e_status_t;
  928. node_def_fe * pDef = new node_def_fe( pName = pNew->GetSymName(), pNew );
  929. // set global version
  930. pError_status_t = pNew;
  931. // the typedef of error_status_t in the symbol table
  932. SymKey SKey( pName, NAME_DEF);
  933. pBaseSymTbl->SymInsert(SKey, (SymTable *)NULL, pDef );
  934. //
  935. // we always predefine wchar_t and report the error to the user. If
  936. // we dont enter wchar_t in the predefined types, then we get all
  937. // kinds of syntax and error recovery errors which could be confusing
  938. // in this context. We therefore explicitly give an error on wchar_t.
  939. //
  940. node_wchar_t * pNew2 = new node_wchar_t;
  941. pDef = new node_def_fe( pName = pNew2->GetSymName(), pNew2 );
  942. // the typedef of wchar_t in the symbol table
  943. SKey.SetString( pName );
  944. pBaseSymTbl->SymInsert(SKey, (SymTable *)NULL, pDef );
  945. }
  946. //
  947. // We check for a proc/typedef/member/param/tag/enum/label name already defined
  948. // as an identifier. Only if the identifier is one which will be turned into
  949. // a #define, do we report an error. However, it is not worth it to check if
  950. // an identifier is used as a name because in any case we will not be able to
  951. // check for clashes with field / param names since they are at a lower than
  952. // global, symbol table scope. Generally checking if the name of a member etc
  953. // is already defined as an id which will be turned into a #define should be
  954. // enough.
  955. //
  956. void
  957. CheckGlobalNamesClash(
  958. SymKey SKeyOfSymbolBeingDefined )
  959. {
  960. NAME_T NT = SKeyOfSymbolBeingDefined.GetKind();
  961. char * pName = SKeyOfSymbolBeingDefined.GetString();
  962. SymKey SKey;
  963. SKey.SetString( pName );
  964. switch( NT )
  965. {
  966. case NAME_PROC:
  967. case NAME_MEMBER:
  968. case NAME_TAG:
  969. case NAME_DEF:
  970. case NAME_LABEL:
  971. case NAME_ENUM:
  972. node_id * pID;
  973. SKey.SetKind( NAME_ID );
  974. if ( ( pID = (node_id *) pBaseSymTbl->SymSearch( SKey ) ) != 0 )
  975. {
  976. BOOL fWillBeAHashDefine = !pID->FInSummary( ATTR_EXTERN ) &&
  977. !pID->FInSummary( ATTR_STATIC ) &&
  978. pID->GetInitList();
  979. if( fWillBeAHashDefine )
  980. ParseError( NAME_ALREADY_USED, pName );
  981. }
  982. break;
  983. case NAME_ID:
  984. #if 0
  985. SKey.SetKind( NAME_PROC );
  986. if( !pBaseSymTbl->SymSearch( SKey ) )
  987. {
  988. SKey.SetKind( NAME_TAG );
  989. if( !pBaseSymTbl->SymSearch( SKey ) )
  990. {
  991. SKey.SetKind( NAME_DEF );
  992. if( !pBaseSymTbl->SymSearch( SKey ) )
  993. {
  994. SKey.SetKind( NAME_LABEL );
  995. if( !pBaseSymTbl->SymSearch( SKey ) )
  996. {
  997. SKey.SetKind( NAME_ENUM );
  998. if( !pBaseSymTbl->SymSearch( SKey ) )
  999. break;
  1000. }
  1001. }
  1002. }
  1003. }
  1004. ParseError( NAME_CLASH_WITH_CONST_ID, pName );
  1005. break;
  1006. #endif // 0
  1007. default:
  1008. break;
  1009. }
  1010. }