pgenutil.pas 49 KB

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