ptype.pas 62 KB

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