pdecobj.pas 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does object types 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 pdecobj;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,symconst,symtype,symdef;
  23. { parses a object declaration }
  24. function object_dec(objecttype:tobjecttyp;const n:tidstring;objsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist;fd : tobjectdef;helpertype:thelpertype) : tobjectdef;
  25. { parses a (class) method declaration }
  26. function method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  27. function class_constructor_head(astruct: tabstractrecorddef):tprocdef;
  28. function class_destructor_head(astruct: tabstractrecorddef):tprocdef;
  29. function constructor_head:tprocdef;
  30. function destructor_head:tprocdef;
  31. procedure struct_property_dec(is_classproperty:boolean;var rtti_attrs_def: trtti_attribute_list);
  32. implementation
  33. uses
  34. sysutils,cutils,
  35. globals,verbose,systems,tokens,
  36. symbase,symsym,symtable,symcreat,defcmp,
  37. node,ncon,
  38. fmodule,scanner,
  39. pbase,pexpr,pdecsub,pdecvar,ptype,pdecl,pgenutil,pparautl,ppu
  40. {$ifdef jvm}
  41. ,jvmdef,pjvm;
  42. {$else}
  43. ;
  44. {$endif}
  45. const
  46. { Please leave this here, this module should NOT use
  47. these variables.
  48. Declaring it as string here results in an error when compiling (PFV) }
  49. current_procinfo = 'error';
  50. var
  51. current_objectdef : tobjectdef absolute current_structdef;
  52. procedure constr_destr_finish_head(pd: tprocdef; const astruct: tabstractrecorddef);
  53. begin
  54. case astruct.typ of
  55. recorddef:
  56. begin
  57. parse_record_proc_directives(pd);
  58. end;
  59. objectdef:
  60. begin
  61. parse_object_proc_directives(pd);
  62. end
  63. else
  64. internalerror(2011040502);
  65. end;
  66. // We can't add hidden params here because record is not yet defined
  67. // and therefore record size which has influence on parameter passing rules may change too
  68. // look at record_dec to see where calling conventions are applied (issue #0021044).
  69. // The same goes for objects/classes due to the calling convention that may only be set
  70. // later (mantis #35233).
  71. handle_calling_convention(pd,hcc_default_actions_intf_struct);
  72. { add definition to procsym }
  73. proc_add_definition(pd);
  74. { add procdef options to objectdef options }
  75. if (po_virtualmethod in pd.procoptions) then
  76. include(astruct.objectoptions,oo_has_virtual);
  77. maybe_parse_hint_directives(pd);
  78. end;
  79. function class_constructor_head(astruct: tabstractrecorddef):tprocdef;
  80. var
  81. pd : tprocdef;
  82. begin
  83. result:=nil;
  84. consume(_CONSTRUCTOR);
  85. { must be at same level as in implementation }
  86. parse_proc_head(current_structdef,potype_class_constructor,[],nil,nil,pd);
  87. if not assigned(pd) then
  88. begin
  89. consume(_SEMICOLON);
  90. exit;
  91. end;
  92. pd.calcparas;
  93. if (pd.maxparacount>0) then
  94. Message(parser_e_no_paras_for_class_constructor);
  95. consume(_SEMICOLON);
  96. include(astruct.objectoptions,oo_has_class_constructor);
  97. include(current_module.moduleflags,mf_classinits);
  98. { no return value }
  99. pd.returndef:=voidtype;
  100. constr_destr_finish_head(pd,astruct);
  101. result:=pd;
  102. end;
  103. function constructor_head:tprocdef;
  104. var
  105. pd : tprocdef;
  106. begin
  107. result:=nil;
  108. consume(_CONSTRUCTOR);
  109. { must be at same level as in implementation }
  110. parse_proc_head(current_structdef,potype_constructor,[],nil,nil,pd);
  111. if not assigned(pd) then
  112. begin
  113. consume(_SEMICOLON);
  114. exit;
  115. end;
  116. if (cs_constructor_name in current_settings.globalswitches) and
  117. (pd.procsym.name<>'INIT') then
  118. Message(parser_e_constructorname_must_be_init);
  119. consume(_SEMICOLON);
  120. include(current_structdef.objectoptions,oo_has_constructor);
  121. { Set return type, class and record constructors return the
  122. created instance, helper types return the extended type,
  123. object constructors return boolean }
  124. if is_class(pd.struct) or
  125. is_record(pd.struct) or
  126. is_javaclass(pd.struct) then
  127. pd.returndef:=pd.struct
  128. else
  129. if is_objectpascal_helper(pd.struct) then
  130. pd.returndef:=tobjectdef(pd.struct).extendeddef
  131. else
  132. {$ifdef CPU64bitaddr}
  133. pd.returndef:=bool64type;
  134. {$else CPU64bitaddr}
  135. pd.returndef:=bool32type;
  136. {$endif CPU64bitaddr}
  137. constr_destr_finish_head(pd,pd.struct);
  138. result:=pd;
  139. end;
  140. procedure struct_property_dec(is_classproperty:boolean;var rtti_attrs_def: trtti_attribute_list);
  141. var
  142. p : tpropertysym;
  143. _deprecatedmsg: pshortstring;
  144. _symoptions: tsymoptions;
  145. begin
  146. { check for a class, record or helper }
  147. if not((is_class_or_interface_or_dispinterface(current_structdef) or is_record(current_structdef) or
  148. is_objectpascal_helper(current_structdef) or is_java_class_or_interface(current_structdef)) or
  149. (not(m_tp7 in current_settings.modeswitches) and (is_object(current_structdef)))) then
  150. Message(parser_e_syntax_error);
  151. consume(_PROPERTY);
  152. p:=read_property_dec(is_classproperty,current_structdef);
  153. consume(_SEMICOLON);
  154. if try_to_consume(_DEFAULT) then
  155. begin
  156. if oo_has_default_property in current_structdef.objectoptions then
  157. message(parser_e_only_one_default_property);
  158. include(current_structdef.objectoptions,oo_has_default_property);
  159. include(p.propoptions,ppo_defaultproperty);
  160. if not(ppo_hasparameters in p.propoptions) then
  161. message(parser_e_property_need_paras);
  162. if (token=_COLON) then
  163. begin
  164. Message(parser_e_field_not_allowed_here);
  165. consume_all_until(_SEMICOLON);
  166. end;
  167. consume(_SEMICOLON);
  168. end;
  169. { parse possible enumerator modifier }
  170. if try_to_consume(_ENUMERATOR) then
  171. begin
  172. if (token = _ID) then
  173. begin
  174. if pattern='CURRENT' then
  175. begin
  176. if oo_has_enumerator_current in current_structdef.objectoptions then
  177. message(parser_e_only_one_enumerator_current);
  178. if not p.propaccesslist[palt_read].empty then
  179. begin
  180. include(current_structdef.objectoptions,oo_has_enumerator_current);
  181. include(p.propoptions,ppo_enumerator_current);
  182. end
  183. else
  184. Message(parser_e_enumerator_current_is_not_valid) // property has no reader
  185. end
  186. else
  187. Message1(parser_e_invalid_enumerator_identifier, pattern);
  188. consume(token);
  189. end
  190. else
  191. Message(parser_e_enumerator_identifier_required);
  192. consume(_SEMICOLON);
  193. end;
  194. { in case of a previous error, p might not be assigned }
  195. if assigned(p) then
  196. begin
  197. trtti_attribute_list.bind(rtti_attrs_def,p.rtti_attribute_list);
  198. { hint directives, these can be separated by semicolons here,
  199. that needs to be handled here with a loop (PFV) }
  200. while try_consume_hintdirective(p.symoptions,p.deprecatedmsg) do
  201. Consume(_SEMICOLON);
  202. end
  203. else
  204. begin
  205. { recover from error so we can continue to parse }
  206. { hint directives, these can be separated by semicolons here,
  207. that needs to be handled here with a loop (PFV) }
  208. while try_consume_hintdirective(_symoptions,_deprecatedmsg) do
  209. Consume(_SEMICOLON);
  210. end;
  211. end;
  212. function class_destructor_head(astruct: tabstractrecorddef):tprocdef;
  213. var
  214. pd : tprocdef;
  215. begin
  216. result:=nil;
  217. consume(_DESTRUCTOR);
  218. parse_proc_head(current_structdef,potype_class_destructor,[],nil,nil,pd);
  219. if not assigned(pd) then
  220. begin
  221. consume(_SEMICOLON);
  222. exit;
  223. end;
  224. pd.calcparas;
  225. if (pd.maxparacount>0) then
  226. Message(parser_e_no_paras_for_class_destructor);
  227. consume(_SEMICOLON);
  228. include(astruct.objectoptions,oo_has_class_destructor);
  229. include(current_module.moduleflags,mf_classinits);
  230. { no return value }
  231. pd.returndef:=voidtype;
  232. constr_destr_finish_head(pd,astruct);
  233. result:=pd;
  234. end;
  235. function destructor_head:tprocdef;
  236. var
  237. pd : tprocdef;
  238. begin
  239. result:=nil;
  240. consume(_DESTRUCTOR);
  241. parse_proc_head(current_structdef,potype_destructor,[],nil,nil,pd);
  242. if not assigned(pd) then
  243. begin
  244. consume(_SEMICOLON);
  245. exit;
  246. end;
  247. if (cs_constructor_name in current_settings.globalswitches) and
  248. (pd.procsym.name<>'DONE') then
  249. Message(parser_e_destructorname_must_be_done);
  250. pd.calcparas;
  251. if not(pd.maxparacount=0) and
  252. (m_fpc in current_settings.modeswitches) then
  253. Message(parser_e_no_paras_for_destructor);
  254. consume(_SEMICOLON);
  255. include(current_structdef.objectoptions,oo_has_destructor);
  256. include(current_structdef.objectoptions,oo_has_new_destructor);
  257. { no return value }
  258. pd.returndef:=voidtype;
  259. constr_destr_finish_head(pd,pd.struct);
  260. result:=pd;
  261. end;
  262. procedure setinterfacemethodoptions;
  263. var
  264. i : longint;
  265. def : tdef;
  266. begin
  267. include(current_structdef.objectoptions,oo_has_virtual);
  268. for i:=0 to current_structdef.symtable.DefList.count-1 do
  269. begin
  270. def:=tdef(current_structdef.symtable.DefList[i]);
  271. if assigned(def) and
  272. (def.typ=procdef) then
  273. begin
  274. include(tprocdef(def).procoptions,po_virtualmethod);
  275. tprocdef(def).forwarddef:=false;
  276. end;
  277. end;
  278. end;
  279. procedure setobjcclassmethodoptions;
  280. var
  281. i : longint;
  282. def : tdef;
  283. begin
  284. for i:=0 to current_structdef.symtable.DefList.count-1 do
  285. begin
  286. def:=tdef(current_structdef.symtable.DefList[i]);
  287. if assigned(def) and
  288. (def.typ=procdef) then
  289. begin
  290. include(tprocdef(def).procoptions,po_virtualmethod);
  291. end;
  292. end;
  293. end;
  294. procedure handleImplementedInterface(intfdef : tobjectdef);
  295. begin
  296. if not is_interface(intfdef) then
  297. begin
  298. Message1(type_e_interface_type_expected,intfdef.typename);
  299. exit;
  300. end;
  301. if ([oo_is_forward,oo_is_formal] * intfdef.objectoptions <> []) then
  302. begin
  303. Message1(parser_e_forward_intf_declaration_must_be_resolved,intfdef.objrealname^);
  304. exit;
  305. end;
  306. if find_implemented_interface(current_objectdef,intfdef)<>nil then
  307. Message1(sym_e_duplicate_id,intfdef.objname^)
  308. else
  309. current_objectdef.register_implemented_interface(intfdef,true);
  310. end;
  311. procedure handleImplementedProtocolOrJavaIntf(intfdef : tobjectdef);
  312. begin
  313. intfdef:=find_real_class_definition(intfdef,false);
  314. case current_objectdef.objecttype of
  315. odt_objcclass,
  316. odt_objccategory,
  317. odt_objcprotocol:
  318. if not is_objcprotocol(intfdef) then
  319. begin
  320. Message1(type_e_protocol_type_expected,intfdef.typename);
  321. exit;
  322. end;
  323. odt_javaclass,
  324. odt_interfacejava:
  325. if not is_javainterface(intfdef) then
  326. begin
  327. Message1(type_e_interface_type_expected,intfdef.typename);
  328. exit
  329. end;
  330. else
  331. internalerror(2011010807);
  332. end;
  333. if ([oo_is_forward,oo_is_formal] * intfdef.objectoptions <> []) then
  334. begin
  335. Message1(parser_e_forward_intf_declaration_must_be_resolved,intfdef.objrealname^);
  336. exit;
  337. end;
  338. if find_implemented_interface(current_objectdef,intfdef)<>nil then
  339. Message1(sym_e_duplicate_id,intfdef.objname^)
  340. else
  341. current_objectdef.register_implemented_interface(intfdef,false);
  342. end;
  343. procedure readImplementedInterfacesAndProtocols(intf: boolean);
  344. var
  345. hdef : tdef;
  346. begin
  347. while try_to_consume(_COMMA) do
  348. begin
  349. { use single_type instead of id_type for specialize support }
  350. single_type(hdef,[stoAllowSpecialization,stoParseClassParent]);
  351. if (hdef.typ<>objectdef) then
  352. begin
  353. if intf then
  354. Message1(type_e_interface_type_expected,hdef.typename)
  355. else
  356. Message1(type_e_protocol_type_expected,hdef.typename);
  357. continue;
  358. end;
  359. if intf then
  360. handleImplementedInterface(tobjectdef(hdef))
  361. else
  362. handleImplementedProtocolOrJavaIntf(tobjectdef(hdef));
  363. end;
  364. end;
  365. procedure readinterfaceiid;
  366. var
  367. p : tnode;
  368. valid : boolean;
  369. begin
  370. p:=comp_expr([ef_accept_equal]);
  371. if p.nodetype=stringconstn then
  372. begin
  373. stringdispose(current_objectdef.iidstr);
  374. current_objectdef.iidstr:=stringdup(strpas(tstringconstnode(p).value_str));
  375. valid:=string2guid(current_objectdef.iidstr^,current_objectdef.iidguid^);
  376. if (current_objectdef.objecttype in [odt_interfacecom,odt_dispinterface]) and
  377. not valid then
  378. Message(parser_e_improper_guid_syntax);
  379. include(current_structdef.objectoptions,oo_has_valid_guid);
  380. end
  381. else
  382. Message(parser_e_illegal_expression);
  383. p.free;
  384. end;
  385. procedure get_cpp_or_java_class_external_status(od: tobjectdef);
  386. var
  387. hs: string;
  388. begin
  389. { C++ classes can be external -> all methods inside are external
  390. (defined at the class level instead of per method, so that you cannot
  391. define some methods as external and some not)
  392. }
  393. if try_to_consume(_EXTERNAL) then
  394. begin
  395. hs:='';
  396. if token in [_CSTRING,_CWSTRING,_CCHAR,_CWCHAR] then
  397. begin
  398. { Always add library prefix and suffix to create an uniform name }
  399. hs:=get_stringconst;
  400. if ExtractFileExt(hs)='' then
  401. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  402. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  403. hs:=target_info.sharedlibprefix+hs;
  404. end;
  405. if hs<>'' then
  406. begin
  407. { the JVM expects java/lang/Object rather than java.lang.Object }
  408. if target_info.system in systems_jvm then
  409. Replace(hs,'.','/');
  410. stringdispose(od.import_lib);
  411. od.import_lib:=stringdup(hs);
  412. end;
  413. { check if we shall use another name for the class }
  414. if try_to_consume(_NAME) then
  415. od.objextname:=stringdup(get_stringconst)
  416. else
  417. od.objextname:=stringdup(od.objrealname^);
  418. include(od.objectoptions,oo_is_external);
  419. end
  420. else
  421. begin
  422. od.objextname:=stringdup(od.objrealname^);
  423. end;
  424. end;
  425. procedure get_objc_class_or_protocol_external_status(od: tobjectdef);
  426. begin
  427. { Objective-C classes can be external -> all messages inside are
  428. external (defined at the class level instead of per method, so
  429. that you cannot define some methods as external and some not)
  430. }
  431. if try_to_consume(_EXTERNAL) then
  432. begin
  433. if try_to_consume(_NAME) then
  434. od.objextname:=stringdup(get_stringconst)
  435. else
  436. { the external name doesn't matter for formally declared
  437. classes, and allowing to specify one would mean that we would
  438. have to check it for consistency with the actual definition
  439. later on }
  440. od.objextname:=stringdup(od.objrealname^);
  441. include(od.objectoptions,oo_is_external);
  442. end
  443. else
  444. od.objextname:=stringdup(od.objrealname^);
  445. end;
  446. procedure parse_object_options;
  447. var
  448. gotexternal: boolean;
  449. begin
  450. case current_objectdef.objecttype of
  451. odt_object,odt_class,
  452. odt_javaclass:
  453. begin
  454. gotexternal:=false;
  455. while true do
  456. begin
  457. if try_to_consume(_ABSTRACT) then
  458. include(current_structdef.objectoptions,oo_is_abstract)
  459. else
  460. if try_to_consume(_SEALED) then
  461. include(current_structdef.objectoptions,oo_is_sealed)
  462. else if (current_objectdef.objecttype=odt_javaclass) and
  463. (token=_ID) and
  464. (idtoken=_EXTERNAL) then
  465. begin
  466. get_cpp_or_java_class_external_status(current_objectdef);
  467. gotexternal:=true;
  468. end
  469. else
  470. break;
  471. end;
  472. { don't use <=, because there's a bug in the 2.6.0 SPARC code
  473. generator regarding handling this expression }
  474. if ([oo_is_abstract, oo_is_sealed] * current_structdef.objectoptions) = [oo_is_abstract, oo_is_sealed] then
  475. Message(parser_e_abstract_and_sealed_conflict);
  476. { set default external name in case of no external directive }
  477. if (current_objectdef.objecttype=odt_javaclass) and
  478. not gotexternal then
  479. get_cpp_or_java_class_external_status(current_objectdef)
  480. end;
  481. odt_cppclass,
  482. odt_interfacejava:
  483. get_cpp_or_java_class_external_status(current_objectdef);
  484. odt_objcclass,odt_objcprotocol,odt_objccategory:
  485. get_objc_class_or_protocol_external_status(current_objectdef);
  486. odt_helper: ; // nothing
  487. else
  488. ;
  489. end;
  490. end;
  491. procedure parse_parent_classes;
  492. var
  493. intfchildof,
  494. childof : tobjectdef;
  495. hdef : tdef;
  496. hasparentdefined : boolean;
  497. begin
  498. childof:=nil;
  499. intfchildof:=nil;
  500. hasparentdefined:=false;
  501. { reads the parent class }
  502. if (token=_LKLAMMER) or
  503. is_objccategory(current_structdef) then
  504. begin
  505. consume(_LKLAMMER);
  506. { use single_type instead of id_type for specialize support }
  507. single_type(hdef,[stoAllowSpecialization, stoParseClassParent]);
  508. if (not assigned(hdef)) or
  509. (hdef.typ<>objectdef) then
  510. begin
  511. if assigned(hdef) then
  512. Message1(type_e_class_type_expected,hdef.typename)
  513. else if is_objccategory(current_structdef) then
  514. { a category must specify the class to extend }
  515. Message(type_e_objcclass_type_expected);
  516. end
  517. else
  518. begin
  519. childof:=tobjectdef(hdef);
  520. { a mix of class, interfaces, objects and cppclasses
  521. isn't allowed }
  522. case current_objectdef.objecttype of
  523. odt_class,
  524. odt_javaclass:
  525. if (childof.objecttype<>current_objectdef.objecttype) then
  526. begin
  527. if (is_interface(childof) and
  528. is_class(current_objectdef)) or
  529. (is_javainterface(childof) and
  530. is_javaclass(current_objectdef)) then
  531. begin
  532. { we insert the interface after the child
  533. is set, see below
  534. }
  535. intfchildof:=childof;
  536. childof:=class_tobject;
  537. end
  538. else
  539. Message(parser_e_mix_of_classes_and_objects);
  540. end
  541. else
  542. if oo_is_sealed in childof.objectoptions then
  543. Message1(parser_e_sealed_descendant,childof.typename)
  544. else
  545. childof:=find_real_class_definition(childof,true);
  546. odt_interfacecorba,
  547. odt_interfacecom:
  548. begin
  549. if not(is_interface(childof)) then
  550. Message(parser_e_mix_of_classes_and_objects);
  551. current_objectdef.objecttype:=childof.objecttype;
  552. end;
  553. odt_cppclass:
  554. if not(is_cppclass(childof)) then
  555. Message(parser_e_mix_of_classes_and_objects);
  556. odt_objcclass:
  557. if not(is_objcclass(childof) or
  558. is_objccategory(childof)) then
  559. begin
  560. if is_objcprotocol(childof) then
  561. begin
  562. if not(oo_is_classhelper in current_structdef.objectoptions) then
  563. begin
  564. intfchildof:=childof;
  565. childof:=nil;
  566. CGMessage(parser_h_no_objc_parent);
  567. end
  568. else
  569. { a category must specify the class to extend }
  570. CGMessage(type_e_objcclass_type_expected);
  571. end
  572. else
  573. Message(parser_e_mix_of_classes_and_objects);
  574. end
  575. else
  576. childof:=find_real_class_definition(childof,true);
  577. odt_objcprotocol:
  578. begin
  579. if not(is_objcprotocol(childof)) then
  580. Message(parser_e_mix_of_classes_and_objects);
  581. intfchildof:=childof;
  582. childof:=nil;
  583. end;
  584. odt_interfacejava:
  585. begin
  586. if not(is_javainterface(childof)) then
  587. Message(parser_e_mix_of_classes_and_objects);
  588. intfchildof:=find_real_class_definition(childof,true);
  589. childof:=nil;
  590. end;
  591. odt_object:
  592. if not(is_object(childof)) then
  593. Message(parser_e_mix_of_classes_and_objects)
  594. else
  595. if oo_is_sealed in childof.objectoptions then
  596. Message1(parser_e_sealed_descendant,childof.typename);
  597. odt_dispinterface:
  598. Message(parser_e_dispinterface_cant_have_parent);
  599. odt_helper:
  600. if not is_objectpascal_helper(childof) then
  601. begin
  602. Message(type_e_helper_type_expected);
  603. childof:=nil;
  604. end;
  605. else
  606. ;
  607. end;
  608. end;
  609. hasparentdefined:=true;
  610. end;
  611. { if no parent class, then a class get tobject as parent }
  612. if not assigned(childof) then
  613. begin
  614. case current_objectdef.objecttype of
  615. odt_class:
  616. if current_objectdef<>class_tobject then
  617. childof:=class_tobject;
  618. odt_interfacecom:
  619. if current_objectdef<>interface_iunknown then
  620. childof:=interface_iunknown;
  621. odt_dispinterface:
  622. childof:=interface_idispatch;
  623. odt_objcclass:
  624. CGMessage(parser_h_no_objc_parent);
  625. odt_javaclass:
  626. { inherit from TObject by default for compatibility }
  627. if current_objectdef<>java_jlobject then
  628. childof:=class_tobject;
  629. else
  630. ;
  631. end;
  632. end;
  633. if assigned(childof) then
  634. begin
  635. { Forbid not completly defined objects to be used as parents. This will
  636. also prevent circular loops of classes, because we set the forward flag
  637. at the start of the new definition and will reset it below after the
  638. parent has been set }
  639. if (oo_is_forward in childof.objectoptions) then
  640. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^)
  641. else if not(oo_is_formal in childof.objectoptions) then
  642. current_objectdef.set_parent(childof)
  643. else
  644. Message1(sym_e_formal_class_not_resolved,childof.objrealname^);
  645. end;
  646. if hasparentdefined then
  647. begin
  648. if current_objectdef.objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  649. begin
  650. if assigned(intfchildof) then
  651. if current_objectdef.objecttype=odt_class then
  652. handleImplementedInterface(intfchildof)
  653. else
  654. handleImplementedProtocolOrJavaIntf(intfchildof);
  655. readImplementedInterfacesAndProtocols(current_objectdef.objecttype=odt_class);
  656. end;
  657. consume(_RKLAMMER);
  658. end;
  659. { remove forward flag, is resolved }
  660. exclude(current_structdef.objectoptions,oo_is_forward);
  661. end;
  662. procedure parse_extended_type(helpertype:thelpertype);
  663. procedure validate_extendeddef_typehelper(var def:tdef);
  664. begin
  665. if (def.typ in [undefineddef,procvardef,procdef,
  666. filedef,classrefdef,abstractdef,forwarddef,formaldef]) or
  667. (
  668. (def.typ=objectdef) and
  669. not (tobjectdef(def).objecttype in objecttypes_with_helpers)
  670. ) then
  671. begin
  672. Message1(type_e_type_not_allowed_for_type_helper,def.typename);
  673. def:=generrordef;
  674. end;
  675. end;
  676. procedure check_inheritance_record_type_helper(var def:tdef);
  677. var
  678. tmp : tstoreddef;
  679. begin
  680. if (def.typ<>errordef) and assigned(current_objectdef.childof) then
  681. begin
  682. if def<>current_objectdef.childof.extendeddef then
  683. begin
  684. { a type helper may extend a type alias of the type its
  685. parent type helper extends }
  686. tmp:=tstoreddef(def);
  687. while (df_unique in tmp.defoptions) and assigned(tstoreddef(tmp).orgdef) do
  688. begin
  689. if tmp.orgdef=current_objectdef.childof.extendeddef then
  690. exit;
  691. tmp:=tstoreddef(tmp.orgdef);
  692. end;
  693. Message1(type_e_record_helper_must_extend_same_record,current_objectdef.childof.extendeddef.typename);
  694. def:=generrordef;
  695. end;
  696. end;
  697. end;
  698. procedure check_inheritance_class_helper(var def:tdef);
  699. begin
  700. if (def.typ<>errordef) and assigned(current_objectdef.childof) then
  701. begin
  702. if (current_objectdef.childof.extendeddef.typ<>objectdef) or
  703. not (tobjectdef(current_objectdef.childof.extendeddef).objecttype in objecttypes_with_helpers) then
  704. Internalerror(2011021101);
  705. if not def_is_related(def,current_objectdef.childof.extendeddef) then
  706. begin
  707. Message1(type_e_class_helper_must_extend_subclass,current_objectdef.childof.extendeddef.typename);
  708. def:=generrordef;
  709. end;
  710. end;
  711. end;
  712. var
  713. hdef: tdef;
  714. begin
  715. if not is_objectpascal_helper(current_structdef) then
  716. Internalerror(2011021103);
  717. consume(_FOR);
  718. single_type(hdef,[stoParseClassParent]);
  719. if not assigned(hdef) or (hdef.typ=errordef) then
  720. begin
  721. case helpertype of
  722. ht_class:
  723. Message1(type_e_class_type_expected,hdef.typename);
  724. ht_record:
  725. Message(type_e_record_type_expected);
  726. ht_type:
  727. Message1(type_e_type_id_expected,hdef.typename);
  728. else
  729. internalerror(2019050532);
  730. end;
  731. end
  732. else
  733. begin
  734. case helpertype of
  735. ht_class:
  736. if (hdef.typ<>objectdef) or
  737. not is_class(hdef) then
  738. Message1(type_e_class_type_expected,hdef.typename)
  739. else
  740. begin
  741. { a class helper must extend the same class or a subclass
  742. of the class extended by the parent class helper }
  743. check_inheritance_class_helper(hdef);
  744. end;
  745. ht_record:
  746. if (hdef.typ=objectdef) or
  747. (
  748. { primitive types are allowed for record helpers in mode
  749. delphi }
  750. (hdef.typ<>recorddef) and
  751. not (m_delphi in current_settings.modeswitches)
  752. ) then
  753. Message1(type_e_record_type_expected,hdef.typename)
  754. else
  755. begin
  756. if hdef.typ<>recorddef then
  757. { this is a primitive type in mode delphi, so validate
  758. the def }
  759. validate_extendeddef_typehelper(hdef);
  760. { a record helper must extend the same record as the
  761. parent helper }
  762. check_inheritance_record_type_helper(hdef);
  763. end;
  764. ht_type:
  765. begin
  766. validate_extendeddef_typehelper(hdef);
  767. if (hdef.typ=objectdef) and
  768. (tobjectdef(hdef).objecttype in objecttypes_with_helpers) then
  769. check_inheritance_class_helper(hdef)
  770. else
  771. { a type helper must extend the same type as the
  772. parent helper }
  773. check_inheritance_record_type_helper(hdef);
  774. end;
  775. else
  776. internalerror(2019050531);
  777. end;
  778. end;
  779. if assigned(hdef) then
  780. current_objectdef.extendeddef:=hdef
  781. else
  782. current_objectdef.extendeddef:=generrordef;
  783. end;
  784. procedure parse_guid;
  785. begin
  786. { read GUID }
  787. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  788. try_to_consume(_LECKKLAMMER) then
  789. begin
  790. readinterfaceiid;
  791. consume(_RECKKLAMMER);
  792. end
  793. else if (current_objectdef.objecttype=odt_dispinterface) then
  794. message(parser_e_dispinterface_needs_a_guid);
  795. end;
  796. function method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  797. procedure chkobjc(pd: tprocdef);
  798. begin
  799. if is_objc_class_or_protocol(pd.struct) then
  800. begin
  801. include(pd.procoptions,po_objc);
  802. end;
  803. end;
  804. procedure chkjava(pd: tprocdef);
  805. begin
  806. {$ifdef jvm}
  807. if is_java_class_or_interface(pd.struct) then
  808. begin
  809. { mark all non-virtual instance methods as "virtual; final;",
  810. because
  811. a) that's the only way to guarantee "non-virtual" behaviour
  812. (other than making them class methods with an explicit self
  813. pointer, but that causes problems with interface mappings
  814. and procvars)
  815. b) if we don't mark them virtual, they don't get added to the
  816. vmt and we can't check whether child classes try to override
  817. them
  818. }
  819. if is_javaclass(pd.struct) then
  820. begin
  821. if not(po_virtualmethod in pd.procoptions) and
  822. not(po_classmethod in pd.procoptions) then
  823. begin
  824. include(pd.procoptions,po_virtualmethod);
  825. include(pd.procoptions,po_finalmethod);
  826. include(pd.procoptions,po_java_nonvirtual);
  827. end
  828. else if [po_virtualmethod,po_classmethod]<=pd.procoptions then
  829. begin
  830. if po_staticmethod in pd.procoptions then
  831. Message(type_e_java_class_method_not_static_virtual);
  832. end;
  833. end;
  834. end;
  835. {$endif}
  836. end;
  837. procedure chkcpp(pd:tprocdef);
  838. begin
  839. { nothing currently }
  840. end;
  841. var
  842. oldparse_only: boolean;
  843. flags : tparse_proc_flags;
  844. begin
  845. case token of
  846. _PROCEDURE,
  847. _FUNCTION:
  848. begin
  849. if (astruct.symtable.currentvisibility=vis_published) and
  850. not(oo_can_have_published in astruct.objectoptions) then
  851. Message(parser_e_cant_have_published);
  852. oldparse_only:=parse_only;
  853. parse_only:=true;
  854. flags:=[];
  855. if is_classdef then
  856. include(flags,ppf_classmethod);
  857. if hadgeneric then
  858. include(flags,ppf_generic);
  859. result:=parse_proc_dec(flags,astruct);
  860. { this is for error recovery as well as forward }
  861. { interface mappings, i.e. mapping to a method }
  862. { which isn't declared yet }
  863. if assigned(result) then
  864. begin
  865. parse_object_proc_directives(result);
  866. { check if dispid is set }
  867. if is_dispinterface(result.struct) and not (po_dispid in result.procoptions) then
  868. begin
  869. result.dispid:=tobjectdef(result.struct).get_next_dispid;
  870. include(result.procoptions, po_dispid);
  871. end;
  872. { all Macintosh Object Pascal methods are virtual. }
  873. { this can't be a class method, because macpas mode }
  874. { has no m_class }
  875. if (m_mac in current_settings.modeswitches) then
  876. include(result.procoptions,po_virtualmethod);
  877. { for record and type helpers only static class methods are
  878. allowed }
  879. if is_objectpascal_helper(astruct) and
  880. (
  881. (tobjectdef(astruct).extendeddef.typ<>objectdef) or
  882. (tobjectdef(tobjectdef(astruct).extendeddef).objecttype<>odt_class)
  883. ) and
  884. is_classdef and not (po_staticmethod in result.procoptions) then
  885. MessagePos(result.fileinfo,parser_e_class_methods_only_static_in_records);
  886. handle_calling_convention(result,hcc_default_actions_intf_struct);
  887. { add definition to procsym }
  888. proc_add_definition(result);
  889. { add procdef options to objectdef options }
  890. if (po_msgint in result.procoptions) then
  891. include(astruct.objectoptions,oo_has_msgint);
  892. if (po_msgstr in result.procoptions) then
  893. include(astruct.objectoptions,oo_has_msgstr);
  894. if (po_virtualmethod in result.procoptions) then
  895. include(astruct.objectoptions,oo_has_virtual);
  896. if result.is_generic then
  897. astruct.symtable.includeoption(sto_has_generic);
  898. chkcpp(result);
  899. chkobjc(result);
  900. chkjava(result);
  901. end;
  902. maybe_parse_hint_directives(result);
  903. parse_only:=oldparse_only;
  904. end;
  905. _CONSTRUCTOR :
  906. begin
  907. if (astruct.symtable.currentvisibility=vis_published) and
  908. not(oo_can_have_published in astruct.objectoptions) then
  909. Message(parser_e_cant_have_published);
  910. if not is_classdef and not(astruct.symtable.currentvisibility in [vis_public,vis_published]) then
  911. Message(parser_w_constructor_should_be_public);
  912. if is_interface(astruct) then
  913. Message(parser_e_no_con_des_in_interfaces);
  914. { Objective-C does not know the concept of a constructor }
  915. if is_objc_class_or_protocol(astruct) then
  916. Message(parser_e_objc_no_constructor_destructor);
  917. if is_objectpascal_helper(astruct) then
  918. if is_classdef then
  919. { class constructors are not allowed in class helpers }
  920. Message(parser_e_no_class_constructor_in_helpers);
  921. { only 1 class constructor is allowed }
  922. if is_classdef and (oo_has_class_constructor in astruct.objectoptions) then
  923. Message1(parser_e_only_one_class_constructor_allowed, astruct.objrealname^);
  924. oldparse_only:=parse_only;
  925. parse_only:=true;
  926. if is_classdef then
  927. result:=class_constructor_head(current_structdef)
  928. else
  929. begin
  930. result:=constructor_head;
  931. if is_objectpascal_helper(astruct) and
  932. (tobjectdef(astruct).extendeddef.typ<>objectdef) and
  933. (result.minparacount=0) then
  934. { as long as parameterless constructors aren't allowed in records they
  935. aren't allowed in record/type helpers either }
  936. MessagePos(result.procsym.fileinfo,parser_e_no_parameterless_constructor_in_records);
  937. end;
  938. chkcpp(result);
  939. parse_only:=oldparse_only;
  940. end;
  941. _DESTRUCTOR :
  942. begin
  943. if (astruct.symtable.currentvisibility=vis_published) and
  944. not(oo_can_have_published in astruct.objectoptions) then
  945. Message(parser_e_cant_have_published);
  946. if not is_classdef then
  947. if (oo_has_new_destructor in astruct.objectoptions) then
  948. Message(parser_n_only_one_destructor);
  949. if is_interface(astruct) then
  950. Message(parser_e_no_con_des_in_interfaces);
  951. { (class) destructors are not allowed in class helpers }
  952. if is_objectpascal_helper(astruct) then
  953. Message(parser_e_no_destructor_in_records);
  954. if not is_classdef and (astruct.symtable.currentvisibility<>vis_public) then
  955. Message(parser_w_destructor_should_be_public);
  956. { Objective-C does not know the concept of a destructor }
  957. if is_objc_class_or_protocol(astruct) then
  958. Message(parser_e_objc_no_constructor_destructor);
  959. { only 1 class destructor is allowed }
  960. if is_classdef and (oo_has_class_destructor in astruct.objectoptions) then
  961. Message1(parser_e_only_one_class_destructor_allowed, astruct.objrealname^);
  962. oldparse_only:=parse_only;
  963. parse_only:=true;
  964. if is_classdef then
  965. result:=class_destructor_head(current_structdef)
  966. else
  967. result:=destructor_head;
  968. chkcpp(result);
  969. parse_only:=oldparse_only;
  970. end;
  971. else
  972. internalerror(2011032102);
  973. end;
  974. end;
  975. procedure parse_object_members;
  976. var
  977. typedconstswritable: boolean;
  978. object_member_blocktype : tblock_type;
  979. hadgeneric,
  980. fields_allowed, is_classdef, class_fields, is_final, final_fields,
  981. threadvar_fields : boolean;
  982. vdoptions: tvar_dec_options;
  983. fieldlist: tfpobjectlist;
  984. rtti_attrs_def: trtti_attribute_list;
  985. procedure parse_const;
  986. begin
  987. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  988. Message(parser_e_type_var_const_only_in_records_and_classes);
  989. consume(_CONST);
  990. object_member_blocktype:=bt_const;
  991. final_fields:=is_final;
  992. is_final:=false;
  993. end;
  994. procedure parse_var(isthreadvar:boolean);
  995. begin
  996. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass]) and
  997. { Java interfaces can contain static final class vars }
  998. not((current_objectdef.objecttype=odt_interfacejava) and
  999. is_final and is_classdef) then
  1000. Message(parser_e_type_var_const_only_in_records_and_classes);
  1001. if isthreadvar then
  1002. consume(_THREADVAR)
  1003. else
  1004. consume(_VAR);
  1005. fields_allowed:=true;
  1006. object_member_blocktype:=bt_general;
  1007. class_fields:=is_classdef;
  1008. final_fields:=is_final;
  1009. threadvar_fields:=isthreadvar;
  1010. is_classdef:=false;
  1011. is_final:=false;
  1012. end;
  1013. procedure parse_class;
  1014. begin
  1015. is_classdef:=false;
  1016. { read class method/field/property }
  1017. consume(_CLASS);
  1018. { class modifier is only allowed for procedures, functions, }
  1019. { constructors, destructors, fields and properties }
  1020. if not((token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_DESTRUCTOR,_THREADVAR]) or (token=_CONSTRUCTOR)) then
  1021. Message(parser_e_procedure_or_function_expected);
  1022. { Java interfaces can contain final class vars }
  1023. if is_interface(current_structdef) or
  1024. (is_javainterface(current_structdef) and
  1025. (not(is_final) or
  1026. (token<>_VAR))) then
  1027. Message(parser_e_no_static_method_in_interfaces)
  1028. else
  1029. { class methods are also allowed for Objective-C protocols }
  1030. is_classdef:=true;
  1031. end;
  1032. procedure parse_visibility(vis: tvisibility; oo: tobjectoption);
  1033. begin
  1034. { Objective-C and Java classes do not support "published",
  1035. as basically everything is published. }
  1036. if (vis=vis_published) and
  1037. (is_objc_class_or_protocol(current_structdef) or
  1038. is_java_class_or_interface(current_structdef)) then
  1039. Message(parser_e_no_objc_published)
  1040. else if is_interface(current_structdef) or
  1041. is_objc_protocol_or_category(current_structdef) or
  1042. is_javainterface(current_structdef) then
  1043. Message(parser_e_no_access_specifier_in_interfaces);
  1044. current_structdef.symtable.currentvisibility:=vis;
  1045. consume(token);
  1046. if (oo<>oo_none) then
  1047. include(current_structdef.objectoptions,oo);
  1048. fields_allowed:=true;
  1049. is_classdef:=false;
  1050. class_fields:=false;
  1051. threadvar_fields:=false;
  1052. is_final:=false;
  1053. object_member_blocktype:=bt_general;
  1054. end;
  1055. procedure check_unbound_attributes;
  1056. begin
  1057. if assigned(rtti_attrs_def) and (rtti_attrs_def.get_attribute_count>0) then
  1058. Message1(parser_e_unbound_attribute,trtti_attribute(rtti_attrs_def.rtti_attributes[0]).typesym.prettyname);
  1059. rtti_attrs_def.free;
  1060. rtti_attrs_def:=nil;
  1061. end;
  1062. begin
  1063. { empty class declaration ? }
  1064. if (current_objectdef.objecttype in [odt_class,odt_objcclass,odt_javaclass]) and
  1065. (token=_SEMICOLON) then
  1066. exit;
  1067. { in "publishable" classes the default access type is published }
  1068. if (oo_can_have_published in current_structdef.objectoptions) then
  1069. current_structdef.symtable.currentvisibility:=vis_published
  1070. else
  1071. current_structdef.symtable.currentvisibility:=vis_public;
  1072. fields_allowed:=true;
  1073. is_classdef:=false;
  1074. class_fields:=false;
  1075. is_final:=false;
  1076. final_fields:=false;
  1077. rtti_attrs_def:=nil;
  1078. hadgeneric:=false;
  1079. threadvar_fields:=false;
  1080. object_member_blocktype:=bt_general;
  1081. fieldlist:=tfpobjectlist.create(false);
  1082. repeat
  1083. case token of
  1084. _TYPE :
  1085. begin
  1086. check_unbound_attributes;
  1087. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  1088. Message(parser_e_type_var_const_only_in_records_and_classes);
  1089. consume(_TYPE);
  1090. object_member_blocktype:=bt_type;
  1091. { expect at least one type declaration }
  1092. if token<>_ID then
  1093. consume(_ID);
  1094. end;
  1095. _VAR :
  1096. begin
  1097. check_unbound_attributes;
  1098. rtti_attrs_def := nil;
  1099. parse_var(false);
  1100. { expect at least one var declaration }
  1101. if token<>_ID then
  1102. consume(_ID);
  1103. end;
  1104. _CONST:
  1105. begin
  1106. check_unbound_attributes;
  1107. rtti_attrs_def := nil;
  1108. parse_const;
  1109. { expect at least one constant declaration }
  1110. if token<>_ID then
  1111. consume(_ID);
  1112. end;
  1113. _THREADVAR :
  1114. begin
  1115. check_unbound_attributes;
  1116. if not is_classdef then
  1117. begin
  1118. Message(parser_e_threadvar_must_be_class);
  1119. { for error recovery we enforce class fields }
  1120. is_classdef:=true;
  1121. end;
  1122. parse_var(true);
  1123. { expect at least one threadvar declaration }
  1124. if token<>_ID then
  1125. consume(_ID);
  1126. end;
  1127. _ID :
  1128. begin
  1129. check_unbound_attributes;
  1130. if is_objcprotocol(current_structdef) and
  1131. ((idtoken=_REQUIRED) or
  1132. (idtoken=_OPTIONAL)) then
  1133. begin
  1134. current_structdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  1135. consume(idtoken)
  1136. end
  1137. else case idtoken of
  1138. _PRIVATE :
  1139. begin
  1140. parse_visibility(vis_private,oo_has_private);
  1141. end;
  1142. _PROTECTED :
  1143. begin
  1144. parse_visibility(vis_protected,oo_has_protected);
  1145. end;
  1146. _PUBLIC :
  1147. begin
  1148. parse_visibility(vis_public,oo_none);
  1149. end;
  1150. _PUBLISHED :
  1151. begin
  1152. parse_visibility(vis_published,oo_none);
  1153. end;
  1154. _STRICT :
  1155. begin
  1156. if is_interface(current_structdef) or
  1157. is_objc_protocol_or_category(current_structdef) or
  1158. is_javainterface(current_structdef) then
  1159. Message(parser_e_no_access_specifier_in_interfaces);
  1160. consume(_STRICT);
  1161. if token=_ID then
  1162. begin
  1163. case idtoken of
  1164. _PRIVATE:
  1165. begin
  1166. consume(_PRIVATE);
  1167. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  1168. include(current_structdef.objectoptions,oo_has_strictprivate);
  1169. end;
  1170. _PROTECTED:
  1171. begin
  1172. consume(_PROTECTED);
  1173. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  1174. include(current_structdef.objectoptions,oo_has_strictprotected);
  1175. end;
  1176. else
  1177. message(parser_e_protected_or_private_expected);
  1178. end;
  1179. end
  1180. else
  1181. message(parser_e_protected_or_private_expected);
  1182. fields_allowed:=true;
  1183. is_classdef:=false;
  1184. class_fields:=false;
  1185. threadvar_fields:=false;
  1186. is_final:=false;
  1187. final_fields:=false;
  1188. object_member_blocktype:=bt_general;
  1189. end
  1190. else if (m_final_fields in current_settings.modeswitches) and
  1191. (token=_ID) and
  1192. (idtoken=_FINAL) then
  1193. begin
  1194. { currently only supported for external classes, because
  1195. requires fully working DFA otherwise }
  1196. if (current_structdef.typ<>objectdef) or
  1197. not(oo_is_external in tobjectdef(current_structdef).objectoptions) then
  1198. Message(parser_e_final_only_external);
  1199. consume(_final);
  1200. is_final:=true;
  1201. if token=_CLASS then
  1202. parse_class;
  1203. if not(token in [_CONST,_VAR]) then
  1204. message(parser_e_final_only_const_var);
  1205. end
  1206. else
  1207. begin
  1208. if object_member_blocktype=bt_general then
  1209. begin
  1210. rtti_attrs_def := nil;
  1211. if (idtoken=_GENERIC) and
  1212. not (m_delphi in current_settings.modeswitches) and
  1213. (
  1214. not fields_allowed or
  1215. is_objectpascal_helper(current_structdef)
  1216. ) then
  1217. begin
  1218. if hadgeneric then
  1219. Message(parser_e_procedure_or_function_expected);
  1220. consume(_ID);
  1221. hadgeneric:=true;
  1222. if not (token in [_PROCEDURE,_FUNCTION,_CLASS]) then
  1223. Message(parser_e_procedure_or_function_expected);
  1224. end
  1225. else
  1226. begin
  1227. if is_interface(current_structdef) or
  1228. is_objc_protocol_or_category(current_structdef) or
  1229. (
  1230. is_objectpascal_helper(current_structdef) and
  1231. not class_fields
  1232. ) or
  1233. (is_javainterface(current_structdef) and
  1234. not(class_fields and final_fields)) then
  1235. Message(parser_e_no_vars_in_interfaces);
  1236. if (current_structdef.symtable.currentvisibility=vis_published) and
  1237. not(oo_can_have_published in current_structdef.objectoptions) then
  1238. Message(parser_e_cant_have_published);
  1239. if (not fields_allowed) then
  1240. Message(parser_e_field_not_allowed_here);
  1241. vdoptions:=[vd_object];
  1242. if not (m_delphi in current_settings.modeswitches) then
  1243. include(vdoptions,vd_check_generic);
  1244. if class_fields then
  1245. include(vdoptions,vd_class);
  1246. if is_class(current_structdef) then
  1247. include(vdoptions,vd_canreorder);
  1248. if final_fields then
  1249. include(vdoptions,vd_final);
  1250. if threadvar_fields then
  1251. include(vdoptions,vd_threadvar);
  1252. read_record_fields(vdoptions,fieldlist,nil,hadgeneric);
  1253. end;
  1254. end
  1255. else if object_member_blocktype=bt_type then
  1256. types_dec(true,hadgeneric, rtti_attrs_def)
  1257. else if object_member_blocktype=bt_const then
  1258. begin
  1259. typedconstswritable:=false;
  1260. if final_fields then
  1261. begin
  1262. { the value of final fields cannot be changed
  1263. once they've been assigned a value }
  1264. typedconstswritable:=cs_typed_const_writable in current_settings.localswitches;
  1265. exclude(current_settings.localswitches,cs_typed_const_writable);
  1266. end;
  1267. consts_dec(true,not is_javainterface(current_structdef),hadgeneric);
  1268. if final_fields and
  1269. typedconstswritable then
  1270. include(current_settings.localswitches,cs_typed_const_writable);
  1271. end
  1272. else
  1273. internalerror(2010011103);
  1274. end;
  1275. end;
  1276. end;
  1277. _PROPERTY :
  1278. begin
  1279. { for now attributes are only allowed on published properties }
  1280. if current_structdef.symtable.currentvisibility<>vis_published then
  1281. check_unbound_attributes;
  1282. struct_property_dec(is_classdef, rtti_attrs_def);
  1283. fields_allowed:=false;
  1284. is_classdef:=false;
  1285. end;
  1286. _CLASS:
  1287. begin
  1288. { class properties currently can't have attributes, so it's safe
  1289. to check for unbound attributes here }
  1290. check_unbound_attributes;
  1291. parse_class;
  1292. end;
  1293. _PROCEDURE,
  1294. _FUNCTION,
  1295. _CONSTRUCTOR,
  1296. _DESTRUCTOR :
  1297. begin
  1298. check_unbound_attributes;
  1299. rtti_attrs_def := nil;
  1300. method_dec(current_structdef,is_classdef,hadgeneric);
  1301. fields_allowed:=false;
  1302. is_classdef:=false;
  1303. hadgeneric:=false;
  1304. end;
  1305. _LECKKLAMMER:
  1306. begin
  1307. if m_prefixed_attributes in current_settings.modeswitches then
  1308. parse_rttiattributes(rtti_attrs_def)
  1309. else
  1310. consume(_ID);
  1311. end;
  1312. _END :
  1313. begin
  1314. check_unbound_attributes;
  1315. consume(_END);
  1316. break;
  1317. end;
  1318. else
  1319. consume(_ID); { Give a ident expected message, like tp7 }
  1320. end;
  1321. until false;
  1322. if is_class(current_structdef) then
  1323. tabstractrecordsymtable(current_structdef.symtable).addfieldlist(fieldlist,true);
  1324. fieldlist.free;
  1325. end;
  1326. function object_dec(objecttype:tobjecttyp;const n:tidstring;objsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist;fd : tobjectdef;helpertype:thelpertype) : tobjectdef;
  1327. var
  1328. old_current_structdef: tabstractrecorddef;
  1329. old_current_genericdef,
  1330. old_current_specializedef: tstoreddef;
  1331. old_parse_generic: boolean;
  1332. list: TFPObjectList;
  1333. s: TSymStr;
  1334. st: TSymtable;
  1335. olddef: tdef;
  1336. begin
  1337. old_current_structdef:=current_structdef;
  1338. old_current_genericdef:=current_genericdef;
  1339. old_current_specializedef:=current_specializedef;
  1340. old_parse_generic:=parse_generic;
  1341. current_structdef:=nil;
  1342. current_genericdef:=nil;
  1343. current_specializedef:=nil;
  1344. { objects and class types can't be declared local }
  1345. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  1346. not assigned(genericlist) then
  1347. Message(parser_e_no_local_objects);
  1348. { reuse forward objectdef? }
  1349. if assigned(fd) then
  1350. begin
  1351. if (fd.objecttype<>objecttype) or ((fd.is_generic or fd.is_specialization) xor assigned(genericlist)) then
  1352. begin
  1353. Message(parser_e_forward_mismatch);
  1354. { recover }
  1355. current_structdef:=cobjectdef.create(objecttype,n,nil,true);
  1356. include(current_structdef.objectoptions,oo_is_forward);
  1357. end
  1358. else
  1359. current_structdef:=fd
  1360. end
  1361. else
  1362. begin
  1363. { anonym objects aren't allow (o : object a : longint; end;) }
  1364. if n='' then
  1365. Message(parser_f_no_anonym_objects);
  1366. { create new class }
  1367. current_structdef:=cobjectdef.create(objecttype,n,nil,true);
  1368. tobjectdef(current_structdef).helpertype:=helpertype;
  1369. { include always the forward flag, it'll be removed after the parent class have been
  1370. added. This is to prevent circular childof loops }
  1371. include(current_structdef.objectoptions,oo_is_forward);
  1372. if (cs_compilesystem in current_settings.moduleswitches) then
  1373. begin
  1374. case current_objectdef.objecttype of
  1375. odt_interfacecom :
  1376. if (current_structdef.objname^='IUNKNOWN') then
  1377. interface_iunknown:=current_objectdef
  1378. else
  1379. if (current_structdef.objname^='IDISPATCH') then
  1380. interface_idispatch:=current_objectdef;
  1381. odt_class :
  1382. if (current_structdef.objname^='TOBJECT') then
  1383. class_tobject:=current_objectdef;
  1384. odt_javaclass:
  1385. begin
  1386. if (current_structdef.objname^='TOBJECT') then
  1387. class_tobject:=current_objectdef
  1388. else if (current_objectdef.objname^='JLOBJECT') then
  1389. java_jlobject:=current_objectdef
  1390. else if (current_objectdef.objname^='JLTHROWABLE') then
  1391. java_jlthrowable:=current_objectdef
  1392. else if (current_objectdef.objname^='FPCBASERECORDTYPE') then
  1393. java_fpcbaserecordtype:=current_objectdef
  1394. else if (current_objectdef.objname^='JLSTRING') then
  1395. java_jlstring:=current_objectdef
  1396. else if (current_objectdef.objname^='ANSISTRINGCLASS') then
  1397. java_ansistring:=current_objectdef
  1398. else if (current_objectdef.objname^='SHORTSTRINGCLASS') then
  1399. java_shortstring:=current_objectdef
  1400. else if (current_objectdef.objname^='JLENUM') then
  1401. java_jlenum:=current_objectdef
  1402. else if (current_objectdef.objname^='JUENUMSET') then
  1403. java_juenumset:=current_objectdef
  1404. else if (current_objectdef.objname^='FPCBITSET') then
  1405. java_jubitset:=current_objectdef
  1406. else if (current_objectdef.objname^='FPCBASEPROCVARTYPE') then
  1407. java_procvarbase:=current_objectdef;
  1408. end;
  1409. else
  1410. ;
  1411. end;
  1412. end;
  1413. if (current_module.modulename^='OBJCBASE') then
  1414. begin
  1415. case current_objectdef.objecttype of
  1416. odt_objcclass:
  1417. if (current_objectdef.objname^='Protocol') then
  1418. objc_protocoltype:=current_objectdef;
  1419. else
  1420. ;
  1421. end;
  1422. end;
  1423. end;
  1424. { usage of specialized type inside its generic template }
  1425. if assigned(genericdef) then
  1426. current_specializedef:=current_structdef;
  1427. { reject declaration of generic class inside generic class }
  1428. if assigned(genericlist) then
  1429. current_genericdef:=current_structdef;
  1430. { nested types of specializations are specializations as well }
  1431. if assigned(old_current_structdef) and
  1432. (df_specialization in old_current_structdef.defoptions) then
  1433. include(current_structdef.defoptions,df_specialization);
  1434. if assigned(old_current_structdef) and
  1435. (df_generic in old_current_structdef.defoptions) then
  1436. begin
  1437. include(current_structdef.defoptions,df_generic);
  1438. current_genericdef:=current_structdef;
  1439. end;
  1440. { set published flag in $M+ mode, it can also be inherited and will
  1441. be added when the parent class set with tobjectdef.set_parent (PFV) }
  1442. if (cs_generate_rtti in current_settings.localswitches) and
  1443. (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_class,odt_helper]) then
  1444. include(current_structdef.objectoptions,oo_can_have_published);
  1445. { Objective-C/Java objectdefs can be "formal definitions", in which case
  1446. the syntax is "type tc = objcclass external;" -> we have to parse
  1447. its object options (external) already here, to make sure that such
  1448. definitions are recognised as formal defs }
  1449. if objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  1450. parse_object_options;
  1451. { forward def? }
  1452. if not assigned(fd) and
  1453. (token=_SEMICOLON) then
  1454. begin
  1455. if is_objectpascal_helper(current_structdef) then
  1456. consume(_FOR);
  1457. { add to the list of definitions to check that the forward
  1458. is resolved. this is required for delphi mode }
  1459. current_module.checkforwarddefs.add(current_structdef);
  1460. symtablestack.push(current_structdef.symtable);
  1461. insert_generic_parameter_types(current_structdef,genericdef,genericlist,false);
  1462. { when we are parsing a generic already then this is a generic as
  1463. well }
  1464. if old_parse_generic then
  1465. include(current_structdef.defoptions,df_generic);
  1466. parse_generic:=(df_generic in current_structdef.defoptions);
  1467. { *don't* add the strict private symbol for non-Delphi modes for
  1468. forward defs }
  1469. symtablestack.pop(current_structdef.symtable);
  1470. end
  1471. else
  1472. begin
  1473. { change objccategories into objcclass helpers }
  1474. if (objecttype=odt_objccategory) then
  1475. begin
  1476. current_objectdef.objecttype:=odt_objcclass;
  1477. include(current_structdef.objectoptions,oo_is_classhelper);
  1478. end;
  1479. { include the class helper flag for Object Pascal helpers }
  1480. if (objecttype=odt_helper) then
  1481. include(current_objectdef.objectoptions,oo_is_classhelper);
  1482. { parse list of options (abstract / sealed) }
  1483. if not(objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava]) then
  1484. parse_object_options;
  1485. symtablestack.push(current_structdef.symtable);
  1486. insert_generic_parameter_types(current_structdef,genericdef,genericlist,assigned(fd));
  1487. { when we are parsing a generic already then this is a generic as
  1488. well }
  1489. if old_parse_generic then
  1490. include(current_structdef.defoptions, df_generic);
  1491. parse_generic:=(df_generic in current_structdef.defoptions);
  1492. { in non-Delphi modes we need a strict private symbol without type
  1493. count and type parameters in the name to simplify resolving }
  1494. maybe_insert_generic_rename_symbol(n,genericlist);
  1495. { parse list of parent classes }
  1496. { for record helpers in mode Delphi this is not allowed }
  1497. if not (is_objectpascal_helper(current_objectdef) and
  1498. (m_delphi in current_settings.modeswitches) and
  1499. (helpertype=ht_record)) then
  1500. parse_parent_classes
  1501. else
  1502. { remove forward flag, is resolved (this is normally done inside
  1503. parse_parent_classes) }
  1504. exclude(current_structdef.objectoptions,oo_is_forward);
  1505. { parse extended type for helpers }
  1506. if is_objectpascal_helper(current_structdef) then
  1507. parse_extended_type(helpertype);
  1508. if is_interface(current_objectdef) and
  1509. is_interface(current_objectdef.childof) and
  1510. (oo_is_invokable in tobjectdef(current_objectdef.childof).objectoptions) then
  1511. include(current_objectdef.objectoptions,oo_is_invokable);
  1512. { parse optional GUID for interfaces }
  1513. parse_guid;
  1514. { classes can handle links to themself not only inside type blocks
  1515. but in const blocks too. Additionally this is needed to parse parameters that are
  1516. specializations of the currently parsed type in basically everything except C++ and
  1517. ObjC classes. To make this possible we need to set their symbols to real defs instead
  1518. of errordef }
  1519. if assigned(objsym) and not (objecttype in [odt_cppclass,odt_objccategory,odt_objcclass,odt_objcprotocol]) then
  1520. begin
  1521. olddef:=ttypesym(objsym).typedef;
  1522. ttypesym(objsym).typedef:=current_structdef;
  1523. current_structdef.typesym:=objsym;
  1524. end
  1525. else
  1526. olddef:=nil;
  1527. { apply $RTTI directive to current object }
  1528. current_structdef.apply_rtti_directive(current_module.rtti_directive);
  1529. { parse and insert object members }
  1530. parse_object_members;
  1531. if assigned(olddef) then
  1532. begin
  1533. ttypesym(objsym).typedef:=olddef;
  1534. current_structdef.typesym:=nil;
  1535. end;
  1536. if not(oo_is_external in current_structdef.objectoptions) then
  1537. begin
  1538. { In Java, constructors are not automatically inherited (so you can
  1539. hide them). Emulate the Pascal behaviour for classes implemented
  1540. in Pascal (we cannot do it for classes implemented in Java, since
  1541. we obviously cannot add constructors to those) }
  1542. if is_javaclass(current_structdef) then
  1543. begin
  1544. add_missing_parent_constructors_intf(tobjectdef(current_structdef),true,vis_none);
  1545. {$ifdef jvm}
  1546. maybe_add_public_default_java_constructor(tobjectdef(current_structdef));
  1547. jvm_wrap_virtual_class_methods(tobjectdef(current_structdef));
  1548. {$endif}
  1549. end;
  1550. { need method to hold the initialization code for typed constants? }
  1551. if (target_info.system in systems_typed_constants_node_init) and
  1552. not is_any_interface_kind(current_structdef) then
  1553. add_typedconst_init_routine(current_structdef);
  1554. end;
  1555. symtablestack.pop(current_structdef.symtable);
  1556. end;
  1557. { generate vmt space if needed }
  1558. if not(oo_has_vmt in current_structdef.objectoptions) and
  1559. not(oo_is_forward in current_structdef.objectoptions) and
  1560. not(parse_generic) and
  1561. { no vmt for helpers ever }
  1562. not is_objectpascal_helper(current_structdef) and
  1563. (
  1564. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_structdef.objectoptions<>[]) or
  1565. (current_objectdef.objecttype in [odt_class])
  1566. ) then
  1567. current_objectdef.insertvmt;
  1568. { for implemented classes with a vmt check if there is a constructor }
  1569. if (oo_has_vmt in current_structdef.objectoptions) and
  1570. not(oo_is_forward in current_structdef.objectoptions) and
  1571. not(oo_has_constructor in current_structdef.objectoptions) and
  1572. not is_objc_class_or_protocol(current_structdef) and
  1573. not is_java_class_or_interface(current_structdef) then
  1574. Message1(parser_w_virtual_without_constructor,current_structdef.objrealname^);
  1575. if is_interface(current_structdef) or
  1576. is_objcprotocol(current_structdef) or
  1577. is_javainterface(current_structdef) then
  1578. setinterfacemethodoptions
  1579. else if is_objcclass(current_structdef) then
  1580. setobjcclassmethodoptions;
  1581. { we need to add this helper to the extendeddefs of the current module,
  1582. as the global and static symtable are not pushed onto the symtable
  1583. stack again (it will be removed when poping the symtable) }
  1584. if is_objectpascal_helper(current_structdef) and
  1585. (current_objectdef.extendeddef.typ<>errordef) then
  1586. begin
  1587. { the topmost symtable must be a static symtable }
  1588. st:=current_structdef.owner;
  1589. while st.symtabletype in [objectsymtable,recordsymtable] do
  1590. st:=st.defowner.owner;
  1591. if st.symtabletype in [staticsymtable,globalsymtable] then
  1592. begin
  1593. if current_objectdef.extendeddef.typ in [recorddef,objectdef] then
  1594. s:=make_mangledname('',tabstractrecorddef(current_objectdef.extendeddef).symtable,'')
  1595. else
  1596. s:=make_mangledname('',current_objectdef.extendeddef.owner,current_objectdef.extendeddef.typesym.name);
  1597. Message1(sym_d_adding_helper_for,s);
  1598. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1599. if not assigned(list) then
  1600. begin
  1601. list:=TFPObjectList.Create(false);
  1602. current_module.extendeddefs.Add(s, list);
  1603. end;
  1604. list.add(current_structdef);
  1605. end;
  1606. end;
  1607. tabstractrecordsymtable(current_objectdef.symtable).addalignmentpadding;
  1608. { return defined objectdef }
  1609. result:=current_objectdef;
  1610. { restore old state }
  1611. current_structdef:=old_current_structdef;
  1612. current_genericdef:=old_current_genericdef;
  1613. current_specializedef:=old_current_specializedef;
  1614. parse_generic:=old_parse_generic;
  1615. end;
  1616. end.