pdecobj.pas 58 KB

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