pgenutil.pas 71 KB

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