pgenutil.pas 71 KB

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