ptype.pas 62 KB

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