pgenutil.pas 59 KB

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