pgenutil.pas 56 KB

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