pgenutil.pas 73 KB

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