pdecobj.pas 64 KB

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