pexpr.pas 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Florian Klaempfl
  4. Does parsing of expression 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 pexpr;
  19. interface
  20. uses symtable,tree;
  21. { reads a whole expression }
  22. function expr : ptree;
  23. { reads an expression without assignements and .. }
  24. function comp_expr(accept_equal : boolean):Ptree;
  25. { reads a single factor }
  26. function factor(getaddr : boolean) : ptree;
  27. { the ID token has to be consumed before calling this function }
  28. procedure do_member_read(getaddr : boolean;const sym : psym;var p1 : ptree;
  29. var pd : pdef;var again : boolean);
  30. function get_intconst:longint;
  31. function get_stringconst:string;
  32. implementation
  33. uses
  34. cobjects,globals,scanner,aasm,pass_1,systems,
  35. hcodegen,types,verbose
  36. { parser specific stuff }
  37. ,pbase,pdecl
  38. { processor specific stuff }
  39. {$ifdef i386}
  40. ,i386
  41. {$endif}
  42. {$ifdef m68k}
  43. ,m68k
  44. {$endif}
  45. ;
  46. function parse_paras(_colon,in_prop_paras : boolean) : ptree;
  47. var
  48. p1,p2 : ptree;
  49. end_of_paras : ttoken;
  50. begin
  51. if in_prop_paras then
  52. end_of_paras:=RECKKLAMMER
  53. else
  54. end_of_paras:=RKLAMMER;
  55. if token=end_of_paras then
  56. begin
  57. parse_paras:=nil;
  58. exit;
  59. end;
  60. p2:=nil;
  61. inc(parsing_para_level);
  62. while true do
  63. begin
  64. p1:=comp_expr(true);
  65. p2:=gencallparanode(p1,p2);
  66. { it's for the str(l:5,s); }
  67. if _colon and (token=COLON) then
  68. begin
  69. consume(COLON);
  70. p1:=comp_expr(true);
  71. p2:=gencallparanode(p1,p2);
  72. p2^.is_colon_para:=true;
  73. if token=COLON then
  74. begin
  75. consume(COLON);
  76. p1:=comp_expr(true);
  77. p2:=gencallparanode(p1,p2);
  78. p2^.is_colon_para:=true;
  79. end
  80. end;
  81. if token=COMMA then
  82. consume(COMMA)
  83. else
  84. break;
  85. end;
  86. dec(parsing_para_level);
  87. parse_paras:=p2;
  88. end;
  89. function statement_syssym(l : longint;var pd : pdef) : ptree;
  90. { const regnames:array[R_EAX..R_EDI] of string[3]=
  91. ('EAX','ECX','EDX','EBX','','','ESI','EDI'); }
  92. var
  93. p1,p2 : ptree;
  94. paras : ptree;
  95. prev_in_args : boolean;
  96. Store_valid : boolean;
  97. begin
  98. prev_in_args:=in_args;
  99. Store_valid:=Must_be_valid;
  100. case l of
  101. in_ord_x:
  102. begin
  103. consume(LKLAMMER);
  104. in_args:=true;
  105. Must_be_valid:=true;
  106. p1:=comp_expr(true);
  107. consume(RKLAMMER);
  108. do_firstpass(p1);
  109. p1:=geninlinenode(in_ord_x,p1);
  110. do_firstpass(p1);
  111. statement_syssym := p1;
  112. pd:=p1^.resulttype;
  113. end;
  114. in_break:
  115. begin
  116. statement_syssym:=genzeronode(breakn);
  117. pd:=voiddef;
  118. end;
  119. in_continue:
  120. begin
  121. statement_syssym:=genzeronode(continuen);
  122. pd:=voiddef;
  123. end;
  124. in_typeof_x : begin
  125. consume(LKLAMMER);
  126. in_args:=true;
  127. p1:=comp_expr(true);
  128. consume(RKLAMMER);
  129. pd:=voidpointerdef;
  130. if p1^.treetype=typen then
  131. begin
  132. if (p1^.resulttype=nil) then
  133. begin
  134. Message(sym_e_type_mismatch);
  135. statement_syssym:=genzeronode(errorn);
  136. end
  137. else
  138. if p1^.resulttype^.deftype=objectdef then
  139. statement_syssym:=geninlinenode(in_typeof_x,p1)
  140. else
  141. begin
  142. Message(sym_e_type_mismatch);
  143. statement_syssym:=genzeronode(errorn);
  144. end;
  145. end
  146. else
  147. begin
  148. Must_be_valid:=false;
  149. do_firstpass(p1);
  150. if (p1^.resulttype=nil) then
  151. begin
  152. Message(sym_e_type_mismatch);
  153. statement_syssym:=genzeronode(errorn)
  154. end
  155. else
  156. if p1^.resulttype^.deftype=objectdef then
  157. statement_syssym:=geninlinenode(in_typeof_x,p1)
  158. else
  159. begin
  160. Message(sym_e_type_mismatch);
  161. statement_syssym:=genzeronode(errorn)
  162. end;
  163. end;
  164. end;
  165. in_sizeof_x : begin
  166. consume(LKLAMMER);
  167. in_args:=true;
  168. p1:=comp_expr(true);
  169. consume(RKLAMMER);
  170. pd:=s32bitdef;
  171. if p1^.treetype=typen then
  172. begin
  173. statement_syssym:=genordinalconstnode(
  174. p1^.resulttype^.size,pd);
  175. { p1 not needed !}
  176. disposetree(p1);
  177. end
  178. else
  179. begin
  180. Must_be_valid:=false;
  181. do_firstpass(p1);
  182. if p1^.resulttype^.deftype<>objectdef then
  183. begin
  184. statement_syssym:=genordinalconstnode(
  185. p1^.resulttype^.size,pd);
  186. { p1 not needed !}
  187. disposetree(p1);
  188. end
  189. else
  190. begin
  191. statement_syssym:=geninlinenode(in_sizeof_x,p1);
  192. end;
  193. end;
  194. end;
  195. in_assigned_x : begin
  196. consume(LKLAMMER);
  197. in_args:=true;
  198. p1:=comp_expr(true);
  199. Must_be_valid:=true;
  200. do_firstpass(p1);
  201. case p1^.resulttype^.deftype of
  202. pointerdef,procvardef,
  203. classrefdef:
  204. ;
  205. objectdef:
  206. if not(pobjectdef(p1^.resulttype)^.isclass) then
  207. Message(parser_e_illegal_parameter_list);
  208. else Message(parser_e_illegal_parameter_list);
  209. end;
  210. p2:=gencallparanode(p1,nil);
  211. p2:=geninlinenode(in_assigned_x,p2);
  212. consume(RKLAMMER);
  213. pd:=booldef;
  214. statement_syssym:=p2;
  215. end;
  216. in_ofs_x : begin
  217. consume(LKLAMMER);
  218. in_args:=true;
  219. p1:=comp_expr(true);
  220. p1:=gensinglenode(addrn,p1);
  221. Must_be_valid:=false;
  222. do_firstpass(p1);
  223. { Ofs() returns a longint, not a pointer }
  224. p1^.resulttype:=u32bitdef;
  225. pd:=p1^.resulttype;
  226. consume(RKLAMMER);
  227. statement_syssym:=p1;
  228. end;
  229. in_seg_x : begin
  230. consume(LKLAMMER);
  231. in_args:=true;
  232. p1:=comp_expr(true);
  233. do_firstpass(p1);
  234. if p1^.location.loc<>LOC_REFERENCE then
  235. Message(cg_e_illegal_expression);
  236. p1:=genordinalconstnode(0,s32bitdef);
  237. Must_be_valid:=false;
  238. pd:=s32bitdef;
  239. consume(RKLAMMER);
  240. statement_syssym:=p1;
  241. end;
  242. in_high_x,
  243. in_low_x : begin
  244. consume(LKLAMMER);
  245. in_args:=true;
  246. p1:=comp_expr(true);
  247. do_firstpass(p1);
  248. Must_be_valid:=false;
  249. p2:=geninlinenode(l,p1);
  250. consume(RKLAMMER);
  251. pd:=s32bitdef;
  252. statement_syssym:=p2;
  253. end;
  254. in_succ_x,
  255. in_pred_x : begin
  256. consume(LKLAMMER);
  257. in_args:=true;
  258. p1:=comp_expr(true);
  259. do_firstpass(p1);
  260. Must_be_valid:=false;
  261. p2:=geninlinenode(l,p1);
  262. consume(RKLAMMER);
  263. pd:=p1^.resulttype;
  264. statement_syssym:=p2;
  265. end;
  266. in_inc_x,
  267. in_dec_x : begin
  268. consume(LKLAMMER);
  269. in_args:=true;
  270. p1:=comp_expr(true);
  271. Must_be_valid:=false;
  272. if token=COMMA then
  273. begin
  274. consume(COMMA);
  275. p2:=gencallparanode(comp_expr(true),nil);
  276. end
  277. else
  278. p2:=nil;
  279. p2:=gencallparanode(p1,p2);
  280. statement_syssym:=geninlinenode(l,p2);
  281. consume(RKLAMMER);
  282. pd:=voiddef;
  283. end;
  284. in_concat_x : begin
  285. consume(LKLAMMER);
  286. in_args:=true;
  287. p2:=nil;
  288. while true do
  289. begin
  290. p1:=comp_expr(true);
  291. Must_be_valid:=true;
  292. do_firstpass(p1);
  293. if not((p1^.resulttype^.deftype=stringdef) or
  294. ((p1^.resulttype^.deftype=orddef) and
  295. (porddef(p1^.resulttype)^.typ=uchar)
  296. )
  297. ) then Message(parser_e_illegal_parameter_list);
  298. if p2<>nil then
  299. p2:=gennode(addn,p2,p1)
  300. else p2:=p1;
  301. if token=COMMA then
  302. consume(COMMA)
  303. else break;
  304. end;
  305. consume(RKLAMMER);
  306. pd:=cstringdef;
  307. statement_syssym:=p2;
  308. end;
  309. in_read_x,
  310. in_readln_x : begin
  311. if token=LKLAMMER then
  312. begin
  313. consume(LKLAMMER);
  314. in_args:=true;
  315. Must_be_valid:=false;
  316. paras:=parse_paras(false,false);
  317. consume(RKLAMMER);
  318. end
  319. else
  320. paras:=nil;
  321. pd:=voiddef;
  322. p1:=geninlinenode(l,paras);
  323. do_firstpass(p1);
  324. statement_syssym := p1;
  325. end;
  326. in_write_x,
  327. in_writeln_x : begin
  328. if token=LKLAMMER then
  329. begin
  330. consume(LKLAMMER);
  331. in_args:=true;
  332. Must_be_valid:=true;
  333. paras:=parse_paras(true,false);
  334. consume(RKLAMMER);
  335. end
  336. else
  337. paras:=nil;
  338. pd:=voiddef;
  339. p1 := geninlinenode(l,paras);
  340. do_firstpass(p1);
  341. statement_syssym := p1;
  342. end;
  343. in_str_x_string : begin
  344. consume(LKLAMMER);
  345. in_args:=true;
  346. paras:=parse_paras(true,false);
  347. consume(RKLAMMER);
  348. p1 := geninlinenode(l,paras);
  349. do_firstpass(p1);
  350. statement_syssym := p1;
  351. pd:=voiddef;
  352. end;
  353. in_include_x_y,
  354. in_exclude_x_y:
  355. begin
  356. consume(LKLAMMER);
  357. in_args:=true;
  358. p1:=comp_expr(true);
  359. Must_be_valid:=false;
  360. consume(COMMA);
  361. p2:=comp_expr(true);
  362. { just a bit lisp feeling }
  363. statement_syssym:=geninlinenode(l,
  364. gencallparanode(p1,gencallparanode(p2,nil)));
  365. consume(RKLAMMER);
  366. pd:=voiddef;
  367. end;
  368. {in_val_x : begin
  369. consume(LKLAMMER);
  370. paras:=parse_paras(false);
  371. consume(RKLAMMER);
  372. p1 := geninlinenode(l,paras);
  373. do_firstpass(p1);
  374. statement_syssym := p1;
  375. pd:=voiddef;
  376. end; }
  377. else internalerror(15);
  378. end;
  379. in_args:=prev_in_args;
  380. Must_be_valid:=Store_valid;
  381. end;
  382. { reads the parameter for a subroutine call }
  383. procedure do_proc_call(getaddr : boolean;var again : boolean;var p1:Ptree;var pd:Pdef);
  384. var
  385. prev_in_args : boolean;
  386. prevafterassn : boolean;
  387. begin
  388. prev_in_args:=in_args;
  389. prevafterassn:=afterassignment;
  390. afterassignment:=false;
  391. { want we only determine the address of }
  392. { a subroutine ? }
  393. if not(getaddr) then
  394. begin
  395. if token=LKLAMMER then
  396. begin
  397. consume(LKLAMMER);
  398. in_args:=true;
  399. p1^.left:=parse_paras(false,false);
  400. consume(RKLAMMER);
  401. end
  402. else p1^.left:=nil;
  403. { do firstpass because we need the }
  404. { result type }
  405. do_firstpass(p1);
  406. end
  407. else
  408. begin
  409. { address operator @: }
  410. p1^.left:=nil;
  411. { forget pd }
  412. pd:=nil;
  413. { no postfix operators }
  414. again:=false;
  415. end;
  416. pd:=p1^.resulttype;
  417. in_args:=prev_in_args;
  418. afterassignment:=prevafterassn;
  419. end;
  420. { the following procedure handles the access to a property symbol }
  421. procedure handle_propertysym(sym : psym;var p1 : ptree;
  422. var pd : pdef);
  423. var
  424. paras : ptree;
  425. p2 : ptree;
  426. begin
  427. paras:=nil;
  428. { property parameters? }
  429. if token=LECKKLAMMER then
  430. begin
  431. consume(LECKKLAMMER);
  432. paras:=parse_paras(false,true);
  433. consume(RECKKLAMMER);
  434. end;
  435. { indexed property }
  436. if (ppropertysym(sym)^.options and ppo_indexed)<>0 then
  437. begin
  438. p2:=genordinalconstnode(ppropertysym(sym)^.index,s32bitdef);
  439. paras:=gencallparanode(p2,paras);
  440. end;
  441. if not(afterassignment) and not(in_args) then
  442. begin
  443. { write property: }
  444. { no result }
  445. pd:=voiddef;
  446. if assigned(ppropertysym(sym)^.writeaccesssym) then
  447. begin
  448. if ppropertysym(sym)^.writeaccesssym^.typ=procsym then
  449. begin
  450. { generate the method call }
  451. p1:=genmethodcallnode(pprocsym(
  452. ppropertysym(sym)^.writeaccesssym),
  453. ppropertysym(sym)^.writeaccesssym^.owner,p1);
  454. { we know the procedure to call, so
  455. force the usage of that procedure }
  456. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.writeaccessdef);
  457. p1^.left:=paras;
  458. consume(ASSIGNMENT);
  459. { read the expression }
  460. p2:=comp_expr(true);
  461. p1^.left:=gencallparanode(p2,p1^.left);
  462. end
  463. else if ppropertysym(sym)^.writeaccesssym^.typ=varsym then
  464. begin
  465. if assigned(paras) then
  466. message(parser_e_no_paras_allowed);
  467. { subscribed access? }
  468. if p1=nil then
  469. begin
  470. p1:=genloadnode(pvarsym(ppropertysym(sym)^.readaccesssym),
  471. ppropertysym(sym)^.readaccesssym^.owner);
  472. end
  473. else
  474. p1:=gensubscriptnode(pvarsym(
  475. ppropertysym(sym)^.readaccesssym),p1);
  476. consume(ASSIGNMENT);
  477. { read the expression }
  478. p2:=comp_expr(true);
  479. p1:=gennode(assignn,p1,p2);
  480. end
  481. else
  482. begin
  483. p1:=genzeronode(errorn);
  484. Message(parser_e_no_procedure_to_access_property);
  485. end;
  486. end
  487. else
  488. begin
  489. p1:=genzeronode(errorn);
  490. Message(parser_e_no_procedure_to_access_property);
  491. end;
  492. end
  493. else
  494. begin
  495. { read property: }
  496. pd:=ppropertysym(sym)^.proptype;
  497. if assigned(ppropertysym(sym)^.readaccesssym) then
  498. begin
  499. if ppropertysym(sym)^.readaccesssym^.typ=varsym then
  500. begin
  501. if assigned(paras) then
  502. message(parser_e_no_paras_allowed);
  503. { subscribed access? }
  504. if p1=nil then
  505. begin
  506. p1:=genloadnode(pvarsym(ppropertysym(sym)^.readaccesssym),
  507. ppropertysym(sym)^.readaccesssym^.owner);
  508. end
  509. else
  510. p1:=gensubscriptnode(pvarsym(
  511. ppropertysym(sym)^.readaccesssym),p1);
  512. end
  513. else if ppropertysym(sym)^.readaccesssym^.typ=procsym then
  514. begin
  515. { generate the method call }
  516. p1:=genmethodcallnode(pprocsym(
  517. ppropertysym(sym)^.readaccesssym),
  518. ppropertysym(sym)^.readaccesssym^.owner,p1);
  519. { we know the procedure to call, so
  520. force the usage of that procedure }
  521. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.readaccessdef);
  522. { insert paras }
  523. p1^.left:=paras;
  524. { if we should be delphi compatible }
  525. { then force type conversion }
  526. { }
  527. { isn't neccessary, the result types }
  528. { have to match excatly }
  529. {if cs_delphi2_compatible in aktswitches then
  530. p1:=gentypeconvnode(p1,pd);
  531. }
  532. end
  533. else
  534. begin
  535. p1:=genzeronode(errorn);
  536. Message(sym_e_type_mismatch);
  537. end;
  538. end
  539. else
  540. begin
  541. { error, no function to read property }
  542. p1:=genzeronode(errorn);
  543. Message(parser_e_no_procedure_to_access_property);
  544. end;
  545. end;
  546. end;
  547. { the ID token has to be consumed before calling this function }
  548. procedure do_member_read(getaddr : boolean;const sym : psym;var p1 : ptree;
  549. var pd : pdef;var again : boolean);
  550. var
  551. static_name : string;
  552. isclassref : boolean;
  553. begin
  554. if sym=nil then
  555. begin
  556. { pattern is still valid unless
  557. there is another ID just after the ID of sym }
  558. Message1(sym_e_id_no_member,pattern);
  559. disposetree(p1);
  560. p1:=genzeronode(errorn);
  561. { try to clean up }
  562. pd:=generrordef;
  563. again:=false;
  564. end
  565. else
  566. begin
  567. isclassref:=pd^.deftype=classrefdef;
  568. { check protected and private members }
  569. if ((sym^.properties and sp_private)<>0) and
  570. (pobjectdef(sym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  571. Message(parser_e_cant_access_private_member);
  572. { this is wrong protected should not be overwritten but
  573. can be called !! PM
  574. if ((sym^.properties and sp_protected)<>0) and
  575. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  576. Message(parser_e_cant_access_protected_member); }
  577. { we assume, that only procsyms and varsyms are in an object }
  578. { symbol table, for classes, properties are allowed }
  579. case sym^.typ of
  580. procsym:
  581. begin
  582. p1:=genmethodcallnode(pprocsym(sym),srsymtable,p1);
  583. do_proc_call(getaddr,again,p1,pd);
  584. { now we know the real method e.g. we can check for }
  585. { a class method }
  586. if isclassref and ((p1^.procdefinition^.options and (poclassmethod or poconstructor))=0) then
  587. Message(parser_e_only_class_methods_via_class_ref);
  588. end;
  589. varsym:
  590. begin
  591. if ((sym^.properties and sp_protected)<>0) and
  592. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) and
  593. not(afterassignment) and
  594. not(in_args) then
  595. Message(parser_e_cant_access_protected_member);
  596. if isclassref then
  597. Message(parser_e_only_class_methods_via_class_ref);
  598. if (sym^.properties and sp_static)<>0 then
  599. begin
  600. static_name:=lower(srsymtable^.name^)+'_'+sym^.name;
  601. getsym(static_name,true);
  602. disposetree(p1);
  603. p1:=genloadnode(pvarsym(srsym),srsymtable);
  604. end
  605. else
  606. p1:=gensubscriptnode(pvarsym(sym),p1);
  607. pd:=pvarsym(sym)^.definition;
  608. end;
  609. propertysym:
  610. begin
  611. if isclassref then
  612. Message(parser_e_only_class_methods_via_class_ref);
  613. handle_propertysym(sym,p1,pd);
  614. end;
  615. else internalerror(16);
  616. end;
  617. end;
  618. end;
  619. function factor(getaddr : boolean) : ptree;
  620. var
  621. l : longint;
  622. p1,p2,p3 : ptree;
  623. code : word;
  624. pd,pd2 : pdef;
  625. unit_specific, again : boolean;
  626. static_name : string;
  627. sym : pvarsym;
  628. classh : pobjectdef;
  629. d : bestreal;
  630. constset : pconstset;
  631. propsym : ppropertysym;
  632. oldp1 : ptree;
  633. filepos : tfileposinfo;
  634. procedure check_tokenpos;
  635. begin
  636. if (p1<>oldp1) then
  637. begin
  638. if assigned(p1) then
  639. set_tree_filepos(p1,filepos);
  640. oldp1:=p1;
  641. filepos:=tokenpos;
  642. end;
  643. end;
  644. { p1 and p2 must contain valid values }
  645. procedure postfixoperators;
  646. begin
  647. check_tokenpos;
  648. while again do
  649. begin
  650. case token of
  651. CARET:
  652. begin
  653. consume(CARET);
  654. if pd^.deftype<>pointerdef then
  655. begin
  656. { ^ as binary operator is a problem!!!! (FK) }
  657. again:=false;
  658. Message(cg_e_invalid_qualifier);
  659. disposetree(p1);
  660. p1:=genzeronode(errorn);
  661. end
  662. else
  663. begin
  664. p1:=gensinglenode(derefn,p1);
  665. pd:=ppointerdef(pd)^.definition;
  666. end;
  667. end;
  668. LECKKLAMMER:
  669. begin
  670. if (pd^.deftype=objectdef) and
  671. pobjectdef(pd)^.isclass then
  672. begin
  673. { default property }
  674. propsym:=search_default_property(pobjectdef(pd));
  675. if not(assigned(propsym)) then
  676. begin
  677. disposetree(p1);
  678. p1:=genzeronode(errorn);
  679. again:=false;
  680. message(parser_e_no_default_property_available);
  681. end
  682. else
  683. begin
  684. p1:=nil;
  685. handle_propertysym(propsym,p1,pd);
  686. end;
  687. end
  688. else
  689. begin
  690. consume(LECKKLAMMER);
  691. repeat
  692. if (pd^.deftype<>arraydef) and
  693. (pd^.deftype<>stringdef) and
  694. (pd^.deftype<>pointerdef) then
  695. begin
  696. Message(cg_e_invalid_qualifier);
  697. disposetree(p1);
  698. p1:=genzeronode(errorn);
  699. again:=false;
  700. end
  701. else if (pd^.deftype=pointerdef) then
  702. begin
  703. p2:=comp_expr(true);
  704. p1:=gennode(vecn,p1,p2);
  705. pd:=ppointerdef(pd)^.definition;
  706. end
  707. else
  708. begin
  709. p2:=comp_expr(true);
  710. {$ifdef i386}
  711. { support SEG:OFS for go32v2 Mem[] }
  712. if (target_info.target=target_GO32V2) and
  713. (p1^.treetype=loadn) and
  714. assigned(p1^.symtableentry) and
  715. assigned(p1^.symtableentry^.owner^.name) and
  716. (p1^.symtableentry^.owner^.name^='SYSTEM') and
  717. ((p1^.symtableentry^.name='MEM') or
  718. (p1^.symtableentry^.name='MEMW') or
  719. (p1^.symtableentry^.name='MEML')) then
  720. begin
  721. if (token=COLON) then
  722. begin
  723. consume(COLON);
  724. p3:=gennode(muln,genordinalconstnode($10,s32bitdef),p2);
  725. p2:=comp_expr(true);
  726. p2:=gennode(addn,p2,p3);
  727. p1:=gennode(vecn,p1,p2);
  728. p1^.memseg:=true;
  729. p1^.memindex:=true;
  730. end
  731. else
  732. begin
  733. p1:=gennode(vecn,p1,p2);
  734. p1^.memindex:=true;
  735. end;
  736. end
  737. else
  738. {$endif}
  739. p1:=gennode(vecn,p1,p2);
  740. if pd^.deftype=stringdef then
  741. pd:=cchardef
  742. else
  743. pd:=parraydef(pd)^.definition;
  744. end;
  745. if token=COMMA then consume(COMMA)
  746. else break;
  747. until false;
  748. consume(RECKKLAMMER);
  749. end;
  750. end;
  751. POINT:
  752. begin
  753. consume(POINT);
  754. case pd^.deftype of
  755. recorddef:
  756. begin
  757. sym:=pvarsym(precdef(pd)^.symtable^.search(pattern));
  758. consume(ID);
  759. if sym=nil then
  760. begin
  761. Message(sym_e_illegal_field);
  762. disposetree(p1);
  763. p1:=genzeronode(errorn);
  764. end
  765. else
  766. begin
  767. p1:=gensubscriptnode(sym,p1);
  768. pd:=sym^.definition;
  769. end;
  770. end;
  771. classrefdef:
  772. begin
  773. classh:=pobjectdef(pclassrefdef(pd)^.definition);
  774. sym:=nil;
  775. while assigned(classh) do
  776. begin
  777. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  778. srsymtable:=classh^.publicsyms;
  779. if assigned(sym) then
  780. break;
  781. classh:=classh^.childof;
  782. end;
  783. consume(ID);
  784. do_member_read(false,sym,p1,pd,again);
  785. end;
  786. objectdef:
  787. begin
  788. classh:=pobjectdef(pd);
  789. sym:=nil;
  790. while assigned(classh) do
  791. begin
  792. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  793. srsymtable:=classh^.publicsyms;
  794. if assigned(sym) then
  795. break;
  796. classh:=classh^.childof;
  797. end;
  798. consume(ID);
  799. do_member_read(false,sym,p1,pd,again);
  800. end;
  801. pointerdef:
  802. begin
  803. if ppointerdef(pd)^.definition^.deftype
  804. in [recorddef,objectdef,classrefdef] then
  805. begin
  806. Message(cg_e_invalid_qualifier);
  807. Message(parser_h_maybe_deref_caret_missing);
  808. end
  809. else
  810. Message(cg_e_invalid_qualifier);
  811. end
  812. else
  813. begin
  814. Message(cg_e_invalid_qualifier);
  815. disposetree(p1);
  816. p1:=genzeronode(errorn);
  817. end;
  818. end;
  819. end;
  820. else
  821. begin
  822. { is this a procedure variable ? }
  823. if assigned(pd) then
  824. begin
  825. if (pd^.deftype=procvardef) then
  826. begin
  827. if getprocvar then
  828. again:=false
  829. else
  830. if (token=LKLAMMER) or
  831. ((pprocvardef(pd)^.para1=nil) and
  832. (token<>ASSIGNMENT) and (not in_args)) then
  833. begin
  834. { do this in a strange way }
  835. { it's not a clean solution }
  836. p2:=p1;
  837. p1:=gencallnode(nil,
  838. nil);
  839. p1^.right:=p2;
  840. p1^.unit_specific:=unit_specific;
  841. if token=LKLAMMER then
  842. begin
  843. consume(LKLAMMER);
  844. p1^.left:=parse_paras(false,false);
  845. consume(RKLAMMER);
  846. end;
  847. pd:=pprocvardef(pd)^.retdef;
  848. p1^.resulttype:=pd;
  849. end
  850. else again:=false;
  851. p1^.resulttype:=pd;
  852. end
  853. else again:=false;
  854. end
  855. else again:=false;
  856. end;
  857. end;
  858. check_tokenpos;
  859. end;
  860. end;
  861. procedure do_set(p : pconstset;pos : longint);
  862. var
  863. l : longint;
  864. begin
  865. if (pos>255) or
  866. (pos<0) then
  867. Message(parser_e_illegal_set_expr);
  868. l:=pos div 8;
  869. { do we allow the same twice }
  870. if (p^[l] and (1 shl (pos mod 8)))<>0 then
  871. Message(parser_e_illegal_set_expr);
  872. p^[l]:=p^[l] or (1 shl (pos mod 8));
  873. end;
  874. {$ifdef TEST_FUNCRET}
  875. function is_func_ret(sym : psym) : boolean;
  876. var
  877. p : pprocinfo;
  878. begin
  879. p:=@procinfo;
  880. is_func_ret:=false;
  881. while assigned(p) do
  882. begin
  883. { is this an access to a function result ? }
  884. if assigned(aktprocsym) and
  885. ((sym^.name=aktprocsym^.name){ or
  886. ((pvarsym(srsym)=opsym) and
  887. ((p^.flags and pi_operator)<>0))}) and
  888. (p^.retdef<>pdef(voiddef)) and
  889. (token<>LKLAMMER) and
  890. (not ((cs_tp_compatible in aktswitches) and
  891. (afterassignment or in_args))) then
  892. begin
  893. p1:=genzeronode(funcretn);
  894. pd:=p^.retdef;
  895. p1^.funcretprocinfo:=p;
  896. p1^.retdef:=pd;
  897. is_func_ret:=true;
  898. exit;
  899. end;
  900. p:=p^.parent;
  901. end;
  902. end;
  903. {$endif TEST_FUNCRET}
  904. var
  905. possible_error : boolean;
  906. begin
  907. oldp1:=nil;
  908. filepos:=tokenpos;
  909. case token of
  910. ID:
  911. begin
  912. { allow post fix operators }
  913. again:=true;
  914. if (cs_delphi2_compatible in aktmoduleswitches) and
  915. (pattern='RESULT') and
  916. assigned(aktprocsym) and
  917. (procinfo.retdef<>pdef(voiddef)) then
  918. begin
  919. consume(ID);
  920. p1:=genzeronode(funcretn);
  921. pd:=procinfo.retdef;
  922. {$ifdef TEST_FUNCRET}
  923. p1^.funcretprocinfo:=pointer(@procinfo);
  924. p1^.retdef:=pd;
  925. {$endif TEST_FUNCRET}
  926. end
  927. else
  928. begin
  929. if lastsymknown then
  930. begin
  931. srsym:=lastsrsym;
  932. srsymtable:=lastsrsymtable;
  933. lastsymknown:=false;
  934. end
  935. else
  936. getsym(pattern,true);
  937. consume(ID);
  938. {$ifndef TEST_FUNCRET}
  939. { is this an access to a function result ? }
  940. if assigned(aktprocsym) and
  941. ((srsym^.name=aktprocsym^.name){ or
  942. ((pvarsym(srsym)=opsym) and
  943. ((procinfo.flags and pi_operator)<>0))}) and
  944. (procinfo.retdef<>pdef(voiddef)) and
  945. (token<>LKLAMMER) and
  946. (not ((cs_tp_compatible in aktmoduleswitches) and
  947. (afterassignment or in_args))) then
  948. begin
  949. p1:=genzeronode(funcretn);
  950. pd:=procinfo.retdef;
  951. end
  952. else
  953. {$else TEST_FUNCRET}
  954. if not is_func_ret(srsym) then
  955. {$endif TEST_FUNCRET}
  956. { else it's a normal symbol }
  957. begin
  958. if srsym^.typ=unitsym then
  959. begin
  960. consume(POINT);
  961. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  962. unit_specific:=true;
  963. consume(ID);
  964. end
  965. else
  966. unit_specific:=false;
  967. if not assigned(srsym) then
  968. Begin
  969. p1:=genzeronode(errorn);
  970. { try to clean up }
  971. pd:=generrordef;
  972. end
  973. else
  974. Begin
  975. { check semantics of private }
  976. if (srsym^.typ in [propertysym,procsym,varsym]) and
  977. (srsymtable^.symtabletype=objectsymtable) then
  978. begin
  979. if ((srsym^.properties and sp_private)<>0) and
  980. (pobjectdef(srsym^.owner^.defowner)^.
  981. owner^.symtabletype=unitsymtable) then
  982. Message(parser_e_cant_access_private_member);
  983. end;
  984. case srsym^.typ of
  985. absolutesym:
  986. begin
  987. p1:=genloadnode(pvarsym(srsym),srsymtable);
  988. pd:=pabsolutesym(srsym)^.definition;
  989. end;
  990. varsym:
  991. begin
  992. { are we in a class method ? }
  993. if (srsymtable^.symtabletype=objectsymtable) and
  994. assigned(aktprocsym) and
  995. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  996. Message(parser_e_only_class_methods);
  997. if (srsym^.properties and sp_static)<>0 then
  998. begin
  999. static_name:=lower(srsymtable^.name^)+'_'+srsym^.name;
  1000. getsym(static_name,true);
  1001. end;
  1002. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1003. if pvarsym(srsym)^.is_valid=0 then
  1004. begin
  1005. p1^.is_first := true;
  1006. { set special between first loaded
  1007. until checked in firstpass }
  1008. pvarsym(srsym)^.is_valid:=2;
  1009. end;
  1010. pd:=pvarsym(srsym)^.definition;
  1011. end;
  1012. typedconstsym:
  1013. begin
  1014. p1:=gentypedconstloadnode(ptypedconstsym(srsym),srsymtable);
  1015. pd:=ptypedconstsym(srsym)^.definition;
  1016. end;
  1017. syssym:
  1018. p1:=statement_syssym(psyssym(srsym)^.number,pd);
  1019. typesym:
  1020. begin
  1021. pd:=ptypesym(srsym)^.definition;
  1022. { if we read a type declaration }
  1023. { we have to return the type and }
  1024. { nothing else }
  1025. if block_type=bt_type then
  1026. begin
  1027. p1:=genzeronode(typen);
  1028. p1^.resulttype:=pd;
  1029. pd:=voiddef;
  1030. end
  1031. else
  1032. begin
  1033. if token=LKLAMMER then
  1034. begin
  1035. consume(LKLAMMER);
  1036. p1:=comp_expr(true);
  1037. consume(RKLAMMER);
  1038. p1:=gentypeconvnode(p1,pd);
  1039. p1^.explizit:=true;
  1040. end
  1041. else if (token=POINT) and
  1042. (pd^.deftype=objectdef) and
  1043. ((pobjectdef(pd)^.options and oois_class)=0) then
  1044. begin
  1045. consume(POINT);
  1046. if assigned(procinfo._class) then
  1047. begin
  1048. if procinfo._class^.isrelated(pobjectdef(pd)) then
  1049. begin
  1050. p1:=genzeronode(typen);
  1051. p1^.resulttype:=pd;
  1052. srsymtable:=pobjectdef(pd)^.publicsyms;
  1053. sym:=pvarsym(srsymtable^.search(pattern));
  1054. { search also in inherited methods }
  1055. while sym=nil do
  1056. begin
  1057. pd:=pobjectdef(pd)^.childof;
  1058. srsymtable:=pobjectdef(pd)^.publicsyms;
  1059. sym:=pvarsym(srsymtable^.search(pattern));
  1060. end;
  1061. consume(ID);
  1062. do_member_read(false,sym,p1,pd,again);
  1063. end
  1064. else
  1065. begin
  1066. Message(parser_e_no_super_class);
  1067. pd:=generrordef;
  1068. again:=false;
  1069. end;
  1070. end
  1071. else
  1072. begin
  1073. { allows @TObject.Load }
  1074. { also allows static methods and variables }
  1075. p1:=genzeronode(typen);
  1076. p1^.resulttype:=pd;
  1077. {
  1078. srsymtable:=pobjectdef(pd)^.publicsyms;
  1079. sym:=pvarsym(srsymtable^.search(pattern));
  1080. }
  1081. { TP allows also @TMenu.Load if Load is only }
  1082. { defined in an anchestor class }
  1083. sym:=pvarsym(search_class_member(pobjectdef(pd),pattern));
  1084. if not(getaddr) and
  1085. ((sym^.properties and sp_static)=0) then
  1086. Message(sym_e_only_static_in_static)
  1087. else
  1088. begin
  1089. consume(ID);
  1090. do_member_read(getaddr,sym,p1,pd,again);
  1091. end;
  1092. end
  1093. end
  1094. else
  1095. begin
  1096. { class reference ? }
  1097. if (pd^.deftype=objectdef)
  1098. and ((pobjectdef(pd)^.options and oois_class)<>0) then
  1099. begin
  1100. p1:=genzeronode(typen);
  1101. p1^.resulttype:=pd;
  1102. pd:=new(pclassrefdef,init(pd));
  1103. p1:=gensinglenode(loadvmtn,p1);
  1104. p1^.resulttype:=pd;
  1105. end
  1106. else
  1107. begin
  1108. { generate a type node }
  1109. { (for typeof etc) }
  1110. p1:=genzeronode(typen);
  1111. p1^.resulttype:=pd;
  1112. pd:=voiddef;
  1113. end;
  1114. end;
  1115. end;
  1116. end;
  1117. enumsym:
  1118. begin
  1119. p1:=genenumnode(penumsym(srsym));
  1120. pd:=p1^.resulttype;
  1121. end;
  1122. constsym:
  1123. begin
  1124. case pconstsym(srsym)^.consttype of
  1125. constint:
  1126. p1:=genordinalconstnode(pconstsym(srsym)^.value,s32bitdef);
  1127. conststring:
  1128. p1:=genstringconstnode(pstring(pconstsym(srsym)^.value)^);
  1129. constchar:
  1130. p1:=genordinalconstnode(pconstsym(srsym)^.value,cchardef);
  1131. constreal:
  1132. p1:=genrealconstnode(pdouble(pconstsym(srsym)^.value)^);
  1133. constbool:
  1134. p1:=genordinalconstnode(pconstsym(srsym)^.value,booldef);
  1135. constseta:
  1136. p1:=gensetconstruktnode(pconstset(pconstsym(srsym)^.value),
  1137. psetdef(pconstsym(srsym)^.definition));
  1138. constord:
  1139. p1:=genordinalconstnode(pconstsym(srsym)^.value,
  1140. pconstsym(srsym)^.definition);
  1141. end;
  1142. pd:=p1^.resulttype;
  1143. end;
  1144. procsym:
  1145. begin
  1146. { are we in a class method ? }
  1147. possible_error:=(srsymtable^.symtabletype=objectsymtable) and
  1148. assigned(aktprocsym) and
  1149. ((aktprocsym^.definition^.options and poclassmethod)<>0);
  1150. p1:=gencallnode(pprocsym(srsym),srsymtable);
  1151. p1^.unit_specific:=unit_specific;
  1152. do_proc_call(getaddr,again,p1,pd);
  1153. if possible_error and
  1154. ((p1^.procdefinition^.options and poclassmethod)=0) then
  1155. Message(parser_e_only_class_methods);
  1156. end;
  1157. propertysym:
  1158. begin
  1159. { access to property in a method }
  1160. { are we in a class method ? }
  1161. if (srsymtable^.symtabletype=objectsymtable) and
  1162. assigned(aktprocsym) and
  1163. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  1164. Message(parser_e_only_class_methods);
  1165. { no method pointer }
  1166. p1:=nil;
  1167. handle_propertysym(srsym,p1,pd);
  1168. end;
  1169. errorsym:
  1170. begin
  1171. p1:=genzeronode(errorn);
  1172. pd:=generrordef;
  1173. if token=LKLAMMER then
  1174. begin
  1175. consume(LKLAMMER);
  1176. parse_paras(false,false);
  1177. consume(RKLAMMER);
  1178. end;
  1179. end;
  1180. else
  1181. begin
  1182. p1:=genzeronode(errorn);
  1183. pd:=generrordef;
  1184. Message(cg_e_illegal_expression);
  1185. end;
  1186. end; { end case }
  1187. end;
  1188. end;
  1189. end;
  1190. { handle post fix operators }
  1191. postfixoperators;
  1192. end;
  1193. _NEW : begin
  1194. consume(_NEW);
  1195. consume(LKLAMMER);
  1196. p1:=factor(false);
  1197. if p1^.treetype<>typen then
  1198. Message(sym_e_type_id_expected);
  1199. pd:=p1^.resulttype;
  1200. pd2:=pd;
  1201. if (pd^.deftype<>pointerdef) or
  1202. (ppointerdef(pd)^.definition^.deftype<>objectdef) then
  1203. begin
  1204. Message(parser_e_pointer_to_class_expected);
  1205. { if an error occurs, read til the end of the new }
  1206. { statement }
  1207. p1:=genzeronode(errorn);
  1208. l:=1;
  1209. while true do
  1210. begin
  1211. case token of
  1212. LKLAMMER : inc(l);
  1213. RKLAMMER : dec(l);
  1214. end;
  1215. consume(token);
  1216. if l=0 then
  1217. break;
  1218. end;
  1219. end
  1220. else
  1221. begin
  1222. disposetree(p1);
  1223. p1:=genzeronode(hnewn);
  1224. p1^.resulttype:=ppointerdef(pd)^.definition;
  1225. consume(COMMA);
  1226. afterassignment:=false;
  1227. { determines the current object defintion }
  1228. classh:=pobjectdef(ppointerdef(pd)^.definition);
  1229. { check for an abstract class }
  1230. if (classh^.options and oois_abstract)<>0 then
  1231. Message(sym_e_no_instance_of_abstract_object);
  1232. { search the constructor also in the symbol tables of }
  1233. { the parents }
  1234. { no constructor found }
  1235. sym:=nil;
  1236. while assigned(classh) do
  1237. begin
  1238. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  1239. srsymtable:=classh^.publicsyms;
  1240. if assigned(sym) then
  1241. break;
  1242. classh:=classh^.childof;
  1243. end;
  1244. consume(ID);
  1245. do_member_read(false,sym,p1,pd,again);
  1246. if (p1^.treetype<>calln) or
  1247. (assigned(p1^.procdefinition) and
  1248. ((p1^.procdefinition^.options and poconstructor)=0)) then
  1249. Message(parser_e_expr_have_to_be_constructor_call);
  1250. p1:=gensinglenode(newn,p1);
  1251. { set the resulttype }
  1252. p1^.resulttype:=pd2;
  1253. consume(RKLAMMER);
  1254. end;
  1255. end;
  1256. _SELF:
  1257. begin
  1258. again:=true;
  1259. consume(_SELF);
  1260. if not assigned(procinfo._class) then
  1261. begin
  1262. p1:=genzeronode(errorn);
  1263. pd:=generrordef;
  1264. again:=false;
  1265. Message(parser_e_self_not_in_method);
  1266. end
  1267. else
  1268. begin
  1269. if (aktprocsym^.definition^.options and poclassmethod)<>0 then
  1270. begin
  1271. { self in class methods is a class reference type }
  1272. pd:=new(pclassrefdef,init(procinfo._class));
  1273. p1:=genselfnode(pd);
  1274. p1^.resulttype:=pd;
  1275. end
  1276. else
  1277. begin
  1278. p1:=genselfnode(procinfo._class);
  1279. p1^.resulttype:=procinfo._class;
  1280. end;
  1281. pd:=p1^.resulttype;
  1282. postfixoperators;
  1283. end;
  1284. end;
  1285. _INHERITED : begin
  1286. again:=true;
  1287. consume(_INHERITED);
  1288. if assigned(procinfo._class) then
  1289. begin
  1290. classh:=procinfo._class^.childof;
  1291. while assigned(classh) do
  1292. begin
  1293. srsymtable:=pobjectdef(classh)^.publicsyms;
  1294. sym:=pvarsym(srsymtable^.search(pattern));
  1295. if assigned(sym) then
  1296. begin
  1297. p1:=genzeronode(typen);
  1298. p1^.resulttype:=classh;
  1299. pd:=p1^.resulttype;
  1300. consume(ID);
  1301. do_member_read(false,sym,p1,pd,again);
  1302. break;
  1303. end;
  1304. classh:=classh^.childof;
  1305. end;
  1306. if classh=nil then
  1307. begin
  1308. Message1(sym_e_id_no_member,pattern);
  1309. again:=false;
  1310. pd:=generrordef;
  1311. p1:=genzeronode(errorn);
  1312. end;
  1313. end
  1314. else
  1315. Message(parser_e_generic_methods_only_in_methods);
  1316. postfixoperators;
  1317. end;
  1318. INTCONST : begin
  1319. valint(pattern,l,code);
  1320. if code<>0 then
  1321. begin
  1322. val(pattern,d,code);
  1323. if code<>0 then
  1324. begin
  1325. Message(cg_e_invalid_integer);
  1326. l:=1;
  1327. consume(INTCONST);
  1328. p1:=genordinalconstnode(l,s32bitdef);
  1329. end
  1330. else
  1331. begin
  1332. consume(INTCONST);
  1333. p1:=genrealconstnode(d);
  1334. end;
  1335. end
  1336. else
  1337. begin
  1338. consume(INTCONST);
  1339. p1:=genordinalconstnode(l,s32bitdef);
  1340. end;
  1341. end;
  1342. REALNUMBER : begin
  1343. val(pattern,d,code);
  1344. if code<>0 then
  1345. begin
  1346. Message(parser_e_error_in_real);
  1347. d:=1.0;
  1348. end;
  1349. consume(REALNUMBER);
  1350. p1:=genrealconstnode(d);
  1351. end;
  1352. { FILE and STRING can be also a type cast }
  1353. _STRING:
  1354. begin
  1355. pd:=stringtype;
  1356. consume(LKLAMMER);
  1357. p1:=comp_expr(true);
  1358. consume(RKLAMMER);
  1359. p1:=gentypeconvnode(p1,pd);
  1360. p1^.explizit:=true;
  1361. { handle postfix operators here e.g. string(a)[10] }
  1362. again:=true;
  1363. postfixoperators;
  1364. end;
  1365. _FILE:
  1366. begin
  1367. pd:=cfiledef;
  1368. consume(_FILE);
  1369. if token=LKLAMMER then
  1370. begin
  1371. consume(LKLAMMER);
  1372. p1:=comp_expr(true);
  1373. consume(RKLAMMER);
  1374. p1:=gentypeconvnode(p1,pd);
  1375. end
  1376. else
  1377. begin
  1378. p1:=genzeronode(typen);
  1379. p1^.resulttype:=pd;
  1380. end;
  1381. p1^.explizit:=true;
  1382. { handle postfix operators here e.g. string(a)[10] }
  1383. again:=true;
  1384. postfixoperators;
  1385. end;
  1386. CSTRING:
  1387. begin
  1388. p1:=genstringconstnode(pattern);
  1389. consume(CSTRING);
  1390. end;
  1391. CCHAR:
  1392. begin
  1393. p1:=genordinalconstnode(ord(pattern[1]),cchardef);
  1394. consume(CCHAR);
  1395. end;
  1396. KLAMMERAFFE : begin
  1397. consume(KLAMMERAFFE);
  1398. p1:=factor(true);
  1399. p1:=gensinglenode(addrn,p1);
  1400. end;
  1401. LKLAMMER : begin
  1402. consume(LKLAMMER);
  1403. p1:=comp_expr(true);
  1404. consume(RKLAMMER);
  1405. { it's not a good solution }
  1406. { but (a+b)^ makes some problems }
  1407. case token of
  1408. CARET,POINT,LECKKLAMMER:
  1409. begin
  1410. { we need the resulttype }
  1411. { of the expression in pd }
  1412. do_firstpass(p1);
  1413. pd:=p1^.resulttype;
  1414. again:=true;
  1415. postfixoperators;
  1416. end;
  1417. end;
  1418. end;
  1419. LECKKLAMMER : begin
  1420. consume(LECKKLAMMER);
  1421. new(constset);
  1422. for l:=0 to 31 do
  1423. constset^[l]:=0;
  1424. p2:=nil;
  1425. pd:=nil;
  1426. if token<>RECKKLAMMER then
  1427. while true do
  1428. begin
  1429. p1:=comp_expr(true);
  1430. do_firstpass(p1);
  1431. case p1^.treetype of
  1432. ordconstn : begin
  1433. if porddef(p1^.resulttype)=s32bitdef then
  1434. p1^.resulttype:=u8bitdef;
  1435. if pd=nil then
  1436. pd:=p1^.resulttype;
  1437. if not(is_equal(pd,p1^.resulttype)) then
  1438. Message(parser_e_typeconflict_in_set)
  1439. else
  1440. if token=POINTPOINT then
  1441. begin
  1442. consume(POINTPOINT);
  1443. p3:=comp_expr(true);
  1444. do_firstpass(p3);
  1445. if porddef(p3^.resulttype)=s32bitdef then
  1446. p3^.resulttype:=u8bitdef;
  1447. if not(is_equal(pd,p3^.resulttype)) then
  1448. Message(parser_e_typeconflict_in_set)
  1449. else
  1450. for l:=p1^.value to p3^.value do
  1451. do_set(constset,l);
  1452. disposetree(p3);
  1453. end
  1454. else
  1455. do_set(constset,p1^.value);
  1456. disposetree(p1);
  1457. end;
  1458. stringconstn : begin
  1459. if pd=nil then
  1460. pd:=cchardef;
  1461. if not(is_equal(pd,cchardef)) then
  1462. Message(parser_e_typeconflict_in_set)
  1463. else
  1464. for l:=1 to length(pstring(p1^.values)^) do
  1465. do_set(constset,ord(pstring(p1^.values)^[l]));
  1466. disposetree(p1);
  1467. end;
  1468. else
  1469. begin
  1470. if porddef(p1^.resulttype)=s32bitdef then
  1471. p1^.resulttype:=u8bitdef;
  1472. if pd=nil then
  1473. pd:=p1^.resulttype;
  1474. if not(is_equal(pd,p1^.resulttype)) then
  1475. Message(parser_e_typeconflict_in_set);
  1476. p2:=gennode(setelen,p1,p2);
  1477. end;
  1478. end;
  1479. if token=COMMA then
  1480. consume(COMMA)
  1481. else break;
  1482. end;
  1483. consume(RECKKLAMMER);
  1484. p1:=gensinglenode(setconstrn,p2);
  1485. { use smallset if emudef with max < 32
  1486. but allow sets for enumdefs with more than
  1487. 256 elements like tasmop !! }
  1488. if assigned(pd) and (pd^.deftype=enumdef) and (penumdef(pd)^.max<=255) then
  1489. p1^.resulttype:=new(psetdef,init(pd,penumdef(pd)^.max))
  1490. else
  1491. p1^.resulttype:=new(psetdef,init(pd,255));
  1492. p1^.constset:=constset;
  1493. end;
  1494. PLUS : begin
  1495. consume(PLUS);
  1496. p1:=factor(false);
  1497. end;
  1498. MINUS : begin
  1499. consume(MINUS);
  1500. p1:=factor(false);
  1501. p1:=gensinglenode(umminusn,p1);
  1502. end;
  1503. _NOT : begin
  1504. consume(_NOT);
  1505. p1:=factor(false);
  1506. p1:=gensinglenode(notn,p1);
  1507. end;
  1508. _TRUE : begin
  1509. consume(_TRUE);
  1510. p1:=genordinalconstnode(1,booldef);
  1511. end;
  1512. _FALSE : begin
  1513. consume(_FALSE);
  1514. p1:=genordinalconstnode(0,booldef);
  1515. end;
  1516. _NIL : begin
  1517. consume(_NIL);
  1518. p1:=genzeronode(niln);
  1519. end;
  1520. else
  1521. begin
  1522. p1:=genzeronode(errorn);
  1523. consume(token);
  1524. Message(cg_e_illegal_expression);
  1525. end;
  1526. end;
  1527. factor:=p1;
  1528. check_tokenpos;
  1529. end;
  1530. type Toperator_precedence=(opcompare,opaddition,opmultiply);
  1531. const tok2node:array[PLUS.._XOR] of Ttreetyp=
  1532. (addn,subn,muln,slashn,equaln,gtn,ltn,gten,lten,
  1533. isn,asn,inn,
  1534. symdifn,starstarn,nothingn,caretn,unequaln,nothingn,
  1535. nothingn,nothingn,nothingn,nothingn,nothingn,
  1536. nothingn,nothingn,nothingn,nothingn,nothingn,
  1537. nothingn,nothingn,nothingn,nothingn,nothingn,
  1538. nothingn,andn,nothingn,nothingn,nothingn,
  1539. nothingn,nothingn,nothingn,nothingn,nothingn,
  1540. nothingn,nothingn,divn,nothingn,nothingn,
  1541. nothingn,nothingn,nothingn,nothingn,nothingn,
  1542. nothingn,nothingn,nothingn,nothingn,nothingn,
  1543. nothingn,nothingn,nothingn,nothingn,nothingn,
  1544. modn,nothingn,nothingn,nothingn,nothingn,
  1545. nothingn,nothingn,orn,
  1546. nothingn,nothingn,nothingn,nothingn,nothingn,
  1547. nothingn,nothingn,shln,shrn,
  1548. nothingn,nothingn,nothingn,nothingn,nothingn,
  1549. nothingn,nothingn,nothingn,nothingn,nothingn,
  1550. nothingn,xorn);
  1551. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1552. ([LT,LTE,GT,GTE,EQUAL,UNEQUAL,_IN,_IS],
  1553. [PLUS,MINUS,_OR,_XOR],
  1554. [CARET,SYMDIF,STARSTAR,STAR,SLASH,_DIV,_MOD,_AND,_SHL,_SHR,_AS]);
  1555. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):Ptree;
  1556. {Reads a subexpression while the operators are of the current precedence
  1557. level, or any higher level. Replaces the old term, simpl_expr and
  1558. simpl2_expr.}
  1559. var p1,p2:Ptree;
  1560. oldt:Ttoken;
  1561. filepos : tfileposinfo;
  1562. begin
  1563. { if pred_level=high(Toperator_precedence) then }
  1564. if pred_level=opmultiply then
  1565. p1:=factor(getprocvar)
  1566. else
  1567. p1:=sub_expr(succ(pred_level),true);
  1568. repeat
  1569. { aweful hack to support const a : 1..2=1; }
  1570. { disadvantage of tables :) FK }
  1571. if (token in operator_levels[pred_level]) and
  1572. ((token<>EQUAL) or accept_equal) then
  1573. begin
  1574. oldt:=token;
  1575. filepos:=tokenpos;
  1576. consume(token);
  1577. { if pred_level=high(Toperator_precedence) then }
  1578. if pred_level=opmultiply then
  1579. p2:=factor(getprocvar)
  1580. else
  1581. p2:=sub_expr(succ(pred_level),true);
  1582. p1:=gennode(tok2node[oldt],p1,p2);
  1583. set_tree_filepos(p1,filepos);
  1584. end
  1585. else
  1586. break;
  1587. until false;
  1588. sub_expr:=p1;
  1589. end;
  1590. function comp_expr(accept_equal : boolean):Ptree;
  1591. var
  1592. oldafterassignment : boolean;
  1593. begin
  1594. oldafterassignment:=afterassignment;
  1595. afterassignment:=true;
  1596. comp_expr:=sub_expr(opcompare,accept_equal);
  1597. afterassignment:=oldafterassignment;
  1598. end;
  1599. function expr : ptree;
  1600. var
  1601. p1,p2 : ptree;
  1602. oldafterassignment : boolean;
  1603. oldp1 : ptree;
  1604. filepos : tfileposinfo;
  1605. begin
  1606. oldafterassignment:=afterassignment;
  1607. p1:=sub_expr(opcompare,true);
  1608. if token in [ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  1609. afterassignment:=true;
  1610. filepos:=tokenpos;
  1611. oldp1:=p1;
  1612. case token of
  1613. POINTPOINT : begin
  1614. consume(POINTPOINT);
  1615. p2:=sub_expr(opcompare,true);
  1616. p1:=gennode(rangen,p1,p2);
  1617. end;
  1618. ASSIGNMENT : begin
  1619. consume(ASSIGNMENT);
  1620. { avoid a firstpass of a procedure if
  1621. it must be assigned to a procvar }
  1622. { should be recursive for a:=b:=c !!! }
  1623. if (p1^.resulttype<>nil) and (p1^.resulttype^.deftype=procvardef) then
  1624. getprocvar:=true;
  1625. p2:=sub_expr(opcompare,true);
  1626. if getprocvar and (p2^.treetype=calln) then
  1627. begin
  1628. p2^.treetype:=loadn;
  1629. p2^.resulttype:=pprocsym(p2^.symtableprocentry)^.definition;
  1630. p2^.symtableentry:=p2^.symtableprocentry;
  1631. end;
  1632. getprocvar:=false;
  1633. p1:=gennode(assignn,p1,p2);
  1634. end;
  1635. { this is the code for C like assignements }
  1636. { from an improvement of Peter Schaefer }
  1637. _PLUSASN : begin
  1638. consume(_PLUSASN );
  1639. p2:=sub_expr(opcompare,true);
  1640. p1:=gennode(assignn,p1,gennode(addn,getcopy(p1),p2));
  1641. { was first
  1642. p1:=gennode(assignn,p1,gennode(addn,p1,p2));
  1643. but disposetree assumes that we have a real
  1644. *** tree *** }
  1645. end;
  1646. _MINUSASN : begin
  1647. consume(_MINUSASN );
  1648. p2:=sub_expr(opcompare,true);
  1649. p1:=gennode(assignn,p1,gennode(subn,getcopy(p1),p2));
  1650. end;
  1651. _STARASN : begin
  1652. consume(_STARASN );
  1653. p2:=sub_expr(opcompare,true);
  1654. p1:=gennode(assignn,p1,gennode(muln,getcopy(p1),p2));
  1655. end;
  1656. _SLASHASN : begin
  1657. consume(_SLASHASN );
  1658. p2:=sub_expr(opcompare,true);
  1659. p1:=gennode(assignn,p1,gennode(slashn,getcopy(p1),p2));
  1660. end;
  1661. end;
  1662. afterassignment:=oldafterassignment;
  1663. if p1<>oldp1 then
  1664. set_tree_filepos(p1,filepos);
  1665. expr:=p1;
  1666. end;
  1667. function get_intconst:longint;
  1668. {Reads an expression, tries to evalute it and check if it is an integer
  1669. constant. Then the constant is returned.}
  1670. var p:Ptree;
  1671. begin
  1672. p:=comp_expr(true);
  1673. do_firstpass(p);
  1674. if (p^.treetype<>ordconstn) and
  1675. (p^.resulttype^.deftype=orddef) and
  1676. not (Porddef(p^.resulttype)^.typ in
  1677. [uvoid,uchar,bool8bit]) then
  1678. Message(cg_e_illegal_expression)
  1679. else
  1680. get_intconst:=p^.value;
  1681. disposetree(p);
  1682. end;
  1683. function get_stringconst:string;
  1684. {Reads an expression, tries to evaluate it and checks if it is a string
  1685. constant. Then the constant is returned.}
  1686. var p:Ptree;
  1687. begin
  1688. get_stringconst:='';
  1689. p:=comp_expr(true);
  1690. do_firstpass(p);
  1691. if p^.treetype<>stringconstn then
  1692. if (p^.treetype=ordconstn) and
  1693. (p^.resulttype^.deftype=orddef) and
  1694. (Porddef(p^.resulttype)^.typ=uchar) then
  1695. get_stringconst:=char(p^.value)
  1696. else
  1697. Message(cg_e_illegal_expression)
  1698. else
  1699. get_stringconst:=p^.values^;
  1700. disposetree(p);
  1701. end;
  1702. end.
  1703. {
  1704. $Log$
  1705. Revision 1.31 1998-08-10 14:50:11 peter
  1706. + localswitches, moduleswitches, globalswitches splitting
  1707. Revision 1.30 1998/07/28 21:52:54 florian
  1708. + implementation of raise and try..finally
  1709. + some misc. exception stuff
  1710. Revision 1.29 1998/07/27 21:57:13 florian
  1711. * fix to allow tv like stream registration:
  1712. @tmenu.load doesn't work if load had parameters or if load was only
  1713. declared in an anchestor class of tmenu
  1714. Revision 1.28 1998/07/14 21:46:51 peter
  1715. * updated messages file
  1716. Revision 1.27 1998/06/25 14:04:23 peter
  1717. + internal inc/dec
  1718. Revision 1.26 1998/06/09 16:01:46 pierre
  1719. + added procedure directive parsing for procvars
  1720. (accepted are popstack cdecl and pascal)
  1721. + added C vars with the following syntax
  1722. var C calias 'true_c_name';(can be followed by external)
  1723. reason is that you must add the Cprefix
  1724. which is target dependent
  1725. Revision 1.25 1998/06/05 14:37:33 pierre
  1726. * fixes for inline for operators
  1727. * inline procedure more correctly restricted
  1728. Revision 1.24 1998/06/04 23:51:52 peter
  1729. * m68k compiles
  1730. + .def file creation moved to gendef.pas so it could also be used
  1731. for win32
  1732. Revision 1.23 1998/06/04 09:55:40 pierre
  1733. * demangled name of procsym reworked to become independant of the mangling scheme
  1734. Come test_funcret improvements (not yet working)S: ----------------------------------------------------------------------
  1735. Revision 1.22 1998/06/02 17:03:03 pierre
  1736. * with node corrected for objects
  1737. * small bugs for SUPPORT_MMX fixed
  1738. Revision 1.21 1998/05/27 19:45:05 peter
  1739. * symtable.pas splitted into includefiles
  1740. * symtable adapted for $ifdef NEWPPU
  1741. Revision 1.20 1998/05/26 07:53:59 pierre
  1742. * bug fix for empty sets (nil pd was dereferenced )
  1743. Revision 1.19 1998/05/25 17:11:43 pierre
  1744. * firstpasscount bug fixed
  1745. now all is already set correctly the first time
  1746. under EXTDEBUG try -gp to skip all other firstpasses
  1747. it works !!
  1748. * small bug fixes
  1749. - for smallsets with -dTESTSMALLSET
  1750. - some warnings removed (by correcting code !)
  1751. Revision 1.18 1998/05/23 01:21:20 peter
  1752. + aktasmmode, aktoptprocessor, aktoutputformat
  1753. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1754. + $LIBNAME to set the library name where the unit will be put in
  1755. * splitted cgi386 a bit (codeseg to large for bp7)
  1756. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1757. Revision 1.17 1998/05/22 12:37:03 carl
  1758. * crash bugfix (patched msanually to main branch)
  1759. Revision 1.16 1998/05/21 19:33:32 peter
  1760. + better procedure directive handling and only one table
  1761. Revision 1.15 1998/05/20 09:42:35 pierre
  1762. + UseTokenInfo now default
  1763. * unit in interface uses and implementation uses gives error now
  1764. * only one error for unknown symbol (uses lastsymknown boolean)
  1765. the problem came from the label code !
  1766. + first inlined procedures and function work
  1767. (warning there might be allowed cases were the result is still wrong !!)
  1768. * UseBrower updated gives a global list of all position of all used symbols
  1769. with switch -gb
  1770. Revision 1.14 1998/05/11 13:07:56 peter
  1771. + $ifdef NEWPPU for the new ppuformat
  1772. + $define GDB not longer required
  1773. * removed all warnings and stripped some log comments
  1774. * no findfirst/findnext anymore to remove smartlink *.o files
  1775. Revision 1.13 1998/05/06 08:38:45 pierre
  1776. * better position info with UseTokenInfo
  1777. UseTokenInfo greatly simplified
  1778. + added check for changed tree after first time firstpass
  1779. (if we could remove all the cases were it happen
  1780. we could skip all firstpass if firstpasscount > 1)
  1781. Only with ExtDebug
  1782. Revision 1.12 1998/05/05 12:05:42 florian
  1783. * problems with properties fixed
  1784. * crash fixed: i:=l when i and l are undefined, was a problem with
  1785. implementation of private/protected
  1786. Revision 1.11 1998/05/04 11:22:26 florian
  1787. * problem with DOM solved: it crashes when accessing a property in a method
  1788. Revision 1.10 1998/05/01 16:38:45 florian
  1789. * handling of private and protected fixed
  1790. + change_keywords_to_tp implemented to remove
  1791. keywords which aren't supported by tp
  1792. * break and continue are now symbols of the system unit
  1793. + widestring, longstring and ansistring type released
  1794. Revision 1.9 1998/04/29 10:33:58 pierre
  1795. + added some code for ansistring (not complete nor working yet)
  1796. * corrected operator overloading
  1797. * corrected nasm output
  1798. + started inline procedures
  1799. + added starstarn : use ** for exponentiation (^ gave problems)
  1800. + started UseTokenInfo cond to get accurate positions
  1801. Revision 1.8 1998/04/14 23:27:03 florian
  1802. + exclude/include with constant second parameter added
  1803. Revision 1.7 1998/04/09 23:02:15 florian
  1804. * small problems solved to get remake3 work
  1805. Revision 1.6 1998/04/09 22:16:35 florian
  1806. * problem with previous REGALLOC solved
  1807. * improved property support
  1808. Revision 1.5 1998/04/08 10:26:09 florian
  1809. * correct error handling of virtual constructors
  1810. * problem with new type declaration handling fixed
  1811. Revision 1.4 1998/04/07 22:45:05 florian
  1812. * bug0092, bug0115 and bug0121 fixed
  1813. + packed object/class/array
  1814. Revision 1.3 1998/04/07 13:19:46 pierre
  1815. * bugfixes for reset_gdb_info
  1816. in MEM parsing for go32v2
  1817. better external symbol creation
  1818. support for rhgdb.exe (lowercase file names)
  1819. }