pexpr.pas 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596
  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. globtype,systems,tokens,
  35. cobjects,globals,scanner,
  36. symconst,aasm,
  37. hcodegen,types,verbose,strings,
  38. {$ifndef newcg}
  39. tccal,
  40. {$endif newcg}
  41. pass_1,
  42. { parser specific stuff }
  43. pbase,pdecl,
  44. { processor specific stuff }
  45. cpubase,cpuinfo;
  46. const
  47. allow_type : boolean = true;
  48. got_addrn : boolean = false;
  49. function parse_paras(__colon,in_prop_paras : boolean) : ptree;
  50. var
  51. p1,p2 : ptree;
  52. end_of_paras : ttoken;
  53. begin
  54. if in_prop_paras then
  55. end_of_paras:=_RECKKLAMMER
  56. else
  57. end_of_paras:=_RKLAMMER;
  58. if token=end_of_paras then
  59. begin
  60. parse_paras:=nil;
  61. exit;
  62. end;
  63. p2:=nil;
  64. inc(parsing_para_level);
  65. while true do
  66. begin
  67. p1:=comp_expr(true);
  68. p2:=gencallparanode(p1,p2);
  69. { it's for the str(l:5,s); }
  70. if __colon and (token=_COLON) then
  71. begin
  72. consume(_COLON);
  73. p1:=comp_expr(true);
  74. p2:=gencallparanode(p1,p2);
  75. p2^.is_colon_para:=true;
  76. if token=_COLON then
  77. begin
  78. consume(_COLON);
  79. p1:=comp_expr(true);
  80. p2:=gencallparanode(p1,p2);
  81. p2^.is_colon_para:=true;
  82. end
  83. end;
  84. if token=_COMMA then
  85. consume(_COMMA)
  86. else
  87. break;
  88. end;
  89. dec(parsing_para_level);
  90. parse_paras:=p2;
  91. end;
  92. procedure check_tp_procvar(var p : ptree);
  93. var
  94. p1 : ptree;
  95. Store_valid : boolean;
  96. begin
  97. if (m_tp_procvar in aktmodeswitches) and
  98. (not got_addrn) and
  99. (not in_args) and
  100. (p^.treetype=loadn) then
  101. begin
  102. { support if procvar then for tp7 and many other expression like this }
  103. Store_valid:=Must_be_valid;
  104. Must_be_valid:=false;
  105. do_firstpass(p);
  106. Must_be_valid:=Store_valid;
  107. if not(getprocvar) and (p^.resulttype^.deftype=procvardef) then
  108. begin
  109. p1:=gencallnode(nil,nil);
  110. p1^.right:=p;
  111. p1^.resulttype:=pprocvardef(p^.resulttype)^.retdef;
  112. firstpass(p1);
  113. p:=p1;
  114. end;
  115. end;
  116. end;
  117. function statement_syssym(l : longint;var pd : pdef) : ptree;
  118. var
  119. p1,p2,paras : ptree;
  120. prev_in_args : boolean;
  121. Store_valid : boolean;
  122. begin
  123. prev_in_args:=in_args;
  124. Store_valid:=Must_be_valid;
  125. case l of
  126. in_ord_x :
  127. begin
  128. consume(_LKLAMMER);
  129. in_args:=true;
  130. Must_be_valid:=true;
  131. p1:=comp_expr(true);
  132. consume(_RKLAMMER);
  133. do_firstpass(p1);
  134. p1:=geninlinenode(in_ord_x,false,p1);
  135. do_firstpass(p1);
  136. statement_syssym := p1;
  137. pd:=p1^.resulttype;
  138. end;
  139. in_break :
  140. begin
  141. statement_syssym:=genzeronode(breakn);
  142. pd:=voiddef;
  143. end;
  144. in_continue :
  145. begin
  146. statement_syssym:=genzeronode(continuen);
  147. pd:=voiddef;
  148. end;
  149. in_typeof_x :
  150. begin
  151. consume(_LKLAMMER);
  152. in_args:=true;
  153. {allow_type:=true;}
  154. p1:=comp_expr(true);
  155. {allow_type:=false;}
  156. consume(_RKLAMMER);
  157. pd:=voidpointerdef;
  158. if p1^.treetype=typen then
  159. begin
  160. if (p1^.typenodetype=nil) then
  161. begin
  162. Message(type_e_mismatch);
  163. statement_syssym:=genzeronode(errorn);
  164. end
  165. else
  166. if p1^.typenodetype^.deftype=objectdef then
  167. begin
  168. { we can use resulttype in pass_2 (PM) }
  169. p1^.resulttype:=p1^.typenodetype;
  170. statement_syssym:=geninlinenode(in_typeof_x,false,p1);
  171. end
  172. else
  173. begin
  174. Message(type_e_mismatch);
  175. disposetree(p1);
  176. statement_syssym:=genzeronode(errorn);
  177. end;
  178. end
  179. else { not a type node }
  180. begin
  181. Must_be_valid:=false;
  182. do_firstpass(p1);
  183. if (p1^.resulttype=nil) then
  184. begin
  185. Message(type_e_mismatch);
  186. disposetree(p1);
  187. statement_syssym:=genzeronode(errorn)
  188. end
  189. else
  190. if p1^.resulttype^.deftype=objectdef then
  191. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  192. else
  193. begin
  194. Message(type_e_mismatch);
  195. statement_syssym:=genzeronode(errorn);
  196. disposetree(p1);
  197. end;
  198. end;
  199. end;
  200. in_sizeof_x :
  201. begin
  202. consume(_LKLAMMER);
  203. in_args:=true;
  204. {allow_type:=true;}
  205. p1:=comp_expr(true);
  206. {allow_type:=false; }
  207. consume(_RKLAMMER);
  208. pd:=s32bitdef;
  209. if p1^.treetype=typen then
  210. begin
  211. statement_syssym:=genordinalconstnode(p1^.typenodetype^.size,pd);
  212. { p1 not needed !}
  213. disposetree(p1);
  214. end
  215. else
  216. begin
  217. Must_be_valid:=false;
  218. do_firstpass(p1);
  219. if ((p1^.resulttype^.deftype=objectdef) and
  220. (oo_has_constructor in pobjectdef(p1^.resulttype)^.objectoptions)) or
  221. is_open_array(p1^.resulttype) or
  222. is_open_string(p1^.resulttype) then
  223. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  224. else
  225. begin
  226. statement_syssym:=genordinalconstnode(p1^.resulttype^.size,pd);
  227. { p1 not needed !}
  228. disposetree(p1);
  229. end;
  230. end;
  231. end;
  232. in_assigned_x :
  233. begin
  234. consume(_LKLAMMER);
  235. in_args:=true;
  236. p1:=comp_expr(true);
  237. Must_be_valid:=true;
  238. do_firstpass(p1);
  239. case p1^.resulttype^.deftype of
  240. pointerdef,
  241. procvardef,
  242. classrefdef : ;
  243. objectdef : if not(pobjectdef(p1^.resulttype)^.is_class) then
  244. Message(parser_e_illegal_parameter_list);
  245. else
  246. Message(parser_e_illegal_parameter_list);
  247. end;
  248. p2:=gencallparanode(p1,nil);
  249. p2:=geninlinenode(in_assigned_x,false,p2);
  250. consume(_RKLAMMER);
  251. pd:=booldef;
  252. statement_syssym:=p2;
  253. end;
  254. in_ofs_x :
  255. begin
  256. consume(_LKLAMMER);
  257. in_args:=true;
  258. p1:=comp_expr(true);
  259. p1:=gensinglenode(addrn,p1);
  260. Must_be_valid:=false;
  261. do_firstpass(p1);
  262. { Ofs() returns a longint, not a pointer }
  263. p1^.resulttype:=u32bitdef;
  264. pd:=p1^.resulttype;
  265. consume(_RKLAMMER);
  266. statement_syssym:=p1;
  267. end;
  268. in_addr_x :
  269. begin
  270. consume(_LKLAMMER);
  271. in_args:=true;
  272. p1:=comp_expr(true);
  273. p1:=gensinglenode(addrn,p1);
  274. Must_be_valid:=false;
  275. do_firstpass(p1);
  276. pd:=p1^.resulttype;
  277. consume(_RKLAMMER);
  278. statement_syssym:=p1;
  279. end;
  280. in_seg_x :
  281. begin
  282. consume(_LKLAMMER);
  283. in_args:=true;
  284. p1:=comp_expr(true);
  285. do_firstpass(p1);
  286. if p1^.location.loc<>LOC_REFERENCE then
  287. Message(cg_e_illegal_expression);
  288. p1:=genordinalconstnode(0,s32bitdef);
  289. Must_be_valid:=false;
  290. pd:=s32bitdef;
  291. consume(_RKLAMMER);
  292. statement_syssym:=p1;
  293. end;
  294. in_high_x,
  295. in_low_x :
  296. begin
  297. consume(_LKLAMMER);
  298. in_args:=true;
  299. {allow_type:=true;}
  300. p1:=comp_expr(true);
  301. {allow_type:=false;}
  302. do_firstpass(p1);
  303. if p1^.treetype=typen then
  304. p1^.resulttype:=p1^.typenodetype;
  305. Must_be_valid:=false;
  306. p2:=geninlinenode(l,false,p1);
  307. consume(_RKLAMMER);
  308. pd:=s32bitdef;
  309. statement_syssym:=p2;
  310. end;
  311. in_succ_x,
  312. in_pred_x :
  313. begin
  314. consume(_LKLAMMER);
  315. in_args:=true;
  316. p1:=comp_expr(true);
  317. do_firstpass(p1);
  318. Must_be_valid:=false;
  319. p2:=geninlinenode(l,false,p1);
  320. consume(_RKLAMMER);
  321. pd:=p1^.resulttype;
  322. statement_syssym:=p2;
  323. end;
  324. in_inc_x,
  325. in_dec_x :
  326. begin
  327. consume(_LKLAMMER);
  328. in_args:=true;
  329. p1:=comp_expr(true);
  330. Must_be_valid:=false;
  331. if token=_COMMA then
  332. begin
  333. consume(_COMMA);
  334. p2:=gencallparanode(comp_expr(true),nil);
  335. end
  336. else
  337. p2:=nil;
  338. p2:=gencallparanode(p1,p2);
  339. statement_syssym:=geninlinenode(l,false,p2);
  340. consume(_RKLAMMER);
  341. pd:=voiddef;
  342. end;
  343. in_concat_x :
  344. begin
  345. consume(_LKLAMMER);
  346. in_args:=true;
  347. p2:=nil;
  348. while true do
  349. begin
  350. p1:=comp_expr(true);
  351. Must_be_valid:=true;
  352. do_firstpass(p1);
  353. if not((p1^.resulttype^.deftype=stringdef) or
  354. ((p1^.resulttype^.deftype=orddef) and
  355. (porddef(p1^.resulttype)^.typ=uchar))) then
  356. Message(parser_e_illegal_parameter_list);
  357. if p2<>nil then
  358. p2:=gennode(addn,p2,p1)
  359. else
  360. p2:=p1;
  361. if token=_COMMA then
  362. consume(_COMMA)
  363. else
  364. break;
  365. end;
  366. consume(_RKLAMMER);
  367. pd:=cshortstringdef;
  368. statement_syssym:=p2;
  369. end;
  370. in_read_x,
  371. in_readln_x :
  372. begin
  373. if token=_LKLAMMER then
  374. begin
  375. consume(_LKLAMMER);
  376. in_args:=true;
  377. Must_be_valid:=false;
  378. paras:=parse_paras(false,false);
  379. consume(_RKLAMMER);
  380. end
  381. else
  382. paras:=nil;
  383. pd:=voiddef;
  384. p1:=geninlinenode(l,false,paras);
  385. do_firstpass(p1);
  386. statement_syssym := p1;
  387. end;
  388. in_write_x,
  389. in_writeln_x :
  390. begin
  391. if token=_LKLAMMER then
  392. begin
  393. consume(_LKLAMMER);
  394. in_args:=true;
  395. Must_be_valid:=true;
  396. paras:=parse_paras(true,false);
  397. consume(_RKLAMMER);
  398. end
  399. else
  400. paras:=nil;
  401. pd:=voiddef;
  402. p1 := geninlinenode(l,false,paras);
  403. do_firstpass(p1);
  404. statement_syssym := p1;
  405. end;
  406. in_str_x_string :
  407. begin
  408. consume(_LKLAMMER);
  409. in_args:=true;
  410. paras:=parse_paras(true,false);
  411. consume(_RKLAMMER);
  412. p1 := geninlinenode(l,false,paras);
  413. do_firstpass(p1);
  414. statement_syssym := p1;
  415. pd:=voiddef;
  416. end;
  417. in_val_x:
  418. Begin
  419. consume(_LKLAMMER);
  420. in_args := true;
  421. p1:= gencallparanode(comp_expr(true), nil);
  422. Must_be_valid := False;
  423. consume(_COMMA);
  424. p2 := gencallparanode(comp_expr(true),p1);
  425. if (token = _COMMA) then
  426. Begin
  427. consume(_COMMA);
  428. p2 := gencallparanode(comp_expr(true),p2)
  429. End;
  430. consume(_RKLAMMER);
  431. p2 := geninlinenode(l,false,p2);
  432. do_firstpass(p2);
  433. statement_syssym := p2;
  434. pd := voiddef;
  435. End;
  436. in_include_x_y,
  437. in_exclude_x_y :
  438. begin
  439. consume(_LKLAMMER);
  440. in_args:=true;
  441. p1:=comp_expr(true);
  442. Must_be_valid:=false;
  443. consume(_COMMA);
  444. p2:=comp_expr(true);
  445. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  446. consume(_RKLAMMER);
  447. pd:=voiddef;
  448. end;
  449. in_assert_x_y :
  450. begin
  451. consume(_LKLAMMER);
  452. in_args:=true;
  453. p1:=comp_expr(true);
  454. if token=_COMMA then
  455. begin
  456. consume(_COMMA);
  457. p2:=comp_expr(true);
  458. end
  459. else
  460. begin
  461. { then insert an empty string }
  462. p2:=genstringconstnode('');
  463. end;
  464. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  465. consume(_RKLAMMER);
  466. pd:=voiddef;
  467. end;
  468. else
  469. internalerror(15);
  470. end;
  471. in_args:=prev_in_args;
  472. Must_be_valid:=Store_valid;
  473. end;
  474. { reads the parameter for a subroutine call }
  475. procedure do_proc_call(getaddr : boolean;var again : boolean;var p1:Ptree;var pd:Pdef);
  476. var
  477. prev_in_args : boolean;
  478. prevafterassn : boolean;
  479. begin
  480. prev_in_args:=in_args;
  481. prevafterassn:=afterassignment;
  482. afterassignment:=false;
  483. { want we only determine the address of }
  484. { a subroutine ? }
  485. if not(getaddr) then
  486. begin
  487. if token=_LKLAMMER then
  488. begin
  489. consume(_LKLAMMER);
  490. in_args:=true;
  491. p1^.left:=parse_paras(false,false);
  492. consume(_RKLAMMER);
  493. end
  494. else p1^.left:=nil;
  495. { do firstpass because we need the }
  496. { result type }
  497. do_firstpass(p1);
  498. end
  499. else
  500. begin
  501. { address operator @: }
  502. p1^.left:=nil;
  503. { forget pd }
  504. pd:=nil;
  505. if (p1^.symtableproc^.symtabletype=withsymtable) and
  506. (p1^.symtableproc^.defowner^.deftype=objectdef) then
  507. begin
  508. p1^.methodpointer:=getcopy(pwithsymtable(p1^.symtableproc)^.withrefnode);
  509. end
  510. else if not(assigned(p1^.methodpointer)) then
  511. begin
  512. { we must provide a method pointer, if it isn't given, }
  513. { it is self }
  514. p1^.methodpointer:=genselfnode(procinfo._class);
  515. p1^.methodpointer^.resulttype:=procinfo._class;
  516. end;
  517. { no postfix operators }
  518. again:=false;
  519. end;
  520. pd:=p1^.resulttype;
  521. in_args:=prev_in_args;
  522. afterassignment:=prevafterassn;
  523. end;
  524. procedure handle_procvar(procvar : pprocvardef;var t : ptree);
  525. var
  526. hp : ptree;
  527. begin
  528. hp:=nil;
  529. if (proc_to_procvar_equal(pprocsym(t^.symtableentry)^.definition,procvar)) then
  530. begin
  531. if (po_methodpointer in procvar^.procoptions) then
  532. hp:=genloadmethodcallnode(pprocsym(t^.symtableprocentry),t^.symtable,getcopy(t^.methodpointer))
  533. else
  534. hp:=genloadcallnode(pprocsym(t^.symtableprocentry),t^.symtable);
  535. end;
  536. if assigned(hp) then
  537. begin
  538. disposetree(t);
  539. t:=hp;
  540. end;
  541. end;
  542. { the following procedure handles the access to a property symbol }
  543. procedure handle_propertysym(sym : psym;st : psymtable;var p1 : ptree;
  544. var pd : pdef);
  545. var
  546. paras : ptree;
  547. p2 : ptree;
  548. begin
  549. paras:=nil;
  550. { property parameters? }
  551. if token=_LECKKLAMMER then
  552. begin
  553. consume(_LECKKLAMMER);
  554. paras:=parse_paras(false,true);
  555. consume(_RECKKLAMMER);
  556. end;
  557. { indexed property }
  558. if (ppo_indexed in ppropertysym(sym)^.propoptions) then
  559. begin
  560. p2:=genordinalconstnode(ppropertysym(sym)^.index,s32bitdef);
  561. paras:=gencallparanode(p2,paras);
  562. end;
  563. { we need only a write property if a := follows }
  564. { if not(afterassignment) and not(in_args) then }
  565. if token=_ASSIGNMENT then
  566. begin
  567. { write property: }
  568. { no result }
  569. pd:=voiddef;
  570. if assigned(ppropertysym(sym)^.writeaccesssym) then
  571. begin
  572. if ppropertysym(sym)^.writeaccesssym^.typ=procsym then
  573. begin
  574. { generate the method call }
  575. p1:=genmethodcallnode(pprocsym(
  576. ppropertysym(sym)^.writeaccesssym),st,p1);
  577. { we know the procedure to call, so
  578. force the usage of that procedure }
  579. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.writeaccessdef);
  580. p1^.left:=paras;
  581. consume(_ASSIGNMENT);
  582. { read the expression }
  583. getprocvar:=ppropertysym(sym)^.proptype^.deftype=procvardef;
  584. p2:=comp_expr(true);
  585. if getprocvar then
  586. begin
  587. if (p2^.treetype=calln) then
  588. handle_procvar(pprocvardef(ppropertysym(sym)^.proptype),p2)
  589. else
  590. if (p2^.treetype=typeconvn) and
  591. (p2^.left^.treetype=calln) then
  592. handle_procvar(pprocvardef(ppropertysym(sym)^.proptype),p2^.left);
  593. end;
  594. p1^.left:=gencallparanode(p2,p1^.left);
  595. getprocvar:=false;
  596. end
  597. else if ppropertysym(sym)^.writeaccesssym^.typ=varsym then
  598. begin
  599. if assigned(paras) then
  600. message(parser_e_no_paras_allowed);
  601. { subscribed access? }
  602. if p1=nil then
  603. p1:=genloadnode(pvarsym(ppropertysym(sym)^.writeaccesssym),st)
  604. else
  605. p1:=gensubscriptnode(pvarsym(ppropertysym(sym)^.writeaccesssym),p1);
  606. consume(_ASSIGNMENT);
  607. { read the expression }
  608. p2:=comp_expr(true);
  609. p1:=gennode(assignn,p1,p2);
  610. end
  611. else
  612. begin
  613. p1:=genzeronode(errorn);
  614. Message(parser_e_no_procedure_to_access_property);
  615. end;
  616. end
  617. else
  618. begin
  619. p1:=genzeronode(errorn);
  620. Message(parser_e_no_procedure_to_access_property);
  621. end;
  622. end
  623. else
  624. begin
  625. { read property: }
  626. pd:=ppropertysym(sym)^.proptype;
  627. if assigned(ppropertysym(sym)^.readaccesssym) then
  628. begin
  629. if ppropertysym(sym)^.readaccesssym^.typ=varsym then
  630. begin
  631. if assigned(paras) then
  632. message(parser_e_no_paras_allowed);
  633. { subscribed access? }
  634. if p1=nil then
  635. p1:=genloadnode(pvarsym(ppropertysym(sym)^.readaccesssym),st)
  636. else
  637. p1:=gensubscriptnode(pvarsym(ppropertysym(sym)^.readaccesssym),p1);
  638. end
  639. else if ppropertysym(sym)^.readaccesssym^.typ=procsym then
  640. begin
  641. { generate the method call }
  642. p1:=genmethodcallnode(pprocsym(ppropertysym(sym)^.readaccesssym),st,p1);
  643. { we know the procedure to call, so
  644. force the usage of that procedure }
  645. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.readaccessdef);
  646. { insert paras }
  647. p1^.left:=paras;
  648. end
  649. else
  650. begin
  651. p1:=genzeronode(errorn);
  652. Message(type_e_mismatch);
  653. end;
  654. end
  655. else
  656. begin
  657. { error, no function to read property }
  658. p1:=genzeronode(errorn);
  659. Message(parser_e_no_procedure_to_access_property);
  660. end;
  661. end;
  662. end;
  663. { the ID token has to be consumed before calling this function }
  664. procedure do_member_read(getaddr : boolean;const sym : psym;var p1 : ptree;
  665. var pd : pdef;var again : boolean);
  666. var
  667. static_name : string;
  668. isclassref : boolean;
  669. begin
  670. if sym=nil then
  671. begin
  672. { pattern is still valid unless
  673. there is another ID just after the ID of sym }
  674. Message1(sym_e_id_no_member,pattern);
  675. disposetree(p1);
  676. p1:=genzeronode(errorn);
  677. { try to clean up }
  678. pd:=generrordef;
  679. again:=false;
  680. end
  681. else
  682. begin
  683. isclassref:=pd^.deftype=classrefdef;
  684. { check protected and private members }
  685. { please leave this code as it is, }
  686. { it has now the same behaviaor as TP/Delphi }
  687. if (sp_private in sym^.symoptions) and
  688. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  689. Message(parser_e_cant_access_private_member);
  690. if (sp_protected in sym^.symoptions) and
  691. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  692. begin
  693. if assigned(aktprocsym^.definition^._class) then
  694. begin
  695. if not aktprocsym^.definition^._class^.is_related(pobjectdef(sym^.owner^.defowner)) then
  696. Message(parser_e_cant_access_protected_member);
  697. end
  698. else
  699. Message(parser_e_cant_access_protected_member);
  700. end;
  701. { we assume, that only procsyms and varsyms are in an object }
  702. { symbol table, for classes, properties are allowed }
  703. case sym^.typ of
  704. procsym:
  705. begin
  706. p1:=genmethodcallnode(pprocsym(sym),srsymtable,p1);
  707. do_proc_call(getaddr or
  708. (getprocvar and
  709. (m_tp_procvar in aktmodeswitches) and
  710. proc_to_procvar_equal(pprocsym(sym)^.definition,getprocvardef))
  711. ,again,p1,pd);
  712. { now we know the real method e.g. we can check for }
  713. { a class method }
  714. if isclassref and
  715. not(po_classmethod in p1^.procdefinition^.procoptions) and
  716. not(p1^.procdefinition^.proctypeoption=potype_constructor) then
  717. Message(parser_e_only_class_methods_via_class_ref);
  718. end;
  719. varsym:
  720. begin
  721. if isclassref then
  722. Message(parser_e_only_class_methods_via_class_ref);
  723. if (sp_static in sym^.symoptions) then
  724. begin
  725. { static_name:=lower(srsymtable^.name^)+'_'+sym^.name;
  726. this is wrong for static field in with symtable (PM) }
  727. static_name:=lower(srsym^.owner^.name^)+'_'+sym^.name;
  728. getsym(static_name,true);
  729. disposetree(p1);
  730. p1:=genloadnode(pvarsym(srsym),srsymtable);
  731. end
  732. else
  733. p1:=gensubscriptnode(pvarsym(sym),p1);
  734. pd:=pvarsym(sym)^.definition;
  735. end;
  736. propertysym:
  737. begin
  738. if isclassref then
  739. Message(parser_e_only_class_methods_via_class_ref);
  740. handle_propertysym(sym,srsymtable,p1,pd);
  741. end;
  742. else internalerror(16);
  743. end;
  744. end;
  745. end;
  746. {****************************************************************************
  747. Factor
  748. ****************************************************************************}
  749. function factor(getaddr : boolean) : ptree;
  750. var
  751. l : longint;
  752. oldp1,
  753. p1,p2,p3 : ptree;
  754. code : integer;
  755. pd,pd2 : pdef;
  756. possible_error,
  757. unit_specific,
  758. again : boolean;
  759. sym : pvarsym;
  760. classh : pobjectdef;
  761. d : bestreal;
  762. static_name : string;
  763. propsym : ppropertysym;
  764. filepos : tfileposinfo;
  765. {---------------------------------------------
  766. Is_func_ret
  767. ---------------------------------------------}
  768. function is_func_ret(sym : psym) : boolean;
  769. var
  770. p : pprocinfo;
  771. storesymtablestack : psymtable;
  772. begin
  773. is_func_ret:=false;
  774. if (sym^.typ<>funcretsym) and ((procinfo.flags and pi_operator)=0) then
  775. exit;
  776. p:=@procinfo;
  777. while system.assigned(p) do
  778. begin
  779. { is this an access to a function result ? }
  780. if assigned(p^.funcretsym) and
  781. ((pfuncretsym(sym)=p^.funcretsym) or
  782. ((pvarsym(sym)=opsym) and
  783. ((p^.flags and pi_operator)<>0))) and
  784. (p^.retdef<>pdef(voiddef)) and
  785. (token<>_LKLAMMER) and
  786. (not ((m_tp in aktmodeswitches) and
  787. (afterassignment or in_args))) then
  788. begin
  789. if ((pvarsym(sym)=opsym) and
  790. ((p^.flags and pi_operator)<>0)) then
  791. inc(opsym^.refs);
  792. if ((pvarsym(sym)=opsym) and
  793. ((p^.flags and pi_operator)<>0)) then
  794. inc(opsym^.refs);
  795. p1:=genzeronode(funcretn);
  796. pd:=p^.retdef;
  797. p1^.funcretprocinfo:=p;
  798. p1^.retdef:=pd;
  799. is_func_ret:=true;
  800. exit;
  801. end;
  802. p:=p^.parent;
  803. end;
  804. { we must use the function call }
  805. if(sym^.typ=funcretsym) then
  806. begin
  807. storesymtablestack:=symtablestack;
  808. symtablestack:=srsymtable^.next;
  809. getsym(sym^.name,true);
  810. if srsym^.typ<>procsym then
  811. Message(cg_e_illegal_expression);
  812. symtablestack:=storesymtablestack;
  813. end;
  814. end;
  815. {---------------------------------------------
  816. Factor_read_id
  817. ---------------------------------------------}
  818. procedure factor_read_id;
  819. var
  820. pc : pchar;
  821. len : longint;
  822. begin
  823. { allow post fix operators }
  824. again:=true;
  825. if (m_result in aktmodeswitches) and
  826. (idtoken=_RESULT) and
  827. assigned(aktprocsym) and
  828. (procinfo.retdef<>pdef(voiddef)) then
  829. begin
  830. consume(_ID);
  831. p1:=genzeronode(funcretn);
  832. pd:=procinfo.retdef;
  833. p1^.funcretprocinfo:=pointer(@procinfo);
  834. p1^.retdef:=pd;
  835. end
  836. else
  837. begin
  838. if lastsymknown then
  839. begin
  840. srsym:=lastsrsym;
  841. srsymtable:=lastsrsymtable;
  842. lastsymknown:=false;
  843. end
  844. else
  845. getsym(pattern,true);
  846. consume(_ID);
  847. if not is_func_ret(srsym) then
  848. { else it's a normal symbol }
  849. begin
  850. { is it defined like UNIT.SYMBOL ? }
  851. if srsym^.typ=unitsym then
  852. begin
  853. consume(_POINT);
  854. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  855. unit_specific:=true;
  856. consume(_ID);
  857. end
  858. else
  859. unit_specific:=false;
  860. if not assigned(srsym) then
  861. Begin
  862. p1:=genzeronode(errorn);
  863. { try to clean up }
  864. pd:=generrordef;
  865. end
  866. else
  867. Begin
  868. { check semantics of private }
  869. if (srsym^.typ in [propertysym,procsym,varsym]) and
  870. (srsymtable^.symtabletype=objectsymtable) then
  871. begin
  872. if (sp_private in srsym^.symoptions) and
  873. (pobjectdef(srsym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  874. Message(parser_e_cant_access_private_member);
  875. end;
  876. case srsym^.typ of
  877. absolutesym : begin
  878. p1:=genloadnode(pvarsym(srsym),srsymtable);
  879. pd:=pabsolutesym(srsym)^.definition;
  880. end;
  881. varsym : begin
  882. { are we in a class method ? }
  883. if (srsymtable^.symtabletype=objectsymtable) and
  884. assigned(aktprocsym) and
  885. (po_classmethod in aktprocsym^.definition^.procoptions) then
  886. Message(parser_e_only_class_methods);
  887. if (sp_static in srsym^.symoptions) then
  888. begin
  889. static_name:=lower(srsym^.owner^.name^)+'_'+srsym^.name;
  890. getsym(static_name,true);
  891. end;
  892. p1:=genloadnode(pvarsym(srsym),srsymtable);
  893. if pvarsym(srsym)^.varstate=vs_declared then
  894. begin
  895. p1^.is_first := true;
  896. { set special between first loaded until checked in firstpass }
  897. pvarsym(srsym)^.varstate:=vs_declared2;
  898. end;
  899. pd:=pvarsym(srsym)^.definition;
  900. end;
  901. typedconstsym : begin
  902. p1:=gentypedconstloadnode(ptypedconstsym(srsym),srsymtable);
  903. pd:=ptypedconstsym(srsym)^.definition;
  904. end;
  905. syssym : p1:=statement_syssym(psyssym(srsym)^.number,pd);
  906. typesym : begin
  907. pd:=ptypesym(srsym)^.definition;
  908. if not assigned(pd) then
  909. begin
  910. pd:=generrordef;
  911. again:=false;
  912. end
  913. else
  914. begin
  915. { if we read a type declaration }
  916. { we have to return the type and }
  917. { nothing else }
  918. if block_type=bt_type then
  919. begin
  920. { we don't need sym reference when it's in the
  921. current unit or system unit, because those
  922. units are always loaded (PFV) }
  923. if (pd^.owner^.unitid=0) or
  924. (pd^.owner^.unitid=1) then
  925. p1:=gentypenode(pd,nil)
  926. else
  927. p1:=gentypenode(pd,ptypesym(srsym));
  928. { here we can also set resulttype !! }
  929. p1^.resulttype:=pd;
  930. pd:=voiddef;
  931. end
  932. else { not type block }
  933. begin
  934. if token=_LKLAMMER then
  935. begin
  936. consume(_LKLAMMER);
  937. p1:=comp_expr(true);
  938. consume(_RKLAMMER);
  939. p1:=gentypeconvnode(p1,pd);
  940. p1^.explizit:=true;
  941. end
  942. else { not LKLAMMER}
  943. if (token=_POINT) and
  944. (pd^.deftype=objectdef) and
  945. not(pobjectdef(pd)^.is_class) then
  946. begin
  947. consume(_POINT);
  948. if assigned(procinfo._class) then
  949. begin
  950. if procinfo._class^.is_related(pobjectdef(pd)) then
  951. begin
  952. p1:=gentypenode(pd,ptypesym(srsym));
  953. p1^.resulttype:=pd;
  954. srsymtable:=pobjectdef(pd)^.symtable;
  955. sym:=pvarsym(srsymtable^.search(pattern));
  956. { search also in inherited methods }
  957. while sym=nil do
  958. begin
  959. pd:=pobjectdef(pd)^.childof;
  960. srsymtable:=pobjectdef(pd)^.symtable;
  961. sym:=pvarsym(srsymtable^.search(pattern));
  962. end;
  963. consume(_ID);
  964. do_member_read(false,sym,p1,pd,again);
  965. end
  966. else
  967. begin
  968. Message(parser_e_no_super_class);
  969. pd:=generrordef;
  970. again:=false;
  971. end;
  972. end
  973. else
  974. begin
  975. { allows @TObject.Load }
  976. { also allows static methods and variables }
  977. p1:=genzeronode(typen);
  978. p1^.resulttype:=pd;
  979. { srsymtable:=pobjectdef(pd)^.symtable;
  980. sym:=pvarsym(srsymtable^.search(pattern)); }
  981. { TP allows also @TMenu.Load if Load is only }
  982. { defined in an anchestor class }
  983. sym:=pvarsym(search_class_member(pobjectdef(pd),pattern));
  984. if not assigned(sym) then
  985. Message1(sym_e_id_no_member,pattern)
  986. else if not(getaddr) and not(sp_static in sym^.symoptions) then
  987. Message(sym_e_only_static_in_static)
  988. else
  989. begin
  990. consume(_ID);
  991. do_member_read(getaddr,sym,p1,pd,again);
  992. end;
  993. end;
  994. end
  995. else
  996. begin
  997. { class reference ? }
  998. if (pd^.deftype=objectdef)
  999. and pobjectdef(pd)^.is_class then
  1000. begin
  1001. p1:=gentypenode(pd,nil);
  1002. p1^.resulttype:=pd;
  1003. pd:=new(pclassrefdef,init(pd));
  1004. p1:=gensinglenode(loadvmtn,p1);
  1005. p1^.resulttype:=pd;
  1006. end
  1007. else
  1008. begin
  1009. { generate a type node }
  1010. { (for typeof etc) }
  1011. if allow_type then
  1012. begin
  1013. p1:=gentypenode(pd,nil);
  1014. { here we must use typenodetype explicitly !! PM
  1015. p1^.resulttype:=pd; }
  1016. pd:=voiddef;
  1017. end
  1018. else
  1019. Message(parser_e_no_type_not_allowed_here);
  1020. end;
  1021. end;
  1022. end;
  1023. end;
  1024. end;
  1025. enumsym : begin
  1026. p1:=genenumnode(penumsym(srsym));
  1027. pd:=p1^.resulttype;
  1028. end;
  1029. constsym : begin
  1030. case pconstsym(srsym)^.consttype of
  1031. constint :
  1032. p1:=genordinalconstnode(pconstsym(srsym)^.value,s32bitdef);
  1033. conststring :
  1034. begin
  1035. len:=pconstsym(srsym)^.len;
  1036. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1037. len:=255;
  1038. getmem(pc,len+1);
  1039. move(pchar(pconstsym(srsym)^.value)^,pc^,len);
  1040. pc[len]:=#0;
  1041. p1:=genpcharconstnode(pc,len);
  1042. end;
  1043. constchar :
  1044. p1:=genordinalconstnode(pconstsym(srsym)^.value,cchardef);
  1045. constreal :
  1046. p1:=genrealconstnode(pbestreal(pconstsym(srsym)^.value)^,bestrealdef^);
  1047. constbool :
  1048. p1:=genordinalconstnode(pconstsym(srsym)^.value,booldef);
  1049. constset :
  1050. p1:=gensetconstnode(pconstset(pconstsym(srsym)^.value),
  1051. psetdef(pconstsym(srsym)^.definition));
  1052. constord :
  1053. p1:=genordinalconstnode(pconstsym(srsym)^.value,
  1054. pconstsym(srsym)^.definition);
  1055. constnil :
  1056. p1:=genzeronode(niln);
  1057. constresourcestring:
  1058. begin
  1059. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1060. p1^.resulttype:=cansistringdef;
  1061. end;
  1062. end;
  1063. pd:=p1^.resulttype;
  1064. end;
  1065. procsym : begin
  1066. { are we in a class method ? }
  1067. possible_error:=(srsymtable^.symtabletype=objectsymtable) and
  1068. assigned(aktprocsym) and
  1069. (po_classmethod in aktprocsym^.definition^.procoptions);
  1070. p1:=gencallnode(pprocsym(srsym),srsymtable);
  1071. p1^.unit_specific:=unit_specific;
  1072. do_proc_call(getaddr or
  1073. (getprocvar and
  1074. (m_tp_procvar in aktmodeswitches) and
  1075. proc_to_procvar_equal(pprocsym(srsym)^.definition,getprocvardef)),
  1076. again,p1,pd);
  1077. if possible_error and
  1078. not(po_classmethod in p1^.procdefinition^.procoptions) then
  1079. Message(parser_e_only_class_methods);
  1080. end;
  1081. propertysym : begin
  1082. { access to property in a method }
  1083. { are we in a class method ? }
  1084. if (srsymtable^.symtabletype=objectsymtable) and
  1085. assigned(aktprocsym) and
  1086. (po_classmethod in aktprocsym^.definition^.procoptions) then
  1087. Message(parser_e_only_class_methods);
  1088. { no method pointer }
  1089. p1:=nil;
  1090. handle_propertysym(srsym,srsymtable,p1,pd);
  1091. end;
  1092. errorsym : begin
  1093. p1:=genzeronode(errorn);
  1094. p1^.resulttype:=generrordef;
  1095. pd:=generrordef;
  1096. if token=_LKLAMMER then
  1097. begin
  1098. consume(_LKLAMMER);
  1099. parse_paras(false,false);
  1100. consume(_RKLAMMER);
  1101. end;
  1102. end;
  1103. else
  1104. begin
  1105. p1:=genzeronode(errorn);
  1106. pd:=generrordef;
  1107. Message(cg_e_illegal_expression);
  1108. end;
  1109. end; { end case }
  1110. end;
  1111. end;
  1112. end;
  1113. end;
  1114. {---------------------------------------------
  1115. Factor_Read_Set
  1116. ---------------------------------------------}
  1117. { Read a set between [] }
  1118. function factor_read_set:ptree;
  1119. var
  1120. p1,
  1121. lastp,
  1122. buildp : ptree;
  1123. begin
  1124. buildp:=nil;
  1125. { be sure that a least one arrayconstructn is used, also for an
  1126. empty [] }
  1127. if token=_RECKKLAMMER then
  1128. buildp:=gennode(arrayconstructn,nil,buildp)
  1129. else
  1130. begin
  1131. while true do
  1132. begin
  1133. p1:=comp_expr(true);
  1134. if token=_POINTPOINT then
  1135. begin
  1136. consume(_POINTPOINT);
  1137. p2:=comp_expr(true);
  1138. p1:=gennode(arrayconstructrangen,p1,p2);
  1139. end;
  1140. { insert at the end of the tree, to get the correct order }
  1141. if not assigned(buildp) then
  1142. begin
  1143. buildp:=gennode(arrayconstructn,p1,nil);
  1144. lastp:=buildp;
  1145. end
  1146. else
  1147. begin
  1148. lastp^.right:=gennode(arrayconstructn,p1,nil);
  1149. lastp:=lastp^.right;
  1150. end;
  1151. { there could be more elements }
  1152. if token=_COMMA then
  1153. consume(_COMMA)
  1154. else
  1155. break;
  1156. end;
  1157. end;
  1158. factor_read_set:=buildp;
  1159. end;
  1160. {---------------------------------------------
  1161. Helpers
  1162. ---------------------------------------------}
  1163. procedure check_tokenpos;
  1164. begin
  1165. if (p1<>oldp1) then
  1166. begin
  1167. if assigned(p1) then
  1168. set_tree_filepos(p1,filepos);
  1169. oldp1:=p1;
  1170. filepos:=tokenpos;
  1171. end;
  1172. end;
  1173. {---------------------------------------------
  1174. PostFixOperators
  1175. ---------------------------------------------}
  1176. procedure postfixoperators;
  1177. var
  1178. store_static : boolean;
  1179. { p1 and p2 must contain valid value_str }
  1180. begin
  1181. check_tokenpos;
  1182. while again do
  1183. begin
  1184. { prevent crashes with unknown types }
  1185. if not assigned(pd) then
  1186. begin
  1187. { try to recover }
  1188. repeat
  1189. case token of
  1190. _CARET:
  1191. consume(_CARET);
  1192. _POINT:
  1193. begin
  1194. consume(_POINT);
  1195. consume(_ID);
  1196. end;
  1197. _LECKKLAMMER:
  1198. begin
  1199. repeat
  1200. consume(token);
  1201. until token in [_RECKKLAMMER,_SEMICOLON];
  1202. end;
  1203. else
  1204. break;
  1205. end;
  1206. until false;
  1207. exit;
  1208. end;
  1209. { handle token }
  1210. case token of
  1211. _CARET:
  1212. begin
  1213. consume(_CARET);
  1214. if (pd^.deftype<>pointerdef) then
  1215. begin
  1216. { ^ as binary operator is a problem!!!! (FK) }
  1217. again:=false;
  1218. Message(cg_e_invalid_qualifier);
  1219. disposetree(p1);
  1220. p1:=genzeronode(errorn);
  1221. end
  1222. else
  1223. begin
  1224. p1:=gensinglenode(derefn,p1);
  1225. pd:=ppointerdef(pd)^.definition;
  1226. end;
  1227. end;
  1228. _LECKKLAMMER:
  1229. begin
  1230. if (pd^.deftype=objectdef) and pobjectdef(pd)^.is_class then
  1231. begin
  1232. { default property }
  1233. propsym:=search_default_property(pobjectdef(pd));
  1234. if not(assigned(propsym)) then
  1235. begin
  1236. disposetree(p1);
  1237. p1:=genzeronode(errorn);
  1238. again:=false;
  1239. message(parser_e_no_default_property_available);
  1240. end
  1241. else
  1242. handle_propertysym(propsym,propsym^.owner,p1,pd);
  1243. end
  1244. else
  1245. begin
  1246. consume(_LECKKLAMMER);
  1247. repeat
  1248. case pd^.deftype of
  1249. pointerdef:
  1250. begin
  1251. p2:=comp_expr(true);
  1252. p1:=gennode(vecn,p1,p2);
  1253. pd:=ppointerdef(pd)^.definition;
  1254. end;
  1255. stringdef : begin
  1256. p2:=comp_expr(true);
  1257. p1:=gennode(vecn,p1,p2);
  1258. pd:=cchardef
  1259. end;
  1260. arraydef : begin
  1261. p2:=comp_expr(true);
  1262. { support SEG:OFS for go32v2 Mem[] }
  1263. if (target_info.target=target_i386_go32v2) and
  1264. (p1^.treetype=loadn) and
  1265. assigned(p1^.symtableentry) and
  1266. assigned(p1^.symtableentry^.owner^.name) and
  1267. (p1^.symtableentry^.owner^.name^='SYSTEM') and
  1268. ((p1^.symtableentry^.name='MEM') or
  1269. (p1^.symtableentry^.name='MEMW') or
  1270. (p1^.symtableentry^.name='MEML')) then
  1271. begin
  1272. if (token=_COLON) then
  1273. begin
  1274. consume(_COLON);
  1275. p3:=gennode(muln,genordinalconstnode($10,s32bitdef),p2);
  1276. p2:=comp_expr(true);
  1277. p2:=gennode(addn,p2,p3);
  1278. p1:=gennode(vecn,p1,p2);
  1279. p1^.memseg:=true;
  1280. p1^.memindex:=true;
  1281. end
  1282. else
  1283. begin
  1284. p1:=gennode(vecn,p1,p2);
  1285. p1^.memindex:=true;
  1286. end;
  1287. end
  1288. else
  1289. p1:=gennode(vecn,p1,p2);
  1290. pd:=parraydef(pd)^.definition;
  1291. end;
  1292. else
  1293. begin
  1294. Message(cg_e_invalid_qualifier);
  1295. disposetree(p1);
  1296. p1:=genzeronode(errorn);
  1297. again:=false;
  1298. end;
  1299. end;
  1300. if token=_COMMA then
  1301. consume(_COMMA)
  1302. else
  1303. break;
  1304. until false;
  1305. consume(_RECKKLAMMER);
  1306. end;
  1307. end;
  1308. _POINT : begin
  1309. consume(_POINT);
  1310. if (pd^.deftype=pointerdef) and
  1311. (m_autoderef in aktmodeswitches) then
  1312. begin
  1313. p1:=gensinglenode(derefn,p1);
  1314. pd:=ppointerdef(pd)^.definition;
  1315. end;
  1316. case pd^.deftype of
  1317. recorddef:
  1318. begin
  1319. sym:=pvarsym(precorddef(pd)^.symtable^.search(pattern));
  1320. if sym=nil then
  1321. begin
  1322. Message1(sym_e_illegal_field,pattern);
  1323. disposetree(p1);
  1324. p1:=genzeronode(errorn);
  1325. end
  1326. else
  1327. begin
  1328. p1:=gensubscriptnode(sym,p1);
  1329. pd:=sym^.definition;
  1330. end;
  1331. consume(_ID);
  1332. end;
  1333. classrefdef:
  1334. begin
  1335. classh:=pobjectdef(pclassrefdef(pd)^.definition);
  1336. sym:=nil;
  1337. while assigned(classh) do
  1338. begin
  1339. sym:=pvarsym(classh^.symtable^.search(pattern));
  1340. srsymtable:=classh^.symtable;
  1341. if assigned(sym) then
  1342. break;
  1343. classh:=classh^.childof;
  1344. end;
  1345. consume(_ID);
  1346. do_member_read(getaddr,sym,p1,pd,again);
  1347. end;
  1348. objectdef:
  1349. begin
  1350. classh:=pobjectdef(pd);
  1351. sym:=nil;
  1352. store_static:=allow_only_static;
  1353. allow_only_static:=false;
  1354. while assigned(classh) do
  1355. begin
  1356. sym:=pvarsym(classh^.symtable^.search(pattern));
  1357. srsymtable:=classh^.symtable;
  1358. if assigned(sym) then
  1359. break;
  1360. classh:=classh^.childof;
  1361. end;
  1362. allow_only_static:=store_static;
  1363. consume(_ID);
  1364. do_member_read(getaddr,sym,p1,pd,again);
  1365. end;
  1366. pointerdef:
  1367. begin
  1368. Message(cg_e_invalid_qualifier);
  1369. if ppointerdef(pd)^.definition^.deftype in [recorddef,objectdef,classrefdef] then
  1370. Message(parser_h_maybe_deref_caret_missing);
  1371. end;
  1372. else
  1373. begin
  1374. Message(cg_e_invalid_qualifier);
  1375. disposetree(p1);
  1376. p1:=genzeronode(errorn);
  1377. end;
  1378. end;
  1379. end;
  1380. else
  1381. begin
  1382. { is this a procedure variable ? }
  1383. if assigned(pd) then
  1384. begin
  1385. if (pd^.deftype=procvardef) then
  1386. begin
  1387. if getprocvar and is_equal(pd,getprocvardef) then
  1388. again:=false
  1389. else
  1390. if (token=_LKLAMMER) or
  1391. ((pprocvardef(pd)^.para1=nil) and
  1392. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1393. (not afterassignment) and
  1394. (not in_args)) then
  1395. begin
  1396. { do this in a strange way }
  1397. { it's not a clean solution }
  1398. p2:=p1;
  1399. p1:=gencallnode(nil,nil);
  1400. p1^.right:=p2;
  1401. p1^.unit_specific:=unit_specific;
  1402. p1^.symtableprocentry:=pprocsym(sym);
  1403. if token=_LKLAMMER then
  1404. begin
  1405. consume(_LKLAMMER);
  1406. p1^.left:=parse_paras(false,false);
  1407. consume(_RKLAMMER);
  1408. end;
  1409. pd:=pprocvardef(pd)^.retdef;
  1410. { proc():= is never possible }
  1411. if token=_ASSIGNMENT then
  1412. begin
  1413. Message(cg_e_illegal_expression);
  1414. p1:=genzeronode(errorn);
  1415. again:=false;
  1416. end;
  1417. p1^.resulttype:=pd;
  1418. end
  1419. else
  1420. again:=false;
  1421. p1^.resulttype:=pd;
  1422. end
  1423. else
  1424. again:=false;
  1425. end
  1426. else
  1427. again:=false;
  1428. end;
  1429. end;
  1430. check_tokenpos;
  1431. end; { while again }
  1432. end;
  1433. {---------------------------------------------
  1434. Factor (Main)
  1435. ---------------------------------------------}
  1436. begin
  1437. oldp1:=nil;
  1438. p1:=nil;
  1439. filepos:=tokenpos;
  1440. if token=_ID then
  1441. begin
  1442. factor_read_id;
  1443. { handle post fix operators }
  1444. postfixoperators;
  1445. end
  1446. else
  1447. case token of
  1448. _NEW : begin
  1449. consume(_NEW);
  1450. consume(_LKLAMMER);
  1451. {allow_type:=true;}
  1452. p1:=factor(false);
  1453. {allow_type:=false;}
  1454. if p1^.treetype<>typen then
  1455. begin
  1456. Message(type_e_type_id_expected);
  1457. disposetree(p1);
  1458. pd:=generrordef;
  1459. end
  1460. else
  1461. pd:=p1^.typenodetype;
  1462. pd2:=pd;
  1463. if (pd^.deftype<>pointerdef) then
  1464. Message1(type_e_pointer_type_expected,pd^.typename)
  1465. else if {(ppointerdef(pd)^.definition^.deftype<>objectdef)}
  1466. token=_RKLAMMER then
  1467. begin
  1468. if (ppointerdef(pd)^.definition^.deftype=objectdef) and
  1469. (oo_has_vmt in pobjectdef(ppointerdef(pd)^.definition)^.objectoptions) then
  1470. Message(parser_w_use_extended_syntax_for_objects);
  1471. p1:=gensinglenode(newn,nil);
  1472. p1^.resulttype:=pd2;
  1473. consume(_RKLAMMER);
  1474. (*Message(parser_e_pointer_to_class_expected);
  1475. { if an error occurs, read til the end of
  1476. the new statement }
  1477. p1:=genzeronode(errorn);
  1478. l:=1;
  1479. while true do
  1480. begin
  1481. case token of
  1482. _LKLAMMER : inc(l);
  1483. _RKLAMMER : dec(l);
  1484. end;
  1485. consume(token);
  1486. if l=0 then
  1487. break;
  1488. end;*)
  1489. end
  1490. else
  1491. begin
  1492. disposetree(p1);
  1493. p1:=genzeronode(hnewn);
  1494. p1^.resulttype:=ppointerdef(pd)^.definition;
  1495. consume(_COMMA);
  1496. afterassignment:=false;
  1497. { determines the current object defintion }
  1498. classh:=pobjectdef(ppointerdef(pd)^.definition);
  1499. { check for an abstract class }
  1500. if (oo_has_abstract in classh^.objectoptions) then
  1501. Message(sym_e_no_instance_of_abstract_object);
  1502. { search the constructor also in the symbol tables of
  1503. the parents }
  1504. { no constructor found }
  1505. sym:=nil;
  1506. while assigned(classh) do
  1507. begin
  1508. sym:=pvarsym(classh^.symtable^.search(pattern));
  1509. srsymtable:=classh^.symtable;
  1510. if assigned(sym) then
  1511. break;
  1512. classh:=classh^.childof;
  1513. end;
  1514. consume(_ID);
  1515. do_member_read(false,sym,p1,pd,again);
  1516. if (p1^.treetype<>calln) or
  1517. (assigned(p1^.procdefinition) and
  1518. (p1^.procdefinition^.proctypeoption<>potype_constructor)) then
  1519. Message(parser_e_expr_have_to_be_constructor_call);
  1520. p1:=gensinglenode(newn,p1);
  1521. { set the resulttype }
  1522. p1^.resulttype:=pd2;
  1523. consume(_RKLAMMER);
  1524. end;
  1525. postfixoperators;
  1526. end;
  1527. _SELF : begin
  1528. again:=true;
  1529. consume(_SELF);
  1530. if not assigned(procinfo._class) then
  1531. begin
  1532. p1:=genzeronode(errorn);
  1533. pd:=generrordef;
  1534. again:=false;
  1535. Message(parser_e_self_not_in_method);
  1536. end
  1537. else
  1538. begin
  1539. if (po_classmethod in aktprocsym^.definition^.procoptions) then
  1540. begin
  1541. { self in class methods is a class reference type }
  1542. pd:=new(pclassrefdef,init(procinfo._class));
  1543. p1:=genselfnode(pd);
  1544. p1^.resulttype:=pd;
  1545. end
  1546. else
  1547. begin
  1548. p1:=genselfnode(procinfo._class);
  1549. p1^.resulttype:=procinfo._class;
  1550. end;
  1551. pd:=p1^.resulttype;
  1552. postfixoperators;
  1553. end;
  1554. end;
  1555. _INHERITED : begin
  1556. again:=true;
  1557. consume(_INHERITED);
  1558. if assigned(procinfo._class) then
  1559. begin
  1560. classh:=procinfo._class^.childof;
  1561. while assigned(classh) do
  1562. begin
  1563. srsymtable:=pobjectdef(classh)^.symtable;
  1564. sym:=pvarsym(srsymtable^.search(pattern));
  1565. if assigned(sym) then
  1566. begin
  1567. p1:=genzeronode(typen);
  1568. p1^.resulttype:=classh;
  1569. pd:=p1^.resulttype;
  1570. consume(_ID);
  1571. do_member_read(false,sym,p1,pd,again);
  1572. break;
  1573. end;
  1574. classh:=classh^.childof;
  1575. end;
  1576. if classh=nil then
  1577. begin
  1578. Message1(sym_e_id_no_member,pattern);
  1579. again:=false;
  1580. pd:=generrordef;
  1581. p1:=genzeronode(errorn);
  1582. end;
  1583. end
  1584. else
  1585. begin
  1586. Message(parser_e_generic_methods_only_in_methods);
  1587. again:=false;
  1588. pd:=generrordef;
  1589. p1:=genzeronode(errorn);
  1590. end;
  1591. postfixoperators;
  1592. end;
  1593. _INTCONST : begin
  1594. valint(pattern,l,code);
  1595. if code<>0 then
  1596. begin
  1597. val(pattern,d,code);
  1598. if code<>0 then
  1599. begin
  1600. Message(cg_e_invalid_integer);
  1601. consume(_INTCONST);
  1602. l:=1;
  1603. p1:=genordinalconstnode(l,s32bitdef);
  1604. end
  1605. else
  1606. begin
  1607. consume(_INTCONST);
  1608. p1:=genrealconstnode(d,bestrealdef^);
  1609. end;
  1610. end
  1611. else
  1612. begin
  1613. consume(_INTCONST);
  1614. p1:=genordinalconstnode(l,s32bitdef);
  1615. end;
  1616. end;
  1617. _REALNUMBER : begin
  1618. val(pattern,d,code);
  1619. if code<>0 then
  1620. begin
  1621. Message(parser_e_error_in_real);
  1622. d:=1.0;
  1623. end;
  1624. consume(_REALNUMBER);
  1625. p1:=genrealconstnode(d,bestrealdef^);
  1626. end;
  1627. _STRING : begin
  1628. pd:=stringtype;
  1629. { STRING can be also a type cast }
  1630. if token=_LKLAMMER then
  1631. begin
  1632. consume(_LKLAMMER);
  1633. p1:=comp_expr(true);
  1634. consume(_RKLAMMER);
  1635. p1:=gentypeconvnode(p1,pd);
  1636. p1^.explizit:=true;
  1637. { handle postfix operators here e.g. string(a)[10] }
  1638. again:=true;
  1639. postfixoperators;
  1640. end
  1641. else
  1642. p1:=gentypenode(pd,nil);
  1643. end;
  1644. _FILE : begin
  1645. pd:=cfiledef;
  1646. consume(_FILE);
  1647. { FILE can be also a type cast }
  1648. if token=_LKLAMMER then
  1649. begin
  1650. consume(_LKLAMMER);
  1651. p1:=comp_expr(true);
  1652. consume(_RKLAMMER);
  1653. p1:=gentypeconvnode(p1,pd);
  1654. p1^.explizit:=true;
  1655. { handle postfix operators here e.g. string(a)[10] }
  1656. again:=true;
  1657. postfixoperators;
  1658. end
  1659. else
  1660. p1:=gentypenode(pd,nil);
  1661. end;
  1662. _CSTRING : begin
  1663. p1:=genstringconstnode(pattern);
  1664. consume(_CSTRING);
  1665. end;
  1666. _CCHAR : begin
  1667. p1:=genordinalconstnode(ord(pattern[1]),cchardef);
  1668. consume(_CCHAR);
  1669. end;
  1670. _KLAMMERAFFE : begin
  1671. consume(_KLAMMERAFFE);
  1672. got_addrn:=true;
  1673. p1:=factor(true);
  1674. got_addrn:=false;
  1675. p1:=gensinglenode(addrn,p1);
  1676. end;
  1677. _LKLAMMER : begin
  1678. consume(_LKLAMMER);
  1679. p1:=comp_expr(true);
  1680. consume(_RKLAMMER);
  1681. { it's not a good solution }
  1682. { but (a+b)^ makes some problems }
  1683. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1684. begin
  1685. { we need the resulttype }
  1686. { of the expression in pd }
  1687. do_firstpass(p1);
  1688. pd:=p1^.resulttype;
  1689. again:=true;
  1690. postfixoperators;
  1691. end;
  1692. end;
  1693. _LECKKLAMMER : begin
  1694. consume(_LECKKLAMMER);
  1695. p1:=factor_read_set;
  1696. consume(_RECKKLAMMER);
  1697. end;
  1698. _PLUS : begin
  1699. consume(_PLUS);
  1700. p1:=factor(false);
  1701. end;
  1702. _MINUS : begin
  1703. consume(_MINUS);
  1704. p1:=factor(false);
  1705. p1:=gensinglenode(umminusn,p1);
  1706. end;
  1707. _NOT : begin
  1708. consume(_NOT);
  1709. p1:=factor(false);
  1710. p1:=gensinglenode(notn,p1);
  1711. end;
  1712. _TRUE : begin
  1713. consume(_TRUE);
  1714. p1:=genordinalconstnode(1,booldef);
  1715. end;
  1716. _FALSE : begin
  1717. consume(_FALSE);
  1718. p1:=genordinalconstnode(0,booldef);
  1719. end;
  1720. _NIL : begin
  1721. consume(_NIL);
  1722. p1:=genzeronode(niln);
  1723. end;
  1724. else
  1725. begin
  1726. p1:=genzeronode(errorn);
  1727. consume(token);
  1728. Message(cg_e_illegal_expression);
  1729. end;
  1730. end;
  1731. { generate error node if no node is created }
  1732. if not assigned(p1) then
  1733. p1:=genzeronode(errorn);
  1734. { tp7 procvar handling, but not if the next token
  1735. will be a := }
  1736. if (m_tp_procvar in aktmodeswitches) and
  1737. (token<>_ASSIGNMENT) then
  1738. check_tp_procvar(p1);
  1739. factor:=p1;
  1740. check_tokenpos;
  1741. end;
  1742. {****************************************************************************
  1743. Sub_Expr
  1744. ****************************************************************************}
  1745. type
  1746. Toperator_precedence=(opcompare,opaddition,opmultiply);
  1747. Ttok2nodeRec=record
  1748. tok : ttoken;
  1749. nod : ttreetyp;
  1750. end;
  1751. const
  1752. tok2nodes=23;
  1753. tok2node:array[1..tok2nodes] of ttok2noderec=(
  1754. (tok:_PLUS ;nod:addn),
  1755. (tok:_MINUS ;nod:subn),
  1756. (tok:_STAR ;nod:muln),
  1757. (tok:_SLASH ;nod:slashn),
  1758. (tok:_EQUAL ;nod:equaln),
  1759. (tok:_GT ;nod:gtn),
  1760. (tok:_LT ;nod:ltn),
  1761. (tok:_GTE ;nod:gten),
  1762. (tok:_LTE ;nod:lten),
  1763. (tok:_SYMDIF ;nod:symdifn),
  1764. (tok:_STARSTAR;nod:starstarn),
  1765. (tok:_CARET ;nod:caretn),
  1766. (tok:_UNEQUAL ;nod:unequaln),
  1767. (tok:_AS ;nod:asn),
  1768. (tok:_IN ;nod:inn),
  1769. (tok:_IS ;nod:isn),
  1770. (tok:_OR ;nod:orn),
  1771. (tok:_AND ;nod:andn),
  1772. (tok:_DIV ;nod:divn),
  1773. (tok:_MOD ;nod:modn),
  1774. (tok:_SHL ;nod:shln),
  1775. (tok:_SHR ;nod:shrn),
  1776. (tok:_XOR ;nod:xorn)
  1777. );
  1778. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1779. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_IN,_IS],
  1780. [_PLUS,_MINUS,_OR,_XOR],
  1781. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,_DIV,_MOD,_AND,_SHL,_SHR,_AS]);
  1782. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):Ptree;
  1783. {Reads a subexpression while the operators are of the current precedence
  1784. level, or any higher level. Replaces the old term, simpl_expr and
  1785. simpl2_expr.}
  1786. var
  1787. low,high,mid : longint;
  1788. p1,p2 : Ptree;
  1789. oldt : Ttoken;
  1790. filepos : tfileposinfo;
  1791. begin
  1792. if pred_level=opmultiply then
  1793. p1:=factor(false)
  1794. else
  1795. p1:=sub_expr(succ(pred_level),true);
  1796. repeat
  1797. if (token in operator_levels[pred_level]) and
  1798. ((token<>_EQUAL) or accept_equal) then
  1799. begin
  1800. oldt:=token;
  1801. filepos:=tokenpos;
  1802. consume(token);
  1803. if pred_level=opmultiply then
  1804. p2:=factor(false)
  1805. else
  1806. p2:=sub_expr(succ(pred_level),true);
  1807. low:=1;
  1808. high:=tok2nodes;
  1809. while (low<high) do
  1810. begin
  1811. mid:=(low+high+1) shr 1;
  1812. if oldt<tok2node[mid].tok then
  1813. high:=mid-1
  1814. else
  1815. low:=mid;
  1816. end;
  1817. if tok2node[high].tok=oldt then
  1818. p1:=gennode(tok2node[high].nod,p1,p2)
  1819. else
  1820. p1:=gennode(nothingn,p1,p2);
  1821. set_tree_filepos(p1,filepos);
  1822. end
  1823. else
  1824. break;
  1825. until false;
  1826. sub_expr:=p1;
  1827. end;
  1828. function comp_expr(accept_equal : boolean):Ptree;
  1829. var
  1830. oldafterassignment : boolean;
  1831. p1 : ptree;
  1832. begin
  1833. oldafterassignment:=afterassignment;
  1834. afterassignment:=true;
  1835. p1:=sub_expr(opcompare,accept_equal);
  1836. afterassignment:=oldafterassignment;
  1837. comp_expr:=p1;
  1838. end;
  1839. function expr : ptree;
  1840. var
  1841. p1,p2 : ptree;
  1842. oldafterassignment : boolean;
  1843. oldp1 : ptree;
  1844. filepos : tfileposinfo;
  1845. begin
  1846. oldafterassignment:=afterassignment;
  1847. p1:=sub_expr(opcompare,true);
  1848. filepos:=tokenpos;
  1849. if (m_tp_procvar in aktmodeswitches) and
  1850. (token<>_ASSIGNMENT) then
  1851. check_tp_procvar(p1);
  1852. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  1853. afterassignment:=true;
  1854. oldp1:=p1;
  1855. case token of
  1856. _POINTPOINT : begin
  1857. consume(_POINTPOINT);
  1858. p2:=sub_expr(opcompare,true);
  1859. p1:=gennode(rangen,p1,p2);
  1860. end;
  1861. _ASSIGNMENT : begin
  1862. consume(_ASSIGNMENT);
  1863. { avoid a firstpass of a procedure if
  1864. it must be assigned to a procvar }
  1865. { should be recursive for a:=b:=c !!! }
  1866. if (p1^.resulttype<>nil) and (p1^.resulttype^.deftype=procvardef) then
  1867. begin
  1868. getprocvar:=true;
  1869. getprocvardef:=pprocvardef(p1^.resulttype);
  1870. end;
  1871. p2:=sub_expr(opcompare,true);
  1872. if getprocvar then
  1873. begin
  1874. if (p2^.treetype=calln) then
  1875. handle_procvar(getprocvardef,p2)
  1876. else
  1877. { also allow p:= proc(t); !! (PM) }
  1878. if (p2^.treetype=typeconvn) and
  1879. (p2^.left^.treetype=calln) then
  1880. handle_procvar(getprocvardef,p2^.left);
  1881. end;
  1882. getprocvar:=false;
  1883. p1:=gennode(assignn,p1,p2);
  1884. end;
  1885. { this is the code for C like assignements }
  1886. { from an improvement of Peter Schaefer }
  1887. _PLUSASN : begin
  1888. consume(_PLUSASN );
  1889. p2:=sub_expr(opcompare,true);
  1890. p1:=gennode(assignn,p1,gennode(addn,getcopy(p1),p2));
  1891. { was first
  1892. p1:=gennode(assignn,p1,gennode(addn,p1,p2));
  1893. but disposetree assumes that we have a real
  1894. *** tree *** }
  1895. end;
  1896. _MINUSASN : begin
  1897. consume(_MINUSASN );
  1898. p2:=sub_expr(opcompare,true);
  1899. p1:=gennode(assignn,p1,gennode(subn,getcopy(p1),p2));
  1900. end;
  1901. _STARASN : begin
  1902. consume(_STARASN );
  1903. p2:=sub_expr(opcompare,true);
  1904. p1:=gennode(assignn,p1,gennode(muln,getcopy(p1),p2));
  1905. end;
  1906. _SLASHASN : begin
  1907. consume(_SLASHASN );
  1908. p2:=sub_expr(opcompare,true);
  1909. p1:=gennode(assignn,p1,gennode(slashn,getcopy(p1),p2));
  1910. end;
  1911. end;
  1912. afterassignment:=oldafterassignment;
  1913. if p1<>oldp1 then
  1914. set_tree_filepos(p1,filepos);
  1915. expr:=p1;
  1916. end;
  1917. function get_intconst:longint;
  1918. {Reads an expression, tries to evalute it and check if it is an integer
  1919. constant. Then the constant is returned.}
  1920. var
  1921. p:Ptree;
  1922. begin
  1923. p:=comp_expr(true);
  1924. do_firstpass(p);
  1925. if not codegenerror then
  1926. begin
  1927. if (p^.treetype<>ordconstn) and
  1928. (p^.resulttype^.deftype=orddef) and
  1929. not(Porddef(p^.resulttype)^.typ in [uvoid,uchar,bool8bit,bool16bit,bool32bit]) then
  1930. Message(cg_e_illegal_expression)
  1931. else
  1932. get_intconst:=p^.value;
  1933. end;
  1934. disposetree(p);
  1935. end;
  1936. function get_stringconst:string;
  1937. {Reads an expression, tries to evaluate it and checks if it is a string
  1938. constant. Then the constant is returned.}
  1939. var
  1940. p:Ptree;
  1941. begin
  1942. get_stringconst:='';
  1943. p:=comp_expr(true);
  1944. do_firstpass(p);
  1945. if p^.treetype<>stringconstn then
  1946. begin
  1947. if (p^.treetype=ordconstn) and is_char(p^.resulttype) then
  1948. get_stringconst:=char(p^.value)
  1949. else
  1950. Message(cg_e_illegal_expression);
  1951. end
  1952. else
  1953. get_stringconst:=strpas(p^.value_str);
  1954. disposetree(p);
  1955. end;
  1956. end.
  1957. {
  1958. $Log$
  1959. Revision 1.133 1999-08-05 16:53:04 peter
  1960. * V_Fatal=1, all other V_ are also increased
  1961. * Check for local procedure when assigning procvar
  1962. * fixed comment parsing because directives
  1963. * oldtp mode directives better supported
  1964. * added some messages to errore.msg
  1965. Revision 1.132 1999/08/04 13:49:45 florian
  1966. * new(...)^. is now allowed
  1967. Revision 1.131 1999/08/04 13:02:55 jonas
  1968. * all tokens now start with an underscore
  1969. * PowerPC compiles!!
  1970. Revision 1.130 1999/08/04 00:23:12 florian
  1971. * renamed i386asm and i386base to cpuasm and cpubase
  1972. Revision 1.129 1999/08/03 22:02:59 peter
  1973. * moved bitmask constants to sets
  1974. * some other type/const renamings
  1975. Revision 1.128 1999/08/03 13:50:17 michael
  1976. + Changes for alpha
  1977. Revision 1.127 1999/08/01 18:28:13 florian
  1978. * modifications for the new code generator
  1979. Revision 1.126 1999/07/30 12:28:40 peter
  1980. * fixed crash with unknown id and colon parameter in write
  1981. Revision 1.125 1999/07/27 23:42:14 peter
  1982. * indirect type referencing is now allowed
  1983. Revision 1.124 1999/07/23 21:31:42 peter
  1984. * fixed crash with resourcestring
  1985. Revision 1.123 1999/07/23 11:37:46 peter
  1986. * error for illegal type reference, instead of 10998
  1987. Revision 1.122 1999/07/22 09:37:52 florian
  1988. + resourcestring implemented
  1989. + start of longstring support
  1990. Revision 1.121 1999/07/16 10:04:35 peter
  1991. * merged
  1992. Revision 1.120 1999/07/06 22:38:11 florian
  1993. * another fix for TP/Delphi styled procedure variables
  1994. Revision 1.119 1999/07/05 20:13:16 peter
  1995. * removed temp defines
  1996. Revision 1.118 1999/07/01 21:33:57 peter
  1997. * merged
  1998. Revision 1.117 1999/06/30 15:43:20 florian
  1999. * two bugs regarding method variables fixed
  2000. - if you take in a method the address of another method
  2001. don't need self anymore
  2002. - if the class pointer was in a register, wrong code for a method
  2003. variable load was generated
  2004. Revision 1.116 1999/06/26 00:24:53 pierre
  2005. * mereg from fixes-0_99_12 branch
  2006. Revision 1.112.2.8 1999/07/16 09:54:57 peter
  2007. * @procvar support in tp7 mode works again
  2008. Revision 1.112.2.7 1999/07/07 07:53:10 michael
  2009. + Merged patches from florian
  2010. Revision 1.112.2.6 1999/07/01 21:31:59 peter
  2011. * procvar fixes again
  2012. Revision 1.112.2.5 1999/07/01 15:17:17 peter
  2013. * methoidpointer fixes from florian
  2014. Revision 1.112.2.4 1999/06/26 00:22:30 pierre
  2015. * wrong warnings in -So mode suppressed
  2016. Revision 1.112.2.3 1999/06/17 12:51:44 pierre
  2017. * changed is_assignment_overloaded into
  2018. function assignment_overloaded : pprocdef
  2019. to allow overloading of assignment with only different result type
  2020. Revision 1.112.2.2 1999/06/15 18:54:52 peter
  2021. * more procvar fixes
  2022. Revision 1.112.2.1 1999/06/13 22:38:09 peter
  2023. * tp_procvar check for loading of procvars when getaddr=false
  2024. Revision 1.112 1999/06/02 22:44:11 pierre
  2025. * previous wrong log corrected
  2026. Revision 1.111 1999/06/02 22:25:43 pierre
  2027. * changed $ifdef FPC @ into $ifndef TP
  2028. * changes for correct procvar handling under tp mode
  2029. Revision 1.110 1999/06/01 19:27:55 peter
  2030. * better checks for procvar and methodpointer
  2031. Revision 1.109 1999/05/27 19:44:46 peter
  2032. * removed oldasm
  2033. * plabel -> pasmlabel
  2034. * -a switches to source writing automaticly
  2035. * assembler readers OOPed
  2036. * asmsymbol automaticly external
  2037. * jumptables and other label fixes for asm readers
  2038. Revision 1.108 1999/05/18 14:15:54 peter
  2039. * containsself fixes
  2040. * checktypes()
  2041. Revision 1.107 1999/05/18 09:52:18 peter
  2042. * procedure of object and addrn fixes
  2043. Revision 1.106 1999/05/16 17:06:31 peter
  2044. * remove firstcallparan which looks obsolete
  2045. Revision 1.105 1999/05/12 22:36:09 florian
  2046. * override isn't allowed in objects!
  2047. Revision 1.104 1999/05/07 10:35:23 florian
  2048. * first fix for a problem with method pointer properties, still doesn't work
  2049. with WITH
  2050. Revision 1.103 1999/05/06 21:40:16 peter
  2051. * fixed crash
  2052. Revision 1.101 1999/05/06 09:05:21 peter
  2053. * generic write_float and str_float
  2054. * fixed constant float conversions
  2055. Revision 1.100 1999/05/04 21:44:57 florian
  2056. * changes to compile it with Delphi 4.0
  2057. Revision 1.99 1999/05/01 13:24:31 peter
  2058. * merged nasm compiler
  2059. * old asm moved to oldasm/
  2060. Revision 1.98 1999/04/26 18:29:56 peter
  2061. * farpointerdef moved into pointerdef.is_far
  2062. Revision 1.97 1999/04/19 09:27:48 peter
  2063. * removed my property fix
  2064. Revision 1.96 1999/04/19 09:13:47 peter
  2065. * class property without write support
  2066. Revision 1.95 1999/04/19 06:10:08 florian
  2067. * property problem fixed: a propertysym is only a write
  2068. access if it is followed by a assignment token
  2069. Revision 1.94 1999/04/17 13:12:17 peter
  2070. * addr() internal
  2071. Revision 1.93 1999/04/15 09:00:08 peter
  2072. * fixed property write
  2073. Revision 1.92 1999/04/08 20:59:43 florian
  2074. * fixed problem with default properties which are a class
  2075. * case bug (from the mailing list with -O2) fixed, the
  2076. distance of the case labels can be greater than the positive
  2077. range of a longint => it is now a dword for fpc
  2078. Revision 1.91 1999/04/06 11:21:56 peter
  2079. * more use of ttoken
  2080. Revision 1.90 1999/03/31 13:55:12 peter
  2081. * assembler inlining working for ag386bin
  2082. Revision 1.89 1999/03/26 00:05:36 peter
  2083. * released valintern
  2084. + deffile is now removed when compiling is finished
  2085. * ^( compiles now correct
  2086. + static directive
  2087. * shrd fixed
  2088. Revision 1.88 1999/03/24 23:17:15 peter
  2089. * fixed bugs 212,222,225,227,229,231,233
  2090. Revision 1.87 1999/03/16 17:52:52 jonas
  2091. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  2092. * in cgi386inl: also range checking for subrange types (compile with "-dreadrangecheck")
  2093. * in cgai386: also small fixes to emitrangecheck
  2094. Revision 1.86 1999/03/04 13:55:44 pierre
  2095. * some m68k fixes (still not compilable !)
  2096. * new(tobj) does not give warning if tobj has no VMT !
  2097. Revision 1.85 1999/02/22 15:09:39 florian
  2098. * behaviaor of PROTECTED and PRIVATE fixed, works now like TP/Delphi
  2099. Revision 1.84 1999/02/22 02:15:26 peter
  2100. * updates for ag386bin
  2101. Revision 1.83 1999/02/11 09:46:25 pierre
  2102. * fix for normal method calls inside static methods :
  2103. WARNING there were both parser and codegen errors !!
  2104. added static_call boolean to calln tree
  2105. Revision 1.82 1999/01/28 14:06:47 florian
  2106. * small fix for method pointers
  2107. * found the annoying strpas bug, mainly nested call to type cast which
  2108. use ansistrings crash
  2109. Revision 1.81 1999/01/27 00:13:55 florian
  2110. * "procedure of object"-stuff fixed
  2111. Revision 1.80 1999/01/21 16:41:01 pierre
  2112. * fix for constructor inside with statements
  2113. Revision 1.79 1998/12/30 22:15:48 peter
  2114. + farpointer type
  2115. * absolutesym now also stores if its far
  2116. Revision 1.78 1998/12/11 00:03:32 peter
  2117. + globtype,tokens,version unit splitted from globals
  2118. Revision 1.77 1998/12/04 10:18:09 florian
  2119. * some stuff for procedures of object added
  2120. * bug with overridden virtual constructors fixed (reported by Italo Gomes)
  2121. Revision 1.76 1998/11/27 14:50:40 peter
  2122. + open strings, $P switch support
  2123. Revision 1.75 1998/11/25 19:12:51 pierre
  2124. * var:=new(pointer_type) support added
  2125. Revision 1.74 1998/11/13 10:18:11 peter
  2126. + nil constants
  2127. Revision 1.73 1998/11/05 12:02:52 peter
  2128. * released useansistring
  2129. * removed -Sv, its now available in fpc modes
  2130. Revision 1.72 1998/11/04 10:11:41 peter
  2131. * ansistring fixes
  2132. Revision 1.71 1998/10/22 23:57:29 peter
  2133. * fixed filedef for typenodetype
  2134. Revision 1.70 1998/10/21 15:12:54 pierre
  2135. * bug fix for IOCHECK inside a procedure with iocheck modifier
  2136. * removed the GPF for unexistant overloading
  2137. (firstcall was called with procedinition=nil !)
  2138. * changed typen to what Florian proposed
  2139. gentypenode(p : pdef) sets the typenodetype field
  2140. and resulttype is only set if inside bt_type block !
  2141. Revision 1.69 1998/10/20 15:10:19 pierre
  2142. * type ptree only allowed inside expression
  2143. if following sizeof typeof low high or as first arg of new !!
  2144. Revision 1.68 1998/10/20 11:15:44 pierre
  2145. * calling of private method allowed inside child object method
  2146. Revision 1.67 1998/10/19 08:54:57 pierre
  2147. * wrong stabs info corrected once again !!
  2148. + variable vmt offset with vmt field only if required
  2149. implemented now !!!
  2150. Revision 1.66 1998/10/15 15:13:28 pierre
  2151. + added oo_hasconstructor and oo_hasdestructor
  2152. for objects options
  2153. Revision 1.65 1998/10/13 13:10:24 peter
  2154. * new style for m68k/i386 infos and enums
  2155. Revision 1.64 1998/10/12 12:20:55 pierre
  2156. + added tai_const_symbol_offset
  2157. for r : pointer = @var.field;
  2158. * better message for different arg names on implementation
  2159. of function
  2160. Revision 1.63 1998/10/12 10:28:30 florian
  2161. + auto dereferencing of pointers to structured types in delphi mode
  2162. Revision 1.62 1998/10/12 10:05:41 peter
  2163. * fixed mem leak with arrayconstrutor
  2164. Revision 1.61 1998/10/05 13:57:15 peter
  2165. * crash preventions
  2166. Revision 1.60 1998/10/05 12:32:46 peter
  2167. + assert() support
  2168. Revision 1.59 1998/10/01 14:56:24 peter
  2169. * crash preventions
  2170. Revision 1.58 1998/09/30 07:40:35 florian
  2171. * better error recovering
  2172. Revision 1.57 1998/09/28 16:18:16 florian
  2173. * two fixes to get ansi strings work
  2174. Revision 1.56 1998/09/26 17:45:36 peter
  2175. + idtoken and only one token table
  2176. Revision 1.55 1998/09/24 23:49:10 peter
  2177. + aktmodeswitches
  2178. Revision 1.54 1998/09/23 15:46:39 florian
  2179. * problem with with and classes fixed
  2180. Revision 1.53 1998/09/23 09:58:54 peter
  2181. * first working array of const things
  2182. Revision 1.52 1998/09/20 09:38:45 florian
  2183. * hasharray for defs fixed
  2184. * ansistring code generation corrected (init/final, assignement)
  2185. Revision 1.51 1998/09/18 16:03:43 florian
  2186. * some changes to compile with Delphi
  2187. Revision 1.50 1998/09/17 13:41:18 pierre
  2188. sizeof(TPOINT) problem
  2189. Revision 1.49.2.1 1998/09/17 08:42:31 pierre
  2190. TPOINT sizeof fix
  2191. Revision 1.49 1998/09/09 11:50:53 pierre
  2192. * forward def are not put in record or objects
  2193. + added check for forwards also in record and objects
  2194. * dummy parasymtable for unit initialization removed from
  2195. symtable stack
  2196. Revision 1.48 1998/09/07 22:25:53 peter
  2197. * fixed str(boolean,string) which was allowed
  2198. * fixed write(' ':<int expression>) only constants where allowed :(
  2199. Revision 1.47 1998/09/07 18:46:10 peter
  2200. * update smartlinking, uses getdatalabel
  2201. * renamed ptree.value vars to value_str,value_real,value_set
  2202. Revision 1.46 1998/09/04 08:42:03 peter
  2203. * updated some error messages
  2204. Revision 1.45 1998/09/01 17:39:49 peter
  2205. + internal constant functions
  2206. Revision 1.44 1998/08/28 10:54:24 peter
  2207. * fixed smallset generation from elements, it has never worked before!
  2208. Revision 1.43 1998/08/23 16:07:24 florian
  2209. * internalerror with mod/div fixed
  2210. Revision 1.42 1998/08/21 14:08:50 pierre
  2211. + TEST_FUNCRET now default (old code removed)
  2212. works also for m68k (at least compiles)
  2213. Revision 1.41 1998/08/20 21:36:39 peter
  2214. * fixed 'with object do' bug
  2215. Revision 1.40 1998/08/20 09:26:41 pierre
  2216. + funcret setting in underproc testing
  2217. compile with _dTEST_FUNCRET
  2218. Revision 1.39 1998/08/18 16:48:48 pierre
  2219. * bug for -So proc assignment to p^rocvar fixed
  2220. Revision 1.38 1998/08/18 14:17:09 pierre
  2221. * bug about assigning the return value of a function to
  2222. a procvar fixed : warning
  2223. assigning a proc to a procvar need @ in FPC mode !!
  2224. * missing file/line info restored
  2225. Revision 1.37 1998/08/18 09:24:43 pierre
  2226. * small warning position bug fixed
  2227. * support_mmx switches splitting was missing
  2228. * rhide error and warning output corrected
  2229. Revision 1.36 1998/08/15 16:50:29 peter
  2230. * fixed proc()=expr which was not allowed anymore by my previous fix
  2231. Revision 1.35 1998/08/14 18:18:46 peter
  2232. + dynamic set contruction
  2233. * smallsets are now working (always longint size)
  2234. Revision 1.34 1998/08/13 11:00:12 peter
  2235. * fixed procedure<>procedure construct
  2236. Revision 1.33 1998/08/11 15:31:39 peter
  2237. * write extended to ppu file
  2238. * new version 0.99.7
  2239. Revision 1.32 1998/08/11 14:05:32 peter
  2240. * fixed sizeof(array of char)
  2241. Revision 1.31 1998/08/10 14:50:11 peter
  2242. + localswitches, moduleswitches, globalswitches splitting
  2243. Revision 1.30 1998/07/28 21:52:54 florian
  2244. + implementation of raise and try..finally
  2245. + some misc. exception stuff
  2246. Revision 1.29 1998/07/27 21:57:13 florian
  2247. * fix to allow tv like stream registration:
  2248. @tmenu.load doesn't work if load had parameters or if load was only
  2249. declared in an anchestor class of tmenu
  2250. Revision 1.28 1998/07/14 21:46:51 peter
  2251. * updated messages file
  2252. Revision 1.27 1998/06/25 14:04:23 peter
  2253. + internal inc/dec
  2254. Revision 1.26 1998/06/09 16:01:46 pierre
  2255. + added procedure directive parsing for procvars
  2256. (accepted are popstack cdecl and pascal)
  2257. + added C vars with the following syntax
  2258. var C calias 'true_c_name';(can be followed by external)
  2259. reason is that you must add the Cprefix
  2260. which is target dependent
  2261. Revision 1.25 1998/06/05 14:37:33 pierre
  2262. * fixes for inline for operators
  2263. * inline procedure more correctly restricted
  2264. Revision 1.24 1998/06/04 23:51:52 peter
  2265. * m68k compiles
  2266. + .def file creation moved to gendef.pas so it could also be used
  2267. for win32
  2268. Revision 1.23 1998/06/04 09:55:40 pierre
  2269. * demangled name of procsym reworked to become independant of the mangling scheme
  2270. Revision 1.22 1998/06/02 17:03:03 pierre
  2271. * with node corrected for objects
  2272. * small bugs for SUPPORT_MMX fixed
  2273. Revision 1.21 1998/05/27 19:45:05 peter
  2274. * symtable.pas splitted into includefiles
  2275. * symtable adapted for $ifdef NEWPPU
  2276. Revision 1.20 1998/05/26 07:53:59 pierre
  2277. * bug fix for empty sets (nil pd was dereferenced )
  2278. Revision 1.19 1998/05/25 17:11:43 pierre
  2279. * firstpasscount bug fixed
  2280. now all is already set correctly the first time
  2281. under EXTDEBUG try -gp to skip all other firstpasses
  2282. it works !!
  2283. * small bug fixes
  2284. - for smallsets with -dTESTSMALLSET
  2285. - some warnings removed (by correcting code !)
  2286. Revision 1.18 1998/05/23 01:21:20 peter
  2287. + aktasmmode, aktoptprocessor, aktoutputformat
  2288. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  2289. + $LIBNAME to set the library name where the unit will be put in
  2290. * splitted cgi386 a bit (codeseg to large for bp7)
  2291. * nasm, tasm works again. nasm moved to ag386nsm.pas
  2292. Revision 1.17 1998/05/22 12:37:03 carl
  2293. * crash bugfix (patched msanually to main branch)
  2294. Revision 1.16 1998/05/21 19:33:32 peter
  2295. + better procedure directive handling and only one table
  2296. Revision 1.15 1998/05/20 09:42:35 pierre
  2297. + UseTokenInfo now default
  2298. * unit in interface uses and implementation uses gives error now
  2299. * only one error for unknown symbol (uses lastsymknown boolean)
  2300. the problem came from the label code !
  2301. + first inlined procedures and function work
  2302. (warning there might be allowed cases were the result is still wrong !!)
  2303. * UseBrower updated gives a global list of all position of all used symbols
  2304. with switch -gb
  2305. Revision 1.14 1998/05/11 13:07:56 peter
  2306. + $ifdef NEWPPU for the new ppuformat
  2307. + $define GDB not longer required
  2308. * removed all warnings and stripped some log comments
  2309. * no findfirst/findnext anymore to remove smartlink *.o files
  2310. Revision 1.13 1998/05/06 08:38:45 pierre
  2311. * better position info with UseTokenInfo
  2312. UseTokenInfo greatly simplified
  2313. + added check for changed tree after first time firstpass
  2314. (if we could remove all the cases were it happen
  2315. we could skip all firstpass if firstpasscount > 1)
  2316. Only with ExtDebug
  2317. Revision 1.12 1998/05/05 12:05:42 florian
  2318. * problems with properties fixed
  2319. * crash fixed: i:=l when i and l are undefined, was a problem with
  2320. implementation of private/protected
  2321. Revision 1.11 1998/05/04 11:22:26 florian
  2322. * problem with DOM solved: it crashes when accessing a property in a method
  2323. Revision 1.10 1998/05/01 16:38:45 florian
  2324. * handling of private and protected fixed
  2325. + change_keywords_to_tp implemented to remove
  2326. keywords which aren't supported by tp
  2327. * break and continue are now symbols of the system unit
  2328. + widestring, longstring and ansistring type released
  2329. Revision 1.9 1998/04/29 10:33:58 pierre
  2330. + added some code for ansistring (not complete nor working yet)
  2331. * corrected operator overloading
  2332. * corrected nasm output
  2333. + started inline procedures
  2334. + added starstarn : use ** for exponentiation (^ gave problems)
  2335. + started UseTokenInfo cond to get accurate positions
  2336. Revision 1.8 1998/04/14 23:27:03 florian
  2337. + exclude/include with constant second parameter added
  2338. Revision 1.7 1998/04/09 23:02:15 florian
  2339. * small problems solved to get remake3 work
  2340. Revision 1.6 1998/04/09 22:16:35 florian
  2341. * problem with previous REGALLOC solved
  2342. * improved property support
  2343. Revision 1.5 1998/04/08 10:26:09 florian
  2344. * correct error handling of virtual constructors
  2345. * problem with new type declaration handling fixed
  2346. Revision 1.4 1998/04/07 22:45:05 florian
  2347. * bug0092, bug0115 and bug0121 fixed
  2348. + packed object/class/array
  2349. Revision 1.3 1998/04/07 13:19:46 pierre
  2350. * bugfixes for reset_gdb_info
  2351. in MEM parsing for go32v2
  2352. better external symbol creation
  2353. support for rhgdb.exe (lowercase file names)
  2354. }