pdecl.pas 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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. hdef:=hdef2;
  855. end
  856. else
  857. begin
  858. hdef:=tstoreddef(hdef).getcopy;
  859. { check if it is an ansistirng(codepage) declaration }
  860. if is_ansistring(hdef) and try_to_consume(_LKLAMMER) then
  861. begin
  862. p:=comp_expr([ef_accept_equal]);
  863. consume(_RKLAMMER);
  864. if not is_constintnode(p) then
  865. begin
  866. Message(parser_e_illegal_expression);
  867. { error recovery }
  868. end
  869. else
  870. begin
  871. if (tordconstnode(p).value<0) or (tordconstnode(p).value>65535) then
  872. begin
  873. Message(parser_e_invalid_codepage);
  874. tordconstnode(p).value:=0;
  875. end;
  876. tstringdef(hdef).encoding:=int64(tordconstnode(p).value);
  877. end;
  878. p.free;
  879. end;
  880. if (hdef.typ in [pointerdef,classrefdef]) and
  881. (tabstractpointerdef(hdef).pointeddef.typ=forwarddef) then
  882. current_module.checkforwarddefs.add(hdef);
  883. end;
  884. include(hdef.defoptions,df_unique);
  885. end;
  886. if not assigned(hdef.typesym) then
  887. begin
  888. hdef.typesym:=newtype;
  889. if sp_generic_dummy in newtype.symoptions then
  890. add_generic_dummysym(newtype);
  891. end;
  892. end;
  893. { in non-Delphi modes we need a reference to the generic def
  894. without the generic suffix, so it can be found easily when
  895. parsing method implementations }
  896. if isgeneric and assigned(sym) and
  897. not (m_delphi in current_settings.modeswitches) and
  898. (ttypesym(sym).typedef.typ=undefineddef) then
  899. begin
  900. { don't free the undefineddef as the defids rely on the count
  901. of the defs in the def list of the module}
  902. ttypesym(sym).typedef:=hdef;
  903. setdummysym:=true;
  904. end;
  905. newtype.typedef:=hdef;
  906. { ensure that the type is registered when no specialization is
  907. currently done }
  908. if (current_scanner.replay_stack_depth=0) and
  909. (
  910. (hdef.typ<>procvardef) or
  911. not (po_is_function_ref in tabstractprocdef(hdef).procoptions)
  912. ) then
  913. hdef.register_def;
  914. { KAZ: handle TGUID declaration in system unit }
  915. if (cs_compilesystem in current_settings.moduleswitches) and
  916. assigned(hdef) and
  917. (hdef.typ=recorddef) then
  918. begin
  919. if not assigned(rec_tguid) and
  920. (gentypename='TGUID') and
  921. (hdef.size=16) then
  922. rec_tguid:=trecorddef(hdef)
  923. else if not assigned(rec_jmp_buf) and
  924. (gentypename='JMP_BUF') then
  925. rec_jmp_buf:=trecorddef(hdef)
  926. else if not assigned(rec_exceptaddr) and
  927. (gentypename='TEXCEPTADDR') then
  928. rec_exceptaddr:=trecorddef(hdef);
  929. end;
  930. end;
  931. if assigned(hdef) then
  932. begin
  933. case hdef.typ of
  934. pointerdef :
  935. begin
  936. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  937. consume(_SEMICOLON);
  938. {$ifdef x86}
  939. {$ifdef i8086}
  940. if try_to_consume(_HUGE) then
  941. begin
  942. tcpupointerdef(hdef).x86pointertyp:=x86pt_huge;
  943. consume(_SEMICOLON);
  944. end
  945. else
  946. {$endif i8086}
  947. if try_to_consume(_FAR) then
  948. begin
  949. {$if defined(i8086)}
  950. tcpupointerdef(hdef).x86pointertyp:=x86pt_far;
  951. {$elseif defined(i386)}
  952. tcpupointerdef(hdef).x86pointertyp:=x86pt_near_fs;
  953. {$elseif defined(x86_64)}
  954. { for compatibility with previous versions of fpc,
  955. far pointer = regular pointer on x86_64 }
  956. Message1(parser_w_ptr_type_ignored,'FAR');
  957. {$endif}
  958. consume(_SEMICOLON);
  959. end
  960. else
  961. if try_to_consume(_NEAR) then
  962. begin
  963. if token <> _SEMICOLON then
  964. begin
  965. segment_register:=get_stringconst;
  966. case UpCase(segment_register) of
  967. 'CS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_cs;
  968. 'DS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_ds;
  969. 'SS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_ss;
  970. 'ES': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_es;
  971. 'FS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_fs;
  972. 'GS': tcpupointerdef(hdef).x86pointertyp:=x86pt_near_gs;
  973. else
  974. Message(asmr_e_invalid_register);
  975. end;
  976. end
  977. else
  978. tcpupointerdef(hdef).x86pointertyp:=x86pt_near;
  979. consume(_SEMICOLON);
  980. end;
  981. {$else x86}
  982. { Previous versions of FPC support declaring a pointer as
  983. far even on non-x86 platforms. }
  984. if try_to_consume(_FAR) then
  985. begin
  986. Message1(parser_w_ptr_type_ignored,'FAR');
  987. consume(_SEMICOLON);
  988. end;
  989. {$endif x86}
  990. end;
  991. procvardef :
  992. begin
  993. { in case of type renaming, don't parse proc directives }
  994. if istyperenaming then
  995. begin
  996. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  997. consume(_SEMICOLON);
  998. end
  999. else
  1000. begin
  1001. if not check_proc_directive(true) then
  1002. begin
  1003. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  1004. consume(_SEMICOLON);
  1005. end;
  1006. parse_proctype_directives(tprocvardef(hdef));
  1007. if po_is_function_ref in tprocvardef(hdef).procoptions then
  1008. begin
  1009. if not (m_function_references in current_settings.modeswitches) and
  1010. not (po_is_block in tprocvardef(hdef).procoptions) then
  1011. messagepos(storetokenpos,sym_e_error_in_type_def)
  1012. else
  1013. begin
  1014. if setdummysym then
  1015. dummysym:=sym
  1016. else
  1017. dummysym:=nil;
  1018. adjust_funcref(hdef,newtype,dummysym);
  1019. end;
  1020. if current_scanner.replay_stack_depth=0 then
  1021. hdef.register_def;
  1022. end;
  1023. if hdef.typ=procvardef then
  1024. flags:=hcc_default_actions_intf
  1025. else
  1026. flags:=hcc_default_actions_intf_struct;
  1027. handle_calling_convention(hdef,flags);
  1028. if (hdef.typ=procvardef) and (po_is_function_ref in tprocvardef(hdef).procoptions) then
  1029. begin
  1030. if (po_is_block in tprocvardef(hdef).procoptions) and
  1031. not (tprocvardef(hdef).proccalloption in [pocall_cdecl,pocall_mwpascal]) then
  1032. message(type_e_cblock_callconv);
  1033. end;
  1034. if try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg) then
  1035. consume(_SEMICOLON);
  1036. end;
  1037. end;
  1038. objectdef :
  1039. begin
  1040. if is_funcref(hdef) then
  1041. begin
  1042. if not check_proc_directive(true) then
  1043. begin
  1044. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  1045. consume(_SEMICOLON);
  1046. end;
  1047. parse_proctype_directives(hdef);
  1048. if try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg) then
  1049. consume(_SEMICOLON);
  1050. end
  1051. else
  1052. begin
  1053. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  1054. consume(_SEMICOLON);
  1055. end;
  1056. { change a forward and external class declaration into
  1057. formal external definition, so the compiler does not
  1058. expect an real definition later }
  1059. if is_objc_class_or_protocol(hdef) or
  1060. is_java_class_or_interface(hdef) then
  1061. finalize_class_external_status(tobjectdef(hdef));
  1062. { Build VMT indexes, skip for type renaming and forward classes }
  1063. if not istyperenaming and
  1064. not(oo_is_forward in tobjectdef(hdef).objectoptions) then
  1065. build_vmt(tobjectdef(hdef));
  1066. { In case of an objcclass, verify that all methods have a message
  1067. name set. We only check this now, because message names can be set
  1068. during the protocol (interface) mapping. At the same time, set the
  1069. mangled names (these depend on the "external" name of the class),
  1070. and mark private fields of external classes as "used" (to avoid
  1071. bogus notes about them being unused)
  1072. }
  1073. { watch out for crashes in case of errors }
  1074. if is_objc_class_or_protocol(hdef) and
  1075. (not is_objccategory(hdef) or
  1076. assigned(tobjectdef(hdef).childof)) then
  1077. begin
  1078. tobjectdef(hdef).finish_objc_data;
  1079. tobjectdef(hdef).symtable.DefList.ForEachCall(@pd_set_objc_related_result,nil);
  1080. end;
  1081. if is_cppclass(hdef) then
  1082. tobjectdef(hdef).finish_cpp_data;
  1083. end;
  1084. recorddef :
  1085. begin
  1086. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  1087. consume(_SEMICOLON);
  1088. end;
  1089. else
  1090. begin
  1091. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  1092. consume(_SEMICOLON);
  1093. end;
  1094. end;
  1095. { if we have a real type definition or a unique type we may bind
  1096. attributes to this def }
  1097. if not istyperenaming or isunique then
  1098. trtti_attribute_list.bind(rtti_attrs_def,tstoreddef(hdef).rtti_attribute_list);
  1099. if df_generic in hdef.defoptions then
  1100. { flag parent symtables that they now contain a generic }
  1101. hdef.owner.includeoption(sto_has_generic);
  1102. end;
  1103. if isgeneric and (not(hdef.typ in [objectdef,recorddef,arraydef,procvardef])
  1104. or is_objectpascal_helper(hdef)) then
  1105. begin
  1106. newtype.typedef:=generrordef;
  1107. message(parser_e_cant_create_generics_of_this_type);
  1108. end;
  1109. { Stop recording a generic template }
  1110. if assigned(generictypelist) then
  1111. begin
  1112. current_scanner.stoprecordtokens;
  1113. tstoreddef(hdef).generictokenbuf:=localgenerictokenbuf;
  1114. { Generic is never a type renaming }
  1115. hdef.typesym:=newtype;
  1116. { reusing a forward declared type also reuses the type parameters,
  1117. so free them if they haven't been used }
  1118. for i:=0 to generictypelist.count-1 do
  1119. begin
  1120. if (tstoredsym(generictypelist[i]).typ=typesym) and
  1121. not ttypesym(generictypelist[i]).typedef.is_registered then
  1122. ttypesym(generictypelist[i]).typedef.free;
  1123. if not tstoredsym(generictypelist[i]).is_registered then
  1124. tstoredsym(generictypelist[i]).free;
  1125. end;
  1126. generictypelist.free;
  1127. end;
  1128. if not (m_delphi in current_settings.modeswitches) and
  1129. (token=_ID) and (idtoken=_GENERIC) then
  1130. begin
  1131. had_generic:=true;
  1132. consume(_ID);
  1133. if token in [_PROCEDURE,_FUNCTION,_CLASS] then
  1134. break;
  1135. end
  1136. else
  1137. had_generic:=false;
  1138. first:=false;
  1139. if assigned(rtti_attrs_def) and (rtti_attrs_def.get_attribute_count>0) then
  1140. Message1(parser_e_unbound_attribute,trtti_attribute(rtti_attrs_def.rtti_attributes[0]).typesym.prettyname);
  1141. {$ifdef DEBUG_NODE_XML}
  1142. if Assigned(hdef) then
  1143. hdef.XMLPrintDef(newtype);
  1144. {$endif DEBUG_NODE_XML}
  1145. until ((token<>_ID) and (token<>_LECKKLAMMER)) or
  1146. (in_structure and
  1147. ((idtoken in [_PRIVATE,_PROTECTED,_PUBLIC,_PUBLISHED,_STRICT]) or
  1148. ((m_final_fields in current_settings.modeswitches) and
  1149. (idtoken=_FINAL))));
  1150. { resolve type block forward declarations and restore a unit
  1151. container for them }
  1152. resolve_forward_types;
  1153. current_module.checkforwarddefs.free;
  1154. current_module.checkforwarddefs:=old_checkforwarddefs;
  1155. block_type:=old_block_type;
  1156. end;
  1157. { reads a type declaration to the symbol table }
  1158. procedure type_dec(out had_generic:boolean);
  1159. var
  1160. rtti_attrs_def: trtti_attribute_list;
  1161. begin
  1162. consume(_TYPE);
  1163. rtti_attrs_def := nil;
  1164. types_dec(false,had_generic,rtti_attrs_def);
  1165. rtti_attrs_def.free;
  1166. end;
  1167. procedure var_dec(out had_generic:boolean);
  1168. { parses variable declarations and inserts them in }
  1169. { the top symbol table of symtablestack }
  1170. begin
  1171. consume(_VAR);
  1172. read_var_decls([vd_check_generic],had_generic);
  1173. end;
  1174. procedure property_dec;
  1175. { parses a global property (fpc mode feature) }
  1176. var
  1177. old_block_type: tblock_type;
  1178. begin
  1179. consume(_PROPERTY);
  1180. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  1181. message(parser_e_property_only_sgr);
  1182. old_block_type:=block_type;
  1183. block_type:=bt_const;
  1184. repeat
  1185. read_property_dec(false, nil);
  1186. consume(_SEMICOLON);
  1187. until token<>_ID;
  1188. block_type:=old_block_type;
  1189. end;
  1190. procedure threadvar_dec(out had_generic:boolean);
  1191. { parses thread variable declarations and inserts them in }
  1192. { the top symbol table of symtablestack }
  1193. begin
  1194. consume(_THREADVAR);
  1195. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  1196. message(parser_e_threadvars_only_sg);
  1197. if f_threading in features then
  1198. read_var_decls([vd_threadvar,vd_check_generic],had_generic)
  1199. else
  1200. begin
  1201. Message1(parser_f_unsupported_feature,featurestr[f_threading]);
  1202. read_var_decls([vd_check_generic],had_generic);
  1203. end;
  1204. end;
  1205. procedure resourcestring_dec(out had_generic:boolean);
  1206. var
  1207. orgname : TIDString;
  1208. p : tnode;
  1209. dummysymoptions : tsymoptions;
  1210. deprecatedmsg : pshortstring;
  1211. storetokenpos,filepos : tfileposinfo;
  1212. old_block_type : tblock_type;
  1213. sp : pchar;
  1214. sym : tsym;
  1215. first,
  1216. isgeneric : boolean;
  1217. begin
  1218. if target_info.system in systems_managed_vm then
  1219. message(parser_e_feature_unsupported_for_vm);
  1220. consume(_RESOURCESTRING);
  1221. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  1222. message(parser_e_resourcestring_only_sg);
  1223. first:=true;
  1224. had_generic:=false;
  1225. old_block_type:=block_type;
  1226. block_type:=bt_const;
  1227. repeat
  1228. orgname:=orgpattern;
  1229. filepos:=current_tokenpos;
  1230. isgeneric:=not (m_delphi in current_settings.modeswitches) and (token=_ID) and (idtoken=_GENERIC);
  1231. consume(_ID);
  1232. case token of
  1233. _EQ:
  1234. begin
  1235. consume(_EQ);
  1236. p:=comp_expr([ef_accept_equal]);
  1237. storetokenpos:=current_tokenpos;
  1238. current_tokenpos:=filepos;
  1239. sym:=nil;
  1240. case p.nodetype of
  1241. ordconstn:
  1242. begin
  1243. if is_constcharnode(p) then
  1244. begin
  1245. getmem(sp,2);
  1246. sp[0]:=chr(tordconstnode(p).value.svalue);
  1247. sp[1]:=#0;
  1248. sym:=cconstsym.create_string(orgname,constresourcestring,sp,1,nil);
  1249. end
  1250. else
  1251. Message(parser_e_illegal_expression);
  1252. end;
  1253. stringconstn:
  1254. with Tstringconstnode(p) do
  1255. begin
  1256. { resourcestrings are currently always single byte }
  1257. if cst_type in [cst_widestring,cst_unicodestring] then
  1258. changestringtype(getansistringdef);
  1259. getmem(sp,len+1);
  1260. move(value_str^,sp^,len+1);
  1261. sym:=cconstsym.create_string(orgname,constresourcestring,sp,len,nil);
  1262. end;
  1263. else
  1264. Message(parser_e_illegal_expression);
  1265. end;
  1266. current_tokenpos:=storetokenpos;
  1267. { Support hint directives }
  1268. dummysymoptions:=[];
  1269. deprecatedmsg:=nil;
  1270. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  1271. if assigned(sym) then
  1272. begin
  1273. sym.symoptions:=sym.symoptions+dummysymoptions;
  1274. sym.deprecatedmsg:=deprecatedmsg;
  1275. symtablestack.top.insertsym(sym);
  1276. end
  1277. else
  1278. stringdispose(deprecatedmsg);
  1279. consume(_SEMICOLON);
  1280. p.free;
  1281. end;
  1282. else
  1283. if not first and isgeneric and
  1284. (token in [_PROCEDURE, _FUNCTION, _CLASS]) then
  1285. begin
  1286. had_generic:=true;
  1287. break;
  1288. end
  1289. else
  1290. consume(_EQ);
  1291. end;
  1292. first:=false;
  1293. until token<>_ID;
  1294. block_type:=old_block_type;
  1295. end;
  1296. end.