pgenutil.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. {
  2. Copyright (c) 2011
  3. Contains different functions that are used in the context of
  4. parsing generics.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pgenutil;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cclasses,
  24. { global }
  25. globtype,
  26. { symtable }
  27. symtype,symdef,symbase;
  28. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string);
  29. function parse_generic_parameters:TFPObjectList;
  30. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  31. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfpobjectlist);
  32. type
  33. tspecializationstate = record
  34. oldsymtablestack : tsymtablestack;
  35. oldextendeddefs : TFPHashObjectList;
  36. end;
  37. procedure specialization_init(genericdef:tdef;var state:tspecializationstate);
  38. procedure specialization_done(var state:tspecializationstate);
  39. implementation
  40. uses
  41. { common }
  42. cutils,fpccrc,
  43. { global }
  44. globals,tokens,verbose,
  45. { symtable }
  46. symconst,symsym,symtable,
  47. { modules }
  48. fmodule,
  49. { pass 1 }
  50. htypechk,
  51. node,nobj,nmem,
  52. { parser }
  53. scanner,
  54. pbase,pexpr,pdecsub,ptype;
  55. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string);
  56. var
  57. st : TSymtable;
  58. srsym : tsym;
  59. pt2 : tnode;
  60. found,
  61. first,
  62. err : boolean;
  63. i,
  64. gencount : longint;
  65. crc : cardinal;
  66. genericdef,def : tstoreddef;
  67. generictype : ttypesym;
  68. genericdeflist : TFPObjectList;
  69. generictypelist : TFPObjectList;
  70. prettyname,specializename : ansistring;
  71. ufinalspecializename,
  72. countstr,genname,ugenname,finalspecializename : string;
  73. vmtbuilder : TVMTBuilder;
  74. specializest : tsymtable;
  75. item : tobject;
  76. old_current_structdef : tabstractrecorddef;
  77. old_current_genericdef,old_current_specializedef : tstoreddef;
  78. tempst : tglobalsymtable;
  79. old_block_type: tblock_type;
  80. hashedid: thashedidstring;
  81. state : tspecializationstate;
  82. hmodule : tmodule;
  83. oldcurrent_filepos : tfileposinfo;
  84. begin
  85. { retrieve generic def that we are going to replace }
  86. genericdef:=tstoreddef(tt);
  87. tt:=nil;
  88. { either symname must be given or genericdef needs to be valid }
  89. if (symname='') and
  90. (not assigned(genericdef) or
  91. not assigned(genericdef.typesym) or
  92. (genericdef.typesym.typ<>typesym)) then
  93. internalerror(2011042701);
  94. { Only parse the parameters for recovery or
  95. for recording in genericbuf }
  96. if parse_generic then
  97. begin
  98. first:=assigned(parsedtype);
  99. if not first and not try_to_consume(_LT) then
  100. consume(_LSHARPBRACKET);
  101. gencount:=0;
  102. repeat
  103. if not first then
  104. begin
  105. pt2:=factor(false,true);
  106. pt2.free;
  107. end;
  108. first:=false;
  109. inc(gencount);
  110. until not try_to_consume(_COMMA);
  111. if not try_to_consume(_GT) then
  112. consume(_RSHARPBRACKET);
  113. { we need to return a def that can later pass some checks like
  114. whether it's an interface or not }
  115. if not assigned(tt) or (tt.typ=undefineddef) then
  116. begin
  117. if (symname='') and (df_generic in genericdef.defoptions) then
  118. { this happens in non-Delphi modes }
  119. tt:=genericdef
  120. else
  121. begin
  122. { find the corresponding generic symbol so that any checks
  123. done on the returned def will be handled correctly }
  124. str(gencount,countstr);
  125. if symname='' then
  126. genname:=ttypesym(genericdef.typesym).realname
  127. else
  128. genname:=symname;
  129. genname:=genname+'$'+countstr;
  130. ugenname:=upper(genname);
  131. { first check whether the found name is the same as that of
  132. the current def or one of its (generic) surrounding defs;
  133. this is necessary as the symbol of the generic can not yet
  134. be used for lookup as it still contains a reference to an
  135. errordef) }
  136. def:=current_genericdef;
  137. repeat
  138. if def.typ in [objectdef,recorddef] then
  139. if tabstractrecorddef(def).objname^=ugenname then
  140. begin
  141. tt:=def;
  142. break;
  143. end;
  144. def:=tstoreddef(def.owner.defowner);
  145. until not assigned(def) or not (df_generic in def.defoptions);
  146. { it's not part of the current object hierarchy, so search
  147. for the symbol }
  148. if not assigned(tt) then
  149. begin
  150. if not searchsym(ugenname,srsym,st) or
  151. (srsym.typ<>typesym) then
  152. begin
  153. identifier_not_found(genname);
  154. exit;
  155. end;
  156. tt:=ttypesym(srsym).typedef;
  157. { this happens in non-Delphi modes if we encounter a
  158. specialization of the generic class or record we're
  159. currently parsing }
  160. if (tt.typ=errordef) and assigned(current_structdef) and
  161. (current_structdef.objname^=ugenname) then
  162. tt:=current_structdef;
  163. end;
  164. end;
  165. end;
  166. exit;
  167. end;
  168. if not assigned(parsedtype) and not try_to_consume(_LT) then
  169. consume(_LSHARPBRACKET);
  170. generictypelist:=TFPObjectList.create(false);
  171. genericdeflist:=TFPObjectList.Create(false);
  172. { Parse type parameters }
  173. err:=false;
  174. { set the block type to type, so that the parsed type are returned as
  175. ttypenode (e.g. classes are in non type-compatible blocks returned as
  176. tloadvmtaddrnode) }
  177. old_block_type:=block_type;
  178. { if parsedtype is set, then the first type identifer was already parsed
  179. (happens in inline specializations) and thus we only need to parse
  180. the remaining types and do as if the first one was already given }
  181. first:=not assigned(parsedtype);
  182. if assigned(parsedtype) then
  183. begin
  184. genericdeflist.Add(parsedtype);
  185. specializename:='$'+parsedtype.typename;
  186. prettyname:=parsedtype.typesym.prettyname;
  187. end
  188. else
  189. begin
  190. specializename:='';
  191. prettyname:='';
  192. end;
  193. while not (token in [_GT,_RSHARPBRACKET]) do
  194. begin
  195. { "first" is set to false at the end of the loop! }
  196. if not first then
  197. consume(_COMMA);
  198. block_type:=bt_type;
  199. pt2:=factor(false,true);
  200. if pt2.nodetype=typen then
  201. begin
  202. if df_generic in pt2.resultdef.defoptions then
  203. Message(parser_e_no_generics_as_params);
  204. genericdeflist.Add(pt2.resultdef);
  205. if not assigned(pt2.resultdef.typesym) then
  206. message(type_e_generics_cannot_reference_itself)
  207. else
  208. begin
  209. specializename:=specializename+'$'+pt2.resultdef.typename;
  210. if first then
  211. prettyname:=prettyname+pt2.resultdef.typesym.prettyname
  212. else
  213. prettyname:=prettyname+','+pt2.resultdef.typesym.prettyname;
  214. end;
  215. end
  216. else
  217. begin
  218. Message(type_e_type_id_expected);
  219. err:=true;
  220. end;
  221. pt2.free;
  222. first:=false;
  223. end;
  224. block_type:=old_block_type;
  225. if err then
  226. begin
  227. try_to_consume(_RSHARPBRACKET);
  228. exit;
  229. end;
  230. { search a generic with the given count of params }
  231. countstr:='';
  232. str(genericdeflist.Count,countstr);
  233. { use the name of the symbol as procvars return a user friendly version
  234. of the name }
  235. if symname='' then
  236. genname:=ttypesym(genericdef.typesym).realname
  237. else
  238. genname:=symname;
  239. { in case of non-Delphi mode the type name could already be a generic
  240. def (but maybe the wrong one) }
  241. if assigned(genericdef) and
  242. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  243. begin
  244. { remove the type count suffix from the generic's name }
  245. for i:=Length(genname) downto 1 do
  246. if genname[i]='$' then
  247. begin
  248. genname:=copy(genname,1,i-1);
  249. break;
  250. end;
  251. { in case of a specialization we've only reached the specialization
  252. checksum yet }
  253. if df_specialization in genericdef.defoptions then
  254. for i:=length(genname) downto 1 do
  255. if genname[i]='$' then
  256. begin
  257. genname:=copy(genname,1,i-1);
  258. break;
  259. end;
  260. end;
  261. genname:=genname+'$'+countstr;
  262. ugenname:=upper(genname);
  263. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  264. begin
  265. if genericdef.owner.symtabletype = objectsymtable then
  266. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,srsym,st,false)
  267. else
  268. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,srsym,st);
  269. end
  270. else
  271. found:=searchsym(ugenname,srsym,st);
  272. if not found or (srsym.typ<>typesym) then
  273. begin
  274. identifier_not_found(genname);
  275. genericdeflist.Free;
  276. generictypelist.Free;
  277. exit;
  278. end;
  279. { we've found the correct def }
  280. genericdef:=tstoreddef(ttypesym(srsym).typedef);
  281. { build the new type's name }
  282. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  283. finalspecializename:=genname+'$crc'+hexstr(crc,8);
  284. ufinalspecializename:=upper(finalspecializename);
  285. prettyname:=genericdef.typesym.prettyname+'<'+prettyname+'>';
  286. { select the symtable containing the params }
  287. case genericdef.typ of
  288. procdef:
  289. st:=genericdef.GetSymtable(gs_para);
  290. objectdef,
  291. recorddef:
  292. st:=genericdef.GetSymtable(gs_record);
  293. arraydef:
  294. st:=tarraydef(genericdef).symtable;
  295. procvardef:
  296. st:=genericdef.GetSymtable(gs_para);
  297. else
  298. internalerror(200511182);
  299. end;
  300. { build the list containing the types for the generic params }
  301. gencount:=0;
  302. for i:=0 to st.SymList.Count-1 do
  303. begin
  304. srsym:=tsym(st.SymList[i]);
  305. if sp_generic_para in srsym.symoptions then
  306. begin
  307. if gencount=genericdeflist.Count then
  308. internalerror(2011042702);
  309. generictype:=ttypesym.create(srsym.realname,tdef(genericdeflist[gencount]));
  310. generictypelist.add(generictype);
  311. inc(gencount);
  312. end;
  313. end;
  314. { Special case if we are referencing the current defined object }
  315. if assigned(current_structdef) and
  316. (current_structdef.objname^=ufinalspecializename) then
  317. tt:=current_structdef;
  318. { decide in which symtable to put the specialization }
  319. if current_module.is_unit and current_module.in_interface then
  320. specializest:=current_module.globalsymtable
  321. else
  322. specializest:=current_module.localsymtable;
  323. { Can we reuse an already specialized type? }
  324. { for this first check whether we are currently specializing a nested
  325. type of the current (main) specialization (this is necessary, because
  326. during that time the symbol of the main specialization will still
  327. contain a reference to an errordef) }
  328. if not assigned(tt) and assigned(current_specializedef) then
  329. begin
  330. def:=current_specializedef;
  331. repeat
  332. if def.typ in [objectdef,recorddef] then
  333. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  334. tt:=def;
  335. break;
  336. end;
  337. def:=tstoreddef(def.owner.defowner);
  338. until not assigned(def) or not (df_specialization in def.defoptions);
  339. end;
  340. { now check whether there is a specialization somewhere else }
  341. if not assigned(tt) then
  342. begin
  343. hashedid.id:=ufinalspecializename;
  344. srsym:=tsym(specializest.findwithhash(hashedid));
  345. if assigned(srsym) then
  346. begin
  347. if srsym.typ<>typesym then
  348. internalerror(200710171);
  349. tt:=ttypesym(srsym).typedef;
  350. end
  351. else
  352. { the generic could have been specialized in the globalsymtable
  353. already, so search there as well }
  354. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  355. begin
  356. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  357. if assigned(srsym) then
  358. begin
  359. if srsym.typ<>typesym then
  360. internalerror(2011121101);
  361. tt:=ttypesym(srsym).typedef;
  362. end;
  363. end;
  364. end;
  365. if not assigned(tt) then
  366. begin
  367. specialization_init(genericdef,state);
  368. { push a temporary global symtable so that the specialization is
  369. added to the correct symtable; this symtable does not contain
  370. any other symbols, so that the type resolution can not be
  371. influenced by symbols in the current unit }
  372. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  373. symtablestack.push(tempst);
  374. { Reparse the original type definition }
  375. if not err then
  376. begin
  377. if parse_class_parent then
  378. begin
  379. old_current_structdef:=current_structdef;
  380. old_current_genericdef:=current_genericdef;
  381. old_current_specializedef:=current_specializedef;
  382. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  383. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  384. else
  385. current_structdef:=nil;
  386. current_genericdef:=nil;
  387. current_specializedef:=nil;
  388. end;
  389. { First a new typesym so we can reuse this specialization and
  390. references to this specialization can be handled }
  391. srsym:=ttypesym.create(finalspecializename,generrordef);
  392. specializest.insert(srsym);
  393. { specializations are declarations as such it is the wisest to
  394. declare set the blocktype to "type"; otherwise we'll
  395. experience unexpected side effects like the addition of
  396. classrefdefs if we have a generic that's derived from another
  397. generic }
  398. old_block_type:=block_type;
  399. block_type:=bt_type;
  400. if not assigned(genericdef.generictokenbuf) then
  401. internalerror(200511171);
  402. hmodule:=find_module_from_symtable(genericdef.owner);
  403. if hmodule=nil then
  404. internalerror(2012051202);
  405. oldcurrent_filepos:=current_filepos;
  406. { use the index the module got from the current compilation process }
  407. current_filepos.moduleindex:=hmodule.unit_index;
  408. current_tokenpos:=current_filepos;
  409. current_scanner.startreplaytokens(genericdef.generictokenbuf,
  410. genericdef.change_endian);
  411. read_named_type(tt,srsym,genericdef,generictypelist,false);
  412. current_filepos:=oldcurrent_filepos;
  413. ttypesym(srsym).typedef:=tt;
  414. tt.typesym:=srsym;
  415. if _prettyname<>'' then
  416. ttypesym(tt.typesym).fprettyname:=_prettyname
  417. else
  418. ttypesym(tt.typesym).fprettyname:=prettyname;
  419. { Note regarding hint directives:
  420. There is no need to remove the flags for them from the
  421. specialized generic symbol, because hint directives that
  422. follow the specialization are handled by the code in
  423. pdecl.types_dec and added to the type symbol.
  424. E.g.: TFoo = TBar<Blubb> deprecated;
  425. Here the symbol TBar$1$Blubb will contain the
  426. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  427. case tt.typ of
  428. { Build VMT indexes for classes and read hint directives }
  429. objectdef:
  430. begin
  431. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  432. consume(_SEMICOLON);
  433. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  434. vmtbuilder.generate_vmt;
  435. vmtbuilder.free;
  436. end;
  437. { handle params, calling convention, etc }
  438. procvardef:
  439. begin
  440. if not check_proc_directive(true) then
  441. begin
  442. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  443. consume(_SEMICOLON);
  444. end;
  445. parse_var_proc_directives(ttypesym(srsym));
  446. handle_calling_convention(tprocvardef(tt));
  447. if try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg) then
  448. consume(_SEMICOLON);
  449. end;
  450. else
  451. { parse hint directives for records and arrays }
  452. begin
  453. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  454. consume(_SEMICOLON);
  455. end;
  456. end;
  457. { Consume the semicolon if it is also recorded }
  458. try_to_consume(_SEMICOLON);
  459. block_type:=old_block_type;
  460. if parse_class_parent then
  461. begin
  462. current_structdef:=old_current_structdef;
  463. current_genericdef:=old_current_genericdef;
  464. current_specializedef:=old_current_specializedef;
  465. end;
  466. end;
  467. { extract all created symbols and defs from the temporary symtable
  468. and add them to the specializest }
  469. for i:=tempst.SymList.Count-1 downto 0 do
  470. begin
  471. item:=tempst.SymList.Items[i];
  472. { using changeowner the symbol is automatically added to the
  473. new symtable }
  474. tsym(item).ChangeOwner(specializest);
  475. end;
  476. for i:=tempst.DefList.Count-1 downto 0 do
  477. begin
  478. item:=tempst.DefList.Items[i];
  479. { using changeowner the def is automatically added to the new
  480. symtable }
  481. tdef(item).ChangeOwner(specializest);
  482. end;
  483. tempst.free;
  484. specialization_done(state);
  485. end;
  486. if not (token in [_GT, _RSHARPBRACKET]) then
  487. begin
  488. consume(_RSHARPBRACKET);
  489. exit;
  490. end
  491. else
  492. consume(token);
  493. genericdeflist.free;
  494. generictypelist.free;
  495. if assigned(genericdef) then
  496. begin
  497. { check the hints of the found generic symbol }
  498. srsym:=genericdef.typesym;
  499. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  500. end;
  501. end;
  502. function parse_generic_parameters:TFPObjectList;
  503. var
  504. generictype : ttypesym;
  505. begin
  506. result:=TFPObjectList.Create(false);
  507. repeat
  508. if token=_ID then
  509. begin
  510. generictype:=ttypesym.create(orgpattern,cundefinedtype);
  511. include(generictype.symoptions,sp_generic_para);
  512. result.add(generictype);
  513. end;
  514. consume(_ID);
  515. until not try_to_consume(_COMMA) ;
  516. end;
  517. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  518. var
  519. i: longint;
  520. generictype: ttypesym;
  521. st: tsymtable;
  522. begin
  523. def.genericdef:=genericdef;
  524. if not assigned(genericlist) then
  525. exit;
  526. case def.typ of
  527. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  528. arraydef: st:=tarraydef(def).symtable;
  529. procvardef,procdef: st:=tabstractprocdef(def).parast;
  530. else
  531. internalerror(201101020);
  532. end;
  533. for i:=0 to genericlist.count-1 do
  534. begin
  535. generictype:=ttypesym(genericlist[i]);
  536. if generictype.typedef.typ=undefineddef then
  537. include(def.defoptions,df_generic)
  538. else
  539. include(def.defoptions,df_specialization);
  540. st.insert(generictype);
  541. end;
  542. end;
  543. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfpobjectlist);
  544. var
  545. gensym : ttypesym;
  546. begin
  547. { for generics in non-Delphi modes we insert a private type symbol
  548. that has the same base name as the currently parsed generic and
  549. that references this defs }
  550. if not (m_delphi in current_settings.modeswitches) and
  551. (
  552. (
  553. parse_generic and
  554. assigned(genericlist) and
  555. (genericlist.count>0)
  556. ) or
  557. (
  558. assigned(current_specializedef) and
  559. assigned(current_structdef.genericdef) and
  560. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  561. (pos('$',name)>0)
  562. )
  563. ) then
  564. begin
  565. { we need to pass nil as def here, because the constructor wants
  566. to set the typesym of the def which is not what we want }
  567. gensym:=ttypesym.create(copy(name,1,pos('$',name)-1),nil);
  568. gensym.typedef:=current_structdef;
  569. include(gensym.symoptions,sp_internal);
  570. { the symbol should be only visible to the generic class
  571. itself }
  572. gensym.visibility:=vis_strictprivate;
  573. symtablestack.top.insert(gensym);
  574. end;
  575. end;
  576. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  577. var
  578. pu : tused_unit;
  579. hmodule : tmodule;
  580. unitsyms : TFPHashObjectList;
  581. sym : tsym;
  582. i : Integer;
  583. begin
  584. if not assigned(genericdef) then
  585. internalerror(200705151);
  586. { Setup symtablestack at definition time
  587. to get types right, however this is not perfect, we should probably record
  588. the resolved symbols }
  589. state.oldsymtablestack:=symtablestack;
  590. state.oldextendeddefs:=current_module.extendeddefs;
  591. current_module.extendeddefs:=TFPHashObjectList.create(true);
  592. symtablestack:=tdefawaresymtablestack.create;
  593. hmodule:=find_module_from_symtable(genericdef.owner);
  594. if hmodule=nil then
  595. internalerror(200705152);
  596. { collect all unit syms in the generic's unit as we need to establish
  597. their unitsym.module link again so that unit identifiers can be used }
  598. unitsyms:=tfphashobjectlist.create(false);
  599. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  600. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  601. begin
  602. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  603. if sym.typ=unitsym then
  604. unitsyms.add(upper(sym.realname),sym);
  605. end;
  606. { add all interface units to the new symtable stack }
  607. pu:=tused_unit(hmodule.used_units.first);
  608. while assigned(pu) do
  609. begin
  610. if not assigned(pu.u.globalsymtable) then
  611. internalerror(200705153);
  612. symtablestack.push(pu.u.globalsymtable);
  613. sym:=tsym(unitsyms.find(pu.u.modulename^));
  614. if assigned(sym) and not assigned(tunitsym(sym).module) then
  615. tunitsym(sym).module:=pu.u;
  616. pu:=tused_unit(pu.next);
  617. end;
  618. unitsyms.free;
  619. if assigned(hmodule.globalsymtable) then
  620. symtablestack.push(hmodule.globalsymtable);
  621. { push the localsymtable if needed }
  622. if (hmodule<>current_module) or not current_module.in_interface then
  623. symtablestack.push(hmodule.localsymtable);
  624. end;
  625. procedure specialization_done(var state: tspecializationstate);
  626. begin
  627. { Restore symtablestack }
  628. current_module.extendeddefs.free;
  629. current_module.extendeddefs:=state.oldextendeddefs;
  630. symtablestack.free;
  631. symtablestack:=state.oldsymtablestack;
  632. { clear the state record to be on the safe side }
  633. fillchar(state, sizeof(state), 0);
  634. end;
  635. end.