ptype.pas 63 KB

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