pdecobj.pas 57 KB

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