pgenutil.pas 71 KB

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