pgenutil.pas 72 KB

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