pexpr.pas 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. {$i defines.inc}
  20. interface
  21. uses
  22. symtable,
  23. node;
  24. { reads a whole expression }
  25. function expr : tnode;
  26. { reads an expression without assignements and .. }
  27. function comp_expr(accept_equal : boolean):tnode;
  28. { reads a single factor }
  29. function factor(getaddr : boolean) : tnode;
  30. function string_dec : pdef;
  31. { the ID token has to be consumed before calling this function }
  32. procedure do_member_read(getaddr : boolean;const sym : psym;var p1 : tnode;
  33. var pd : pdef;var again : boolean);
  34. function get_intconst:longint;
  35. function get_stringconst:string;
  36. implementation
  37. uses
  38. { common }
  39. cutils,cobjects,
  40. { global }
  41. globtype,globals,tokens,verbose,
  42. systems,cpuinfo,
  43. { aasm }
  44. aasm,
  45. { symtable }
  46. symconst,types,
  47. {$ifdef GDB}
  48. gdb,
  49. {$endif}
  50. { pass 1 }
  51. pass_1,htypechk,
  52. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,
  53. { parser }
  54. scanner,
  55. pbase,
  56. { codegen }
  57. {$ifdef newcg}
  58. cgbase
  59. {$else}
  60. hcodegen
  61. {$endif}
  62. ;
  63. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  64. read as - (1**4) and not (-1)**4 PM }
  65. type
  66. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  67. const
  68. highest_precedence = oppower;
  69. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;forward;
  70. const
  71. allow_type : boolean = true;
  72. got_addrn : boolean = false;
  73. auto_inherited : boolean = false;
  74. function string_dec : pdef;
  75. { reads a string type with optional length }
  76. { and returns a pointer to the string }
  77. { definition }
  78. var
  79. p : tnode;
  80. d : pdef;
  81. begin
  82. consume(_STRING);
  83. if token=_LECKKLAMMER then
  84. begin
  85. consume(_LECKKLAMMER);
  86. p:=comp_expr(true);
  87. do_firstpass(p);
  88. if not is_constintnode(p) then
  89. begin
  90. Message(cg_e_illegal_expression);
  91. { error recovery }
  92. consume(_RECKKLAMMER);
  93. d:=cshortstringdef;
  94. end
  95. else
  96. begin
  97. if (tordconstnode(p).value<=0) then
  98. begin
  99. Message(parser_e_invalid_string_size);
  100. tordconstnode(p).value:=255;
  101. end;
  102. consume(_RECKKLAMMER);
  103. if tordconstnode(p).value>255 then
  104. d:=new(pstringdef,longinit(tordconstnode(p).value))
  105. else
  106. if tordconstnode(p).value<>255 then
  107. d:=new(pstringdef,shortinit(tordconstnode(p).value))
  108. else
  109. d:=cshortstringdef;
  110. end;
  111. p.free;
  112. end
  113. else
  114. begin
  115. if cs_ansistrings in aktlocalswitches then
  116. d:=cansistringdef
  117. else
  118. d:=cshortstringdef;
  119. end;
  120. string_dec:=d;
  121. end;
  122. function parse_paras(__colon,in_prop_paras : boolean) : tnode;
  123. var
  124. p1,p2 : tnode;
  125. end_of_paras : ttoken;
  126. begin
  127. if in_prop_paras then
  128. end_of_paras:=_RECKKLAMMER
  129. else
  130. end_of_paras:=_RKLAMMER;
  131. if token=end_of_paras then
  132. begin
  133. parse_paras:=nil;
  134. exit;
  135. end;
  136. p2:=nil;
  137. inc(parsing_para_level);
  138. while true do
  139. begin
  140. p1:=comp_expr(true);
  141. p2:=gencallparanode(p1,p2);
  142. { it's for the str(l:5,s); }
  143. if __colon and (token=_COLON) then
  144. begin
  145. consume(_COLON);
  146. p1:=comp_expr(true);
  147. p2:=gencallparanode(p1,p2);
  148. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  149. if token=_COLON then
  150. begin
  151. consume(_COLON);
  152. p1:=comp_expr(true);
  153. p2:=gencallparanode(p1,p2);
  154. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  155. end
  156. end;
  157. if token=_COMMA then
  158. consume(_COMMA)
  159. else
  160. break;
  161. end;
  162. dec(parsing_para_level);
  163. parse_paras:=p2;
  164. end;
  165. procedure check_tp_procvar(var p : tnode);
  166. var
  167. p1 : tnode;
  168. begin
  169. if (m_tp_procvar in aktmodeswitches) and
  170. (not got_addrn) and
  171. (not in_args) and
  172. (p.nodetype=loadn) then
  173. begin
  174. { support if procvar then for tp7 and many other expression like this }
  175. do_firstpass(p);
  176. set_varstate(p,false);
  177. { reset varstateset to maybe set used state later web bug769 PM }
  178. unset_varstate(p);
  179. if not(getprocvar) and (p.resulttype^.deftype=procvardef) then
  180. begin
  181. p1:=gencallnode(nil,nil);
  182. tcallnode(p1).right:=p;
  183. p1.resulttype:=pprocvardef(p.resulttype)^.rettype.def;
  184. firstpass(p1);
  185. p:=p1;
  186. end;
  187. end;
  188. end;
  189. function statement_syssym(l : longint;var pd : pdef) : tnode;
  190. var
  191. p1,p2,paras : tnode;
  192. prev_in_args : boolean;
  193. begin
  194. prev_in_args:=in_args;
  195. case l of
  196. in_ord_x :
  197. begin
  198. consume(_LKLAMMER);
  199. in_args:=true;
  200. p1:=comp_expr(true);
  201. consume(_RKLAMMER);
  202. do_firstpass(p1);
  203. p1:=geninlinenode(in_ord_x,false,p1);
  204. do_firstpass(p1);
  205. statement_syssym := p1;
  206. pd:=p1.resulttype;
  207. end;
  208. in_break :
  209. begin
  210. statement_syssym:=cbreaknode.create;
  211. pd:=voiddef;
  212. end;
  213. in_continue :
  214. begin
  215. statement_syssym:=ccontinuenode.create;
  216. pd:=voiddef;
  217. end;
  218. in_typeof_x :
  219. begin
  220. consume(_LKLAMMER);
  221. in_args:=true;
  222. {allow_type:=true;}
  223. p1:=comp_expr(true);
  224. {allow_type:=false;}
  225. consume(_RKLAMMER);
  226. pd:=voidpointerdef;
  227. if p1.nodetype=typen then
  228. begin
  229. if (ttypenode(p1).typenodetype=nil) then
  230. begin
  231. Message(type_e_mismatch);
  232. statement_syssym:=cerrornode.create;
  233. end
  234. else
  235. if ttypenode(p1).typenodetype^.deftype=objectdef then
  236. begin
  237. { we can use resulttype in pass_2 (PM) }
  238. p1.resulttype:=ttypenode(p1).typenodetype;
  239. statement_syssym:=geninlinenode(in_typeof_x,false,p1);
  240. end
  241. else
  242. begin
  243. Message(type_e_mismatch);
  244. p1.destroy;
  245. statement_syssym:=cerrornode.create;
  246. end;
  247. end
  248. else { not a type node }
  249. begin
  250. do_firstpass(p1);
  251. set_varstate(p1,false);
  252. if (p1.resulttype=nil) then
  253. begin
  254. Message(type_e_mismatch);
  255. p1.destroy;
  256. statement_syssym:=cerrornode.create;
  257. end
  258. else
  259. if p1.resulttype^.deftype=objectdef then
  260. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  261. else
  262. begin
  263. Message(type_e_mismatch);
  264. statement_syssym:=cerrornode.create;
  265. p1.destroy;
  266. end;
  267. end;
  268. end;
  269. in_sizeof_x :
  270. begin
  271. consume(_LKLAMMER);
  272. in_args:=true;
  273. {allow_type:=true;}
  274. p1:=comp_expr(true);
  275. {allow_type:=false; }
  276. consume(_RKLAMMER);
  277. pd:=s32bitdef;
  278. if p1.nodetype=typen then
  279. begin
  280. statement_syssym:=genordinalconstnode(ttypenode(p1).typenodetype^.size,pd);
  281. { p1 not needed !}
  282. p1.destroy;
  283. end
  284. else
  285. begin
  286. do_firstpass(p1);
  287. if ((p1.resulttype^.deftype=objectdef) and
  288. (oo_has_constructor in pobjectdef(p1.resulttype)^.objectoptions)) or
  289. is_open_array(p1.resulttype) or
  290. is_open_string(p1.resulttype) then
  291. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  292. else
  293. begin
  294. statement_syssym:=genordinalconstnode(p1.resulttype^.size,pd);
  295. { p1 not needed !}
  296. p1.destroy;
  297. end;
  298. end;
  299. end;
  300. in_typeinfo_x :
  301. begin
  302. consume(_LKLAMMER);
  303. in_args:=true;
  304. {allow_type:=true;}
  305. p1:=comp_expr(true);
  306. do_firstpass(p1);
  307. {allow_type:=false; }
  308. if p1.nodetype<>typen then
  309. begin
  310. p1.destroy;
  311. p1:=cerrornode.create;
  312. Message(parser_e_illegal_parameter_list);
  313. end;
  314. consume(_RKLAMMER);
  315. p2:=gencallparanode(p1,nil);
  316. p2:=geninlinenode(in_typeinfo_x,false,p2);
  317. pd:=voidpointerdef;
  318. statement_syssym:=p2;
  319. end;
  320. in_assigned_x :
  321. begin
  322. consume(_LKLAMMER);
  323. in_args:=true;
  324. p1:=comp_expr(true);
  325. do_firstpass(p1);
  326. if not codegenerror then
  327. begin
  328. case p1.resulttype^.deftype of
  329. pointerdef,
  330. procvardef,
  331. classrefdef : ;
  332. objectdef :
  333. if not(pobjectdef(p1.resulttype)^.is_class) then
  334. Message(parser_e_illegal_parameter_list);
  335. else
  336. Message(parser_e_illegal_parameter_list);
  337. end;
  338. end;
  339. p2:=gencallparanode(p1,nil);
  340. p2:=geninlinenode(in_assigned_x,false,p2);
  341. consume(_RKLAMMER);
  342. pd:=booldef;
  343. statement_syssym:=p2;
  344. end;
  345. in_addr_x :
  346. begin
  347. consume(_LKLAMMER);
  348. in_args:=true;
  349. p1:=comp_expr(true);
  350. p1:=caddrnode.create(p1);
  351. do_firstpass(p1);
  352. pd:=p1.resulttype;
  353. consume(_RKLAMMER);
  354. statement_syssym:=p1;
  355. end;
  356. in_ofs_x :
  357. begin
  358. consume(_LKLAMMER);
  359. in_args:=true;
  360. p1:=comp_expr(true);
  361. p1:=caddrnode.create(p1);
  362. do_firstpass(p1);
  363. { Ofs() returns a longint, not a pointer }
  364. p1.resulttype:=u32bitdef;
  365. pd:=p1.resulttype;
  366. consume(_RKLAMMER);
  367. statement_syssym:=p1;
  368. end;
  369. in_seg_x :
  370. begin
  371. consume(_LKLAMMER);
  372. in_args:=true;
  373. { the expression must be allowed to be assigned to }
  374. p1:=comp_expr(true);
  375. p1:=geninlinenode(in_assigned_x,false,p1);
  376. do_firstpass(p1);
  377. consume(_RKLAMMER);
  378. statement_syssym:=p1;
  379. end;
  380. in_high_x,
  381. in_low_x :
  382. begin
  383. consume(_LKLAMMER);
  384. in_args:=true;
  385. {allow_type:=true;}
  386. p1:=comp_expr(true);
  387. {allow_type:=false;}
  388. do_firstpass(p1);
  389. if p1.nodetype=typen then
  390. p1.resulttype:=ttypenode(p1).typenodetype;
  391. p2:=geninlinenode(l,false,p1);
  392. consume(_RKLAMMER);
  393. pd:=s32bitdef;
  394. statement_syssym:=p2;
  395. end;
  396. in_succ_x,
  397. in_pred_x :
  398. begin
  399. consume(_LKLAMMER);
  400. in_args:=true;
  401. p1:=comp_expr(true);
  402. do_firstpass(p1);
  403. p2:=geninlinenode(l,false,p1);
  404. consume(_RKLAMMER);
  405. pd:=p1.resulttype;
  406. statement_syssym:=p2;
  407. end;
  408. in_inc_x,
  409. in_dec_x :
  410. begin
  411. consume(_LKLAMMER);
  412. in_args:=true;
  413. p1:=comp_expr(true);
  414. if token=_COMMA then
  415. begin
  416. consume(_COMMA);
  417. p2:=gencallparanode(comp_expr(true),nil);
  418. end
  419. else
  420. p2:=nil;
  421. p2:=gencallparanode(p1,p2);
  422. statement_syssym:=geninlinenode(l,false,p2);
  423. consume(_RKLAMMER);
  424. pd:=voiddef;
  425. end;
  426. in_concat_x :
  427. begin
  428. consume(_LKLAMMER);
  429. in_args:=true;
  430. p2:=nil;
  431. while true do
  432. begin
  433. p1:=comp_expr(true);
  434. do_firstpass(p1);
  435. set_varstate(p1,true);
  436. if not((p1.resulttype^.deftype=stringdef) or
  437. ((p1.resulttype^.deftype=orddef) and
  438. (porddef(p1.resulttype)^.typ=uchar))) then
  439. Message(parser_e_illegal_parameter_list);
  440. if p2<>nil then
  441. p2:=caddnode.create(addn,p2,p1)
  442. else
  443. p2:=p1;
  444. if token=_COMMA then
  445. consume(_COMMA)
  446. else
  447. break;
  448. end;
  449. consume(_RKLAMMER);
  450. pd:=cshortstringdef;
  451. statement_syssym:=p2;
  452. end;
  453. in_read_x,
  454. in_readln_x :
  455. begin
  456. if token=_LKLAMMER then
  457. begin
  458. consume(_LKLAMMER);
  459. in_args:=true;
  460. paras:=parse_paras(false,false);
  461. consume(_RKLAMMER);
  462. end
  463. else
  464. paras:=nil;
  465. pd:=voiddef;
  466. p1:=geninlinenode(l,false,paras);
  467. do_firstpass(p1);
  468. statement_syssym := p1;
  469. end;
  470. in_write_x,
  471. in_writeln_x :
  472. begin
  473. if token=_LKLAMMER then
  474. begin
  475. consume(_LKLAMMER);
  476. in_args:=true;
  477. paras:=parse_paras(true,false);
  478. consume(_RKLAMMER);
  479. end
  480. else
  481. paras:=nil;
  482. pd:=voiddef;
  483. p1 := geninlinenode(l,false,paras);
  484. do_firstpass(p1);
  485. statement_syssym := p1;
  486. end;
  487. in_str_x_string :
  488. begin
  489. consume(_LKLAMMER);
  490. in_args:=true;
  491. paras:=parse_paras(true,false);
  492. consume(_RKLAMMER);
  493. p1 := geninlinenode(l,false,paras);
  494. do_firstpass(p1);
  495. statement_syssym := p1;
  496. pd:=voiddef;
  497. end;
  498. in_val_x:
  499. Begin
  500. consume(_LKLAMMER);
  501. in_args := true;
  502. p1:= gencallparanode(comp_expr(true), nil);
  503. consume(_COMMA);
  504. p2 := gencallparanode(comp_expr(true),p1);
  505. if (token = _COMMA) then
  506. Begin
  507. consume(_COMMA);
  508. p2 := gencallparanode(comp_expr(true),p2)
  509. End;
  510. consume(_RKLAMMER);
  511. p2 := geninlinenode(l,false,p2);
  512. do_firstpass(p2);
  513. statement_syssym := p2;
  514. pd := voiddef;
  515. End;
  516. in_include_x_y,
  517. in_exclude_x_y :
  518. begin
  519. consume(_LKLAMMER);
  520. in_args:=true;
  521. p1:=comp_expr(true);
  522. consume(_COMMA);
  523. p2:=comp_expr(true);
  524. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  525. consume(_RKLAMMER);
  526. pd:=voiddef;
  527. end;
  528. in_assert_x_y :
  529. begin
  530. consume(_LKLAMMER);
  531. in_args:=true;
  532. p1:=comp_expr(true);
  533. if token=_COMMA then
  534. begin
  535. consume(_COMMA);
  536. p2:=comp_expr(true);
  537. end
  538. else
  539. begin
  540. { then insert an empty string }
  541. p2:=genstringconstnode('',st_default);
  542. end;
  543. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  544. consume(_RKLAMMER);
  545. pd:=voiddef;
  546. end;
  547. else
  548. internalerror(15);
  549. end;
  550. in_args:=prev_in_args;
  551. end;
  552. { reads the parameter for a subroutine call }
  553. procedure do_proc_call(getaddr : boolean;var again : boolean;var p1:tcallnode;var pd:Pdef);
  554. var
  555. prev_in_args : boolean;
  556. prevafterassn : boolean;
  557. hs,hs1 : pvarsym;
  558. st : psymtable;
  559. p2 : tnode;
  560. begin
  561. prev_in_args:=in_args;
  562. prevafterassn:=afterassignment;
  563. afterassignment:=false;
  564. { want we only determine the address of }
  565. { a subroutine ? }
  566. if not(getaddr) then
  567. begin
  568. if auto_inherited then
  569. begin
  570. st:=symtablestack;
  571. while assigned(st) and (st^.symtabletype<>parasymtable) do
  572. st:=st^.next;
  573. p2:=nil;
  574. if assigned(st) then
  575. begin
  576. hs:=pvarsym(st^.symindex^.first);
  577. while assigned(hs) do
  578. begin
  579. if hs^.typ<>varsym then
  580. internalerror(54382953);
  581. { if there is a localcopy then use that }
  582. if assigned(hs^.localvarsym) then
  583. hs1:=hs^.localvarsym
  584. else
  585. hs1:=hs;
  586. p2:=gencallparanode(genloadnode(hs1,hs1^.owner),p2);
  587. hs:=pvarsym(hs^.indexnext);
  588. end;
  589. end
  590. else
  591. internalerror(54382954);
  592. p1.left:=p2;
  593. end
  594. else
  595. begin
  596. if token=_LKLAMMER then
  597. begin
  598. consume(_LKLAMMER);
  599. in_args:=true;
  600. p1.left:=parse_paras(false,false);
  601. consume(_RKLAMMER);
  602. end
  603. else
  604. p1.left:=nil;
  605. end;
  606. { do firstpass because we need the }
  607. { result type }
  608. do_firstpass(p1);
  609. {set_var_state is handled inside firstcalln }
  610. end
  611. else
  612. begin
  613. { address operator @: }
  614. p1.left:=nil;
  615. { forget pd }
  616. pd:=nil;
  617. if (p1.symtableproc^.symtabletype=withsymtable) and
  618. (p1.symtableproc^.defowner^.deftype=objectdef) then
  619. begin
  620. p1.methodpointer:=tnode(pwithsymtable(p1.symtableproc)^.withrefnode).getcopy;
  621. end
  622. else if not(assigned(p1.methodpointer)) then
  623. begin
  624. { we must provide a method pointer, if it isn't given, }
  625. { it is self }
  626. if assigned(procinfo) then
  627. begin
  628. p1.methodpointer:=genselfnode(procinfo^._class);
  629. p1.methodpointer.resulttype:=procinfo^._class;
  630. end
  631. else
  632. begin
  633. p1.methodpointer:=genselfnode(nil);
  634. p1.methodpointer.resulttype:=nil;
  635. end;
  636. end;
  637. { no postfix operators }
  638. again:=false;
  639. end;
  640. pd:=p1.resulttype;
  641. in_args:=prev_in_args;
  642. afterassignment:=prevafterassn;
  643. end;
  644. procedure handle_procvar(pv : pprocvardef;var p2 : tnode);
  645. procedure doconv(procvar : pprocvardef;var t : tnode);
  646. var
  647. hp : tnode;
  648. begin
  649. hp:=nil;
  650. if (proc_to_procvar_equal(pprocsym(tcallnode(t).symtableprocentry)^.definition,procvar)) then
  651. begin
  652. if (po_methodpointer in procvar^.procoptions) then
  653. hp:=genloadmethodcallnode(pprocsym(tcallnode(t).symtableprocentry),tcallnode(t).symtableproc,tnode(tcallnode(t).methodpointer).getcopy)
  654. else
  655. hp:=genloadcallnode(pprocsym(tcallnode(t).symtableprocentry),tcallnode(t).symtableproc);
  656. end;
  657. if assigned(hp) then
  658. begin
  659. t.destroy;
  660. t:=hp;
  661. end;
  662. end;
  663. begin
  664. if (p2.nodetype=calln) then
  665. doconv(pv,tcallnode(p2))
  666. else
  667. if (p2.nodetype=typeconvn) and
  668. (ttypeconvnode(p2).left.nodetype=calln) then
  669. doconv(pv,tcallnode(ttypeconvnode(p2).left));
  670. end;
  671. { the following procedure handles the access to a property symbol }
  672. procedure handle_propertysym(sym : psym;st : psymtable;var p1 : tnode;
  673. var pd : pdef);
  674. var
  675. paras : tnode;
  676. p2 : tnode;
  677. plist : psymlistitem;
  678. begin
  679. paras:=nil;
  680. { property parameters? read them only if the property really }
  681. { has parameters }
  682. if ppo_hasparameters in ppropertysym(sym)^.propoptions then
  683. begin
  684. if token=_LECKKLAMMER then
  685. begin
  686. consume(_LECKKLAMMER);
  687. paras:=parse_paras(false,true);
  688. consume(_RECKKLAMMER);
  689. end;
  690. { indexed property }
  691. if (ppo_indexed in ppropertysym(sym)^.propoptions) then
  692. begin
  693. p2:=genordinalconstnode(ppropertysym(sym)^.index,ppropertysym(sym)^.indextype.def);
  694. paras:=gencallparanode(p2,paras);
  695. end;
  696. end;
  697. { we need only a write property if a := follows }
  698. { if not(afterassignment) and not(in_args) then }
  699. if token=_ASSIGNMENT then
  700. begin
  701. { write property: }
  702. { no result }
  703. pd:=voiddef;
  704. if not ppropertysym(sym)^.writeaccess^.empty then
  705. begin
  706. case ppropertysym(sym)^.writeaccess^.firstsym^.sym^.typ of
  707. procsym :
  708. begin
  709. { generate the method call }
  710. p1:=ccallnode.create(pprocsym(ppropertysym(sym)^.writeaccess^.firstsym^.sym),st,p1);
  711. { we know the procedure to call, so
  712. force the usage of that procedure }
  713. tcallnode(p1).procdefinition:=pprocdef(ppropertysym(sym)^.writeaccess^.def);
  714. tcallnode(p1).left:=paras;
  715. consume(_ASSIGNMENT);
  716. { read the expression }
  717. getprocvar:=ppropertysym(sym)^.proptype.def^.deftype=procvardef;
  718. p2:=comp_expr(true);
  719. if getprocvar then
  720. handle_procvar(pprocvardef(ppropertysym(sym)^.proptype.def),p2);
  721. tcallnode(p1).left:=gencallparanode(p2,tcallnode(p1).left);
  722. include(tcallnode(p1).flags,nf_isproperty);
  723. getprocvar:=false;
  724. end;
  725. varsym :
  726. begin
  727. if assigned(paras) then
  728. message(parser_e_no_paras_allowed);
  729. { subscribed access? }
  730. plist:=ppropertysym(sym)^.writeaccess^.firstsym;
  731. while assigned(plist) do
  732. begin
  733. if p1=nil then
  734. p1:=genloadnode(pvarsym(plist^.sym),st)
  735. else
  736. p1:=gensubscriptnode(pvarsym(plist^.sym),p1);
  737. plist:=plist^.next;
  738. end;
  739. include(tcallnode(p1).flags,nf_isproperty);
  740. consume(_ASSIGNMENT);
  741. { read the expression }
  742. p2:=comp_expr(true);
  743. p1:=cassignmentnode.create(p1,p2);
  744. end
  745. else
  746. begin
  747. p1:=cerrornode.create;
  748. Message(parser_e_no_procedure_to_access_property);
  749. end;
  750. end;
  751. end
  752. else
  753. begin
  754. p1:=cerrornode.create;
  755. Message(parser_e_no_procedure_to_access_property);
  756. end;
  757. end
  758. else
  759. begin
  760. { read property: }
  761. pd:=ppropertysym(sym)^.proptype.def;
  762. if not ppropertysym(sym)^.readaccess^.empty then
  763. begin
  764. case ppropertysym(sym)^.readaccess^.firstsym^.sym^.typ of
  765. varsym :
  766. begin
  767. if assigned(paras) then
  768. message(parser_e_no_paras_allowed);
  769. { subscribed access? }
  770. plist:=ppropertysym(sym)^.readaccess^.firstsym;
  771. while assigned(plist) do
  772. begin
  773. if p1=nil then
  774. p1:=genloadnode(pvarsym(plist^.sym),st)
  775. else
  776. p1:=gensubscriptnode(pvarsym(plist^.sym),p1);
  777. plist:=plist^.next;
  778. end;
  779. include(p1.flags,nf_isproperty);
  780. end;
  781. procsym :
  782. begin
  783. { generate the method call }
  784. p1:=ccallnode.create(pprocsym(ppropertysym(sym)^.readaccess^.firstsym^.sym),st,p1);
  785. { we know the procedure to call, so
  786. force the usage of that procedure }
  787. tcallnode(p1).procdefinition:=pprocdef(ppropertysym(sym)^.readaccess^.def);
  788. { insert paras }
  789. tcallnode(p1).left:=paras;
  790. include(p1.flags,nf_isproperty);
  791. end
  792. else
  793. begin
  794. p1:=cerrornode.create;
  795. Message(type_e_mismatch);
  796. end;
  797. end;
  798. end
  799. else
  800. begin
  801. { error, no function to read property }
  802. p1:=cerrornode.create;
  803. Message(parser_e_no_procedure_to_access_property);
  804. end;
  805. end;
  806. end;
  807. { the ID token has to be consumed before calling this function }
  808. procedure do_member_read(getaddr : boolean;const sym : psym;var p1 : tnode;
  809. var pd : pdef;var again : boolean);
  810. var
  811. static_name : string;
  812. isclassref : boolean;
  813. objdef : pobjectdef;
  814. begin
  815. if sym=nil then
  816. begin
  817. { pattern is still valid unless
  818. there is another ID just after the ID of sym }
  819. Message1(sym_e_id_no_member,pattern);
  820. p1.destroy;
  821. p1:=cerrornode.create;
  822. { try to clean up }
  823. pd:=generrordef;
  824. again:=false;
  825. end
  826. else
  827. begin
  828. objdef:=pobjectdef(sym^.owner^.defowner);
  829. isclassref:=(pd^.deftype=classrefdef);
  830. { check protected and private members }
  831. { please leave this code as it is, }
  832. { it has now the same behaviaor as TP/Delphi }
  833. if (sp_private in sym^.symoptions) and
  834. (objdef^.owner^.symtabletype=unitsymtable) then
  835. Message(parser_e_cant_access_private_member);
  836. if (sp_protected in sym^.symoptions) and
  837. (objdef^.owner^.symtabletype=unitsymtable) then
  838. begin
  839. if assigned(aktprocsym^.definition^._class) then
  840. begin
  841. if not aktprocsym^.definition^._class^.is_related(objdef) then
  842. Message(parser_e_cant_access_protected_member);
  843. end
  844. else
  845. Message(parser_e_cant_access_protected_member);
  846. end;
  847. { we assume, that only procsyms and varsyms are in an object }
  848. { symbol table, for classes, properties are allowed }
  849. case sym^.typ of
  850. procsym:
  851. begin
  852. p1:=ccallnode.create(pprocsym(sym),sym^.owner,p1);
  853. do_proc_call(getaddr or
  854. (getprocvar and
  855. ((block_type=bt_const) or
  856. ((m_tp_procvar in aktmodeswitches) and
  857. proc_to_procvar_equal(pprocsym(sym)^.definition,getprocvardef)
  858. )
  859. )
  860. ),again,tcallnode(p1),pd);
  861. if (block_type=bt_const) and
  862. getprocvar then
  863. handle_procvar(getprocvardef,p1);
  864. { now we know the real method e.g. we can check for a class method }
  865. if isclassref and
  866. assigned(tcallnode(p1).procdefinition) and
  867. not(po_classmethod in tcallnode(p1).procdefinition^.procoptions) and
  868. not(tcallnode(p1).procdefinition^.proctypeoption=potype_constructor) then
  869. Message(parser_e_only_class_methods_via_class_ref);
  870. end;
  871. varsym:
  872. begin
  873. if isclassref then
  874. Message(parser_e_only_class_methods_via_class_ref);
  875. if (sp_static in sym^.symoptions) then
  876. begin
  877. { static_name:=lower(srsymtable^.name^)+'_'+sym^.name;
  878. this is wrong for static field in with symtable (PM) }
  879. static_name:=lower(srsym^.owner^.name^)+'_'+sym^.name;
  880. getsym(static_name,true);
  881. p1.destroy;
  882. p1:=genloadnode(pvarsym(srsym),srsymtable);
  883. end
  884. else
  885. p1:=gensubscriptnode(pvarsym(sym),p1);
  886. pd:=pvarsym(sym)^.vartype.def;
  887. end;
  888. propertysym:
  889. begin
  890. if isclassref then
  891. Message(parser_e_only_class_methods_via_class_ref);
  892. handle_propertysym(sym,srsymtable,p1,pd);
  893. end;
  894. else internalerror(16);
  895. end;
  896. end;
  897. end;
  898. {****************************************************************************
  899. Factor
  900. ****************************************************************************}
  901. {$ifdef fpc}
  902. {$maxfpuregisters 0}
  903. {$endif fpc}
  904. function factor(getaddr : boolean) : tnode;
  905. var
  906. l : longint;
  907. ic : TConstExprInt;
  908. oldp1,
  909. p1,p2,p3 : tnode;
  910. code : integer;
  911. pd,pd2 : pdef;
  912. possible_error,
  913. {$ifdef TEST_PROCSYMS}
  914. unit_specific,
  915. {$endif TEST_PROCSYMS}
  916. again : boolean;
  917. sym : psym;
  918. classh : pobjectdef;
  919. d : bestreal;
  920. hs,
  921. static_name : string;
  922. propsym : ppropertysym;
  923. filepos : tfileposinfo;
  924. {---------------------------------------------
  925. Is_func_ret
  926. ---------------------------------------------}
  927. function is_func_ret(sym : psym) : boolean;
  928. var
  929. p : pprocinfo;
  930. storesymtablestack : psymtable;
  931. begin
  932. is_func_ret:=false;
  933. if not assigned(procinfo) or
  934. ((sym^.typ<>funcretsym) and ((procinfo^.flags and pi_operator)=0)) then
  935. exit;
  936. p:=procinfo;
  937. while assigned(p) do
  938. begin
  939. { is this an access to a function result? Accessing _RESULT is
  940. always allowed and funcretn is generated }
  941. if assigned(p^.funcretsym) and
  942. ((pfuncretsym(sym)=p^.resultfuncretsym) or
  943. ((pfuncretsym(sym)=p^.funcretsym) or
  944. ((pvarsym(sym)=opsym) and ((p^.flags and pi_operator)<>0))) and
  945. (p^.returntype.def<>pdef(voiddef)) and
  946. (token<>_LKLAMMER) and
  947. (not ((m_tp in aktmodeswitches) and (afterassignment or in_args)))
  948. ) then
  949. begin
  950. if ((pvarsym(sym)=opsym) and
  951. ((p^.flags and pi_operator)<>0)) then
  952. inc(opsym^.refs);
  953. p1:=cfuncretnode.create;
  954. pd:=p^.returntype.def;
  955. tfuncretnode(p1).funcretprocinfo:=p;
  956. tfuncretnode(p1).rettype.def:=pd;
  957. is_func_ret:=true;
  958. if p^.funcret_state=vs_declared then
  959. begin
  960. p^.funcret_state:=vs_declared_and_first_found;
  961. include(p1.flags,nf_is_first_funcret);
  962. end;
  963. exit;
  964. end;
  965. p:=p^.parent;
  966. end;
  967. { we must use the function call }
  968. if (sym^.typ=funcretsym) then
  969. begin
  970. storesymtablestack:=symtablestack;
  971. symtablestack:=srsymtable^.next;
  972. getsym(sym^.name,true);
  973. if srsym^.typ<>procsym then
  974. Message(cg_e_illegal_expression);
  975. symtablestack:=storesymtablestack;
  976. end;
  977. end;
  978. {---------------------------------------------
  979. Factor_read_id
  980. ---------------------------------------------}
  981. procedure factor_read_id;
  982. var
  983. pc : pchar;
  984. len : longint;
  985. begin
  986. { allow post fix operators }
  987. again:=true;
  988. begin
  989. if lastsymknown then
  990. begin
  991. srsym:=lastsrsym;
  992. srsymtable:=lastsrsymtable;
  993. lastsymknown:=false;
  994. end
  995. else
  996. getsym(pattern,true);
  997. consume(_ID);
  998. if not is_func_ret(srsym) then
  999. { else it's a normal symbol }
  1000. begin
  1001. { is it defined like UNIT.SYMBOL ? }
  1002. if srsym^.typ=unitsym then
  1003. begin
  1004. consume(_POINT);
  1005. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  1006. {$ifdef TEST_PROCSYMS}
  1007. unit_specific:=true;
  1008. {$endif TEST_PROCSYMS}
  1009. consume(_ID);
  1010. {$ifdef TEST_PROCSYMS}
  1011. end
  1012. else
  1013. unit_specific:=false;
  1014. {$else TEST_PROCSYMS}
  1015. end;
  1016. {$endif TEST_PROCSYMS}
  1017. if not assigned(srsym) then
  1018. Begin
  1019. p1:=cerrornode.create;
  1020. { try to clean up }
  1021. pd:=generrordef;
  1022. end
  1023. else
  1024. Begin
  1025. { check semantics of private }
  1026. if (srsym^.typ in [propertysym,procsym,varsym]) and
  1027. (srsymtable^.symtabletype=objectsymtable) then
  1028. begin
  1029. if (sp_private in srsym^.symoptions) and
  1030. (pobjectdef(srsym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  1031. Message(parser_e_cant_access_private_member);
  1032. end;
  1033. case srsym^.typ of
  1034. absolutesym : begin
  1035. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1036. pd:=pabsolutesym(srsym)^.vartype.def;
  1037. end;
  1038. varsym : begin
  1039. { are we in a class method ? }
  1040. if (srsymtable^.symtabletype=objectsymtable) and
  1041. assigned(aktprocsym) and
  1042. (po_classmethod in aktprocsym^.definition^.procoptions) then
  1043. Message(parser_e_only_class_methods);
  1044. if (sp_static in srsym^.symoptions) then
  1045. begin
  1046. static_name:=lower(srsym^.owner^.name^)+'_'+srsym^.name;
  1047. getsym(static_name,true);
  1048. end;
  1049. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1050. if pvarsym(srsym)^.varstate=vs_declared then
  1051. begin
  1052. include(p1.flags,nf_first);
  1053. { set special between first loaded until checked in firstpass }
  1054. pvarsym(srsym)^.varstate:=vs_declared_and_first_found;
  1055. end;
  1056. pd:=pvarsym(srsym)^.vartype.def;
  1057. end;
  1058. typedconstsym : begin
  1059. p1:=gentypedconstloadnode(ptypedconstsym(srsym),srsymtable);
  1060. pd:=ptypedconstsym(srsym)^.typedconsttype.def;
  1061. end;
  1062. syssym : p1:=statement_syssym(psyssym(srsym)^.number,pd);
  1063. typesym : begin
  1064. pd:=ptypesym(srsym)^.restype.def;
  1065. if not assigned(pd) then
  1066. begin
  1067. pd:=generrordef;
  1068. again:=false;
  1069. end
  1070. else
  1071. begin
  1072. { if we read a type declaration }
  1073. { we have to return the type and }
  1074. { nothing else }
  1075. if block_type=bt_type then
  1076. begin
  1077. { we don't need sym reference when it's in the
  1078. current unit or system unit, because those
  1079. units are always loaded (PFV) }
  1080. if not(assigned(pd^.owner)) or
  1081. (pd^.owner^.unitid=0) or
  1082. (pd^.owner^.unitid=1) then
  1083. p1:=gentypenode(pd,nil)
  1084. else
  1085. p1:=gentypenode(pd,ptypesym(srsym));
  1086. { here we can also set resulttype !! }
  1087. p1.resulttype:=pd;
  1088. pd:=voiddef;
  1089. end
  1090. else { not type block }
  1091. begin
  1092. if token=_LKLAMMER then
  1093. begin
  1094. consume(_LKLAMMER);
  1095. p1:=comp_expr(true);
  1096. consume(_RKLAMMER);
  1097. p1:=gentypeconvnode(p1,pd);
  1098. include(p1.flags,nf_explizit);
  1099. end
  1100. else { not LKLAMMER}
  1101. if (token=_POINT) and
  1102. (pd^.deftype=objectdef) and
  1103. not(pobjectdef(pd)^.is_class) then
  1104. begin
  1105. consume(_POINT);
  1106. if assigned(procinfo) and
  1107. assigned(procinfo^._class) and
  1108. not(getaddr) then
  1109. begin
  1110. if procinfo^._class^.is_related(pobjectdef(pd)) then
  1111. begin
  1112. p1:=gentypenode(pd,ptypesym(srsym));
  1113. p1.resulttype:=pd;
  1114. { search also in inherited methods }
  1115. repeat
  1116. srsymtable:=pobjectdef(pd)^.symtable;
  1117. sym:=pvarsym(srsymtable^.search(pattern));
  1118. if assigned(sym) then
  1119. break;
  1120. pd:=pobjectdef(pd)^.childof;
  1121. until not assigned(pd);
  1122. consume(_ID);
  1123. do_member_read(false,sym,p1,pd,again);
  1124. end
  1125. else
  1126. begin
  1127. Message(parser_e_no_super_class);
  1128. pd:=generrordef;
  1129. again:=false;
  1130. end;
  1131. end
  1132. else
  1133. begin
  1134. { allows @TObject.Load }
  1135. { also allows static methods and variables }
  1136. p1:=ctypenode.create(nil,nil);
  1137. p1.resulttype:=pd;
  1138. { TP allows also @TMenu.Load if Load is only }
  1139. { defined in an anchestor class }
  1140. sym:=pvarsym(search_class_member(pobjectdef(pd),pattern));
  1141. if not assigned(sym) then
  1142. Message1(sym_e_id_no_member,pattern)
  1143. else if not(getaddr) and not(sp_static in sym^.symoptions) then
  1144. Message(sym_e_only_static_in_static)
  1145. else
  1146. begin
  1147. consume(_ID);
  1148. do_member_read(getaddr,sym,p1,pd,again);
  1149. end;
  1150. end;
  1151. end
  1152. else
  1153. begin
  1154. { class reference ? }
  1155. if (pd^.deftype=objectdef)
  1156. and pobjectdef(pd)^.is_class then
  1157. begin
  1158. if getaddr and (token=_POINT) then
  1159. begin
  1160. consume(_POINT);
  1161. { allows @Object.Method }
  1162. { also allows static methods and variables }
  1163. p1:=gentypenode(nil,nil);
  1164. p1.resulttype:=pd;
  1165. { TP allows also @TMenu.Load if Load is only }
  1166. { defined in an anchestor class }
  1167. sym:=pvarsym(search_class_member(pobjectdef(pd),pattern));
  1168. if not assigned(sym) then
  1169. Message1(sym_e_id_no_member,pattern)
  1170. else
  1171. begin
  1172. consume(_ID);
  1173. do_member_read(getaddr,sym,p1,pd,again);
  1174. end;
  1175. end
  1176. else
  1177. begin
  1178. p1:=gentypenode(pd,nil);
  1179. p1.resulttype:=pd;
  1180. pd:=new(pclassrefdef,init(pd));
  1181. p1:=cloadvmtnode.create(p1);
  1182. p1.resulttype:=pd;
  1183. end;
  1184. end
  1185. else
  1186. begin
  1187. { generate a type node }
  1188. { (for typeof etc) }
  1189. if allow_type then
  1190. begin
  1191. p1:=gentypenode(pd,nil);
  1192. { here we must use typenodetype explicitly !! PM
  1193. p1.resulttype:=pd; }
  1194. pd:=voiddef;
  1195. end
  1196. else
  1197. Message(parser_e_no_type_not_allowed_here);
  1198. end;
  1199. end;
  1200. end;
  1201. end;
  1202. end;
  1203. enumsym : begin
  1204. p1:=genenumnode(penumsym(srsym));
  1205. pd:=p1.resulttype;
  1206. end;
  1207. constsym : begin
  1208. case pconstsym(srsym)^.consttyp of
  1209. constint :
  1210. { do a very dirty trick to bootstrap this code }
  1211. if (pconstsym(srsym)^.value>=-(int64(2147483647)+int64(1))) and (pconstsym(srsym)^.value<=2147483647) then
  1212. p1:=genordinalconstnode(pconstsym(srsym)^.value,s32bitdef)
  1213. else
  1214. p1:=genordinalconstnode(pconstsym(srsym)^.value,cs64bitdef);
  1215. conststring :
  1216. begin
  1217. len:=pconstsym(srsym)^.len;
  1218. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1219. len:=255;
  1220. getmem(pc,len+1);
  1221. move(pchar(tpointerord(pconstsym(srsym)^.value))^,pc^,len);
  1222. pc[len]:=#0;
  1223. p1:=genpcharconstnode(pc,len);
  1224. end;
  1225. constchar :
  1226. p1:=genordinalconstnode(pconstsym(srsym)^.value,cchardef);
  1227. constreal :
  1228. p1:=genrealconstnode(pbestreal(tpointerord(pconstsym(srsym)^.value))^,bestrealdef^);
  1229. constbool :
  1230. p1:=genordinalconstnode(pconstsym(srsym)^.value,booldef);
  1231. constset :
  1232. p1:=gensetconstnode(pconstset(tpointerord(pconstsym(srsym)^.value)),
  1233. psetdef(pconstsym(srsym)^.consttype.def));
  1234. constord :
  1235. p1:=genordinalconstnode(pconstsym(srsym)^.value,
  1236. pconstsym(srsym)^.consttype.def);
  1237. constpointer :
  1238. p1:=genpointerconstnode(pconstsym(srsym)^.value,
  1239. pconstsym(srsym)^.consttype.def);
  1240. constnil :
  1241. p1:=cnilnode.create;
  1242. constresourcestring:
  1243. begin
  1244. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1245. p1.resulttype:=cansistringdef;
  1246. end;
  1247. end;
  1248. pd:=p1.resulttype;
  1249. end;
  1250. procsym : begin
  1251. { are we in a class method ? }
  1252. possible_error:=(srsymtable^.symtabletype=objectsymtable) and
  1253. assigned(aktprocsym) and
  1254. (po_classmethod in aktprocsym^.definition^.procoptions);
  1255. p1:=gencallnode(pprocsym(srsym),srsymtable);
  1256. {$ifdef TEST_PROCSYMS}
  1257. p1.unit_specific:=unit_specific;
  1258. {$endif TEST_PROCSYMS}
  1259. do_proc_call(getaddr or
  1260. (getprocvar and
  1261. ((block_type=bt_const) or
  1262. ((m_tp_procvar in aktmodeswitches) and
  1263. proc_to_procvar_equal(pprocsym(srsym)^.definition,getprocvardef)
  1264. )
  1265. )
  1266. ),again,tcallnode(p1),pd);
  1267. if (block_type=bt_const) and
  1268. getprocvar then
  1269. handle_procvar(getprocvardef,p1);
  1270. if possible_error and
  1271. not(po_classmethod in tcallnode(p1).procdefinition^.procoptions) then
  1272. Message(parser_e_only_class_methods);
  1273. end;
  1274. propertysym : begin
  1275. { access to property in a method }
  1276. { are we in a class method ? }
  1277. if (srsymtable^.symtabletype=objectsymtable) and
  1278. assigned(aktprocsym) and
  1279. (po_classmethod in aktprocsym^.definition^.procoptions) then
  1280. Message(parser_e_only_class_methods);
  1281. { no method pointer }
  1282. p1:=nil;
  1283. handle_propertysym(srsym,srsymtable,p1,pd);
  1284. end;
  1285. errorsym : begin
  1286. p1:=cerrornode.create;
  1287. p1.resulttype:=generrordef;
  1288. pd:=generrordef;
  1289. if token=_LKLAMMER then
  1290. begin
  1291. consume(_LKLAMMER);
  1292. parse_paras(false,false);
  1293. consume(_RKLAMMER);
  1294. end;
  1295. end;
  1296. else
  1297. begin
  1298. p1:=cerrornode.create;
  1299. pd:=generrordef;
  1300. Message(cg_e_illegal_expression);
  1301. end;
  1302. end; { end case }
  1303. end;
  1304. end;
  1305. end;
  1306. end;
  1307. {---------------------------------------------
  1308. Factor_Read_Set
  1309. ---------------------------------------------}
  1310. { Read a set between [] }
  1311. function factor_read_set:tnode;
  1312. var
  1313. p1 : tnode;
  1314. lastp,
  1315. buildp : tarrayconstructornode;
  1316. begin
  1317. buildp:=nil;
  1318. { be sure that a least one arrayconstructn is used, also for an
  1319. empty [] }
  1320. if token=_RECKKLAMMER then
  1321. buildp:=carrayconstructornode.create(nil,buildp)
  1322. else
  1323. begin
  1324. while true do
  1325. begin
  1326. p1:=comp_expr(true);
  1327. if token=_POINTPOINT then
  1328. begin
  1329. consume(_POINTPOINT);
  1330. p2:=comp_expr(true);
  1331. p1:=carrayconstructorrangenode.create(p1,p2);
  1332. end;
  1333. { insert at the end of the tree, to get the correct order }
  1334. if not assigned(buildp) then
  1335. begin
  1336. buildp:=carrayconstructornode.create(p1,nil);
  1337. lastp:=buildp;
  1338. end
  1339. else
  1340. begin
  1341. lastp.right:=carrayconstructornode.create(p1,nil);
  1342. lastp:=tarrayconstructornode(lastp.right);
  1343. end;
  1344. { there could be more elements }
  1345. if token=_COMMA then
  1346. consume(_COMMA)
  1347. else
  1348. break;
  1349. end;
  1350. end;
  1351. factor_read_set:=buildp;
  1352. end;
  1353. {---------------------------------------------
  1354. Helpers
  1355. ---------------------------------------------}
  1356. procedure check_tokenpos;
  1357. begin
  1358. if (p1<>oldp1) then
  1359. begin
  1360. if assigned(p1) then
  1361. p1.set_tree_filepos(filepos);
  1362. oldp1:=p1;
  1363. filepos:=tokenpos;
  1364. end;
  1365. end;
  1366. {---------------------------------------------
  1367. PostFixOperators
  1368. ---------------------------------------------}
  1369. procedure postfixoperators;
  1370. { tries to avoid syntax errors after invalid qualifiers }
  1371. procedure recoverconsume_postfixops;
  1372. begin
  1373. while true do
  1374. begin
  1375. case token of
  1376. _CARET:
  1377. consume(_CARET);
  1378. _POINT:
  1379. begin
  1380. consume(_POINT);
  1381. if token=_ID then
  1382. consume(_ID);
  1383. end;
  1384. _LECKKLAMMER:
  1385. begin
  1386. consume(_LECKKLAMMER);
  1387. repeat
  1388. comp_expr(true);
  1389. if token=_COMMA then
  1390. consume(_COMMA)
  1391. else
  1392. break;
  1393. until false;
  1394. consume(_RECKKLAMMER);
  1395. end
  1396. else
  1397. break;
  1398. end;
  1399. end;
  1400. end;
  1401. var
  1402. store_static : boolean;
  1403. { p1 and p2 must contain valid value_str }
  1404. begin
  1405. check_tokenpos;
  1406. while again do
  1407. begin
  1408. { prevent crashes with unknown types }
  1409. if not assigned(pd) then
  1410. begin
  1411. recoverconsume_postfixops;
  1412. exit;
  1413. end;
  1414. { handle token }
  1415. case token of
  1416. _CARET:
  1417. begin
  1418. consume(_CARET);
  1419. if (pd^.deftype<>pointerdef) then
  1420. begin
  1421. { ^ as binary operator is a problem!!!! (FK) }
  1422. again:=false;
  1423. Message(cg_e_invalid_qualifier);
  1424. recoverconsume_postfixops;
  1425. p1.destroy;
  1426. p1:=cerrornode.create;
  1427. end
  1428. else
  1429. begin
  1430. p1:=cderefnode.create(p1);
  1431. pd:=ppointerdef(pd)^.pointertype.def;
  1432. end;
  1433. end;
  1434. _LECKKLAMMER:
  1435. begin
  1436. if (pd^.deftype=objectdef) and pobjectdef(pd)^.is_class then
  1437. begin
  1438. { default property }
  1439. propsym:=search_default_property(pobjectdef(pd));
  1440. if not(assigned(propsym)) then
  1441. begin
  1442. p1.destroy;
  1443. p1:=cerrornode.create;
  1444. again:=false;
  1445. message(parser_e_no_default_property_available);
  1446. end
  1447. else
  1448. handle_propertysym(propsym,propsym^.owner,p1,pd);
  1449. end
  1450. else
  1451. begin
  1452. consume(_LECKKLAMMER);
  1453. repeat
  1454. case pd^.deftype of
  1455. pointerdef:
  1456. begin
  1457. pd:=ppointerdef(pd)^.pointertype.def;
  1458. { support delphi autoderef }
  1459. if (pd^.deftype=arraydef) and
  1460. (m_autoderef in aktmodeswitches) then
  1461. begin
  1462. p1:=cderefnode.create(p1);
  1463. pd:=parraydef(pd)^.elementtype.def;
  1464. end;
  1465. p2:=comp_expr(true);
  1466. p1:=cvecnode.create(p1,p2);
  1467. end;
  1468. stringdef : begin
  1469. p2:=comp_expr(true);
  1470. p1:=cvecnode.create(p1,p2);
  1471. pd:=cchardef
  1472. end;
  1473. arraydef : begin
  1474. p2:=comp_expr(true);
  1475. { support SEG:OFS for go32v2 Mem[] }
  1476. if (target_info.target=target_i386_go32v2) and
  1477. (p1.nodetype=loadn) and
  1478. assigned(tloadnode(p1).symtableentry) and
  1479. assigned(tloadnode(p1).symtableentry^.owner^.name) and
  1480. (tloadnode(p1).symtableentry^.owner^.name^='SYSTEM') and
  1481. ((tloadnode(p1).symtableentry^.name='MEM') or
  1482. (tloadnode(p1).symtableentry^.name='MEMW') or
  1483. (tloadnode(p1).symtableentry^.name='MEML')) then
  1484. begin
  1485. if (token=_COLON) then
  1486. begin
  1487. consume(_COLON);
  1488. p3:=caddnode.create(muln,genordinalconstnode($10,s32bitdef),p2);
  1489. p2:=comp_expr(true);
  1490. p2:=caddnode.create(addn,p2,p3);
  1491. p1:=cvecnode.create(p1,p2);
  1492. include(tvecnode(p1).flags,nf_memseg);
  1493. include(tvecnode(p1).flags,nf_memindex);
  1494. end
  1495. else
  1496. begin
  1497. p1:=cvecnode.create(p1,p2);
  1498. include(tvecnode(p1).flags,nf_memindex);
  1499. end;
  1500. end
  1501. else
  1502. p1:=cvecnode.create(p1,p2);
  1503. pd:=parraydef(pd)^.elementtype.def;
  1504. end;
  1505. else
  1506. begin
  1507. Message(cg_e_invalid_qualifier);
  1508. p1.destroy;
  1509. p1:=cerrornode.create;
  1510. comp_expr(true);
  1511. again:=false;
  1512. end;
  1513. end;
  1514. if token=_COMMA then
  1515. consume(_COMMA)
  1516. else
  1517. break;
  1518. until false;
  1519. consume(_RECKKLAMMER);
  1520. end;
  1521. end;
  1522. _POINT : begin
  1523. consume(_POINT);
  1524. if (pd^.deftype=pointerdef) and
  1525. (m_autoderef in aktmodeswitches) then
  1526. begin
  1527. p1:=cderefnode.create(p1);
  1528. pd:=ppointerdef(pd)^.pointertype.def;
  1529. end;
  1530. case pd^.deftype of
  1531. recorddef:
  1532. begin
  1533. sym:=precorddef(pd)^.symtable^.search(pattern);
  1534. if assigned(sym) and
  1535. (sym^.typ=varsym) then
  1536. begin
  1537. p1:=gensubscriptnode(pvarsym(sym),p1);
  1538. pd:=pvarsym(sym)^.vartype.def;
  1539. end
  1540. else
  1541. begin
  1542. Message1(sym_e_illegal_field,pattern);
  1543. p1.destroy;
  1544. p1:=cerrornode.create;
  1545. end;
  1546. consume(_ID);
  1547. end;
  1548. classrefdef:
  1549. begin
  1550. classh:=pobjectdef(pclassrefdef(pd)^.pointertype.def);
  1551. sym:=nil;
  1552. while assigned(classh) do
  1553. begin
  1554. sym:=classh^.symtable^.search(pattern);
  1555. srsymtable:=classh^.symtable;
  1556. if assigned(sym) then
  1557. break;
  1558. classh:=classh^.childof;
  1559. end;
  1560. if sym=nil then
  1561. begin
  1562. Message1(sym_e_id_no_member,pattern);
  1563. p1.destroy;
  1564. p1:=cerrornode.create;
  1565. { try to clean up }
  1566. pd:=generrordef;
  1567. consume(_ID);
  1568. end
  1569. else
  1570. begin
  1571. consume(_ID);
  1572. do_member_read(getaddr,sym,p1,pd,again);
  1573. end;
  1574. end;
  1575. objectdef:
  1576. begin
  1577. classh:=pobjectdef(pd);
  1578. sym:=nil;
  1579. store_static:=allow_only_static;
  1580. allow_only_static:=false;
  1581. while assigned(classh) do
  1582. begin
  1583. sym:=classh^.symtable^.search(pattern);
  1584. srsymtable:=classh^.symtable;
  1585. if assigned(sym) then
  1586. break;
  1587. classh:=classh^.childof;
  1588. end;
  1589. allow_only_static:=store_static;
  1590. if sym=nil then
  1591. begin
  1592. Message1(sym_e_id_no_member,pattern);
  1593. p1.destroy;
  1594. p1:=cerrornode.create;
  1595. { try to clean up }
  1596. pd:=generrordef;
  1597. consume(_ID);
  1598. end
  1599. else
  1600. begin
  1601. consume(_ID);
  1602. do_member_read(getaddr,sym,p1,pd,again);
  1603. end;
  1604. end;
  1605. pointerdef:
  1606. begin
  1607. Message(cg_e_invalid_qualifier);
  1608. if ppointerdef(pd)^.pointertype.def^.deftype in [recorddef,objectdef,classrefdef] then
  1609. Message(parser_h_maybe_deref_caret_missing);
  1610. end;
  1611. else
  1612. begin
  1613. Message(cg_e_invalid_qualifier);
  1614. p1.destroy;
  1615. p1:=cerrornode.create;
  1616. consume(_ID);
  1617. end;
  1618. end;
  1619. end;
  1620. else
  1621. begin
  1622. { is this a procedure variable ? }
  1623. if assigned(pd) then
  1624. begin
  1625. if (pd^.deftype=procvardef) then
  1626. begin
  1627. if getprocvar and is_equal(pd,getprocvardef) then
  1628. again:=false
  1629. else
  1630. if (token=_LKLAMMER) or
  1631. ((pprocvardef(pd)^.para^.empty) and
  1632. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1633. (not afterassignment) and
  1634. (not in_args)) then
  1635. begin
  1636. { do this in a strange way }
  1637. { it's not a clean solution }
  1638. p2:=p1;
  1639. p1:=gencallnode(nil,nil);
  1640. tcallnode(p1).right:=p2;
  1641. {$ifdef TEST_PROCSYMS}
  1642. p1.unit_specific:=unit_specific;
  1643. {$endif TEST_PROCSYMS}
  1644. tcallnode(p1).symtableprocentry:=pprocsym(sym);
  1645. if token=_LKLAMMER then
  1646. begin
  1647. consume(_LKLAMMER);
  1648. tcallnode(p1).left:=parse_paras(false,false);
  1649. consume(_RKLAMMER);
  1650. end;
  1651. pd:=pprocvardef(pd)^.rettype.def;
  1652. { proc():= is never possible }
  1653. if token=_ASSIGNMENT then
  1654. begin
  1655. Message(cg_e_illegal_expression);
  1656. p1:=cerrornode.create;
  1657. again:=false;
  1658. end;
  1659. p1.resulttype:=pd;
  1660. end
  1661. else
  1662. again:=false;
  1663. p1.resulttype:=pd;
  1664. end
  1665. else
  1666. again:=false;
  1667. end
  1668. else
  1669. again:=false;
  1670. end;
  1671. end;
  1672. check_tokenpos;
  1673. end; { while again }
  1674. end;
  1675. {---------------------------------------------
  1676. Factor (Main)
  1677. ---------------------------------------------}
  1678. begin
  1679. oldp1:=nil;
  1680. p1:=nil;
  1681. filepos:=tokenpos;
  1682. if token=_ID then
  1683. begin
  1684. factor_read_id;
  1685. { handle post fix operators }
  1686. postfixoperators;
  1687. end
  1688. else
  1689. case token of
  1690. _NEW : begin
  1691. consume(_NEW);
  1692. consume(_LKLAMMER);
  1693. {allow_type:=true;}
  1694. p1:=factor(false);
  1695. {allow_type:=false;}
  1696. if p1.nodetype<>typen then
  1697. begin
  1698. Message(type_e_type_id_expected);
  1699. p1.destroy;
  1700. pd:=generrordef;
  1701. end
  1702. else
  1703. pd:=ttypenode(p1).typenodetype;
  1704. pd2:=pd;
  1705. if (pd^.deftype<>pointerdef) then
  1706. Message1(type_e_pointer_type_expected,pd^.typename)
  1707. else
  1708. if token=_RKLAMMER then
  1709. begin
  1710. if (ppointerdef(pd)^.pointertype.def^.deftype=objectdef) and
  1711. (oo_has_vmt in pobjectdef(ppointerdef(pd)^.pointertype.def)^.objectoptions) then
  1712. Message(parser_w_use_extended_syntax_for_objects);
  1713. p1:=cnewnode.create(nil);
  1714. p1.resulttype:=pd2;
  1715. consume(_RKLAMMER);
  1716. end
  1717. else
  1718. begin
  1719. p1.destroy;
  1720. p1:=chnewnode.create;
  1721. p1.resulttype:=ppointerdef(pd)^.pointertype.def;
  1722. consume(_COMMA);
  1723. afterassignment:=false;
  1724. { determines the current object defintion }
  1725. classh:=pobjectdef(ppointerdef(pd)^.pointertype.def);
  1726. if classh^.deftype<>objectdef then
  1727. Message(parser_e_pointer_to_class_expected)
  1728. else
  1729. begin
  1730. { check for an abstract class }
  1731. if (oo_has_abstract in classh^.objectoptions) then
  1732. Message(sym_e_no_instance_of_abstract_object);
  1733. { search the constructor also in the symbol tables of
  1734. the parents }
  1735. sym:=nil;
  1736. while assigned(classh) do
  1737. begin
  1738. sym:=classh^.symtable^.search(pattern);
  1739. srsymtable:=classh^.symtable;
  1740. if assigned(sym) then
  1741. break;
  1742. classh:=classh^.childof;
  1743. end;
  1744. consume(_ID);
  1745. do_member_read(false,sym,p1,pd,again);
  1746. if (p1.nodetype<>calln) or
  1747. (assigned(tcallnode(p1).procdefinition) and
  1748. (tcallnode(p1).procdefinition^.proctypeoption<>potype_constructor)) then
  1749. Message(parser_e_expr_have_to_be_constructor_call);
  1750. end;
  1751. p1:=cnewnode.create(p1);
  1752. { set the resulttype }
  1753. p1.resulttype:=pd2;
  1754. consume(_RKLAMMER);
  1755. end;
  1756. postfixoperators;
  1757. end;
  1758. _SELF : begin
  1759. again:=true;
  1760. consume(_SELF);
  1761. if not assigned(procinfo^._class) then
  1762. begin
  1763. p1:=cerrornode.create;
  1764. pd:=generrordef;
  1765. again:=false;
  1766. Message(parser_e_self_not_in_method);
  1767. end
  1768. else
  1769. begin
  1770. if (po_classmethod in aktprocsym^.definition^.procoptions) then
  1771. begin
  1772. { self in class methods is a class reference type }
  1773. pd:=new(pclassrefdef,init(procinfo^._class));
  1774. p1:=genselfnode(pd);
  1775. p1.resulttype:=pd;
  1776. end
  1777. else
  1778. begin
  1779. p1:=genselfnode(procinfo^._class);
  1780. p1.resulttype:=procinfo^._class;
  1781. end;
  1782. pd:=p1.resulttype;
  1783. postfixoperators;
  1784. end;
  1785. end;
  1786. _INHERITED : begin
  1787. again:=true;
  1788. consume(_INHERITED);
  1789. if assigned(procinfo^._class) then
  1790. begin
  1791. { if inherited; only then we need the method with
  1792. the same name }
  1793. if token=_SEMICOLON then
  1794. begin
  1795. hs:=aktprocsym^.name;
  1796. auto_inherited:=true
  1797. end
  1798. else
  1799. begin
  1800. hs:=pattern;
  1801. consume(_ID);
  1802. auto_inherited:=false;
  1803. end;
  1804. classh:=procinfo^._class^.childof;
  1805. while assigned(classh) do
  1806. begin
  1807. srsymtable:=pobjectdef(classh)^.symtable;
  1808. sym:=srsymtable^.search(hs);
  1809. if assigned(sym) then
  1810. begin
  1811. { only for procsyms we need to set the type (PFV) }
  1812. case sym^.typ of
  1813. procsym :
  1814. begin
  1815. p1:=ctypenode.create(nil,nil);
  1816. p1.resulttype:=classh;
  1817. pd:=p1.resulttype;
  1818. end;
  1819. varsym :
  1820. begin
  1821. p1:=nil;
  1822. pd:=pvarsym(sym)^.vartype.def;
  1823. end;
  1824. propertysym :
  1825. begin
  1826. p1:=nil;
  1827. pd:=ppropertysym(sym)^.proptype.def;
  1828. end;
  1829. else
  1830. internalerror(83251763);
  1831. end;
  1832. do_member_read(false,sym,p1,pd,again);
  1833. break;
  1834. end;
  1835. classh:=classh^.childof;
  1836. end;
  1837. if classh=nil then
  1838. begin
  1839. Message1(sym_e_id_no_member,hs);
  1840. again:=false;
  1841. pd:=generrordef;
  1842. p1:=cerrornode.create;
  1843. end;
  1844. { turn auto inheriting off }
  1845. auto_inherited:=false;
  1846. end
  1847. else
  1848. begin
  1849. Message(parser_e_generic_methods_only_in_methods);
  1850. again:=false;
  1851. pd:=generrordef;
  1852. p1:=cerrornode.create;
  1853. end;
  1854. postfixoperators;
  1855. end;
  1856. _INTCONST : begin
  1857. valint(pattern,l,code);
  1858. if code<>0 then
  1859. begin
  1860. { try int64 if available }
  1861. { if no int64 available longint is tried a second }
  1862. { time which doesn't hurt }
  1863. val(pattern,ic,code);
  1864. if code<>0 then
  1865. begin
  1866. val(pattern,d,code);
  1867. if code<>0 then
  1868. begin
  1869. Message(cg_e_invalid_integer);
  1870. consume(_INTCONST);
  1871. l:=1;
  1872. p1:=genordinalconstnode(l,s32bitdef);
  1873. end
  1874. else
  1875. begin
  1876. consume(_INTCONST);
  1877. p1:=genrealconstnode(d,bestrealdef^);
  1878. end;
  1879. end
  1880. else
  1881. begin
  1882. consume(_INTCONST);
  1883. p1:=genordinalconstnode(ic,cs64bitdef);
  1884. end;
  1885. end
  1886. else
  1887. begin
  1888. consume(_INTCONST);
  1889. p1:=genordinalconstnode(l,s32bitdef)
  1890. end;
  1891. end;
  1892. _REALNUMBER : begin
  1893. val(pattern,d,code);
  1894. if code<>0 then
  1895. begin
  1896. Message(parser_e_error_in_real);
  1897. d:=1.0;
  1898. end;
  1899. consume(_REALNUMBER);
  1900. p1:=genrealconstnode(d,bestrealdef^);
  1901. end;
  1902. _STRING : begin
  1903. pd:=string_dec;
  1904. { STRING can be also a type cast }
  1905. if token=_LKLAMMER then
  1906. begin
  1907. consume(_LKLAMMER);
  1908. p1:=comp_expr(true);
  1909. consume(_RKLAMMER);
  1910. p1:=gentypeconvnode(p1,pd);
  1911. include(p1.flags,nf_explizit);
  1912. { handle postfix operators here e.g. string(a)[10] }
  1913. again:=true;
  1914. postfixoperators;
  1915. end
  1916. else
  1917. p1:=gentypenode(pd,nil);
  1918. end;
  1919. _FILE : begin
  1920. pd:=cfiledef;
  1921. consume(_FILE);
  1922. { FILE can be also a type cast }
  1923. if token=_LKLAMMER then
  1924. begin
  1925. consume(_LKLAMMER);
  1926. p1:=comp_expr(true);
  1927. consume(_RKLAMMER);
  1928. p1:=gentypeconvnode(p1,pd);
  1929. include(p1.flags,nf_explizit);
  1930. { handle postfix operators here e.g. string(a)[10] }
  1931. again:=true;
  1932. postfixoperators;
  1933. end
  1934. else
  1935. p1:=gentypenode(pd,nil);
  1936. end;
  1937. _CSTRING : begin
  1938. p1:=genstringconstnode(pattern,st_default);
  1939. consume(_CSTRING);
  1940. end;
  1941. _CCHAR : begin
  1942. p1:=genordinalconstnode(ord(pattern[1]),cchardef);
  1943. consume(_CCHAR);
  1944. end;
  1945. _KLAMMERAFFE : begin
  1946. consume(_KLAMMERAFFE);
  1947. got_addrn:=true;
  1948. { support both @<x> and @(<x>) }
  1949. if token=_LKLAMMER then
  1950. begin
  1951. consume(_LKLAMMER);
  1952. p1:=factor(true);
  1953. consume(_RKLAMMER);
  1954. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1955. begin
  1956. { we need the resulttype }
  1957. { of the expression in pd }
  1958. do_firstpass(p1);
  1959. pd:=p1.resulttype;
  1960. again:=true;
  1961. postfixoperators;
  1962. end;
  1963. end
  1964. else
  1965. p1:=factor(true);
  1966. got_addrn:=false;
  1967. p1:=caddrnode.create(p1);
  1968. end;
  1969. _LKLAMMER : begin
  1970. consume(_LKLAMMER);
  1971. p1:=comp_expr(true);
  1972. consume(_RKLAMMER);
  1973. { it's not a good solution }
  1974. { but (a+b)^ makes some problems }
  1975. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1976. begin
  1977. { we need the resulttype }
  1978. { of the expression in pd }
  1979. do_firstpass(p1);
  1980. pd:=p1.resulttype;
  1981. again:=true;
  1982. postfixoperators;
  1983. end;
  1984. end;
  1985. _LECKKLAMMER : begin
  1986. consume(_LECKKLAMMER);
  1987. p1:=factor_read_set;
  1988. consume(_RECKKLAMMER);
  1989. end;
  1990. _PLUS : begin
  1991. consume(_PLUS);
  1992. p1:=factor(false);
  1993. end;
  1994. _MINUS : begin
  1995. consume(_MINUS);
  1996. p1:=sub_expr(oppower,false);
  1997. p1:=cunaryminusnode.create(p1);
  1998. end;
  1999. _OP_NOT : begin
  2000. consume(_OP_NOT);
  2001. p1:=factor(false);
  2002. p1:=cnotnode.create(p1);
  2003. end;
  2004. _TRUE : begin
  2005. consume(_TRUE);
  2006. p1:=genordinalconstnode(1,booldef);
  2007. end;
  2008. _FALSE : begin
  2009. consume(_FALSE);
  2010. p1:=genordinalconstnode(0,booldef);
  2011. end;
  2012. _NIL : begin
  2013. consume(_NIL);
  2014. p1:=cnilnode.create;
  2015. end;
  2016. else
  2017. begin
  2018. p1:=cerrornode.create;
  2019. consume(token);
  2020. Message(cg_e_illegal_expression);
  2021. end;
  2022. end;
  2023. { generate error node if no node is created }
  2024. if not assigned(p1) then
  2025. p1:=cerrornode.create;
  2026. { tp7 procvar handling, but not if the next token
  2027. will be a := }
  2028. if (m_tp_procvar in aktmodeswitches) and
  2029. (token<>_ASSIGNMENT) then
  2030. check_tp_procvar(p1);
  2031. factor:=p1;
  2032. check_tokenpos;
  2033. end;
  2034. {$ifdef fpc}
  2035. {$maxfpuregisters default}
  2036. {$endif fpc}
  2037. {****************************************************************************
  2038. Sub_Expr
  2039. ****************************************************************************}
  2040. const
  2041. { Warning these stay be ordered !! }
  2042. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2043. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN,_OP_IS],
  2044. [_PLUS,_MINUS,_OP_OR,_OP_XOR],
  2045. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2046. _OP_AS,_OP_AND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2047. [_STARSTAR] );
  2048. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2049. {Reads a subexpression while the operators are of the current precedence
  2050. level, or any higher level. Replaces the old term, simpl_expr and
  2051. simpl2_expr.}
  2052. var
  2053. p1,p2 : tnode;
  2054. oldt : Ttoken;
  2055. filepos : tfileposinfo;
  2056. begin
  2057. if pred_level=highest_precedence then
  2058. p1:=factor(false)
  2059. else
  2060. p1:=sub_expr(succ(pred_level),true);
  2061. repeat
  2062. if (token in operator_levels[pred_level]) and
  2063. ((token<>_EQUAL) or accept_equal) then
  2064. begin
  2065. oldt:=token;
  2066. filepos:=tokenpos;
  2067. consume(token);
  2068. if pred_level=highest_precedence then
  2069. p2:=factor(false)
  2070. else
  2071. p2:=sub_expr(succ(pred_level),true);
  2072. case oldt of
  2073. _PLUS :
  2074. p1:=caddnode.create(addn,p1,p2);
  2075. _MINUS :
  2076. p1:=caddnode.create(subn,p1,p2);
  2077. _STAR :
  2078. p1:=caddnode.create(muln,p1,p2);
  2079. _SLASH :
  2080. p1:=caddnode.create(slashn,p1,p2);
  2081. _EQUAL :
  2082. p1:=caddnode.create(equaln,p1,p2);
  2083. _GT :
  2084. p1:=caddnode.create(gtn,p1,p2);
  2085. _LT :
  2086. p1:=caddnode.create(ltn,p1,p2);
  2087. _GTE :
  2088. p1:=caddnode.create(gten,p1,p2);
  2089. _LTE :
  2090. p1:=caddnode.create(lten,p1,p2);
  2091. _SYMDIF :
  2092. p1:=caddnode.create(symdifn,p1,p2);
  2093. _STARSTAR :
  2094. p1:=caddnode.create(starstarn,p1,p2);
  2095. _OP_AS :
  2096. p1:=casnode.create(p1,p2);
  2097. _OP_IN :
  2098. p1:=cinnode.create(p1,p2);
  2099. _OP_IS :
  2100. p1:=cisnode.create(p1,p2);
  2101. _OP_OR :
  2102. p1:=caddnode.create(orn,p1,p2);
  2103. _OP_AND :
  2104. p1:=caddnode.create(andn,p1,p2);
  2105. _OP_DIV :
  2106. p1:=cmoddivnode.create(divn,p1,p2);
  2107. _OP_NOT :
  2108. p1:=cnotnode.create(p1);
  2109. _OP_MOD :
  2110. p1:=cmoddivnode.create(modn,p1,p2);
  2111. _OP_SHL :
  2112. p1:=cshlshrnode.create(shln,p1,p2);
  2113. _OP_SHR :
  2114. p1:=cshlshrnode.create(shrn,p1,p2);
  2115. _OP_XOR :
  2116. p1:=caddnode.create(xorn,p1,p2);
  2117. _ASSIGNMENT :
  2118. p1:=cassignmentnode.create(p1,p2);
  2119. _CARET :
  2120. p1:=caddnode.create(caretn,p1,p2);
  2121. _UNEQUAL :
  2122. p1:=caddnode.create(unequaln,p1,p2);
  2123. end;
  2124. p1.set_tree_filepos(filepos);
  2125. end
  2126. else
  2127. break;
  2128. until false;
  2129. sub_expr:=p1;
  2130. end;
  2131. function comp_expr(accept_equal : boolean):tnode;
  2132. var
  2133. oldafterassignment : boolean;
  2134. p1 : tnode;
  2135. begin
  2136. oldafterassignment:=afterassignment;
  2137. afterassignment:=true;
  2138. p1:=sub_expr(opcompare,accept_equal);
  2139. afterassignment:=oldafterassignment;
  2140. comp_expr:=p1;
  2141. end;
  2142. function expr : tnode;
  2143. var
  2144. p1,p2 : tnode;
  2145. oldafterassignment : boolean;
  2146. oldp1 : tnode;
  2147. filepos : tfileposinfo;
  2148. begin
  2149. oldafterassignment:=afterassignment;
  2150. p1:=sub_expr(opcompare,true);
  2151. filepos:=tokenpos;
  2152. if (m_tp_procvar in aktmodeswitches) and
  2153. (token<>_ASSIGNMENT) then
  2154. check_tp_procvar(p1);
  2155. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2156. afterassignment:=true;
  2157. oldp1:=p1;
  2158. case token of
  2159. _POINTPOINT :
  2160. begin
  2161. consume(_POINTPOINT);
  2162. p2:=sub_expr(opcompare,true);
  2163. p1:=crangenode.create(p1,p2);
  2164. end;
  2165. _ASSIGNMENT :
  2166. begin
  2167. consume(_ASSIGNMENT);
  2168. if (p1.resulttype<>nil) and (p1.resulttype^.deftype=procvardef) then
  2169. begin
  2170. getprocvar:=true;
  2171. getprocvardef:=pprocvardef(p1.resulttype);
  2172. end;
  2173. p2:=sub_expr(opcompare,true);
  2174. if getprocvar then
  2175. handle_procvar(getprocvardef,p2);
  2176. getprocvar:=false;
  2177. p1:=cassignmentnode.create(p1,p2);
  2178. end;
  2179. _PLUSASN :
  2180. begin
  2181. consume(_PLUSASN);
  2182. p2:=sub_expr(opcompare,true);
  2183. p1:=cassignmentnode.create(p1,caddnode.create(addn,p1.getcopy,p2));
  2184. end;
  2185. _MINUSASN :
  2186. begin
  2187. consume(_MINUSASN);
  2188. p2:=sub_expr(opcompare,true);
  2189. p1:=cassignmentnode.create(p1,caddnode.create(subn,p1.getcopy,p2));
  2190. end;
  2191. _STARASN :
  2192. begin
  2193. consume(_STARASN );
  2194. p2:=sub_expr(opcompare,true);
  2195. p1:=cassignmentnode.create(p1,caddnode.create(muln,p1.getcopy,p2));
  2196. end;
  2197. _SLASHASN :
  2198. begin
  2199. consume(_SLASHASN );
  2200. p2:=sub_expr(opcompare,true);
  2201. p1:=cassignmentnode.create(p1,caddnode.create(slashn,p1.getcopy,p2));
  2202. end;
  2203. end;
  2204. afterassignment:=oldafterassignment;
  2205. if p1<>oldp1 then
  2206. p1.set_tree_filepos(filepos);
  2207. expr:=p1;
  2208. end;
  2209. function get_intconst:longint;
  2210. {Reads an expression, tries to evalute it and check if it is an integer
  2211. constant. Then the constant is returned.}
  2212. var
  2213. p:tnode;
  2214. begin
  2215. p:=comp_expr(true);
  2216. do_firstpass(p);
  2217. if not codegenerror then
  2218. begin
  2219. if (p.nodetype<>ordconstn) and
  2220. (p.resulttype^.deftype=orddef) and
  2221. not(Porddef(p.resulttype)^.typ in [uvoid,uchar,bool8bit,bool16bit,bool32bit]) then
  2222. Message(cg_e_illegal_expression)
  2223. else
  2224. get_intconst:=tordconstnode(p).value;
  2225. end;
  2226. p.free;
  2227. end;
  2228. function get_stringconst:string;
  2229. {Reads an expression, tries to evaluate it and checks if it is a string
  2230. constant. Then the constant is returned.}
  2231. var
  2232. p:tnode;
  2233. begin
  2234. get_stringconst:='';
  2235. p:=comp_expr(true);
  2236. do_firstpass(p);
  2237. if p.nodetype<>stringconstn then
  2238. begin
  2239. if (p.nodetype=ordconstn) and is_char(p.resulttype) then
  2240. get_stringconst:=char(tordconstnode(p).value)
  2241. else
  2242. Message(cg_e_illegal_expression);
  2243. end
  2244. else
  2245. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2246. p.free;
  2247. end;
  2248. end.
  2249. {
  2250. $Log$
  2251. Revision 1.11 2000-10-14 10:14:51 peter
  2252. * moehrendorf oct 2000 rewrite
  2253. Revision 1.10 2000/10/01 19:48:25 peter
  2254. * lot of compile updates for cg11
  2255. Revision 1.9 2000/09/24 21:19:50 peter
  2256. * delphi compile fixes
  2257. Revision 1.8 2000/09/24 15:06:22 peter
  2258. * use defines.inc
  2259. Revision 1.7 2000/08/27 16:11:51 peter
  2260. * moved some util functions from globals,cobjects to cutils
  2261. * splitted files into finput,fmodule
  2262. Revision 1.6 2000/08/20 15:12:49 peter
  2263. * auto derefence mode for array pointer (merged)
  2264. Revision 1.5 2000/08/16 18:33:53 peter
  2265. * splitted namedobjectitem.next into indexnext and listnext so it
  2266. can be used in both lists
  2267. * don't allow "word = word" type definitions (merged)
  2268. Revision 1.4 2000/08/16 13:06:06 florian
  2269. + support of 64 bit integer constants
  2270. Revision 1.3 2000/08/04 22:00:52 peter
  2271. * merges from fixes
  2272. Revision 1.2 2000/07/13 11:32:44 michael
  2273. + removed logs
  2274. }