pgenutil.pas 73 KB

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