pgenutil.pas 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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;parsedpos:tfileposinfo);
  29. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);
  30. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  31. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  32. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  33. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  34. function generate_generic_name(const name:tidstring;specializename:ansistring):tidstring;
  35. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  36. function resolve_generic_dummysym(const name:tidstring):tsym;
  37. function could_be_generic(const name:tidstring):boolean;inline;
  38. type
  39. tspecializationstate = record
  40. oldsymtablestack : tsymtablestack;
  41. oldextendeddefs : TFPHashObjectList;
  42. oldgenericdummysyms: tfphashobjectlist;
  43. end;
  44. procedure specialization_init(genericdef:tdef;var state:tspecializationstate);
  45. procedure specialization_done(var state:tspecializationstate);
  46. implementation
  47. uses
  48. { common }
  49. cutils,fpccrc,
  50. { global }
  51. globals,tokens,verbose,finput,
  52. { symtable }
  53. symconst,symsym,symtable,defcmp,procinfo,
  54. { modules }
  55. fmodule,
  56. { pass 1 }
  57. htypechk,
  58. node,nobj,nmem,
  59. { parser }
  60. scanner,
  61. pbase,pexpr,pdecsub,ptype;
  62. procedure maybe_add_waiting_unit(tt:tdef);
  63. var
  64. hmodule : tmodule;
  65. begin
  66. if not assigned(tt) or
  67. not (df_generic in tt.defoptions) then
  68. exit;
  69. hmodule:=find_module_from_symtable(tt.owner);
  70. if not assigned(hmodule) then
  71. internalerror(2012092401);
  72. if hmodule=current_module then
  73. exit;
  74. if hmodule.state<>ms_compiled then
  75. begin
  76. {$ifdef DEBUG_UNITWAITING}
  77. Writeln('Unit ', current_module.modulename^,
  78. ' waiting for ', hmodule.modulename^);
  79. {$endif DEBUG_UNITWAITING}
  80. if current_module.waitingforunit.indexof(hmodule)<0 then
  81. current_module.waitingforunit.add(hmodule);
  82. if hmodule.waitingunits.indexof(current_module)<0 then
  83. hmodule.waitingunits.add(current_module);
  84. end;
  85. end;
  86. function check_generic_constraints(genericdef:tstoreddef;paradeflist:tfpobjectlist;poslist:tfplist):boolean;
  87. var
  88. i,j,
  89. intfcount : longint;
  90. formaldef,
  91. paradef : tstoreddef;
  92. objdef,
  93. paraobjdef,
  94. formalobjdef : tobjectdef;
  95. intffound : boolean;
  96. filepos : tfileposinfo;
  97. begin
  98. { check whether the given specialization parameters fit to the eventual
  99. constraints of the generic }
  100. if not assigned(genericdef.genericparas) or (genericdef.genericparas.count=0) then
  101. internalerror(2012101001);
  102. if genericdef.genericparas.count<>paradeflist.count then
  103. internalerror(2012101002);
  104. if paradeflist.count<>poslist.count then
  105. internalerror(2012120801);
  106. result:=true;
  107. for i:=0 to genericdef.genericparas.count-1 do
  108. begin
  109. filepos:=pfileposinfo(poslist[i])^;
  110. formaldef:=tstoreddef(ttypesym(genericdef.genericparas[i]).typedef);
  111. if formaldef.typ=undefineddef then
  112. { the parameter is of unspecified type, so no need to check }
  113. continue;
  114. if not (df_genconstraint in formaldef.defoptions) or
  115. not assigned(formaldef.genconstraintdata) then
  116. internalerror(2013021602);
  117. paradef:=tstoreddef(paradeflist[i]);
  118. { undefineddef is compatible with anything }
  119. if formaldef.typ=undefineddef then
  120. continue;
  121. if paradef.typ<>formaldef.typ then
  122. begin
  123. case formaldef.typ of
  124. recorddef:
  125. MessagePos(filepos,type_e_record_type_expected);
  126. objectdef:
  127. case tobjectdef(formaldef).objecttype of
  128. odt_class,
  129. odt_javaclass:
  130. MessagePos1(filepos,type_e_class_type_expected,paradef.typename);
  131. odt_interfacecom,
  132. odt_interfacecorba,
  133. odt_dispinterface,
  134. odt_interfacejava:
  135. MessagePos1(filepos,type_e_interface_type_expected,paradef.typename);
  136. else
  137. internalerror(2012101003);
  138. end;
  139. errordef:
  140. { ignore }
  141. ;
  142. else
  143. internalerror(2012101004);
  144. end;
  145. result:=false;
  146. end
  147. else
  148. begin
  149. { the paradef types are the same, so do special checks for the
  150. cases in which they are needed }
  151. if formaldef.typ=objectdef then
  152. begin
  153. paraobjdef:=tobjectdef(paradef);
  154. formalobjdef:=tobjectdef(formaldef);
  155. if not (formalobjdef.objecttype in [odt_class,odt_javaclass,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface]) then
  156. internalerror(2012101102);
  157. if formalobjdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface] then
  158. begin
  159. { this is either a concerete interface or class type (the
  160. latter without specific implemented interfaces) }
  161. case paraobjdef.objecttype of
  162. odt_interfacecom,
  163. odt_interfacecorba,
  164. odt_interfacejava,
  165. odt_dispinterface:
  166. if not def_is_related(paraobjdef,formalobjdef.childof) then
  167. begin
  168. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  169. result:=false;
  170. end;
  171. odt_class,
  172. odt_javaclass:
  173. begin
  174. objdef:=paraobjdef;
  175. intffound:=false;
  176. while assigned(objdef) do
  177. begin
  178. for j:=0 to objdef.implementedinterfaces.count-1 do
  179. if timplementedinterface(objdef.implementedinterfaces[j]).intfdef=formalobjdef.childof then
  180. begin
  181. intffound:=true;
  182. break;
  183. end;
  184. if intffound then
  185. break;
  186. objdef:=objdef.childof;
  187. end;
  188. result:=intffound;
  189. if not result then
  190. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,formalobjdef.childof.typename);
  191. end;
  192. else
  193. begin
  194. MessagePos1(filepos,type_e_class_or_interface_type_expected,paraobjdef.typename);
  195. result:=false;
  196. end;
  197. end;
  198. end
  199. else
  200. begin
  201. { this is either a "class" or a concrete instance with
  202. or without implemented interfaces }
  203. if not (paraobjdef.objecttype in [odt_class,odt_javaclass]) then
  204. begin
  205. MessagePos1(filepos,type_e_class_type_expected,paraobjdef.typename);
  206. result:=false;
  207. continue;
  208. end;
  209. if assigned(formalobjdef.childof) and
  210. not def_is_related(paradef,formalobjdef.childof) then
  211. begin
  212. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  213. result:=false;
  214. end;
  215. intfcount:=0;
  216. for j:=0 to formalobjdef.implementedinterfaces.count-1 do
  217. begin
  218. objdef:=paraobjdef;
  219. while assigned(objdef) do
  220. begin
  221. intffound:=assigned(
  222. find_implemented_interface(objdef,
  223. timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef
  224. )
  225. );
  226. if intffound then
  227. break;
  228. objdef:=objdef.childof;
  229. end;
  230. if intffound then
  231. inc(intfcount)
  232. else
  233. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef.typename);
  234. end;
  235. if intfcount<>formalobjdef.implementedinterfaces.count then
  236. result:=false;
  237. end;
  238. end;
  239. end;
  240. end;
  241. end;
  242. function parse_generic_specialization_types_internal(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring;parsedtype:tdef;parsedpos:tfileposinfo):boolean;
  243. var
  244. old_block_type : tblock_type;
  245. first : boolean;
  246. typeparam : tnode;
  247. parampos : pfileposinfo;
  248. tmpparampos : tfileposinfo;
  249. begin
  250. result:=true;
  251. if genericdeflist=nil then
  252. internalerror(2012061401);
  253. { set the block type to type, so that the parsed type are returned as
  254. ttypenode (e.g. classes are in non type-compatible blocks returned as
  255. tloadvmtaddrnode) }
  256. old_block_type:=block_type;
  257. { if parsedtype is set, then the first type identifer was already parsed
  258. (happens in inline specializations) and thus we only need to parse
  259. the remaining types and do as if the first one was already given }
  260. first:=not assigned(parsedtype);
  261. if assigned(parsedtype) then
  262. begin
  263. genericdeflist.Add(parsedtype);
  264. specializename:='$'+parsedtype.fulltypename;
  265. prettyname:=parsedtype.typesym.prettyname;
  266. if assigned(poslist) then
  267. begin
  268. New(parampos);
  269. parampos^:=parsedpos;
  270. poslist.add(parampos);
  271. end;
  272. end
  273. else
  274. begin
  275. specializename:='';
  276. prettyname:='';
  277. end;
  278. while not (token in [_GT,_RSHARPBRACKET]) do
  279. begin
  280. { "first" is set to false at the end of the loop! }
  281. if not first then
  282. consume(_COMMA);
  283. block_type:=bt_type;
  284. tmpparampos:=current_filepos;
  285. typeparam:=factor(false,true);
  286. if typeparam.nodetype=typen then
  287. begin
  288. if tstoreddef(typeparam.resultdef).is_generic and
  289. (
  290. not parse_generic or
  291. not defs_belong_to_same_generic(typeparam.resultdef,current_genericdef)
  292. ) then
  293. Message(parser_e_no_generics_as_params);
  294. if assigned(poslist) then
  295. begin
  296. New(parampos);
  297. parampos^:=tmpparampos;
  298. poslist.add(parampos);
  299. end;
  300. genericdeflist.Add(typeparam.resultdef);
  301. if not assigned(typeparam.resultdef.typesym) then
  302. message(type_e_generics_cannot_reference_itself)
  303. else
  304. begin
  305. { we use the full name of the type to uniquely identify it }
  306. specializename:=specializename+'$'+typeparam.resultdef.fulltypename;
  307. if not first then
  308. prettyname:=prettyname+',';
  309. prettyname:=prettyname+typeparam.resultdef.fullownerhierarchyname+typeparam.resultdef.typesym.prettyname;
  310. end;
  311. end
  312. else
  313. begin
  314. Message(type_e_type_id_expected);
  315. result:=false;
  316. end;
  317. typeparam.free;
  318. first:=false;
  319. end;
  320. block_type:=old_block_type;
  321. end;
  322. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  323. var
  324. dummypos : tfileposinfo;
  325. begin
  326. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  327. result:=parse_generic_specialization_types_internal(genericdeflist,poslist,prettyname,specializename,nil,dummypos);
  328. end;
  329. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);
  330. var
  331. dummypos : tfileposinfo;
  332. begin
  333. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  334. generate_specialization(tt,parse_class_parent,_prettyname,nil,'',dummypos);
  335. end;
  336. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);
  337. procedure unset_forwarddef(def: tdef);
  338. var
  339. st : TSymtable;
  340. i : longint;
  341. begin
  342. case def.typ of
  343. procdef:
  344. tprocdef(def).forwarddef:=false;
  345. objectdef,
  346. recorddef:
  347. begin
  348. st:=def.getsymtable(gs_record);
  349. for i:=0 to st.deflist.count-1 do
  350. unset_forwarddef(tdef(st.deflist[i]));
  351. end;
  352. end;
  353. end;
  354. var
  355. st : TSymtable;
  356. srsym : tsym;
  357. pt2 : tnode;
  358. errorrecovery,
  359. found,
  360. first,
  361. err : boolean;
  362. errval,
  363. i,
  364. gencount : longint;
  365. genericdef,def : tstoreddef;
  366. generictype : ttypesym;
  367. genericdeflist : TFPObjectList;
  368. generictypelist : tfphashobjectlist;
  369. prettyname,specializename : ansistring;
  370. ufinalspecializename,
  371. countstr,genname,ugenname,finalspecializename : string;
  372. vmtbuilder : TVMTBuilder;
  373. specializest : tsymtable;
  374. item : tobject;
  375. old_current_structdef : tabstractrecorddef;
  376. old_current_genericdef,old_current_specializedef : tstoreddef;
  377. tempst : tglobalsymtable;
  378. old_block_type: tblock_type;
  379. hashedid: thashedidstring;
  380. state : tspecializationstate;
  381. hmodule : tmodule;
  382. oldcurrent_filepos : tfileposinfo;
  383. poslist : tfplist;
  384. recordbuf: tdynamicarray;
  385. begin
  386. { retrieve generic def that we are going to replace }
  387. genericdef:=tstoreddef(tt);
  388. tt:=nil;
  389. { either symname must be given or genericdef needs to be valid }
  390. errorrecovery:=false;
  391. if (symname='') and
  392. (not assigned(genericdef) or
  393. not assigned(genericdef.typesym) or
  394. (genericdef.typesym.typ<>typesym)) then
  395. begin
  396. errorrecovery:=true;
  397. tt:=generrordef;
  398. end;
  399. { Only parse the parameters for recovery or
  400. for recording in genericbuf }
  401. if errorrecovery then
  402. begin
  403. first:=assigned(parsedtype);
  404. if not first and not try_to_consume(_LT) then
  405. consume(_LSHARPBRACKET);
  406. gencount:=0;
  407. { handle "<>" }
  408. if not first and ((token=_RSHARPBRACKET) or (token=_GT)) then
  409. Message(type_e_type_id_expected)
  410. else
  411. repeat
  412. if not first then
  413. begin
  414. pt2:=factor(false,true);
  415. pt2.free;
  416. end;
  417. first:=false;
  418. inc(gencount);
  419. until not try_to_consume(_COMMA);
  420. if not try_to_consume(_GT) then
  421. consume(_RSHARPBRACKET);
  422. { we need to return a def that can later pass some checks like
  423. whether it's an interface or not }
  424. if not errorrecovery and
  425. (not assigned(tt) or (tt.typ=undefineddef)) then
  426. begin
  427. if (symname='') and genericdef.is_generic then
  428. { this happens in non-Delphi modes }
  429. tt:=genericdef
  430. else
  431. begin
  432. { find the corresponding generic symbol so that any checks
  433. done on the returned def will be handled correctly }
  434. str(gencount,countstr);
  435. if symname='' then
  436. genname:=ttypesym(genericdef.typesym).realname
  437. else
  438. genname:=symname;
  439. genname:=genname+'$'+countstr;
  440. ugenname:=upper(genname);
  441. { first check whether the found name is the same as that of
  442. the current def or one of its (generic) surrounding defs;
  443. this is necessary as the symbol of the generic can not yet
  444. be used for lookup as it still contains a reference to an
  445. errordef) }
  446. def:=current_genericdef;
  447. repeat
  448. if def.typ in [objectdef,recorddef] then
  449. if tabstractrecorddef(def).objname^=ugenname then
  450. begin
  451. tt:=def;
  452. break;
  453. end;
  454. def:=tstoreddef(def.owner.defowner);
  455. until not assigned(def) or not (df_generic in def.defoptions);
  456. { it's not part of the current object hierarchy, so search
  457. for the symbol }
  458. if not assigned(tt) then
  459. begin
  460. srsym:=nil;
  461. if not searchsym(ugenname,srsym,st) or
  462. (srsym.typ<>typesym) then
  463. begin
  464. identifier_not_found(genname);
  465. tt:=generrordef;
  466. exit;
  467. end;
  468. tt:=ttypesym(srsym).typedef;
  469. { this happens in non-Delphi modes if we encounter a
  470. specialization of the generic class or record we're
  471. currently parsing }
  472. if (tt.typ=errordef) and assigned(current_structdef) and
  473. (current_structdef.objname^=ugenname) then
  474. tt:=current_structdef;
  475. end;
  476. end;
  477. end;
  478. exit;
  479. end;
  480. if not assigned(parsedtype) and not try_to_consume(_LT) then
  481. begin
  482. consume(_LSHARPBRACKET);
  483. { handle "<>" }
  484. if (token=_GT) or (token=_RSHARPBRACKET) then
  485. begin
  486. Message(type_e_type_id_expected);
  487. if not try_to_consume(_GT) then
  488. try_to_consume(_RSHARPBRACKET);
  489. tt:=generrordef;
  490. exit;
  491. end;
  492. end;
  493. genericdeflist:=TFPObjectList.Create(false);
  494. poslist:=tfplist.create;
  495. { Parse type parameters }
  496. err:=not parse_generic_specialization_types_internal(genericdeflist,poslist,prettyname,specializename,parsedtype,parsedpos);
  497. if err then
  498. begin
  499. if not try_to_consume(_GT) then
  500. try_to_consume(_RSHARPBRACKET);
  501. genericdeflist.free;
  502. for i:=0 to poslist.count-1 do
  503. dispose(pfileposinfo(poslist[i]));
  504. poslist.free;
  505. tt:=generrordef;
  506. exit;
  507. end;
  508. { use the name of the symbol as procvars return a user friendly version
  509. of the name }
  510. if symname='' then
  511. genname:=ttypesym(genericdef.typesym).realname
  512. else
  513. genname:=symname;
  514. { in case of non-Delphi mode the type name could already be a generic
  515. def (but maybe the wrong one) }
  516. if assigned(genericdef) and
  517. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  518. begin
  519. { remove the type count suffix from the generic's name }
  520. for i:=Length(genname) downto 1 do
  521. if genname[i]='$' then
  522. begin
  523. genname:=copy(genname,1,i-1);
  524. break;
  525. end;
  526. { in case of a specialization we've only reached the specialization
  527. checksum yet }
  528. if df_specialization in genericdef.defoptions then
  529. for i:=length(genname) downto 1 do
  530. if genname[i]='$' then
  531. begin
  532. genname:=copy(genname,1,i-1);
  533. break;
  534. end;
  535. end
  536. else
  537. begin
  538. split_generic_name(genname,ugenname,gencount);
  539. if genname<>ugenname then
  540. genname:=ugenname;
  541. end;
  542. { search a generic with the given count of params }
  543. countstr:='';
  544. str(genericdeflist.Count,countstr);
  545. genname:=genname+'$'+countstr;
  546. ugenname:=upper(genname);
  547. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  548. begin
  549. if genericdef.owner.symtabletype = objectsymtable then
  550. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,srsym,st,[])
  551. else
  552. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,srsym,st);
  553. end
  554. else
  555. found:=searchsym(ugenname,srsym,st);
  556. if not found or (srsym.typ<>typesym) then
  557. begin
  558. identifier_not_found(genname);
  559. if not try_to_consume(_GT) then
  560. try_to_consume(_RSHARPBRACKET);
  561. for i:=0 to poslist.count-1 do
  562. dispose(pfileposinfo(poslist[i]));
  563. poslist.free;
  564. genericdeflist.Free;
  565. tt:=generrordef;
  566. exit;
  567. end;
  568. { we've found the correct def }
  569. genericdef:=tstoreddef(ttypesym(srsym).typedef);
  570. if not check_generic_constraints(genericdef,genericdeflist,poslist) then
  571. begin
  572. { the parameters didn't fit the constraints, so don't continue with the
  573. specialization }
  574. genericdeflist.free;
  575. for i:=0 to poslist.count-1 do
  576. dispose(pfileposinfo(poslist[i]));
  577. poslist.free;
  578. tt:=generrordef;
  579. if not try_to_consume(_GT) then
  580. try_to_consume(_RSHARPBRACKET);
  581. exit;
  582. end;
  583. { build the new type's name }
  584. finalspecializename:=generate_generic_name(genname,specializename);
  585. ufinalspecializename:=upper(finalspecializename);
  586. prettyname:=genericdef.typesym.prettyname+'<'+prettyname+'>';
  587. { select the symtable containing the params }
  588. case genericdef.typ of
  589. procdef:
  590. st:=genericdef.GetSymtable(gs_para);
  591. objectdef,
  592. recorddef:
  593. st:=genericdef.GetSymtable(gs_record);
  594. arraydef:
  595. st:=tarraydef(genericdef).symtable;
  596. procvardef:
  597. st:=genericdef.GetSymtable(gs_para);
  598. else
  599. internalerror(200511182);
  600. end;
  601. generictypelist:=tfphashobjectlist.create(false);
  602. { build the list containing the types for the generic params }
  603. if not assigned(genericdef.genericparas) then
  604. internalerror(2013092601);
  605. if genericdeflist.count<>genericdef.genericparas.count then
  606. internalerror(2013092603);
  607. for i:=0 to genericdef.genericparas.Count-1 do
  608. begin
  609. srsym:=tsym(genericdef.genericparas[i]);
  610. if not (sp_generic_para in srsym.symoptions) then
  611. internalerror(2013092602);
  612. generictypelist.add(srsym.realname,tdef(genericdeflist[i]).typesym);
  613. end;
  614. { Special case if we are referencing the current defined object }
  615. if assigned(current_structdef) and
  616. (current_structdef.objname^=ufinalspecializename) then
  617. tt:=current_structdef;
  618. { decide in which symtable to put the specialization }
  619. if parse_generic then
  620. begin
  621. if not assigned(current_genericdef) then
  622. internalerror(2014050901);
  623. if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  624. { if we are parsing the definition of a method we specialize into
  625. the local symtable of it }
  626. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  627. else
  628. { we specialize the partial specialization into the symtable of the currently parsed
  629. generic }
  630. case current_genericdef.typ of
  631. procvardef,
  632. procdef:
  633. specializest:=current_genericdef.getsymtable(gs_local);
  634. objectdef,
  635. recorddef:
  636. specializest:=current_genericdef.getsymtable(gs_record);
  637. arraydef:
  638. specializest:=tarraydef(current_genericdef).symtable;
  639. else
  640. internalerror(2014050902);
  641. end;
  642. end
  643. else
  644. if current_module.is_unit and current_module.in_interface then
  645. specializest:=current_module.globalsymtable
  646. else
  647. specializest:=current_module.localsymtable;
  648. if not assigned(specializest) then
  649. internalerror(2014050910);
  650. { Can we reuse an already specialized type? }
  651. { for this first check whether we are currently specializing a nested
  652. type of the current (main) specialization (this is necessary, because
  653. during that time the symbol of the main specialization will still
  654. contain a reference to an errordef) }
  655. if not assigned(tt) and assigned(current_specializedef) then
  656. begin
  657. def:=current_specializedef;
  658. repeat
  659. if def.typ in [objectdef,recorddef] then
  660. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  661. tt:=def;
  662. break;
  663. end;
  664. def:=tstoreddef(def.owner.defowner);
  665. until not assigned(def) or not (df_specialization in def.defoptions);
  666. end;
  667. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  668. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  669. parsing anyway), so for now we treat those still as generic defs without doing a partial
  670. specialization }
  671. if not assigned(tt) then
  672. begin
  673. def:=current_genericdef;
  674. while assigned(def) and (def.typ in [recorddef,objectdef]) do
  675. begin
  676. if def=genericdef then
  677. begin
  678. tt:=def;
  679. break;
  680. end;
  681. def:=tstoreddef(def.owner.defowner);
  682. end;
  683. end;
  684. { now check whether there is a specialization somewhere else }
  685. if not assigned(tt) then
  686. begin
  687. hashedid.id:=ufinalspecializename;
  688. srsym:=tsym(specializest.findwithhash(hashedid));
  689. if assigned(srsym) then
  690. begin
  691. if srsym.typ<>typesym then
  692. internalerror(200710171);
  693. tt:=ttypesym(srsym).typedef;
  694. end
  695. else
  696. { the generic could have been specialized in the globalsymtable
  697. already, so search there as well }
  698. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  699. begin
  700. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  701. if assigned(srsym) then
  702. begin
  703. if srsym.typ<>typesym then
  704. internalerror(2011121101);
  705. tt:=ttypesym(srsym).typedef;
  706. end;
  707. end;
  708. end;
  709. if not assigned(tt) then
  710. begin
  711. specialization_init(genericdef,state);
  712. { push a temporary global symtable so that the specialization is
  713. added to the correct symtable; this symtable does not contain
  714. any other symbols, so that the type resolution can not be
  715. influenced by symbols in the current unit }
  716. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  717. symtablestack.push(tempst);
  718. { Reparse the original type definition }
  719. if not err then
  720. begin
  721. old_current_specializedef:=nil;
  722. old_current_genericdef:=nil;
  723. old_current_structdef:=nil;
  724. if parse_class_parent then
  725. begin
  726. old_current_structdef:=current_structdef;
  727. old_current_genericdef:=current_genericdef;
  728. old_current_specializedef:=current_specializedef;
  729. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  730. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  731. else
  732. current_structdef:=nil;
  733. current_genericdef:=nil;
  734. current_specializedef:=nil;
  735. end;
  736. maybe_add_waiting_unit(genericdef);
  737. { First a new typesym so we can reuse this specialization and
  738. references to this specialization can be handled }
  739. srsym:=ctypesym.create(finalspecializename,generrordef);
  740. specializest.insert(srsym);
  741. { specializations are declarations as such it is the wisest to
  742. declare set the blocktype to "type"; otherwise we'll
  743. experience unexpected side effects like the addition of
  744. classrefdefs if we have a generic that's derived from another
  745. generic }
  746. old_block_type:=block_type;
  747. block_type:=bt_type;
  748. if not assigned(genericdef.generictokenbuf) then
  749. internalerror(200511171);
  750. hmodule:=find_module_from_symtable(genericdef.owner);
  751. if hmodule=nil then
  752. internalerror(2012051202);
  753. oldcurrent_filepos:=current_filepos;
  754. { use the index the module got from the current compilation process }
  755. current_filepos.moduleindex:=hmodule.unit_index;
  756. current_tokenpos:=current_filepos;
  757. if parse_generic then
  758. begin
  759. recordbuf:=current_scanner.recordtokenbuf;
  760. current_scanner.recordtokenbuf:=nil;
  761. end
  762. else
  763. recordbuf:=nil;
  764. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  765. read_named_type(tt,srsym,genericdef,generictypelist,false,false);
  766. current_filepos:=oldcurrent_filepos;
  767. ttypesym(srsym).typedef:=tt;
  768. tt.typesym:=srsym;
  769. if _prettyname<>'' then
  770. ttypesym(tt.typesym).fprettyname:=_prettyname
  771. else
  772. ttypesym(tt.typesym).fprettyname:=prettyname;
  773. { Note regarding hint directives:
  774. There is no need to remove the flags for them from the
  775. specialized generic symbol, because hint directives that
  776. follow the specialization are handled by the code in
  777. pdecl.types_dec and added to the type symbol.
  778. E.g.: TFoo = TBar<Blubb> deprecated;
  779. Here the symbol TBar$1$Blubb will contain the
  780. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  781. case tt.typ of
  782. { Build VMT indexes for classes and read hint directives }
  783. objectdef:
  784. begin
  785. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  786. consume(_SEMICOLON);
  787. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  788. vmtbuilder.generate_vmt;
  789. vmtbuilder.free;
  790. end;
  791. { handle params, calling convention, etc }
  792. procvardef:
  793. begin
  794. if not check_proc_directive(true) then
  795. begin
  796. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  797. consume(_SEMICOLON);
  798. end;
  799. parse_var_proc_directives(ttypesym(srsym));
  800. handle_calling_convention(tprocvardef(tt));
  801. if try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg) then
  802. consume(_SEMICOLON);
  803. end;
  804. else
  805. { parse hint directives for records and arrays }
  806. begin
  807. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  808. consume(_SEMICOLON);
  809. end;
  810. end;
  811. { Consume the semicolon if it is also recorded }
  812. try_to_consume(_SEMICOLON);
  813. if assigned(recordbuf) then
  814. begin
  815. if assigned(current_scanner.recordtokenbuf) then
  816. internalerror(2014050909);
  817. current_scanner.recordtokenbuf:=recordbuf;
  818. end;
  819. block_type:=old_block_type;
  820. if parse_class_parent then
  821. begin
  822. current_structdef:=old_current_structdef;
  823. current_genericdef:=old_current_genericdef;
  824. current_specializedef:=old_current_specializedef;
  825. end;
  826. end;
  827. { extract all created symbols and defs from the temporary symtable
  828. and add them to the specializest }
  829. for i:=tempst.SymList.Count-1 downto 0 do
  830. begin
  831. item:=tempst.SymList.Items[i];
  832. { using changeowner the symbol is automatically added to the
  833. new symtable }
  834. tsym(item).ChangeOwner(specializest);
  835. end;
  836. for i:=tempst.DefList.Count-1 downto 0 do
  837. begin
  838. item:=tempst.DefList.Items[i];
  839. { using changeowner the def is automatically added to the new
  840. symtable }
  841. tdef(item).ChangeOwner(specializest);
  842. { for partial specializations we implicitely declare any methods as having their
  843. implementations although we'll not specialize them in reality }
  844. if parse_generic then
  845. unset_forwarddef(tdef(item));
  846. end;
  847. { if a generic was declared during the specialization we need to
  848. flag the specialize symtable accordingly }
  849. if sto_has_generic in tempst.tableoptions then
  850. specializest.includeoption(sto_has_generic);
  851. tempst.free;
  852. specialization_done(state);
  853. end;
  854. if not (token in [_GT, _RSHARPBRACKET]) then
  855. begin
  856. consume(_RSHARPBRACKET);
  857. exit;
  858. end
  859. else
  860. consume(token);
  861. genericdeflist.free;
  862. generictypelist.free;
  863. if assigned(genericdef) then
  864. begin
  865. { check the hints of the found generic symbol }
  866. srsym:=genericdef.typesym;
  867. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  868. end;
  869. end;
  870. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  871. var
  872. generictype : ttypesym;
  873. i,firstidx : longint;
  874. srsymtable : tsymtable;
  875. basedef,def : tdef;
  876. defname : tidstring;
  877. allowconstructor,
  878. doconsume : boolean;
  879. constraintdata : tgenericconstraintdata;
  880. old_block_type : tblock_type;
  881. begin
  882. result:=tfphashobjectlist.create(false);
  883. firstidx:=0;
  884. old_block_type:=block_type;
  885. block_type:=bt_type;
  886. repeat
  887. if token=_ID then
  888. begin
  889. generictype:=ctypesym.create(orgpattern,cundefinedtype);
  890. include(generictype.symoptions,sp_generic_para);
  891. result.add(orgpattern,generictype);
  892. end;
  893. consume(_ID);
  894. if try_to_consume(_COLON) then
  895. begin
  896. if not allowconstraints then
  897. { TODO }
  898. Message(parser_e_illegal_expression{ parser_e_generic_constraints_not_allowed_here});
  899. { construct a name which can be used for a type specification }
  900. constraintdata:=tgenericconstraintdata.create;
  901. defname:='';
  902. str(current_module.deflist.count,defname);
  903. defname:='$gendef'+defname;
  904. allowconstructor:=m_delphi in current_settings.modeswitches;
  905. basedef:=generrordef;
  906. repeat
  907. doconsume:=true;
  908. case token of
  909. _CONSTRUCTOR:
  910. begin
  911. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  912. Message(parser_e_illegal_expression);
  913. include(constraintdata.flags,gcf_constructor);
  914. allowconstructor:=false;
  915. end;
  916. _CLASS:
  917. begin
  918. if gcf_class in constraintdata.flags then
  919. Message(parser_e_illegal_expression);
  920. if basedef=generrordef then
  921. include(constraintdata.flags,gcf_class)
  922. else
  923. Message(parser_e_illegal_expression);
  924. end;
  925. _RECORD:
  926. begin
  927. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  928. or (constraintdata.interfaces.count>0) then
  929. Message(parser_e_illegal_expression)
  930. else
  931. begin
  932. srsymtable:=trecordsymtable.create(defname,0);
  933. basedef:=crecorddef.create(defname,srsymtable);
  934. include(constraintdata.flags,gcf_record);
  935. allowconstructor:=false;
  936. end;
  937. end;
  938. else
  939. begin
  940. { after single_type "token" is the trailing ",", ";" or
  941. ">"! }
  942. doconsume:=false;
  943. { def is already set to a class or record }
  944. if gcf_record in constraintdata.flags then
  945. Message(parser_e_illegal_expression);
  946. single_type(def, [stoAllowSpecialization]);
  947. { only types that are inheritable are allowed }
  948. if (def.typ<>objectdef) or
  949. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  950. Message1(type_e_class_or_interface_type_expected,def.typename)
  951. else
  952. case tobjectdef(def).objecttype of
  953. odt_class,
  954. odt_javaclass:
  955. begin
  956. if gcf_class in constraintdata.flags then
  957. { "class" + concrete class is not allowed }
  958. Message(parser_e_illegal_expression)
  959. else
  960. { do we already have a concrete class? }
  961. if basedef<>generrordef then
  962. Message(parser_e_illegal_expression)
  963. else
  964. basedef:=def;
  965. end;
  966. odt_interfacecom,
  967. odt_interfacecorba,
  968. odt_interfacejava,
  969. odt_dispinterface:
  970. constraintdata.interfaces.add(def);
  971. end;
  972. end;
  973. end;
  974. if doconsume then
  975. consume(token);
  976. until not try_to_consume(_COMMA);
  977. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  978. (constraintdata.interfaces.count>1) or
  979. (
  980. (basedef.typ=objectdef) and
  981. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  982. ) then
  983. begin
  984. if basedef.typ=errordef then
  985. { don't pass an errordef as a parent to a tobjectdef }
  986. basedef:=class_tobject
  987. else
  988. if (basedef.typ<>objectdef) or
  989. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  990. internalerror(2012101101);
  991. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef));
  992. for i:=0 to constraintdata.interfaces.count-1 do
  993. tobjectdef(basedef).implementedinterfaces.add(
  994. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  995. end
  996. else
  997. if constraintdata.interfaces.count=1 then
  998. begin
  999. if basedef.typ<>errordef then
  1000. internalerror(2013021601);
  1001. def:=tdef(constraintdata.interfaces[0]);
  1002. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def));
  1003. constraintdata.interfaces.delete(0);
  1004. end;
  1005. if basedef.typ<>errordef then
  1006. with tstoreddef(basedef) do
  1007. begin
  1008. genconstraintdata:=tgenericconstraintdata.create;
  1009. genconstraintdata.flags:=constraintdata.flags;
  1010. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  1011. include(defoptions,df_genconstraint);
  1012. end;
  1013. for i:=firstidx to result.count-1 do
  1014. ttypesym(result[i]).typedef:=basedef;
  1015. { we need a typesym in case we do a Delphi-mode inline
  1016. specialization with this parameter; so just use the first sym }
  1017. if not assigned(basedef.typesym) then
  1018. basedef.typesym:=ttypesym(result[firstidx]);
  1019. firstidx:=result.count;
  1020. constraintdata.free;
  1021. end;
  1022. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  1023. block_type:=old_block_type;
  1024. end;
  1025. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1026. var
  1027. i : longint;
  1028. generictype,sym : ttypesym;
  1029. st : tsymtable;
  1030. begin
  1031. def.genericdef:=genericdef;
  1032. if not assigned(genericlist) then
  1033. exit;
  1034. if assigned(genericdef) then
  1035. include(def.defoptions,df_specialization)
  1036. else
  1037. if genericlist.count>0 then
  1038. include(def.defoptions,df_generic);
  1039. case def.typ of
  1040. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  1041. arraydef: st:=tarraydef(def).symtable;
  1042. procvardef,procdef: st:=tabstractprocdef(def).parast;
  1043. else
  1044. internalerror(201101020);
  1045. end;
  1046. if (genericlist.count>0) and not assigned(def.genericparas) then
  1047. def.genericparas:=tfphashobjectlist.create(false);
  1048. for i:=0 to genericlist.count-1 do
  1049. begin
  1050. generictype:=ttypesym(genericlist[i]);
  1051. if assigned(generictype.owner) then
  1052. begin
  1053. sym:=ctypesym.create(genericlist.nameofindex(i),generictype.typedef);
  1054. st.insert(sym);
  1055. include(sym.symoptions,sp_generic_para);
  1056. end
  1057. else
  1058. begin
  1059. st.insert(generictype);
  1060. include(generictype.symoptions,sp_generic_para);
  1061. end;
  1062. def.genericparas.add(genericlist.nameofindex(i),generictype);
  1063. end;
  1064. end;
  1065. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  1066. var
  1067. gensym : ttypesym;
  1068. begin
  1069. { for generics in non-Delphi modes we insert a private type symbol
  1070. that has the same base name as the currently parsed generic and
  1071. that references this defs }
  1072. if not (m_delphi in current_settings.modeswitches) and
  1073. (
  1074. (
  1075. parse_generic and
  1076. assigned(genericlist) and
  1077. (genericlist.count>0)
  1078. ) or
  1079. (
  1080. assigned(current_specializedef) and
  1081. assigned(current_structdef.genericdef) and
  1082. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  1083. (pos('$',name)>0)
  1084. )
  1085. ) then
  1086. begin
  1087. { we need to pass nil as def here, because the constructor wants
  1088. to set the typesym of the def which is not what we want }
  1089. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil);
  1090. gensym.typedef:=current_structdef;
  1091. include(gensym.symoptions,sp_internal);
  1092. { the symbol should be only visible to the generic class
  1093. itself }
  1094. gensym.visibility:=vis_strictprivate;
  1095. symtablestack.top.insert(gensym);
  1096. end;
  1097. end;
  1098. function generate_generic_name(const name:tidstring;specializename:ansistring):tidstring;
  1099. var
  1100. crc : cardinal;
  1101. begin
  1102. if specializename='' then
  1103. internalerror(2012061901);
  1104. { build the new type's name }
  1105. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  1106. result:=name+'$crc'+hexstr(crc,8);
  1107. end;
  1108. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  1109. var
  1110. i,code : longint;
  1111. countstr : string;
  1112. begin
  1113. for i:=length(name) downto 1 do
  1114. if name[i]='$' then
  1115. begin
  1116. nongeneric:=copy(name,1,i-1);
  1117. countstr:=copy(name,i+1,length(name)-i);
  1118. val(countstr,count,code);
  1119. if code<>0 then
  1120. break;
  1121. exit;
  1122. end;
  1123. nongeneric:=name;
  1124. count:=0;
  1125. end;
  1126. function resolve_generic_dummysym(const name:tidstring):tsym;
  1127. var
  1128. list : tfpobjectlist;
  1129. begin
  1130. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  1131. if assigned(list) and (list.count>0) then
  1132. result:=tgenericdummyentry(list.last).resolvedsym
  1133. else
  1134. result:=nil;
  1135. end;
  1136. function could_be_generic(const name:tidstring):boolean;
  1137. begin
  1138. result:=(name<>'') and
  1139. (current_module.genericdummysyms.findindexof(name)>=0);
  1140. end;
  1141. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  1142. var
  1143. pu : tused_unit;
  1144. hmodule : tmodule;
  1145. unitsyms : TFPHashObjectList;
  1146. sym : tsym;
  1147. i : Integer;
  1148. begin
  1149. if not assigned(genericdef) then
  1150. internalerror(200705151);
  1151. { Setup symtablestack at definition time
  1152. to get types right, however this is not perfect, we should probably record
  1153. the resolved symbols }
  1154. state.oldsymtablestack:=symtablestack;
  1155. state.oldextendeddefs:=current_module.extendeddefs;
  1156. state.oldgenericdummysyms:=current_module.genericdummysyms;
  1157. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1158. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  1159. symtablestack:=tdefawaresymtablestack.create;
  1160. hmodule:=find_module_from_symtable(genericdef.owner);
  1161. if hmodule=nil then
  1162. internalerror(200705152);
  1163. { collect all unit syms in the generic's unit as we need to establish
  1164. their unitsym.module link again so that unit identifiers can be used }
  1165. unitsyms:=tfphashobjectlist.create(false);
  1166. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1167. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1168. begin
  1169. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1170. if sym.typ=unitsym then
  1171. unitsyms.add(upper(sym.realname),sym);
  1172. end;
  1173. { add all units if we are specializing inside the current unit (as the
  1174. generic could have been declared in the implementation part), but load
  1175. only interface units, if we are in a different unit as then the generic
  1176. needs to be in the interface section }
  1177. pu:=tused_unit(hmodule.used_units.first);
  1178. while assigned(pu) do
  1179. begin
  1180. if not assigned(pu.u.globalsymtable) then
  1181. { in certain circular, but valid unit constellations it can happen
  1182. that we specialize a generic in a different unit that was used
  1183. in the implementation section of the generic's unit and were the
  1184. interface is still being parsed and thus the localsymtable is in
  1185. reality the global symtable }
  1186. if pu.u.in_interface then
  1187. symtablestack.push(pu.u.localsymtable)
  1188. else
  1189. internalerror(200705153)
  1190. else
  1191. symtablestack.push(pu.u.globalsymtable);
  1192. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1193. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1194. tunitsym(sym).module:=pu.u;
  1195. pu:=tused_unit(pu.next);
  1196. end;
  1197. unitsyms.free;
  1198. if assigned(hmodule.globalsymtable) then
  1199. symtablestack.push(hmodule.globalsymtable);
  1200. { push the localsymtable if needed }
  1201. if (hmodule<>current_module) or not current_module.in_interface then
  1202. symtablestack.push(hmodule.localsymtable);
  1203. end;
  1204. procedure specialization_done(var state: tspecializationstate);
  1205. begin
  1206. { Restore symtablestack }
  1207. current_module.extendeddefs.free;
  1208. current_module.extendeddefs:=state.oldextendeddefs;
  1209. current_module.genericdummysyms.free;
  1210. current_module.genericdummysyms:=state.oldgenericdummysyms;
  1211. symtablestack.free;
  1212. symtablestack:=state.oldsymtablestack;
  1213. { clear the state record to be on the safe side }
  1214. fillchar(state, sizeof(state), 0);
  1215. end;
  1216. end.