pdecobj.pas 59 KB

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