pgenutil.pas 75 KB

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