pdecl.pas 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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. localgenerictokenbuf : tdynamicarray;
  620. p:tnode;
  621. gendef : tstoreddef;
  622. s : shortstring;
  623. i : longint;
  624. {$ifdef x86}
  625. segment_register: string;
  626. {$endif x86}
  627. begin
  628. old_block_type:=block_type;
  629. { save unit container of forward declarations -
  630. we can be inside nested class type block }
  631. old_checkforwarddefs:=current_module.checkforwarddefs;
  632. current_module.checkforwarddefs:=TFPObjectList.Create(false);
  633. block_type:=bt_type;
  634. hdef:=nil;
  635. first:=true;
  636. had_generic:=false;
  637. repeat
  638. defpos:=current_tokenpos;
  639. istyperenaming:=false;
  640. generictypelist:=nil;
  641. localgenerictokenbuf:=nil;
  642. { class attribute definitions? }
  643. if m_prefixed_attributes in current_settings.modeswitches then
  644. while token=_LECKKLAMMER do
  645. parse_rttiattributes(rtti_attrs_def);
  646. { fpc generic declaration? }
  647. if first then
  648. had_generic:=not(m_delphi in current_settings.modeswitches) and try_to_consume(_GENERIC);
  649. isgeneric:=had_generic;
  650. typename:=pattern;
  651. orgtypename:=orgpattern;
  652. consume(_ID);
  653. { delphi generic declaration? }
  654. if (m_delphi in current_settings.modeswitches) then
  655. isgeneric:=token=_LSHARPBRACKET;
  656. { Generic type declaration? }
  657. if isgeneric then
  658. begin
  659. if assigned(current_genericdef) then
  660. Message(parser_f_no_generic_inside_generic);
  661. consume(_LSHARPBRACKET);
  662. generictypelist:=parse_generic_parameters(true);
  663. consume(_RSHARPBRACKET);
  664. { we are not freeing the type parameters, so register them }
  665. for i:=0 to generictypelist.count-1 do
  666. begin
  667. ttypesym(generictypelist[i]).register_sym;
  668. tstoreddef(ttypesym(generictypelist[i]).typedef).register_def;
  669. end;
  670. str(generictypelist.Count,s);
  671. gentypename:=typename+'$'+s;
  672. genorgtypename:=orgtypename+'$'+s;
  673. end
  674. else
  675. begin
  676. gentypename:=typename;
  677. genorgtypename:=orgtypename;
  678. end;
  679. consume(_EQ);
  680. { support 'ttype=type word' syntax }
  681. isunique:=try_to_consume(_TYPE);
  682. { MacPas object model is more like Delphi's than like TP's, but }
  683. { uses the object keyword instead of class }
  684. if (m_mac in current_settings.modeswitches) and
  685. (token = _OBJECT) then
  686. token := _CLASS;
  687. { Start recording a generic template }
  688. if assigned(generictypelist) then
  689. begin
  690. localgenerictokenbuf:=tdynamicarray.create(256);
  691. current_scanner.startrecordtokens(localgenerictokenbuf);
  692. end;
  693. { is the type already defined? -- must be in the current symtable,
  694. not in a nested symtable or one higher up the stack -> don't
  695. use searchsym & frinds! }
  696. sym:=tsym(symtablestack.top.find(gentypename));
  697. newtype:=nil;
  698. { found a symbol with this name? }
  699. if assigned(sym) then
  700. begin
  701. if (sym.typ=typesym) and
  702. { this should not be a symbol that was created by a generic
  703. that was declared earlier }
  704. not (
  705. (ttypesym(sym).typedef.typ=undefineddef) and
  706. (sp_generic_dummy in sym.symoptions)
  707. ) then
  708. begin
  709. if ((token=_CLASS) or
  710. (token=_INTERFACE) or
  711. (token=_DISPINTERFACE) or
  712. (token=_OBJCCLASS) or
  713. (token=_OBJCPROTOCOL) or
  714. (token=_OBJCCATEGORY)) and
  715. (assigned(ttypesym(sym).typedef)) and
  716. is_implicit_pointer_object_type(ttypesym(sym).typedef) and
  717. (oo_is_forward in tobjectdef(ttypesym(sym).typedef).objectoptions) then
  718. begin
  719. case token of
  720. _CLASS :
  721. objecttype:=default_class_type;
  722. _INTERFACE :
  723. case current_settings.interfacetype of
  724. it_interfacecom:
  725. objecttype:=odt_interfacecom;
  726. it_interfacecorba:
  727. objecttype:=odt_interfacecorba;
  728. it_interfacejava:
  729. objecttype:=odt_interfacejava;
  730. end;
  731. _DISPINTERFACE :
  732. objecttype:=odt_dispinterface;
  733. _OBJCCLASS,
  734. _OBJCCATEGORY :
  735. objecttype:=odt_objcclass;
  736. _OBJCPROTOCOL :
  737. objecttype:=odt_objcprotocol;
  738. else
  739. internalerror(200811072);
  740. end;
  741. consume(token);
  742. { determine the generic def in case we are in a nested type
  743. of a specialization }
  744. gendef:=determine_generic_def(gentypename);
  745. { we can ignore the result, the definition is modified }
  746. object_dec(objecttype,genorgtypename,newtype,gendef,generictypelist,tobjectdef(ttypesym(sym).typedef),ht_none);
  747. newtype:=ttypesym(sym);
  748. hdef:=newtype.typedef;
  749. end
  750. else
  751. message1(parser_h_type_redef,genorgtypename);
  752. end;
  753. end;
  754. { no old type reused ? Then insert this new type }
  755. if not assigned(newtype) then
  756. begin
  757. { insert the new type first with an errordef, so that
  758. referencing the type before it's really set it
  759. will give an error (PFV) }
  760. hdef:=generrordef;
  761. gendef:=nil;
  762. storetokenpos:=current_tokenpos;
  763. if isgeneric then
  764. begin
  765. { for generics we need to check whether a non-generic type
  766. already exists and if not we need to insert a symbol with
  767. the non-generic name (available in (org)typename) that is a
  768. undefineddef, so that inline specializations can be used }
  769. sym:=tsym(symtablestack.top.Find(typename));
  770. if not assigned(sym) then
  771. begin
  772. sym:=ctypesym.create(orgtypename,cundefineddef.create(true));
  773. Include(sym.symoptions,sp_generic_dummy);
  774. ttypesym(sym).typedef.typesym:=sym;
  775. sym.visibility:=symtablestack.top.currentvisibility;
  776. symtablestack.top.insert(sym);
  777. ttypesym(sym).typedef.owner:=sym.owner;
  778. end
  779. else
  780. { this is not allowed in non-Delphi modes }
  781. if not (m_delphi in current_settings.modeswitches) then
  782. Message1(sym_e_duplicate_id,genorgtypename)
  783. else
  784. begin
  785. { we need to find this symbol even if it's a variable or
  786. something else when doing an inline specialization }
  787. Include(sym.symoptions,sp_generic_dummy);
  788. add_generic_dummysym(sym);
  789. end;
  790. end
  791. else
  792. begin
  793. if assigned(sym) and (sym.typ=typesym) and
  794. (ttypesym(sym).typedef.typ=undefineddef) and
  795. (sp_generic_dummy in sym.symoptions) then
  796. begin
  797. { this is a symbol that was added by an earlier generic
  798. declaration, reuse it }
  799. newtype:=ttypesym(sym);
  800. newtype.typedef:=hdef;
  801. { use the correct casing }
  802. newtype.RealName:=genorgtypename;
  803. sym:=nil;
  804. end;
  805. { determine the generic def in case we are in a nested type
  806. of a specialization }
  807. gendef:=determine_generic_def(gentypename);
  808. end;
  809. { insert a new type if we don't reuse an existing symbol }
  810. if not assigned(newtype) then
  811. begin
  812. newtype:=ctypesym.create(genorgtypename,hdef);
  813. newtype.visibility:=symtablestack.top.currentvisibility;
  814. symtablestack.top.insert(newtype);
  815. end;
  816. current_tokenpos:=defpos;
  817. current_tokenpos:=storetokenpos;
  818. { read the type definition }
  819. read_named_type(hdef,newtype,gendef,generictypelist,false,isunique);
  820. { update the definition of the type }
  821. if assigned(hdef) then
  822. begin
  823. if df_generic in hdef.defoptions then
  824. { flag parent symtables that they now contain a generic }
  825. hdef.owner.includeoption(sto_has_generic);
  826. if assigned(hdef.typesym) then
  827. begin
  828. istyperenaming:=true;
  829. include(newtype.symoptions,sp_explicitrename);
  830. end;
  831. if isunique then
  832. begin
  833. if is_objc_class_or_protocol(hdef) or
  834. is_java_class_or_interface(hdef) then
  835. Message(parser_e_unique_unsupported);
  836. if is_object(hdef) or
  837. is_class_or_interface_or_dispinterface(hdef) then
  838. begin
  839. { just create a child class type; this is
  840. Delphi-compatible }
  841. hdef:=cobjectdef.create(tobjectdef(hdef).objecttype,genorgtypename,tobjectdef(hdef),true);
  842. end
  843. else
  844. begin
  845. hdef:=tstoreddef(hdef).getcopy;
  846. { check if it is an ansistirng(codepage) declaration }
  847. if is_ansistring(hdef) and try_to_consume(_LKLAMMER) then
  848. begin
  849. p:=comp_expr([ef_accept_equal]);
  850. consume(_RKLAMMER);
  851. if not is_constintnode(p) then
  852. begin
  853. Message(parser_e_illegal_expression);
  854. { error recovery }
  855. end
  856. else
  857. begin
  858. if (tordconstnode(p).value<0) or (tordconstnode(p).value>65535) then
  859. begin
  860. Message(parser_e_invalid_codepage);
  861. tordconstnode(p).value:=0;
  862. end;
  863. tstringdef(hdef).encoding:=int64(tordconstnode(p).value);
  864. end;
  865. p.free;
  866. end;
  867. if (hdef.typ in [pointerdef,classrefdef]) and
  868. (tabstractpointerdef(hdef).pointeddef.typ=forwarddef) then
  869. current_module.checkforwarddefs.add(hdef);
  870. end;
  871. include(hdef.defoptions,df_unique);
  872. end;
  873. if not assigned(hdef.typesym) then
  874. begin
  875. hdef.typesym:=newtype;
  876. if sp_generic_dummy in newtype.symoptions then
  877. add_generic_dummysym(newtype);
  878. end;
  879. end;
  880. { in non-Delphi modes we need a reference to the generic def
  881. without the generic suffix, so it can be found easily when
  882. parsing method implementations }
  883. if isgeneric and assigned(sym) and
  884. not (m_delphi in current_settings.modeswitches) and
  885. (ttypesym(sym).typedef.typ=undefineddef) then
  886. { don't free the undefineddef as the defids rely on the count
  887. of the defs in the def list of the module}
  888. ttypesym(sym).typedef:=hdef;
  889. newtype.typedef:=hdef;
  890. { ensure that the type is registered when no specialization is
  891. currently done }
  892. if current_scanner.replay_stack_depth=0 then
  893. hdef.register_def;
  894. { KAZ: handle TGUID declaration in system unit }
  895. if (cs_compilesystem in current_settings.moduleswitches) and
  896. assigned(hdef) and
  897. (hdef.typ=recorddef) then
  898. begin
  899. if not assigned(rec_tguid) and
  900. (gentypename='TGUID') and
  901. (hdef.size=16) then
  902. rec_tguid:=trecorddef(hdef)
  903. else if not assigned(rec_jmp_buf) and
  904. (gentypename='JMP_BUF') then
  905. rec_jmp_buf:=trecorddef(hdef)
  906. else if not assigned(rec_exceptaddr) and
  907. (gentypename='TEXCEPTADDR') then
  908. rec_exceptaddr:=trecorddef(hdef);
  909. end;
  910. end;
  911. if assigned(hdef) then
  912. begin
  913. case hdef.typ of
  914. pointerdef :
  915. begin
  916. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  917. consume(_SEMICOLON);
  918. {$ifdef x86}
  919. {$ifdef i8086}
  920. if try_to_consume(_HUGE) then
  921. begin
  922. tcpupointerdef(hdef).x86pointertyp:=x86pt_huge;
  923. consume(_SEMICOLON);
  924. end
  925. else
  926. {$endif i8086}
  927. if try_to_consume(_FAR) then
  928. begin
  929. {$if defined(i8086)}
  930. tcpupointerdef(hdef).x86pointertyp:=x86pt_far;
  931. {$elseif defined(i386)}
  932. tcpupointerdef(hdef).x86pointertyp:=x86pt_near_fs;
  933. {$elseif defined(x86_64)}
  934. { for compatibility with previous versions of fpc,
  935. far pointer = regular pointer on x86_64 }
  936. Message1(parser_w_ptr_type_ignored,'FAR');
  937. {$endif}
  938. consume(_SEMICOLON);
  939. end
  940. else
  941. if try_to_consume(_NEAR) then
  942. begin
  943. if token <> _SEMICOLON then
  944. begin
  945. segment_register:=get_stringconst;
  946. case UpCase(segment_register) of
  947. 'CS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_cs;
  948. 'DS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_ds;
  949. 'SS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_ss;
  950. 'ES': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_es;
  951. 'FS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_fs;
  952. 'GS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_gs;
  953. else
  954. Message(asmr_e_invalid_register);
  955. end;
  956. end
  957. else
  958. tcpupointerdef(hdef).x86pointertyp:=x86pt_near;
  959. consume(_SEMICOLON);
  960. end;
  961. {$else x86}
  962. { Previous versions of FPC support declaring a pointer as
  963. far even on non-x86 platforms. }
  964. if try_to_consume(_FAR) then
  965. begin
  966. Message1(parser_w_ptr_type_ignored,'FAR');
  967. consume(_SEMICOLON);
  968. end;
  969. {$endif x86}
  970. end;
  971. procvardef :
  972. begin
  973. { in case of type renaming, don't parse proc directives }
  974. if istyperenaming then
  975. begin
  976. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  977. consume(_SEMICOLON);
  978. end
  979. else
  980. begin
  981. if not check_proc_directive(true) then
  982. begin
  983. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  984. consume(_SEMICOLON);
  985. end;
  986. parse_var_proc_directives(tsym(newtype));
  987. if po_is_function_ref in tprocvardef(hdef).procoptions then
  988. begin
  989. { these always support everything, no "of object" or
  990. "is_nested" is allowed }
  991. if is_nested_pd(tprocvardef(hdef)) or
  992. is_methodpointer(hdef) then
  993. cgmessage(type_e_function_reference_kind)
  994. else
  995. begin
  996. { this message is only temporary; once Delphi style anonymous functions
  997. are supported, this check is no longer required }
  998. if not (po_is_block in tprocvardef(hdef).procoptions) then
  999. comment(v_error,'Function references are not yet supported, only C blocks (add "cblock;" at the end)');
  1000. end;
  1001. end;
  1002. handle_calling_convention(tprocvardef(hdef),hcc_default_actions_intf);
  1003. if po_is_function_ref in tprocvardef(hdef).procoptions then
  1004. begin
  1005. if (po_is_block in tprocvardef(hdef).procoptions) and
  1006. not (tprocvardef(hdef).proccalloption in [pocall_cdecl,pocall_mwpascal]) then
  1007. message(type_e_cblock_callconv);
  1008. end;
  1009. if try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg) then
  1010. consume(_SEMICOLON);
  1011. end;
  1012. end;
  1013. objectdef :
  1014. begin
  1015. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  1016. consume(_SEMICOLON);
  1017. { change a forward and external class declaration into
  1018. formal external definition, so the compiler does not
  1019. expect an real definition later }
  1020. if is_objc_class_or_protocol(hdef) or
  1021. is_java_class_or_interface(hdef) then
  1022. finalize_class_external_status(tobjectdef(hdef));
  1023. { Build VMT indexes, skip for type renaming and forward classes }
  1024. if (hdef.typesym=newtype) and
  1025. not(oo_is_forward in tobjectdef(hdef).objectoptions) then
  1026. build_vmt(tobjectdef(hdef));
  1027. { In case of an objcclass, verify that all methods have a message
  1028. name set. We only check this now, because message names can be set
  1029. during the protocol (interface) mapping. At the same time, set the
  1030. mangled names (these depend on the "external" name of the class),
  1031. and mark private fields of external classes as "used" (to avoid
  1032. bogus notes about them being unused)
  1033. }
  1034. { watch out for crashes in case of errors }
  1035. if is_objc_class_or_protocol(hdef) and
  1036. (not is_objccategory(hdef) or
  1037. assigned(tobjectdef(hdef).childof)) then
  1038. begin
  1039. tobjectdef(hdef).finish_objc_data;
  1040. tobjectdef(hdef).symtable.DefList.ForEachCall(@pd_set_objc_related_result,nil);
  1041. end;
  1042. if is_cppclass(hdef) then
  1043. tobjectdef(hdef).finish_cpp_data;
  1044. end;
  1045. recorddef :
  1046. begin
  1047. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  1048. consume(_SEMICOLON);
  1049. end;
  1050. else
  1051. begin
  1052. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  1053. consume(_SEMICOLON);
  1054. end;
  1055. end;
  1056. { if we have a real type definition or a unique type we may bind
  1057. attributes to this def }
  1058. if not istyperenaming or isunique then
  1059. trtti_attribute_list.bind(rtti_attrs_def,tstoreddef(hdef).rtti_attribute_list);
  1060. end;
  1061. if isgeneric and (not(hdef.typ in [objectdef,recorddef,arraydef,procvardef])
  1062. or is_objectpascal_helper(hdef)) then
  1063. message(parser_e_cant_create_generics_of_this_type);
  1064. { Stop recording a generic template }
  1065. if assigned(generictypelist) then
  1066. begin
  1067. current_scanner.stoprecordtokens;
  1068. tstoreddef(hdef).generictokenbuf:=localgenerictokenbuf;
  1069. { Generic is never a type renaming }
  1070. hdef.typesym:=newtype;
  1071. generictypelist.free;
  1072. end;
  1073. if not (m_delphi in current_settings.modeswitches) and
  1074. (token=_ID) and (idtoken=_GENERIC) then
  1075. begin
  1076. had_generic:=true;
  1077. consume(_ID);
  1078. if token in [_PROCEDURE,_FUNCTION,_CLASS] then
  1079. break;
  1080. end
  1081. else
  1082. had_generic:=false;
  1083. first:=false;
  1084. if assigned(rtti_attrs_def) and (rtti_attrs_def.get_attribute_count>0) then
  1085. Message1(parser_e_unbound_attribute,trtti_attribute(rtti_attrs_def.rtti_attributes[0]).typesym.prettyname);
  1086. until ((token<>_ID) and (token<>_LECKKLAMMER)) or
  1087. (in_structure and
  1088. ((idtoken in [_PRIVATE,_PROTECTED,_PUBLIC,_PUBLISHED,_STRICT]) or
  1089. ((m_final_fields in current_settings.modeswitches) and
  1090. (idtoken=_FINAL))));
  1091. { resolve type block forward declarations and restore a unit
  1092. container for them }
  1093. resolve_forward_types;
  1094. current_module.checkforwarddefs.free;
  1095. current_module.checkforwarddefs:=old_checkforwarddefs;
  1096. block_type:=old_block_type;
  1097. end;
  1098. { reads a type declaration to the symbol table }
  1099. procedure type_dec(out had_generic:boolean);
  1100. var
  1101. rtti_attrs_def: trtti_attribute_list;
  1102. begin
  1103. consume(_TYPE);
  1104. rtti_attrs_def := nil;
  1105. types_dec(false,had_generic,rtti_attrs_def);
  1106. rtti_attrs_def.free;
  1107. end;
  1108. procedure var_dec(out had_generic:boolean);
  1109. { parses variable declarations and inserts them in }
  1110. { the top symbol table of symtablestack }
  1111. begin
  1112. consume(_VAR);
  1113. read_var_decls([vd_check_generic],had_generic);
  1114. end;
  1115. procedure property_dec;
  1116. { parses a global property (fpc mode feature) }
  1117. var
  1118. old_block_type: tblock_type;
  1119. begin
  1120. consume(_PROPERTY);
  1121. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  1122. message(parser_e_property_only_sgr);
  1123. old_block_type:=block_type;
  1124. block_type:=bt_const;
  1125. repeat
  1126. read_property_dec(false, nil);
  1127. consume(_SEMICOLON);
  1128. until token<>_ID;
  1129. block_type:=old_block_type;
  1130. end;
  1131. procedure threadvar_dec(out had_generic:boolean);
  1132. { parses thread variable declarations and inserts them in }
  1133. { the top symbol table of symtablestack }
  1134. begin
  1135. consume(_THREADVAR);
  1136. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  1137. message(parser_e_threadvars_only_sg);
  1138. if f_threading in features then
  1139. read_var_decls([vd_threadvar,vd_check_generic],had_generic)
  1140. else
  1141. begin
  1142. Message1(parser_f_unsupported_feature,featurestr[f_threading]);
  1143. read_var_decls([vd_check_generic],had_generic);
  1144. end;
  1145. end;
  1146. procedure resourcestring_dec(out had_generic:boolean);
  1147. var
  1148. orgname : TIDString;
  1149. p : tnode;
  1150. dummysymoptions : tsymoptions;
  1151. deprecatedmsg : pshortstring;
  1152. storetokenpos,filepos : tfileposinfo;
  1153. old_block_type : tblock_type;
  1154. sp : pchar;
  1155. sym : tsym;
  1156. first,
  1157. isgeneric : boolean;
  1158. begin
  1159. if target_info.system in systems_managed_vm then
  1160. message(parser_e_feature_unsupported_for_vm);
  1161. consume(_RESOURCESTRING);
  1162. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  1163. message(parser_e_resourcestring_only_sg);
  1164. first:=true;
  1165. had_generic:=false;
  1166. old_block_type:=block_type;
  1167. block_type:=bt_const;
  1168. repeat
  1169. orgname:=orgpattern;
  1170. filepos:=current_tokenpos;
  1171. isgeneric:=not (m_delphi in current_settings.modeswitches) and (token=_ID) and (idtoken=_GENERIC);
  1172. consume(_ID);
  1173. case token of
  1174. _EQ:
  1175. begin
  1176. consume(_EQ);
  1177. p:=comp_expr([ef_accept_equal]);
  1178. storetokenpos:=current_tokenpos;
  1179. current_tokenpos:=filepos;
  1180. sym:=nil;
  1181. case p.nodetype of
  1182. ordconstn:
  1183. begin
  1184. if is_constcharnode(p) then
  1185. begin
  1186. getmem(sp,2);
  1187. sp[0]:=chr(tordconstnode(p).value.svalue);
  1188. sp[1]:=#0;
  1189. sym:=cconstsym.create_string(orgname,constresourcestring,sp,1,nil);
  1190. end
  1191. else
  1192. Message(parser_e_illegal_expression);
  1193. end;
  1194. stringconstn:
  1195. with Tstringconstnode(p) do
  1196. begin
  1197. { resourcestrings are currently always single byte }
  1198. if cst_type in [cst_widestring,cst_unicodestring] then
  1199. changestringtype(getansistringdef);
  1200. getmem(sp,len+1);
  1201. move(value_str^,sp^,len+1);
  1202. sym:=cconstsym.create_string(orgname,constresourcestring,sp,len,nil);
  1203. end;
  1204. else
  1205. Message(parser_e_illegal_expression);
  1206. end;
  1207. current_tokenpos:=storetokenpos;
  1208. { Support hint directives }
  1209. dummysymoptions:=[];
  1210. deprecatedmsg:=nil;
  1211. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  1212. if assigned(sym) then
  1213. begin
  1214. sym.symoptions:=sym.symoptions+dummysymoptions;
  1215. sym.deprecatedmsg:=deprecatedmsg;
  1216. symtablestack.top.insert(sym);
  1217. end
  1218. else
  1219. stringdispose(deprecatedmsg);
  1220. consume(_SEMICOLON);
  1221. p.free;
  1222. end;
  1223. else
  1224. if not first and isgeneric and
  1225. (token in [_PROCEDURE, _FUNCTION, _CLASS]) then
  1226. begin
  1227. had_generic:=true;
  1228. break;
  1229. end
  1230. else
  1231. consume(_EQ);
  1232. end;
  1233. first:=false;
  1234. until token<>_ID;
  1235. block_type:=old_block_type;
  1236. end;
  1237. end.