pdecobj.pas 60 KB

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