pdecl.pas 56 KB

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