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