pexpr.pas 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.inc}
  20. interface
  21. uses
  22. symtype,
  23. node,
  24. globals,
  25. cpuinfo;
  26. { reads a whole expression }
  27. function expr : tnode;
  28. { reads an expression without assignements and .. }
  29. function comp_expr(accept_equal : boolean):tnode;
  30. { reads a single factor }
  31. function factor(getaddr : boolean) : tnode;
  32. procedure string_dec(var t: ttype);
  33. function parse_paras(__colon,in_prop_paras : boolean) : tnode;
  34. { the ID token has to be consumed before calling this function }
  35. procedure do_member_read(getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean);
  36. {$ifdef int64funcresok}
  37. function get_intconst:TConstExprInt;
  38. {$else int64funcresok}
  39. function get_intconst:longint;
  40. {$endif int64funcresok}
  41. function get_stringconst:string;
  42. implementation
  43. uses
  44. {$ifdef delphi}
  45. SysUtils,
  46. {$endif}
  47. { common }
  48. cutils,
  49. { global }
  50. globtype,tokens,verbose,
  51. systems,widestr,
  52. { symtable }
  53. symconst,symbase,symdef,symsym,symtable,defutil,defcmp,
  54. { pass 1 }
  55. pass_1,htypechk,
  56. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,
  57. { parser }
  58. scanner,
  59. pbase,pinline,
  60. { codegen }
  61. cgbase
  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. got_addrn : boolean = false;
  72. anon_inherited : boolean = false;
  73. procedure string_dec(var t: ttype);
  74. { reads a string type with optional length }
  75. { and returns a pointer to the string }
  76. { definition }
  77. var
  78. p : tnode;
  79. begin
  80. t:=cshortstringtype;
  81. consume(_STRING);
  82. if token=_LECKKLAMMER then
  83. begin
  84. consume(_LECKKLAMMER);
  85. p:=comp_expr(true);
  86. if not is_constintnode(p) then
  87. begin
  88. Message(cg_e_illegal_expression);
  89. { error recovery }
  90. consume(_RECKKLAMMER);
  91. end
  92. else
  93. begin
  94. if (tordconstnode(p).value<=0) then
  95. begin
  96. Message(parser_e_invalid_string_size);
  97. tordconstnode(p).value:=255;
  98. end;
  99. consume(_RECKKLAMMER);
  100. if tordconstnode(p).value>255 then
  101. begin
  102. { longstring is currently unsupported (CEC)! }
  103. { t.setdef(tstringdef.createlong(tordconstnode(p).value))}
  104. Message(parser_e_invalid_string_size);
  105. tordconstnode(p).value:=255;
  106. t.setdef(tstringdef.createshort(tordconstnode(p).value));
  107. end
  108. else
  109. if tordconstnode(p).value<>255 then
  110. t.setdef(tstringdef.createshort(tordconstnode(p).value));
  111. end;
  112. p.free;
  113. end
  114. else
  115. begin
  116. if cs_ansistrings in aktlocalswitches then
  117. t:=cansistringtype
  118. else
  119. t:=cshortstringtype;
  120. end;
  121. end;
  122. function parse_paras(__colon,in_prop_paras : boolean) : tnode;
  123. var
  124. p1,p2 : tnode;
  125. end_of_paras : ttoken;
  126. prev_in_args : boolean;
  127. old_allow_array_constructor : boolean;
  128. begin
  129. if in_prop_paras then
  130. end_of_paras:=_RECKKLAMMER
  131. else
  132. end_of_paras:=_RKLAMMER;
  133. if token=end_of_paras then
  134. begin
  135. parse_paras:=nil;
  136. exit;
  137. end;
  138. { save old values }
  139. prev_in_args:=in_args;
  140. old_allow_array_constructor:=allow_array_constructor;
  141. { set para parsing values }
  142. in_args:=true;
  143. inc(parsing_para_level);
  144. allow_array_constructor:=true;
  145. p2:=nil;
  146. while true do
  147. begin
  148. p1:=comp_expr(true);
  149. p2:=ccallparanode.create(p1,p2);
  150. { it's for the str(l:5,s); }
  151. if __colon and (token=_COLON) then
  152. begin
  153. consume(_COLON);
  154. p1:=comp_expr(true);
  155. p2:=ccallparanode.create(p1,p2);
  156. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  157. if token=_COLON then
  158. begin
  159. consume(_COLON);
  160. p1:=comp_expr(true);
  161. p2:=ccallparanode.create(p1,p2);
  162. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  163. end
  164. end;
  165. if token=_COMMA then
  166. consume(_COMMA)
  167. else
  168. break;
  169. end;
  170. allow_array_constructor:=old_allow_array_constructor;
  171. dec(parsing_para_level);
  172. in_args:=prev_in_args;
  173. parse_paras:=p2;
  174. end;
  175. procedure check_tp_procvar(var p : tnode);
  176. var
  177. hp,
  178. p1 : tnode;
  179. begin
  180. if (m_tp_procvar in aktmodeswitches) and
  181. (token<>_ASSIGNMENT) and
  182. (not got_addrn) and
  183. (block_type=bt_general) then
  184. begin
  185. { ignore vecn,subscriptn }
  186. hp:=p;
  187. repeat
  188. case hp.nodetype of
  189. vecn :
  190. hp:=tvecnode(hp).left;
  191. subscriptn :
  192. hp:=tsubscriptnode(hp).left;
  193. else
  194. break;
  195. end;
  196. until false;
  197. if (hp.nodetype=loadn) then
  198. begin
  199. { get the resulttype of p }
  200. do_resulttypepass(p);
  201. { convert the procvar load to a call:
  202. - not expecting a procvar
  203. - the procvar does not get arguments, when it
  204. requires arguments the callnode will fail
  205. Note: When arguments were passed there was no loadn }
  206. if (getprocvardef=nil) and
  207. (p.resulttype.def.deftype=procvardef) and
  208. (tprocvardef(p.resulttype.def).minparacount=0) then
  209. begin
  210. p1:=ccallnode.create(nil,nil,nil,nil);
  211. tcallnode(p1).set_procvar(p);
  212. resulttypepass(p1);
  213. p:=p1;
  214. end;
  215. end;
  216. end;
  217. end;
  218. function statement_syssym(l : longint) : tnode;
  219. var
  220. p1,p2,paras : tnode;
  221. err,
  222. prev_in_args : boolean;
  223. begin
  224. prev_in_args:=in_args;
  225. case l of
  226. in_new_x :
  227. begin
  228. if afterassignment or in_args then
  229. statement_syssym:=new_function
  230. else
  231. statement_syssym:=new_dispose_statement(true);
  232. end;
  233. in_dispose_x :
  234. begin
  235. statement_syssym:=new_dispose_statement(false);
  236. end;
  237. in_ord_x :
  238. begin
  239. consume(_LKLAMMER);
  240. in_args:=true;
  241. p1:=comp_expr(true);
  242. consume(_RKLAMMER);
  243. p1:=geninlinenode(in_ord_x,false,p1);
  244. statement_syssym := p1;
  245. end;
  246. in_exit :
  247. begin
  248. if try_to_consume(_LKLAMMER) then
  249. begin
  250. p1:=comp_expr(true);
  251. consume(_RKLAMMER);
  252. if (block_type=bt_except) then
  253. Message(parser_e_exit_with_argument_not__possible);
  254. if is_void(aktprocdef.rettype.def) then
  255. Message(parser_e_void_function);
  256. end
  257. else
  258. p1:=nil;
  259. statement_syssym:=cexitnode.create(p1);
  260. end;
  261. in_break :
  262. begin
  263. statement_syssym:=cbreaknode.create;
  264. end;
  265. in_continue :
  266. begin
  267. statement_syssym:=ccontinuenode.create;
  268. end;
  269. in_typeof_x :
  270. begin
  271. consume(_LKLAMMER);
  272. in_args:=true;
  273. p1:=comp_expr(true);
  274. consume(_RKLAMMER);
  275. if p1.nodetype=typen then
  276. ttypenode(p1).allowed:=true;
  277. if (p1.resulttype.def.deftype = objectdef) or
  278. ((p1.resulttype.def.deftype = classrefdef) and
  279. (p1.nodetype in [selfn,loadvmtn])) then
  280. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  281. else
  282. begin
  283. Message(type_e_mismatch);
  284. p1.destroy;
  285. statement_syssym:=cerrornode.create;
  286. end;
  287. end;
  288. in_sizeof_x :
  289. begin
  290. consume(_LKLAMMER);
  291. in_args:=true;
  292. p1:=comp_expr(true);
  293. consume(_RKLAMMER);
  294. if (p1.nodetype<>typen) and
  295. (
  296. (is_object(p1.resulttype.def) and
  297. (oo_has_constructor in tobjectdef(p1.resulttype.def).objectoptions)) or
  298. is_open_array(p1.resulttype.def) or
  299. is_open_string(p1.resulttype.def)
  300. ) then
  301. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  302. else
  303. begin
  304. statement_syssym:=cordconstnode.create(p1.resulttype.def.size,s32bittype,true);
  305. { p1 not needed !}
  306. p1.destroy;
  307. end;
  308. end;
  309. in_typeinfo_x :
  310. begin
  311. consume(_LKLAMMER);
  312. in_args:=true;
  313. p1:=comp_expr(true);
  314. if p1.nodetype=typen then
  315. ttypenode(p1).allowed:=true
  316. else
  317. begin
  318. p1.destroy;
  319. p1:=cerrornode.create;
  320. Message(parser_e_illegal_parameter_list);
  321. end;
  322. consume(_RKLAMMER);
  323. p2:=ccallparanode.create(p1,nil);
  324. p2:=geninlinenode(in_typeinfo_x,false,p2);
  325. statement_syssym:=p2;
  326. end;
  327. in_assigned_x :
  328. begin
  329. err:=false;
  330. consume(_LKLAMMER);
  331. in_args:=true;
  332. p1:=comp_expr(true);
  333. if not codegenerror then
  334. begin
  335. { With tp procvars we allways need to load a
  336. procvar when it is passed }
  337. if (m_tp_procvar in aktmodeswitches) and
  338. (p1.nodetype=calln) then
  339. load_procvar_from_calln(p1);
  340. case p1.resulttype.def.deftype of
  341. pointerdef,
  342. procvardef,
  343. classrefdef : ;
  344. objectdef :
  345. if not is_class_or_interface(p1.resulttype.def) then
  346. begin
  347. Message(parser_e_illegal_parameter_list);
  348. err:=true;
  349. end;
  350. else
  351. begin
  352. Message(parser_e_illegal_parameter_list);
  353. err:=true;
  354. end;
  355. end;
  356. end
  357. else
  358. err:=true;
  359. if not err then
  360. begin
  361. p2:=ccallparanode.create(p1,nil);
  362. p2:=geninlinenode(in_assigned_x,false,p2);
  363. end
  364. else
  365. begin
  366. p1.free;
  367. p2:=cerrornode.create;
  368. end;
  369. consume(_RKLAMMER);
  370. statement_syssym:=p2;
  371. end;
  372. in_addr_x :
  373. begin
  374. consume(_LKLAMMER);
  375. in_args:=true;
  376. p1:=comp_expr(true);
  377. p1:=caddrnode.create(p1);
  378. consume(_RKLAMMER);
  379. statement_syssym:=p1;
  380. end;
  381. in_ofs_x :
  382. begin
  383. consume(_LKLAMMER);
  384. in_args:=true;
  385. p1:=comp_expr(true);
  386. p1:=caddrnode.create(p1);
  387. do_resulttypepass(p1);
  388. { Ofs() returns a cardinal, not a pointer }
  389. p1.resulttype:=u32bittype;
  390. consume(_RKLAMMER);
  391. statement_syssym:=p1;
  392. end;
  393. in_seg_x :
  394. begin
  395. consume(_LKLAMMER);
  396. in_args:=true;
  397. p1:=comp_expr(true);
  398. p1:=geninlinenode(in_seg_x,false,p1);
  399. consume(_RKLAMMER);
  400. statement_syssym:=p1;
  401. end;
  402. in_high_x,
  403. in_low_x :
  404. begin
  405. consume(_LKLAMMER);
  406. in_args:=true;
  407. p1:=comp_expr(true);
  408. p2:=geninlinenode(l,false,p1);
  409. consume(_RKLAMMER);
  410. statement_syssym:=p2;
  411. end;
  412. in_succ_x,
  413. in_pred_x :
  414. begin
  415. consume(_LKLAMMER);
  416. in_args:=true;
  417. p1:=comp_expr(true);
  418. p2:=geninlinenode(l,false,p1);
  419. consume(_RKLAMMER);
  420. statement_syssym:=p2;
  421. end;
  422. in_inc_x,
  423. in_dec_x :
  424. begin
  425. consume(_LKLAMMER);
  426. in_args:=true;
  427. p1:=comp_expr(true);
  428. if token=_COMMA then
  429. begin
  430. consume(_COMMA);
  431. p2:=ccallparanode.create(comp_expr(true),nil);
  432. end
  433. else
  434. p2:=nil;
  435. p2:=ccallparanode.create(p1,p2);
  436. statement_syssym:=geninlinenode(l,false,p2);
  437. consume(_RKLAMMER);
  438. end;
  439. in_finalize_x:
  440. begin
  441. statement_syssym:=inline_finalize;
  442. end;
  443. in_copy_x:
  444. begin
  445. statement_syssym:=inline_copy;
  446. end;
  447. in_concat_x :
  448. begin
  449. consume(_LKLAMMER);
  450. in_args:=true;
  451. p2:=nil;
  452. while true do
  453. begin
  454. p1:=comp_expr(true);
  455. set_varstate(p1,true);
  456. if not((p1.resulttype.def.deftype=stringdef) or
  457. ((p1.resulttype.def.deftype=orddef) and
  458. (torddef(p1.resulttype.def).typ=uchar))) then
  459. Message(parser_e_illegal_parameter_list);
  460. if p2<>nil then
  461. p2:=caddnode.create(addn,p2,p1)
  462. else
  463. p2:=p1;
  464. if token=_COMMA then
  465. consume(_COMMA)
  466. else
  467. break;
  468. end;
  469. consume(_RKLAMMER);
  470. statement_syssym:=p2;
  471. end;
  472. in_read_x,
  473. in_readln_x :
  474. begin
  475. if token=_LKLAMMER then
  476. begin
  477. consume(_LKLAMMER);
  478. paras:=parse_paras(false,false);
  479. consume(_RKLAMMER);
  480. end
  481. else
  482. paras:=nil;
  483. p1:=geninlinenode(l,false,paras);
  484. statement_syssym := p1;
  485. end;
  486. in_setlength_x:
  487. begin
  488. statement_syssym := inline_setlength;
  489. end;
  490. in_length_x:
  491. begin
  492. consume(_LKLAMMER);
  493. in_args:=true;
  494. p1:=comp_expr(true);
  495. p2:=geninlinenode(l,false,p1);
  496. consume(_RKLAMMER);
  497. statement_syssym:=p2;
  498. end;
  499. in_write_x,
  500. in_writeln_x :
  501. begin
  502. if token=_LKLAMMER then
  503. begin
  504. consume(_LKLAMMER);
  505. paras:=parse_paras(true,false);
  506. consume(_RKLAMMER);
  507. end
  508. else
  509. paras:=nil;
  510. p1 := geninlinenode(l,false,paras);
  511. statement_syssym := p1;
  512. end;
  513. in_str_x_string :
  514. begin
  515. consume(_LKLAMMER);
  516. paras:=parse_paras(true,false);
  517. consume(_RKLAMMER);
  518. p1 := geninlinenode(l,false,paras);
  519. statement_syssym := p1;
  520. end;
  521. in_val_x:
  522. Begin
  523. consume(_LKLAMMER);
  524. in_args := true;
  525. p1:= ccallparanode.create(comp_expr(true), nil);
  526. consume(_COMMA);
  527. p2 := ccallparanode.create(comp_expr(true),p1);
  528. if (token = _COMMA) then
  529. Begin
  530. consume(_COMMA);
  531. p2 := ccallparanode.create(comp_expr(true),p2)
  532. End;
  533. consume(_RKLAMMER);
  534. p2 := geninlinenode(l,false,p2);
  535. statement_syssym := p2;
  536. End;
  537. in_include_x_y,
  538. in_exclude_x_y :
  539. begin
  540. consume(_LKLAMMER);
  541. in_args:=true;
  542. p1:=comp_expr(true);
  543. consume(_COMMA);
  544. p2:=comp_expr(true);
  545. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  546. consume(_RKLAMMER);
  547. end;
  548. in_assert_x_y :
  549. begin
  550. consume(_LKLAMMER);
  551. in_args:=true;
  552. p1:=comp_expr(true);
  553. if token=_COMMA then
  554. begin
  555. consume(_COMMA);
  556. p2:=comp_expr(true);
  557. end
  558. else
  559. begin
  560. { then insert an empty string }
  561. p2:=cstringconstnode.createstr('',st_default);
  562. end;
  563. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  564. consume(_RKLAMMER);
  565. end;
  566. else
  567. internalerror(15);
  568. end;
  569. in_args:=prev_in_args;
  570. end;
  571. { reads the parameter for a subroutine call }
  572. procedure do_proc_call(sym:tsym;st:tsymtable;getaddr:boolean;var again : boolean;var p1:tnode);
  573. var
  574. prevafterassn : boolean;
  575. hs,hs1 : tvarsym;
  576. para,p2 : tnode;
  577. hst : tsymtable;
  578. aprocdef : tprocdef;
  579. begin
  580. prevafterassn:=afterassignment;
  581. afterassignment:=false;
  582. aprocdef:=nil;
  583. { When we are expecting a procvar we also need
  584. to get the address in some cases }
  585. if assigned(getprocvardef) then
  586. begin
  587. if (block_type=bt_const) or
  588. getaddr then
  589. begin
  590. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  591. getaddr:=true;
  592. end
  593. else
  594. if (m_tp_procvar in aktmodeswitches) then
  595. begin
  596. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  597. if assigned(aprocdef) then
  598. getaddr:=true;
  599. end;
  600. end;
  601. { want we only determine the address of }
  602. { a subroutine ? }
  603. if not(getaddr) then
  604. begin
  605. para:=nil;
  606. if anon_inherited then
  607. begin
  608. hst:=symtablestack;
  609. while assigned(hst) and (hst.symtabletype<>parasymtable) do
  610. hst:=hst.next;
  611. if assigned(hst) then
  612. begin
  613. hs:=tvarsym(hst.symindex.first);
  614. while assigned(hs) do
  615. begin
  616. if hs.typ<>varsym then
  617. internalerror(54382953);
  618. { if there is a localcopy then use that }
  619. if assigned(hs.localvarsym) then
  620. hs1:=hs.localvarsym
  621. else
  622. hs1:=hs;
  623. para:=ccallparanode.create(cloadnode.create(hs1,hs1.owner),para);
  624. hs:=tvarsym(hs.indexnext);
  625. end;
  626. end
  627. else
  628. internalerror(54382954);
  629. end
  630. else
  631. begin
  632. if try_to_consume(_LKLAMMER) then
  633. begin
  634. para:=parse_paras(false,false);
  635. consume(_RKLAMMER);
  636. end;
  637. end;
  638. p1:=ccallnode.create(para,tprocsym(sym),st,p1);
  639. end
  640. else
  641. begin
  642. { address operator @: }
  643. if not assigned(p1) then
  644. begin
  645. case st.symtabletype of
  646. withsymtable :
  647. begin
  648. if (st.defowner.deftype=objectdef) then
  649. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  650. end;
  651. objectsymtable :
  652. begin
  653. { we must provide a method pointer, if it isn't given, }
  654. { it is self }
  655. p1:=cselfnode.create(tobjectdef(st.defowner));
  656. end;
  657. end;
  658. end;
  659. { Retrieve info which procvar to call. For tp_procvar the
  660. aprocdef is already loaded above so we can reuse it }
  661. if not assigned(aprocdef) and
  662. assigned(getprocvardef) then
  663. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  664. { generate a methodcallnode or proccallnode }
  665. { we shouldn't convert things like @tcollection.load }
  666. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  667. if assigned(p1) then
  668. begin
  669. if (p1.nodetype<>typen) then
  670. tloadnode(p2).set_mp(p1)
  671. else
  672. p1.free;
  673. end;
  674. p1:=p2;
  675. { no postfix operators }
  676. again:=false;
  677. end;
  678. afterassignment:=prevafterassn;
  679. end;
  680. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  681. var
  682. hp,hp2 : tnode;
  683. hpp : ^tnode;
  684. currprocdef : tprocdef;
  685. begin
  686. if not assigned(pv) then
  687. internalerror(200301121);
  688. if (m_tp_procvar in aktmodeswitches) then
  689. begin
  690. hp:=p2;
  691. hpp:=@p2;
  692. while assigned(hp) and
  693. (hp.nodetype=typeconvn) do
  694. begin
  695. hp:=ttypeconvnode(hp).left;
  696. { save orignal address of the old tree so we can replace the node }
  697. hpp:=@hp;
  698. end;
  699. if (hp.nodetype=calln) and
  700. { a procvar can't have parameters! }
  701. not assigned(tcallnode(hp).left) then
  702. begin
  703. currprocdef:=tcallnode(hp).symtableprocentry.search_procdef_byprocvardef(pv);
  704. if assigned(currprocdef) then
  705. begin
  706. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  707. if (po_methodpointer in pv.procoptions) then
  708. tloadnode(hp2).set_mp(tnode(tcallnode(hp).methodpointer).getcopy);
  709. hp.destroy;
  710. { replace the old callnode with the new loadnode }
  711. hpp^:=hp2;
  712. end;
  713. end;
  714. end;
  715. end;
  716. { the following procedure handles the access to a property symbol }
  717. procedure handle_propertysym(sym : tsym;st : tsymtable;var p1 : tnode; getaddr: boolean);
  718. procedure symlist_to_node(var p1:tnode;pl:tsymlist);
  719. var
  720. plist : psymlistitem;
  721. begin
  722. plist:=pl.firstsym;
  723. while assigned(plist) do
  724. begin
  725. case plist^.sltype of
  726. sl_load :
  727. begin
  728. { p1 can already contain the loadnode of
  729. the class variable. Then we need to use a
  730. subscriptn. If no tree is found (with block), then
  731. generate a loadn }
  732. if assigned(p1) then
  733. p1:=csubscriptnode.create(plist^.sym,p1)
  734. else
  735. p1:=cloadnode.create(plist^.sym,st);
  736. end;
  737. sl_subscript :
  738. p1:=csubscriptnode.create(plist^.sym,p1);
  739. sl_vec :
  740. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,s32bittype,true));
  741. else
  742. internalerror(200110205);
  743. end;
  744. plist:=plist^.next;
  745. end;
  746. include(p1.flags,nf_isproperty);
  747. end;
  748. var
  749. paras : tnode;
  750. p2 : tnode;
  751. begin
  752. paras:=nil;
  753. { property parameters? read them only if the property really }
  754. { has parameters }
  755. if (ppo_hasparameters in tpropertysym(sym).propoptions) then
  756. begin
  757. if token=_LECKKLAMMER then
  758. begin
  759. consume(_LECKKLAMMER);
  760. paras:=parse_paras(false,true);
  761. consume(_RECKKLAMMER);
  762. end;
  763. end;
  764. { indexed property }
  765. if (ppo_indexed in tpropertysym(sym).propoptions) then
  766. begin
  767. p2:=cordconstnode.create(tpropertysym(sym).index,tpropertysym(sym).indextype,true);
  768. paras:=ccallparanode.create(p2,paras);
  769. end;
  770. { we need only a write property if a := follows }
  771. { if not(afterassignment) and not(in_args) then }
  772. if token=_ASSIGNMENT then
  773. begin
  774. { write property: }
  775. if not tpropertysym(sym).writeaccess.empty then
  776. begin
  777. case tpropertysym(sym).writeaccess.firstsym^.sym.typ of
  778. procsym :
  779. begin
  780. { generate the method call }
  781. p1:=ccallnode.create(paras,
  782. tprocsym(tpropertysym(sym).writeaccess.firstsym^.sym),st,p1);
  783. paras:=nil;
  784. consume(_ASSIGNMENT);
  785. { read the expression }
  786. if tpropertysym(sym).proptype.def.deftype=procvardef then
  787. getprocvardef:=tprocvardef(tpropertysym(sym).proptype.def);
  788. p2:=comp_expr(true);
  789. if assigned(getprocvardef) then
  790. handle_procvar(getprocvardef,p2);
  791. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  792. include(tcallnode(p1).flags,nf_isproperty);
  793. getprocvardef:=nil;
  794. end;
  795. varsym :
  796. begin
  797. { generate access code }
  798. symlist_to_node(p1,tpropertysym(sym).writeaccess);
  799. consume(_ASSIGNMENT);
  800. { read the expression }
  801. p2:=comp_expr(true);
  802. p1:=cassignmentnode.create(p1,p2);
  803. end
  804. else
  805. begin
  806. p1:=cerrornode.create;
  807. Message(parser_e_no_procedure_to_access_property);
  808. end;
  809. end;
  810. end
  811. else
  812. begin
  813. p1:=cerrornode.create;
  814. Message(parser_e_no_procedure_to_access_property);
  815. end;
  816. end
  817. else
  818. begin
  819. { read property: }
  820. if not tpropertysym(sym).readaccess.empty then
  821. begin
  822. case tpropertysym(sym).readaccess.firstsym^.sym.typ of
  823. varsym :
  824. begin
  825. { generate access code }
  826. symlist_to_node(p1,tpropertysym(sym).readaccess);
  827. end;
  828. procsym :
  829. begin
  830. { generate the method call }
  831. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).readaccess.firstsym^.sym),st,p1);
  832. paras:=nil;
  833. include(p1.flags,nf_isproperty);
  834. end
  835. else
  836. begin
  837. p1:=cerrornode.create;
  838. Message(type_e_mismatch);
  839. end;
  840. end;
  841. end
  842. else
  843. begin
  844. { error, no function to read property }
  845. p1:=cerrornode.create;
  846. Message(parser_e_no_procedure_to_access_property);
  847. end;
  848. end;
  849. { release paras if not used }
  850. if assigned(paras) then
  851. paras.free;
  852. end;
  853. { the ID token has to be consumed before calling this function }
  854. procedure do_member_read(getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean);
  855. var
  856. static_name : string;
  857. isclassref : boolean;
  858. srsymtable : tsymtable;
  859. begin
  860. if sym=nil then
  861. begin
  862. { pattern is still valid unless
  863. there is another ID just after the ID of sym }
  864. Message1(sym_e_id_no_member,pattern);
  865. p1.free;
  866. p1:=cerrornode.create;
  867. { try to clean up }
  868. again:=false;
  869. end
  870. else
  871. begin
  872. if assigned(p1) then
  873. begin
  874. if not assigned(p1.resulttype.def) then
  875. do_resulttypepass(p1);
  876. isclassref:=(p1.resulttype.def.deftype=classrefdef);
  877. end
  878. else
  879. isclassref:=false;
  880. { we assume, that only procsyms and varsyms are in an object }
  881. { symbol table, for classes, properties are allowed }
  882. case sym.typ of
  883. procsym:
  884. begin
  885. do_proc_call(sym,sym.owner,
  886. (getaddr and not(token in [_CARET,_POINT])),
  887. again,p1);
  888. { we need to know which procedure is called }
  889. do_resulttypepass(p1);
  890. { now we know the real method e.g. we can check for a class method }
  891. if isclassref and
  892. (p1.nodetype=calln) and
  893. assigned(tcallnode(p1).procdefinition) and
  894. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  895. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  896. Message(parser_e_only_class_methods_via_class_ref);
  897. end;
  898. varsym:
  899. begin
  900. if isclassref then
  901. Message(parser_e_only_class_methods_via_class_ref);
  902. if (sp_static in sym.symoptions) then
  903. begin
  904. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  905. searchsym(static_name,sym,srsymtable);
  906. check_hints(sym);
  907. p1.free;
  908. p1:=cloadnode.create(sym,srsymtable);
  909. end
  910. else
  911. p1:=csubscriptnode.create(sym,p1);
  912. end;
  913. propertysym:
  914. begin
  915. if isclassref then
  916. Message(parser_e_only_class_methods_via_class_ref);
  917. handle_propertysym(sym,sym.owner,p1,getaddr);
  918. end;
  919. else internalerror(16);
  920. end;
  921. end;
  922. end;
  923. {****************************************************************************
  924. Factor
  925. ****************************************************************************}
  926. {$ifdef fpc}
  927. {$maxfpuregisters 0}
  928. {$endif fpc}
  929. function factor(getaddr : boolean) : tnode;
  930. {---------------------------------------------
  931. Factor_read_id
  932. ---------------------------------------------}
  933. procedure factor_read_id(var p1:tnode;var again:boolean);
  934. var
  935. pc : pchar;
  936. len : longint;
  937. srsym : tsym;
  938. possible_error : boolean;
  939. srsymtable : tsymtable;
  940. storesymtablestack : tsymtable;
  941. htype : ttype;
  942. static_name : string;
  943. begin
  944. { allow post fix operators }
  945. again:=true;
  946. consume_sym(srsym,srsymtable);
  947. { Access to funcret or need to call the function? }
  948. if (srsym.typ in [absolutesym,varsym]) and
  949. (vo_is_funcret in tvarsym(srsym).varoptions) and
  950. (
  951. (token=_LKLAMMER) or
  952. (not(m_fpc in aktmodeswitches) and
  953. (afterassignment or in_args) and
  954. not(vo_is_result in tvarsym(srsym).varoptions))
  955. ) then
  956. begin
  957. storesymtablestack:=symtablestack;
  958. symtablestack:=srsym.owner.next;
  959. searchsym(srsym.name,srsym,srsymtable);
  960. if not assigned(srsym) then
  961. srsym:=generrorsym;
  962. if (srsym.typ<>procsym) then
  963. Message(cg_e_illegal_expression);
  964. symtablestack:=storesymtablestack;
  965. end;
  966. begin
  967. { check semantics of private }
  968. if (srsym.typ in [propertysym,procsym,varsym]) and
  969. (srsym.owner.symtabletype=objectsymtable) then
  970. begin
  971. if (sp_private in srsym.symoptions) and
  972. (tobjectdef(srsym.owner.defowner).owner.symtabletype=globalsymtable) and
  973. (tobjectdef(srsym.owner.defowner).owner.unitid<>0) then
  974. Message(parser_e_cant_access_private_member);
  975. end;
  976. case srsym.typ of
  977. absolutesym :
  978. begin
  979. p1:=cloadnode.create(srsym,srsymtable);
  980. end;
  981. varsym :
  982. begin
  983. { are we in a class method, we check here the
  984. srsymtable, because a field in another object
  985. also has objectsymtable. And withsymtable is
  986. not possible for self in class methods (PFV) }
  987. if (srsymtable.symtabletype=objectsymtable) and
  988. assigned(aktprocsym) and
  989. (po_classmethod in aktprocdef.procoptions) then
  990. Message(parser_e_only_class_methods);
  991. if (sp_static in srsym.symoptions) then
  992. begin
  993. static_name:=lower(srsym.owner.name^)+'_'+srsym.name;
  994. searchsym(static_name,srsym,srsymtable);
  995. check_hints(srsym);
  996. end;
  997. p1:=cloadnode.create(srsym,srsymtable);
  998. if tvarsym(srsym).varstate=vs_declared then
  999. begin
  1000. include(p1.flags,nf_first_use);
  1001. { set special between first loaded until checked in resulttypepass }
  1002. tvarsym(srsym).varstate:=vs_declared_and_first_found;
  1003. end;
  1004. end;
  1005. typedconstsym :
  1006. begin
  1007. p1:=cloadnode.create(srsym,srsymtable);
  1008. end;
  1009. syssym :
  1010. begin
  1011. p1:=statement_syssym(tsyssym(srsym).number);
  1012. end;
  1013. typesym :
  1014. begin
  1015. htype.setsym(srsym);
  1016. if not assigned(htype.def) then
  1017. begin
  1018. again:=false;
  1019. end
  1020. else
  1021. begin
  1022. if token=_LKLAMMER then
  1023. begin
  1024. consume(_LKLAMMER);
  1025. p1:=comp_expr(true);
  1026. consume(_RKLAMMER);
  1027. p1:=ctypeconvnode.create_explicit(p1,htype);
  1028. end
  1029. else { not LKLAMMER }
  1030. if (token=_POINT) and
  1031. is_object(htype.def) then
  1032. begin
  1033. consume(_POINT);
  1034. if assigned(procinfo) and
  1035. assigned(procinfo._class) and
  1036. not(getaddr) then
  1037. begin
  1038. if procinfo._class.is_related(tobjectdef(htype.def)) then
  1039. begin
  1040. p1:=ctypenode.create(htype);
  1041. { search also in inherited methods }
  1042. srsym:=searchsym_in_class(tobjectdef(htype.def),pattern);
  1043. check_hints(srsym);
  1044. consume(_ID);
  1045. do_member_read(false,srsym,p1,again);
  1046. end
  1047. else
  1048. begin
  1049. Message(parser_e_no_super_class);
  1050. again:=false;
  1051. end;
  1052. end
  1053. else
  1054. begin
  1055. { allows @TObject.Load }
  1056. { also allows static methods and variables }
  1057. p1:=ctypenode.create(htype);
  1058. { TP allows also @TMenu.Load if Load is only }
  1059. { defined in an anchestor class }
  1060. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1061. check_hints(srsym);
  1062. if not assigned(srsym) then
  1063. Message1(sym_e_id_no_member,pattern)
  1064. else if not(getaddr) and not(sp_static in srsym.symoptions) then
  1065. Message(sym_e_only_static_in_static)
  1066. else
  1067. begin
  1068. consume(_ID);
  1069. do_member_read(getaddr,srsym,p1,again);
  1070. end;
  1071. end;
  1072. end
  1073. else
  1074. begin
  1075. { class reference ? }
  1076. if is_class(htype.def) then
  1077. begin
  1078. if getaddr and (token=_POINT) then
  1079. begin
  1080. consume(_POINT);
  1081. { allows @Object.Method }
  1082. { also allows static methods and variables }
  1083. p1:=ctypenode.create(htype);
  1084. { TP allows also @TMenu.Load if Load is only }
  1085. { defined in an anchestor class }
  1086. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1087. check_hints(srsym);
  1088. if not assigned(srsym) then
  1089. Message1(sym_e_id_no_member,pattern)
  1090. else
  1091. begin
  1092. consume(_ID);
  1093. do_member_read(getaddr,srsym,p1,again);
  1094. end;
  1095. end
  1096. else
  1097. begin
  1098. p1:=ctypenode.create(htype);
  1099. { For a type block we simply return only
  1100. the type. For all other blocks we return
  1101. a loadvmt node }
  1102. if (block_type<>bt_type) then
  1103. p1:=cloadvmtnode.create(p1);
  1104. end;
  1105. end
  1106. else
  1107. p1:=ctypenode.create(htype);
  1108. end;
  1109. end;
  1110. end;
  1111. enumsym :
  1112. begin
  1113. p1:=genenumnode(tenumsym(srsym));
  1114. end;
  1115. constsym :
  1116. begin
  1117. case tconstsym(srsym).consttyp of
  1118. constint :
  1119. begin
  1120. { do a very dirty trick to bootstrap this code }
  1121. if (tconstsym(srsym).value.valueord>=-(int64(2147483647)+int64(1))) and
  1122. (tconstsym(srsym).value.valueord<=2147483647) then
  1123. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,s32bittype,true)
  1124. else if (tconstsym(srsym).value.valueord > maxlongint) and
  1125. (tconstsym(srsym).value.valueord <= int64(maxlongint)+int64(maxlongint)+1) then
  1126. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,u32bittype,true)
  1127. else
  1128. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,cs64bittype,true);
  1129. end;
  1130. conststring :
  1131. begin
  1132. len:=tconstsym(srsym).value.len;
  1133. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1134. len:=255;
  1135. getmem(pc,len+1);
  1136. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1137. pc[len]:=#0;
  1138. p1:=cstringconstnode.createpchar(pc,len);
  1139. end;
  1140. constchar :
  1141. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,cchartype,true);
  1142. constreal :
  1143. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1144. constbool :
  1145. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,booltype,true);
  1146. constset :
  1147. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).consttype);
  1148. constord :
  1149. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).consttype,true);
  1150. constpointer :
  1151. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).consttype);
  1152. constnil :
  1153. p1:=cnilnode.create;
  1154. constresourcestring:
  1155. begin
  1156. p1:=cloadnode.create(srsym,srsymtable);
  1157. do_resulttypepass(p1);
  1158. p1.resulttype:=cansistringtype;
  1159. end;
  1160. constguid :
  1161. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1162. end;
  1163. end;
  1164. procsym :
  1165. begin
  1166. { are we in a class method ? }
  1167. possible_error:=(srsym.owner.symtabletype=objectsymtable) and
  1168. not(is_interface(tdef(srsym.owner.defowner))) and
  1169. assigned(aktprocsym) and
  1170. (po_classmethod in aktprocdef.procoptions);
  1171. do_proc_call(srsym,srsymtable,
  1172. (getaddr and not(token in [_CARET,_POINT])),
  1173. again,p1);
  1174. { we need to know which procedure is called }
  1175. if possible_error then
  1176. begin
  1177. do_resulttypepass(p1);
  1178. if not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1179. Message(parser_e_only_class_methods);
  1180. end;
  1181. end;
  1182. propertysym :
  1183. begin
  1184. { access to property in a method }
  1185. { are we in a class method ? }
  1186. if (srsym.owner.symtabletype=objectsymtable) and
  1187. assigned(aktprocsym) and
  1188. (po_classmethod in aktprocdef.procoptions) then
  1189. Message(parser_e_only_class_methods);
  1190. { no method pointer }
  1191. p1:=nil;
  1192. handle_propertysym(srsym,srsymtable,p1,getaddr);
  1193. end;
  1194. labelsym :
  1195. begin
  1196. consume(_COLON);
  1197. if tlabelsym(srsym).defined then
  1198. Message(sym_e_label_already_defined);
  1199. tlabelsym(srsym).defined:=true;
  1200. p1:=clabelnode.create(tlabelsym(srsym),nil);
  1201. end;
  1202. errorsym :
  1203. begin
  1204. p1:=cerrornode.create;
  1205. if token=_LKLAMMER then
  1206. begin
  1207. consume(_LKLAMMER);
  1208. parse_paras(false,false);
  1209. consume(_RKLAMMER);
  1210. end;
  1211. end;
  1212. else
  1213. begin
  1214. p1:=cerrornode.create;
  1215. Message(cg_e_illegal_expression);
  1216. end;
  1217. end; { end case }
  1218. end;
  1219. end;
  1220. {---------------------------------------------
  1221. Factor_Read_Set
  1222. ---------------------------------------------}
  1223. { Read a set between [] }
  1224. function factor_read_set:tnode;
  1225. var
  1226. p1,p2 : tnode;
  1227. lastp,
  1228. buildp : tarrayconstructornode;
  1229. begin
  1230. buildp:=nil;
  1231. { be sure that a least one arrayconstructn is used, also for an
  1232. empty [] }
  1233. if token=_RECKKLAMMER then
  1234. buildp:=carrayconstructornode.create(nil,buildp)
  1235. else
  1236. begin
  1237. while true do
  1238. begin
  1239. p1:=comp_expr(true);
  1240. if token=_POINTPOINT then
  1241. begin
  1242. consume(_POINTPOINT);
  1243. p2:=comp_expr(true);
  1244. p1:=carrayconstructorrangenode.create(p1,p2);
  1245. end;
  1246. { insert at the end of the tree, to get the correct order }
  1247. if not assigned(buildp) then
  1248. begin
  1249. buildp:=carrayconstructornode.create(p1,nil);
  1250. lastp:=buildp;
  1251. end
  1252. else
  1253. begin
  1254. lastp.right:=carrayconstructornode.create(p1,nil);
  1255. lastp:=tarrayconstructornode(lastp.right);
  1256. end;
  1257. { there could be more elements }
  1258. if token=_COMMA then
  1259. consume(_COMMA)
  1260. else
  1261. break;
  1262. end;
  1263. end;
  1264. factor_read_set:=buildp;
  1265. end;
  1266. {---------------------------------------------
  1267. PostFixOperators
  1268. ---------------------------------------------}
  1269. procedure postfixoperators(var p1:tnode;var again:boolean);
  1270. { tries to avoid syntax errors after invalid qualifiers }
  1271. procedure recoverconsume_postfixops;
  1272. begin
  1273. while true do
  1274. begin
  1275. case token of
  1276. _CARET:
  1277. consume(_CARET);
  1278. _POINT:
  1279. begin
  1280. consume(_POINT);
  1281. if token=_ID then
  1282. consume(_ID);
  1283. end;
  1284. _LECKKLAMMER:
  1285. begin
  1286. consume(_LECKKLAMMER);
  1287. repeat
  1288. comp_expr(true);
  1289. if token=_COMMA then
  1290. consume(_COMMA)
  1291. else
  1292. break;
  1293. until false;
  1294. consume(_RECKKLAMMER);
  1295. end
  1296. else
  1297. break;
  1298. end;
  1299. end;
  1300. end;
  1301. var
  1302. store_static : boolean;
  1303. protsym : tpropertysym;
  1304. p2,p3 : tnode;
  1305. hsym : tsym;
  1306. classh : tobjectdef;
  1307. begin
  1308. again:=true;
  1309. while again do
  1310. begin
  1311. { we need the resulttype }
  1312. do_resulttypepass(p1);
  1313. if codegenerror then
  1314. begin
  1315. recoverconsume_postfixops;
  1316. exit;
  1317. end;
  1318. { handle token }
  1319. case token of
  1320. _CARET:
  1321. begin
  1322. consume(_CARET);
  1323. if (p1.resulttype.def.deftype<>pointerdef) then
  1324. begin
  1325. { ^ as binary operator is a problem!!!! (FK) }
  1326. again:=false;
  1327. Message(cg_e_invalid_qualifier);
  1328. recoverconsume_postfixops;
  1329. p1.destroy;
  1330. p1:=cerrornode.create;
  1331. end
  1332. else
  1333. begin
  1334. p1:=cderefnode.create(p1);
  1335. end;
  1336. end;
  1337. _LECKKLAMMER:
  1338. begin
  1339. if is_class_or_interface(p1.resulttype.def) then
  1340. begin
  1341. { default property }
  1342. protsym:=search_default_property(tobjectdef(p1.resulttype.def));
  1343. if not(assigned(protsym)) then
  1344. begin
  1345. p1.destroy;
  1346. p1:=cerrornode.create;
  1347. again:=false;
  1348. message(parser_e_no_default_property_available);
  1349. end
  1350. else
  1351. begin
  1352. { The property symbol is referenced indirect }
  1353. inc(protsym.refs);
  1354. handle_propertysym(protsym,protsym.owner,p1,getaddr);
  1355. end;
  1356. end
  1357. else
  1358. begin
  1359. consume(_LECKKLAMMER);
  1360. repeat
  1361. case p1.resulttype.def.deftype of
  1362. pointerdef:
  1363. begin
  1364. { support delphi autoderef }
  1365. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=arraydef) and
  1366. (m_autoderef in aktmodeswitches) then
  1367. begin
  1368. p1:=cderefnode.create(p1);
  1369. end;
  1370. p2:=comp_expr(true);
  1371. p1:=cvecnode.create(p1,p2);
  1372. end;
  1373. stringdef :
  1374. begin
  1375. p2:=comp_expr(true);
  1376. p1:=cvecnode.create(p1,p2);
  1377. end;
  1378. arraydef :
  1379. begin
  1380. p2:=comp_expr(true);
  1381. { support SEG:OFS for go32v2 Mem[] }
  1382. if (target_info.system=system_i386_go32v2) and
  1383. (p1.nodetype=loadn) and
  1384. assigned(tloadnode(p1).symtableentry) and
  1385. assigned(tloadnode(p1).symtableentry.owner.name) and
  1386. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1387. ((tloadnode(p1).symtableentry.name='MEM') or
  1388. (tloadnode(p1).symtableentry.name='MEMW') or
  1389. (tloadnode(p1).symtableentry.name='MEML')) then
  1390. begin
  1391. if (token=_COLON) then
  1392. begin
  1393. consume(_COLON);
  1394. p3:=caddnode.create(muln,cordconstnode.create($10,s32bittype,false),p2);
  1395. p2:=comp_expr(true);
  1396. p2:=caddnode.create(addn,p2,p3);
  1397. p1:=cvecnode.create(p1,p2);
  1398. include(tvecnode(p1).flags,nf_memseg);
  1399. include(tvecnode(p1).flags,nf_memindex);
  1400. end
  1401. else
  1402. begin
  1403. p1:=cvecnode.create(p1,p2);
  1404. include(tvecnode(p1).flags,nf_memindex);
  1405. end;
  1406. end
  1407. else
  1408. p1:=cvecnode.create(p1,p2);
  1409. end;
  1410. else
  1411. begin
  1412. Message(cg_e_invalid_qualifier);
  1413. p1.destroy;
  1414. p1:=cerrornode.create;
  1415. comp_expr(true);
  1416. again:=false;
  1417. end;
  1418. end;
  1419. do_resulttypepass(p1);
  1420. if token=_COMMA then
  1421. consume(_COMMA)
  1422. else
  1423. break;
  1424. until false;
  1425. consume(_RECKKLAMMER);
  1426. end;
  1427. end;
  1428. _POINT :
  1429. begin
  1430. consume(_POINT);
  1431. if (p1.resulttype.def.deftype=pointerdef) and
  1432. (m_autoderef in aktmodeswitches) then
  1433. begin
  1434. p1:=cderefnode.create(p1);
  1435. do_resulttypepass(p1);
  1436. end;
  1437. case p1.resulttype.def.deftype of
  1438. recorddef:
  1439. begin
  1440. hsym:=tsym(trecorddef(p1.resulttype.def).symtable.search(pattern));
  1441. check_hints(hsym);
  1442. if assigned(hsym) and
  1443. (hsym.typ=varsym) then
  1444. p1:=csubscriptnode.create(hsym,p1)
  1445. else
  1446. begin
  1447. Message1(sym_e_illegal_field,pattern);
  1448. p1.destroy;
  1449. p1:=cerrornode.create;
  1450. end;
  1451. consume(_ID);
  1452. end;
  1453. variantdef:
  1454. begin
  1455. end;
  1456. classrefdef:
  1457. begin
  1458. classh:=tobjectdef(tclassrefdef(p1.resulttype.def).pointertype.def);
  1459. hsym:=searchsym_in_class(classh,pattern);
  1460. check_hints(hsym);
  1461. if hsym=nil then
  1462. begin
  1463. Message1(sym_e_id_no_member,pattern);
  1464. p1.destroy;
  1465. p1:=cerrornode.create;
  1466. { try to clean up }
  1467. consume(_ID);
  1468. end
  1469. else
  1470. begin
  1471. consume(_ID);
  1472. do_member_read(getaddr,hsym,p1,again);
  1473. end;
  1474. end;
  1475. objectdef:
  1476. begin
  1477. store_static:=allow_only_static;
  1478. allow_only_static:=false;
  1479. classh:=tobjectdef(p1.resulttype.def);
  1480. hsym:=searchsym_in_class(classh,pattern);
  1481. check_hints(hsym);
  1482. allow_only_static:=store_static;
  1483. if hsym=nil then
  1484. begin
  1485. Message1(sym_e_id_no_member,pattern);
  1486. p1.destroy;
  1487. p1:=cerrornode.create;
  1488. { try to clean up }
  1489. consume(_ID);
  1490. end
  1491. else
  1492. begin
  1493. consume(_ID);
  1494. do_member_read(getaddr,hsym,p1,again);
  1495. end;
  1496. end;
  1497. pointerdef:
  1498. begin
  1499. Message(cg_e_invalid_qualifier);
  1500. if tpointerdef(p1.resulttype.def).pointertype.def.deftype in [recorddef,objectdef,classrefdef] then
  1501. Message(parser_h_maybe_deref_caret_missing);
  1502. end;
  1503. else
  1504. begin
  1505. Message(cg_e_invalid_qualifier);
  1506. p1.destroy;
  1507. p1:=cerrornode.create;
  1508. consume(_ID);
  1509. end;
  1510. end;
  1511. end;
  1512. else
  1513. begin
  1514. { is this a procedure variable ? }
  1515. if assigned(p1.resulttype.def) then
  1516. begin
  1517. if (p1.resulttype.def.deftype=procvardef) then
  1518. begin
  1519. if assigned(getprocvardef) and
  1520. equal_defs(p1.resulttype.def,getprocvardef) then
  1521. again:=false
  1522. else
  1523. if (token=_LKLAMMER) or
  1524. ((tprocvardef(p1.resulttype.def).para.empty) and
  1525. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1526. (not afterassignment) and
  1527. (not in_args)) then
  1528. begin
  1529. { do this in a strange way }
  1530. { it's not a clean solution }
  1531. p2:=p1;
  1532. p1:=ccallnode.create(nil,nil,nil,nil);
  1533. tcallnode(p1).set_procvar(p2);
  1534. if try_to_consume(_LKLAMMER) then
  1535. begin
  1536. tcallnode(p1).left:=parse_paras(false,false);
  1537. consume(_RKLAMMER);
  1538. end;
  1539. { proc():= is never possible }
  1540. if token=_ASSIGNMENT then
  1541. begin
  1542. Message(cg_e_illegal_expression);
  1543. p1:=cerrornode.create;
  1544. again:=false;
  1545. end;
  1546. end
  1547. else
  1548. again:=false;
  1549. end
  1550. else
  1551. again:=false;
  1552. end
  1553. else
  1554. again:=false;
  1555. end;
  1556. end;
  1557. end; { while again }
  1558. end;
  1559. {---------------------------------------------
  1560. Factor (Main)
  1561. ---------------------------------------------}
  1562. var
  1563. l : longint;
  1564. card : cardinal;
  1565. ic : TConstExprInt;
  1566. oldp1,
  1567. p1 : tnode;
  1568. code : integer;
  1569. again : boolean;
  1570. sym : tsym;
  1571. classh : tobjectdef;
  1572. d : bestreal;
  1573. hs : string;
  1574. htype : ttype;
  1575. filepos : tfileposinfo;
  1576. {---------------------------------------------
  1577. Helpers
  1578. ---------------------------------------------}
  1579. procedure check_tokenpos;
  1580. begin
  1581. if (p1<>oldp1) then
  1582. begin
  1583. if assigned(p1) then
  1584. p1.set_tree_filepos(filepos);
  1585. oldp1:=p1;
  1586. filepos:=akttokenpos;
  1587. end;
  1588. end;
  1589. begin
  1590. oldp1:=nil;
  1591. p1:=nil;
  1592. filepos:=akttokenpos;
  1593. again:=false;
  1594. if token=_ID then
  1595. begin
  1596. factor_read_id(p1,again);
  1597. if again then
  1598. begin
  1599. check_tokenpos;
  1600. { handle post fix operators }
  1601. postfixoperators(p1,again);
  1602. end;
  1603. end
  1604. else
  1605. case token of
  1606. _SELF :
  1607. begin
  1608. again:=true;
  1609. consume(_SELF);
  1610. if not assigned(procinfo._class) then
  1611. begin
  1612. p1:=cerrornode.create;
  1613. again:=false;
  1614. Message(parser_e_self_not_in_method);
  1615. end
  1616. else
  1617. begin
  1618. if (po_classmethod in aktprocdef.procoptions) then
  1619. begin
  1620. { self in class methods is a class reference type }
  1621. htype.setdef(procinfo._class);
  1622. p1:=cselfnode.create(tclassrefdef.create(htype));
  1623. end
  1624. else
  1625. p1:=cselfnode.create(procinfo._class);
  1626. postfixoperators(p1,again);
  1627. end;
  1628. end;
  1629. _INHERITED :
  1630. begin
  1631. again:=true;
  1632. consume(_INHERITED);
  1633. if assigned(procinfo._class) then
  1634. begin
  1635. classh:=procinfo._class.childof;
  1636. { if inherited; only then we need the method with
  1637. the same name }
  1638. if token in endtokens then
  1639. begin
  1640. hs:=aktprocsym.name;
  1641. anon_inherited:=true;
  1642. { For message methods we need to search using the message
  1643. number or string }
  1644. if (po_msgint in aktprocsym.first_procdef.procoptions) then
  1645. sym:=searchsym_in_class_by_msgint(classh,aktprocsym.first_procdef.messageinf.i)
  1646. else
  1647. if (po_msgstr in aktprocsym.first_procdef.procoptions) then
  1648. sym:=searchsym_in_class_by_msgstr(classh,aktprocsym.first_procdef.messageinf.str)
  1649. else
  1650. sym:=searchsym_in_class(classh,hs);
  1651. end
  1652. else
  1653. begin
  1654. hs:=pattern;
  1655. consume(_ID);
  1656. anon_inherited:=false;
  1657. sym:=searchsym_in_class(classh,hs);
  1658. end;
  1659. if assigned(sym) then
  1660. begin
  1661. check_hints(sym);
  1662. if sym.typ=procsym then
  1663. begin
  1664. htype.setdef(classh);
  1665. p1:=ctypenode.create(htype);
  1666. end;
  1667. do_member_read(false,sym,p1,again);
  1668. { Add flag to indicate that inherited is used }
  1669. if p1.nodetype=calln then
  1670. include(p1.flags,nf_anon_inherited);
  1671. end
  1672. else
  1673. begin
  1674. if anon_inherited then
  1675. begin
  1676. { we didn't find a member in the parents call the
  1677. DefaultHandler }
  1678. sym:=searchsym_in_class(classh,'DEFAULTHANDLER');
  1679. if not assigned(sym) or
  1680. (sym.typ<>procsym) then
  1681. internalerror(200303171);
  1682. p1:=nil;
  1683. do_proc_call(sym,sym.owner,false,again,p1);
  1684. end
  1685. else
  1686. begin
  1687. Message1(sym_e_id_no_member,hs);
  1688. p1:=cerrornode.create;
  1689. end;
  1690. again:=false;
  1691. end;
  1692. { turn auto inheriting off }
  1693. anon_inherited:=false;
  1694. end
  1695. else
  1696. begin
  1697. Message(parser_e_generic_methods_only_in_methods);
  1698. again:=false;
  1699. p1:=cerrornode.create;
  1700. end;
  1701. postfixoperators(p1,again);
  1702. end;
  1703. _INTCONST :
  1704. begin
  1705. { try cardinal first }
  1706. val(pattern,card,code);
  1707. if code<>0 then
  1708. begin
  1709. { then longint }
  1710. valint(pattern,l,code);
  1711. if code <> 0 then
  1712. begin
  1713. { then int64 }
  1714. val(pattern,ic,code);
  1715. if code<>0 then
  1716. begin
  1717. {finally float }
  1718. val(pattern,d,code);
  1719. if code<>0 then
  1720. begin
  1721. Message(cg_e_invalid_integer);
  1722. consume(_INTCONST);
  1723. l:=1;
  1724. p1:=cordconstnode.create(l,s32bittype,true);
  1725. end
  1726. else
  1727. begin
  1728. consume(_INTCONST);
  1729. p1:=crealconstnode.create(d,pbestrealtype^);
  1730. end;
  1731. end
  1732. else
  1733. begin
  1734. consume(_INTCONST);
  1735. p1:=cordconstnode.create(ic,cs64bittype,true);
  1736. end
  1737. end
  1738. else
  1739. begin
  1740. consume(_INTCONST);
  1741. p1:=cordconstnode.create(l,s32bittype,true)
  1742. end
  1743. end
  1744. else
  1745. begin
  1746. consume(_INTCONST);
  1747. { check whether the value isn't in the longint range as well }
  1748. { (longint is easier to perform calculations with) (JM) }
  1749. if card <= $7fffffff then
  1750. { no sign extension necessary, so not longint typecast (JM) }
  1751. p1:=cordconstnode.create(card,s32bittype,true)
  1752. else
  1753. p1:=cordconstnode.create(card,u32bittype,true)
  1754. end;
  1755. end;
  1756. _REALNUMBER :
  1757. begin
  1758. val(pattern,d,code);
  1759. if code<>0 then
  1760. begin
  1761. Message(parser_e_error_in_real);
  1762. d:=1.0;
  1763. end;
  1764. consume(_REALNUMBER);
  1765. p1:=crealconstnode.create(d,pbestrealtype^);
  1766. end;
  1767. _STRING :
  1768. begin
  1769. string_dec(htype);
  1770. { STRING can be also a type cast }
  1771. if token=_LKLAMMER then
  1772. begin
  1773. consume(_LKLAMMER);
  1774. p1:=comp_expr(true);
  1775. consume(_RKLAMMER);
  1776. p1:=ctypeconvnode.create_explicit(p1,htype);
  1777. { handle postfix operators here e.g. string(a)[10] }
  1778. again:=true;
  1779. postfixoperators(p1,again);
  1780. end
  1781. else
  1782. p1:=ctypenode.create(htype);
  1783. end;
  1784. _FILE :
  1785. begin
  1786. htype:=cfiletype;
  1787. consume(_FILE);
  1788. { FILE can be also a type cast }
  1789. if token=_LKLAMMER then
  1790. begin
  1791. consume(_LKLAMMER);
  1792. p1:=comp_expr(true);
  1793. consume(_RKLAMMER);
  1794. p1:=ctypeconvnode.create_explicit(p1,htype);
  1795. { handle postfix operators here e.g. string(a)[10] }
  1796. again:=true;
  1797. postfixoperators(p1,again);
  1798. end
  1799. else
  1800. begin
  1801. p1:=ctypenode.create(htype);
  1802. end;
  1803. end;
  1804. _CSTRING :
  1805. begin
  1806. p1:=cstringconstnode.createstr(pattern,st_default);
  1807. consume(_CSTRING);
  1808. end;
  1809. _CCHAR :
  1810. begin
  1811. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  1812. consume(_CCHAR);
  1813. end;
  1814. _CWSTRING:
  1815. begin
  1816. p1:=cstringconstnode.createwstr(patternw);
  1817. consume(_CWSTRING);
  1818. end;
  1819. _CWCHAR:
  1820. begin
  1821. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  1822. consume(_CWCHAR);
  1823. end;
  1824. _KLAMMERAFFE :
  1825. begin
  1826. consume(_KLAMMERAFFE);
  1827. got_addrn:=true;
  1828. { support both @<x> and @(<x>) }
  1829. if try_to_consume(_LKLAMMER) then
  1830. begin
  1831. p1:=factor(true);
  1832. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1833. begin
  1834. again:=true;
  1835. postfixoperators(p1,again);
  1836. end;
  1837. consume(_RKLAMMER);
  1838. end
  1839. else
  1840. p1:=factor(true);
  1841. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1842. begin
  1843. again:=true;
  1844. postfixoperators(p1,again);
  1845. end;
  1846. got_addrn:=false;
  1847. p1:=caddrnode.create(p1);
  1848. { Store the procvar that we are expecting, the
  1849. addrn will use the information to find the correct
  1850. procdef or it will return an error }
  1851. if assigned(getprocvardef) and
  1852. (taddrnode(p1).left.nodetype = loadn) then
  1853. taddrnode(p1).getprocvardef:=getprocvardef;
  1854. end;
  1855. _LKLAMMER :
  1856. begin
  1857. consume(_LKLAMMER);
  1858. p1:=comp_expr(true);
  1859. consume(_RKLAMMER);
  1860. { it's not a good solution }
  1861. { but (a+b)^ makes some problems }
  1862. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1863. begin
  1864. again:=true;
  1865. postfixoperators(p1,again);
  1866. end;
  1867. end;
  1868. _LECKKLAMMER :
  1869. begin
  1870. consume(_LECKKLAMMER);
  1871. p1:=factor_read_set;
  1872. consume(_RECKKLAMMER);
  1873. end;
  1874. _PLUS :
  1875. begin
  1876. consume(_PLUS);
  1877. p1:=factor(false);
  1878. end;
  1879. _MINUS :
  1880. begin
  1881. consume(_MINUS);
  1882. p1:=sub_expr(oppower,false);
  1883. p1:=cunaryminusnode.create(p1);
  1884. end;
  1885. _OP_NOT :
  1886. begin
  1887. consume(_OP_NOT);
  1888. p1:=factor(false);
  1889. p1:=cnotnode.create(p1);
  1890. end;
  1891. _TRUE :
  1892. begin
  1893. consume(_TRUE);
  1894. p1:=cordconstnode.create(1,booltype,false);
  1895. end;
  1896. _FALSE :
  1897. begin
  1898. consume(_FALSE);
  1899. p1:=cordconstnode.create(0,booltype,false);
  1900. end;
  1901. _NIL :
  1902. begin
  1903. consume(_NIL);
  1904. p1:=cnilnode.create;
  1905. { It's really ugly code nil^, but delphi allows it }
  1906. if token in [_CARET] then
  1907. begin
  1908. again:=true;
  1909. postfixoperators(p1,again);
  1910. end;
  1911. end;
  1912. else
  1913. begin
  1914. p1:=cerrornode.create;
  1915. consume(token);
  1916. Message(cg_e_illegal_expression);
  1917. end;
  1918. end;
  1919. { generate error node if no node is created }
  1920. if not assigned(p1) then
  1921. begin
  1922. {$ifdef EXTDEBUG}
  1923. Comment(V_Warning,'factor: p1=nil');
  1924. {$endif}
  1925. p1:=cerrornode.create;
  1926. end;
  1927. { get the resulttype for the node }
  1928. if (not assigned(p1.resulttype.def)) then
  1929. do_resulttypepass(p1);
  1930. { tp7 procvar handling, but not if the next token
  1931. will be a := }
  1932. check_tp_procvar(p1);
  1933. factor:=p1;
  1934. check_tokenpos;
  1935. end;
  1936. {$ifdef fpc}
  1937. {$maxfpuregisters default}
  1938. {$endif fpc}
  1939. {****************************************************************************
  1940. Sub_Expr
  1941. ****************************************************************************}
  1942. const
  1943. { Warning these stay be ordered !! }
  1944. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1945. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN,_OP_IS],
  1946. [_PLUS,_MINUS,_OP_OR,_OP_XOR],
  1947. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  1948. _OP_AS,_OP_AND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  1949. [_STARSTAR] );
  1950. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  1951. {Reads a subexpression while the operators are of the current precedence
  1952. level, or any higher level. Replaces the old term, simpl_expr and
  1953. simpl2_expr.}
  1954. var
  1955. p1,p2 : tnode;
  1956. oldt : Ttoken;
  1957. filepos : tfileposinfo;
  1958. begin
  1959. if pred_level=highest_precedence then
  1960. p1:=factor(false)
  1961. else
  1962. p1:=sub_expr(succ(pred_level),true);
  1963. repeat
  1964. if (token in operator_levels[pred_level]) and
  1965. ((token<>_EQUAL) or accept_equal) then
  1966. begin
  1967. oldt:=token;
  1968. filepos:=akttokenpos;
  1969. consume(token);
  1970. if pred_level=highest_precedence then
  1971. p2:=factor(false)
  1972. else
  1973. p2:=sub_expr(succ(pred_level),true);
  1974. case oldt of
  1975. _PLUS :
  1976. p1:=caddnode.create(addn,p1,p2);
  1977. _MINUS :
  1978. p1:=caddnode.create(subn,p1,p2);
  1979. _STAR :
  1980. p1:=caddnode.create(muln,p1,p2);
  1981. _SLASH :
  1982. p1:=caddnode.create(slashn,p1,p2);
  1983. _EQUAL :
  1984. p1:=caddnode.create(equaln,p1,p2);
  1985. _GT :
  1986. p1:=caddnode.create(gtn,p1,p2);
  1987. _LT :
  1988. p1:=caddnode.create(ltn,p1,p2);
  1989. _GTE :
  1990. p1:=caddnode.create(gten,p1,p2);
  1991. _LTE :
  1992. p1:=caddnode.create(lten,p1,p2);
  1993. _SYMDIF :
  1994. p1:=caddnode.create(symdifn,p1,p2);
  1995. _STARSTAR :
  1996. p1:=caddnode.create(starstarn,p1,p2);
  1997. _OP_AS :
  1998. p1:=casnode.create(p1,p2);
  1999. _OP_IN :
  2000. p1:=cinnode.create(p1,p2);
  2001. _OP_IS :
  2002. p1:=cisnode.create(p1,p2);
  2003. _OP_OR :
  2004. p1:=caddnode.create(orn,p1,p2);
  2005. _OP_AND :
  2006. p1:=caddnode.create(andn,p1,p2);
  2007. _OP_DIV :
  2008. p1:=cmoddivnode.create(divn,p1,p2);
  2009. _OP_NOT :
  2010. p1:=cnotnode.create(p1);
  2011. _OP_MOD :
  2012. p1:=cmoddivnode.create(modn,p1,p2);
  2013. _OP_SHL :
  2014. p1:=cshlshrnode.create(shln,p1,p2);
  2015. _OP_SHR :
  2016. p1:=cshlshrnode.create(shrn,p1,p2);
  2017. _OP_XOR :
  2018. p1:=caddnode.create(xorn,p1,p2);
  2019. _ASSIGNMENT :
  2020. p1:=cassignmentnode.create(p1,p2);
  2021. _CARET :
  2022. p1:=caddnode.create(caretn,p1,p2);
  2023. _UNEQUAL :
  2024. p1:=caddnode.create(unequaln,p1,p2);
  2025. end;
  2026. p1.set_tree_filepos(filepos);
  2027. end
  2028. else
  2029. break;
  2030. until false;
  2031. sub_expr:=p1;
  2032. end;
  2033. function comp_expr(accept_equal : boolean):tnode;
  2034. var
  2035. oldafterassignment : boolean;
  2036. p1 : tnode;
  2037. begin
  2038. oldafterassignment:=afterassignment;
  2039. afterassignment:=true;
  2040. p1:=sub_expr(opcompare,accept_equal);
  2041. { get the resulttype for this expression }
  2042. if not assigned(p1.resulttype.def) then
  2043. do_resulttypepass(p1);
  2044. afterassignment:=oldafterassignment;
  2045. comp_expr:=p1;
  2046. end;
  2047. function expr : tnode;
  2048. var
  2049. p1,p2 : tnode;
  2050. oldafterassignment : boolean;
  2051. oldp1 : tnode;
  2052. filepos : tfileposinfo;
  2053. begin
  2054. oldafterassignment:=afterassignment;
  2055. p1:=sub_expr(opcompare,true);
  2056. { get the resulttype for this expression }
  2057. if not assigned(p1.resulttype.def) then
  2058. do_resulttypepass(p1);
  2059. filepos:=akttokenpos;
  2060. check_tp_procvar(p1);
  2061. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2062. afterassignment:=true;
  2063. oldp1:=p1;
  2064. case token of
  2065. _POINTPOINT :
  2066. begin
  2067. consume(_POINTPOINT);
  2068. p2:=sub_expr(opcompare,true);
  2069. p1:=crangenode.create(p1,p2);
  2070. end;
  2071. _ASSIGNMENT :
  2072. begin
  2073. consume(_ASSIGNMENT);
  2074. if (p1.resulttype.def.deftype=procvardef) then
  2075. getprocvardef:=tprocvardef(p1.resulttype.def);
  2076. p2:=sub_expr(opcompare,true);
  2077. if assigned(getprocvardef) then
  2078. handle_procvar(getprocvardef,p2);
  2079. getprocvardef:=nil;
  2080. p1:=cassignmentnode.create(p1,p2);
  2081. end;
  2082. _PLUSASN :
  2083. begin
  2084. consume(_PLUSASN);
  2085. p2:=sub_expr(opcompare,true);
  2086. p1:=cassignmentnode.create(p1,caddnode.create(addn,p1.getcopy,p2));
  2087. end;
  2088. _MINUSASN :
  2089. begin
  2090. consume(_MINUSASN);
  2091. p2:=sub_expr(opcompare,true);
  2092. p1:=cassignmentnode.create(p1,caddnode.create(subn,p1.getcopy,p2));
  2093. end;
  2094. _STARASN :
  2095. begin
  2096. consume(_STARASN );
  2097. p2:=sub_expr(opcompare,true);
  2098. p1:=cassignmentnode.create(p1,caddnode.create(muln,p1.getcopy,p2));
  2099. end;
  2100. _SLASHASN :
  2101. begin
  2102. consume(_SLASHASN );
  2103. p2:=sub_expr(opcompare,true);
  2104. p1:=cassignmentnode.create(p1,caddnode.create(slashn,p1.getcopy,p2));
  2105. end;
  2106. end;
  2107. { get the resulttype for this expression }
  2108. if not assigned(p1.resulttype.def) then
  2109. do_resulttypepass(p1);
  2110. afterassignment:=oldafterassignment;
  2111. if p1<>oldp1 then
  2112. p1.set_tree_filepos(filepos);
  2113. expr:=p1;
  2114. end;
  2115. {$ifdef int64funcresok}
  2116. function get_intconst:TConstExprInt;
  2117. {$else int64funcresok}
  2118. function get_intconst:longint;
  2119. {$endif int64funcresok}
  2120. {Reads an expression, tries to evalute it and check if it is an integer
  2121. constant. Then the constant is returned.}
  2122. var
  2123. p:tnode;
  2124. begin
  2125. p:=comp_expr(true);
  2126. if not codegenerror then
  2127. begin
  2128. if (p.nodetype<>ordconstn) or
  2129. not(is_integer(p.resulttype.def)) then
  2130. Message(cg_e_illegal_expression)
  2131. else
  2132. get_intconst:=tordconstnode(p).value;
  2133. end;
  2134. p.free;
  2135. end;
  2136. function get_stringconst:string;
  2137. {Reads an expression, tries to evaluate it and checks if it is a string
  2138. constant. Then the constant is returned.}
  2139. var
  2140. p:tnode;
  2141. begin
  2142. get_stringconst:='';
  2143. p:=comp_expr(true);
  2144. if p.nodetype<>stringconstn then
  2145. begin
  2146. if (p.nodetype=ordconstn) and is_char(p.resulttype.def) then
  2147. get_stringconst:=char(tordconstnode(p).value)
  2148. else
  2149. Message(cg_e_illegal_expression);
  2150. end
  2151. else
  2152. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2153. p.free;
  2154. end;
  2155. end.
  2156. {
  2157. $Log$
  2158. Revision 1.111 2003-04-26 00:33:07 peter
  2159. * vo_is_result flag added for the special RESULT symbol
  2160. Revision 1.110 2003/04/25 20:59:33 peter
  2161. * removed funcretn,funcretsym, function result is now in varsym
  2162. and aliases for result and function name are added using absolutesym
  2163. * vs_hidden parameter for funcret passed in parameter
  2164. * vs_hidden fixes
  2165. * writenode changed to printnode and released from extdebug
  2166. * -vp option added to generate a tree.log with the nodetree
  2167. * nicer printnode for statements, callnode
  2168. Revision 1.109 2003/04/23 10:13:55 peter
  2169. * firstaddr will check procvardef
  2170. Revision 1.108 2003/04/22 23:50:23 peter
  2171. * firstpass uses expectloc
  2172. * checks if there are differences between the expectloc and
  2173. location.loc from secondpass in EXTDEBUG
  2174. Revision 1.107 2003/04/11 15:49:01 peter
  2175. * default property also increased the reference count for the
  2176. property symbol
  2177. Revision 1.106 2003/04/11 14:50:08 peter
  2178. * fix tw2454
  2179. Revision 1.105 2003/03/27 17:44:13 peter
  2180. * fixed small mem leaks
  2181. Revision 1.104 2003/03/17 18:55:30 peter
  2182. * allow more tokens instead of only semicolon after inherited
  2183. Revision 1.103 2003/03/17 16:54:41 peter
  2184. * support DefaultHandler and anonymous inheritance fixed
  2185. for message methods
  2186. Revision 1.102 2003/01/30 21:46:57 peter
  2187. * self fixes for static methods (merged)
  2188. Revision 1.101 2003/01/16 22:12:22 peter
  2189. * Find the correct procvar to load when using @ in fpc mode
  2190. Revision 1.100 2003/01/15 01:44:32 peter
  2191. * merged methodpointer fixes from 1.0.x
  2192. Revision 1.98 2003/01/12 17:51:42 peter
  2193. * tp procvar handling fix for tb0448
  2194. Revision 1.97 2003/01/05 22:44:14 peter
  2195. * remove a lot of code to support typen in loadn-procsym
  2196. Revision 1.96 2002/12/11 22:40:36 peter
  2197. * assigned(procvar) fix for delphi mode, fixes tb0430
  2198. Revision 1.95 2002/11/30 11:12:48 carl
  2199. + checking for symbols used with hint directives is done mostly in pexpr
  2200. only now
  2201. Revision 1.94 2002/11/27 15:33:47 peter
  2202. * the never ending story of tp procvar hacks
  2203. Revision 1.93 2002/11/26 22:58:24 peter
  2204. * fix for tw2178. When a ^ or . follows a procsym then the procsym
  2205. needs to be called
  2206. Revision 1.92 2002/11/25 17:43:22 peter
  2207. * splitted defbase in defutil,symutil,defcmp
  2208. * merged isconvertable and is_equal into compare_defs(_ext)
  2209. * made operator search faster by walking the list only once
  2210. Revision 1.91 2002/11/22 22:48:10 carl
  2211. * memory optimization with tconstsym (1.5%)
  2212. Revision 1.90 2002/11/20 22:49:55 pierre
  2213. * commented check code tht was invalid in 1.1
  2214. Revision 1.89 2002/11/18 18:34:41 peter
  2215. * fix crash with EXTDEBUG code
  2216. Revision 1.88 2002/11/18 17:48:21 peter
  2217. * fix tw2209 (merged)
  2218. Revision 1.87 2002/11/18 17:31:58 peter
  2219. * pass proccalloption to ret_in_xxx and push_xxx functions
  2220. Revision 1.86 2002/10/05 00:48:57 peter
  2221. * support inherited; support for overload as it is handled by
  2222. delphi. This is only for delphi mode as it is working is
  2223. undocumented and hard to predict what is done
  2224. Revision 1.85 2002/10/04 21:13:59 peter
  2225. * ignore vecn,subscriptn when checking for a procvar loadn
  2226. Revision 1.84 2002/10/02 20:51:22 peter
  2227. * don't check interfaces for class methods
  2228. Revision 1.83 2002/10/02 18:20:52 peter
  2229. * Copy() is now internal syssym that calls compilerprocs
  2230. Revision 1.82 2002/09/30 07:00:48 florian
  2231. * fixes to common code to get the alpha compiler compiled applied
  2232. Revision 1.81 2002/09/16 19:06:14 peter
  2233. * allow ^ after nil
  2234. Revision 1.80 2002/09/07 15:25:07 peter
  2235. * old logs removed and tabs fixed
  2236. Revision 1.79 2002/09/07 12:16:03 carl
  2237. * second part bug report 1996 fix, testrange in cordconstnode
  2238. only called if option is set (also make parsing a tiny faster)
  2239. Revision 1.78 2002/09/03 16:26:27 daniel
  2240. * Make Tprocdef.defs protected
  2241. Revision 1.77 2002/08/18 20:06:24 peter
  2242. * inlining is now also allowed in interface
  2243. * renamed write/load to ppuwrite/ppuload
  2244. * tnode storing in ppu
  2245. * nld,ncon,nbas are already updated for storing in ppu
  2246. Revision 1.76 2002/08/17 09:23:39 florian
  2247. * first part of procinfo rewrite
  2248. Revision 1.75 2002/08/01 16:37:47 jonas
  2249. - removed some superfluous "in_paras := true" statements
  2250. Revision 1.74 2002/07/26 21:15:41 florian
  2251. * rewrote the system handling
  2252. Revision 1.73 2002/07/23 09:51:23 daniel
  2253. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  2254. are worth comitting.
  2255. Revision 1.72 2002/07/20 11:57:55 florian
  2256. * types.pas renamed to defbase.pas because D6 contains a types
  2257. unit so this would conflicts if D6 programms are compiled
  2258. + Willamette/SSE2 instructions to assembler added
  2259. Revision 1.71 2002/07/16 15:34:20 florian
  2260. * exit is now a syssym instead of a keyword
  2261. Revision 1.70 2002/07/06 20:18:02 carl
  2262. * longstring declaration now gives parser error since its not supported!
  2263. Revision 1.69 2002/06/12 15:46:14 jonas
  2264. * fixed web bug 1995
  2265. Revision 1.68 2002/05/18 13:34:12 peter
  2266. * readded missing revisions
  2267. Revision 1.67 2002/05/16 19:46:43 carl
  2268. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2269. + try to fix temp allocation (still in ifdef)
  2270. + generic constructor calls
  2271. + start of tassembler / tmodulebase class cleanup
  2272. Revision 1.65 2002/05/12 16:53:09 peter
  2273. * moved entry and exitcode to ncgutil and cgobj
  2274. * foreach gets extra argument for passing local data to the
  2275. iterator function
  2276. * -CR checks also class typecasts at runtime by changing them
  2277. into as
  2278. * fixed compiler to cycle with the -CR option
  2279. * fixed stabs with elf writer, finally the global variables can
  2280. be watched
  2281. * removed a lot of routines from cga unit and replaced them by
  2282. calls to cgobj
  2283. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2284. u32bit then the other is typecasted also to u32bit without giving
  2285. a rangecheck warning/error.
  2286. * fixed pascal calling method with reversing also the high tree in
  2287. the parast, detected by tcalcst3 test
  2288. Revision 1.64 2002/04/23 19:16:34 peter
  2289. * add pinline unit that inserts compiler supported functions using
  2290. one or more statements
  2291. * moved finalize and setlength from ninl to pinline
  2292. Revision 1.63 2002/04/21 19:02:05 peter
  2293. * removed newn and disposen nodes, the code is now directly
  2294. inlined from pexpr
  2295. * -an option that will write the secondpass nodes to the .s file, this
  2296. requires EXTDEBUG define to actually write the info
  2297. * fixed various internal errors and crashes due recent code changes
  2298. Revision 1.62 2002/04/16 16:11:17 peter
  2299. * using inherited; without a parent having the same function
  2300. will do nothing like delphi
  2301. Revision 1.61 2002/04/07 13:31:36 carl
  2302. + change unit use
  2303. Revision 1.60 2002/04/01 20:57:13 jonas
  2304. * fixed web bug 1907
  2305. * fixed some other procvar related bugs (all related to accepting procvar
  2306. constructs with either too many or too little parameters)
  2307. (both merged, includes second typo fix of pexpr.pas)
  2308. Revision 1.59 2002/03/31 20:26:35 jonas
  2309. + a_loadfpu_* and a_loadmm_* methods in tcg
  2310. * register allocation is now handled by a class and is mostly processor
  2311. independent (+rgobj.pas and i386/rgcpu.pas)
  2312. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2313. * some small improvements and fixes to the optimizer
  2314. * some register allocation fixes
  2315. * some fpuvaroffset fixes in the unary minus node
  2316. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2317. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2318. also better optimizable)
  2319. * fixed and optimized register saving/restoring for new/dispose nodes
  2320. * LOC_FPU locations now also require their "register" field to be set to
  2321. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2322. - list field removed of the tnode class because it's not used currently
  2323. and can cause hard-to-find bugs
  2324. }