pgenutil.pas 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  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. tmpstack : tfpobjectlist;
  437. begin
  438. context:=nil;
  439. result:=nil;
  440. { either symname must be given or genericdef needs to be valid }
  441. errorrecovery:=false;
  442. if (symname='') and
  443. (not assigned(genericdef) or
  444. (
  445. (genericdef.typ<>procdef) and
  446. (
  447. not assigned(genericdef.typesym) or
  448. (genericdef.typesym.typ<>typesym)
  449. )
  450. ) or
  451. (
  452. (genericdef.typ=procdef) and
  453. (
  454. not assigned(tprocdef(genericdef).procsym) or
  455. (tprocdef(genericdef).procsym.typ<>procsym)
  456. )
  457. )
  458. ) then
  459. begin
  460. errorrecovery:=true;
  461. result:=generrordef;
  462. end;
  463. { Only parse the parameters for recovery or
  464. for recording in genericbuf }
  465. if errorrecovery then
  466. begin
  467. first:=assigned(parsedtype);
  468. if not first and not try_to_consume(_LT) then
  469. consume(_LSHARPBRACKET);
  470. gencount:=0;
  471. { handle "<>" }
  472. if not first and ((token=_RSHARPBRACKET) or (token=_GT)) then
  473. Message(type_e_type_id_expected)
  474. else
  475. repeat
  476. if not first then
  477. begin
  478. pt2:=factor(false,[ef_type_only]);
  479. pt2.free;
  480. end;
  481. first:=false;
  482. inc(gencount);
  483. until not try_to_consume(_COMMA);
  484. if not try_to_consume(_GT) then
  485. consume(_RSHARPBRACKET);
  486. { we need to return a def that can later pass some checks like
  487. whether it's an interface or not }
  488. if not errorrecovery and
  489. (not assigned(result) or (result.typ=undefineddef)) then
  490. begin
  491. if (symname='') and tstoreddef(genericdef).is_generic then
  492. { this happens in non-Delphi modes }
  493. result:=genericdef
  494. else
  495. begin
  496. { find the corresponding generic symbol so that any checks
  497. done on the returned def will be handled correctly }
  498. str(gencount,countstr);
  499. if symname='' then
  500. genname:=ttypesym(genericdef.typesym).realname
  501. else
  502. genname:=symname;
  503. genname:=genname+'$'+countstr;
  504. ugenname:=upper(genname);
  505. { first check whether the found name is the same as that of
  506. the current def or one of its (generic) surrounding defs;
  507. this is necessary as the symbol of the generic can not yet
  508. be used for lookup as it still contains a reference to an
  509. errordef) }
  510. def:=current_genericdef;
  511. repeat
  512. if def.typ in [objectdef,recorddef] then
  513. if tabstractrecorddef(def).objname^=ugenname then
  514. begin
  515. result:=def;
  516. break;
  517. end;
  518. def:=tstoreddef(def.owner.defowner);
  519. until not assigned(def) or not (df_generic in def.defoptions);
  520. { it's not part of the current object hierarchy, so search
  521. for the symbol }
  522. if not assigned(result) then
  523. begin
  524. srsym:=nil;
  525. if not searchsym(ugenname,srsym,st) or
  526. (srsym.typ<>typesym) then
  527. begin
  528. identifier_not_found(genname);
  529. result:=generrordef;
  530. exit;
  531. end;
  532. result:=ttypesym(srsym).typedef;
  533. { this happens in non-Delphi modes if we encounter a
  534. specialization of the generic class or record we're
  535. currently parsing }
  536. if (result.typ=errordef) and assigned(current_structdef) and
  537. (current_structdef.objname^=ugenname) then
  538. result:=current_structdef;
  539. end;
  540. end;
  541. end;
  542. exit;
  543. end;
  544. if not assigned(parsedtype) and not try_to_consume(_LT) then
  545. begin
  546. consume(_LSHARPBRACKET);
  547. { handle "<>" }
  548. if (token=_GT) or (token=_RSHARPBRACKET) then
  549. begin
  550. Message(type_e_type_id_expected);
  551. if not try_to_consume(_GT) then
  552. try_to_consume(_RSHARPBRACKET);
  553. result:=generrordef;
  554. exit;
  555. end;
  556. end;
  557. context:=tspecializationcontext.create;
  558. { Parse type parameters }
  559. err:=not parse_generic_specialization_types_internal(context.genericdeflist,context.poslist,context.prettyname,context.specializename,parsedtype,parsedpos);
  560. if err then
  561. begin
  562. if not try_to_consume(_GT) then
  563. try_to_consume(_RSHARPBRACKET);
  564. context.free;
  565. context:=nil;
  566. result:=generrordef;
  567. exit;
  568. end;
  569. { use the name of the symbol as procvars return a user friendly version
  570. of the name }
  571. if symname='' then
  572. begin
  573. if genericdef.typ=procdef then
  574. genname:=tprocdef(genericdef).procsym.realname
  575. else
  576. genname:=ttypesym(genericdef.typesym).realname;
  577. end
  578. else
  579. genname:=symname;
  580. { in case of non-Delphi mode the type name could already be a generic
  581. def (but maybe the wrong one) }
  582. if assigned(genericdef) and
  583. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  584. begin
  585. { remove the type count suffix from the generic's name }
  586. for i:=Length(genname) downto 1 do
  587. if genname[i]='$' then
  588. begin
  589. genname:=copy(genname,1,i-1);
  590. break;
  591. end;
  592. { in case of a specialization we've only reached the specialization
  593. checksum yet }
  594. if df_specialization in genericdef.defoptions then
  595. for i:=length(genname) downto 1 do
  596. if genname[i]='$' then
  597. begin
  598. genname:=copy(genname,1,i-1);
  599. break;
  600. end;
  601. end
  602. else
  603. begin
  604. split_generic_name(genname,ugenname,gencount);
  605. if genname<>ugenname then
  606. genname:=ugenname;
  607. end;
  608. { search a generic with the given count of params }
  609. countstr:='';
  610. str(context.genericdeflist.Count,countstr);
  611. genname:=genname+'$'+countstr;
  612. ugenname:=upper(genname);
  613. context.genname:=genname;
  614. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  615. begin
  616. if genericdef.owner.symtabletype = objectsymtable then
  617. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,context.sym,context.symtable,[])
  618. else
  619. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,context.sym,context.symtable);
  620. if not found then
  621. found:=searchsym(ugenname,context.sym,context.symtable);
  622. end
  623. else
  624. found:=searchsym(ugenname,context.sym,context.symtable);
  625. if found and (context.sym.typ=absolutevarsym) and
  626. (vo_is_funcret in tabstractvarsym(context.sym).varoptions) then
  627. begin
  628. { we found the function result alias of a generic function; go up the
  629. symbol stack *before* this alias was inserted, so that we can
  630. (hopefully) find the correct generic symbol }
  631. tmpstack:=tfpobjectlist.create(false);
  632. while assigned(symtablestack.top) do
  633. begin
  634. tmpstack.Add(symtablestack.top);
  635. symtablestack.pop(symtablestack.top);
  636. if tmpstack.Last=context.symtable then
  637. break;
  638. end;
  639. if not assigned(symtablestack.top) then
  640. internalerror(2019123001);
  641. found:=searchsym(ugenname,context.sym,context.symtable);
  642. for i:=tmpstack.count-1 downto 0 do
  643. symtablestack.push(tsymtable(tmpstack[i]));
  644. tmpstack.free;
  645. end;
  646. if not found or not (context.sym.typ in [typesym,procsym]) then
  647. begin
  648. identifier_not_found(genname);
  649. if not try_to_consume(_GT) then
  650. try_to_consume(_RSHARPBRACKET);
  651. context.free;
  652. context:=nil;
  653. result:=generrordef;
  654. exit;
  655. end;
  656. { we've found the correct def }
  657. if context.sym.typ=typesym then
  658. result:=tstoreddef(ttypesym(context.sym).typedef)
  659. else
  660. begin
  661. if tprocsym(context.sym).procdeflist.count=0 then
  662. internalerror(2015061203);
  663. result:=tstoreddef(tprocsym(context.sym).procdefList[0]);
  664. end;
  665. if not try_to_consume(_GT) then
  666. consume(_RSHARPBRACKET);
  667. end;
  668. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;_prettyname:ansistring):tdef;
  669. procedure unset_forwarddef(def: tdef);
  670. var
  671. st : TSymtable;
  672. i : longint;
  673. begin
  674. case def.typ of
  675. procdef:
  676. tprocdef(def).forwarddef:=false;
  677. objectdef,
  678. recorddef:
  679. begin
  680. st:=def.getsymtable(gs_record);
  681. for i:=0 to st.deflist.count-1 do
  682. unset_forwarddef(tdef(st.deflist[i]));
  683. end;
  684. end;
  685. end;
  686. procedure retrieve_genericdef_or_procsym(sym:tsym;out gendef:tdef;out psym:tsym);
  687. var
  688. i : longint;
  689. begin
  690. gendef:=nil;
  691. psym:=nil;
  692. case sym.typ of
  693. typesym:
  694. begin
  695. gendef:=ttypesym(sym).typedef
  696. end;
  697. procsym:
  698. begin
  699. for i:=0 to tprocsym(sym).procdeflist.count-1 do
  700. if tstoreddef(tprocsym(sym).procdeflist[i]).genericdef=genericdef then
  701. begin
  702. gendef:=tdef(tprocsym(sym).procdeflist[i]);
  703. break;
  704. end;
  705. psym:=sym;
  706. end
  707. else
  708. internalerror(200710171);
  709. end;
  710. end;
  711. var
  712. finalspecializename,
  713. ufinalspecializename : tidstring;
  714. prettyname : ansistring;
  715. generictypelist : tfphashobjectlist;
  716. srsymtable,
  717. specializest : tsymtable;
  718. hashedid : thashedidstring;
  719. tempst : tglobalsymtable;
  720. psym,
  721. srsym : tsym;
  722. def : tdef;
  723. old_block_type : tblock_type;
  724. state : tspecializationstate;
  725. old_current_structdef : tabstractrecorddef;
  726. old_current_specializedef,
  727. old_current_genericdef : tstoreddef;
  728. old_current_procinfo : tprocinfo;
  729. old_module_procinfo : tobject;
  730. hmodule : tmodule;
  731. oldcurrent_filepos : tfileposinfo;
  732. recordbuf : tdynamicarray;
  733. hadtypetoken : boolean;
  734. vmtbuilder : tvmtbuilder;
  735. i,
  736. replaydepth : longint;
  737. item : tobject;
  738. allequal,
  739. hintsprocessed : boolean;
  740. pd : tprocdef;
  741. pdflags : tpdflags;
  742. begin
  743. if not assigned(context) then
  744. internalerror(2015052203);
  745. result:=nil;
  746. pd:=nil;
  747. if not check_generic_constraints(genericdef,context.genericdeflist,context.poslist) then
  748. begin
  749. { the parameters didn't fit the constraints, so don't continue with the
  750. specialization }
  751. result:=generrordef;
  752. exit;
  753. end;
  754. { build the new type's name }
  755. finalspecializename:=generate_generic_name(context.genname,context.specializename,genericdef.ownerhierarchyname);
  756. ufinalspecializename:=upper(finalspecializename);
  757. if genericdef.typ=procdef then
  758. prettyname:=tprocdef(genericdef).procsym.prettyname
  759. else
  760. prettyname:=genericdef.typesym.prettyname;
  761. prettyname:=prettyname+'<'+context.prettyname+'>';
  762. generictypelist:=tfphashobjectlist.create(false);
  763. { build the list containing the types for the generic params }
  764. if not assigned(genericdef.genericparas) then
  765. internalerror(2013092601);
  766. if context.genericdeflist.count<>genericdef.genericparas.count then
  767. internalerror(2013092603);
  768. for i:=0 to genericdef.genericparas.Count-1 do
  769. begin
  770. srsym:=tsym(genericdef.genericparas[i]);
  771. if not (sp_generic_para in srsym.symoptions) then
  772. internalerror(2013092602);
  773. generictypelist.add(srsym.realname,tdef(context.genericdeflist[i]).typesym);
  774. end;
  775. { Special case if we are referencing the current defined object }
  776. if assigned(current_structdef) and
  777. (current_structdef.objname^=ufinalspecializename) then
  778. result:=current_structdef;
  779. { Can we reuse an already specialized type? }
  780. { for this first check whether we are currently specializing a nested
  781. type of the current (main) specialization (this is necessary, because
  782. during that time the symbol of the main specialization will still
  783. contain a reference to an errordef) }
  784. if not assigned(result) and assigned(current_specializedef) then
  785. begin
  786. def:=current_specializedef;
  787. repeat
  788. if def.typ in [objectdef,recorddef] then
  789. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  790. result:=def;
  791. break;
  792. end;
  793. if assigned(def.owner) then
  794. def:=tstoreddef(def.owner.defowner)
  795. else
  796. { this can happen when specializing a generic function }
  797. def:=nil;
  798. until not assigned(def) or not (df_specialization in def.defoptions);
  799. end;
  800. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  801. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  802. parsing anyway), so for now we treat those still as generic defs without doing a partial
  803. specialization }
  804. if not assigned(result) then
  805. begin
  806. def:=current_genericdef;
  807. while assigned(def) and (def.typ in [recorddef,objectdef]) do
  808. begin
  809. if (df_generic in def.defoptions) and (def=genericdef) then
  810. begin
  811. result:=def;
  812. break;
  813. end;
  814. { the following happens when a routine with its parent struct
  815. as parameter is specialized as a parameter or result of a
  816. generic function }
  817. if (df_specialization in def.defoptions) and (tstoreddef(def).genericdef=genericdef) then
  818. begin
  819. if tstoreddef(def).genericparas.count=generictypelist.count then
  820. begin
  821. allequal:=true;
  822. for i:=0 to generictypelist.count-1 do
  823. begin
  824. if not equal_defs(ttypesym(generictypelist[i]).typedef,ttypesym(tstoreddef(def).genericparas[i]).typedef) then
  825. begin
  826. allequal:=false;
  827. break;
  828. end;
  829. end;
  830. if allequal then
  831. begin
  832. result:=def;
  833. break;
  834. end;
  835. end;
  836. end;
  837. def:=tstoreddef(def.owner.defowner);
  838. end;
  839. end;
  840. { decide in which symtable to put the specialization }
  841. if parse_generic and not assigned(result) then
  842. begin
  843. srsymtable:=symtablestack.top;
  844. if (srsymtable.symtabletype in [localsymtable,parasymtable]) and tstoreddef(srsymtable.defowner).is_specialization then
  845. { if we are currently specializing a routine we need to specialize into
  846. the routine's local- or parasymtable so that they are correctly
  847. registered should the specialization be finalized }
  848. specializest:=srsymtable
  849. else if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  850. { if we are parsing the definition of a method we specialize into
  851. the local symtable of it }
  852. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  853. else
  854. begin
  855. if not assigned(current_genericdef) then
  856. internalerror(2014050901);
  857. { we specialize the partial specialization into the symtable of the currently parsed
  858. generic }
  859. case current_genericdef.typ of
  860. procvardef:
  861. specializest:=current_genericdef.getsymtable(gs_para);
  862. procdef:
  863. specializest:=current_genericdef.getsymtable(gs_local);
  864. objectdef,
  865. recorddef:
  866. specializest:=current_genericdef.getsymtable(gs_record);
  867. arraydef:
  868. specializest:=tarraydef(current_genericdef).symtable;
  869. else
  870. internalerror(2014050902);
  871. end;
  872. end;
  873. end
  874. else
  875. if current_module.is_unit and current_module.in_interface then
  876. specializest:=current_module.globalsymtable
  877. else
  878. specializest:=current_module.localsymtable;
  879. if not assigned(specializest) then
  880. internalerror(2014050910);
  881. { now check whether there is a specialization somewhere else }
  882. psym:=nil;
  883. if not assigned(result) then
  884. begin
  885. hashedid.id:=ufinalspecializename;
  886. if specializest.symtabletype=objectsymtable then
  887. begin
  888. { search also in parent classes }
  889. if not assigned(current_genericdef) or (current_genericdef.typ<>objectdef) then
  890. internalerror(2016112901);
  891. if not searchsym_in_class(tobjectdef(current_genericdef),tobjectdef(current_genericdef),ufinalspecializename,srsym,srsymtable,[]) then
  892. srsym:=nil;
  893. end
  894. else
  895. srsym:=tsym(specializest.findwithhash(hashedid));
  896. if assigned(srsym) then
  897. begin
  898. retrieve_genericdef_or_procsym(srsym,result,psym);
  899. end
  900. else
  901. { the generic could have been specialized in the globalsymtable
  902. already, so search there as well }
  903. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  904. begin
  905. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  906. if assigned(srsym) then
  907. begin
  908. retrieve_genericdef_or_procsym(srsym,result,psym);
  909. end;
  910. end;
  911. end;
  912. if not assigned(result) then
  913. begin
  914. specialization_init(genericdef,state);
  915. { push a temporary global symtable so that the specialization is
  916. added to the correct symtable; this symtable does not contain
  917. any other symbols, so that the type resolution can not be
  918. influenced by symbols in the current unit }
  919. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  920. symtablestack.push(tempst);
  921. { Reparse the original type definition }
  922. begin
  923. old_current_specializedef:=nil;
  924. old_current_genericdef:=nil;
  925. old_current_structdef:=nil;
  926. old_current_procinfo:=current_procinfo;
  927. old_module_procinfo:=current_module.procinfo;
  928. current_procinfo:=nil;
  929. current_module.procinfo:=nil;
  930. if parse_class_parent then
  931. begin
  932. old_current_structdef:=current_structdef;
  933. old_current_genericdef:=current_genericdef;
  934. old_current_specializedef:=current_specializedef;
  935. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  936. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  937. else
  938. current_structdef:=nil;
  939. current_genericdef:=nil;
  940. current_specializedef:=nil;
  941. end;
  942. maybe_add_waiting_unit(genericdef);
  943. { First a new sym so we can reuse this specialization and
  944. references to this specialization can be handled }
  945. if genericdef.typ=procdef then
  946. if assigned(psym) then
  947. srsym:=psym
  948. else
  949. srsym:=cprocsym.create(finalspecializename)
  950. else
  951. srsym:=ctypesym.create(finalspecializename,generrordef);
  952. { insert the symbol only if we don't know already that we have
  953. a procsym to add it to }
  954. if not assigned(psym) then
  955. specializest.insert(srsym);
  956. { specializations are declarations as such it is the wisest to
  957. declare set the blocktype to "type"; otherwise we'll
  958. experience unexpected side effects like the addition of
  959. classrefdefs if we have a generic that's derived from another
  960. generic }
  961. old_block_type:=block_type;
  962. block_type:=bt_type;
  963. if (
  964. (genericdef.typ=procdef) and
  965. not assigned(tprocdef(genericdef).genericdecltokenbuf)
  966. ) or (
  967. (genericdef.typ<>procdef) and
  968. not assigned(genericdef.generictokenbuf)
  969. ) then
  970. internalerror(200511171);
  971. hmodule:=find_module_from_symtable(genericdef.owner);
  972. if hmodule=nil then
  973. internalerror(2012051202);
  974. oldcurrent_filepos:=current_filepos;
  975. { use the index the module got from the current compilation process }
  976. current_filepos.moduleindex:=hmodule.unit_index;
  977. current_tokenpos:=current_filepos;
  978. if parse_generic then
  979. begin
  980. recordbuf:=current_scanner.recordtokenbuf;
  981. current_scanner.recordtokenbuf:=nil;
  982. end
  983. else
  984. recordbuf:=nil;
  985. replaydepth:=current_scanner.replay_stack_depth;
  986. if genericdef.typ=procdef then
  987. begin
  988. current_scanner.startreplaytokens(tprocdef(genericdef).genericdecltokenbuf,hmodule.change_endian);
  989. parse_proc_head(tprocdef(genericdef).struct,tprocdef(genericdef).proctypeoption,false,genericdef,generictypelist,pd);
  990. if assigned(pd) then
  991. begin
  992. if assigned(psym) then
  993. pd.procsym:=psym
  994. else
  995. pd.procsym:=srsym;
  996. parse_proc_dec_finish(pd,po_classmethod in tprocdef(genericdef).procoptions,tprocdef(genericdef).struct);
  997. end;
  998. result:=pd;
  999. end
  1000. else
  1001. begin
  1002. current_scanner.startreplaytokens(genericdef.generictokenbuf,hmodule.change_endian);
  1003. hadtypetoken:=false;
  1004. read_named_type(result,srsym,genericdef,generictypelist,false,hadtypetoken);
  1005. ttypesym(srsym).typedef:=result;
  1006. result.typesym:=srsym;
  1007. if _prettyname<>'' then
  1008. ttypesym(result.typesym).fprettyname:=_prettyname
  1009. else
  1010. ttypesym(result.typesym).fprettyname:=prettyname;
  1011. end;
  1012. current_filepos:=oldcurrent_filepos;
  1013. { Note regarding hint directives:
  1014. There is no need to remove the flags for them from the
  1015. specialized generic symbol, because hint directives that
  1016. follow the specialization are handled by the code in
  1017. pdecl.types_dec and added to the type symbol.
  1018. E.g.: TFoo = TBar<Blubb> deprecated;
  1019. Here the symbol TBar$1$Blubb will contain the
  1020. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  1021. case result.typ of
  1022. { Build VMT indexes for classes and read hint directives }
  1023. objectdef:
  1024. begin
  1025. if replaydepth>current_scanner.replay_stack_depth then
  1026. begin
  1027. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1028. if replaydepth>current_scanner.replay_stack_depth then
  1029. consume(_SEMICOLON);
  1030. end;
  1031. vmtbuilder:=TVMTBuilder.Create(tobjectdef(result));
  1032. vmtbuilder.generate_vmt;
  1033. vmtbuilder.free;
  1034. end;
  1035. { handle params, calling convention, etc }
  1036. procvardef:
  1037. begin
  1038. hintsprocessed:=false;
  1039. if replaydepth>current_scanner.replay_stack_depth then
  1040. begin
  1041. if not check_proc_directive(true) then
  1042. begin
  1043. hintsprocessed:=try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1044. if replaydepth>current_scanner.replay_stack_depth then
  1045. consume(_SEMICOLON);
  1046. end
  1047. else
  1048. hintsprocessed:=true;
  1049. end;
  1050. if replaydepth>current_scanner.replay_stack_depth then
  1051. parse_var_proc_directives(ttypesym(srsym));
  1052. handle_calling_convention(tprocvardef(result),hcc_default_actions_intf);
  1053. if not hintsprocessed and (replaydepth>current_scanner.replay_stack_depth) then
  1054. begin
  1055. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1056. if replaydepth>current_scanner.replay_stack_depth then
  1057. consume(_SEMICOLON);
  1058. end;
  1059. end;
  1060. procdef:
  1061. begin
  1062. pdflags:=[pd_body,pd_implemen];
  1063. if genericdef.owner.symtabletype=objectsymtable then
  1064. include(pdflags,pd_object)
  1065. else if genericdef.owner.symtabletype=recordsymtable then
  1066. include(pdflags,pd_record);
  1067. parse_proc_directives(pd,pdflags);
  1068. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1069. consume(_SEMICOLON);
  1070. if parse_generic then
  1071. handle_calling_convention(tprocdef(result),hcc_default_actions_intf)
  1072. else
  1073. handle_calling_convention(tprocdef(result),hcc_default_actions_impl);
  1074. proc_add_definition(tprocdef(result));
  1075. { for partial specializations we implicitely declare the routine as
  1076. having its implementation although we'll not specialize it in reality }
  1077. if parse_generic then
  1078. unset_forwarddef(result);
  1079. end;
  1080. else
  1081. { parse hint directives for records and arrays }
  1082. if replaydepth>current_scanner.replay_stack_depth then begin
  1083. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1084. if replaydepth>current_scanner.replay_stack_depth then
  1085. consume(_SEMICOLON);
  1086. end;
  1087. end;
  1088. { Consume the remainder of the buffer }
  1089. while current_scanner.replay_stack_depth>replaydepth do
  1090. consume(token);
  1091. if assigned(recordbuf) then
  1092. begin
  1093. if assigned(current_scanner.recordtokenbuf) then
  1094. internalerror(2014050909);
  1095. current_scanner.recordtokenbuf:=recordbuf;
  1096. end;
  1097. block_type:=old_block_type;
  1098. current_procinfo:=old_current_procinfo;
  1099. current_module.procinfo:=old_module_procinfo;
  1100. if parse_class_parent then
  1101. begin
  1102. current_structdef:=old_current_structdef;
  1103. current_genericdef:=old_current_genericdef;
  1104. current_specializedef:=old_current_specializedef;
  1105. end;
  1106. end;
  1107. { extract all created symbols and defs from the temporary symtable
  1108. and add them to the specializest }
  1109. for i:=tempst.SymList.Count-1 downto 0 do
  1110. begin
  1111. item:=tempst.SymList.Items[i];
  1112. { using changeowner the symbol is automatically added to the
  1113. new symtable }
  1114. tsym(item).ChangeOwner(specializest);
  1115. end;
  1116. for i:=tempst.DefList.Count-1 downto 0 do
  1117. begin
  1118. item:=tempst.DefList.Items[i];
  1119. { using changeowner the def is automatically added to the new
  1120. symtable }
  1121. tdef(item).ChangeOwner(specializest);
  1122. { for partial specializations we implicitely declare any methods as having their
  1123. implementations although we'll not specialize them in reality }
  1124. if parse_generic then
  1125. unset_forwarddef(tdef(item));
  1126. end;
  1127. { if a generic was declared during the specialization we need to
  1128. flag the specialize symtable accordingly }
  1129. if sto_has_generic in tempst.tableoptions then
  1130. specializest.includeoption(sto_has_generic);
  1131. tempst.free;
  1132. specialization_done(state);
  1133. { procdefs are only added once we know which overload we use }
  1134. if not parse_generic and (result.typ<>procdef) then
  1135. current_module.pendingspecializations.add(result.typename,result);
  1136. end;
  1137. generictypelist.free;
  1138. if assigned(genericdef) then
  1139. begin
  1140. { check the hints of the found generic symbol }
  1141. if genericdef.typ=procdef then
  1142. srsym:=tprocdef(genericdef).procsym
  1143. else
  1144. srsym:=genericdef.typesym;
  1145. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1146. end;
  1147. end;
  1148. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);
  1149. var
  1150. context : tspecializationcontext;
  1151. genericdef : tstoreddef;
  1152. begin
  1153. genericdef:=tstoreddef(generate_specialization_phase1(context,tt,parsedtype,symname,parsedpos));
  1154. if genericdef<>generrordef then
  1155. genericdef:=tstoreddef(generate_specialization_phase2(context,genericdef,parse_class_parent,_prettyname));
  1156. tt:=genericdef;
  1157. if assigned(context) then
  1158. context.free;
  1159. end;
  1160. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  1161. var
  1162. generictype : ttypesym;
  1163. i,firstidx : longint;
  1164. srsymtable : tsymtable;
  1165. basedef,def : tdef;
  1166. defname : tidstring;
  1167. allowconstructor,
  1168. doconsume : boolean;
  1169. constraintdata : tgenericconstraintdata;
  1170. old_block_type : tblock_type;
  1171. fileinfo : tfileposinfo;
  1172. begin
  1173. result:=tfphashobjectlist.create(false);
  1174. firstidx:=0;
  1175. old_block_type:=block_type;
  1176. block_type:=bt_type;
  1177. repeat
  1178. if token=_ID then
  1179. begin
  1180. generictype:=ctypesym.create(orgpattern,cundefinedtype);
  1181. { type parameters need to be added as strict private }
  1182. generictype.visibility:=vis_strictprivate;
  1183. include(generictype.symoptions,sp_generic_para);
  1184. result.add(orgpattern,generictype);
  1185. end;
  1186. consume(_ID);
  1187. fileinfo:=current_tokenpos;
  1188. if try_to_consume(_COLON) then
  1189. begin
  1190. if not allowconstraints then
  1191. { TODO }
  1192. Message(parser_e_illegal_expression{ parser_e_generic_constraints_not_allowed_here});
  1193. { construct a name which can be used for a type specification }
  1194. constraintdata:=tgenericconstraintdata.create;
  1195. constraintdata.fileinfo:=fileinfo;
  1196. defname:='';
  1197. str(current_module.deflist.count,defname);
  1198. defname:='$gendef'+defname;
  1199. allowconstructor:=m_delphi in current_settings.modeswitches;
  1200. basedef:=generrordef;
  1201. repeat
  1202. doconsume:=true;
  1203. case token of
  1204. _CONSTRUCTOR:
  1205. begin
  1206. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  1207. Message(parser_e_illegal_expression);
  1208. include(constraintdata.flags,gcf_constructor);
  1209. allowconstructor:=false;
  1210. end;
  1211. _CLASS:
  1212. begin
  1213. if gcf_class in constraintdata.flags then
  1214. Message(parser_e_illegal_expression);
  1215. if basedef=generrordef then
  1216. include(constraintdata.flags,gcf_class)
  1217. else
  1218. Message(parser_e_illegal_expression);
  1219. end;
  1220. _RECORD:
  1221. begin
  1222. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  1223. or (constraintdata.interfaces.count>0) then
  1224. Message(parser_e_illegal_expression)
  1225. else
  1226. begin
  1227. srsymtable:=trecordsymtable.create(defname,0,1,1);
  1228. basedef:=crecorddef.create(defname,srsymtable);
  1229. include(constraintdata.flags,gcf_record);
  1230. allowconstructor:=false;
  1231. end;
  1232. end;
  1233. else
  1234. begin
  1235. { after single_type "token" is the trailing ",", ";" or
  1236. ">"! }
  1237. doconsume:=false;
  1238. { def is already set to a class or record }
  1239. if gcf_record in constraintdata.flags then
  1240. Message(parser_e_illegal_expression);
  1241. single_type(def, [stoAllowSpecialization]);
  1242. { only types that are inheritable are allowed }
  1243. if (def.typ<>objectdef) or
  1244. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  1245. Message1(type_e_class_or_interface_type_expected,def.typename)
  1246. else
  1247. case tobjectdef(def).objecttype of
  1248. odt_class,
  1249. odt_javaclass:
  1250. begin
  1251. if gcf_class in constraintdata.flags then
  1252. { "class" + concrete class is not allowed }
  1253. Message(parser_e_illegal_expression)
  1254. else
  1255. { do we already have a concrete class? }
  1256. if basedef<>generrordef then
  1257. Message(parser_e_illegal_expression)
  1258. else
  1259. basedef:=def;
  1260. end;
  1261. odt_interfacecom,
  1262. odt_interfacecorba,
  1263. odt_interfacejava,
  1264. odt_dispinterface:
  1265. constraintdata.interfaces.add(def);
  1266. end;
  1267. end;
  1268. end;
  1269. if doconsume then
  1270. consume(token);
  1271. until not try_to_consume(_COMMA);
  1272. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  1273. (constraintdata.interfaces.count>1) or
  1274. (
  1275. (basedef.typ=objectdef) and
  1276. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  1277. ) then
  1278. begin
  1279. if basedef.typ=errordef then
  1280. { don't pass an errordef as a parent to a tobjectdef }
  1281. basedef:=class_tobject
  1282. else
  1283. if (basedef.typ<>objectdef) or
  1284. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  1285. internalerror(2012101101);
  1286. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef),false);
  1287. for i:=0 to constraintdata.interfaces.count-1 do
  1288. tobjectdef(basedef).implementedinterfaces.add(
  1289. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  1290. end
  1291. else
  1292. if constraintdata.interfaces.count=1 then
  1293. begin
  1294. if basedef.typ<>errordef then
  1295. internalerror(2013021601);
  1296. def:=tdef(constraintdata.interfaces[0]);
  1297. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def),false);
  1298. constraintdata.interfaces.delete(0);
  1299. end;
  1300. if basedef.typ<>errordef then
  1301. with tstoreddef(basedef) do
  1302. begin
  1303. genconstraintdata:=tgenericconstraintdata.create;
  1304. genconstraintdata.flags:=constraintdata.flags;
  1305. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  1306. genconstraintdata.fileinfo:=constraintdata.fileinfo;
  1307. include(defoptions,df_genconstraint);
  1308. end;
  1309. for i:=firstidx to result.count-1 do
  1310. ttypesym(result[i]).typedef:=basedef;
  1311. { we need a typesym in case we do a Delphi-mode inline
  1312. specialization with this parameter; so just use the first sym }
  1313. if not assigned(basedef.typesym) then
  1314. basedef.typesym:=ttypesym(result[firstidx]);
  1315. firstidx:=result.count;
  1316. constraintdata.free;
  1317. end
  1318. else
  1319. begin
  1320. if token=_SEMICOLON then
  1321. begin
  1322. { two different typeless parameters are considered as incompatible }
  1323. for i:=firstidx to result.count-1 do
  1324. begin
  1325. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1326. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1327. end;
  1328. { a semicolon terminates a type parameter group }
  1329. firstidx:=result.count;
  1330. end;
  1331. end;
  1332. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  1333. { two different typeless parameters are considered as incompatible }
  1334. for i:=firstidx to result.count-1 do
  1335. begin
  1336. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1337. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1338. end;
  1339. block_type:=old_block_type;
  1340. end;
  1341. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1342. var
  1343. i : longint;
  1344. generictype,sym : ttypesym;
  1345. st : tsymtable;
  1346. begin
  1347. def.genericdef:=genericdef;
  1348. if not assigned(genericlist) then
  1349. exit;
  1350. if assigned(genericdef) then
  1351. include(def.defoptions,df_specialization)
  1352. else
  1353. if genericlist.count>0 then
  1354. include(def.defoptions,df_generic);
  1355. case def.typ of
  1356. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  1357. arraydef: st:=tarraydef(def).symtable;
  1358. procvardef,procdef: st:=tabstractprocdef(def).parast;
  1359. else
  1360. internalerror(201101020);
  1361. end;
  1362. if (genericlist.count>0) and not assigned(def.genericparas) then
  1363. def.genericparas:=tfphashobjectlist.create(false);
  1364. for i:=0 to genericlist.count-1 do
  1365. begin
  1366. generictype:=ttypesym(genericlist[i]);
  1367. if assigned(generictype.owner) then
  1368. begin
  1369. sym:=ctypesym.create(genericlist.nameofindex(i),generictype.typedef);
  1370. { type parameters need to be added as strict private }
  1371. sym.visibility:=vis_strictprivate;
  1372. st.insert(sym);
  1373. include(sym.symoptions,sp_generic_para);
  1374. end
  1375. else
  1376. begin
  1377. if (generictype.typedef.typ=undefineddef) and (generictype.typedef<>cundefinedtype) then
  1378. begin
  1379. { the generic parameters were parsed before the genericdef existed thus the
  1380. undefineddefs were added as part of the parent symtable }
  1381. if assigned(generictype.typedef.owner) then
  1382. generictype.typedef.owner.DefList.Extract(generictype.typedef);
  1383. generictype.typedef.changeowner(st);
  1384. end;
  1385. st.insert(generictype);
  1386. include(generictype.symoptions,sp_generic_para);
  1387. end;
  1388. def.genericparas.add(genericlist.nameofindex(i),generictype);
  1389. end;
  1390. end;
  1391. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  1392. var
  1393. gensym : ttypesym;
  1394. begin
  1395. { for generics in non-Delphi modes we insert a private type symbol
  1396. that has the same base name as the currently parsed generic and
  1397. that references this defs }
  1398. if not (m_delphi in current_settings.modeswitches) and
  1399. (
  1400. (
  1401. parse_generic and
  1402. assigned(genericlist) and
  1403. (genericlist.count>0)
  1404. ) or
  1405. (
  1406. assigned(current_specializedef) and
  1407. assigned(current_structdef.genericdef) and
  1408. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  1409. (pos('$',name)>0)
  1410. )
  1411. ) then
  1412. begin
  1413. { we need to pass nil as def here, because the constructor wants
  1414. to set the typesym of the def which is not what we want }
  1415. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil);
  1416. gensym.typedef:=current_structdef;
  1417. include(gensym.symoptions,sp_internal);
  1418. { the symbol should be only visible to the generic class
  1419. itself }
  1420. gensym.visibility:=vis_strictprivate;
  1421. symtablestack.top.insert(gensym);
  1422. end;
  1423. end;
  1424. function generate_generic_name(const name:tidstring;specializename:ansistring;owner_hierarchy:string):tidstring;
  1425. var
  1426. crc : cardinal;
  1427. begin
  1428. if specializename='' then
  1429. internalerror(2012061901);
  1430. { build the new type's name }
  1431. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  1432. result:=name+'$crc'+hexstr(crc,8);
  1433. if owner_hierarchy<>'' then
  1434. begin
  1435. crc:=UpdateCrc32(0,owner_hierarchy[1],length(owner_hierarchy));
  1436. result:=result+'$crc'+hexstr(crc,8);
  1437. end;
  1438. end;
  1439. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  1440. var
  1441. i,code : longint;
  1442. countstr : string;
  1443. begin
  1444. for i:=length(name) downto 1 do
  1445. if name[i]='$' then
  1446. begin
  1447. nongeneric:=copy(name,1,i-1);
  1448. countstr:=copy(name,i+1,length(name)-i);
  1449. val(countstr,count,code);
  1450. if code<>0 then
  1451. break;
  1452. exit;
  1453. end;
  1454. nongeneric:=name;
  1455. count:=0;
  1456. end;
  1457. procedure add_generic_dummysym(sym:tsym);
  1458. var
  1459. list: TFPObjectList;
  1460. srsym : tsym;
  1461. srsymtable : tsymtable;
  1462. entry : tgenericdummyentry;
  1463. begin
  1464. if sp_generic_dummy in sym.symoptions then
  1465. begin
  1466. { did we already search for a generic with that name? }
  1467. list:=tfpobjectlist(current_module.genericdummysyms.find(sym.name));
  1468. if not assigned(list) then
  1469. begin
  1470. list:=tfpobjectlist.create(true);
  1471. current_module.genericdummysyms.add(sym.name,list);
  1472. end;
  1473. { is the dummy sym still "dummy"? }
  1474. if (sym.typ=typesym) and
  1475. (
  1476. { dummy sym defined in mode Delphi }
  1477. (ttypesym(sym).typedef.typ=undefineddef) or
  1478. { dummy sym defined in non-Delphi mode }
  1479. (tstoreddef(ttypesym(sym).typedef).is_generic)
  1480. ) then
  1481. begin
  1482. { do we have a non-generic type of the same name
  1483. available? }
  1484. if not searchsym_with_flags(sym.name,srsym,srsymtable,[ssf_no_addsymref]) then
  1485. srsym:=nil;
  1486. end
  1487. else if (sym.typ=procsym) and
  1488. (tprocsym(sym).procdeflist.count>0) then
  1489. srsym:=sym
  1490. else
  1491. { dummy symbol is already not so dummy anymore }
  1492. srsym:=nil;
  1493. if assigned(srsym) then
  1494. begin
  1495. entry:=tgenericdummyentry.create;
  1496. entry.resolvedsym:=srsym;
  1497. entry.dummysym:=sym;
  1498. list.add(entry);
  1499. end;
  1500. end;
  1501. end;
  1502. function resolve_generic_dummysym(const name:tidstring):tsym;
  1503. var
  1504. list : tfpobjectlist;
  1505. begin
  1506. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  1507. if assigned(list) and (list.count>0) then
  1508. result:=tgenericdummyentry(list.last).resolvedsym
  1509. else
  1510. result:=nil;
  1511. end;
  1512. function could_be_generic(const name:tidstring):boolean;
  1513. begin
  1514. result:=(name<>'') and
  1515. (current_module.genericdummysyms.findindexof(name)>=0);
  1516. end;
  1517. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  1518. var
  1519. pu : tused_unit;
  1520. hmodule : tmodule;
  1521. unitsyms : TFPHashObjectList;
  1522. sym : tsym;
  1523. i : Integer;
  1524. begin
  1525. if not assigned(genericdef) then
  1526. internalerror(200705151);
  1527. { Setup symtablestack at definition time
  1528. to get types right, however this is not perfect, we should probably record
  1529. the resolved symbols }
  1530. state.oldsymtablestack:=symtablestack;
  1531. state.oldextendeddefs:=current_module.extendeddefs;
  1532. state.oldgenericdummysyms:=current_module.genericdummysyms;
  1533. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1534. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  1535. symtablestack:=tdefawaresymtablestack.create;
  1536. hmodule:=find_module_from_symtable(genericdef.owner);
  1537. if hmodule=nil then
  1538. internalerror(200705152);
  1539. { collect all unit syms in the generic's unit as we need to establish
  1540. their unitsym.module link again so that unit identifiers can be used }
  1541. unitsyms:=tfphashobjectlist.create(false);
  1542. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1543. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1544. begin
  1545. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1546. if sym.typ=unitsym then
  1547. unitsyms.add(upper(sym.realname),sym);
  1548. end;
  1549. { add all units if we are specializing inside the current unit (as the
  1550. generic could have been declared in the implementation part), but load
  1551. only interface units, if we are in a different unit as then the generic
  1552. needs to be in the interface section }
  1553. pu:=tused_unit(hmodule.used_units.first);
  1554. while assigned(pu) do
  1555. begin
  1556. if not assigned(pu.u.globalsymtable) then
  1557. { in certain circular, but valid unit constellations it can happen
  1558. that we specialize a generic in a different unit that was used
  1559. in the implementation section of the generic's unit and were the
  1560. interface is still being parsed and thus the localsymtable is in
  1561. reality the global symtable }
  1562. if pu.u.in_interface then
  1563. symtablestack.push(pu.u.localsymtable)
  1564. else
  1565. internalerror(200705153)
  1566. else
  1567. symtablestack.push(pu.u.globalsymtable);
  1568. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1569. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1570. tunitsym(sym).module:=pu.u;
  1571. pu:=tused_unit(pu.next);
  1572. end;
  1573. unitsyms.free;
  1574. if assigned(hmodule.globalsymtable) then
  1575. symtablestack.push(hmodule.globalsymtable);
  1576. { push the localsymtable if needed }
  1577. if ((hmodule<>current_module) or not current_module.in_interface)
  1578. and assigned(hmodule.localsymtable) then
  1579. symtablestack.push(hmodule.localsymtable);
  1580. end;
  1581. procedure specialization_done(var state: tspecializationstate);
  1582. begin
  1583. { Restore symtablestack }
  1584. current_module.extendeddefs.free;
  1585. current_module.extendeddefs:=state.oldextendeddefs;
  1586. current_module.genericdummysyms.free;
  1587. current_module.genericdummysyms:=state.oldgenericdummysyms;
  1588. symtablestack.free;
  1589. symtablestack:=state.oldsymtablestack;
  1590. { clear the state record to be on the safe side }
  1591. fillchar(state, sizeof(state), 0);
  1592. end;
  1593. {****************************************************************************
  1594. SPECIALIZATION BODY GENERATION
  1595. ****************************************************************************}
  1596. procedure process_procdef(def:tprocdef;hmodule:tmodule);
  1597. var
  1598. oldcurrent_filepos : tfileposinfo;
  1599. begin
  1600. if assigned(def.genericdef) and
  1601. (def.genericdef.typ=procdef) and
  1602. assigned(tprocdef(def.genericdef).generictokenbuf) then
  1603. begin
  1604. if not assigned(tprocdef(def.genericdef).generictokenbuf) then
  1605. internalerror(2015061902);
  1606. oldcurrent_filepos:=current_filepos;
  1607. current_filepos:=tprocdef(def.genericdef).fileinfo;
  1608. { use the index the module got from the current compilation process }
  1609. current_filepos.moduleindex:=hmodule.unit_index;
  1610. current_tokenpos:=current_filepos;
  1611. current_scanner.startreplaytokens(tprocdef(def.genericdef).generictokenbuf,hmodule.change_endian);
  1612. read_proc_body(def);
  1613. current_filepos:=oldcurrent_filepos;
  1614. end
  1615. { synthetic routines will be implemented afterwards }
  1616. else if def.synthetickind=tsk_none then
  1617. MessagePos1(def.fileinfo,sym_e_forward_not_resolved,def.fullprocname(false));
  1618. end;
  1619. function process_abstractrecorddef(def:tabstractrecorddef):boolean;
  1620. var
  1621. i : longint;
  1622. hp : tdef;
  1623. hmodule : tmodule;
  1624. begin
  1625. result:=true;
  1626. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1627. if hmodule=nil then
  1628. internalerror(201202041);
  1629. for i:=0 to def.symtable.DefList.Count-1 do
  1630. begin
  1631. hp:=tdef(def.symtable.DefList[i]);
  1632. if hp.typ=procdef then
  1633. begin
  1634. { only generate the code if we need a body }
  1635. if assigned(tprocdef(hp).struct) and not tprocdef(hp).forwarddef then
  1636. continue;
  1637. { and the body is available already (which is implicitely the
  1638. case if the generic routine is part of another unit) }
  1639. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and
  1640. { may not be assigned in case it's a synthetic procdef that
  1641. still needs to be generated }
  1642. assigned(tprocdef(hp).genericdef) and
  1643. tprocdef(tprocdef(hp).genericdef).forwarddef then
  1644. begin
  1645. result:=false;
  1646. continue;
  1647. end;
  1648. process_procdef(tprocdef(hp),hmodule);
  1649. end
  1650. else
  1651. if hp.typ in [objectdef,recorddef] then
  1652. { generate code for subtypes as well }
  1653. result:=process_abstractrecorddef(tabstractrecorddef(hp)) and result;
  1654. end;
  1655. end;
  1656. procedure generate_specialization_procs;
  1657. var
  1658. i : longint;
  1659. list,
  1660. readdlist : tfpobjectlist;
  1661. def : tstoreddef;
  1662. state : tspecializationstate;
  1663. hmodule : tmodule;
  1664. begin
  1665. { first copy all entries and then work with that list to ensure that
  1666. we don't get an infinite recursion }
  1667. list:=tfpobjectlist.create(false);
  1668. readdlist:=tfpobjectlist.create(false);
  1669. for i:=0 to current_module.pendingspecializations.Count-1 do
  1670. list.add(current_module.pendingspecializations.Items[i]);
  1671. current_module.pendingspecializations.clear;
  1672. for i:=0 to list.count-1 do
  1673. begin
  1674. def:=tstoreddef(list[i]);
  1675. if not tstoreddef(def).is_specialization then
  1676. continue;
  1677. case def.typ of
  1678. procdef:
  1679. begin
  1680. { the use of forwarddef should not backfire as the
  1681. specialization always belongs to the current module }
  1682. if not tprocdef(def).forwarddef then
  1683. continue;
  1684. if not assigned(def.genericdef) then
  1685. internalerror(2015061903);
  1686. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1687. if hmodule=nil then
  1688. internalerror(2015061904);
  1689. { we need to check for a forward declaration only if the
  1690. generic was declared in the same unit (otherwise there
  1691. should be one) }
  1692. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and tprocdef(def.genericdef).forwarddef then
  1693. begin
  1694. readdlist.add(def);
  1695. continue;
  1696. end;
  1697. specialization_init(tstoreddef(def).genericdef,state);
  1698. process_procdef(tprocdef(def),hmodule);
  1699. specialization_done(state);
  1700. end;
  1701. recorddef,
  1702. objectdef:
  1703. begin
  1704. specialization_init(tstoreddef(def).genericdef,state);
  1705. if not process_abstractrecorddef(tabstractrecorddef(def)) then
  1706. readdlist.add(def);
  1707. specialization_done(state);
  1708. end;
  1709. end;
  1710. end;
  1711. { add those defs back to the pending list for which we don't yet have
  1712. all method bodies }
  1713. for i:=0 to readdlist.count-1 do
  1714. current_module.pendingspecializations.add(tstoreddef(readdlist[i]).typename,readdlist[i]);
  1715. readdlist.free;
  1716. list.free;
  1717. end;
  1718. procedure maybe_add_pending_specialization(def:tdef);
  1719. var
  1720. hmodule : tmodule;
  1721. st : tsymtable;
  1722. begin
  1723. if parse_generic then
  1724. exit;
  1725. st:=def.owner;
  1726. while st.symtabletype in [localsymtable] do
  1727. st:=st.defowner.owner;
  1728. hmodule:=find_module_from_symtable(st);
  1729. if tstoreddef(def).is_specialization and (hmodule=current_module) then
  1730. current_module.pendingspecializations.add(def.typename,def);
  1731. end;
  1732. end.