pdecl.pas 55 KB

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