pgenutil.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. read_named_type(tt,srsym,genericdef,generictypelist,false);
  361. current_filepos:=oldcurrent_filepos;
  362. ttypesym(srsym).typedef:=tt;
  363. tt.typesym:=srsym;
  364. if _prettyname<>'' then
  365. ttypesym(tt.typesym).fprettyname:=_prettyname
  366. else
  367. ttypesym(tt.typesym).fprettyname:=prettyname;
  368. { Note regarding hint directives:
  369. There is no need to remove the flags for them from the
  370. specialized generic symbol, because hint directives that
  371. follow the specialization are handled by the code in
  372. pdecl.types_dec and added to the type symbol.
  373. E.g.: TFoo = TBar<Blubb> deprecated;
  374. Here the symbol TBar$1$Blubb will contain the
  375. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  376. case tt.typ of
  377. { Build VMT indexes for classes and read hint directives }
  378. objectdef:
  379. begin
  380. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  381. consume(_SEMICOLON);
  382. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  383. vmtbuilder.generate_vmt;
  384. vmtbuilder.free;
  385. end;
  386. { handle params, calling convention, etc }
  387. procvardef:
  388. begin
  389. if not check_proc_directive(true) then
  390. begin
  391. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  392. consume(_SEMICOLON);
  393. end;
  394. parse_var_proc_directives(ttypesym(srsym));
  395. handle_calling_convention(tprocvardef(tt));
  396. if try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg) then
  397. consume(_SEMICOLON);
  398. end;
  399. else
  400. { parse hint directives for records and arrays }
  401. begin
  402. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  403. consume(_SEMICOLON);
  404. end;
  405. end;
  406. { Consume the semicolon if it is also recorded }
  407. try_to_consume(_SEMICOLON);
  408. block_type:=old_block_type;
  409. if parse_class_parent then
  410. begin
  411. current_structdef:=old_current_structdef;
  412. current_genericdef:=old_current_genericdef;
  413. current_specializedef:=old_current_specializedef;
  414. end;
  415. end;
  416. { extract all created symbols and defs from the temporary symtable
  417. and add them to the specializest }
  418. for i:=tempst.SymList.Count-1 downto 0 do
  419. begin
  420. item:=tempst.SymList.Items[i];
  421. { using changeowner the symbol is automatically added to the
  422. new symtable }
  423. tsym(item).ChangeOwner(specializest);
  424. end;
  425. for i:=tempst.DefList.Count-1 downto 0 do
  426. begin
  427. item:=tempst.DefList.Items[i];
  428. { using changeowner the def is automatically added to the new
  429. symtable }
  430. tdef(item).ChangeOwner(specializest);
  431. end;
  432. { if a generic was declared during the specialization we need to
  433. flag the specialize symtable accordingly }
  434. if sto_has_generic in tempst.tableoptions then
  435. specializest.includeoption(sto_has_generic);
  436. tempst.free;
  437. specialization_done(state);
  438. end;
  439. if not (token in [_GT, _RSHARPBRACKET]) then
  440. begin
  441. consume(_RSHARPBRACKET);
  442. exit;
  443. end
  444. else
  445. consume(token);
  446. genericdeflist.free;
  447. generictypelist.free;
  448. if assigned(genericdef) then
  449. begin
  450. { check the hints of the found generic symbol }
  451. srsym:=genericdef.typesym;
  452. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  453. end;
  454. end;
  455. function parse_generic_parameters:TFPObjectList;
  456. var
  457. generictype : ttypesym;
  458. begin
  459. result:=TFPObjectList.Create(false);
  460. repeat
  461. if token=_ID then
  462. begin
  463. generictype:=ttypesym.create(orgpattern,cundefinedtype);
  464. include(generictype.symoptions,sp_generic_para);
  465. result.add(generictype);
  466. end;
  467. consume(_ID);
  468. until not try_to_consume(_COMMA) ;
  469. end;
  470. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;out prettyname,specializename:ansistring;parsedtype:tdef):boolean;
  471. var
  472. old_block_type : tblock_type;
  473. first : boolean;
  474. typeparam : tnode;
  475. begin
  476. result:=true;
  477. if genericdeflist=nil then
  478. internalerror(2012061401);
  479. { set the block type to type, so that the parsed type are returned as
  480. ttypenode (e.g. classes are in non type-compatible blocks returned as
  481. tloadvmtaddrnode) }
  482. old_block_type:=block_type;
  483. { if parsedtype is set, then the first type identifer was already parsed
  484. (happens in inline specializations) and thus we only need to parse
  485. the remaining types and do as if the first one was already given }
  486. first:=not assigned(parsedtype);
  487. if assigned(parsedtype) then
  488. begin
  489. genericdeflist.Add(parsedtype);
  490. specializename:='$'+parsedtype.typename;
  491. prettyname:=parsedtype.typesym.prettyname;
  492. end
  493. else
  494. begin
  495. specializename:='';
  496. prettyname:='';
  497. end;
  498. while not (token in [_GT,_RSHARPBRACKET]) do
  499. begin
  500. { "first" is set to false at the end of the loop! }
  501. if not first then
  502. consume(_COMMA);
  503. block_type:=bt_type;
  504. typeparam:=factor(false,true);
  505. if typeparam.nodetype=typen then
  506. begin
  507. if df_generic in typeparam.resultdef.defoptions then
  508. Message(parser_e_no_generics_as_params);
  509. genericdeflist.Add(typeparam.resultdef);
  510. if not assigned(typeparam.resultdef.typesym) then
  511. message(type_e_generics_cannot_reference_itself)
  512. else
  513. begin
  514. specializename:=specializename+'$'+typeparam.resultdef.typename;
  515. if first then
  516. prettyname:=prettyname+typeparam.resultdef.typesym.prettyname
  517. else
  518. prettyname:=prettyname+','+typeparam.resultdef.typesym.prettyname;
  519. end;
  520. end
  521. else
  522. begin
  523. Message(type_e_type_id_expected);
  524. result:=false;
  525. end;
  526. typeparam.free;
  527. first:=false;
  528. end;
  529. block_type:=old_block_type;
  530. end;
  531. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  532. var
  533. i: longint;
  534. generictype: ttypesym;
  535. st: tsymtable;
  536. begin
  537. def.genericdef:=genericdef;
  538. if not assigned(genericlist) then
  539. exit;
  540. case def.typ of
  541. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  542. arraydef: st:=tarraydef(def).symtable;
  543. procvardef,procdef: st:=tabstractprocdef(def).parast;
  544. else
  545. internalerror(201101020);
  546. end;
  547. for i:=0 to genericlist.count-1 do
  548. begin
  549. generictype:=ttypesym(genericlist[i]);
  550. if generictype.typedef.typ=undefineddef then
  551. include(def.defoptions,df_generic)
  552. else
  553. include(def.defoptions,df_specialization);
  554. st.insert(generictype);
  555. end;
  556. end;
  557. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfpobjectlist);
  558. var
  559. gensym : ttypesym;
  560. begin
  561. { for generics in non-Delphi modes we insert a private type symbol
  562. that has the same base name as the currently parsed generic and
  563. that references this defs }
  564. if not (m_delphi in current_settings.modeswitches) and
  565. (
  566. (
  567. parse_generic and
  568. assigned(genericlist) and
  569. (genericlist.count>0)
  570. ) or
  571. (
  572. assigned(current_specializedef) and
  573. assigned(current_structdef.genericdef) and
  574. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  575. (pos('$',name)>0)
  576. )
  577. ) then
  578. begin
  579. { we need to pass nil as def here, because the constructor wants
  580. to set the typesym of the def which is not what we want }
  581. gensym:=ttypesym.create(copy(name,1,pos('$',name)-1),nil);
  582. gensym.typedef:=current_structdef;
  583. include(gensym.symoptions,sp_internal);
  584. { the symbol should be only visible to the generic class
  585. itself }
  586. gensym.visibility:=vis_strictprivate;
  587. symtablestack.top.insert(gensym);
  588. end;
  589. end;
  590. function generate_generic_name(const name:tidstring;specializename:ansistring):tidstring;
  591. var
  592. crc : cardinal;
  593. begin
  594. if specializename='' then
  595. internalerror(2012061901);
  596. { build the new type's name }
  597. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  598. result:=name+'$crc'+hexstr(crc,8);
  599. end;
  600. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  601. var
  602. pu : tused_unit;
  603. hmodule : tmodule;
  604. unitsyms : TFPHashObjectList;
  605. sym : tsym;
  606. i : Integer;
  607. begin
  608. if not assigned(genericdef) then
  609. internalerror(200705151);
  610. { Setup symtablestack at definition time
  611. to get types right, however this is not perfect, we should probably record
  612. the resolved symbols }
  613. state.oldsymtablestack:=symtablestack;
  614. state.oldextendeddefs:=current_module.extendeddefs;
  615. current_module.extendeddefs:=TFPHashObjectList.create(true);
  616. symtablestack:=tdefawaresymtablestack.create;
  617. hmodule:=find_module_from_symtable(genericdef.owner);
  618. if hmodule=nil then
  619. internalerror(200705152);
  620. { collect all unit syms in the generic's unit as we need to establish
  621. their unitsym.module link again so that unit identifiers can be used }
  622. unitsyms:=tfphashobjectlist.create(false);
  623. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  624. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  625. begin
  626. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  627. if sym.typ=unitsym then
  628. unitsyms.add(upper(sym.realname),sym);
  629. end;
  630. { add all units if we are specializing inside the current unit (as the
  631. generic could have been declared in the implementation part), but load
  632. only interface units, if we are in a different unit as then the generic
  633. needs to be in the interface section }
  634. pu:=tused_unit(hmodule.used_units.first);
  635. while assigned(pu) do
  636. begin
  637. if (hmodule<>current_module) and not pu.in_interface then
  638. begin
  639. pu:=tused_unit(pu.next);
  640. continue;
  641. end;
  642. if not assigned(pu.u.globalsymtable) then
  643. internalerror(200705153);
  644. symtablestack.push(pu.u.globalsymtable);
  645. sym:=tsym(unitsyms.find(pu.u.modulename^));
  646. if assigned(sym) and not assigned(tunitsym(sym).module) then
  647. tunitsym(sym).module:=pu.u;
  648. pu:=tused_unit(pu.next);
  649. end;
  650. unitsyms.free;
  651. if assigned(hmodule.globalsymtable) then
  652. symtablestack.push(hmodule.globalsymtable);
  653. { push the localsymtable if needed }
  654. if (hmodule<>current_module) or not current_module.in_interface then
  655. symtablestack.push(hmodule.localsymtable);
  656. end;
  657. procedure specialization_done(var state: tspecializationstate);
  658. begin
  659. { Restore symtablestack }
  660. current_module.extendeddefs.free;
  661. current_module.extendeddefs:=state.oldextendeddefs;
  662. symtablestack.free;
  663. symtablestack:=state.oldsymtablestack;
  664. { clear the state record to be on the safe side }
  665. fillchar(state, sizeof(state), 0);
  666. end;
  667. end.