pdecobj.pas 61 KB

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