pgenutil.pas 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. { symtable }
  27. symtype,symdef,symbase;
  28. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);
  29. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);
  30. function parse_generic_parameters(allowconstraints:boolean):TFPObjectList;
  31. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  32. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  33. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfpobjectlist);
  34. function generate_generic_name(const name:tidstring;specializename:ansistring):tidstring;
  35. type
  36. tspecializationstate = record
  37. oldsymtablestack : tsymtablestack;
  38. oldextendeddefs : TFPHashObjectList;
  39. end;
  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,
  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. paradef : tstoreddef;
  87. objdef,
  88. paraobjdef,
  89. formalobjdef : tobjectdef;
  90. generictype : ttypesym;
  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 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. generictype:=ttypesym(genericdef.genericparas[i]);
  106. filepos:=pfileposinfo(poslist[i])^;
  107. if not assigned(generictype.genconstraintdata) then
  108. { the parameter is of unspecified type, so no need to check }
  109. continue;
  110. paradef:=tstoreddef(paradeflist[i]);
  111. { undefineddef is compatible with anything }
  112. if generictype.typedef.typ=undefineddef then
  113. continue;
  114. if paradef.typ<>generictype.typedef.typ then
  115. begin
  116. case generictype.typedef.typ of
  117. recorddef:
  118. MessagePos(filepos,type_e_record_type_expected);
  119. objectdef:
  120. case tobjectdef(generictype.typedef).objecttype of
  121. odt_class,
  122. odt_javaclass:
  123. MessagePos1(filepos,type_e_class_type_expected,paradef.typename);
  124. odt_interfacecom,
  125. odt_interfacecorba,
  126. odt_dispinterface,
  127. odt_interfacejava:
  128. MessagePos1(filepos,type_e_interface_type_expected,paradef.typename);
  129. else
  130. internalerror(2012101003);
  131. end;
  132. errordef:
  133. { ignore }
  134. ;
  135. else
  136. internalerror(2012101004);
  137. end;
  138. result:=false;
  139. end
  140. else
  141. begin
  142. { the paradef types are the same, so do special checks for the
  143. cases in which they are needed }
  144. if generictype.typedef.typ=objectdef then
  145. begin
  146. paraobjdef:=tobjectdef(paradef);
  147. formalobjdef:=tobjectdef(generictype.typedef);
  148. if not (formalobjdef.objecttype in [odt_class,odt_javaclass,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface]) then
  149. internalerror(2012101102);
  150. if formalobjdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface] then
  151. begin
  152. { this is either a concerete interface or class type (the
  153. latter without specific implemented interfaces) }
  154. case paraobjdef.objecttype of
  155. odt_interfacecom,
  156. odt_interfacecorba,
  157. odt_interfacejava,
  158. odt_dispinterface:
  159. if not paraobjdef.is_related(formalobjdef) then
  160. begin
  161. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.typename);
  162. result:=false;
  163. end;
  164. odt_class,
  165. odt_javaclass:
  166. begin
  167. objdef:=paraobjdef;
  168. intffound:=false;
  169. while assigned(objdef) do
  170. begin
  171. for j:=0 to objdef.implementedinterfaces.count-1 do
  172. if timplementedinterface(objdef.implementedinterfaces[j]).intfdef=formalobjdef then
  173. begin
  174. intffound:=true;
  175. break;
  176. end;
  177. if intffound then
  178. break;
  179. objdef:=objdef.childof;
  180. end;
  181. result:=intffound;
  182. if not result then
  183. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,formalobjdef.typename);
  184. end;
  185. else
  186. begin
  187. MessagePos1(filepos,type_e_class_or_interface_type_expected,paraobjdef.typename);
  188. result:=false;
  189. end;
  190. end;
  191. end
  192. else
  193. if df_genconstraint in formalobjdef.defoptions then
  194. begin
  195. { this is either a "class" or a concrete instance
  196. which shall implement interfaces }
  197. if not (paraobjdef.objecttype in [odt_class,odt_javaclass]) then
  198. begin
  199. MessagePos1(filepos,type_e_class_type_expected,paraobjdef.typename);
  200. result:=false;
  201. continue;
  202. end;
  203. if assigned(formalobjdef.childof) and
  204. not paradef.is_related(formalobjdef.childof) then
  205. begin
  206. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  207. result:=false;
  208. end;
  209. intfcount:=0;
  210. for j:=0 to formalobjdef.implementedinterfaces.count-1 do
  211. begin
  212. objdef:=paraobjdef;
  213. while assigned(objdef) do
  214. begin
  215. intffound:=assigned(
  216. objdef.find_implemented_interface(
  217. timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef
  218. )
  219. );
  220. if intffound then
  221. break;
  222. objdef:=objdef.childof;
  223. end;
  224. if intffound then
  225. inc(intfcount)
  226. else
  227. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef.typename);
  228. end;
  229. if intfcount<>formalobjdef.implementedinterfaces.count then
  230. result:=false;
  231. end
  232. else
  233. if not paraobjdef.is_related(formalobjdef) then
  234. begin
  235. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.typename);
  236. result:=false;
  237. end;
  238. end;
  239. end;
  240. end;
  241. end;
  242. function parse_generic_specialization_types_internal(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring;parsedtype:tdef;parsedpos:tfileposinfo):boolean;
  243. var
  244. old_block_type : tblock_type;
  245. first : boolean;
  246. typeparam : tnode;
  247. parampos : pfileposinfo;
  248. tmpparampos : tfileposinfo;
  249. begin
  250. result:=true;
  251. if genericdeflist=nil then
  252. internalerror(2012061401);
  253. { set the block type to type, so that the parsed type are returned as
  254. ttypenode (e.g. classes are in non type-compatible blocks returned as
  255. tloadvmtaddrnode) }
  256. old_block_type:=block_type;
  257. { if parsedtype is set, then the first type identifer was already parsed
  258. (happens in inline specializations) and thus we only need to parse
  259. the remaining types and do as if the first one was already given }
  260. first:=not assigned(parsedtype);
  261. if assigned(parsedtype) then
  262. begin
  263. genericdeflist.Add(parsedtype);
  264. specializename:='$'+parsedtype.typename;
  265. prettyname:=parsedtype.typesym.prettyname;
  266. if assigned(poslist) then
  267. begin
  268. New(parampos);
  269. parampos^:=parsedpos;
  270. poslist.add(parampos);
  271. end;
  272. end
  273. else
  274. begin
  275. specializename:='';
  276. prettyname:='';
  277. end;
  278. while not (token in [_GT,_RSHARPBRACKET]) do
  279. begin
  280. { "first" is set to false at the end of the loop! }
  281. if not first then
  282. consume(_COMMA);
  283. block_type:=bt_type;
  284. tmpparampos:=current_filepos;
  285. typeparam:=factor(false,true);
  286. if typeparam.nodetype=typen then
  287. begin
  288. if df_generic in typeparam.resultdef.defoptions then
  289. Message(parser_e_no_generics_as_params);
  290. if assigned(poslist) then
  291. begin
  292. New(parampos);
  293. parampos^:=tmpparampos;
  294. poslist.add(parampos);
  295. end;
  296. genericdeflist.Add(typeparam.resultdef);
  297. if not assigned(typeparam.resultdef.typesym) then
  298. message(type_e_generics_cannot_reference_itself)
  299. else
  300. begin
  301. specializename:=specializename+'$'+typeparam.resultdef.typename;
  302. if first then
  303. prettyname:=prettyname+typeparam.resultdef.typesym.prettyname
  304. else
  305. prettyname:=prettyname+','+typeparam.resultdef.typesym.prettyname;
  306. end;
  307. end
  308. else
  309. begin
  310. Message(type_e_type_id_expected);
  311. result:=false;
  312. end;
  313. typeparam.free;
  314. first:=false;
  315. end;
  316. block_type:=old_block_type;
  317. end;
  318. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  319. var
  320. dummypos : tfileposinfo;
  321. begin
  322. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  323. result:=parse_generic_specialization_types_internal(genericdeflist,poslist,prettyname,specializename,nil,dummypos);
  324. end;
  325. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);
  326. var
  327. dummypos : tfileposinfo;
  328. begin
  329. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  330. generate_specialization(tt,parse_class_parent,_prettyname,nil,'',dummypos);
  331. end;
  332. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);
  333. var
  334. st : TSymtable;
  335. srsym : tsym;
  336. pt2 : tnode;
  337. found,
  338. first,
  339. err : boolean;
  340. errval,
  341. i,
  342. gencount : longint;
  343. genericdef,def : tstoreddef;
  344. generictype : ttypesym;
  345. genericdeflist : TFPObjectList;
  346. generictypelist : TFPObjectList;
  347. prettyname,specializename : ansistring;
  348. ufinalspecializename,
  349. countstr,genname,ugenname,finalspecializename : string;
  350. vmtbuilder : TVMTBuilder;
  351. specializest : tsymtable;
  352. item : tobject;
  353. old_current_structdef : tabstractrecorddef;
  354. old_current_genericdef,old_current_specializedef : tstoreddef;
  355. tempst : tglobalsymtable;
  356. old_block_type: tblock_type;
  357. hashedid: thashedidstring;
  358. state : tspecializationstate;
  359. hmodule : tmodule;
  360. oldcurrent_filepos : tfileposinfo;
  361. poslist : tfplist;
  362. begin
  363. { retrieve generic def that we are going to replace }
  364. genericdef:=tstoreddef(tt);
  365. tt:=nil;
  366. { either symname must be given or genericdef needs to be valid }
  367. if (symname='') and
  368. (not assigned(genericdef) or
  369. not assigned(genericdef.typesym) or
  370. (genericdef.typesym.typ<>typesym)) then
  371. internalerror(2011042701);
  372. { Only parse the parameters for recovery or
  373. for recording in genericbuf }
  374. if parse_generic then
  375. begin
  376. first:=assigned(parsedtype);
  377. if not first and not try_to_consume(_LT) then
  378. consume(_LSHARPBRACKET);
  379. gencount:=0;
  380. repeat
  381. if not first then
  382. begin
  383. pt2:=factor(false,true);
  384. pt2.free;
  385. end;
  386. first:=false;
  387. inc(gencount);
  388. until not try_to_consume(_COMMA);
  389. if not try_to_consume(_GT) then
  390. consume(_RSHARPBRACKET);
  391. { we need to return a def that can later pass some checks like
  392. whether it's an interface or not }
  393. if not assigned(tt) or (tt.typ=undefineddef) then
  394. begin
  395. if (symname='') and (df_generic in genericdef.defoptions) then
  396. { this happens in non-Delphi modes }
  397. tt:=genericdef
  398. else
  399. begin
  400. { find the corresponding generic symbol so that any checks
  401. done on the returned def will be handled correctly }
  402. str(gencount,countstr);
  403. if symname='' then
  404. genname:=ttypesym(genericdef.typesym).realname
  405. else
  406. genname:=symname;
  407. genname:=genname+'$'+countstr;
  408. ugenname:=upper(genname);
  409. { first check whether the found name is the same as that of
  410. the current def or one of its (generic) surrounding defs;
  411. this is necessary as the symbol of the generic can not yet
  412. be used for lookup as it still contains a reference to an
  413. errordef) }
  414. def:=current_genericdef;
  415. repeat
  416. if def.typ in [objectdef,recorddef] then
  417. if tabstractrecorddef(def).objname^=ugenname then
  418. begin
  419. tt:=def;
  420. break;
  421. end;
  422. def:=tstoreddef(def.owner.defowner);
  423. until not assigned(def) or not (df_generic in def.defoptions);
  424. { it's not part of the current object hierarchy, so search
  425. for the symbol }
  426. if not assigned(tt) then
  427. begin
  428. if not searchsym(ugenname,srsym,st) or
  429. (srsym.typ<>typesym) then
  430. begin
  431. identifier_not_found(genname);
  432. tt:=generrordef;
  433. exit;
  434. end;
  435. tt:=ttypesym(srsym).typedef;
  436. { this happens in non-Delphi modes if we encounter a
  437. specialization of the generic class or record we're
  438. currently parsing }
  439. if (tt.typ=errordef) and assigned(current_structdef) and
  440. (current_structdef.objname^=ugenname) then
  441. tt:=current_structdef;
  442. end;
  443. end;
  444. end;
  445. exit;
  446. end;
  447. if not assigned(parsedtype) and not try_to_consume(_LT) then
  448. consume(_LSHARPBRACKET);
  449. genericdeflist:=TFPObjectList.Create(false);
  450. poslist:=tfplist.create;
  451. { Parse type parameters }
  452. err:=not parse_generic_specialization_types_internal(genericdeflist,poslist,prettyname,specializename,parsedtype,parsedpos);
  453. if err then
  454. begin
  455. if not try_to_consume(_GT) then
  456. try_to_consume(_RSHARPBRACKET);
  457. genericdeflist.free;
  458. for i:=0 to poslist.count-1 do
  459. dispose(pfileposinfo(poslist[i]));
  460. poslist.free;
  461. tt:=generrordef;
  462. exit;
  463. end;
  464. { use the name of the symbol as procvars return a user friendly version
  465. of the name }
  466. if symname='' then
  467. genname:=ttypesym(genericdef.typesym).realname
  468. else
  469. genname:=symname;
  470. { in case of non-Delphi mode the type name could already be a generic
  471. def (but maybe the wrong one) }
  472. if assigned(genericdef) and
  473. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  474. begin
  475. { remove the type count suffix from the generic's name }
  476. for i:=Length(genname) downto 1 do
  477. if genname[i]='$' then
  478. begin
  479. genname:=copy(genname,1,i-1);
  480. break;
  481. end;
  482. { in case of a specialization we've only reached the specialization
  483. checksum yet }
  484. if df_specialization in genericdef.defoptions then
  485. for i:=length(genname) downto 1 do
  486. if genname[i]='$' then
  487. begin
  488. genname:=copy(genname,1,i-1);
  489. break;
  490. end;
  491. end
  492. else
  493. { search for a potential suffix }
  494. for i:=length(genname) downto 1 do
  495. if genname[i]='$' then
  496. begin
  497. { if the part right of the $ is a number we assume that the left
  498. part is the name of the generic, otherwise we assume that the
  499. complete name is the name of the generic }
  500. countstr:=copy(genname,i+1,length(genname)-i);
  501. gencount:=0;
  502. val(countstr,gencount,errval);
  503. if errval=0 then
  504. genname:=copy(genname,1,i-1);
  505. break;
  506. end;
  507. { search a generic with the given count of params }
  508. countstr:='';
  509. str(genericdeflist.Count,countstr);
  510. genname:=genname+'$'+countstr;
  511. ugenname:=upper(genname);
  512. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  513. begin
  514. if genericdef.owner.symtabletype = objectsymtable then
  515. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,srsym,st,false)
  516. else
  517. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,srsym,st);
  518. end
  519. else
  520. found:=searchsym(ugenname,srsym,st);
  521. if not found or (srsym.typ<>typesym) then
  522. begin
  523. identifier_not_found(genname);
  524. if not try_to_consume(_GT) then
  525. try_to_consume(_RSHARPBRACKET);
  526. for i:=0 to poslist.count-1 do
  527. dispose(pfileposinfo(poslist[i]));
  528. poslist.free;
  529. genericdeflist.Free;
  530. tt:=generrordef;
  531. exit;
  532. end;
  533. { we've found the correct def }
  534. genericdef:=tstoreddef(ttypesym(srsym).typedef);
  535. if not check_generic_constraints(genericdef,genericdeflist,poslist) then
  536. begin
  537. { the parameters didn't fit the constraints, so don't continue with the
  538. specialization }
  539. genericdeflist.free;
  540. for i:=0 to poslist.count-1 do
  541. dispose(pfileposinfo(poslist[i]));
  542. poslist.free;
  543. tt:=generrordef;
  544. if not try_to_consume(_GT) then
  545. try_to_consume(_RSHARPBRACKET);
  546. exit;
  547. end;
  548. { build the new type's name }
  549. finalspecializename:=generate_generic_name(genname,specializename);
  550. ufinalspecializename:=upper(finalspecializename);
  551. prettyname:=genericdef.typesym.prettyname+'<'+prettyname+'>';
  552. { select the symtable containing the params }
  553. case genericdef.typ of
  554. procdef:
  555. st:=genericdef.GetSymtable(gs_para);
  556. objectdef,
  557. recorddef:
  558. st:=genericdef.GetSymtable(gs_record);
  559. arraydef:
  560. st:=tarraydef(genericdef).symtable;
  561. procvardef:
  562. st:=genericdef.GetSymtable(gs_para);
  563. else
  564. internalerror(200511182);
  565. end;
  566. generictypelist:=tfpobjectlist.create(false);
  567. { build the list containing the types for the generic params }
  568. gencount:=0;
  569. for i:=0 to st.SymList.Count-1 do
  570. begin
  571. srsym:=tsym(st.SymList[i]);
  572. if sp_generic_para in srsym.symoptions then
  573. begin
  574. if gencount=genericdeflist.Count then
  575. internalerror(2011042702);
  576. generictype:=ttypesym.create(srsym.realname,tdef(genericdeflist[gencount]));
  577. generictypelist.add(generictype);
  578. inc(gencount);
  579. end;
  580. end;
  581. { Special case if we are referencing the current defined object }
  582. if assigned(current_structdef) and
  583. (current_structdef.objname^=ufinalspecializename) then
  584. tt:=current_structdef;
  585. { decide in which symtable to put the specialization }
  586. if current_module.is_unit and current_module.in_interface then
  587. specializest:=current_module.globalsymtable
  588. else
  589. specializest:=current_module.localsymtable;
  590. { Can we reuse an already specialized type? }
  591. { for this first check whether we are currently specializing a nested
  592. type of the current (main) specialization (this is necessary, because
  593. during that time the symbol of the main specialization will still
  594. contain a reference to an errordef) }
  595. if not assigned(tt) and assigned(current_specializedef) then
  596. begin
  597. def:=current_specializedef;
  598. repeat
  599. if def.typ in [objectdef,recorddef] then
  600. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  601. tt:=def;
  602. break;
  603. end;
  604. def:=tstoreddef(def.owner.defowner);
  605. until not assigned(def) or not (df_specialization in def.defoptions);
  606. end;
  607. { now check whether there is a specialization somewhere else }
  608. if not assigned(tt) then
  609. begin
  610. hashedid.id:=ufinalspecializename;
  611. srsym:=tsym(specializest.findwithhash(hashedid));
  612. if assigned(srsym) then
  613. begin
  614. if srsym.typ<>typesym then
  615. internalerror(200710171);
  616. tt:=ttypesym(srsym).typedef;
  617. end
  618. else
  619. { the generic could have been specialized in the globalsymtable
  620. already, so search there as well }
  621. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  622. begin
  623. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  624. if assigned(srsym) then
  625. begin
  626. if srsym.typ<>typesym then
  627. internalerror(2011121101);
  628. tt:=ttypesym(srsym).typedef;
  629. end;
  630. end;
  631. end;
  632. if not assigned(tt) then
  633. begin
  634. specialization_init(genericdef,state);
  635. { push a temporary global symtable so that the specialization is
  636. added to the correct symtable; this symtable does not contain
  637. any other symbols, so that the type resolution can not be
  638. influenced by symbols in the current unit }
  639. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  640. symtablestack.push(tempst);
  641. { Reparse the original type definition }
  642. if not err then
  643. begin
  644. if parse_class_parent then
  645. begin
  646. old_current_structdef:=current_structdef;
  647. old_current_genericdef:=current_genericdef;
  648. old_current_specializedef:=current_specializedef;
  649. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  650. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  651. else
  652. current_structdef:=nil;
  653. current_genericdef:=nil;
  654. current_specializedef:=nil;
  655. end;
  656. maybe_add_waiting_unit(genericdef);
  657. { First a new typesym so we can reuse this specialization and
  658. references to this specialization can be handled }
  659. srsym:=ttypesym.create(finalspecializename,generrordef);
  660. specializest.insert(srsym);
  661. { specializations are declarations as such it is the wisest to
  662. declare set the blocktype to "type"; otherwise we'll
  663. experience unexpected side effects like the addition of
  664. classrefdefs if we have a generic that's derived from another
  665. generic }
  666. old_block_type:=block_type;
  667. block_type:=bt_type;
  668. if not assigned(genericdef.generictokenbuf) then
  669. internalerror(200511171);
  670. hmodule:=find_module_from_symtable(genericdef.owner);
  671. if hmodule=nil then
  672. internalerror(2012051202);
  673. oldcurrent_filepos:=current_filepos;
  674. { use the index the module got from the current compilation process }
  675. current_filepos.moduleindex:=hmodule.unit_index;
  676. current_tokenpos:=current_filepos;
  677. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  678. read_named_type(tt,srsym,genericdef,generictypelist,false);
  679. current_filepos:=oldcurrent_filepos;
  680. ttypesym(srsym).typedef:=tt;
  681. tt.typesym:=srsym;
  682. if _prettyname<>'' then
  683. ttypesym(tt.typesym).fprettyname:=_prettyname
  684. else
  685. ttypesym(tt.typesym).fprettyname:=prettyname;
  686. { Note regarding hint directives:
  687. There is no need to remove the flags for them from the
  688. specialized generic symbol, because hint directives that
  689. follow the specialization are handled by the code in
  690. pdecl.types_dec and added to the type symbol.
  691. E.g.: TFoo = TBar<Blubb> deprecated;
  692. Here the symbol TBar$1$Blubb will contain the
  693. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  694. case tt.typ of
  695. { Build VMT indexes for classes and read hint directives }
  696. objectdef:
  697. begin
  698. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  699. consume(_SEMICOLON);
  700. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  701. vmtbuilder.generate_vmt;
  702. vmtbuilder.free;
  703. end;
  704. { handle params, calling convention, etc }
  705. procvardef:
  706. begin
  707. if not check_proc_directive(true) then
  708. begin
  709. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  710. consume(_SEMICOLON);
  711. end;
  712. parse_var_proc_directives(ttypesym(srsym));
  713. handle_calling_convention(tprocvardef(tt));
  714. if try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg) then
  715. consume(_SEMICOLON);
  716. end;
  717. else
  718. { parse hint directives for records and arrays }
  719. begin
  720. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  721. consume(_SEMICOLON);
  722. end;
  723. end;
  724. { Consume the semicolon if it is also recorded }
  725. try_to_consume(_SEMICOLON);
  726. block_type:=old_block_type;
  727. if parse_class_parent then
  728. begin
  729. current_structdef:=old_current_structdef;
  730. current_genericdef:=old_current_genericdef;
  731. current_specializedef:=old_current_specializedef;
  732. end;
  733. end;
  734. { extract all created symbols and defs from the temporary symtable
  735. and add them to the specializest }
  736. for i:=tempst.SymList.Count-1 downto 0 do
  737. begin
  738. item:=tempst.SymList.Items[i];
  739. { using changeowner the symbol is automatically added to the
  740. new symtable }
  741. tsym(item).ChangeOwner(specializest);
  742. end;
  743. for i:=tempst.DefList.Count-1 downto 0 do
  744. begin
  745. item:=tempst.DefList.Items[i];
  746. { using changeowner the def is automatically added to the new
  747. symtable }
  748. tdef(item).ChangeOwner(specializest);
  749. end;
  750. { if a generic was declared during the specialization we need to
  751. flag the specialize symtable accordingly }
  752. if sto_has_generic in tempst.tableoptions then
  753. specializest.includeoption(sto_has_generic);
  754. tempst.free;
  755. specialization_done(state);
  756. end;
  757. if not (token in [_GT, _RSHARPBRACKET]) then
  758. begin
  759. consume(_RSHARPBRACKET);
  760. exit;
  761. end
  762. else
  763. consume(token);
  764. genericdeflist.free;
  765. generictypelist.free;
  766. if assigned(genericdef) then
  767. begin
  768. { check the hints of the found generic symbol }
  769. srsym:=genericdef.typesym;
  770. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  771. end;
  772. end;
  773. function parse_generic_parameters(allowconstraints:boolean):TFPObjectList;
  774. var
  775. generictype : ttypesym;
  776. i,firstidx : longint;
  777. srsymtable : tsymtable;
  778. def : tdef;
  779. defname : tidstring;
  780. allowconstructor,
  781. doconsume : boolean;
  782. constraintdata : tgenericconstraintdata;
  783. begin
  784. result:=TFPObjectList.Create(false);
  785. firstidx:=0;
  786. repeat
  787. if token=_ID then
  788. begin
  789. generictype:=ttypesym.create(orgpattern,cundefinedtype);
  790. include(generictype.symoptions,sp_generic_para);
  791. result.add(generictype);
  792. end;
  793. consume(_ID);
  794. if try_to_consume(_COLON) then
  795. begin
  796. if not allowconstraints then
  797. { TODO }
  798. Message(parser_e_illegal_expression{ parser_e_generic_constraints_not_allowed_here});
  799. { construct a name which can be used for a type specification }
  800. constraintdata:=tgenericconstraintdata.create;
  801. defname:='';
  802. str(current_module.deflist.count,defname);
  803. defname:='$gendef'+defname;
  804. allowconstructor:=m_delphi in current_settings.modeswitches;
  805. constraintdata.basedef:=generrordef;
  806. repeat
  807. doconsume:=true;
  808. case token of
  809. _CONSTRUCTOR:
  810. begin
  811. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  812. Message(parser_e_illegal_expression);
  813. include(constraintdata.flags,gcf_constructor);
  814. allowconstructor:=false;
  815. end;
  816. _CLASS:
  817. begin
  818. if gcf_class in constraintdata.flags then
  819. Message(parser_e_illegal_expression);
  820. if constraintdata.basedef=generrordef then
  821. include(constraintdata.flags,gcf_class)
  822. else
  823. Message(parser_e_illegal_expression);
  824. end;
  825. _RECORD:
  826. begin
  827. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  828. or (constraintdata.interfaces.count>0) then
  829. Message(parser_e_illegal_expression)
  830. else
  831. begin
  832. srsymtable:=trecordsymtable.create(defname,0);
  833. constraintdata.basedef:=trecorddef.create(defname,srsymtable);
  834. include(constraintdata.flags,gcf_record);
  835. allowconstructor:=false;
  836. end;
  837. end;
  838. else
  839. begin
  840. { after single_type "token" is the trailing ",", ";" or
  841. ">"! }
  842. doconsume:=false;
  843. { def is already set to a class or record }
  844. if gcf_record in constraintdata.flags then
  845. Message(parser_e_illegal_expression);
  846. single_type(def, [stoAllowSpecialization]);
  847. { only types that are inheritable are allowed }
  848. if (def.typ<>objectdef) or
  849. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  850. Message(type_e_class_or_interface_type_expected);
  851. case tobjectdef(def).objecttype of
  852. odt_class,
  853. odt_javaclass:
  854. begin
  855. if gcf_class in constraintdata.flags then
  856. { "class" + concrete class is not allowed }
  857. Message(parser_e_illegal_expression)
  858. else
  859. { do we already have a concrete class? }
  860. if constraintdata.basedef<>generrordef then
  861. Message(parser_e_illegal_expression)
  862. else
  863. constraintdata.basedef:=def;
  864. end;
  865. odt_interfacecom,
  866. odt_interfacecorba,
  867. odt_interfacejava,
  868. odt_dispinterface:
  869. constraintdata.interfaces.add(def);
  870. end;
  871. end;
  872. end;
  873. if doconsume then
  874. consume(token);
  875. until not try_to_consume(_COMMA);
  876. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  877. ((constraintdata.interfaces.count>1) and (constraintdata.basedef=generrordef)) or
  878. ((constraintdata.interfaces.count>0) and (constraintdata.basedef<>generrordef)) then
  879. begin
  880. if constraintdata.basedef.typ=errordef then
  881. { don't pass an errordef as a parent to a tobjectdef }
  882. constraintdata.basedef:=nil
  883. else
  884. if constraintdata.basedef.typ<>objectdef then
  885. internalerror(2012101101);
  886. constraintdata.basedef:=tobjectdef.create({$ifdef jvm}odt_javaclass{$else}odt_class{$endif},defname,tobjectdef(constraintdata.basedef));
  887. include(constraintdata.basedef.defoptions,df_genconstraint);
  888. for i:=0 to constraintdata.interfaces.count-1 do
  889. tobjectdef(constraintdata.basedef).implementedinterfaces.add(
  890. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  891. end
  892. else
  893. if constraintdata.interfaces.count=1 then
  894. begin
  895. constraintdata.basedef:=tdef(constraintdata.interfaces[0]);
  896. constraintdata.interfaces.delete(0);
  897. end;
  898. for i:=firstidx to result.count-1 do
  899. with ttypesym(result[i]) do
  900. begin
  901. genconstraintdata:=tgenericconstraintdata.create;
  902. genconstraintdata.basedef:=constraintdata.basedef;
  903. genconstraintdata.flags:=constraintdata.flags;
  904. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  905. typedef:=constraintdata.basedef;
  906. end;
  907. firstidx:=result.count;
  908. constraintdata.free;
  909. end;
  910. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  911. end;
  912. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  913. var
  914. i: longint;
  915. generictype: ttypesym;
  916. st: tsymtable;
  917. begin
  918. def.genericdef:=genericdef;
  919. if not assigned(genericlist) then
  920. exit;
  921. if assigned(genericdef) then
  922. include(def.defoptions,df_specialization)
  923. else
  924. if genericlist.count>0 then
  925. include(def.defoptions,df_generic);
  926. case def.typ of
  927. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  928. arraydef: st:=tarraydef(def).symtable;
  929. procvardef,procdef: st:=tabstractprocdef(def).parast;
  930. else
  931. internalerror(201101020);
  932. end;
  933. for i:=0 to genericlist.count-1 do
  934. begin
  935. generictype:=ttypesym(genericlist[i]);
  936. st.insert(generictype);
  937. include(generictype.symoptions,sp_generic_para);
  938. def.genericparas.add(generictype.name,generictype);
  939. end;
  940. end;
  941. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfpobjectlist);
  942. var
  943. gensym : ttypesym;
  944. begin
  945. { for generics in non-Delphi modes we insert a private type symbol
  946. that has the same base name as the currently parsed generic and
  947. that references this defs }
  948. if not (m_delphi in current_settings.modeswitches) and
  949. (
  950. (
  951. parse_generic and
  952. assigned(genericlist) and
  953. (genericlist.count>0)
  954. ) or
  955. (
  956. assigned(current_specializedef) and
  957. assigned(current_structdef.genericdef) and
  958. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  959. (pos('$',name)>0)
  960. )
  961. ) then
  962. begin
  963. { we need to pass nil as def here, because the constructor wants
  964. to set the typesym of the def which is not what we want }
  965. gensym:=ttypesym.create(copy(name,1,pos('$',name)-1),nil);
  966. gensym.typedef:=current_structdef;
  967. include(gensym.symoptions,sp_internal);
  968. { the symbol should be only visible to the generic class
  969. itself }
  970. gensym.visibility:=vis_strictprivate;
  971. symtablestack.top.insert(gensym);
  972. end;
  973. end;
  974. function generate_generic_name(const name:tidstring;specializename:ansistring):tidstring;
  975. var
  976. crc : cardinal;
  977. begin
  978. if specializename='' then
  979. internalerror(2012061901);
  980. { build the new type's name }
  981. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  982. result:=name+'$crc'+hexstr(crc,8);
  983. end;
  984. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  985. var
  986. pu : tused_unit;
  987. hmodule : tmodule;
  988. unitsyms : TFPHashObjectList;
  989. sym : tsym;
  990. i : Integer;
  991. begin
  992. if not assigned(genericdef) then
  993. internalerror(200705151);
  994. { Setup symtablestack at definition time
  995. to get types right, however this is not perfect, we should probably record
  996. the resolved symbols }
  997. state.oldsymtablestack:=symtablestack;
  998. state.oldextendeddefs:=current_module.extendeddefs;
  999. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1000. symtablestack:=tdefawaresymtablestack.create;
  1001. hmodule:=find_module_from_symtable(genericdef.owner);
  1002. if hmodule=nil then
  1003. internalerror(200705152);
  1004. { collect all unit syms in the generic's unit as we need to establish
  1005. their unitsym.module link again so that unit identifiers can be used }
  1006. unitsyms:=tfphashobjectlist.create(false);
  1007. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1008. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1009. begin
  1010. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1011. if sym.typ=unitsym then
  1012. unitsyms.add(upper(sym.realname),sym);
  1013. end;
  1014. { add all units if we are specializing inside the current unit (as the
  1015. generic could have been declared in the implementation part), but load
  1016. only interface units, if we are in a different unit as then the generic
  1017. needs to be in the interface section }
  1018. pu:=tused_unit(hmodule.used_units.first);
  1019. while assigned(pu) do
  1020. begin
  1021. if not assigned(pu.u.globalsymtable) then
  1022. { in certain circular, but valid unit constellations it can happen
  1023. that we specialize a generic in a different unit that was used
  1024. in the implementation section of the generic's unit and were the
  1025. interface is still being parsed and thus the localsymtable is in
  1026. reality the global symtable }
  1027. if pu.u.in_interface then
  1028. symtablestack.push(pu.u.localsymtable)
  1029. else
  1030. internalerror(200705153)
  1031. else
  1032. symtablestack.push(pu.u.globalsymtable);
  1033. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1034. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1035. tunitsym(sym).module:=pu.u;
  1036. pu:=tused_unit(pu.next);
  1037. end;
  1038. unitsyms.free;
  1039. if assigned(hmodule.globalsymtable) then
  1040. symtablestack.push(hmodule.globalsymtable);
  1041. { push the localsymtable if needed }
  1042. if (hmodule<>current_module) or not current_module.in_interface then
  1043. symtablestack.push(hmodule.localsymtable);
  1044. end;
  1045. procedure specialization_done(var state: tspecializationstate);
  1046. begin
  1047. { Restore symtablestack }
  1048. current_module.extendeddefs.free;
  1049. current_module.extendeddefs:=state.oldextendeddefs;
  1050. symtablestack.free;
  1051. symtablestack:=state.oldsymtablestack;
  1052. { clear the state record to be on the safe side }
  1053. fillchar(state, sizeof(state), 0);
  1054. end;
  1055. end.