pdecl.pas 55 KB

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