pdecl.pas 56 KB

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