pgenutil.pas 71 KB

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