pgenutil.pas 28 KB

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