pdecobj.pas 69 KB

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