pgenutil.pas 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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. { parser }
  27. pgentype,
  28. { symtable }
  29. symtype,symdef,symbase;
  30. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);inline;
  31. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);inline;
  32. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef):tdef;inline;
  33. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;symname:string):tdef;inline;
  34. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;parsedtype:tdef;symname:string;parsedpos:tfileposinfo):tdef;
  35. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;_prettyname:ansistring):tdef;
  36. function check_generic_constraints(genericdef:tstoreddef;paradeflist:tfpobjectlist;poslist:tfplist):boolean;
  37. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  38. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  39. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  40. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  41. function generate_generic_name(const name:tidstring;specializename:ansistring;owner_hierarchy:string):tidstring;
  42. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  43. procedure add_generic_dummysym(sym:tsym);
  44. function resolve_generic_dummysym(const name:tidstring):tsym;
  45. function could_be_generic(const name:tidstring):boolean;inline;
  46. procedure generate_specialization_procs;
  47. procedure maybe_add_pending_specialization(def:tdef);
  48. procedure specialization_init(genericdef:tdef;var state:tspecializationstate);
  49. procedure specialization_done(var state:tspecializationstate);
  50. implementation
  51. uses
  52. { common }
  53. cutils,fpccrc,
  54. { global }
  55. globals,tokens,verbose,finput,
  56. { symtable }
  57. symconst,symsym,symtable,defcmp,procinfo,
  58. { modules }
  59. fmodule,
  60. node,nobj,
  61. { parser }
  62. scanner,
  63. pbase,pexpr,pdecsub,ptype,psub,pparautl;
  64. procedure maybe_add_waiting_unit(tt:tdef);
  65. var
  66. hmodule : tmodule;
  67. begin
  68. if not assigned(tt) or
  69. not (df_generic in tt.defoptions) then
  70. exit;
  71. hmodule:=find_module_from_symtable(tt.owner);
  72. if not assigned(hmodule) then
  73. internalerror(2012092401);
  74. if hmodule=current_module then
  75. exit;
  76. if hmodule.state<>ms_compiled then
  77. begin
  78. {$ifdef DEBUG_UNITWAITING}
  79. Writeln('Unit ', current_module.modulename^,
  80. ' waiting for ', hmodule.modulename^);
  81. {$endif DEBUG_UNITWAITING}
  82. if current_module.waitingforunit.indexof(hmodule)<0 then
  83. current_module.waitingforunit.add(hmodule);
  84. if hmodule.waitingunits.indexof(current_module)<0 then
  85. hmodule.waitingunits.add(current_module);
  86. end;
  87. end;
  88. function check_generic_constraints(genericdef:tstoreddef;paradeflist:tfpobjectlist;poslist:tfplist):boolean;
  89. var
  90. i,j,
  91. intfcount : longint;
  92. formaldef,
  93. paradef : tstoreddef;
  94. objdef,
  95. paraobjdef,
  96. formalobjdef : tobjectdef;
  97. intffound : boolean;
  98. filepos : tfileposinfo;
  99. begin
  100. { check whether the given specialization parameters fit to the eventual
  101. constraints of the generic }
  102. if not assigned(genericdef.genericparas) or (genericdef.genericparas.count=0) then
  103. internalerror(2012101001);
  104. if genericdef.genericparas.count<>paradeflist.count then
  105. internalerror(2012101002);
  106. if paradeflist.count<>poslist.count then
  107. internalerror(2012120801);
  108. result:=true;
  109. for i:=0 to genericdef.genericparas.count-1 do
  110. begin
  111. filepos:=pfileposinfo(poslist[i])^;
  112. formaldef:=tstoreddef(ttypesym(genericdef.genericparas[i]).typedef);
  113. if formaldef.typ=undefineddef then
  114. { the parameter is of unspecified type, so no need to check }
  115. continue;
  116. if not (df_genconstraint in formaldef.defoptions) or
  117. not assigned(formaldef.genconstraintdata) then
  118. internalerror(2013021602);
  119. paradef:=tstoreddef(paradeflist[i]);
  120. { undefineddef is compatible with anything }
  121. if formaldef.typ=undefineddef then
  122. continue;
  123. if paradef.typ<>formaldef.typ then
  124. begin
  125. case formaldef.typ of
  126. recorddef:
  127. { delphi has own fantasy about record constraint
  128. (almost non-nullable/non-nilable value type) }
  129. if m_delphi in current_settings.modeswitches then
  130. case paradef.typ of
  131. floatdef,enumdef,orddef:
  132. continue;
  133. objectdef:
  134. if tobjectdef(paradef).objecttype=odt_object then
  135. continue
  136. else
  137. MessagePos(filepos,type_e_record_type_expected);
  138. else
  139. MessagePos(filepos,type_e_record_type_expected);
  140. end
  141. else
  142. MessagePos(filepos,type_e_record_type_expected);
  143. objectdef:
  144. case tobjectdef(formaldef).objecttype of
  145. odt_class,
  146. odt_javaclass:
  147. MessagePos1(filepos,type_e_class_type_expected,paradef.typename);
  148. odt_interfacecom,
  149. odt_interfacecorba,
  150. odt_dispinterface,
  151. odt_interfacejava:
  152. MessagePos1(filepos,type_e_interface_type_expected,paradef.typename);
  153. else
  154. internalerror(2012101003);
  155. end;
  156. errordef:
  157. { ignore }
  158. ;
  159. else
  160. internalerror(2012101004);
  161. end;
  162. result:=false;
  163. end
  164. else
  165. begin
  166. { the paradef types are the same, so do special checks for the
  167. cases in which they are needed }
  168. if formaldef.typ=objectdef then
  169. begin
  170. paraobjdef:=tobjectdef(paradef);
  171. formalobjdef:=tobjectdef(formaldef);
  172. if not (formalobjdef.objecttype in [odt_class,odt_javaclass,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface]) then
  173. internalerror(2012101102);
  174. if formalobjdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface] then
  175. begin
  176. { this is either a concerete interface or class type (the
  177. latter without specific implemented interfaces) }
  178. case paraobjdef.objecttype of
  179. odt_interfacecom,
  180. odt_interfacecorba,
  181. odt_interfacejava,
  182. odt_dispinterface:
  183. begin
  184. if (oo_is_forward in paraobjdef.objectoptions) and
  185. (paraobjdef.objecttype=formalobjdef.objecttype) and
  186. (df_genconstraint in formalobjdef.defoptions) and
  187. (
  188. (formalobjdef.objecttype=odt_interfacecom) and
  189. (formalobjdef.childof=interface_iunknown)
  190. )
  191. or
  192. (
  193. (formalobjdef.objecttype=odt_interfacecorba) and
  194. (formalobjdef.childof=nil)
  195. ) then
  196. continue;
  197. if not def_is_related(paraobjdef,formalobjdef.childof) then
  198. begin
  199. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  200. result:=false;
  201. end;
  202. end;
  203. odt_class,
  204. odt_javaclass:
  205. begin
  206. objdef:=paraobjdef;
  207. intffound:=false;
  208. while assigned(objdef) do
  209. begin
  210. for j:=0 to objdef.implementedinterfaces.count-1 do
  211. if timplementedinterface(objdef.implementedinterfaces[j]).intfdef=formalobjdef.childof then
  212. begin
  213. intffound:=true;
  214. break;
  215. end;
  216. if intffound then
  217. break;
  218. objdef:=objdef.childof;
  219. end;
  220. result:=intffound;
  221. if not result then
  222. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,formalobjdef.childof.typename);
  223. end;
  224. else
  225. begin
  226. MessagePos1(filepos,type_e_class_or_interface_type_expected,paraobjdef.typename);
  227. result:=false;
  228. end;
  229. end;
  230. end
  231. else
  232. begin
  233. { this is either a "class" or a concrete instance with
  234. or without implemented interfaces }
  235. if not (paraobjdef.objecttype in [odt_class,odt_javaclass]) then
  236. begin
  237. MessagePos1(filepos,type_e_class_type_expected,paraobjdef.typename);
  238. result:=false;
  239. continue;
  240. end;
  241. { for forward declared classes we allow pure TObject/class declarations }
  242. if (oo_is_forward in paraobjdef.objectoptions) and
  243. (df_genconstraint in formaldef.defoptions) then
  244. begin
  245. if (formalobjdef.childof=class_tobject) and
  246. not formalobjdef.implements_any_interfaces then
  247. continue;
  248. end;
  249. if assigned(formalobjdef.childof) and
  250. not def_is_related(paradef,formalobjdef.childof) then
  251. begin
  252. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  253. result:=false;
  254. end;
  255. intfcount:=0;
  256. for j:=0 to formalobjdef.implementedinterfaces.count-1 do
  257. begin
  258. objdef:=paraobjdef;
  259. while assigned(objdef) do
  260. begin
  261. intffound:=assigned(
  262. find_implemented_interface(objdef,
  263. timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef
  264. )
  265. );
  266. if intffound then
  267. break;
  268. objdef:=objdef.childof;
  269. end;
  270. if intffound then
  271. inc(intfcount)
  272. else
  273. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef.typename);
  274. end;
  275. if intfcount<>formalobjdef.implementedinterfaces.count then
  276. result:=false;
  277. end;
  278. end;
  279. end;
  280. end;
  281. end;
  282. function parse_generic_specialization_types_internal(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring;parsedtype:tdef;parsedpos:tfileposinfo):boolean;
  283. var
  284. old_block_type : tblock_type;
  285. first : boolean;
  286. typeparam : tnode;
  287. parampos : pfileposinfo;
  288. tmpparampos : tfileposinfo;
  289. namepart : string;
  290. prettynamepart : ansistring;
  291. module : tmodule;
  292. begin
  293. result:=true;
  294. if genericdeflist=nil then
  295. internalerror(2012061401);
  296. { set the block type to type, so that the parsed type are returned as
  297. ttypenode (e.g. classes are in non type-compatible blocks returned as
  298. tloadvmtaddrnode) }
  299. old_block_type:=block_type;
  300. { if parsedtype is set, then the first type identifer was already parsed
  301. (happens in inline specializations) and thus we only need to parse
  302. the remaining types and do as if the first one was already given }
  303. first:=not assigned(parsedtype);
  304. if assigned(parsedtype) then
  305. begin
  306. genericdeflist.Add(parsedtype);
  307. module:=find_module_from_symtable(parsedtype.owner);
  308. if not assigned(module) then
  309. internalerror(2016112801);
  310. namepart:='_$'+hexstr(module.moduleid,8)+'$$'+parsedtype.unique_id_str;
  311. specializename:='$'+namepart;
  312. prettyname:=parsedtype.fullownerhierarchyname(true)+parsedtype.typesym.prettyname;
  313. if assigned(poslist) then
  314. begin
  315. New(parampos);
  316. parampos^:=parsedpos;
  317. poslist.add(parampos);
  318. end;
  319. end
  320. else
  321. begin
  322. specializename:='$';
  323. prettyname:='';
  324. end;
  325. while not (token in [_GT,_RSHARPBRACKET]) do
  326. begin
  327. { "first" is set to false at the end of the loop! }
  328. if not first then
  329. consume(_COMMA);
  330. block_type:=bt_type;
  331. tmpparampos:=current_filepos;
  332. typeparam:=factor(false,[ef_type_only]);
  333. if typeparam.nodetype=typen then
  334. begin
  335. if tstoreddef(typeparam.resultdef).is_generic and
  336. (
  337. not parse_generic or
  338. not defs_belong_to_same_generic(typeparam.resultdef,current_genericdef)
  339. ) then
  340. Message(parser_e_no_generics_as_params);
  341. if assigned(poslist) then
  342. begin
  343. New(parampos);
  344. parampos^:=tmpparampos;
  345. poslist.add(parampos);
  346. end;
  347. if typeparam.resultdef.typ<>errordef then
  348. begin
  349. if not assigned(typeparam.resultdef.typesym) then
  350. message(type_e_generics_cannot_reference_itself)
  351. else if (typeparam.resultdef.typ<>errordef) then
  352. begin
  353. genericdeflist.Add(typeparam.resultdef);
  354. module:=find_module_from_symtable(typeparam.resultdef.owner);
  355. if not assigned(module) then
  356. internalerror(2016112802);
  357. namepart:='_$'+hexstr(module.moduleid,8)+'$$'+typeparam.resultdef.unique_id_str;
  358. { we use the full name of the type to uniquely identify it }
  359. if (symtablestack.top.symtabletype=parasymtable) and
  360. (symtablestack.top.defowner.typ=procdef) and
  361. (typeparam.resultdef.owner=symtablestack.top) then
  362. begin
  363. { special handling for specializations inside generic function declarations }
  364. prettynamepart:=tdef(symtablestack.top.defowner).fullownerhierarchyname(true)+tprocdef(symtablestack.top.defowner).procsym.prettyname;
  365. end
  366. else
  367. begin
  368. prettynamepart:=typeparam.resultdef.fullownerhierarchyname(true);
  369. end;
  370. specializename:=specializename+namepart;
  371. if not first then
  372. prettyname:=prettyname+',';
  373. prettyname:=prettyname+prettynamepart+typeparam.resultdef.typesym.prettyname;
  374. end;
  375. end
  376. else
  377. begin
  378. result:=false;
  379. end;
  380. end
  381. else
  382. begin
  383. Message(type_e_type_id_expected);
  384. result:=false;
  385. end;
  386. typeparam.free;
  387. first:=false;
  388. end;
  389. block_type:=old_block_type;
  390. end;
  391. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  392. var
  393. dummypos : tfileposinfo;
  394. begin
  395. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  396. result:=parse_generic_specialization_types_internal(genericdeflist,poslist,prettyname,specializename,nil,dummypos);
  397. end;
  398. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);
  399. var
  400. dummypos : tfileposinfo;
  401. begin
  402. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  403. generate_specialization(tt,parse_class_parent,_prettyname,nil,'',dummypos);
  404. end;
  405. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef):tdef;
  406. var
  407. dummypos : tfileposinfo;
  408. {$push}
  409. {$warn 5036 off}
  410. begin
  411. result:=generate_specialization_phase1(context,genericdef,nil,'',dummypos);
  412. end;
  413. {$pop}
  414. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;symname:string):tdef;
  415. var
  416. dummypos : tfileposinfo;
  417. {$push}
  418. {$warn 5036 off}
  419. begin
  420. result:=generate_specialization_phase1(context,genericdef,nil,symname,dummypos);
  421. end;
  422. {$pop}
  423. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;parsedtype:tdef;symname:string;parsedpos:tfileposinfo):tdef;
  424. var
  425. pt2 : tnode;
  426. errorrecovery,
  427. found,
  428. first,
  429. err : boolean;
  430. i,
  431. gencount : longint;
  432. def : tstoreddef;
  433. countstr,genname,ugenname : string;
  434. srsym : tsym;
  435. st : tsymtable;
  436. begin
  437. context:=nil;
  438. result:=nil;
  439. { either symname must be given or genericdef needs to be valid }
  440. errorrecovery:=false;
  441. if (symname='') and
  442. (not assigned(genericdef) or
  443. (
  444. (genericdef.typ<>procdef) and
  445. (
  446. not assigned(genericdef.typesym) or
  447. (genericdef.typesym.typ<>typesym)
  448. )
  449. ) or
  450. (
  451. (genericdef.typ=procdef) and
  452. (
  453. not assigned(tprocdef(genericdef).procsym) or
  454. (tprocdef(genericdef).procsym.typ<>procsym)
  455. )
  456. )
  457. ) then
  458. begin
  459. errorrecovery:=true;
  460. result:=generrordef;
  461. end;
  462. { Only parse the parameters for recovery or
  463. for recording in genericbuf }
  464. if errorrecovery then
  465. begin
  466. first:=assigned(parsedtype);
  467. if not first and not try_to_consume(_LT) then
  468. consume(_LSHARPBRACKET);
  469. gencount:=0;
  470. { handle "<>" }
  471. if not first and ((token=_RSHARPBRACKET) or (token=_GT)) then
  472. Message(type_e_type_id_expected)
  473. else
  474. repeat
  475. if not first then
  476. begin
  477. pt2:=factor(false,[ef_type_only]);
  478. pt2.free;
  479. end;
  480. first:=false;
  481. inc(gencount);
  482. until not try_to_consume(_COMMA);
  483. if not try_to_consume(_GT) then
  484. consume(_RSHARPBRACKET);
  485. { we need to return a def that can later pass some checks like
  486. whether it's an interface or not }
  487. if not errorrecovery and
  488. (not assigned(result) or (result.typ=undefineddef)) then
  489. begin
  490. if (symname='') and tstoreddef(genericdef).is_generic then
  491. { this happens in non-Delphi modes }
  492. result:=genericdef
  493. else
  494. begin
  495. { find the corresponding generic symbol so that any checks
  496. done on the returned def will be handled correctly }
  497. str(gencount,countstr);
  498. if symname='' then
  499. genname:=ttypesym(genericdef.typesym).realname
  500. else
  501. genname:=symname;
  502. genname:=genname+'$'+countstr;
  503. ugenname:=upper(genname);
  504. { first check whether the found name is the same as that of
  505. the current def or one of its (generic) surrounding defs;
  506. this is necessary as the symbol of the generic can not yet
  507. be used for lookup as it still contains a reference to an
  508. errordef) }
  509. def:=current_genericdef;
  510. repeat
  511. if def.typ in [objectdef,recorddef] then
  512. if tabstractrecorddef(def).objname^=ugenname then
  513. begin
  514. result:=def;
  515. break;
  516. end;
  517. def:=tstoreddef(def.owner.defowner);
  518. until not assigned(def) or not (df_generic in def.defoptions);
  519. { it's not part of the current object hierarchy, so search
  520. for the symbol }
  521. if not assigned(result) then
  522. begin
  523. srsym:=nil;
  524. if not searchsym(ugenname,srsym,st) or
  525. (srsym.typ<>typesym) then
  526. begin
  527. identifier_not_found(genname);
  528. result:=generrordef;
  529. exit;
  530. end;
  531. result:=ttypesym(srsym).typedef;
  532. { this happens in non-Delphi modes if we encounter a
  533. specialization of the generic class or record we're
  534. currently parsing }
  535. if (result.typ=errordef) and assigned(current_structdef) and
  536. (current_structdef.objname^=ugenname) then
  537. result:=current_structdef;
  538. end;
  539. end;
  540. end;
  541. exit;
  542. end;
  543. if not assigned(parsedtype) and not try_to_consume(_LT) then
  544. begin
  545. consume(_LSHARPBRACKET);
  546. { handle "<>" }
  547. if (token=_GT) or (token=_RSHARPBRACKET) then
  548. begin
  549. Message(type_e_type_id_expected);
  550. if not try_to_consume(_GT) then
  551. try_to_consume(_RSHARPBRACKET);
  552. result:=generrordef;
  553. exit;
  554. end;
  555. end;
  556. context:=tspecializationcontext.create;
  557. { Parse type parameters }
  558. err:=not parse_generic_specialization_types_internal(context.genericdeflist,context.poslist,context.prettyname,context.specializename,parsedtype,parsedpos);
  559. if err then
  560. begin
  561. if not try_to_consume(_GT) then
  562. try_to_consume(_RSHARPBRACKET);
  563. context.free;
  564. context:=nil;
  565. result:=generrordef;
  566. exit;
  567. end;
  568. { use the name of the symbol as procvars return a user friendly version
  569. of the name }
  570. if symname='' then
  571. begin
  572. if genericdef.typ=procdef then
  573. genname:=tprocdef(genericdef).procsym.realname
  574. else
  575. genname:=ttypesym(genericdef.typesym).realname;
  576. end
  577. else
  578. genname:=symname;
  579. { in case of non-Delphi mode the type name could already be a generic
  580. def (but maybe the wrong one) }
  581. if assigned(genericdef) and
  582. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  583. begin
  584. { remove the type count suffix from the generic's name }
  585. for i:=Length(genname) downto 1 do
  586. if genname[i]='$' then
  587. begin
  588. genname:=copy(genname,1,i-1);
  589. break;
  590. end;
  591. { in case of a specialization we've only reached the specialization
  592. checksum yet }
  593. if df_specialization in genericdef.defoptions then
  594. for i:=length(genname) downto 1 do
  595. if genname[i]='$' then
  596. begin
  597. genname:=copy(genname,1,i-1);
  598. break;
  599. end;
  600. end
  601. else
  602. begin
  603. split_generic_name(genname,ugenname,gencount);
  604. if genname<>ugenname then
  605. genname:=ugenname;
  606. end;
  607. { search a generic with the given count of params }
  608. countstr:='';
  609. str(context.genericdeflist.Count,countstr);
  610. genname:=genname+'$'+countstr;
  611. ugenname:=upper(genname);
  612. context.genname:=genname;
  613. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  614. begin
  615. if genericdef.owner.symtabletype = objectsymtable then
  616. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,context.sym,context.symtable,[])
  617. else
  618. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,context.sym,context.symtable);
  619. if not found then
  620. found:=searchsym(ugenname,context.sym,context.symtable);
  621. end
  622. else
  623. found:=searchsym(ugenname,context.sym,context.symtable);
  624. if not found or not (context.sym.typ in [typesym,procsym]) then
  625. begin
  626. identifier_not_found(genname);
  627. if not try_to_consume(_GT) then
  628. try_to_consume(_RSHARPBRACKET);
  629. context.free;
  630. context:=nil;
  631. result:=generrordef;
  632. exit;
  633. end;
  634. { we've found the correct def }
  635. if context.sym.typ=typesym then
  636. result:=tstoreddef(ttypesym(context.sym).typedef)
  637. else
  638. begin
  639. if tprocsym(context.sym).procdeflist.count=0 then
  640. internalerror(2015061203);
  641. result:=tstoreddef(tprocsym(context.sym).procdefList[0]);
  642. end;
  643. if not try_to_consume(_GT) then
  644. consume(_RSHARPBRACKET);
  645. end;
  646. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;_prettyname:ansistring):tdef;
  647. procedure unset_forwarddef(def: tdef);
  648. var
  649. st : TSymtable;
  650. i : longint;
  651. begin
  652. case def.typ of
  653. procdef:
  654. tprocdef(def).forwarddef:=false;
  655. objectdef,
  656. recorddef:
  657. begin
  658. st:=def.getsymtable(gs_record);
  659. for i:=0 to st.deflist.count-1 do
  660. unset_forwarddef(tdef(st.deflist[i]));
  661. end;
  662. else
  663. ;
  664. end;
  665. end;
  666. procedure retrieve_genericdef_or_procsym(sym:tsym;out gendef:tdef;out psym:tsym);
  667. var
  668. i : longint;
  669. begin
  670. gendef:=nil;
  671. psym:=nil;
  672. case sym.typ of
  673. typesym:
  674. begin
  675. gendef:=ttypesym(sym).typedef
  676. end;
  677. procsym:
  678. begin
  679. for i:=0 to tprocsym(sym).procdeflist.count-1 do
  680. if tstoreddef(tprocsym(sym).procdeflist[i]).genericdef=genericdef then
  681. begin
  682. gendef:=tdef(tprocsym(sym).procdeflist[i]);
  683. break;
  684. end;
  685. psym:=sym;
  686. end
  687. else
  688. internalerror(200710171);
  689. end;
  690. end;
  691. var
  692. finalspecializename,
  693. ufinalspecializename : tidstring;
  694. prettyname : ansistring;
  695. generictypelist : tfphashobjectlist;
  696. srsymtable,
  697. specializest : tsymtable;
  698. hashedid : thashedidstring;
  699. tempst : tglobalsymtable;
  700. psym,
  701. srsym : tsym;
  702. def : tdef;
  703. old_block_type : tblock_type;
  704. state : tspecializationstate;
  705. old_current_structdef : tabstractrecorddef;
  706. old_current_specializedef,
  707. old_current_genericdef : tstoreddef;
  708. old_current_procinfo : tprocinfo;
  709. old_module_procinfo : tobject;
  710. hmodule : tmodule;
  711. oldcurrent_filepos : tfileposinfo;
  712. recordbuf : tdynamicarray;
  713. hadtypetoken : boolean;
  714. vmtbuilder : tvmtbuilder;
  715. i,
  716. replaydepth : longint;
  717. item : tobject;
  718. allequal,
  719. hintsprocessed : boolean;
  720. pd : tprocdef;
  721. pdflags : tpdflags;
  722. begin
  723. if not assigned(context) then
  724. internalerror(2015052203);
  725. result:=nil;
  726. pd:=nil;
  727. if not check_generic_constraints(genericdef,context.genericdeflist,context.poslist) then
  728. begin
  729. { the parameters didn't fit the constraints, so don't continue with the
  730. specialization }
  731. result:=generrordef;
  732. exit;
  733. end;
  734. { build the new type's name }
  735. finalspecializename:=generate_generic_name(context.genname,context.specializename,genericdef.ownerhierarchyname);
  736. ufinalspecializename:=upper(finalspecializename);
  737. if genericdef.typ=procdef then
  738. prettyname:=tprocdef(genericdef).procsym.prettyname
  739. else
  740. prettyname:=genericdef.typesym.prettyname;
  741. prettyname:=prettyname+'<'+context.prettyname+'>';
  742. generictypelist:=tfphashobjectlist.create(false);
  743. { build the list containing the types for the generic params }
  744. if not assigned(genericdef.genericparas) then
  745. internalerror(2013092601);
  746. if context.genericdeflist.count<>genericdef.genericparas.count then
  747. internalerror(2013092603);
  748. for i:=0 to genericdef.genericparas.Count-1 do
  749. begin
  750. srsym:=tsym(genericdef.genericparas[i]);
  751. if not (sp_generic_para in srsym.symoptions) then
  752. internalerror(2013092602);
  753. generictypelist.add(srsym.realname,tdef(context.genericdeflist[i]).typesym);
  754. end;
  755. { Special case if we are referencing the current defined object }
  756. if assigned(current_structdef) and
  757. (current_structdef.objname^=ufinalspecializename) then
  758. result:=current_structdef;
  759. { Can we reuse an already specialized type? }
  760. { for this first check whether we are currently specializing a nested
  761. type of the current (main) specialization (this is necessary, because
  762. during that time the symbol of the main specialization will still
  763. contain a reference to an errordef) }
  764. if not assigned(result) and assigned(current_specializedef) then
  765. begin
  766. def:=current_specializedef;
  767. repeat
  768. if def.typ in [objectdef,recorddef] then
  769. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  770. result:=def;
  771. break;
  772. end;
  773. if assigned(def.owner) then
  774. def:=tstoreddef(def.owner.defowner)
  775. else
  776. { this can happen when specializing a generic function }
  777. def:=nil;
  778. until not assigned(def) or not (df_specialization in def.defoptions);
  779. end;
  780. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  781. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  782. parsing anyway), so for now we treat those still as generic defs without doing a partial
  783. specialization }
  784. if not assigned(result) then
  785. begin
  786. def:=current_genericdef;
  787. while assigned(def) and (def.typ in [recorddef,objectdef]) do
  788. begin
  789. if (df_generic in def.defoptions) and (def=genericdef) then
  790. begin
  791. result:=def;
  792. break;
  793. end;
  794. { the following happens when a routine with its parent struct
  795. as parameter is specialized as a parameter or result of a
  796. generic function }
  797. if (df_specialization in def.defoptions) and (tstoreddef(def).genericdef=genericdef) then
  798. begin
  799. if tstoreddef(def).genericparas.count=generictypelist.count then
  800. begin
  801. allequal:=true;
  802. for i:=0 to generictypelist.count-1 do
  803. begin
  804. if not equal_defs(ttypesym(generictypelist[i]).typedef,ttypesym(tstoreddef(def).genericparas[i]).typedef) then
  805. begin
  806. allequal:=false;
  807. break;
  808. end;
  809. end;
  810. if allequal then
  811. begin
  812. result:=def;
  813. break;
  814. end;
  815. end;
  816. end;
  817. def:=tstoreddef(def.owner.defowner);
  818. end;
  819. end;
  820. { decide in which symtable to put the specialization }
  821. if parse_generic and not assigned(result) then
  822. begin
  823. srsymtable:=symtablestack.top;
  824. if (srsymtable.symtabletype in [localsymtable,parasymtable]) and tstoreddef(srsymtable.defowner).is_specialization then
  825. { if we are currently specializing a routine we need to specialize into
  826. the routine's local- or parasymtable so that they are correctly
  827. registered should the specialization be finalized }
  828. specializest:=srsymtable
  829. else if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  830. { if we are parsing the definition of a method we specialize into
  831. the local symtable of it }
  832. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  833. else
  834. begin
  835. if not assigned(current_genericdef) then
  836. internalerror(2014050901);
  837. { we specialize the partial specialization into the symtable of the currently parsed
  838. generic }
  839. case current_genericdef.typ of
  840. procvardef:
  841. specializest:=current_genericdef.getsymtable(gs_para);
  842. procdef:
  843. specializest:=current_genericdef.getsymtable(gs_local);
  844. objectdef,
  845. recorddef:
  846. specializest:=current_genericdef.getsymtable(gs_record);
  847. arraydef:
  848. specializest:=tarraydef(current_genericdef).symtable;
  849. else
  850. internalerror(2014050902);
  851. end;
  852. end;
  853. end
  854. else
  855. if current_module.is_unit and current_module.in_interface then
  856. specializest:=current_module.globalsymtable
  857. else
  858. specializest:=current_module.localsymtable;
  859. if not assigned(specializest) then
  860. internalerror(2014050910);
  861. { now check whether there is a specialization somewhere else }
  862. psym:=nil;
  863. if not assigned(result) then
  864. begin
  865. hashedid.id:=ufinalspecializename;
  866. if specializest.symtabletype=objectsymtable then
  867. begin
  868. { search also in parent classes }
  869. if not assigned(current_genericdef) or (current_genericdef.typ<>objectdef) then
  870. internalerror(2016112901);
  871. if not searchsym_in_class(tobjectdef(current_genericdef),tobjectdef(current_genericdef),ufinalspecializename,srsym,srsymtable,[]) then
  872. srsym:=nil;
  873. end
  874. else
  875. srsym:=tsym(specializest.findwithhash(hashedid));
  876. if assigned(srsym) then
  877. begin
  878. retrieve_genericdef_or_procsym(srsym,result,psym);
  879. end
  880. else
  881. { the generic could have been specialized in the globalsymtable
  882. already, so search there as well }
  883. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  884. begin
  885. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  886. if assigned(srsym) then
  887. begin
  888. retrieve_genericdef_or_procsym(srsym,result,psym);
  889. end;
  890. end;
  891. end;
  892. if not assigned(result) then
  893. begin
  894. specialization_init(genericdef,state);
  895. { push a temporary global symtable so that the specialization is
  896. added to the correct symtable; this symtable does not contain
  897. any other symbols, so that the type resolution can not be
  898. influenced by symbols in the current unit }
  899. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  900. symtablestack.push(tempst);
  901. { Reparse the original type definition }
  902. begin
  903. old_current_specializedef:=nil;
  904. old_current_genericdef:=nil;
  905. old_current_structdef:=nil;
  906. old_current_procinfo:=current_procinfo;
  907. old_module_procinfo:=current_module.procinfo;
  908. current_procinfo:=nil;
  909. current_module.procinfo:=nil;
  910. if parse_class_parent then
  911. begin
  912. old_current_structdef:=current_structdef;
  913. old_current_genericdef:=current_genericdef;
  914. old_current_specializedef:=current_specializedef;
  915. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  916. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  917. else
  918. current_structdef:=nil;
  919. current_genericdef:=nil;
  920. current_specializedef:=nil;
  921. end;
  922. maybe_add_waiting_unit(genericdef);
  923. { First a new sym so we can reuse this specialization and
  924. references to this specialization can be handled }
  925. if genericdef.typ=procdef then
  926. if assigned(psym) then
  927. srsym:=psym
  928. else
  929. srsym:=cprocsym.create(finalspecializename)
  930. else
  931. srsym:=ctypesym.create(finalspecializename,generrordef);
  932. { insert the symbol only if we don't know already that we have
  933. a procsym to add it to }
  934. if not assigned(psym) then
  935. specializest.insert(srsym);
  936. { specializations are declarations as such it is the wisest to
  937. declare set the blocktype to "type"; otherwise we'll
  938. experience unexpected side effects like the addition of
  939. classrefdefs if we have a generic that's derived from another
  940. generic }
  941. old_block_type:=block_type;
  942. block_type:=bt_type;
  943. if (
  944. (genericdef.typ=procdef) and
  945. not assigned(tprocdef(genericdef).genericdecltokenbuf)
  946. ) or (
  947. (genericdef.typ<>procdef) and
  948. not assigned(genericdef.generictokenbuf)
  949. ) then
  950. internalerror(200511171);
  951. hmodule:=find_module_from_symtable(genericdef.owner);
  952. if hmodule=nil then
  953. internalerror(2012051202);
  954. oldcurrent_filepos:=current_filepos;
  955. { use the index the module got from the current compilation process }
  956. current_filepos.moduleindex:=hmodule.unit_index;
  957. current_tokenpos:=current_filepos;
  958. if parse_generic then
  959. begin
  960. recordbuf:=current_scanner.recordtokenbuf;
  961. current_scanner.recordtokenbuf:=nil;
  962. end
  963. else
  964. recordbuf:=nil;
  965. replaydepth:=current_scanner.replay_stack_depth;
  966. if genericdef.typ=procdef then
  967. begin
  968. current_scanner.startreplaytokens(tprocdef(genericdef).genericdecltokenbuf);
  969. parse_proc_head(tprocdef(genericdef).struct,tprocdef(genericdef).proctypeoption,false,genericdef,generictypelist,pd);
  970. if assigned(pd) then
  971. begin
  972. if assigned(psym) then
  973. pd.procsym:=psym
  974. else
  975. pd.procsym:=srsym;
  976. parse_proc_dec_finish(pd,po_classmethod in tprocdef(genericdef).procoptions,tprocdef(genericdef).struct);
  977. end;
  978. result:=pd;
  979. end
  980. else
  981. begin
  982. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  983. hadtypetoken:=false;
  984. read_named_type(result,srsym,genericdef,generictypelist,false,hadtypetoken);
  985. ttypesym(srsym).typedef:=result;
  986. result.typesym:=srsym;
  987. if _prettyname<>'' then
  988. ttypesym(result.typesym).fprettyname:=_prettyname
  989. else
  990. ttypesym(result.typesym).fprettyname:=prettyname;
  991. end;
  992. current_filepos:=oldcurrent_filepos;
  993. { Note regarding hint directives:
  994. There is no need to remove the flags for them from the
  995. specialized generic symbol, because hint directives that
  996. follow the specialization are handled by the code in
  997. pdecl.types_dec and added to the type symbol.
  998. E.g.: TFoo = TBar<Blubb> deprecated;
  999. Here the symbol TBar$1$Blubb will contain the
  1000. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  1001. case result.typ of
  1002. { Build VMT indexes for classes and read hint directives }
  1003. objectdef:
  1004. begin
  1005. if replaydepth>current_scanner.replay_stack_depth then
  1006. begin
  1007. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1008. if replaydepth>current_scanner.replay_stack_depth then
  1009. consume(_SEMICOLON);
  1010. end;
  1011. vmtbuilder:=TVMTBuilder.Create(tobjectdef(result));
  1012. vmtbuilder.generate_vmt;
  1013. vmtbuilder.free;
  1014. end;
  1015. { handle params, calling convention, etc }
  1016. procvardef:
  1017. begin
  1018. hintsprocessed:=false;
  1019. if replaydepth>current_scanner.replay_stack_depth then
  1020. begin
  1021. if not check_proc_directive(true) then
  1022. begin
  1023. hintsprocessed:=try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1024. if replaydepth>current_scanner.replay_stack_depth then
  1025. consume(_SEMICOLON);
  1026. end
  1027. else
  1028. hintsprocessed:=true;
  1029. end;
  1030. if replaydepth>current_scanner.replay_stack_depth then
  1031. parse_var_proc_directives(ttypesym(srsym));
  1032. handle_calling_convention(tprocvardef(result),hcc_default_actions_intf);
  1033. if not hintsprocessed and (replaydepth>current_scanner.replay_stack_depth) then
  1034. begin
  1035. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1036. if replaydepth>current_scanner.replay_stack_depth then
  1037. consume(_SEMICOLON);
  1038. end;
  1039. end;
  1040. procdef:
  1041. begin
  1042. pdflags:=[pd_body,pd_implemen];
  1043. if genericdef.owner.symtabletype=objectsymtable then
  1044. include(pdflags,pd_object)
  1045. else if genericdef.owner.symtabletype=recordsymtable then
  1046. include(pdflags,pd_record);
  1047. parse_proc_directives(pd,pdflags);
  1048. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1049. consume(_SEMICOLON);
  1050. if parse_generic then
  1051. handle_calling_convention(tprocdef(result),hcc_default_actions_intf)
  1052. else
  1053. handle_calling_convention(tprocdef(result),hcc_default_actions_impl);
  1054. proc_add_definition(tprocdef(result));
  1055. { for partial specializations we implicitely declare the routine as
  1056. having its implementation although we'll not specialize it in reality }
  1057. if parse_generic then
  1058. unset_forwarddef(result);
  1059. end;
  1060. else
  1061. { parse hint directives for records and arrays }
  1062. if replaydepth>current_scanner.replay_stack_depth then begin
  1063. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1064. if replaydepth>current_scanner.replay_stack_depth then
  1065. consume(_SEMICOLON);
  1066. end;
  1067. end;
  1068. { Consume the remainder of the buffer }
  1069. while current_scanner.replay_stack_depth>replaydepth do
  1070. consume(token);
  1071. if assigned(recordbuf) then
  1072. begin
  1073. if assigned(current_scanner.recordtokenbuf) then
  1074. internalerror(2014050909);
  1075. current_scanner.recordtokenbuf:=recordbuf;
  1076. end;
  1077. block_type:=old_block_type;
  1078. current_procinfo:=old_current_procinfo;
  1079. current_module.procinfo:=old_module_procinfo;
  1080. if parse_class_parent then
  1081. begin
  1082. current_structdef:=old_current_structdef;
  1083. current_genericdef:=old_current_genericdef;
  1084. current_specializedef:=old_current_specializedef;
  1085. end;
  1086. end;
  1087. { extract all created symbols and defs from the temporary symtable
  1088. and add them to the specializest }
  1089. for i:=tempst.SymList.Count-1 downto 0 do
  1090. begin
  1091. item:=tempst.SymList.Items[i];
  1092. { using changeowner the symbol is automatically added to the
  1093. new symtable }
  1094. tsym(item).ChangeOwner(specializest);
  1095. end;
  1096. for i:=tempst.DefList.Count-1 downto 0 do
  1097. begin
  1098. item:=tempst.DefList.Items[i];
  1099. { using changeowner the def is automatically added to the new
  1100. symtable }
  1101. tdef(item).ChangeOwner(specializest);
  1102. { for partial specializations we implicitely declare any methods as having their
  1103. implementations although we'll not specialize them in reality }
  1104. if parse_generic then
  1105. unset_forwarddef(tdef(item));
  1106. end;
  1107. { if a generic was declared during the specialization we need to
  1108. flag the specialize symtable accordingly }
  1109. if sto_has_generic in tempst.tableoptions then
  1110. specializest.includeoption(sto_has_generic);
  1111. tempst.free;
  1112. specialization_done(state);
  1113. { procdefs are only added once we know which overload we use }
  1114. if not parse_generic and (result.typ<>procdef) then
  1115. current_module.pendingspecializations.add(result.typename,result);
  1116. end;
  1117. generictypelist.free;
  1118. if assigned(genericdef) then
  1119. begin
  1120. { check the hints of the found generic symbol }
  1121. if genericdef.typ=procdef then
  1122. srsym:=tprocdef(genericdef).procsym
  1123. else
  1124. srsym:=genericdef.typesym;
  1125. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1126. end;
  1127. end;
  1128. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);
  1129. var
  1130. context : tspecializationcontext;
  1131. genericdef : tstoreddef;
  1132. begin
  1133. genericdef:=tstoreddef(generate_specialization_phase1(context,tt,parsedtype,symname,parsedpos));
  1134. if genericdef<>generrordef then
  1135. genericdef:=tstoreddef(generate_specialization_phase2(context,genericdef,parse_class_parent,_prettyname));
  1136. tt:=genericdef;
  1137. if assigned(context) then
  1138. context.free;
  1139. end;
  1140. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  1141. var
  1142. generictype : ttypesym;
  1143. i,firstidx : longint;
  1144. srsymtable : tsymtable;
  1145. basedef,def : tdef;
  1146. defname : tidstring;
  1147. allowconstructor,
  1148. doconsume : boolean;
  1149. constraintdata : tgenericconstraintdata;
  1150. old_block_type : tblock_type;
  1151. begin
  1152. result:=tfphashobjectlist.create(false);
  1153. firstidx:=0;
  1154. old_block_type:=block_type;
  1155. block_type:=bt_type;
  1156. repeat
  1157. if token=_ID then
  1158. begin
  1159. generictype:=ctypesym.create(orgpattern,cundefinedtype);
  1160. { type parameters need to be added as strict private }
  1161. generictype.visibility:=vis_strictprivate;
  1162. include(generictype.symoptions,sp_generic_para);
  1163. result.add(orgpattern,generictype);
  1164. end;
  1165. consume(_ID);
  1166. if try_to_consume(_COLON) then
  1167. begin
  1168. if not allowconstraints then
  1169. { TODO }
  1170. Message(parser_e_illegal_expression{ parser_e_generic_constraints_not_allowed_here});
  1171. { construct a name which can be used for a type specification }
  1172. constraintdata:=tgenericconstraintdata.create;
  1173. defname:='';
  1174. str(current_module.deflist.count,defname);
  1175. defname:='$gendef'+defname;
  1176. allowconstructor:=m_delphi in current_settings.modeswitches;
  1177. basedef:=generrordef;
  1178. repeat
  1179. doconsume:=true;
  1180. case token of
  1181. _CONSTRUCTOR:
  1182. begin
  1183. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  1184. Message(parser_e_illegal_expression);
  1185. include(constraintdata.flags,gcf_constructor);
  1186. allowconstructor:=false;
  1187. end;
  1188. _CLASS:
  1189. begin
  1190. if gcf_class in constraintdata.flags then
  1191. Message(parser_e_illegal_expression);
  1192. if basedef=generrordef then
  1193. include(constraintdata.flags,gcf_class)
  1194. else
  1195. Message(parser_e_illegal_expression);
  1196. end;
  1197. _RECORD:
  1198. begin
  1199. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  1200. or (constraintdata.interfaces.count>0) then
  1201. Message(parser_e_illegal_expression)
  1202. else
  1203. begin
  1204. srsymtable:=trecordsymtable.create(defname,0,1);
  1205. basedef:=crecorddef.create(defname,srsymtable);
  1206. include(constraintdata.flags,gcf_record);
  1207. allowconstructor:=false;
  1208. end;
  1209. end;
  1210. else
  1211. begin
  1212. { after single_type "token" is the trailing ",", ";" or
  1213. ">"! }
  1214. doconsume:=false;
  1215. { def is already set to a class or record }
  1216. if gcf_record in constraintdata.flags then
  1217. Message(parser_e_illegal_expression);
  1218. single_type(def, [stoAllowSpecialization]);
  1219. { only types that are inheritable are allowed }
  1220. if (def.typ<>objectdef) or
  1221. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  1222. Message1(type_e_class_or_interface_type_expected,def.typename)
  1223. else
  1224. case tobjectdef(def).objecttype of
  1225. odt_class,
  1226. odt_javaclass:
  1227. begin
  1228. if gcf_class in constraintdata.flags then
  1229. { "class" + concrete class is not allowed }
  1230. Message(parser_e_illegal_expression)
  1231. else
  1232. { do we already have a concrete class? }
  1233. if basedef<>generrordef then
  1234. Message(parser_e_illegal_expression)
  1235. else
  1236. basedef:=def;
  1237. end;
  1238. odt_interfacecom,
  1239. odt_interfacecorba,
  1240. odt_interfacejava,
  1241. odt_dispinterface:
  1242. constraintdata.interfaces.add(def);
  1243. else
  1244. ;
  1245. end;
  1246. end;
  1247. end;
  1248. if doconsume then
  1249. consume(token);
  1250. until not try_to_consume(_COMMA);
  1251. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  1252. (constraintdata.interfaces.count>1) or
  1253. (
  1254. (basedef.typ=objectdef) and
  1255. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  1256. ) then
  1257. begin
  1258. if basedef.typ=errordef then
  1259. { don't pass an errordef as a parent to a tobjectdef }
  1260. basedef:=class_tobject
  1261. else
  1262. if (basedef.typ<>objectdef) or
  1263. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  1264. internalerror(2012101101);
  1265. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef),false);
  1266. for i:=0 to constraintdata.interfaces.count-1 do
  1267. tobjectdef(basedef).implementedinterfaces.add(
  1268. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  1269. end
  1270. else
  1271. if constraintdata.interfaces.count=1 then
  1272. begin
  1273. if basedef.typ<>errordef then
  1274. internalerror(2013021601);
  1275. def:=tdef(constraintdata.interfaces[0]);
  1276. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def),false);
  1277. constraintdata.interfaces.delete(0);
  1278. end;
  1279. if basedef.typ<>errordef then
  1280. with tstoreddef(basedef) do
  1281. begin
  1282. genconstraintdata:=tgenericconstraintdata.create;
  1283. genconstraintdata.flags:=constraintdata.flags;
  1284. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  1285. include(defoptions,df_genconstraint);
  1286. end;
  1287. for i:=firstidx to result.count-1 do
  1288. ttypesym(result[i]).typedef:=basedef;
  1289. { we need a typesym in case we do a Delphi-mode inline
  1290. specialization with this parameter; so just use the first sym }
  1291. if not assigned(basedef.typesym) then
  1292. basedef.typesym:=ttypesym(result[firstidx]);
  1293. firstidx:=result.count;
  1294. constraintdata.free;
  1295. end
  1296. else
  1297. begin
  1298. if token=_SEMICOLON then
  1299. begin
  1300. { two different typeless parameters are considered as incompatible }
  1301. for i:=firstidx to result.count-1 do
  1302. begin
  1303. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1304. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1305. end;
  1306. { a semicolon terminates a type parameter group }
  1307. firstidx:=result.count;
  1308. end;
  1309. end;
  1310. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  1311. { two different typeless parameters are considered as incompatible }
  1312. for i:=firstidx to result.count-1 do
  1313. begin
  1314. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1315. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1316. end;
  1317. block_type:=old_block_type;
  1318. end;
  1319. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1320. var
  1321. i : longint;
  1322. generictype,sym : ttypesym;
  1323. st : tsymtable;
  1324. begin
  1325. def.genericdef:=genericdef;
  1326. if not assigned(genericlist) then
  1327. exit;
  1328. if assigned(genericdef) then
  1329. include(def.defoptions,df_specialization)
  1330. else
  1331. if genericlist.count>0 then
  1332. include(def.defoptions,df_generic);
  1333. case def.typ of
  1334. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  1335. arraydef: st:=tarraydef(def).symtable;
  1336. procvardef,procdef: st:=tabstractprocdef(def).parast;
  1337. else
  1338. internalerror(201101020);
  1339. end;
  1340. if (genericlist.count>0) and not assigned(def.genericparas) then
  1341. def.genericparas:=tfphashobjectlist.create(false);
  1342. for i:=0 to genericlist.count-1 do
  1343. begin
  1344. generictype:=ttypesym(genericlist[i]);
  1345. if assigned(generictype.owner) then
  1346. begin
  1347. sym:=ctypesym.create(genericlist.nameofindex(i),generictype.typedef);
  1348. { type parameters need to be added as strict private }
  1349. sym.visibility:=vis_strictprivate;
  1350. st.insert(sym);
  1351. include(sym.symoptions,sp_generic_para);
  1352. end
  1353. else
  1354. begin
  1355. if (generictype.typedef.typ=undefineddef) and (generictype.typedef<>cundefinedtype) then
  1356. begin
  1357. { the generic parameters were parsed before the genericdef existed thus the
  1358. undefineddefs were added as part of the parent symtable }
  1359. if assigned(generictype.typedef.owner) then
  1360. generictype.typedef.owner.DefList.Extract(generictype.typedef);
  1361. generictype.typedef.changeowner(st);
  1362. end;
  1363. st.insert(generictype);
  1364. include(generictype.symoptions,sp_generic_para);
  1365. end;
  1366. def.genericparas.add(genericlist.nameofindex(i),generictype);
  1367. end;
  1368. end;
  1369. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  1370. var
  1371. gensym : ttypesym;
  1372. begin
  1373. { for generics in non-Delphi modes we insert a private type symbol
  1374. that has the same base name as the currently parsed generic and
  1375. that references this defs }
  1376. if not (m_delphi in current_settings.modeswitches) and
  1377. (
  1378. (
  1379. parse_generic and
  1380. assigned(genericlist) and
  1381. (genericlist.count>0)
  1382. ) or
  1383. (
  1384. assigned(current_specializedef) and
  1385. assigned(current_structdef.genericdef) and
  1386. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  1387. (pos('$',name)>0)
  1388. )
  1389. ) then
  1390. begin
  1391. { we need to pass nil as def here, because the constructor wants
  1392. to set the typesym of the def which is not what we want }
  1393. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil);
  1394. gensym.typedef:=current_structdef;
  1395. include(gensym.symoptions,sp_internal);
  1396. { the symbol should be only visible to the generic class
  1397. itself }
  1398. gensym.visibility:=vis_strictprivate;
  1399. symtablestack.top.insert(gensym);
  1400. end;
  1401. end;
  1402. function generate_generic_name(const name:tidstring;specializename:ansistring;owner_hierarchy:string):tidstring;
  1403. var
  1404. crc : cardinal;
  1405. begin
  1406. if specializename='' then
  1407. internalerror(2012061901);
  1408. { build the new type's name }
  1409. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  1410. result:=name+'$crc'+hexstr(crc,8);
  1411. if owner_hierarchy<>'' then
  1412. begin
  1413. crc:=UpdateCrc32(0,owner_hierarchy[1],length(owner_hierarchy));
  1414. result:=result+'$crc'+hexstr(crc,8);
  1415. end;
  1416. end;
  1417. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  1418. var
  1419. i,code : longint;
  1420. countstr : string;
  1421. begin
  1422. for i:=length(name) downto 1 do
  1423. if name[i]='$' then
  1424. begin
  1425. nongeneric:=copy(name,1,i-1);
  1426. countstr:=copy(name,i+1,length(name)-i);
  1427. val(countstr,count,code);
  1428. if code<>0 then
  1429. break;
  1430. exit;
  1431. end;
  1432. nongeneric:=name;
  1433. count:=0;
  1434. end;
  1435. procedure add_generic_dummysym(sym:tsym);
  1436. var
  1437. list: TFPObjectList;
  1438. srsym : tsym;
  1439. srsymtable : tsymtable;
  1440. entry : tgenericdummyentry;
  1441. begin
  1442. if sp_generic_dummy in sym.symoptions then
  1443. begin
  1444. { did we already search for a generic with that name? }
  1445. list:=tfpobjectlist(current_module.genericdummysyms.find(sym.name));
  1446. if not assigned(list) then
  1447. begin
  1448. list:=tfpobjectlist.create(true);
  1449. current_module.genericdummysyms.add(sym.name,list);
  1450. end;
  1451. { is the dummy sym still "dummy"? }
  1452. if (sym.typ=typesym) and
  1453. (
  1454. { dummy sym defined in mode Delphi }
  1455. (ttypesym(sym).typedef.typ=undefineddef) or
  1456. { dummy sym defined in non-Delphi mode }
  1457. (tstoreddef(ttypesym(sym).typedef).is_generic)
  1458. ) then
  1459. begin
  1460. { do we have a non-generic type of the same name
  1461. available? }
  1462. if not searchsym_with_flags(sym.name,srsym,srsymtable,[ssf_no_addsymref]) then
  1463. srsym:=nil;
  1464. end
  1465. else if (sym.typ=procsym) and
  1466. (tprocsym(sym).procdeflist.count>0) then
  1467. srsym:=sym
  1468. else
  1469. { dummy symbol is already not so dummy anymore }
  1470. srsym:=nil;
  1471. if assigned(srsym) then
  1472. begin
  1473. entry:=tgenericdummyentry.create;
  1474. entry.resolvedsym:=srsym;
  1475. entry.dummysym:=sym;
  1476. list.add(entry);
  1477. end;
  1478. end;
  1479. end;
  1480. function resolve_generic_dummysym(const name:tidstring):tsym;
  1481. var
  1482. list : tfpobjectlist;
  1483. begin
  1484. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  1485. if assigned(list) and (list.count>0) then
  1486. result:=tgenericdummyentry(list.last).resolvedsym
  1487. else
  1488. result:=nil;
  1489. end;
  1490. function could_be_generic(const name:tidstring):boolean;
  1491. begin
  1492. result:=(name<>'') and
  1493. (current_module.genericdummysyms.findindexof(name)>=0);
  1494. end;
  1495. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  1496. var
  1497. pu : tused_unit;
  1498. hmodule : tmodule;
  1499. unitsyms : TFPHashObjectList;
  1500. sym : tsym;
  1501. i : Integer;
  1502. begin
  1503. if not assigned(genericdef) then
  1504. internalerror(200705151);
  1505. { Setup symtablestack at definition time
  1506. to get types right, however this is not perfect, we should probably record
  1507. the resolved symbols }
  1508. state.oldsymtablestack:=symtablestack;
  1509. state.oldextendeddefs:=current_module.extendeddefs;
  1510. state.oldgenericdummysyms:=current_module.genericdummysyms;
  1511. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1512. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  1513. symtablestack:=tdefawaresymtablestack.create;
  1514. hmodule:=find_module_from_symtable(genericdef.owner);
  1515. if hmodule=nil then
  1516. internalerror(200705152);
  1517. { collect all unit syms in the generic's unit as we need to establish
  1518. their unitsym.module link again so that unit identifiers can be used }
  1519. unitsyms:=tfphashobjectlist.create(false);
  1520. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1521. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1522. begin
  1523. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1524. if sym.typ=unitsym then
  1525. unitsyms.add(upper(sym.realname),sym);
  1526. end;
  1527. { add all units if we are specializing inside the current unit (as the
  1528. generic could have been declared in the implementation part), but load
  1529. only interface units, if we are in a different unit as then the generic
  1530. needs to be in the interface section }
  1531. pu:=tused_unit(hmodule.used_units.first);
  1532. while assigned(pu) do
  1533. begin
  1534. if not assigned(pu.u.globalsymtable) then
  1535. { in certain circular, but valid unit constellations it can happen
  1536. that we specialize a generic in a different unit that was used
  1537. in the implementation section of the generic's unit and were the
  1538. interface is still being parsed and thus the localsymtable is in
  1539. reality the global symtable }
  1540. if pu.u.in_interface then
  1541. symtablestack.push(pu.u.localsymtable)
  1542. else
  1543. internalerror(200705153)
  1544. else
  1545. symtablestack.push(pu.u.globalsymtable);
  1546. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1547. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1548. tunitsym(sym).module:=pu.u;
  1549. pu:=tused_unit(pu.next);
  1550. end;
  1551. unitsyms.free;
  1552. if assigned(hmodule.globalsymtable) then
  1553. symtablestack.push(hmodule.globalsymtable);
  1554. { push the localsymtable if needed }
  1555. if ((hmodule<>current_module) or not current_module.in_interface)
  1556. and assigned(hmodule.localsymtable) then
  1557. symtablestack.push(hmodule.localsymtable);
  1558. end;
  1559. procedure specialization_done(var state: tspecializationstate);
  1560. begin
  1561. { Restore symtablestack }
  1562. current_module.extendeddefs.free;
  1563. current_module.extendeddefs:=state.oldextendeddefs;
  1564. current_module.genericdummysyms.free;
  1565. current_module.genericdummysyms:=state.oldgenericdummysyms;
  1566. symtablestack.free;
  1567. symtablestack:=state.oldsymtablestack;
  1568. { clear the state record to be on the safe side }
  1569. fillchar(state, sizeof(state), 0);
  1570. end;
  1571. {****************************************************************************
  1572. SPECIALIZATION BODY GENERATION
  1573. ****************************************************************************}
  1574. procedure process_procdef(def:tprocdef;hmodule:tmodule);
  1575. var
  1576. oldcurrent_filepos : tfileposinfo;
  1577. begin
  1578. if assigned(def.genericdef) and
  1579. (def.genericdef.typ=procdef) and
  1580. assigned(tprocdef(def.genericdef).generictokenbuf) then
  1581. begin
  1582. if not assigned(tprocdef(def.genericdef).generictokenbuf) then
  1583. internalerror(2015061902);
  1584. oldcurrent_filepos:=current_filepos;
  1585. current_filepos:=tprocdef(def.genericdef).fileinfo;
  1586. { use the index the module got from the current compilation process }
  1587. current_filepos.moduleindex:=hmodule.unit_index;
  1588. current_tokenpos:=current_filepos;
  1589. current_scanner.startreplaytokens(tprocdef(def.genericdef).generictokenbuf);
  1590. read_proc_body(def);
  1591. current_filepos:=oldcurrent_filepos;
  1592. end
  1593. { synthetic routines will be implemented afterwards }
  1594. else if def.synthetickind=tsk_none then
  1595. MessagePos1(def.fileinfo,sym_e_forward_not_resolved,def.fullprocname(false));
  1596. end;
  1597. function process_abstractrecorddef(def:tabstractrecorddef):boolean;
  1598. var
  1599. i : longint;
  1600. hp : tdef;
  1601. hmodule : tmodule;
  1602. begin
  1603. result:=true;
  1604. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1605. if hmodule=nil then
  1606. internalerror(201202041);
  1607. for i:=0 to def.symtable.DefList.Count-1 do
  1608. begin
  1609. hp:=tdef(def.symtable.DefList[i]);
  1610. if hp.typ=procdef then
  1611. begin
  1612. { only generate the code if we need a body }
  1613. if assigned(tprocdef(hp).struct) and not tprocdef(hp).forwarddef then
  1614. continue;
  1615. { and the body is available already (which is implicitely the
  1616. case if the generic routine is part of another unit) }
  1617. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and
  1618. { may not be assigned in case it's a synthetic procdef that
  1619. still needs to be generated }
  1620. assigned(tprocdef(hp).genericdef) and
  1621. tprocdef(tprocdef(hp).genericdef).forwarddef then
  1622. begin
  1623. result:=false;
  1624. continue;
  1625. end;
  1626. process_procdef(tprocdef(hp),hmodule);
  1627. end
  1628. else
  1629. if hp.typ in [objectdef,recorddef] then
  1630. { generate code for subtypes as well }
  1631. result:=process_abstractrecorddef(tabstractrecorddef(hp)) and result;
  1632. end;
  1633. end;
  1634. procedure generate_specialization_procs;
  1635. var
  1636. i : longint;
  1637. list,
  1638. readdlist : tfpobjectlist;
  1639. def : tstoreddef;
  1640. state : tspecializationstate;
  1641. hmodule : tmodule;
  1642. begin
  1643. { first copy all entries and then work with that list to ensure that
  1644. we don't get an infinite recursion }
  1645. list:=tfpobjectlist.create(false);
  1646. readdlist:=tfpobjectlist.create(false);
  1647. for i:=0 to current_module.pendingspecializations.Count-1 do
  1648. list.add(current_module.pendingspecializations.Items[i]);
  1649. current_module.pendingspecializations.clear;
  1650. for i:=0 to list.count-1 do
  1651. begin
  1652. def:=tstoreddef(list[i]);
  1653. if not tstoreddef(def).is_specialization then
  1654. continue;
  1655. case def.typ of
  1656. procdef:
  1657. begin
  1658. { the use of forwarddef should not backfire as the
  1659. specialization always belongs to the current module }
  1660. if not tprocdef(def).forwarddef then
  1661. continue;
  1662. if not assigned(def.genericdef) then
  1663. internalerror(2015061903);
  1664. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1665. if hmodule=nil then
  1666. internalerror(2015061904);
  1667. { we need to check for a forward declaration only if the
  1668. generic was declared in the same unit (otherwise there
  1669. should be one) }
  1670. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and tprocdef(def.genericdef).forwarddef then
  1671. begin
  1672. readdlist.add(def);
  1673. continue;
  1674. end;
  1675. specialization_init(tstoreddef(def).genericdef,state);
  1676. process_procdef(tprocdef(def),hmodule);
  1677. specialization_done(state);
  1678. end;
  1679. recorddef,
  1680. objectdef:
  1681. begin
  1682. specialization_init(tstoreddef(def).genericdef,state);
  1683. if not process_abstractrecorddef(tabstractrecorddef(def)) then
  1684. readdlist.add(def);
  1685. specialization_done(state);
  1686. end;
  1687. else
  1688. ;
  1689. end;
  1690. end;
  1691. { add those defs back to the pending list for which we don't yet have
  1692. all method bodies }
  1693. for i:=0 to readdlist.count-1 do
  1694. current_module.pendingspecializations.add(tstoreddef(readdlist[i]).typename,readdlist[i]);
  1695. readdlist.free;
  1696. list.free;
  1697. end;
  1698. procedure maybe_add_pending_specialization(def:tdef);
  1699. var
  1700. hmodule : tmodule;
  1701. st : tsymtable;
  1702. begin
  1703. if parse_generic then
  1704. exit;
  1705. st:=def.owner;
  1706. while st.symtabletype in [localsymtable] do
  1707. st:=st.defowner.owner;
  1708. hmodule:=find_module_from_symtable(st);
  1709. if tstoreddef(def).is_specialization and (hmodule=current_module) then
  1710. current_module.pendingspecializations.add(def.typename,def);
  1711. end;
  1712. end.