ptype.pas 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Does parsing types for Free Pascal
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ptype;
  19. interface
  20. uses
  21. globtype,symtable
  22. {$IFDEF NEWST}
  23. ,symbols,defs
  24. {$ENDIF NEWST};
  25. const
  26. { forward types should only be possible inside a TYPE statement }
  27. typecanbeforward : boolean = false;
  28. var
  29. { hack, which allows to use the current parsed }
  30. { object type as function argument type }
  31. testcurobject : byte;
  32. curobjectname : stringid;
  33. { parses a string declaration }
  34. function string_dec : pdef;
  35. { parses a object declaration }
  36. function object_dec(const n : stringid;fd : pobjectdef) : pdef;
  37. { reads a string, file type or a type id and returns a name and }
  38. { pdef }
  39. {$IFDEF NEWST}
  40. procedure single_type(var tt:Tdef;var s : string;isforwarddef:boolean);
  41. procedure read_type(var tt:Tdef;const name : stringid);
  42. {$ELSE}
  43. procedure single_type(var tt:ttype;var s : string;isforwarddef:boolean);
  44. procedure read_type(var tt:ttype;const name : stringid);
  45. {$ENDIF NEWST}
  46. implementation
  47. uses
  48. cobjects,globals,verbose,systems,tokens,
  49. aasm,symconst,types,
  50. {$ifdef GDB}
  51. gdb,
  52. {$endif}
  53. tree,hcodegen,hcgdata,
  54. scanner,pbase,pexpr,pdecl,psub,
  55. {$ifdef newcg}
  56. cgbase,
  57. {$else}
  58. tccnv,
  59. {$endif}
  60. pass_1;
  61. function string_dec : pdef;
  62. { reads a string type with optional length }
  63. { and returns a pointer to the string }
  64. { definition }
  65. var
  66. p : ptree;
  67. d : pdef;
  68. begin
  69. consume(_STRING);
  70. if token=_LECKKLAMMER then
  71. begin
  72. consume(_LECKKLAMMER);
  73. p:=comp_expr(true);
  74. do_firstpass(p);
  75. if not is_constintnode(p) then
  76. Message(cg_e_illegal_expression);
  77. if (p^.value<=0) then
  78. begin
  79. Message(parser_e_invalid_string_size);
  80. p^.value:=255;
  81. end;
  82. consume(_RECKKLAMMER);
  83. if p^.value>255 then
  84. d:=new(pstringdef,longinit(p^.value))
  85. else
  86. if p^.value<>255 then
  87. d:=new(pstringdef,shortinit(p^.value))
  88. else
  89. d:=cshortstringdef;
  90. disposetree(p);
  91. end
  92. else
  93. begin
  94. if cs_ansistrings in aktlocalswitches then
  95. d:=cansistringdef
  96. else
  97. d:=cshortstringdef;
  98. end;
  99. string_dec:=d;
  100. end;
  101. procedure id_type(var tt : ttype;var s : string;isforwarddef:boolean);
  102. { reads a type definition }
  103. { to a appropriating pdef, s gets the name of }
  104. { the type to allow name mangling }
  105. var
  106. is_unit_specific : boolean;
  107. pos : tfileposinfo;
  108. begin
  109. s:=pattern;
  110. pos:=tokenpos;
  111. { classes can be used also in classes }
  112. if (curobjectname=pattern) and aktobjectdef^.is_class then
  113. begin
  114. tt.setdef(aktobjectdef);
  115. consume(_ID);
  116. exit;
  117. end;
  118. { objects can be parameters }
  119. if (testcurobject=2) and (curobjectname=pattern) then
  120. begin
  121. tt.setdef(aktobjectdef);
  122. consume(_ID);
  123. exit;
  124. end;
  125. { try to load the symbol to see if it's a unitsym }
  126. is_unit_specific:=false;
  127. getsym(s,false);
  128. consume(_ID);
  129. if assigned(srsym) and
  130. (srsym^.typ=unitsym) then
  131. begin
  132. consume(_POINT);
  133. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  134. pos:=tokenpos;
  135. s:=pattern;
  136. consume(_ID);
  137. is_unit_specific:=true;
  138. end;
  139. { are we parsing a possible forward def ? }
  140. if isforwarddef and
  141. not(is_unit_specific) then
  142. begin
  143. tt.setdef(new(pforwarddef,init(s,pos)));
  144. exit;
  145. end;
  146. { unknown sym ? }
  147. if not assigned(srsym) then
  148. begin
  149. Message1(sym_e_id_not_found,s);
  150. tt.setdef(generrordef);
  151. exit;
  152. end;
  153. if (srsym^.typ<>typesym) then
  154. begin
  155. Message(type_e_type_id_expected);
  156. tt.setdef(generrordef);
  157. exit;
  158. end;
  159. { Only use the definitions for system/current unit, becuase
  160. they can be refered from the parameters and symbols are not
  161. loaded at that time. A symbol reference to an other unit
  162. is still possible, because it's already loaded (PFV)
  163. can't use in [] here, becuase unitid can be > 255 }
  164. if (ptypesym(srsym)^.owner^.unitid=0) or
  165. (ptypesym(srsym)^.owner^.unitid=1) then
  166. tt.setdef(ptypesym(srsym)^.restype.def)
  167. else
  168. tt.setsym(srsym);
  169. end;
  170. procedure single_type(var tt:ttype;var s : string;isforwarddef:boolean);
  171. { reads a string, file type or a type id and returns a name and }
  172. { pdef }
  173. var
  174. hs : string;
  175. t2 : ttype;
  176. begin
  177. case token of
  178. _STRING:
  179. begin
  180. tt.setdef(string_dec);
  181. s:='STRING';
  182. end;
  183. _FILE:
  184. begin
  185. consume(_FILE);
  186. if token=_OF then
  187. begin
  188. consume(_OF);
  189. single_type(t2,hs,false);
  190. tt.setdef(new(pfiledef,inittyped(t2)));
  191. s:='FILE$OF$'+hs;
  192. end
  193. else
  194. begin
  195. tt.setdef(cfiledef);
  196. s:='FILE';
  197. end;
  198. end;
  199. else
  200. begin
  201. id_type(tt,s,isforwarddef);
  202. end;
  203. end;
  204. end;
  205. function object_dec(const n : stringid;fd : pobjectdef) : pdef;
  206. { this function parses an object or class declaration }
  207. var
  208. actmembertype : tsymoptions;
  209. there_is_a_destructor : boolean;
  210. is_a_class : boolean;
  211. childof : pobjectdef;
  212. aktclass : pobjectdef;
  213. procedure constructor_head;
  214. begin
  215. consume(_CONSTRUCTOR);
  216. { must be at same level as in implementation }
  217. inc(lexlevel);
  218. parse_proc_head(potype_constructor);
  219. dec(lexlevel);
  220. if (cs_constructor_name in aktglobalswitches) and (aktprocsym^.name<>'INIT') then
  221. Message(parser_e_constructorname_must_be_init);
  222. {$ifdef INCLUDEOK}
  223. include(aktclass^.objectoptions,oo_has_constructor);
  224. {$else}
  225. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_constructor];
  226. {$endif}
  227. consume(_SEMICOLON);
  228. begin
  229. if (aktclass^.is_class) then
  230. begin
  231. { CLASS constructors return the created instance }
  232. aktprocsym^.definition^.rettype.def:=aktclass;
  233. end
  234. else
  235. begin
  236. { OBJECT constructors return a boolean }
  237. aktprocsym^.definition^.rettype.setdef(booldef);
  238. end;
  239. end;
  240. end;
  241. procedure property_dec;
  242. var
  243. sym : psym;
  244. propertyparas : plinkedlist;
  245. { returns the matching procedure to access a property }
  246. function get_procdef : pprocdef;
  247. var
  248. p : pprocdef;
  249. begin
  250. p:=pprocsym(sym)^.definition;
  251. get_procdef:=nil;
  252. while assigned(p) do
  253. begin
  254. if equal_paras(p^.para,propertyparas,true) then
  255. break;
  256. p:=p^.nextoverloaded;
  257. end;
  258. get_procdef:=p;
  259. end;
  260. var
  261. hp2,datacoll : pparaitem;
  262. p : ppropertysym;
  263. overriden : psym;
  264. hs : string;
  265. varspez : tvarspez;
  266. sc : pstringcontainer;
  267. s : string;
  268. tt : ttype;
  269. declarepos : tfileposinfo;
  270. pp : pprocdef;
  271. pt : ptree;
  272. propname : stringid;
  273. begin
  274. { check for a class }
  275. if not(aktclass^.is_class) then
  276. Message(parser_e_syntax_error);
  277. consume(_PROPERTY);
  278. new(propertyparas,init);
  279. datacoll:=nil;
  280. if token=_ID then
  281. begin
  282. p:=new(ppropertysym,init(pattern));
  283. propname:=pattern;
  284. consume(_ID);
  285. { property parameters ? }
  286. if token=_LECKKLAMMER then
  287. begin
  288. if (sp_published in current_object_option) then
  289. Message(parser_e_cant_publish_that_property);
  290. { create a list of the parameters in propertyparas }
  291. consume(_LECKKLAMMER);
  292. inc(testcurobject);
  293. repeat
  294. if token=_VAR then
  295. begin
  296. consume(_VAR);
  297. varspez:=vs_var;
  298. end
  299. else if token=_CONST then
  300. begin
  301. consume(_CONST);
  302. varspez:=vs_const;
  303. end
  304. else varspez:=vs_value;
  305. sc:=idlist;
  306. {$ifdef fixLeaksOnError}
  307. strContStack.push(sc);
  308. {$endif fixLeaksOnError}
  309. if token=_COLON then
  310. begin
  311. consume(_COLON);
  312. if token=_ARRAY then
  313. begin
  314. {
  315. if (varspez<>vs_const) and
  316. (varspez<>vs_var) then
  317. begin
  318. varspez:=vs_const;
  319. Message(parser_e_illegal_open_parameter);
  320. end;
  321. }
  322. consume(_ARRAY);
  323. consume(_OF);
  324. { define range and type of range }
  325. tt.setdef(new(parraydef,init(0,-1,s32bitdef)));
  326. { define field type }
  327. single_type(parraydef(tt.def)^.elementtype,s,false);
  328. end
  329. else
  330. single_type(tt,s,false);
  331. end
  332. else
  333. tt.setdef(cformaldef);
  334. repeat
  335. s:=sc^.get_with_tokeninfo(declarepos);
  336. if s='' then
  337. break;
  338. new(hp2,init);
  339. hp2^.paratyp:=varspez;
  340. hp2^.paratype:=tt;
  341. propertyparas^.insert(hp2);
  342. until false;
  343. {$ifdef fixLeaksOnError}
  344. if strContStack.pop <> sc then
  345. writeln('problem with strContStack in ptype');
  346. {$endif fixLeaksOnError}
  347. dispose(sc,done);
  348. until not try_to_consume(_SEMICOLON);
  349. dec(testcurobject);
  350. consume(_RECKKLAMMER);
  351. end;
  352. { overriden property ? }
  353. { force property interface, if there is a property parameter }
  354. if (token=_COLON) or not(propertyparas^.empty) then
  355. begin
  356. consume(_COLON);
  357. single_type(p^.proptype,hs,false);
  358. if (idtoken=_INDEX) then
  359. begin
  360. consume(_INDEX);
  361. pt:=comp_expr(true);
  362. do_firstpass(pt);
  363. if not(is_ordinal(pt^.resulttype)) or
  364. is_64bitint(pt^.resulttype) then
  365. Message(parser_e_invalid_property_index_value);
  366. p^.index:=pt^.value;
  367. p^.indextype.setdef(pt^.resulttype);
  368. include(p^.propoptions,ppo_indexed);
  369. { concat a longint to the para template }
  370. new(hp2,init);
  371. hp2^.paratyp:=vs_value;
  372. hp2^.paratype:=p^.indextype;
  373. propertyparas^.insert(hp2);
  374. disposetree(pt);
  375. end;
  376. { the parser need to know if a property has parameters }
  377. if not(propertyparas^.empty) then
  378. p^.propoptions:=p^.propoptions+[ppo_hasparameters];
  379. end
  380. else
  381. begin
  382. { do an property override }
  383. overriden:=search_class_member(aktclass,propname);
  384. if assigned(overriden) and (overriden^.typ=propertysym) then
  385. begin
  386. p^.dooverride(ppropertysym(overriden));
  387. end
  388. else
  389. begin
  390. p^.proptype.setdef(generrordef);
  391. message(parser_e_no_property_found_to_override);
  392. end;
  393. end;
  394. if (sp_published in current_object_option) and
  395. not(p^.proptype.def^.is_publishable) then
  396. Message(parser_e_cant_publish_that_property);
  397. { create data defcoll to allow correct parameter checks }
  398. new(datacoll,init);
  399. datacoll^.paratyp:=vs_value;
  400. datacoll^.paratype:=p^.proptype;
  401. if (idtoken=_READ) then
  402. begin
  403. p^.readaccess^.clear;
  404. consume(_READ);
  405. sym:=search_class_member(aktclass,pattern);
  406. if not(assigned(sym)) then
  407. begin
  408. Message1(sym_e_unknown_id,pattern);
  409. consume(_ID);
  410. end
  411. else
  412. begin
  413. consume(_ID);
  414. while (token=_POINT) and
  415. ((sym^.typ=varsym) and
  416. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  417. begin
  418. p^.readaccess^.addsym(sym);
  419. consume(_POINT);
  420. getsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  421. if not assigned(srsym) then
  422. Message1(sym_e_illegal_field,pattern);
  423. sym:=srsym;
  424. consume(_ID);
  425. end;
  426. end;
  427. if assigned(sym) then
  428. begin
  429. { search the matching definition }
  430. case sym^.typ of
  431. procsym :
  432. begin
  433. pp:=get_procdef;
  434. if not(assigned(pp)) or
  435. not(is_equal(pp^.rettype.def,p^.proptype.def)) then
  436. Message(parser_e_ill_property_access_sym);
  437. p^.readaccess^.setdef(pp);
  438. end;
  439. varsym :
  440. begin
  441. if not(propertyparas^.empty) or
  442. not(is_equal(pvarsym(sym)^.vartype.def,p^.proptype.def)) then
  443. Message(parser_e_ill_property_access_sym);
  444. end;
  445. else
  446. Message(parser_e_ill_property_access_sym);
  447. end;
  448. p^.readaccess^.addsym(sym);
  449. end;
  450. end;
  451. if (idtoken=_WRITE) then
  452. begin
  453. p^.writeaccess^.clear;
  454. consume(_WRITE);
  455. sym:=search_class_member(aktclass,pattern);
  456. if not(assigned(sym)) then
  457. begin
  458. Message1(sym_e_unknown_id,pattern);
  459. consume(_ID);
  460. end
  461. else
  462. begin
  463. consume(_ID);
  464. while (token=_POINT) and
  465. ((sym^.typ=varsym) and
  466. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  467. begin
  468. p^.writeaccess^.addsym(sym);
  469. consume(_POINT);
  470. getsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  471. if not assigned(srsym) then
  472. Message1(sym_e_illegal_field,pattern);
  473. sym:=srsym;
  474. consume(_ID);
  475. end;
  476. end;
  477. if assigned(sym) then
  478. begin
  479. { search the matching definition }
  480. case sym^.typ of
  481. procsym :
  482. begin
  483. { insert data entry to check access method }
  484. propertyparas^.insert(datacoll);
  485. pp:=get_procdef;
  486. { ... and remove it }
  487. propertyparas^.remove(datacoll);
  488. if not(assigned(pp)) then
  489. Message(parser_e_ill_property_access_sym);
  490. p^.writeaccess^.setdef(pp);
  491. end;
  492. varsym :
  493. begin
  494. if not(propertyparas^.empty) or
  495. not(is_equal(pvarsym(sym)^.vartype.def,p^.proptype.def)) then
  496. Message(parser_e_ill_property_access_sym);
  497. end
  498. else
  499. Message(parser_e_ill_property_access_sym);
  500. end;
  501. p^.writeaccess^.addsym(sym);
  502. end;
  503. end;
  504. include(p^.propoptions,ppo_stored);
  505. if (idtoken=_STORED) then
  506. begin
  507. consume(_STORED);
  508. p^.storedaccess^.clear;
  509. case token of
  510. _ID:
  511. { in the case that idtoken=_DEFAULT }
  512. { we have to do nothing except }
  513. { setting ppo_stored, it's the same }
  514. { as stored true }
  515. if idtoken<>_DEFAULT then
  516. begin
  517. sym:=search_class_member(aktclass,pattern);
  518. if not(assigned(sym)) then
  519. begin
  520. Message1(sym_e_unknown_id,pattern);
  521. consume(_ID);
  522. end
  523. else
  524. begin
  525. consume(_ID);
  526. while (token=_POINT) and
  527. ((sym^.typ=varsym) and
  528. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  529. begin
  530. p^.storedaccess^.addsym(sym);
  531. consume(_POINT);
  532. getsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  533. if not assigned(srsym) then
  534. Message1(sym_e_illegal_field,pattern);
  535. sym:=srsym;
  536. consume(_ID);
  537. end;
  538. end;
  539. if assigned(sym) then
  540. begin
  541. { only non array properties can be stored }
  542. case sym^.typ of
  543. procsym :
  544. begin
  545. pp:=pprocsym(sym)^.definition;
  546. while assigned(pp) do
  547. begin
  548. { the stored function shouldn't have any parameters }
  549. if pp^.para^.empty then
  550. break;
  551. pp:=pp^.nextoverloaded;
  552. end;
  553. { found we a procedure and does it really return a bool? }
  554. if not(assigned(pp)) or
  555. not(is_equal(pp^.rettype.def,booldef)) then
  556. Message(parser_e_ill_property_storage_sym);
  557. p^.storedaccess^.setdef(pp);
  558. end;
  559. varsym :
  560. begin
  561. if not(propertyparas^.empty) or
  562. not(is_equal(pvarsym(sym)^.vartype.def,booldef)) then
  563. Message(parser_e_stored_property_must_be_boolean);
  564. end;
  565. else
  566. Message(parser_e_ill_property_storage_sym);
  567. end;
  568. p^.storedaccess^.addsym(sym);
  569. end;
  570. end;
  571. _FALSE:
  572. begin
  573. consume(_FALSE);
  574. exclude(p^.propoptions,ppo_stored);
  575. end;
  576. _TRUE:
  577. consume(_TRUE);
  578. end;
  579. end;
  580. if (idtoken=_DEFAULT) then
  581. begin
  582. consume(_DEFAULT);
  583. if not(is_ordinal(p^.proptype.def) or
  584. is_64bitint(p^.proptype.def) or
  585. ((p^.proptype.def^.deftype=setdef) and
  586. (psetdef(p^.proptype.def)^.settype=smallset))) or
  587. not(propertyparas^.empty) then
  588. Message(parser_e_property_cant_have_a_default_value);
  589. { Get the result of the default, the firstpass is
  590. needed to support values like -1 }
  591. pt:=comp_expr(true);
  592. do_firstpass(pt);
  593. if p^.proptype.def^.deftype=setdef then
  594. begin
  595. {$ifndef newcg}
  596. {!!!!!!!!!!}
  597. arrayconstructor_to_set(pt);
  598. {$endif newcg}
  599. do_firstpass(pt);
  600. end;
  601. pt:=gentypeconvnode(pt,p^.proptype.def);
  602. do_firstpass(pt);
  603. if not(is_constnode(pt)) then
  604. Message(parser_e_property_default_value_must_const);
  605. if pt^.treetype=setconstn then
  606. p^.default:=plongint(pt^.value_set)^
  607. else
  608. p^.default:=pt^.value;
  609. disposetree(pt);
  610. end
  611. else if (idtoken=_NODEFAULT) then
  612. begin
  613. consume(_NODEFAULT);
  614. p^.default:=0;
  615. end;
  616. symtablestack^.insert(p);
  617. { default property ? }
  618. consume(_SEMICOLON);
  619. if (idtoken=_DEFAULT) then
  620. begin
  621. consume(_DEFAULT);
  622. { overriding a default propertyp is allowed
  623. p2:=search_default_property(aktclass);
  624. if assigned(p2) then
  625. message1(parser_e_only_one_default_property,
  626. pobjectdef(p2^.owner^.defowner)^.objname^)
  627. else
  628. }
  629. begin
  630. include(p^.propoptions,ppo_defaultproperty);
  631. if propertyparas^.empty then
  632. message(parser_e_property_need_paras);
  633. end;
  634. consume(_SEMICOLON);
  635. end;
  636. { clean up }
  637. if assigned(datacoll) then
  638. dispose(datacoll,done);
  639. end
  640. else
  641. begin
  642. consume(_ID);
  643. consume(_SEMICOLON);
  644. end;
  645. dispose(propertyparas,done);
  646. end;
  647. procedure destructor_head;
  648. begin
  649. consume(_DESTRUCTOR);
  650. inc(lexlevel);
  651. parse_proc_head(potype_destructor);
  652. dec(lexlevel);
  653. if (cs_constructor_name in aktglobalswitches) and (aktprocsym^.name<>'DONE') then
  654. Message(parser_e_destructorname_must_be_done);
  655. {$ifdef INCLUDEOK}
  656. include(aktclass^.objectoptions,oo_has_destructor);
  657. {$else}
  658. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_destructor];
  659. {$endif}
  660. consume(_SEMICOLON);
  661. if not(aktprocsym^.definition^.para^.empty) then
  662. if not (m_tp in aktmodeswitches) then
  663. Message(parser_e_no_paras_for_destructor);
  664. { no return value }
  665. aktprocsym^.definition^.rettype.def:=voiddef;
  666. end;
  667. var
  668. hs : string;
  669. pcrd : pclassrefdef;
  670. tt : ttype;
  671. oldprocinfo : pprocinfo;
  672. oldprocsym : pprocsym;
  673. oldparse_only : boolean;
  674. methodnametable,intmessagetable,
  675. strmessagetable,classnamelabel,
  676. fieldtablelabel : pasmlabel;
  677. storetypecanbeforward : boolean;
  678. procedure setclassattributes;
  679. begin
  680. if is_a_class then
  681. begin
  682. {$ifdef INCLUDEOK}
  683. include(aktclass^.objectoptions,oo_is_class);
  684. {$else}
  685. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_is_class];
  686. {$endif}
  687. if (cs_generate_rtti in aktlocalswitches) or
  688. (assigned(aktclass^.childof) and
  689. (oo_can_have_published in aktclass^.childof^.objectoptions)) then
  690. begin
  691. include(aktclass^.objectoptions,oo_can_have_published);
  692. { in "publishable" classes the default access type is published }
  693. actmembertype:=[sp_published];
  694. { don't know if this is necessary (FK) }
  695. current_object_option:=[sp_published];
  696. end;
  697. end;
  698. end;
  699. procedure setclassparent;
  700. begin
  701. { is the current class tobject? }
  702. { so you could define your own tobject }
  703. if (cs_compilesystem in aktmoduleswitches) and
  704. (n='TOBJECT') then
  705. begin
  706. if assigned(fd) then
  707. aktclass:=fd
  708. else
  709. aktclass:=new(pobjectdef,init(n,nil));
  710. class_tobject:=aktclass;
  711. end
  712. else
  713. begin
  714. childof:=class_tobject;
  715. if assigned(fd) then
  716. begin
  717. { the forward of the child must be resolved to get
  718. correct field addresses
  719. }
  720. if (oo_is_forward in childof^.objectoptions) then
  721. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  722. aktclass:=fd;
  723. aktclass^.set_parent(childof);
  724. end
  725. else
  726. begin
  727. aktclass:=new(pobjectdef,init(n,childof));
  728. aktclass^.set_parent(childof);
  729. end;
  730. end;
  731. end;
  732. { generates the vmt for classes as well as for objects }
  733. procedure writevmt;
  734. var
  735. vmtlist : taasmoutput;
  736. {$ifdef WITHDMT}
  737. dmtlabel : pasmlabel;
  738. {$endif WITHDMT}
  739. begin
  740. {$ifdef WITHDMT}
  741. dmtlabel:=gendmt(aktclass);
  742. {$endif WITHDMT}
  743. { this generates the entries }
  744. vmtlist.init;
  745. genvmt(@vmtlist,aktclass);
  746. { write tables for classes, this must be done before the actual
  747. class is written, because we need the labels defined }
  748. if is_a_class then
  749. begin
  750. methodnametable:=genpublishedmethodstable(aktclass);
  751. fieldtablelabel:=aktclass^.generate_field_table;
  752. { rtti }
  753. if (oo_can_have_published in aktclass^.objectoptions) then
  754. aktclass^.generate_rtti;
  755. { write class name }
  756. getdatalabel(classnamelabel);
  757. datasegment^.concat(new(pai_label,init(classnamelabel)));
  758. datasegment^.concat(new(pai_const,init_8bit(length(aktclass^.objname^))));
  759. datasegment^.concat(new(pai_string,init(aktclass^.objname^)));
  760. { generate message and dynamic tables }
  761. if (oo_has_msgstr in aktclass^.objectoptions) then
  762. strmessagetable:=genstrmsgtab(aktclass);
  763. if (oo_has_msgint in aktclass^.objectoptions) then
  764. intmessagetable:=genintmsgtab(aktclass)
  765. else
  766. datasegment^.concat(new(pai_const,init_32bit(0)));
  767. end;
  768. { write debug info }
  769. {$ifdef GDB}
  770. if (cs_debuginfo in aktmoduleswitches) then
  771. begin
  772. do_count_dbx:=true;
  773. if assigned(aktclass^.owner) and assigned(aktclass^.owner^.name) then
  774. datasegment^.concat(new(pai_stabs,init(strpnew('"vmt_'+aktclass^.owner^.name^+n+':S'+
  775. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+aktclass^.vmt_mangledname))));
  776. end;
  777. {$endif GDB}
  778. datasegment^.concat(new(pai_symbol,initname_global(aktclass^.vmt_mangledname,0)));
  779. { determine the size with symtable^.datasize, because }
  780. { size gives back 4 for classes }
  781. datasegment^.concat(new(pai_const,init_32bit(aktclass^.symtable^.datasize)));
  782. datasegment^.concat(new(pai_const,init_32bit(-aktclass^.symtable^.datasize)));
  783. {$ifdef WITHDMT}
  784. if not(is_a_class) then
  785. begin
  786. if assigned(dmtlabel) then
  787. datasegment^.concat(new(pai_const_symbol,init(dmtlabel)))
  788. else
  789. datasegment^.concat(new(pai_const,init_32bit(0)));
  790. end;
  791. {$endif WITHDMT}
  792. { write pointer to parent VMT, this isn't implemented in TP }
  793. { but this is not used in FPC ? (PM) }
  794. { it's not used yet, but the delphi-operators as and is need it (FK) }
  795. { it is not written for parents that don't have any vmt !! }
  796. if assigned(aktclass^.childof) and
  797. (oo_has_vmt in aktclass^.childof^.objectoptions) then
  798. datasegment^.concat(new(pai_const_symbol,initname(aktclass^.childof^.vmt_mangledname)))
  799. else
  800. datasegment^.concat(new(pai_const,init_32bit(0)));
  801. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  802. if is_a_class then
  803. begin
  804. { pointer to class name string }
  805. datasegment^.concat(new(pai_const_symbol,init(classnamelabel)));
  806. { pointer to dynamic table }
  807. if (oo_has_msgint in aktclass^.objectoptions) then
  808. datasegment^.concat(new(pai_const_symbol,init(intmessagetable)))
  809. else
  810. datasegment^.concat(new(pai_const,init_32bit(0)));
  811. { pointer to method table }
  812. if assigned(methodnametable) then
  813. datasegment^.concat(new(pai_const_symbol,init(methodnametable)))
  814. else
  815. datasegment^.concat(new(pai_const,init_32bit(0)));
  816. { pointer to field table }
  817. datasegment^.concat(new(pai_const_symbol,init(fieldtablelabel)));
  818. { pointer to type info of published section }
  819. if (oo_can_have_published in aktclass^.objectoptions) then
  820. datasegment^.concat(new(pai_const_symbol,initname(aktclass^.rtti_name)))
  821. else
  822. datasegment^.concat(new(pai_const,init_32bit(0)));
  823. { inittable for con-/destruction }
  824. {
  825. if aktclass^.needs_inittable then
  826. }
  827. { we generate the init table for classes always, because needs_inittable }
  828. { for classes is always false, it applies only for objects }
  829. datasegment^.concat(new(pai_const_symbol,init(aktclass^.get_inittable_label)));
  830. {
  831. else
  832. datasegment^.concat(new(pai_const,init_32bit(0)));
  833. }
  834. { auto table }
  835. datasegment^.concat(new(pai_const,init_32bit(0)));
  836. { interface table }
  837. datasegment^.concat(new(pai_const,init_32bit(0)));
  838. { table for string messages }
  839. if (oo_has_msgstr in aktclass^.objectoptions) then
  840. datasegment^.concat(new(pai_const_symbol,init(strmessagetable)))
  841. else
  842. datasegment^.concat(new(pai_const,init_32bit(0)));
  843. end;
  844. datasegment^.concatlist(@vmtlist);
  845. vmtlist.done;
  846. { write the size of the VMT }
  847. datasegment^.concat(new(pai_symbol_end,initname(aktclass^.vmt_mangledname)));
  848. end;
  849. procedure readparentclasses;
  850. begin
  851. { reads the parent class }
  852. if token=_LKLAMMER then
  853. begin
  854. consume(_LKLAMMER);
  855. id_type(tt,pattern,false);
  856. childof:=pobjectdef(tt.def);
  857. if (childof^.deftype<>objectdef) then
  858. begin
  859. Message1(type_e_class_type_expected,childof^.typename);
  860. childof:=nil;
  861. aktclass:=new(pobjectdef,init(n,nil));
  862. end
  863. else
  864. begin
  865. { a mix of class and object isn't allowed }
  866. if (childof^.is_class and not is_a_class) or
  867. (not childof^.is_class and is_a_class) then
  868. Message(parser_e_mix_of_classes_and_objects);
  869. { the forward of the child must be resolved to get
  870. correct field addresses }
  871. if assigned(fd) then
  872. begin
  873. if (oo_is_forward in childof^.objectoptions) then
  874. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  875. aktclass:=fd;
  876. { we must inherit several options !!
  877. this was missing !!
  878. all is now done in set_parent
  879. including symtable datasize setting PM }
  880. fd^.set_parent(childof);
  881. end
  882. else
  883. aktclass:=new(pobjectdef,init(n,childof));
  884. end;
  885. consume(_RKLAMMER);
  886. end
  887. { if no parent class, then a class get tobject as parent }
  888. else if is_a_class then
  889. setclassparent
  890. else
  891. aktclass:=new(pobjectdef,init(n,nil));
  892. end;
  893. begin
  894. {Nowadays aktprocsym may already have a value, so we need to save
  895. it.}
  896. oldprocsym:=aktprocsym;
  897. { forward is resolved }
  898. if assigned(fd) then
  899. exclude(fd^.objectoptions,oo_is_forward);
  900. there_is_a_destructor:=false;
  901. actmembertype:=[sp_public];
  902. { objects and class types can't be declared local }
  903. if (symtablestack^.symtabletype<>globalsymtable) and
  904. (symtablestack^.symtabletype<>staticsymtable) then
  905. Message(parser_e_no_local_objects);
  906. storetypecanbeforward:=typecanbeforward;
  907. { for tp mode don't allow forward types }
  908. if (m_tp in aktmodeswitches) and
  909. not (m_delphi in aktmodeswitches) then
  910. typecanbeforward:=false;
  911. { distinguish classes and objects }
  912. case token of
  913. _OBJECT:
  914. begin
  915. is_a_class:=false;
  916. consume(_OBJECT)
  917. end;
  918. _CPPCLASS:
  919. begin
  920. internalerror(2003001);
  921. end;
  922. _INTERFACE:
  923. begin
  924. internalerror(2003002);
  925. end;
  926. _CLASS:
  927. begin
  928. is_a_class:=true;
  929. consume(_CLASS);
  930. if not(assigned(fd)) and (token=_OF) then
  931. begin
  932. { a hack, but it's easy to handle }
  933. { class reference type }
  934. consume(_OF);
  935. single_type(tt,hs,typecanbeforward);
  936. { accept hp1, if is a forward def or a class }
  937. if (tt.def^.deftype=forwarddef) or
  938. ((tt.def^.deftype=objectdef) and pobjectdef(tt.def)^.is_class) then
  939. begin
  940. pcrd:=new(pclassrefdef,init(tt.def));
  941. object_dec:=pcrd;
  942. end
  943. else
  944. begin
  945. object_dec:=generrordef;
  946. Message1(type_e_class_type_expected,generrordef^.typename);
  947. end;
  948. typecanbeforward:=storetypecanbeforward;
  949. exit;
  950. end
  951. { forward class }
  952. else if not(assigned(fd)) and (token=_SEMICOLON) then
  953. begin
  954. { also anonym objects aren't allow (o : object a : longint; end;) }
  955. if n='' then
  956. begin
  957. Message(parser_f_no_anonym_objects)
  958. end;
  959. if (cs_compilesystem in aktmoduleswitches) and
  960. (n='TOBJECT') then
  961. begin
  962. aktclass:=new(pobjectdef,init(n,nil));
  963. class_tobject:=aktclass;
  964. end
  965. else
  966. aktclass:=new(pobjectdef,init(n,nil));
  967. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_is_class,oo_is_forward];
  968. { all classes must have a vmt !! at offset zero }
  969. if not(oo_has_vmt in aktclass^.objectoptions) then
  970. aktclass^.insertvmt;
  971. object_dec:=aktclass;
  972. typecanbeforward:=storetypecanbeforward;
  973. exit;
  974. end;
  975. end;
  976. else
  977. consume(_OBJECT);
  978. end;
  979. { also anonym objects aren't allow (o : object a : longint; end;) }
  980. if n='' then
  981. Message(parser_f_no_anonym_objects);
  982. readparentclasses;
  983. { default access is public }
  984. actmembertype:=[sp_public];
  985. { set class flags and inherits published, if necessary? }
  986. setclassattributes;
  987. aktobjectdef:=aktclass;
  988. aktclass^.symtable^.next:=symtablestack;
  989. symtablestack:=aktclass^.symtable;
  990. testcurobject:=1;
  991. curobjectname:=n;
  992. { new procinfo }
  993. oldprocinfo:=procinfo;
  994. new(procinfo,init);
  995. procinfo^._class:=aktclass;
  996. { short class declaration ? }
  997. if (not is_a_class) or (token<>_SEMICOLON) then
  998. begin
  999. { Parse componenten }
  1000. repeat
  1001. if (sp_private in actmembertype) then
  1002. {$ifdef INCLUDEOK}
  1003. include(aktclass^.objectoptions,oo_has_private);
  1004. {$else}
  1005. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_private];
  1006. {$endif}
  1007. if (sp_protected in actmembertype) then
  1008. {$ifdef INCLUDEOK}
  1009. include(aktclass^.objectoptions,oo_has_protected);
  1010. {$else}
  1011. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_protected];
  1012. {$endif}
  1013. case token of
  1014. _ID : begin
  1015. case idtoken of
  1016. _PRIVATE : begin
  1017. consume(_PRIVATE);
  1018. actmembertype:=[sp_private];
  1019. current_object_option:=[sp_private];
  1020. end;
  1021. _PROTECTED : begin
  1022. consume(_PROTECTED);
  1023. current_object_option:=[sp_protected];
  1024. actmembertype:=[sp_protected];
  1025. end;
  1026. _PUBLIC : begin
  1027. consume(_PUBLIC);
  1028. current_object_option:=[sp_public];
  1029. actmembertype:=[sp_public];
  1030. end;
  1031. _PUBLISHED : begin
  1032. if not(oo_can_have_published in aktclass^.objectoptions) then
  1033. Message(parser_e_cant_have_published);
  1034. consume(_PUBLISHED);
  1035. current_object_option:=[sp_published];
  1036. actmembertype:=[sp_published];
  1037. end;
  1038. else
  1039. read_var_decs(false,true,false);
  1040. end;
  1041. end;
  1042. _PROPERTY : property_dec;
  1043. _PROCEDURE,
  1044. _FUNCTION,
  1045. _CLASS : begin
  1046. oldparse_only:=parse_only;
  1047. parse_only:=true;
  1048. parse_proc_dec;
  1049. {$ifndef newcg}
  1050. parse_object_proc_directives(aktprocsym);
  1051. {$endif newcg}
  1052. if (po_msgint in aktprocsym^.definition^.procoptions) then
  1053. {$ifdef INCLUDEOK}
  1054. include(aktclass^.objectoptions,oo_has_msgint);
  1055. {$else}
  1056. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_msgint];
  1057. {$endif}
  1058. if (po_msgstr in aktprocsym^.definition^.procoptions) then
  1059. {$ifdef INCLUDEOK}
  1060. include(aktclass^.objectoptions,oo_has_msgstr);
  1061. {$else}
  1062. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_msgstr];
  1063. {$endif}
  1064. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1065. {$ifdef INCLUDEOK}
  1066. include(aktclass^.objectoptions,oo_has_virtual);
  1067. {$else}
  1068. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_virtual];
  1069. {$endif}
  1070. parse_only:=oldparse_only;
  1071. end;
  1072. _CONSTRUCTOR : begin
  1073. if not(sp_public in actmembertype) then
  1074. Message(parser_w_constructor_should_be_public);
  1075. oldparse_only:=parse_only;
  1076. parse_only:=true;
  1077. constructor_head;
  1078. {$ifndef newcg}
  1079. parse_object_proc_directives(aktprocsym);
  1080. {$endif newcg}
  1081. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1082. {$ifdef INCLUDEOK}
  1083. include(aktclass^.objectoptions,oo_has_virtual);
  1084. {$else}
  1085. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_virtual];
  1086. {$endif}
  1087. parse_only:=oldparse_only;
  1088. end;
  1089. _DESTRUCTOR : begin
  1090. if there_is_a_destructor then
  1091. Message(parser_n_only_one_destructor);
  1092. there_is_a_destructor:=true;
  1093. if not(sp_public in actmembertype) then
  1094. Message(parser_w_destructor_should_be_public);
  1095. oldparse_only:=parse_only;
  1096. parse_only:=true;
  1097. destructor_head;
  1098. {$ifndef newcg}
  1099. parse_object_proc_directives(aktprocsym);
  1100. {$endif newcg}
  1101. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1102. {$ifdef INCLUDEOK}
  1103. include(aktclass^.objectoptions,oo_has_virtual);
  1104. {$else}
  1105. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_virtual];
  1106. {$endif}
  1107. parse_only:=oldparse_only;
  1108. end;
  1109. _END : begin
  1110. consume(_END);
  1111. break;
  1112. end;
  1113. else
  1114. consume(_ID); { Give a ident expected message, like tp7 }
  1115. end;
  1116. until false;
  1117. current_object_option:=[sp_public];
  1118. end;
  1119. testcurobject:=0;
  1120. curobjectname:='';
  1121. typecanbeforward:=storetypecanbeforward;
  1122. { generate vmt space if needed }
  1123. if not(oo_has_vmt in aktclass^.objectoptions) and
  1124. ([oo_has_virtual,oo_has_constructor,oo_has_destructor,oo_is_class]*aktclass^.objectoptions<>[]) then
  1125. aktclass^.insertvmt;
  1126. if (cs_create_smart in aktmoduleswitches) then
  1127. datasegment^.concat(new(pai_cut,init));
  1128. if (oo_has_vmt in aktclass^.objectoptions) then
  1129. writevmt;
  1130. { restore old state }
  1131. symtablestack:=symtablestack^.next;
  1132. aktobjectdef:=nil;
  1133. {Restore procinfo}
  1134. dispose(procinfo,done);
  1135. procinfo:=oldprocinfo;
  1136. {Restore the aktprocsym.}
  1137. aktprocsym:=oldprocsym;
  1138. object_dec:=aktclass;
  1139. end;
  1140. { reads a record declaration }
  1141. function record_dec : pdef;
  1142. var
  1143. symtable : psymtable;
  1144. storetypecanbeforward : boolean;
  1145. begin
  1146. { create recdef }
  1147. symtable:=new(psymtable,init(recordsymtable));
  1148. record_dec:=new(precorddef,init(symtable));
  1149. { update symtable stack }
  1150. symtable^.next:=symtablestack;
  1151. symtablestack:=symtable;
  1152. { parse record }
  1153. consume(_RECORD);
  1154. storetypecanbeforward:=typecanbeforward;
  1155. { for tp mode don't allow forward types }
  1156. if m_tp in aktmodeswitches then
  1157. typecanbeforward:=false;
  1158. read_var_decs(true,false,false);
  1159. consume(_END);
  1160. typecanbeforward:=storetypecanbeforward;
  1161. { may be scale record size to a size of n*4 ? }
  1162. symtablestack^.datasize:=align(symtablestack^.datasize,symtablestack^.dataalignment);
  1163. { restore symtable stack }
  1164. symtablestack:=symtable^.next;
  1165. end;
  1166. { reads a type definition and returns a pointer to it }
  1167. procedure read_type(var tt : ttype;const name : stringid);
  1168. var
  1169. pt : ptree;
  1170. tt2 : ttype;
  1171. aufdef : penumdef;
  1172. {aufsym : penumsym;}
  1173. ap : parraydef;
  1174. s : stringid;
  1175. l,v : longint;
  1176. oldaktpackrecords : tpackrecords;
  1177. hs : string;
  1178. defpos,storepos : tfileposinfo;
  1179. procedure expr_type;
  1180. var
  1181. pt1,pt2 : ptree;
  1182. begin
  1183. { use of current parsed object ? }
  1184. if (token=_ID) and (testcurobject=2) and (curobjectname=pattern) then
  1185. begin
  1186. consume(_ID);
  1187. tt.setdef(aktobjectdef);
  1188. exit;
  1189. end;
  1190. { classes can be used also in classes }
  1191. if (curobjectname=pattern) and aktobjectdef^.is_class then
  1192. begin
  1193. tt.setdef(aktobjectdef);
  1194. consume(_ID);
  1195. exit;
  1196. end;
  1197. { we can't accept a equal in type }
  1198. pt1:=comp_expr(not(ignore_equal));
  1199. do_firstpass(pt1);
  1200. if (token=_POINTPOINT) then
  1201. begin
  1202. consume(_POINTPOINT);
  1203. { get high value of range }
  1204. pt2:=comp_expr(not(ignore_equal));
  1205. do_firstpass(pt2);
  1206. { both must be evaluated to constants now }
  1207. if (pt1^.treetype<>ordconstn) or (pt2^.treetype<>ordconstn) then
  1208. Message(sym_e_error_in_type_def)
  1209. else
  1210. begin
  1211. { check types }
  1212. if CheckTypes(pt1^.resulttype,pt2^.resulttype) then
  1213. begin
  1214. { Check bounds }
  1215. if pt2^.value<pt1^.value then
  1216. Message(cg_e_upper_lower_than_lower)
  1217. else
  1218. begin
  1219. { All checks passed, create the new def }
  1220. case pt1^.resulttype^.deftype of
  1221. enumdef : tt.setdef(new(penumdef,init_subrange(penumdef(pt1^.resulttype),pt1^.value,pt2^.value)));
  1222. orddef : begin
  1223. if is_char(pt1^.resulttype) then
  1224. tt.setdef(new(porddef,init(uchar,pt1^.value,pt2^.value)))
  1225. else
  1226. if is_boolean(pt1^.resulttype) then
  1227. tt.setdef(new(porddef,init(bool8bit,pt1^.value,pt2^.value)))
  1228. else
  1229. tt.setdef(new(porddef,init(uauto,pt1^.value,pt2^.value)));
  1230. end;
  1231. end;
  1232. end;
  1233. end;
  1234. end;
  1235. disposetree(pt2);
  1236. end
  1237. else
  1238. begin
  1239. { a simple type renaming }
  1240. if (pt1^.treetype=typen) then
  1241. begin
  1242. if assigned(pt1^.typenodesym) then
  1243. tt.setsym(pt1^.typenodesym)
  1244. else
  1245. tt.setdef(pt1^.resulttype);
  1246. end
  1247. else
  1248. Message(sym_e_error_in_type_def);
  1249. end;
  1250. disposetree(pt1);
  1251. end;
  1252. procedure array_dec;
  1253. var
  1254. lowval,
  1255. highval : longint;
  1256. arraytype : pdef;
  1257. begin
  1258. consume(_ARRAY);
  1259. consume(_LECKKLAMMER);
  1260. { defaults }
  1261. arraytype:=generrordef;
  1262. lowval:=$80000000;
  1263. highval:=$7fffffff;
  1264. tt.reset;
  1265. repeat
  1266. { read the expression and check it }
  1267. pt:=expr;
  1268. if pt^.treetype=typen then
  1269. begin
  1270. case pt^.resulttype^.deftype of
  1271. enumdef :
  1272. begin
  1273. lowval:=penumdef(pt^.resulttype)^.min;
  1274. highval:=penumdef(pt^.resulttype)^.max;
  1275. arraytype:=pt^.resulttype;
  1276. end;
  1277. orddef :
  1278. begin
  1279. lowval:=porddef(pt^.resulttype)^.low;
  1280. highval:=porddef(pt^.resulttype)^.high;
  1281. arraytype:=pt^.resulttype;
  1282. end;
  1283. else
  1284. Message(sym_e_error_in_type_def);
  1285. end;
  1286. end
  1287. else
  1288. begin
  1289. do_firstpass(pt);
  1290. if (pt^.treetype=rangen) then
  1291. begin
  1292. if (pt^.left^.treetype=ordconstn) and
  1293. (pt^.right^.treetype=ordconstn) then
  1294. begin
  1295. lowval:=pt^.left^.value;
  1296. highval:=pt^.right^.value;
  1297. if highval<lowval then
  1298. begin
  1299. Message(parser_e_array_lower_less_than_upper_bound);
  1300. highval:=lowval;
  1301. end;
  1302. arraytype:=pt^.right^.resulttype;
  1303. end
  1304. else
  1305. Message(type_e_cant_eval_constant_expr);
  1306. end
  1307. else
  1308. Message(sym_e_error_in_type_def)
  1309. end;
  1310. disposetree(pt);
  1311. { create arraydef }
  1312. if not assigned(tt.def) then
  1313. begin
  1314. ap:=new(parraydef,init(lowval,highval,arraytype));
  1315. tt.setdef(ap);
  1316. end
  1317. else
  1318. begin
  1319. ap^.elementtype.setdef(new(parraydef,init(lowval,highval,arraytype)));
  1320. ap:=parraydef(ap^.elementtype.def);
  1321. end;
  1322. if token=_COMMA then
  1323. consume(_COMMA)
  1324. else
  1325. break;
  1326. until false;
  1327. consume(_RECKKLAMMER);
  1328. consume(_OF);
  1329. read_type(tt2,'');
  1330. { if no error, set element type }
  1331. if assigned(ap) then
  1332. ap^.elementtype:=tt2;
  1333. end;
  1334. begin
  1335. tt.reset;
  1336. case token of
  1337. _STRING,_FILE:
  1338. begin
  1339. single_type(tt,hs,false);
  1340. end;
  1341. _LKLAMMER:
  1342. begin
  1343. consume(_LKLAMMER);
  1344. { allow negativ value_str }
  1345. l:=-1;
  1346. {aufsym := Nil;}
  1347. aufdef:=new(penumdef,init);
  1348. repeat
  1349. s:=pattern;
  1350. defpos:=tokenpos;
  1351. consume(_ID);
  1352. if token=_ASSIGNMENT then
  1353. begin
  1354. consume(_ASSIGNMENT);
  1355. v:=get_intconst;
  1356. { please leave that a note, allows type save }
  1357. { declarations in the win32 units ! }
  1358. if v<=l then
  1359. Message(parser_n_duplicate_enum);
  1360. l:=v;
  1361. end
  1362. else
  1363. inc(l);
  1364. storepos:=tokenpos;
  1365. tokenpos:=defpos;
  1366. constsymtable^.insert(new(penumsym,init(s,aufdef,l)));
  1367. tokenpos:=storepos;
  1368. if token=_COMMA then
  1369. consume(_COMMA)
  1370. else
  1371. break;
  1372. until false;
  1373. {aufdef^.max:=l;
  1374. if we allow unordered enums
  1375. this can be wrong
  1376. min and max are now set in tenumsym.init PM }
  1377. tt.setdef(aufdef);
  1378. consume(_RKLAMMER);
  1379. end;
  1380. _ARRAY:
  1381. begin
  1382. array_dec;
  1383. end;
  1384. _SET:
  1385. begin
  1386. consume(_SET);
  1387. consume(_OF);
  1388. read_type(tt2,'');
  1389. if assigned(tt2.def) then
  1390. begin
  1391. case tt2.def^.deftype of
  1392. { don't forget that min can be negativ PM }
  1393. enumdef :
  1394. if penumdef(tt2.def)^.min>=0 then
  1395. tt.setdef(new(psetdef,init(tt2.def,penumdef(tt2.def)^.max)))
  1396. else
  1397. Message(sym_e_ill_type_decl_set);
  1398. orddef :
  1399. begin
  1400. case porddef(tt2.def)^.typ of
  1401. uchar :
  1402. tt.setdef(new(psetdef,init(tt2.def,255)));
  1403. u8bit,u16bit,u32bit,
  1404. s8bit,s16bit,s32bit :
  1405. begin
  1406. if (porddef(tt2.def)^.low>=0) then
  1407. tt.setdef(new(psetdef,init(tt2.def,porddef(tt2.def)^.high)))
  1408. else
  1409. Message(sym_e_ill_type_decl_set);
  1410. end;
  1411. else
  1412. Message(sym_e_ill_type_decl_set);
  1413. end;
  1414. end;
  1415. else
  1416. Message(sym_e_ill_type_decl_set);
  1417. end;
  1418. end
  1419. else
  1420. tt.setdef(generrordef);
  1421. end;
  1422. _CARET:
  1423. begin
  1424. consume(_CARET);
  1425. single_type(tt2,hs,typecanbeforward);
  1426. tt.setdef(new(ppointerdef,init(tt2)));
  1427. end;
  1428. _RECORD:
  1429. begin
  1430. tt.setdef(record_dec);
  1431. end;
  1432. _PACKED:
  1433. begin
  1434. consume(_PACKED);
  1435. if token=_ARRAY then
  1436. array_dec
  1437. else
  1438. begin
  1439. oldaktpackrecords:=aktpackrecords;
  1440. aktpackrecords:=packrecord_1;
  1441. if token in [_CLASS,_OBJECT] then
  1442. tt.setdef(object_dec(name,nil))
  1443. else
  1444. tt.setdef(record_dec);
  1445. aktpackrecords:=oldaktpackrecords;
  1446. end;
  1447. end;
  1448. _CLASS,
  1449. {$ifdef SUPPORTCPPCLASS}
  1450. _CPPCLASS,
  1451. {$endif SUPPORTCPPCLASS}
  1452. {$ifdef SUPPORTINTERFACES}
  1453. _INTERFACE,
  1454. {$endif SUPPORTINTERFACES}
  1455. _OBJECT:
  1456. begin
  1457. tt.setdef(object_dec(name,nil));
  1458. end;
  1459. _PROCEDURE:
  1460. begin
  1461. consume(_PROCEDURE);
  1462. tt.setdef(new(pprocvardef,init));
  1463. if token=_LKLAMMER then
  1464. parameter_dec(pprocvardef(tt.def));
  1465. if token=_OF then
  1466. begin
  1467. consume(_OF);
  1468. consume(_OBJECT);
  1469. {$ifdef INCLUDEOK}
  1470. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  1471. {$else}
  1472. pprocvardef(tt.def)^.procoptions:=pprocvardef(tt.def)^.procoptions+[po_methodpointer];
  1473. {$endif}
  1474. end;
  1475. end;
  1476. _FUNCTION:
  1477. begin
  1478. consume(_FUNCTION);
  1479. tt.def:=new(pprocvardef,init);
  1480. if token=_LKLAMMER then
  1481. parameter_dec(pprocvardef(tt.def));
  1482. consume(_COLON);
  1483. single_type(pprocvardef(tt.def)^.rettype,hs,false);
  1484. if token=_OF then
  1485. begin
  1486. consume(_OF);
  1487. consume(_OBJECT);
  1488. {$ifdef INCLUDEOK}
  1489. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  1490. {$else}
  1491. pprocvardef(tt.def)^.procoptions:=pprocvardef(tt.def)^.procoptions+[po_methodpointer];
  1492. {$endif}
  1493. end;
  1494. end;
  1495. else
  1496. expr_type;
  1497. end;
  1498. if tt.def=nil then
  1499. tt.setdef(generrordef);
  1500. end;
  1501. end.
  1502. {
  1503. $Log$
  1504. Revision 1.26 2000-06-13 17:09:56 kaz
  1505. * array type property can have default value, fixed.
  1506. Revision 1.25 2000/06/02 18:48:47 florian
  1507. + fieldtable support for classes
  1508. Revision 1.24 2000/03/27 21:51:19 pierre
  1509. * fix for bug 739
  1510. Revision 1.23 2000/03/19 14:56:38 florian
  1511. * bug 873 fixed
  1512. * some cleanup in objectdec
  1513. Revision 1.22 2000/03/14 16:37:26 pierre
  1514. * destructor can have args in TP mode only (bug825 and 839)
  1515. Revision 1.21 2000/03/11 21:11:24 daniel
  1516. * Ported hcgdata to new symtable.
  1517. * Alignment code changed as suggested by Peter
  1518. + Usage of my is operator replacement, is_object
  1519. Revision 1.20 2000/02/24 18:41:39 peter
  1520. * removed warnings/notes
  1521. Revision 1.19 2000/02/21 22:17:49 florian
  1522. * fixed 819
  1523. Revision 1.18 2000/02/09 13:23:01 peter
  1524. * log truncated
  1525. Revision 1.17 2000/02/05 14:33:32 florian
  1526. * fixed init table generation for classes and arrays
  1527. Revision 1.16 2000/01/28 23:17:53 florian
  1528. * virtual XXXX; support for objects, only if -dWITHDMT is defined
  1529. Revision 1.15 2000/01/27 16:31:40 florian
  1530. * bug 738 fixed
  1531. Revision 1.14 2000/01/11 17:16:06 jonas
  1532. * removed a lot of memory leaks when an error is encountered (caused by
  1533. procinfo and pstringcontainers). There are still plenty left though :)
  1534. Revision 1.13 2000/01/07 01:14:34 peter
  1535. * updated copyright to 2000
  1536. Revision 1.12 1999/11/30 10:40:52 peter
  1537. + ttype, tsymlist
  1538. Revision 1.11 1999/11/26 00:19:12 peter
  1539. * property overriding dereference fix, but it need a bigger redesign
  1540. which i'll do tomorrow. This quick hack is for the lazarus ppl so
  1541. they can hack on mwcustomedit.
  1542. Revision 1.10 1999/11/17 17:05:03 pierre
  1543. * Notes/hints changes
  1544. Revision 1.9 1999/11/11 00:56:54 pierre
  1545. * Enum element reference location corrected
  1546. Revision 1.8 1999/11/09 23:43:09 pierre
  1547. * better browser info
  1548. Revision 1.7 1999/11/08 14:02:16 florian
  1549. * problem with "index X"-properties solved
  1550. * typed constants of class references are now allowed
  1551. Revision 1.6 1999/11/07 23:16:49 florian
  1552. * finally bug 517 solved ...
  1553. Revision 1.5 1999/10/27 16:04:06 peter
  1554. * fixed property reading
  1555. Revision 1.4 1999/10/27 14:17:08 florian
  1556. * property overriding fixed
  1557. Revision 1.3 1999/10/26 12:30:45 peter
  1558. * const parameter is now checked
  1559. * better and generic check if a node can be used for assigning
  1560. * export fixes
  1561. * procvar equal works now (it never had worked at least from 0.99.8)
  1562. * defcoll changed to linkedlist with pparaitem so it can easily be
  1563. walked both directions
  1564. Revision 1.2 1999/10/22 14:37:30 peter
  1565. * error when properties are passed to var parameters
  1566. Revision 1.1 1999/10/22 10:39:35 peter
  1567. * split type reading from pdecl to ptype unit
  1568. * parameter_dec routine is now used for procedure and procvars
  1569. }