Windows NT 4.0 source code leak
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.

398 lines
9.6 KiB

4 years ago
  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. comhdr.cxx
  5. Abstract:
  6. Generates com class header file.
  7. Notes:
  8. History:
  9. ----------------------------------------------------------------------------*/
  10. /****************************************************************************
  11. * include files
  12. ***************************************************************************/
  13. #include "becls.hxx"
  14. #pragma hdrstop
  15. #include "buffer.hxx"
  16. /****************************************************************************
  17. * local definitions
  18. ***************************************************************************/
  19. /****************************************************************************
  20. * externs
  21. ***************************************************************************/
  22. extern CMD_ARG * pCommand;
  23. CG_STATUS
  24. CG_COM_CLASS::GenHeader(
  25. CCB * pCCB )
  26. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  27. Routine Description:
  28. Generate interface header file.
  29. Arguments:
  30. pCCB - a pointer to the code generation control block.
  31. Return Value:
  32. CG_OK if all is well, error otherwise.
  33. ----------------------------------------------------------------------------*/
  34. {
  35. node_interface * pInterface = (node_interface *) GetType();
  36. ITERATOR I;
  37. ISTREAM * pStream = pCCB->GetStream();
  38. char * pName = pInterface->GetSymName();
  39. MIDL_TOKEN Token( START_CLASS_TOKEN );
  40. BOOL fReParse = pCCB->IsReparsingCurrentFile();
  41. //Initialize the CCB for this interface.
  42. InitializeCCB(pCCB);
  43. if ( fReParse )
  44. {
  45. MIDL_TOKEN FoundToken;
  46. ((RW_ISTREAM *) pStream)->SaveToNextMidlToken( FoundToken );
  47. if ( FoundToken.GetTokenType() != START_CLASS_TOKEN )
  48. {
  49. assert(!"expecting class token" );
  50. }
  51. }
  52. else
  53. pStream->NewLine();
  54. pStream->EmitToken( Token );
  55. // put out the class guards
  56. pStream->Write("\n#ifndef __");
  57. pStream->Write( pName );
  58. pStream->Write( "_CLASS_DEFINED__\n" );
  59. pStream->Write( "#define __");
  60. pStream->Write( pName );
  61. pStream->Write( "_CLASS_DEFINED__\n" );
  62. // Print out the declarations of the types
  63. pStream->NewLine();
  64. pInterface->PrintType( PRT_INTERFACE | PRT_OMIT_PROTOTYPE, pStream, 0);
  65. Out_CLSID(pCCB);
  66. // print out the vtable/class definitions
  67. pStream->NewLine();
  68. pStream->Write("#if defined(__cplusplus)");
  69. DumpAllMethodsToHeader(pCCB);
  70. pStream->NewLine();
  71. GenComClassFactoryHeader( pCCB );
  72. pStream->NewLine();
  73. pStream->Write("#endif \t/* defined(__cplusplus) */");
  74. pStream->NewLine();
  75. // put out the trailing interface guard
  76. pStream->Write( "\n#endif \t/* __");
  77. pStream->Write( pName );
  78. pStream->Write( "_CLASS_DEFINED__ */\n" );
  79. pStream->NewLine();
  80. if ( fReParse )
  81. {
  82. MIDL_TOKEN FoundToken;
  83. ((RW_ISTREAM *) pStream)->DiscardToNextMidlToken( FoundToken );
  84. if ( FoundToken.GetTokenType() != END_CLASS_TOKEN )
  85. {
  86. assert(!"expecting class token" );
  87. }
  88. }
  89. Token.SetTokenType( END_CLASS_TOKEN );
  90. pStream->EmitToken( Token );
  91. pStream->NewLine();
  92. return CG_OK;
  93. }
  94. CG_STATUS
  95. CG_COM_CLASS::DumpAllMethodsToHeader(CCB *pCCB)
  96. {
  97. ISTREAM * pStream = pCCB->GetStream();
  98. char * pName;
  99. ITERATOR I;
  100. ITERATOR * pDirectBaseInterfaceList = GetBaseInterfaceCGList();
  101. CG_OBJECT_INTERFACE * pIntf;
  102. pStream->NewLine();
  103. pName = GetType()->GetSymName();
  104. assert (pName != (char *)0);
  105. pStream->NewLine();
  106. pStream->Write("class ");
  107. pStream->Write(pName);
  108. pStream->NewLine();
  109. pStream->Write(" : public Cls_MIDL_Unknown");
  110. //Check if this interface was derived from a base interface.
  111. ITERATOR_INIT( *pDirectBaseInterfaceList );
  112. while( ITERATOR_GETNEXT( *pDirectBaseInterfaceList, pIntf ) )
  113. {
  114. pStream->Write( "," );
  115. pStream->NewLine();
  116. pStream->Write(" public ");
  117. pStream->Write(pIntf->GetType()->GetSymName());
  118. }
  119. pStream->NewLine();
  120. pStream->Write('{');
  121. pStream->NewLine();
  122. pStream->Write("public:");
  123. pStream->IndentInc();
  124. // go through all base interfaces, printing them and THEIR base
  125. // interfaces, avoiding duplicates...
  126. GetListOfUniqueBaseInterfaces( I );
  127. // now print them all, unmarking as we go
  128. for ( ITERATOR_INIT(I); ITERATOR_GETNEXT( I, pIntf ); pIntf->MarkVisited(FALSE) )
  129. {
  130. pStream->NewLine( 2 );
  131. pStream->Write( pDelimiterString );
  132. pStream->NewLine();
  133. pStream->Write("// Member functions from: ");
  134. pStream->Write( ( pIntf->GetCGID() == ID_CG_COM_CLASS ) ? "Class " : "Interface " );
  135. pStream->Write( pIntf->GetInterfaceName() );
  136. pStream->NewLine();
  137. pIntf->PrintMemberFunctions( pStream, FALSE );
  138. }
  139. pStream->NewLine( 2 );
  140. pStream->Write( pDelimiterString );
  141. pStream->NewLine( 2 );
  142. pStream->Write("// Constructor and virtual destructor");
  143. pStream->NewLine();
  144. pStream->Write( pName );
  145. pStream->Write( "( IUnknown * pUnknownOuter );" );
  146. pStream->NewLine();
  147. pStream->Write( "virtual ~" );
  148. pStream->Write( pName );
  149. pStream->Write( "();" );
  150. pStream->NewLine();
  151. pStream->NewLine( 2 );
  152. // now the operator new and delete
  153. GenAllocatorHeader( pCCB );
  154. pStream->NewLine();
  155. pStream->Write("friend class ");
  156. pStream->Write( pName );
  157. pStream->Write( "_Internal_Unknown;");
  158. pStream->NewLine(2);
  159. MIDL_TOKEN Token( START_CLASS_USER_TOKEN );
  160. BOOL fReParse = pCCB->IsReparsingCurrentFile();
  161. if ( fReParse )
  162. {
  163. MIDL_TOKEN FoundToken;
  164. ((RW_ISTREAM *) pStream)->DiscardToNextMidlToken( FoundToken );
  165. if ( FoundToken.GetTokenType() != START_CLASS_USER_TOKEN )
  166. {
  167. assert(!"expecting class token" );
  168. }
  169. }
  170. pStream->EmitToken( Token );
  171. if ( fReParse )
  172. pStream->Write( '\n' );
  173. else
  174. {
  175. pStream->NewLine();
  176. pStream->Write( pDelimiterString );
  177. pStream->NewLine();
  178. pStream->Write( pStartUserSection );
  179. pStream->NewLine();
  180. pStream->Write( pEndUserSection );
  181. }
  182. pStream->IndentDec();
  183. if ( fReParse )
  184. {
  185. MIDL_TOKEN FoundToken;
  186. ((RW_ISTREAM *) pStream)->SaveToNextMidlToken( FoundToken );
  187. if ( FoundToken.GetTokenType() != END_CLASS_USER_TOKEN )
  188. {
  189. assert(!"expecting includes token" );
  190. }
  191. }
  192. else
  193. pStream->NewLine();
  194. Token.SetTokenType( END_CLASS_USER_TOKEN );
  195. pStream->EmitToken( Token );
  196. pStream->NewLine();
  197. pStream->Write("};");
  198. pStream->NewLine();
  199. // now the embedded IUnknown for being aggregated
  200. GenEmbeddedIUnknownHeader( pCCB );
  201. pStream->NewLine(2);
  202. return CG_OK;
  203. }
  204. CG_STATUS
  205. CG_COM_CLASS::GenAllocatorHeader(CCB *pCCB)
  206. {
  207. static char * OutputString1 =
  208. "void * operator new( size_t s )\n"
  209. " {\n"
  210. " return CoTaskMemAlloc( s );\n"
  211. " }\n";
  212. static char * OutputString2 =
  213. "void operator delete( void * pv )\n"
  214. " {\n"
  215. " CoTaskMemFree( pv );\n"
  216. " }\n";
  217. ISTREAM * pStream = pCCB->GetStream();
  218. pStream->NewLine();
  219. pStream->Write( OutputString1 );
  220. pStream->NewLine();
  221. pStream->Write( OutputString2 );
  222. return CG_OK;
  223. }
  224. CG_STATUS
  225. CG_COM_CLASS::GenEmbeddedIUnknownHeader(CCB *pCCB)
  226. {
  227. ISTREAM * pStream = pCCB->GetStream();
  228. char * pName = GetSymName();
  229. CG_OBJECT_INTERFACE * pIUnknown = pCCB->GetIUnknownCG();
  230. assert (pName != (char *)0);
  231. pStream->NewLine( 2 );
  232. pStream->Write( pDelimiterString );
  233. pStream->NewLine();
  234. pStream->Write("// Internal IUnknown class for class : ");
  235. pStream->Write( pName );
  236. pStream->NewLine(2);
  237. pStream->Write("class ");
  238. pStream->Write( pName );
  239. pStream->Write("_Internal_Unknown : public Cls_MIDL_Internal_Unknown");
  240. pStream->IndentInc();
  241. pStream->NewLine();
  242. pStream->Write( '{' );
  243. pStream->NewLine();
  244. // print the IUnknown Member functions
  245. pIUnknown->PrintMemberFunctions( pStream, FALSE );
  246. pStream->NewLine();
  247. pStream->Write( "};" );
  248. pStream->IndentDec();
  249. pStream->NewLine(2);
  250. return CG_OK;
  251. }
  252. CG_STATUS
  253. CG_COM_CLASS::GenComClassFactoryHeader(CCB *pCCB)
  254. {
  255. ISTREAM * pStream = pCCB->GetStream();
  256. char * pName;
  257. CG_OBJECT_INTERFACE * pIUnknown = pCCB->GetIUnknownCG();
  258. CG_OBJECT_INTERFACE * pIClassf = pCCB->GetIClassfCG();
  259. pStream->NewLine();
  260. pName = GetSymName();
  261. assert (pName != (char *)0);
  262. pStream->Write( pDelimiterString );
  263. pStream->NewLine();
  264. pStream->Write( "// The class factory for class ");
  265. pStream->Write( pName );
  266. pStream->NewLine(2);
  267. pStream->Write( "class ");
  268. pStream->Write( pName );
  269. pStream->Write( "_Factory: public CGenericMidlClassFactory");
  270. pStream->IndentInc();
  271. pStream->NewLine();
  272. pStream->Write( "{\npublic:" );
  273. pStream->NewLine(2);
  274. // print the IUnknown Member functions
  275. pStream->Write( pDelimiterString );
  276. pStream->NewLine();
  277. pStream->Write( "// IUnknown methods" );
  278. pStream->NewLine();
  279. pIUnknown->PrintMemberFunctions( pStream, FALSE );
  280. pStream->NewLine();
  281. // print the IClassFactory Member functions
  282. pStream->Write( pDelimiterString );
  283. pStream->NewLine();
  284. pStream->Write( "// IClassFactory methods" );
  285. pStream->NewLine();
  286. pIClassf->PrintMemberFunctions( pStream, FALSE );
  287. pStream->NewLine();
  288. pStream->Write( "};" );
  289. pStream->IndentDec();
  290. pStream->NewLine(2);
  291. pStream->Write( "// the global factory constructed at compile time" );
  292. pStream->NewLine();
  293. pStream->Write( "EXTERN_C ");
  294. pStream->Write( pName );
  295. pStream->Write( "_Factory * p" );
  296. pStream->Write( pName );
  297. pStream->Write( "_Factory;");
  298. pStream->NewLine();
  299. pStream->Write( "EXTERN_C Midl_Class_Factory_Info ");
  300. pStream->Write( pName );
  301. pStream->Write( "_Factory_Info;" );
  302. pStream->NewLine(2);
  303. return CG_OK;
  304. }