pdecl.pas 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does declaration (but not type) parsing for Free Pascal
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pdecl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { global }
  24. globtype,
  25. { symtable }
  26. symsym,symdef,
  27. { pass_1 }
  28. node;
  29. function readconstant(const orgname:string;const filepos:tfileposinfo; out nodetype: tnodetype):tconstsym;
  30. procedure const_dec(out had_generic:boolean);
  31. procedure consts_dec(in_structure, allow_typed_const: boolean;out had_generic:boolean);
  32. procedure label_dec;
  33. procedure type_dec(out had_generic:boolean);
  34. procedure types_dec(in_structure: boolean;out had_generic:boolean;var rtti_attrs_def: trtti_attribute_list);
  35. procedure var_dec(out had_generic:boolean);
  36. procedure threadvar_dec(out had_generic:boolean);
  37. procedure property_dec;
  38. procedure resourcestring_dec(out had_generic:boolean);
  39. procedure parse_rttiattributes(var rtti_attrs_def:trtti_attribute_list);
  40. implementation
  41. uses
  42. SysUtils,
  43. { common }
  44. cutils,
  45. { global }
  46. globals,tokens,verbose,widestr,constexp,
  47. systems,aasmdata,fmodule,compinnr,
  48. { symtable }
  49. symconst,symbase,symtype,symcpu,symcreat,defutil,defcmp,symtable,
  50. { pass 1 }
  51. ninl,ncon,nobj,ngenutil,nld,nmem,ncal,pass_1,
  52. { parser }
  53. scanner,
  54. pbase,pexpr,ptype,ptconst,pdecsub,pdecvar,pdecobj,pgenutil,pparautl,
  55. {$ifdef jvm}
  56. pjvm,
  57. {$endif}
  58. { cpu-information }
  59. cpuinfo
  60. ;
  61. function is_system_custom_attribute_descendant(def:tdef):boolean;
  62. begin
  63. if not assigned(class_tcustomattribute) then
  64. class_tcustomattribute:=tobjectdef(search_system_type('TCUSTOMATTRIBUTE').typedef);
  65. Result:=def_is_related(def,class_tcustomattribute);
  66. end;
  67. function readconstant(const orgname:string;const filepos:tfileposinfo; out nodetype: tnodetype):tconstsym;
  68. var
  69. hp : tconstsym;
  70. p : tnode;
  71. ps : pconstset;
  72. pd : pbestreal;
  73. pg : pguid;
  74. sp : pchar;
  75. pw : pcompilerwidestring;
  76. storetokenpos : tfileposinfo;
  77. begin
  78. readconstant:=nil;
  79. if orgname='' then
  80. internalerror(9584582);
  81. hp:=nil;
  82. p:=comp_expr([ef_accept_equal]);
  83. nodetype:=p.nodetype;
  84. storetokenpos:=current_tokenpos;
  85. current_tokenpos:=filepos;
  86. case p.nodetype of
  87. ordconstn:
  88. begin
  89. if p.resultdef.typ=pointerdef then
  90. hp:=cconstsym.create_ordptr(orgname,constpointer,tordconstnode(p).value.uvalue,p.resultdef)
  91. else
  92. hp:=cconstsym.create_ord(orgname,constord,tordconstnode(p).value,p.resultdef);
  93. end;
  94. stringconstn:
  95. begin
  96. if is_wide_or_unicode_string(p.resultdef) then
  97. begin
  98. initwidestring(pw);
  99. copywidestring(pcompilerwidestring(tstringconstnode(p).value_str),pw);
  100. hp:=cconstsym.create_wstring(orgname,constwstring,pw);
  101. end
  102. else
  103. begin
  104. getmem(sp,tstringconstnode(p).len+1);
  105. move(tstringconstnode(p).value_str^,sp^,tstringconstnode(p).len+1);
  106. { if a non-default ansistring code page has been specified,
  107. keep it }
  108. if is_ansistring(p.resultdef) and
  109. (tstringdef(p.resultdef).encoding<>0) then
  110. hp:=cconstsym.create_string(orgname,conststring,sp,tstringconstnode(p).len,p.resultdef)
  111. else
  112. hp:=cconstsym.create_string(orgname,conststring,sp,tstringconstnode(p).len,nil);
  113. end;
  114. end;
  115. realconstn :
  116. begin
  117. new(pd);
  118. pd^:=trealconstnode(p).value_real;
  119. hp:=cconstsym.create_ptr(orgname,constreal,pd,p.resultdef);
  120. end;
  121. setconstn :
  122. begin
  123. new(ps);
  124. ps^:=tsetconstnode(p).value_set^;
  125. hp:=cconstsym.create_ptr(orgname,constset,ps,p.resultdef);
  126. end;
  127. pointerconstn :
  128. begin
  129. hp:=cconstsym.create_ordptr(orgname,constpointer,tpointerconstnode(p).value,p.resultdef);
  130. end;
  131. niln :
  132. begin
  133. hp:=cconstsym.create_ord(orgname,constnil,0,p.resultdef);
  134. end;
  135. typen :
  136. begin
  137. if is_interface(p.resultdef) then
  138. begin
  139. if assigned(tobjectdef(p.resultdef).iidguid) then
  140. begin
  141. new(pg);
  142. pg^:=tobjectdef(p.resultdef).iidguid^;
  143. hp:=cconstsym.create_ptr(orgname,constguid,pg,p.resultdef);
  144. end
  145. else
  146. Message1(parser_e_interface_has_no_guid,tobjectdef(p.resultdef).objrealname^);
  147. end
  148. else
  149. Message(parser_e_illegal_expression);
  150. end;
  151. inlinen:
  152. begin
  153. { this situation only happens if a intrinsic is parsed that has a
  154. generic type as its argument. As we don't know certain
  155. information about the final type yet, we need to use safe
  156. values (mostly 0, except for (Bit)SizeOf()) }
  157. if not parse_generic then
  158. Message(parser_e_illegal_expression);
  159. case tinlinenode(p).inlinenumber of
  160. in_sizeof_x:
  161. begin
  162. hp:=cconstsym.create_ord(orgname,constord,1,p.resultdef);
  163. end;
  164. in_bitsizeof_x:
  165. begin
  166. hp:=cconstsym.create_ord(orgname,constord,8,p.resultdef);
  167. end;
  168. { add other cases here if necessary }
  169. else
  170. Message(parser_e_illegal_expression);
  171. end;
  172. end;
  173. else
  174. Message(parser_e_illegal_expression);
  175. end;
  176. current_tokenpos:=storetokenpos;
  177. p.free;
  178. readconstant:=hp;
  179. end;
  180. procedure const_dec(out had_generic:boolean);
  181. begin
  182. consume(_CONST);
  183. consts_dec(false,true,had_generic);
  184. end;
  185. procedure consts_dec(in_structure, allow_typed_const: boolean;out had_generic:boolean);
  186. var
  187. orgname : TIDString;
  188. hdef : tdef;
  189. sym : tsym;
  190. dummysymoptions : tsymoptions;
  191. deprecatedmsg : pshortstring;
  192. storetokenpos,filepos : tfileposinfo;
  193. nodetype : tnodetype;
  194. old_block_type : tblock_type;
  195. first,
  196. isgeneric,
  197. skipequal : boolean;
  198. tclist : tasmlist;
  199. varspez : tvarspez;
  200. begin
  201. old_block_type:=block_type;
  202. block_type:=bt_const;
  203. had_generic:=false;
  204. first:=true;
  205. repeat
  206. orgname:=orgpattern;
  207. filepos:=current_tokenpos;
  208. isgeneric:=not (m_delphi in current_settings.modeswitches) and (token=_ID) and (idtoken=_GENERIC);
  209. consume(_ID);
  210. case token of
  211. _EQ:
  212. begin
  213. consume(_EQ);
  214. sym:=readconstant(orgname,filepos,nodetype);
  215. { Support hint directives }
  216. dummysymoptions:=[];
  217. deprecatedmsg:=nil;
  218. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  219. if assigned(sym) then
  220. begin
  221. sym.symoptions:=sym.symoptions+dummysymoptions;
  222. sym.deprecatedmsg:=deprecatedmsg;
  223. sym.visibility:=symtablestack.top.currentvisibility;
  224. symtablestack.top.insert(sym);
  225. sym.register_sym;
  226. {$ifdef jvm}
  227. { for the JVM target, some constants need to be
  228. initialized at run time (enums, sets) -> create fake
  229. typed const to do so (at least if they are visible
  230. outside this routine, since we won't directly access
  231. these symbols in the generated code) }
  232. if (symtablestack.top.symtablelevel<normal_function_level) and
  233. assigned(tconstsym(sym).constdef) and
  234. (tconstsym(sym).constdef.typ in [enumdef,setdef]) then
  235. jvm_add_typed_const_initializer(tconstsym(sym));
  236. {$endif}
  237. end
  238. else
  239. stringdispose(deprecatedmsg);
  240. consume(_SEMICOLON);
  241. end;
  242. _COLON:
  243. begin
  244. if not allow_typed_const then
  245. begin
  246. Message(parser_e_no_typed_const);
  247. consume_all_until(_SEMICOLON);
  248. end;
  249. { set the blocktype first so a consume also supports a
  250. caret, to support const s : ^string = nil }
  251. block_type:=bt_const_type;
  252. consume(_COLON);
  253. read_anon_type(hdef,false);
  254. block_type:=bt_const;
  255. skipequal:=false;
  256. { create symbol }
  257. storetokenpos:=current_tokenpos;
  258. current_tokenpos:=filepos;
  259. if not (cs_typed_const_writable in current_settings.localswitches) then
  260. varspez:=vs_const
  261. else
  262. varspez:=vs_value;
  263. { if we are dealing with structure const then we need to handle it as a
  264. structure static variable: create a symbol in unit symtable and a reference
  265. to it from the structure or linking will fail }
  266. if symtablestack.top.symtabletype in [recordsymtable,ObjectSymtable] then
  267. begin
  268. { note: we keep hdef so that we might at least read the
  269. constant data correctly for error recovery }
  270. check_allowed_for_var_or_const(hdef,false);
  271. sym:=cfieldvarsym.create(orgname,varspez,hdef,[]);
  272. symtablestack.top.insert(sym);
  273. sym:=make_field_static(symtablestack.top,tfieldvarsym(sym));
  274. end
  275. else
  276. begin
  277. sym:=cstaticvarsym.create(orgname,varspez,hdef,[]);
  278. sym.visibility:=symtablestack.top.currentvisibility;
  279. symtablestack.top.insert(sym);
  280. end;
  281. sym.register_sym;
  282. current_tokenpos:=storetokenpos;
  283. { procvar can have proc directives, but not type references }
  284. if (hdef.typ=procvardef) and
  285. (hdef.typesym=nil) then
  286. begin
  287. { support p : procedure;stdcall=nil; }
  288. if try_to_consume(_SEMICOLON) then
  289. begin
  290. if check_proc_directive(true) then
  291. parse_var_proc_directives(sym)
  292. else
  293. begin
  294. Message(parser_e_proc_directive_expected);
  295. skipequal:=true;
  296. end;
  297. end
  298. else
  299. { support p : procedure stdcall=nil; }
  300. begin
  301. if check_proc_directive(true) then
  302. parse_var_proc_directives(sym);
  303. end;
  304. { add default calling convention }
  305. handle_calling_convention(tabstractprocdef(hdef),hcc_default_actions_intf);
  306. end;
  307. if not skipequal then
  308. begin
  309. { get init value }
  310. consume(_EQ);
  311. if (cs_typed_const_writable in current_settings.localswitches) then
  312. tclist:=current_asmdata.asmlists[al_typedconsts]
  313. else
  314. tclist:=current_asmdata.asmlists[al_rotypedconsts];
  315. read_typed_const(tclist,tstaticvarsym(sym),in_structure);
  316. end;
  317. end;
  318. else
  319. if not first and isgeneric and (token in [_PROCEDURE,_FUNCTION,_CLASS]) then
  320. begin
  321. had_generic:=true;
  322. break;
  323. end
  324. else
  325. { generate an error }
  326. consume(_EQ);
  327. end;
  328. first:=false;
  329. until (token<>_ID) or
  330. (in_structure and
  331. ((idtoken in [_PRIVATE,_PROTECTED,_PUBLIC,_PUBLISHED,_STRICT]) or
  332. ((m_final_fields in current_settings.modeswitches) and
  333. (idtoken=_FINAL))));
  334. block_type:=old_block_type;
  335. end;
  336. procedure label_dec;
  337. var
  338. labelsym : tlabelsym;
  339. begin
  340. consume(_LABEL);
  341. if not(cs_support_goto in current_settings.moduleswitches) then
  342. Message(sym_e_goto_and_label_not_supported);
  343. repeat
  344. if not(token in [_ID,_INTCONST]) then
  345. consume(_ID)
  346. else
  347. begin
  348. if token=_ID then
  349. labelsym:=clabelsym.create(orgpattern)
  350. else
  351. labelsym:=clabelsym.create(pattern);
  352. symtablestack.top.insert(labelsym);
  353. if m_non_local_goto in current_settings.modeswitches then
  354. begin
  355. if symtablestack.top.symtabletype=localsymtable then
  356. begin
  357. labelsym.jumpbuf:=clocalvarsym.create('LABEL$_'+labelsym.name,vs_value,rec_jmp_buf,[]);
  358. symtablestack.top.insert(labelsym.jumpbuf);
  359. end
  360. else
  361. begin
  362. labelsym.jumpbuf:=cstaticvarsym.create('LABEL$_'+labelsym.name,vs_value,rec_jmp_buf,[]);
  363. symtablestack.top.insert(labelsym.jumpbuf);
  364. cnodeutils.insertbssdata(tstaticvarsym(labelsym.jumpbuf));
  365. end;
  366. include(labelsym.jumpbuf.symoptions,sp_internal);
  367. { the buffer will be setup later, but avoid a hint }
  368. tabstractvarsym(labelsym.jumpbuf).varstate:=vs_written;
  369. end;
  370. consume(token);
  371. end;
  372. if token<>_SEMICOLON then consume(_COMMA);
  373. until not(token in [_ID,_INTCONST]);
  374. consume(_SEMICOLON);
  375. end;
  376. function find_create_constructor(objdef:tobjectdef):tsymentry;
  377. begin
  378. while assigned(objdef) do
  379. begin
  380. result:=objdef.symtable.Find('CREATE');
  381. if assigned(result) then
  382. exit;
  383. objdef:=objdef.childof;
  384. end;
  385. // A class without a constructor called 'create'?!?
  386. internalerror(2012111101);
  387. end;
  388. procedure parse_rttiattributes(var rtti_attrs_def:trtti_attribute_list);
  389. function read_attr_paras:tnode;
  390. var
  391. old_block_type : tblock_type;
  392. begin
  393. if try_to_consume(_LKLAMMER) then
  394. begin
  395. { we only want constants here }
  396. old_block_type:=block_type;
  397. block_type:=bt_const;
  398. result:=parse_paras(false,false,_RKLAMMER);
  399. block_type:=old_block_type;
  400. consume(_RKLAMMER);
  401. end
  402. else
  403. result:=nil;
  404. end;
  405. var
  406. p,paran,pcalln,ptmp : tnode;
  407. i,pcount : sizeint;
  408. paras : array of tnode;
  409. od : tobjectdef;
  410. constrsym : tsymentry;
  411. typesym : ttypesym;
  412. parasok : boolean;
  413. begin
  414. consume(_LECKKLAMMER);
  415. repeat
  416. { Parse attribute type }
  417. p:=factor(false,[ef_type_only,ef_check_attr_suffix]);
  418. if p.nodetype=typen then
  419. begin
  420. typesym:=ttypesym(ttypenode(p).typesym);
  421. od:=tobjectdef(ttypenode(p).typedef);
  422. { Check if the attribute class is related to TCustomAttribute }
  423. if not is_system_custom_attribute_descendant(od) then
  424. begin
  425. incompatibletypes(od,class_tcustomattribute);
  426. read_attr_paras.free;
  427. continue;
  428. end;
  429. paran:=read_attr_paras;
  430. { Search the tprocdef of the constructor which has to be called. }
  431. constrsym:=find_create_constructor(od);
  432. if constrsym.typ<>procsym then
  433. internalerror(2018102301);
  434. pcalln:=ccallnode.create(paran,tprocsym(constrsym),od.symtable,cloadvmtaddrnode.create(p),[],nil);
  435. p:=nil;
  436. typecheckpass(pcalln);
  437. if (pcalln.nodetype=calln) and assigned(tcallnode(pcalln).procdefinition) and not codegenerror then
  438. begin
  439. { TODO: once extended RTTI for methods is supported, reject a
  440. constructor if it doesn't have extended RTTI enabled }
  441. { collect the parameters of the call node as there might be
  442. compile time type conversions (e.g. a Byte parameter being
  443. passed a value > 255) }
  444. paran:=tcallnode(pcalln).left;
  445. { only count visible parameters (thankfully open arrays are not
  446. supported, otherwise we'd need to handle those as well) }
  447. parasok:=true;
  448. paras:=nil;
  449. if assigned(paran) then
  450. begin
  451. ptmp:=paran;
  452. pcount:=0;
  453. while assigned(ptmp) do
  454. begin
  455. if not (vo_is_hidden_para in tcallparanode(ptmp).parasym.varoptions) then
  456. inc(pcount);
  457. ptmp:=tcallparanode(ptmp).right;
  458. end;
  459. setlength(paras,pcount);
  460. ptmp:=paran;
  461. pcount:=0;
  462. while assigned(ptmp) do
  463. begin
  464. if not (vo_is_hidden_para in tcallparanode(ptmp).parasym.varoptions) then
  465. begin
  466. if not is_constnode(tcallparanode(ptmp).left) then
  467. begin
  468. parasok:=false;
  469. messagepos(tcallparanode(ptmp).left.fileinfo,type_e_constant_expr_expected);
  470. end;
  471. paras[high(paras)-pcount]:=tcallparanode(ptmp).left.getcopy;
  472. inc(pcount);
  473. end;
  474. ptmp:=tcallparanode(ptmp).right;
  475. end;
  476. end;
  477. if parasok then
  478. begin
  479. { Add attribute to attribute list which will be added
  480. to the property which is defined next. }
  481. if not assigned(rtti_attrs_def) then
  482. rtti_attrs_def:=trtti_attribute_list.create;
  483. rtti_attrs_def.addattribute(typesym,tcallnode(pcalln).procdefinition,pcalln,paras);
  484. end
  485. else
  486. begin
  487. { cleanup }
  488. pcalln.free;
  489. for i:=0 to high(paras) do
  490. paras[i].free;
  491. end;
  492. end
  493. else
  494. pcalln.free;
  495. end
  496. else
  497. begin
  498. Message(type_e_type_id_expected);
  499. { try to recover by nevertheless reading the parameters (if any) }
  500. read_attr_paras.free;
  501. end;
  502. p.free;
  503. until not try_to_consume(_COMMA);
  504. consume(_RECKKLAMMER);
  505. end;
  506. { From http://clang.llvm.org/docs/LanguageExtensions.html#objective-c-features :
  507. To determine whether a method has an inferred related result type, the first word in the camel-case selector
  508. (e.g., “init” in “initWithObjects”) is considered, and the method will have a related result type if its return
  509. type is compatible with the type of its class and if:
  510. * the first word is "alloc" or "new", and the method is a class method, or
  511. * the first word is "autorelease", "init", "retain", or "self", and the method is an instance method.
  512. If a method with a related result type is overridden by a subclass method, the subclass method must also return
  513. a type that is compatible with the subclass type.
  514. }
  515. procedure pd_set_objc_related_result(def: tobject; para: pointer);
  516. var
  517. pd: tprocdef;
  518. i, firstcamelend: longint;
  519. inferresult: boolean;
  520. begin
  521. if tdef(def).typ<>procdef then
  522. exit;
  523. pd:=tprocdef(def);
  524. if not(po_msgstr in pd.procoptions) then
  525. internalerror(2019082401);
  526. firstcamelend:=length(pd.messageinf.str^);
  527. for i:=1 to length(pd.messageinf.str^) do
  528. if pd.messageinf.str^[i] in ['A'..'Z'] then
  529. begin
  530. firstcamelend:=pred(i);
  531. break;
  532. end;
  533. case copy(pd.messageinf.str^,1,firstcamelend) of
  534. 'alloc',
  535. 'new':
  536. inferresult:=po_classmethod in pd.procoptions;
  537. 'autorelease',
  538. 'init',
  539. 'retain',
  540. 'self':
  541. inferresult:=not(po_classmethod in pd.procoptions);
  542. else
  543. inferresult:=false;
  544. end;
  545. if inferresult and
  546. def_is_related(tdef(pd.procsym.owner.defowner),pd.returndef) then
  547. include(pd.procoptions,po_objc_related_result_type);
  548. end;
  549. procedure types_dec(in_structure: boolean;out had_generic:boolean;var rtti_attrs_def: trtti_attribute_list);
  550. function determine_generic_def(name:tidstring):tstoreddef;
  551. var
  552. hashedid : THashedIDString;
  553. pd : tprocdef;
  554. sym : tsym;
  555. begin
  556. result:=nil;
  557. { check whether this is a declaration of a type inside a
  558. specialization }
  559. if assigned(current_structdef) and
  560. (df_specialization in current_structdef.defoptions) then
  561. begin
  562. if not assigned(current_structdef.genericdef) or
  563. not (current_structdef.genericdef.typ in [recorddef,objectdef]) then
  564. internalerror(2011052301);
  565. hashedid.id:=name;
  566. { we could be inside a method of the specialization
  567. instead of its declaration, so check that first (as
  568. local nested types aren't allowed we don't need to
  569. walk the symtablestack to find the localsymtable) }
  570. if symtablestack.top.symtabletype=localsymtable then
  571. begin
  572. { we are in a method }
  573. if not assigned(symtablestack.top.defowner) or
  574. (symtablestack.top.defowner.typ<>procdef) then
  575. internalerror(2011120701);
  576. pd:=tprocdef(symtablestack.top.defowner);
  577. if not assigned(pd.genericdef) or (pd.genericdef.typ<>procdef) then
  578. internalerror(2011120702);
  579. sym:=tsym(tprocdef(pd.genericdef).localst.findwithhash(hashedid));
  580. end
  581. else
  582. sym:=nil;
  583. if not assigned(sym) or not (sym.typ=typesym) then
  584. begin
  585. { now search in the declaration of the generic }
  586. sym:=tsym(tabstractrecorddef(current_structdef.genericdef).symtable.findwithhash(hashedid));
  587. if not assigned(sym) or not (sym.typ=typesym) then
  588. internalerror(2011052302);
  589. end;
  590. { use the corresponding type in the generic's symtable as
  591. genericdef for the specialized type }
  592. result:=tstoreddef(ttypesym(sym).typedef);
  593. end;
  594. end;
  595. procedure finalize_class_external_status(od: tobjectdef);
  596. begin
  597. if [oo_is_external,oo_is_forward] <= od.objectoptions then
  598. begin
  599. { formal definition: x = objcclass external; }
  600. exclude(od.objectoptions,oo_is_forward);
  601. include(od.objectoptions,oo_is_formal);
  602. end;
  603. end;
  604. var
  605. typename,orgtypename,
  606. gentypename,genorgtypename : TIDString;
  607. newtype : ttypesym;
  608. sym : tsym;
  609. hdef : tdef;
  610. defpos,storetokenpos : tfileposinfo;
  611. old_block_type : tblock_type;
  612. old_checkforwarddefs: TFPObjectList;
  613. objecttype : tobjecttyp;
  614. first,
  615. isgeneric,
  616. isunique,
  617. istyperenaming : boolean;
  618. generictypelist : tfphashobjectlist;
  619. generictokenbuf : tdynamicarray;
  620. vmtbuilder : TVMTBuilder;
  621. p:tnode;
  622. gendef : tstoreddef;
  623. s : shortstring;
  624. i : longint;
  625. {$ifdef x86}
  626. segment_register: string;
  627. {$endif x86}
  628. begin
  629. old_block_type:=block_type;
  630. { save unit container of forward declarations -
  631. we can be inside nested class type block }
  632. old_checkforwarddefs:=current_module.checkforwarddefs;
  633. current_module.checkforwarddefs:=TFPObjectList.Create(false);
  634. block_type:=bt_type;
  635. hdef:=nil;
  636. first:=true;
  637. had_generic:=false;
  638. repeat
  639. defpos:=current_tokenpos;
  640. istyperenaming:=false;
  641. generictypelist:=nil;
  642. generictokenbuf:=nil;
  643. { class attribute definitions? }
  644. if m_prefixed_attributes in current_settings.modeswitches then
  645. while token=_LECKKLAMMER do
  646. parse_rttiattributes(rtti_attrs_def);
  647. { fpc generic declaration? }
  648. if first then
  649. had_generic:=not(m_delphi in current_settings.modeswitches) and try_to_consume(_GENERIC);
  650. isgeneric:=had_generic;
  651. typename:=pattern;
  652. orgtypename:=orgpattern;
  653. consume(_ID);
  654. { delphi generic declaration? }
  655. if (m_delphi in current_settings.modeswitches) then
  656. isgeneric:=token=_LSHARPBRACKET;
  657. { Generic type declaration? }
  658. if isgeneric then
  659. begin
  660. if assigned(current_genericdef) then
  661. Message(parser_f_no_generic_inside_generic);
  662. consume(_LSHARPBRACKET);
  663. generictypelist:=parse_generic_parameters(true);
  664. consume(_RSHARPBRACKET);
  665. { we are not freeing the type parameters, so register them }
  666. for i:=0 to generictypelist.count-1 do
  667. begin
  668. ttypesym(generictypelist[i]).register_sym;
  669. tstoreddef(ttypesym(generictypelist[i]).typedef).register_def;
  670. end;
  671. str(generictypelist.Count,s);
  672. gentypename:=typename+'$'+s;
  673. genorgtypename:=orgtypename+'$'+s;
  674. end
  675. else
  676. begin
  677. gentypename:=typename;
  678. genorgtypename:=orgtypename;
  679. end;
  680. consume(_EQ);
  681. { support 'ttype=type word' syntax }
  682. isunique:=try_to_consume(_TYPE);
  683. { MacPas object model is more like Delphi's than like TP's, but }
  684. { uses the object keyword instead of class }
  685. if (m_mac in current_settings.modeswitches) and
  686. (token = _OBJECT) then
  687. token := _CLASS;
  688. { Start recording a generic template }
  689. if assigned(generictypelist) then
  690. begin
  691. generictokenbuf:=tdynamicarray.create(256);
  692. current_scanner.startrecordtokens(generictokenbuf);
  693. end;
  694. { is the type already defined? -- must be in the current symtable,
  695. not in a nested symtable or one higher up the stack -> don't
  696. use searchsym & frinds! }
  697. sym:=tsym(symtablestack.top.find(gentypename));
  698. newtype:=nil;
  699. { found a symbol with this name? }
  700. if assigned(sym) then
  701. begin
  702. if (sym.typ=typesym) and
  703. { this should not be a symbol that was created by a generic
  704. that was declared earlier }
  705. not (
  706. (ttypesym(sym).typedef.typ=undefineddef) and
  707. (sp_generic_dummy in sym.symoptions)
  708. ) then
  709. begin
  710. if ((token=_CLASS) or
  711. (token=_INTERFACE) or
  712. (token=_DISPINTERFACE) or
  713. (token=_OBJCCLASS) or
  714. (token=_OBJCPROTOCOL) or
  715. (token=_OBJCCATEGORY)) and
  716. (assigned(ttypesym(sym).typedef)) and
  717. is_implicit_pointer_object_type(ttypesym(sym).typedef) and
  718. (oo_is_forward in tobjectdef(ttypesym(sym).typedef).objectoptions) then
  719. begin
  720. case token of
  721. _CLASS :
  722. objecttype:=default_class_type;
  723. _INTERFACE :
  724. case current_settings.interfacetype of
  725. it_interfacecom:
  726. objecttype:=odt_interfacecom;
  727. it_interfacecorba:
  728. objecttype:=odt_interfacecorba;
  729. it_interfacejava:
  730. objecttype:=odt_interfacejava;
  731. end;
  732. _DISPINTERFACE :
  733. objecttype:=odt_dispinterface;
  734. _OBJCCLASS,
  735. _OBJCCATEGORY :
  736. objecttype:=odt_objcclass;
  737. _OBJCPROTOCOL :
  738. objecttype:=odt_objcprotocol;
  739. else
  740. internalerror(200811072);
  741. end;
  742. consume(token);
  743. { determine the generic def in case we are in a nested type
  744. of a specialization }
  745. gendef:=determine_generic_def(gentypename);
  746. { we can ignore the result, the definition is modified }
  747. object_dec(objecttype,genorgtypename,newtype,gendef,generictypelist,tobjectdef(ttypesym(sym).typedef),ht_none);
  748. newtype:=ttypesym(sym);
  749. hdef:=newtype.typedef;
  750. end
  751. else
  752. message1(parser_h_type_redef,genorgtypename);
  753. end;
  754. end;
  755. { no old type reused ? Then insert this new type }
  756. if not assigned(newtype) then
  757. begin
  758. { insert the new type first with an errordef, so that
  759. referencing the type before it's really set it
  760. will give an error (PFV) }
  761. hdef:=generrordef;
  762. gendef:=nil;
  763. storetokenpos:=current_tokenpos;
  764. if isgeneric then
  765. begin
  766. { for generics we need to check whether a non-generic type
  767. already exists and if not we need to insert a symbol with
  768. the non-generic name (available in (org)typename) that is a
  769. undefineddef, so that inline specializations can be used }
  770. sym:=tsym(symtablestack.top.Find(typename));
  771. if not assigned(sym) then
  772. begin
  773. sym:=ctypesym.create(orgtypename,cundefineddef.create(true));
  774. Include(sym.symoptions,sp_generic_dummy);
  775. ttypesym(sym).typedef.typesym:=sym;
  776. sym.visibility:=symtablestack.top.currentvisibility;
  777. symtablestack.top.insert(sym);
  778. ttypesym(sym).typedef.owner:=sym.owner;
  779. end
  780. else
  781. { this is not allowed in non-Delphi modes }
  782. if not (m_delphi in current_settings.modeswitches) then
  783. Message1(sym_e_duplicate_id,genorgtypename)
  784. else
  785. begin
  786. { we need to find this symbol even if it's a variable or
  787. something else when doing an inline specialization }
  788. Include(sym.symoptions,sp_generic_dummy);
  789. add_generic_dummysym(sym);
  790. end;
  791. end
  792. else
  793. begin
  794. if assigned(sym) and (sym.typ=typesym) and
  795. (ttypesym(sym).typedef.typ=undefineddef) and
  796. (sp_generic_dummy in sym.symoptions) then
  797. begin
  798. { this is a symbol that was added by an earlier generic
  799. declaration, reuse it }
  800. newtype:=ttypesym(sym);
  801. newtype.typedef:=hdef;
  802. { use the correct casing }
  803. newtype.RealName:=genorgtypename;
  804. sym:=nil;
  805. end;
  806. { determine the generic def in case we are in a nested type
  807. of a specialization }
  808. gendef:=determine_generic_def(gentypename);
  809. end;
  810. { insert a new type if we don't reuse an existing symbol }
  811. if not assigned(newtype) then
  812. begin
  813. newtype:=ctypesym.create(genorgtypename,hdef);
  814. newtype.visibility:=symtablestack.top.currentvisibility;
  815. symtablestack.top.insert(newtype);
  816. end;
  817. current_tokenpos:=defpos;
  818. current_tokenpos:=storetokenpos;
  819. { read the type definition }
  820. read_named_type(hdef,newtype,gendef,generictypelist,false,isunique);
  821. { update the definition of the type }
  822. if assigned(hdef) then
  823. begin
  824. if df_generic in hdef.defoptions then
  825. { flag parent symtables that they now contain a generic }
  826. hdef.owner.includeoption(sto_has_generic);
  827. if assigned(hdef.typesym) then
  828. begin
  829. istyperenaming:=true;
  830. include(newtype.symoptions,sp_explicitrename);
  831. end;
  832. if isunique then
  833. begin
  834. if is_objc_class_or_protocol(hdef) or
  835. is_java_class_or_interface(hdef) then
  836. Message(parser_e_unique_unsupported);
  837. if is_object(hdef) or
  838. is_class_or_interface_or_dispinterface(hdef) then
  839. begin
  840. { just create a child class type; this is
  841. Delphi-compatible }
  842. hdef:=cobjectdef.create(tobjectdef(hdef).objecttype,genorgtypename,tobjectdef(hdef),true);
  843. end
  844. else
  845. begin
  846. hdef:=tstoreddef(hdef).getcopy;
  847. { check if it is an ansistirng(codepage) declaration }
  848. if is_ansistring(hdef) and try_to_consume(_LKLAMMER) then
  849. begin
  850. p:=comp_expr([ef_accept_equal]);
  851. consume(_RKLAMMER);
  852. if not is_constintnode(p) then
  853. begin
  854. Message(parser_e_illegal_expression);
  855. { error recovery }
  856. end
  857. else
  858. begin
  859. if (tordconstnode(p).value<0) or (tordconstnode(p).value>65535) then
  860. begin
  861. Message(parser_e_invalid_codepage);
  862. tordconstnode(p).value:=0;
  863. end;
  864. tstringdef(hdef).encoding:=int64(tordconstnode(p).value);
  865. end;
  866. p.free;
  867. end;
  868. if (hdef.typ in [pointerdef,classrefdef]) and
  869. (tabstractpointerdef(hdef).pointeddef.typ=forwarddef) then
  870. current_module.checkforwarddefs.add(hdef);
  871. end;
  872. include(hdef.defoptions,df_unique);
  873. end;
  874. if not assigned(hdef.typesym) then
  875. begin
  876. hdef.typesym:=newtype;
  877. if sp_generic_dummy in newtype.symoptions then
  878. add_generic_dummysym(newtype);
  879. end;
  880. end;
  881. { in non-Delphi modes we need a reference to the generic def
  882. without the generic suffix, so it can be found easily when
  883. parsing method implementations }
  884. if isgeneric and assigned(sym) and
  885. not (m_delphi in current_settings.modeswitches) and
  886. (ttypesym(sym).typedef.typ=undefineddef) then
  887. { don't free the undefineddef as the defids rely on the count
  888. of the defs in the def list of the module}
  889. ttypesym(sym).typedef:=hdef;
  890. newtype.typedef:=hdef;
  891. { ensure that the type is registered when no specialization is
  892. currently done }
  893. if current_scanner.replay_stack_depth=0 then
  894. hdef.register_def;
  895. { KAZ: handle TGUID declaration in system unit }
  896. if (cs_compilesystem in current_settings.moduleswitches) and
  897. assigned(hdef) and
  898. (hdef.typ=recorddef) then
  899. begin
  900. if not assigned(rec_tguid) and
  901. (gentypename='TGUID') and
  902. (hdef.size=16) then
  903. rec_tguid:=trecorddef(hdef)
  904. else if not assigned(rec_jmp_buf) and
  905. (gentypename='JMP_BUF') then
  906. rec_jmp_buf:=trecorddef(hdef)
  907. else if not assigned(rec_exceptaddr) and
  908. (gentypename='TEXCEPTADDR') then
  909. rec_exceptaddr:=trecorddef(hdef);
  910. end;
  911. end;
  912. if assigned(hdef) then
  913. begin
  914. case hdef.typ of
  915. pointerdef :
  916. begin
  917. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  918. consume(_SEMICOLON);
  919. {$ifdef x86}
  920. {$ifdef i8086}
  921. if try_to_consume(_HUGE) then
  922. begin
  923. tcpupointerdef(hdef).x86pointertyp:=x86pt_huge;
  924. consume(_SEMICOLON);
  925. end
  926. else
  927. {$endif i8086}
  928. if try_to_consume(_FAR) then
  929. begin
  930. {$if defined(i8086)}
  931. tcpupointerdef(hdef).x86pointertyp:=x86pt_far;
  932. {$elseif defined(i386)}
  933. tcpupointerdef(hdef).x86pointertyp:=x86pt_near_fs;
  934. {$elseif defined(x86_64)}
  935. { for compatibility with previous versions of fpc,
  936. far pointer = regular pointer on x86_64 }
  937. Message1(parser_w_ptr_type_ignored,'FAR');
  938. {$endif}
  939. consume(_SEMICOLON);
  940. end
  941. else
  942. if try_to_consume(_NEAR) then
  943. begin
  944. if token <> _SEMICOLON then
  945. begin
  946. segment_register:=get_stringconst;
  947. case UpCase(segment_register) of
  948. 'CS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_cs;
  949. 'DS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_ds;
  950. 'SS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_ss;
  951. 'ES': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_es;
  952. 'FS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_fs;
  953. 'GS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_gs;
  954. else
  955. Message(asmr_e_invalid_register);
  956. end;
  957. end
  958. else
  959. tcpupointerdef(hdef).x86pointertyp:=x86pt_near;
  960. consume(_SEMICOLON);
  961. end;
  962. {$else x86}
  963. { Previous versions of FPC support declaring a pointer as
  964. far even on non-x86 platforms. }
  965. if try_to_consume(_FAR) then
  966. begin
  967. Message1(parser_w_ptr_type_ignored,'FAR');
  968. consume(_SEMICOLON);
  969. end;
  970. {$endif x86}
  971. end;
  972. procvardef :
  973. begin
  974. { in case of type renaming, don't parse proc directives }
  975. if istyperenaming then
  976. begin
  977. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  978. consume(_SEMICOLON);
  979. end
  980. else
  981. begin
  982. if not check_proc_directive(true) then
  983. begin
  984. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  985. consume(_SEMICOLON);
  986. end;
  987. parse_var_proc_directives(tsym(newtype));
  988. if po_is_function_ref in tprocvardef(hdef).procoptions then
  989. begin
  990. { these always support everything, no "of object" or
  991. "is_nested" is allowed }
  992. if is_nested_pd(tprocvardef(hdef)) or
  993. is_methodpointer(hdef) then
  994. cgmessage(type_e_function_reference_kind)
  995. else
  996. begin
  997. if (po_hascallingconvention in tprocvardef(hdef).procoptions) and
  998. (tprocvardef(hdef).proccalloption in [pocall_cdecl,pocall_mwpascal]) then
  999. begin
  1000. include(tprocvardef(hdef).procoptions,po_is_block);
  1001. { can't check yet whether the parameter types
  1002. are valid for a block, since some of them
  1003. may still be forwarddefs }
  1004. end
  1005. else
  1006. { a regular anonymous function type: not yet supported }
  1007. { the }
  1008. Comment(V_Error,'Function references are not yet supported, only C blocks (add "cdecl;" at the end)');
  1009. end
  1010. end;
  1011. handle_calling_convention(tprocvardef(hdef),hcc_default_actions_intf);
  1012. if try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg) then
  1013. consume(_SEMICOLON);
  1014. end;
  1015. end;
  1016. objectdef :
  1017. begin
  1018. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  1019. consume(_SEMICOLON);
  1020. { change a forward and external class declaration into
  1021. formal external definition, so the compiler does not
  1022. expect an real definition later }
  1023. if is_objc_class_or_protocol(hdef) or
  1024. is_java_class_or_interface(hdef) then
  1025. finalize_class_external_status(tobjectdef(hdef));
  1026. { Build VMT indexes, skip for type renaming and forward classes }
  1027. if (hdef.typesym=newtype) and
  1028. not(oo_is_forward in tobjectdef(hdef).objectoptions) then
  1029. begin
  1030. vmtbuilder:=TVMTBuilder.Create(tobjectdef(hdef));
  1031. vmtbuilder.generate_vmt;
  1032. vmtbuilder.free;
  1033. end;
  1034. { In case of an objcclass, verify that all methods have a message
  1035. name set. We only check this now, because message names can be set
  1036. during the protocol (interface) mapping. At the same time, set the
  1037. mangled names (these depend on the "external" name of the class),
  1038. and mark private fields of external classes as "used" (to avoid
  1039. bogus notes about them being unused)
  1040. }
  1041. { watch out for crashes in case of errors }
  1042. if is_objc_class_or_protocol(hdef) and
  1043. (not is_objccategory(hdef) or
  1044. assigned(tobjectdef(hdef).childof)) then
  1045. begin
  1046. tobjectdef(hdef).finish_objc_data;
  1047. tobjectdef(hdef).symtable.DefList.ForEachCall(@pd_set_objc_related_result,nil);
  1048. end;
  1049. if is_cppclass(hdef) then
  1050. tobjectdef(hdef).finish_cpp_data;
  1051. end;
  1052. recorddef :
  1053. begin
  1054. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  1055. consume(_SEMICOLON);
  1056. end;
  1057. else
  1058. begin
  1059. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  1060. consume(_SEMICOLON);
  1061. end;
  1062. end;
  1063. { if we have a real type definition or a unique type we may bind
  1064. attributes to this def }
  1065. if not istyperenaming or isunique then
  1066. trtti_attribute_list.bind(rtti_attrs_def,tstoreddef(hdef).rtti_attribute_list);
  1067. end;
  1068. if isgeneric and (not(hdef.typ in [objectdef,recorddef,arraydef,procvardef])
  1069. or is_objectpascal_helper(hdef)) then
  1070. message(parser_e_cant_create_generics_of_this_type);
  1071. { Stop recording a generic template }
  1072. if assigned(generictypelist) then
  1073. begin
  1074. current_scanner.stoprecordtokens;
  1075. tstoreddef(hdef).generictokenbuf:=generictokenbuf;
  1076. { Generic is never a type renaming }
  1077. hdef.typesym:=newtype;
  1078. generictypelist.free;
  1079. end;
  1080. if not (m_delphi in current_settings.modeswitches) and
  1081. (token=_ID) and (idtoken=_GENERIC) then
  1082. begin
  1083. had_generic:=true;
  1084. consume(_ID);
  1085. if token in [_PROCEDURE,_FUNCTION,_CLASS] then
  1086. break;
  1087. end
  1088. else
  1089. had_generic:=false;
  1090. first:=false;
  1091. if assigned(rtti_attrs_def) and (rtti_attrs_def.get_attribute_count>0) then
  1092. Message1(parser_e_unbound_attribute,trtti_attribute(rtti_attrs_def.rtti_attributes[0]).typesym.prettyname);
  1093. until ((token<>_ID) and (token<>_LECKKLAMMER)) or
  1094. (in_structure and
  1095. ((idtoken in [_PRIVATE,_PROTECTED,_PUBLIC,_PUBLISHED,_STRICT]) or
  1096. ((m_final_fields in current_settings.modeswitches) and
  1097. (idtoken=_FINAL))));
  1098. { resolve type block forward declarations and restore a unit
  1099. container for them }
  1100. resolve_forward_types;
  1101. current_module.checkforwarddefs.free;
  1102. current_module.checkforwarddefs:=old_checkforwarddefs;
  1103. block_type:=old_block_type;
  1104. end;
  1105. { reads a type declaration to the symbol table }
  1106. procedure type_dec(out had_generic:boolean);
  1107. var
  1108. rtti_attrs_def: trtti_attribute_list;
  1109. begin
  1110. consume(_TYPE);
  1111. rtti_attrs_def := nil;
  1112. types_dec(false,had_generic,rtti_attrs_def);
  1113. rtti_attrs_def.free;
  1114. end;
  1115. procedure var_dec(out had_generic:boolean);
  1116. { parses variable declarations and inserts them in }
  1117. { the top symbol table of symtablestack }
  1118. begin
  1119. consume(_VAR);
  1120. read_var_decls([vd_check_generic],had_generic);
  1121. end;
  1122. procedure property_dec;
  1123. { parses a global property (fpc mode feature) }
  1124. var
  1125. old_block_type: tblock_type;
  1126. begin
  1127. consume(_PROPERTY);
  1128. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  1129. message(parser_e_property_only_sgr);
  1130. old_block_type:=block_type;
  1131. block_type:=bt_const;
  1132. repeat
  1133. read_property_dec(false, nil);
  1134. consume(_SEMICOLON);
  1135. until token<>_ID;
  1136. block_type:=old_block_type;
  1137. end;
  1138. procedure threadvar_dec(out had_generic:boolean);
  1139. { parses thread variable declarations and inserts them in }
  1140. { the top symbol table of symtablestack }
  1141. begin
  1142. consume(_THREADVAR);
  1143. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  1144. message(parser_e_threadvars_only_sg);
  1145. if f_threading in features then
  1146. read_var_decls([vd_threadvar,vd_check_generic],had_generic)
  1147. else
  1148. begin
  1149. Message1(parser_f_unsupported_feature,featurestr[f_threading]);
  1150. read_var_decls([vd_check_generic],had_generic);
  1151. end;
  1152. end;
  1153. procedure resourcestring_dec(out had_generic:boolean);
  1154. var
  1155. orgname : TIDString;
  1156. p : tnode;
  1157. dummysymoptions : tsymoptions;
  1158. deprecatedmsg : pshortstring;
  1159. storetokenpos,filepos : tfileposinfo;
  1160. old_block_type : tblock_type;
  1161. sp : pchar;
  1162. sym : tsym;
  1163. first,
  1164. isgeneric : boolean;
  1165. begin
  1166. if target_info.system in systems_managed_vm then
  1167. message(parser_e_feature_unsupported_for_vm);
  1168. consume(_RESOURCESTRING);
  1169. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  1170. message(parser_e_resourcestring_only_sg);
  1171. first:=true;
  1172. had_generic:=false;
  1173. old_block_type:=block_type;
  1174. block_type:=bt_const;
  1175. repeat
  1176. orgname:=orgpattern;
  1177. filepos:=current_tokenpos;
  1178. isgeneric:=not (m_delphi in current_settings.modeswitches) and (token=_ID) and (idtoken=_GENERIC);
  1179. consume(_ID);
  1180. case token of
  1181. _EQ:
  1182. begin
  1183. consume(_EQ);
  1184. p:=comp_expr([ef_accept_equal]);
  1185. storetokenpos:=current_tokenpos;
  1186. current_tokenpos:=filepos;
  1187. sym:=nil;
  1188. case p.nodetype of
  1189. ordconstn:
  1190. begin
  1191. if is_constcharnode(p) then
  1192. begin
  1193. getmem(sp,2);
  1194. sp[0]:=chr(tordconstnode(p).value.svalue);
  1195. sp[1]:=#0;
  1196. sym:=cconstsym.create_string(orgname,constresourcestring,sp,1,nil);
  1197. end
  1198. else
  1199. Message(parser_e_illegal_expression);
  1200. end;
  1201. stringconstn:
  1202. with Tstringconstnode(p) do
  1203. begin
  1204. { resourcestrings are currently always single byte }
  1205. if cst_type in [cst_widestring,cst_unicodestring] then
  1206. changestringtype(getansistringdef);
  1207. getmem(sp,len+1);
  1208. move(value_str^,sp^,len+1);
  1209. sym:=cconstsym.create_string(orgname,constresourcestring,sp,len,nil);
  1210. end;
  1211. else
  1212. Message(parser_e_illegal_expression);
  1213. end;
  1214. current_tokenpos:=storetokenpos;
  1215. { Support hint directives }
  1216. dummysymoptions:=[];
  1217. deprecatedmsg:=nil;
  1218. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  1219. if assigned(sym) then
  1220. begin
  1221. sym.symoptions:=sym.symoptions+dummysymoptions;
  1222. sym.deprecatedmsg:=deprecatedmsg;
  1223. symtablestack.top.insert(sym);
  1224. end
  1225. else
  1226. stringdispose(deprecatedmsg);
  1227. consume(_SEMICOLON);
  1228. p.free;
  1229. end;
  1230. else
  1231. if not first and isgeneric and
  1232. (token in [_PROCEDURE, _FUNCTION, _CLASS]) then
  1233. begin
  1234. had_generic:=true;
  1235. break;
  1236. end
  1237. else
  1238. consume(_EQ);
  1239. end;
  1240. first:=false;
  1241. until token<>_ID;
  1242. block_type:=old_block_type;
  1243. end;
  1244. end.