pexpr.pas 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing of expression for Free Pascal
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pexpr;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,symdef,symbase,
  22. node,ncal,
  23. tokens,globtype,globals,constexp;
  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. procedure string_dec(var def: tdef; allowtypedef: boolean);
  31. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  32. { the ID token has to be consumed before calling this function }
  33. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  34. function get_intconst:TConstExprInt;
  35. function get_stringconst:string;
  36. implementation
  37. uses
  38. { common }
  39. cutils,
  40. { global }
  41. verbose,
  42. systems,widestr,
  43. { symtable }
  44. symconst,symtable,symsym,defutil,defcmp,
  45. { module }
  46. fmodule,ppu,
  47. { pass 1 }
  48. pass_1,htypechk,
  49. nmat,nadd,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,
  50. { parser }
  51. scanner,
  52. pbase,pinline,ptype,
  53. { codegen }
  54. cgbase,procinfo,cpuinfo
  55. ;
  56. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  57. read as - (1**4) and not (-1)**4 PM }
  58. type
  59. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  60. const
  61. highest_precedence = oppower;
  62. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;forward;
  63. const
  64. { true, if the inherited call is anonymous }
  65. anon_inherited : boolean = false;
  66. { last def found, only used by anon. inherited calls to insert proper type casts }
  67. srdef : tdef = nil;
  68. procedure string_dec(var def:tdef; allowtypedef: boolean);
  69. { reads a string type with optional length }
  70. { and returns a pointer to the string }
  71. { definition }
  72. var
  73. p : tnode;
  74. begin
  75. def:=cshortstringtype;
  76. consume(_STRING);
  77. if (token=_LECKKLAMMER) then
  78. begin
  79. if not(allowtypedef) then
  80. Message(parser_e_no_local_para_def);
  81. consume(_LECKKLAMMER);
  82. p:=comp_expr(true);
  83. if not is_constintnode(p) then
  84. begin
  85. Message(parser_e_illegal_expression);
  86. { error recovery }
  87. consume(_RECKKLAMMER);
  88. end
  89. else
  90. begin
  91. if (tordconstnode(p).value<=0) then
  92. begin
  93. Message(parser_e_invalid_string_size);
  94. tordconstnode(p).value:=255;
  95. end;
  96. consume(_RECKKLAMMER);
  97. if tordconstnode(p).value>255 then
  98. begin
  99. { longstring is currently unsupported (CEC)! }
  100. { t:=tstringdef.createlong(tordconstnode(p).value))}
  101. Message(parser_e_invalid_string_size);
  102. tordconstnode(p).value:=255;
  103. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  104. end
  105. else
  106. if tordconstnode(p).value<>255 then
  107. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  108. end;
  109. p.free;
  110. end
  111. else
  112. begin
  113. if cs_ansistrings in current_settings.localswitches then
  114. def:=cansistringtype
  115. else
  116. def:=cshortstringtype;
  117. end;
  118. end;
  119. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  120. var
  121. p1,p2,argname : tnode;
  122. prev_in_args,
  123. old_named_args_allowed,
  124. old_allow_array_constructor : boolean;
  125. begin
  126. if token=end_of_paras then
  127. begin
  128. parse_paras:=nil;
  129. exit;
  130. end;
  131. { save old values }
  132. prev_in_args:=in_args;
  133. old_allow_array_constructor:=allow_array_constructor;
  134. old_named_args_allowed:=named_args_allowed;
  135. { set para parsing values }
  136. in_args:=true;
  137. named_args_allowed:=false;
  138. allow_array_constructor:=true;
  139. p2:=nil;
  140. repeat
  141. if __namedpara then
  142. begin
  143. if token=_COMMA then
  144. begin
  145. { empty parameter }
  146. p2:=ccallparanode.create(cnothingnode.create,p2);
  147. end
  148. else
  149. begin
  150. named_args_allowed:=true;
  151. p1:=comp_expr(true);
  152. named_args_allowed:=false;
  153. if found_arg_name then
  154. begin
  155. argname:=p1;
  156. p1:=comp_expr(true);
  157. p2:=ccallparanode.create(p1,p2);
  158. tcallparanode(p2).parametername:=argname;
  159. end
  160. else
  161. p2:=ccallparanode.create(p1,p2);
  162. found_arg_name:=false;
  163. end;
  164. end
  165. else
  166. begin
  167. p1:=comp_expr(true);
  168. p2:=ccallparanode.create(p1,p2);
  169. end;
  170. { it's for the str(l:5,s); }
  171. if __colon and (token=_COLON) then
  172. begin
  173. consume(_COLON);
  174. p1:=comp_expr(true);
  175. p2:=ccallparanode.create(p1,p2);
  176. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  177. if try_to_consume(_COLON) then
  178. begin
  179. p1:=comp_expr(true);
  180. p2:=ccallparanode.create(p1,p2);
  181. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  182. end
  183. end;
  184. until not try_to_consume(_COMMA);
  185. allow_array_constructor:=old_allow_array_constructor;
  186. in_args:=prev_in_args;
  187. named_args_allowed:=old_named_args_allowed;
  188. parse_paras:=p2;
  189. end;
  190. function gen_c_style_operator(ntyp:tnodetype;p1,p2:tnode) : tnode;
  191. var
  192. hp : tnode;
  193. hdef : tdef;
  194. temp : ttempcreatenode;
  195. newstatement : tstatementnode;
  196. begin
  197. { Properties are not allowed, because the write can
  198. be different from the read }
  199. if (nf_isproperty in p1.flags) then
  200. begin
  201. Message(type_e_variable_id_expected);
  202. { We can continue with the loading,
  203. it'll not create errors. Only the expected
  204. result can be wrong }
  205. end;
  206. hp:=p1;
  207. while assigned(hp) and
  208. (hp.nodetype in [derefn,subscriptn,vecn,typeconvn]) do
  209. hp:=tunarynode(hp).left;
  210. if not assigned(hp) then
  211. internalerror(200410121);
  212. if (hp.nodetype=calln) then
  213. begin
  214. typecheckpass(p1);
  215. result:=internalstatements(newstatement);
  216. hdef:=tpointerdef.create(p1.resultdef);
  217. temp:=ctempcreatenode.create(hdef,sizeof(pint),tt_persistent,false);
  218. addstatement(newstatement,temp);
  219. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),caddrnode.create_internal(p1)));
  220. addstatement(newstatement,cassignmentnode.create(
  221. cderefnode.create(ctemprefnode.create(temp)),
  222. caddnode.create(ntyp,
  223. cderefnode.create(ctemprefnode.create(temp)),
  224. p2)));
  225. addstatement(newstatement,ctempdeletenode.create(temp));
  226. end
  227. else
  228. result:=cassignmentnode.create(p1,caddnode.create(ntyp,p1.getcopy,p2));
  229. end;
  230. function statement_syssym(l : byte) : tnode;
  231. var
  232. p1,p2,paras : tnode;
  233. err,
  234. prev_in_args : boolean;
  235. begin
  236. prev_in_args:=in_args;
  237. case l of
  238. in_new_x :
  239. begin
  240. if afterassignment or in_args then
  241. statement_syssym:=new_function
  242. else
  243. statement_syssym:=new_dispose_statement(true);
  244. end;
  245. in_dispose_x :
  246. begin
  247. statement_syssym:=new_dispose_statement(false);
  248. end;
  249. in_ord_x :
  250. begin
  251. consume(_LKLAMMER);
  252. in_args:=true;
  253. p1:=comp_expr(true);
  254. consume(_RKLAMMER);
  255. p1:=geninlinenode(in_ord_x,false,p1);
  256. statement_syssym := p1;
  257. end;
  258. in_exit :
  259. begin
  260. if try_to_consume(_LKLAMMER) then
  261. begin
  262. if not (m_mac in current_settings.modeswitches) then
  263. begin
  264. if not(try_to_consume(_RKLAMMER)) then
  265. begin
  266. p1:=comp_expr(true);
  267. consume(_RKLAMMER);
  268. if (block_type=bt_except) then
  269. begin
  270. Message(parser_e_exit_with_argument_not__possible);
  271. { recovery }
  272. p1.free;
  273. p1:=nil;
  274. end
  275. else if (not assigned(current_procinfo) or
  276. is_void(current_procinfo.procdef.returndef)) then
  277. begin
  278. Message(parser_e_void_function);
  279. { recovery }
  280. p1.free;
  281. p1:=nil;
  282. end;
  283. end
  284. else
  285. p1:=nil;
  286. end
  287. else
  288. begin
  289. if not (current_procinfo.procdef.procsym.name = pattern) then
  290. Message(parser_e_macpas_exit_wrong_param);
  291. consume(_ID);
  292. consume(_RKLAMMER);
  293. p1:=nil;
  294. end
  295. end
  296. else
  297. p1:=nil;
  298. statement_syssym:=cexitnode.create(p1);
  299. end;
  300. in_break :
  301. begin
  302. statement_syssym:=cbreaknode.create
  303. end;
  304. in_continue :
  305. begin
  306. statement_syssym:=ccontinuenode.create
  307. end;
  308. in_leave :
  309. begin
  310. if m_mac in current_settings.modeswitches then
  311. statement_syssym:=cbreaknode.create
  312. else
  313. begin
  314. Message1(sym_e_id_not_found, orgpattern);
  315. statement_syssym:=cerrornode.create;
  316. end;
  317. end;
  318. in_cycle :
  319. begin
  320. if m_mac in current_settings.modeswitches then
  321. statement_syssym:=ccontinuenode.create
  322. else
  323. begin
  324. Message1(sym_e_id_not_found, orgpattern);
  325. statement_syssym:=cerrornode.create;
  326. end;
  327. end;
  328. in_typeof_x :
  329. begin
  330. consume(_LKLAMMER);
  331. in_args:=true;
  332. p1:=comp_expr(true);
  333. consume(_RKLAMMER);
  334. if p1.nodetype=typen then
  335. ttypenode(p1).allowed:=true;
  336. { Allow classrefdef, which is required for
  337. Typeof(self) in static class methods }
  338. if not(is_objc_class_or_protocol(p1.resultdef)) and
  339. ((p1.resultdef.typ = objectdef) or
  340. (assigned(current_procinfo) and
  341. ((po_classmethod in current_procinfo.procdef.procoptions) or
  342. (po_staticmethod in current_procinfo.procdef.procoptions)) and
  343. (p1.resultdef.typ=classrefdef))) then
  344. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  345. else
  346. begin
  347. Message(parser_e_class_id_expected);
  348. p1.destroy;
  349. statement_syssym:=cerrornode.create;
  350. end;
  351. end;
  352. in_sizeof_x,
  353. in_bitsizeof_x :
  354. begin
  355. consume(_LKLAMMER);
  356. in_args:=true;
  357. p1:=comp_expr(true);
  358. consume(_RKLAMMER);
  359. if (p1.nodetype<>typen) and
  360. (
  361. (is_object(p1.resultdef) and
  362. (oo_has_constructor in tobjectdef(p1.resultdef).objectoptions)) or
  363. is_open_array(p1.resultdef) or
  364. is_array_of_const(p1.resultdef) or
  365. is_open_string(p1.resultdef)
  366. ) then
  367. begin
  368. statement_syssym:=geninlinenode(in_sizeof_x,false,p1);
  369. { no packed bit support for these things }
  370. if (l = in_bitsizeof_x) then
  371. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  372. end
  373. else
  374. begin
  375. if (l = in_sizeof_x) or
  376. (not((p1.nodetype = vecn) and
  377. is_packed_array(tvecnode(p1).left.resultdef)) and
  378. not((p1.nodetype = subscriptn) and
  379. is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
  380. begin
  381. statement_syssym:=cordconstnode.create(p1.resultdef.size,sinttype,true);
  382. if (l = in_bitsizeof_x) then
  383. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  384. end
  385. else
  386. statement_syssym:=cordconstnode.create(p1.resultdef.packedbitsize,sinttype,true);
  387. { p1 not needed !}
  388. p1.destroy;
  389. end;
  390. end;
  391. in_typeinfo_x :
  392. begin
  393. consume(_LKLAMMER);
  394. in_args:=true;
  395. p1:=comp_expr(true);
  396. { When reading a class type it is parsed as loadvmtaddrn,
  397. typeinfo only needs the type so we remove the loadvmtaddrn }
  398. if p1.nodetype=loadvmtaddrn then
  399. begin
  400. p2:=tloadvmtaddrnode(p1).left;
  401. tloadvmtaddrnode(p1).left:=nil;
  402. p1.free;
  403. p1:=p2;
  404. end;
  405. if p1.nodetype=typen then
  406. ttypenode(p1).allowed:=true;
  407. { else
  408. begin
  409. p1.destroy;
  410. p1:=cerrornode.create;
  411. Message(parser_e_illegal_parameter_list);
  412. end;}
  413. consume(_RKLAMMER);
  414. p2:=geninlinenode(in_typeinfo_x,false,p1);
  415. statement_syssym:=p2;
  416. end;
  417. {$ifdef SUPPORT_UNALIGNED}
  418. in_unaligned_x :
  419. begin
  420. err:=false;
  421. consume(_LKLAMMER);
  422. in_args:=true;
  423. p1:=comp_expr(true);
  424. p2:=ccallparanode.create(p1,nil);
  425. p2:=geninlinenode(in_unaligned_x,false,p2);
  426. consume(_RKLAMMER);
  427. statement_syssym:=p2;
  428. end;
  429. {$endif SUPPORT_UNALIGNED}
  430. in_assigned_x :
  431. begin
  432. err:=false;
  433. consume(_LKLAMMER);
  434. in_args:=true;
  435. p1:=comp_expr(true);
  436. { When reading a class type it is parsed as loadvmtaddrn,
  437. typeinfo only needs the type so we remove the loadvmtaddrn }
  438. if p1.nodetype=loadvmtaddrn then
  439. begin
  440. p2:=tloadvmtaddrnode(p1).left;
  441. tloadvmtaddrnode(p1).left:=nil;
  442. p1.free;
  443. p1:=p2;
  444. end;
  445. if not codegenerror then
  446. begin
  447. case p1.resultdef.typ of
  448. procdef, { procvar }
  449. pointerdef,
  450. procvardef,
  451. classrefdef : ;
  452. objectdef :
  453. if not is_class_or_interface_or_objc(p1.resultdef) then
  454. begin
  455. Message(parser_e_illegal_parameter_list);
  456. err:=true;
  457. end;
  458. arraydef :
  459. if not is_dynamic_array(p1.resultdef) then
  460. begin
  461. Message(parser_e_illegal_parameter_list);
  462. err:=true;
  463. end;
  464. else
  465. begin
  466. Message(parser_e_illegal_parameter_list);
  467. err:=true;
  468. end;
  469. end;
  470. end
  471. else
  472. err:=true;
  473. if not err then
  474. begin
  475. p2:=ccallparanode.create(p1,nil);
  476. p2:=geninlinenode(in_assigned_x,false,p2);
  477. end
  478. else
  479. begin
  480. p1.free;
  481. p2:=cerrornode.create;
  482. end;
  483. consume(_RKLAMMER);
  484. statement_syssym:=p2;
  485. end;
  486. in_addr_x :
  487. begin
  488. consume(_LKLAMMER);
  489. in_args:=true;
  490. p1:=comp_expr(true);
  491. p1:=caddrnode.create(p1);
  492. if cs_typed_addresses in current_settings.localswitches then
  493. include(p1.flags,nf_typedaddr);
  494. consume(_RKLAMMER);
  495. statement_syssym:=p1;
  496. end;
  497. in_ofs_x :
  498. begin
  499. consume(_LKLAMMER);
  500. in_args:=true;
  501. p1:=comp_expr(true);
  502. p1:=caddrnode.create(p1);
  503. do_typecheckpass(p1);
  504. { Ofs() returns a cardinal/qword, not a pointer }
  505. p1.resultdef:=uinttype;
  506. consume(_RKLAMMER);
  507. statement_syssym:=p1;
  508. end;
  509. in_seg_x :
  510. begin
  511. consume(_LKLAMMER);
  512. in_args:=true;
  513. p1:=comp_expr(true);
  514. p1:=geninlinenode(in_seg_x,false,p1);
  515. consume(_RKLAMMER);
  516. statement_syssym:=p1;
  517. end;
  518. in_high_x,
  519. in_low_x :
  520. begin
  521. consume(_LKLAMMER);
  522. in_args:=true;
  523. p1:=comp_expr(true);
  524. p2:=geninlinenode(l,false,p1);
  525. consume(_RKLAMMER);
  526. statement_syssym:=p2;
  527. end;
  528. in_succ_x,
  529. in_pred_x :
  530. begin
  531. consume(_LKLAMMER);
  532. in_args:=true;
  533. p1:=comp_expr(true);
  534. p2:=geninlinenode(l,false,p1);
  535. consume(_RKLAMMER);
  536. statement_syssym:=p2;
  537. end;
  538. in_inc_x,
  539. in_dec_x :
  540. begin
  541. consume(_LKLAMMER);
  542. in_args:=true;
  543. p1:=comp_expr(true);
  544. if try_to_consume(_COMMA) then
  545. p2:=ccallparanode.create(comp_expr(true),nil)
  546. else
  547. p2:=nil;
  548. p2:=ccallparanode.create(p1,p2);
  549. statement_syssym:=geninlinenode(l,false,p2);
  550. consume(_RKLAMMER);
  551. end;
  552. in_slice_x:
  553. begin
  554. if not(in_args) then
  555. begin
  556. message(parser_e_illegal_slice);
  557. consume(_LKLAMMER);
  558. in_args:=true;
  559. comp_expr(true).free;
  560. if try_to_consume(_COMMA) then
  561. comp_expr(true).free;
  562. statement_syssym:=cerrornode.create;
  563. consume(_RKLAMMER);
  564. end
  565. else
  566. begin
  567. consume(_LKLAMMER);
  568. in_args:=true;
  569. p1:=comp_expr(true);
  570. Consume(_COMMA);
  571. if not(codegenerror) then
  572. p2:=ccallparanode.create(comp_expr(true),nil)
  573. else
  574. p2:=cerrornode.create;
  575. p2:=ccallparanode.create(p1,p2);
  576. statement_syssym:=geninlinenode(l,false,p2);
  577. consume(_RKLAMMER);
  578. end;
  579. end;
  580. in_initialize_x:
  581. begin
  582. statement_syssym:=inline_initialize;
  583. end;
  584. in_finalize_x:
  585. begin
  586. statement_syssym:=inline_finalize;
  587. end;
  588. in_copy_x:
  589. begin
  590. statement_syssym:=inline_copy;
  591. end;
  592. in_concat_x :
  593. begin
  594. consume(_LKLAMMER);
  595. in_args:=true;
  596. { Translate to x:=x+y[+z]. The addnode will do the
  597. type checking }
  598. p2:=nil;
  599. repeat
  600. p1:=comp_expr(true);
  601. if p2<>nil then
  602. p2:=caddnode.create(addn,p2,p1)
  603. else
  604. begin
  605. { Force string type if it isn't yet }
  606. if not(
  607. (p1.resultdef.typ=stringdef) or
  608. is_chararray(p1.resultdef) or
  609. is_char(p1.resultdef)
  610. ) then
  611. inserttypeconv(p1,cshortstringtype);
  612. p2:=p1;
  613. end;
  614. until not try_to_consume(_COMMA);
  615. consume(_RKLAMMER);
  616. statement_syssym:=p2;
  617. end;
  618. in_read_x,
  619. in_readln_x,
  620. in_readstr_x:
  621. begin
  622. if try_to_consume(_LKLAMMER) then
  623. begin
  624. paras:=parse_paras(false,false,_RKLAMMER);
  625. consume(_RKLAMMER);
  626. end
  627. else
  628. paras:=nil;
  629. p1:=geninlinenode(l,false,paras);
  630. statement_syssym := p1;
  631. end;
  632. in_setlength_x:
  633. begin
  634. statement_syssym := inline_setlength;
  635. end;
  636. in_objc_selector_x:
  637. begin
  638. if (m_objectivec1 in current_settings.modeswitches) then
  639. begin
  640. consume(_LKLAMMER);
  641. in_args:=true;
  642. { don't turn procsyms into calls (getaddr = true) }
  643. p1:=factor(true);
  644. p2:=geninlinenode(l,false,p1);
  645. consume(_RKLAMMER);
  646. statement_syssym:=p2;
  647. end
  648. else
  649. begin
  650. Message1(sym_e_id_not_found, orgpattern);
  651. statement_syssym:=cerrornode.create;
  652. end;
  653. end;
  654. in_length_x:
  655. begin
  656. consume(_LKLAMMER);
  657. in_args:=true;
  658. p1:=comp_expr(true);
  659. p2:=geninlinenode(l,false,p1);
  660. consume(_RKLAMMER);
  661. statement_syssym:=p2;
  662. end;
  663. in_write_x,
  664. in_writeln_x,
  665. in_writestr_x :
  666. begin
  667. if try_to_consume(_LKLAMMER) then
  668. begin
  669. paras:=parse_paras(true,false,_RKLAMMER);
  670. consume(_RKLAMMER);
  671. end
  672. else
  673. paras:=nil;
  674. p1 := geninlinenode(l,false,paras);
  675. statement_syssym := p1;
  676. end;
  677. in_str_x_string :
  678. begin
  679. consume(_LKLAMMER);
  680. paras:=parse_paras(true,false,_RKLAMMER);
  681. consume(_RKLAMMER);
  682. p1 := geninlinenode(l,false,paras);
  683. statement_syssym := p1;
  684. end;
  685. in_val_x:
  686. Begin
  687. consume(_LKLAMMER);
  688. in_args := true;
  689. p1:= ccallparanode.create(comp_expr(true), nil);
  690. consume(_COMMA);
  691. p2 := ccallparanode.create(comp_expr(true),p1);
  692. if try_to_consume(_COMMA) then
  693. p2 := ccallparanode.create(comp_expr(true),p2);
  694. consume(_RKLAMMER);
  695. p2 := geninlinenode(l,false,p2);
  696. statement_syssym := p2;
  697. End;
  698. in_include_x_y,
  699. in_exclude_x_y :
  700. begin
  701. consume(_LKLAMMER);
  702. in_args:=true;
  703. p1:=comp_expr(true);
  704. consume(_COMMA);
  705. p2:=comp_expr(true);
  706. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  707. consume(_RKLAMMER);
  708. end;
  709. in_pack_x_y_z,
  710. in_unpack_x_y_z :
  711. begin
  712. consume(_LKLAMMER);
  713. in_args:=true;
  714. p1:=comp_expr(true);
  715. consume(_COMMA);
  716. p2:=comp_expr(true);
  717. consume(_COMMA);
  718. paras:=comp_expr(true);
  719. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,ccallparanode.create(paras,nil))));
  720. consume(_RKLAMMER);
  721. end;
  722. in_assert_x_y :
  723. begin
  724. consume(_LKLAMMER);
  725. in_args:=true;
  726. p1:=comp_expr(true);
  727. if try_to_consume(_COMMA) then
  728. p2:=comp_expr(true)
  729. else
  730. begin
  731. { then insert an empty string }
  732. p2:=cstringconstnode.createstr('');
  733. end;
  734. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  735. consume(_RKLAMMER);
  736. end;
  737. in_get_frame:
  738. begin
  739. statement_syssym:=geninlinenode(l,false,nil);
  740. end;
  741. (*
  742. in_get_caller_frame:
  743. begin
  744. if try_to_consume(_LKLAMMER) then
  745. begin
  746. {You used to call get_caller_frame as get_caller_frame(get_frame),
  747. however, as a stack frame may not exist, it does more harm than
  748. good, so ignore it.}
  749. in_args:=true;
  750. p1:=comp_expr(true);
  751. p1.destroy;
  752. consume(_RKLAMMER);
  753. end;
  754. statement_syssym:=geninlinenode(l,false,nil);
  755. end;
  756. *)
  757. else
  758. internalerror(15);
  759. end;
  760. in_args:=prev_in_args;
  761. end;
  762. function maybe_load_methodpointer(st:TSymtable;var p1:tnode):boolean;
  763. begin
  764. maybe_load_methodpointer:=false;
  765. if not assigned(p1) then
  766. begin
  767. case st.symtabletype of
  768. withsymtable :
  769. begin
  770. if (st.defowner.typ=objectdef) then
  771. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  772. end;
  773. ObjectSymtable :
  774. begin
  775. p1:=load_self_node;
  776. { We are calling a member }
  777. maybe_load_methodpointer:=true;
  778. end;
  779. end;
  780. end;
  781. end;
  782. { reads the parameter for a subroutine call }
  783. procedure do_proc_call(sym:tsym;st:TSymtable;obj:tobjectdef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  784. var
  785. membercall,
  786. prevafterassn : boolean;
  787. i : integer;
  788. para,p2 : tnode;
  789. currpara : tparavarsym;
  790. aprocdef : tprocdef;
  791. begin
  792. prevafterassn:=afterassignment;
  793. afterassignment:=false;
  794. membercall:=false;
  795. aprocdef:=nil;
  796. { when it is a call to a member we need to load the
  797. methodpointer first
  798. (except if we are in a static class method)
  799. }
  800. membercall:=
  801. not(assigned(current_procinfo) and
  802. ([po_staticmethod,po_classmethod] <= current_procinfo.procdef.procoptions)) and
  803. maybe_load_methodpointer(st,p1);
  804. { When we are expecting a procvar we also need
  805. to get the address in some cases }
  806. if assigned(getprocvardef) then
  807. begin
  808. if (block_type=bt_const) or
  809. getaddr then
  810. begin
  811. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  812. getaddr:=true;
  813. end
  814. else
  815. if (m_tp_procvar in current_settings.modeswitches) or
  816. (m_mac_procvar in current_settings.modeswitches) then
  817. begin
  818. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  819. if assigned(aprocdef) then
  820. getaddr:=true;
  821. end;
  822. end;
  823. { only need to get the address of the procedure? }
  824. if getaddr then
  825. begin
  826. { Retrieve info which procvar to call. For tp_procvar the
  827. aprocdef is already loaded above so we can reuse it }
  828. if not assigned(aprocdef) and
  829. assigned(getprocvardef) then
  830. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  831. { generate a methodcallnode or proccallnode }
  832. { we shouldn't convert things like @tcollection.load }
  833. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  834. if assigned(p1) then
  835. begin
  836. { for loading methodpointer of an inherited function
  837. we use self as instance and load the address of
  838. the function directly and not through the vmt (PFV) }
  839. if (cnf_inherited in callflags) then
  840. begin
  841. include(p2.flags,nf_inherited);
  842. p1.free;
  843. p1:=load_self_node;
  844. end;
  845. if (p1.nodetype<>typen) then
  846. tloadnode(p2).set_mp(p1)
  847. else
  848. p1.free;
  849. end;
  850. p1:=p2;
  851. { no postfix operators }
  852. again:=false;
  853. end
  854. else
  855. begin
  856. para:=nil;
  857. if anon_inherited then
  858. begin
  859. if not assigned(current_procinfo) then
  860. internalerror(200305054);
  861. for i:=0 to current_procinfo.procdef.paras.count-1 do
  862. begin
  863. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  864. if not(vo_is_hidden_para in currpara.varoptions) then
  865. begin
  866. { inheritance by msgint? }
  867. if assigned(srdef) then
  868. { anonymous inherited via msgid calls only require a var parameter for
  869. both methods, so we need some type casting here }
  870. para:=ccallparanode.create(ctypeconvnode.create_internal(ctypeconvnode.create_internal(
  871. cloadnode.create(currpara,currpara.owner),cformaltype),tparavarsym(tprocdef(srdef).paras[i]).vardef),
  872. para)
  873. else
  874. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  875. end;
  876. end;
  877. end
  878. else
  879. begin
  880. if try_to_consume(_LKLAMMER) then
  881. begin
  882. para:=parse_paras(false,false,_RKLAMMER);
  883. consume(_RKLAMMER);
  884. end;
  885. end;
  886. { indicate if this call was generated by a member and
  887. no explicit self is used, this is needed to determine
  888. how to handle a destructor call (PFV) }
  889. if membercall then
  890. include(callflags,cnf_member_call);
  891. if assigned(obj) then
  892. begin
  893. if (st.symtabletype<>ObjectSymtable) then
  894. internalerror(200310031);
  895. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  896. end
  897. else
  898. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  899. end;
  900. afterassignment:=prevafterassn;
  901. end;
  902. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  903. var
  904. hp,hp2 : tnode;
  905. hpp : ^tnode;
  906. currprocdef : tprocdef;
  907. begin
  908. if not assigned(pv) then
  909. internalerror(200301121);
  910. if (m_tp_procvar in current_settings.modeswitches) or
  911. (m_mac_procvar in current_settings.modeswitches) then
  912. begin
  913. hp:=p2;
  914. hpp:=@p2;
  915. while assigned(hp) and
  916. (hp.nodetype=typeconvn) do
  917. begin
  918. hp:=ttypeconvnode(hp).left;
  919. { save orignal address of the old tree so we can replace the node }
  920. hpp:=@hp;
  921. end;
  922. if (hp.nodetype=calln) and
  923. { a procvar can't have parameters! }
  924. not assigned(tcallnode(hp).left) then
  925. begin
  926. currprocdef:=tcallnode(hp).symtableprocentry.Find_procdef_byprocvardef(pv);
  927. if assigned(currprocdef) then
  928. begin
  929. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  930. if (po_methodpointer in pv.procoptions) then
  931. tloadnode(hp2).set_mp(tcallnode(hp).methodpointer.getcopy);
  932. hp.destroy;
  933. { replace the old callnode with the new loadnode }
  934. hpp^:=hp2;
  935. end;
  936. end;
  937. end;
  938. end;
  939. { the following procedure handles the access to a property symbol }
  940. procedure handle_propertysym(propsym : tpropertysym;st : TSymtable;var p1 : tnode);
  941. var
  942. paras : tnode;
  943. p2 : tnode;
  944. membercall : boolean;
  945. callflags : tcallnodeflags;
  946. propaccesslist : tpropaccesslist;
  947. function getpropaccesslist(pap:tpropaccesslisttypes):boolean;
  948. var
  949. hpropsym : tpropertysym;
  950. begin
  951. result:=false;
  952. { find property in the overriden list }
  953. hpropsym:=propsym;
  954. repeat
  955. propaccesslist:=hpropsym.propaccesslist[pap];
  956. if not propaccesslist.empty then
  957. begin
  958. result:=true;
  959. exit;
  960. end;
  961. hpropsym:=hpropsym.overridenpropsym;
  962. until not assigned(hpropsym);
  963. end;
  964. begin
  965. { property parameters? read them only if the property really }
  966. { has parameters }
  967. paras:=nil;
  968. if (ppo_hasparameters in propsym.propoptions) then
  969. begin
  970. if try_to_consume(_LECKKLAMMER) then
  971. begin
  972. paras:=parse_paras(false,false,_RECKKLAMMER);
  973. consume(_RECKKLAMMER);
  974. end;
  975. end;
  976. { indexed property }
  977. if (ppo_indexed in propsym.propoptions) then
  978. begin
  979. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  980. paras:=ccallparanode.create(p2,paras);
  981. end;
  982. { we need only a write property if a := follows }
  983. { if not(afterassignment) and not(in_args) then }
  984. if token=_ASSIGNMENT then
  985. begin
  986. if getpropaccesslist(palt_write) then
  987. begin
  988. case propaccesslist.firstsym^.sym.typ of
  989. procsym :
  990. begin
  991. callflags:=[];
  992. { generate the method call }
  993. membercall:=maybe_load_methodpointer(st,p1);
  994. if membercall then
  995. include(callflags,cnf_member_call);
  996. p1:=ccallnode.create(paras,tprocsym(propaccesslist.firstsym^.sym),st,p1,callflags);
  997. addsymref(propaccesslist.firstsym^.sym);
  998. paras:=nil;
  999. consume(_ASSIGNMENT);
  1000. { read the expression }
  1001. if propsym.propdef.typ=procvardef then
  1002. getprocvardef:=tprocvardef(propsym.propdef);
  1003. p2:=comp_expr(true);
  1004. if assigned(getprocvardef) then
  1005. handle_procvar(getprocvardef,p2);
  1006. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1007. { mark as property, both the tcallnode and the real call block }
  1008. include(p1.flags,nf_isproperty);
  1009. getprocvardef:=nil;
  1010. end;
  1011. fieldvarsym :
  1012. begin
  1013. { generate access code }
  1014. propaccesslist_to_node(p1,st,propaccesslist);
  1015. include(p1.flags,nf_isproperty);
  1016. consume(_ASSIGNMENT);
  1017. { read the expression }
  1018. p2:=comp_expr(true);
  1019. p1:=cassignmentnode.create(p1,p2);
  1020. end
  1021. else
  1022. begin
  1023. p1:=cerrornode.create;
  1024. Message(parser_e_no_procedure_to_access_property);
  1025. end;
  1026. end;
  1027. end
  1028. else
  1029. begin
  1030. p1:=cerrornode.create;
  1031. Message(parser_e_no_procedure_to_access_property);
  1032. end;
  1033. end
  1034. else
  1035. begin
  1036. if getpropaccesslist(palt_read) then
  1037. begin
  1038. case propaccesslist.firstsym^.sym.typ of
  1039. fieldvarsym :
  1040. begin
  1041. { generate access code }
  1042. propaccesslist_to_node(p1,st,propaccesslist);
  1043. include(p1.flags,nf_isproperty);
  1044. end;
  1045. procsym :
  1046. begin
  1047. callflags:=[];
  1048. { generate the method call }
  1049. membercall:=maybe_load_methodpointer(st,p1);
  1050. if membercall then
  1051. include(callflags,cnf_member_call);
  1052. p1:=ccallnode.create(paras,tprocsym(propaccesslist.firstsym^.sym),st,p1,callflags);
  1053. paras:=nil;
  1054. include(p1.flags,nf_isproperty);
  1055. end
  1056. else
  1057. begin
  1058. p1:=cerrornode.create;
  1059. Message(type_e_mismatch);
  1060. end;
  1061. end;
  1062. end
  1063. else
  1064. begin
  1065. { error, no function to read property }
  1066. p1:=cerrornode.create;
  1067. Message(parser_e_no_procedure_to_access_property);
  1068. end;
  1069. end;
  1070. { release paras if not used }
  1071. if assigned(paras) then
  1072. paras.free;
  1073. end;
  1074. { the ID token has to be consumed before calling this function }
  1075. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  1076. var
  1077. static_name : string;
  1078. isclassref : boolean;
  1079. srsymtable : TSymtable;
  1080. begin
  1081. if sym=nil then
  1082. begin
  1083. { pattern is still valid unless
  1084. there is another ID just after the ID of sym }
  1085. Message1(sym_e_id_no_member,orgpattern);
  1086. p1.free;
  1087. p1:=cerrornode.create;
  1088. { try to clean up }
  1089. again:=false;
  1090. end
  1091. else
  1092. begin
  1093. if assigned(p1) then
  1094. begin
  1095. if not assigned(p1.resultdef) then
  1096. do_typecheckpass(p1);
  1097. isclassref:=(p1.resultdef.typ=classrefdef);
  1098. end
  1099. else
  1100. isclassref:=false;
  1101. { we assume, that only procsyms and varsyms are in an object }
  1102. { symbol table, for classes, properties are allowed }
  1103. case sym.typ of
  1104. procsym:
  1105. begin
  1106. do_proc_call(sym,sym.owner,classh,
  1107. (getaddr and not(token in [_CARET,_POINT])),
  1108. again,p1,callflags);
  1109. { we need to know which procedure is called }
  1110. do_typecheckpass(p1);
  1111. { calling using classref? }
  1112. if isclassref and
  1113. (p1.nodetype=calln) and
  1114. assigned(tcallnode(p1).procdefinition) and
  1115. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1116. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1117. Message(parser_e_only_class_methods_via_class_ref);
  1118. end;
  1119. fieldvarsym:
  1120. begin
  1121. if (sp_static in sym.symoptions) then
  1122. begin
  1123. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1124. searchsym(static_name,sym,srsymtable);
  1125. if assigned(sym) then
  1126. check_hints(sym,sym.symoptions);
  1127. p1.free;
  1128. p1:=cloadnode.create(sym,srsymtable);
  1129. end
  1130. else
  1131. begin
  1132. if isclassref then
  1133. if assigned(p1) and
  1134. is_self_node(p1) then
  1135. Message(parser_e_only_class_methods)
  1136. else
  1137. Message(parser_e_only_class_methods_via_class_ref);
  1138. p1:=csubscriptnode.create(sym,p1);
  1139. end;
  1140. end;
  1141. propertysym:
  1142. begin
  1143. if isclassref then
  1144. Message(parser_e_only_class_methods_via_class_ref);
  1145. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1146. end;
  1147. typesym:
  1148. begin
  1149. p1:=ctypenode.create(ttypesym(sym).typedef);
  1150. end;
  1151. else internalerror(16);
  1152. end;
  1153. end;
  1154. end;
  1155. {****************************************************************************
  1156. Factor
  1157. ****************************************************************************}
  1158. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  1159. out memberparentdef: tdef): boolean;
  1160. var
  1161. hdef : tdef;
  1162. begin
  1163. result:=true;
  1164. memberparentdef:=nil;
  1165. case st.symtabletype of
  1166. ObjectSymtable:
  1167. begin
  1168. memberparentdef:=tdef(st.defowner);
  1169. exit;
  1170. end;
  1171. WithSymtable:
  1172. begin
  1173. if assigned(p1) then
  1174. internalerror(2007012002);
  1175. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  1176. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1177. if not(hdef.typ in [objectdef,classrefdef]) then
  1178. exit;
  1179. if (hdef.typ=classrefdef) then
  1180. hdef:=tclassrefdef(hdef).pointeddef;
  1181. memberparentdef:=hdef;
  1182. end;
  1183. else
  1184. result:=false;
  1185. end;
  1186. end;
  1187. {$maxfpuregisters 0}
  1188. function factor(getaddr : boolean) : tnode;
  1189. {---------------------------------------------
  1190. Factor_read_id
  1191. ---------------------------------------------}
  1192. procedure factor_read_id(var p1:tnode;var again:boolean);
  1193. var
  1194. pc : pchar;
  1195. srsym : tsym;
  1196. unit_found : boolean;
  1197. srsymtable : TSymtable;
  1198. hdef : tdef;
  1199. orgstoredpattern,
  1200. storedpattern : string;
  1201. len : longint;
  1202. t : ttoken;
  1203. begin
  1204. { allow post fix operators }
  1205. again:=true;
  1206. { first check for identifier }
  1207. if token<>_ID then
  1208. begin
  1209. srsym:=generrorsym;
  1210. srsymtable:=nil;
  1211. consume(_ID);
  1212. end
  1213. else
  1214. begin
  1215. searchsym(pattern,srsym,srsymtable);
  1216. { handle unit specification like System.Writeln }
  1217. unit_found:=try_consume_unitsym(srsym,srsymtable,t);
  1218. storedpattern:=pattern;
  1219. orgstoredpattern:=orgpattern;
  1220. consume(t);
  1221. { named parameter support }
  1222. found_arg_name:=false;
  1223. if not(unit_found) and
  1224. named_args_allowed and
  1225. (token=_ASSIGNMENT) then
  1226. begin
  1227. found_arg_name:=true;
  1228. p1:=cstringconstnode.createstr(storedpattern);
  1229. consume(_ASSIGNMENT);
  1230. exit;
  1231. end;
  1232. { if nothing found give error and return errorsym }
  1233. if assigned(srsym) then
  1234. check_hints(srsym,srsym.symoptions)
  1235. else
  1236. begin
  1237. identifier_not_found(orgstoredpattern);
  1238. srsym:=generrorsym;
  1239. srsymtable:=nil;
  1240. end;
  1241. end;
  1242. { Access to funcret or need to call the function? }
  1243. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1244. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1245. (
  1246. (token=_LKLAMMER) or
  1247. (
  1248. (
  1249. (m_tp7 in current_settings.modeswitches) or
  1250. (m_delphi in current_settings.modeswitches)
  1251. ) and
  1252. (afterassignment or in_args) and
  1253. not(vo_is_result in tabstractvarsym(srsym).varoptions)
  1254. )
  1255. ) then
  1256. begin
  1257. hdef:=tdef(srsym.owner.defowner);
  1258. if assigned(hdef) and
  1259. (hdef.typ=procdef) then
  1260. srsym:=tprocdef(hdef).procsym
  1261. else
  1262. begin
  1263. Message(parser_e_illegal_expression);
  1264. srsym:=generrorsym;
  1265. end;
  1266. srsymtable:=srsym.owner;
  1267. end;
  1268. begin
  1269. case srsym.typ of
  1270. absolutevarsym :
  1271. begin
  1272. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1273. begin
  1274. p1:=nil;
  1275. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1276. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  1277. include(p1.flags,nf_absolute);
  1278. end
  1279. else
  1280. p1:=cloadnode.create(srsym,srsymtable);
  1281. end;
  1282. staticvarsym,
  1283. localvarsym,
  1284. paravarsym,
  1285. fieldvarsym :
  1286. begin
  1287. { check if we are reading a field of an object/class/ }
  1288. { record. is_member_read() will deal with withsymtables }
  1289. { if needed. }
  1290. if is_member_read(srsym,srsymtable,p1,hdef) then
  1291. begin
  1292. { if the field was originally found in an }
  1293. { objectsymtable, it means it's part of self }
  1294. if (srsymtable.symtabletype=ObjectSymtable) then
  1295. p1:=load_self_node;
  1296. { now, if the field itself is part of an objectsymtab }
  1297. { (it can be even if it was found in a withsymtable, }
  1298. { e.g., "with classinstance do field := 5"), then }
  1299. { let do_member_read handle it }
  1300. if (srsym.owner.symtabletype=ObjectSymtable) then
  1301. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1302. else
  1303. { otherwise it's a regular record subscript }
  1304. p1:=csubscriptnode.create(srsym,p1);
  1305. end
  1306. else
  1307. { regular non-field load }
  1308. p1:=cloadnode.create(srsym,srsymtable);
  1309. end;
  1310. syssym :
  1311. begin
  1312. p1:=statement_syssym(tsyssym(srsym).number);
  1313. end;
  1314. typesym :
  1315. begin
  1316. hdef:=ttypesym(srsym).typedef;
  1317. if not assigned(hdef) then
  1318. begin
  1319. again:=false;
  1320. end
  1321. else
  1322. begin
  1323. { We need to know if this unit uses Variants }
  1324. if (hdef=cvarianttype) and
  1325. not(cs_compilesystem in current_settings.moduleswitches) then
  1326. current_module.flags:=current_module.flags or uf_uses_variants;
  1327. if try_to_consume(_LKLAMMER) then
  1328. begin
  1329. p1:=comp_expr(true);
  1330. consume(_RKLAMMER);
  1331. p1:=ctypeconvnode.create_explicit(p1,hdef);
  1332. end
  1333. else { not LKLAMMER }
  1334. if (token=_POINT) and
  1335. is_object(hdef) then
  1336. begin
  1337. consume(_POINT);
  1338. if assigned(current_objectdef) and
  1339. not(getaddr) then
  1340. begin
  1341. if current_objectdef.is_related(tobjectdef(hdef)) then
  1342. begin
  1343. p1:=ctypenode.create(hdef);
  1344. { search also in inherited methods }
  1345. searchsym_in_class(tobjectdef(hdef),current_objectdef,pattern,srsym,srsymtable);
  1346. if assigned(srsym) then
  1347. check_hints(srsym,srsym.symoptions);
  1348. consume(_ID);
  1349. do_member_read(tobjectdef(hdef),false,srsym,p1,again,[]);
  1350. end
  1351. else
  1352. begin
  1353. Message(parser_e_no_super_class);
  1354. again:=false;
  1355. end;
  1356. end
  1357. else
  1358. begin
  1359. { allows @TObject.Load }
  1360. { also allows static methods and variables }
  1361. p1:=ctypenode.create(hdef);
  1362. { TP allows also @TMenu.Load if Load is only }
  1363. { defined in an anchestor class }
  1364. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1365. if assigned(srsym) then
  1366. begin
  1367. check_hints(srsym,srsym.symoptions);
  1368. if not(getaddr) and not(sp_static in srsym.symoptions) then
  1369. Message(sym_e_only_static_in_static)
  1370. else
  1371. begin
  1372. consume(_ID);
  1373. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1374. end;
  1375. end
  1376. else
  1377. Message1(sym_e_id_no_member,orgpattern);
  1378. end;
  1379. end
  1380. else
  1381. begin
  1382. { class reference ? }
  1383. if is_class(hdef) then
  1384. begin
  1385. if getaddr and (token=_POINT) then
  1386. begin
  1387. consume(_POINT);
  1388. { allows @Object.Method }
  1389. { also allows static methods and variables }
  1390. p1:=ctypenode.create(hdef);
  1391. { TP allows also @TMenu.Load if Load is only }
  1392. { defined in an anchestor class }
  1393. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1394. if assigned(srsym) then
  1395. begin
  1396. check_hints(srsym,srsym.symoptions);
  1397. consume(_ID);
  1398. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1399. end
  1400. else
  1401. begin
  1402. Message1(sym_e_id_no_member,orgpattern);
  1403. consume(_ID);
  1404. end;
  1405. end
  1406. else
  1407. begin
  1408. p1:=ctypenode.create(hdef);
  1409. { For a type block we simply return only
  1410. the type. For all other blocks we return
  1411. a loadvmt node }
  1412. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1413. p1:=cloadvmtaddrnode.create(p1);
  1414. end;
  1415. end
  1416. else
  1417. p1:=ctypenode.create(hdef);
  1418. end;
  1419. end;
  1420. end;
  1421. enumsym :
  1422. begin
  1423. p1:=genenumnode(tenumsym(srsym));
  1424. end;
  1425. constsym :
  1426. begin
  1427. case tconstsym(srsym).consttyp of
  1428. constord :
  1429. begin
  1430. if tconstsym(srsym).constdef=nil then
  1431. internalerror(200403232);
  1432. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).constdef,true);
  1433. end;
  1434. conststring :
  1435. begin
  1436. len:=tconstsym(srsym).value.len;
  1437. if not(cs_ansistrings in current_settings.localswitches) and (len>255) then
  1438. len:=255;
  1439. getmem(pc,len+1);
  1440. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1441. pc[len]:=#0;
  1442. p1:=cstringconstnode.createpchar(pc,len);
  1443. end;
  1444. constwstring :
  1445. p1:=cstringconstnode.createwstr(pcompilerwidestring(tconstsym(srsym).value.valueptr));
  1446. constreal :
  1447. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1448. constset :
  1449. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).constdef);
  1450. constpointer :
  1451. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).constdef);
  1452. constnil :
  1453. p1:=cnilnode.create;
  1454. constresourcestring:
  1455. begin
  1456. p1:=cloadnode.create(srsym,srsymtable);
  1457. do_typecheckpass(p1);
  1458. p1.resultdef:=cansistringtype;
  1459. end;
  1460. constguid :
  1461. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1462. else
  1463. internalerror(200507181);
  1464. end;
  1465. end;
  1466. procsym :
  1467. begin
  1468. { check if it's a method/class method }
  1469. if is_member_read(srsym,srsymtable,p1,hdef) then
  1470. begin
  1471. { not srsymtable.symtabletype since that can be }
  1472. { withsymtable as well }
  1473. if (srsym.owner.symtabletype=ObjectSymtable) then
  1474. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1475. else
  1476. { no procsyms in records (yet) }
  1477. internalerror(2007012006);
  1478. end
  1479. else
  1480. { regular procedure/function call }
  1481. do_proc_call(srsym,srsymtable,nil,
  1482. (getaddr and not(token in [_CARET,_POINT])),
  1483. again,p1,[]);
  1484. end;
  1485. propertysym :
  1486. begin
  1487. { property of a class/object? }
  1488. if is_member_read(srsym,srsymtable,p1,hdef) then
  1489. begin
  1490. if (srsymtable.symtabletype=ObjectSymtable) then
  1491. p1:=load_self_node;
  1492. { not srsymtable.symtabletype since that can be }
  1493. { withsymtable as well }
  1494. if (srsym.owner.symtabletype=ObjectSymtable) then
  1495. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1496. else
  1497. { no propertysyms in records (yet) }
  1498. internalerror(2007012006);
  1499. end
  1500. else
  1501. { no method pointer }
  1502. begin
  1503. p1:=nil;
  1504. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  1505. end;
  1506. end;
  1507. labelsym :
  1508. begin
  1509. { Support @label }
  1510. if getaddr then
  1511. begin
  1512. if srsym.owner<>current_procinfo.procdef.localst then
  1513. CGMessage(parser_e_label_outside_proc);
  1514. p1:=cloadnode.create(srsym,srsym.owner)
  1515. end
  1516. else
  1517. begin
  1518. consume(_COLON);
  1519. if tlabelsym(srsym).defined then
  1520. Message(sym_e_label_already_defined);
  1521. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1522. begin
  1523. tlabelsym(srsym).nonlocal:=true;
  1524. exclude(current_procinfo.procdef.procoptions,po_inline);
  1525. end;
  1526. tlabelsym(srsym).defined:=true;
  1527. p1:=clabelnode.create(nil,tlabelsym(srsym));
  1528. tlabelsym(srsym).code:=p1;
  1529. end;
  1530. end;
  1531. errorsym :
  1532. begin
  1533. p1:=cerrornode.create;
  1534. if try_to_consume(_LKLAMMER) then
  1535. begin
  1536. parse_paras(false,false,_RKLAMMER);
  1537. consume(_RKLAMMER);
  1538. end;
  1539. end;
  1540. else
  1541. begin
  1542. p1:=cerrornode.create;
  1543. Message(parser_e_illegal_expression);
  1544. end;
  1545. end; { end case }
  1546. end;
  1547. end;
  1548. {---------------------------------------------
  1549. Factor_Read_Set
  1550. ---------------------------------------------}
  1551. { Read a set between [] }
  1552. function factor_read_set:tnode;
  1553. var
  1554. p1,p2 : tnode;
  1555. lastp,
  1556. buildp : tarrayconstructornode;
  1557. begin
  1558. buildp:=nil;
  1559. { be sure that a least one arrayconstructn is used, also for an
  1560. empty [] }
  1561. if token=_RECKKLAMMER then
  1562. buildp:=carrayconstructornode.create(nil,buildp)
  1563. else
  1564. repeat
  1565. p1:=comp_expr(true);
  1566. if try_to_consume(_POINTPOINT) then
  1567. begin
  1568. p2:=comp_expr(true);
  1569. p1:=carrayconstructorrangenode.create(p1,p2);
  1570. end;
  1571. { insert at the end of the tree, to get the correct order }
  1572. if not assigned(buildp) then
  1573. begin
  1574. buildp:=carrayconstructornode.create(p1,nil);
  1575. lastp:=buildp;
  1576. end
  1577. else
  1578. begin
  1579. lastp.right:=carrayconstructornode.create(p1,nil);
  1580. lastp:=tarrayconstructornode(lastp.right);
  1581. end;
  1582. { there could be more elements }
  1583. until not try_to_consume(_COMMA);
  1584. factor_read_set:=buildp;
  1585. end;
  1586. {---------------------------------------------
  1587. PostFixOperators
  1588. ---------------------------------------------}
  1589. procedure postfixoperators(var p1:tnode;var again:boolean);
  1590. { tries to avoid syntax errors after invalid qualifiers }
  1591. procedure recoverconsume_postfixops;
  1592. begin
  1593. repeat
  1594. if not try_to_consume(_CARET) then
  1595. if try_to_consume(_POINT) then
  1596. try_to_consume(_ID)
  1597. else if try_to_consume(_LECKKLAMMER) then
  1598. begin
  1599. repeat
  1600. comp_expr(true);
  1601. until not try_to_consume(_COMMA);
  1602. consume(_RECKKLAMMER);
  1603. end
  1604. else if try_to_consume(_LKLAMMER) then
  1605. begin
  1606. repeat
  1607. comp_expr(true);
  1608. until not try_to_consume(_COMMA);
  1609. consume(_RKLAMMER);
  1610. end
  1611. else
  1612. break;
  1613. until false;
  1614. end;
  1615. procedure handle_variantarray;
  1616. var
  1617. p4 : tnode;
  1618. newstatement : tstatementnode;
  1619. tempresultvariant,
  1620. temp : ttempcreatenode;
  1621. paras : tcallparanode;
  1622. newblock : tnode;
  1623. countindices : aint;
  1624. begin
  1625. { create statements with call initialize the arguments and
  1626. call fpc_dynarr_setlength }
  1627. newblock:=internalstatements(newstatement);
  1628. { get temp for array of indicies,
  1629. we set the real size later }
  1630. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1631. addstatement(newstatement,temp);
  1632. countindices:=0;
  1633. repeat
  1634. p4:=comp_expr(true);
  1635. addstatement(newstatement,cassignmentnode.create(
  1636. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1637. inc(countindices);
  1638. until not try_to_consume(_COMMA);
  1639. { set real size }
  1640. temp.size:=countindices*s32inttype.size;
  1641. consume(_RECKKLAMMER);
  1642. { we need only a write access if a := follows }
  1643. if token=_ASSIGNMENT then
  1644. begin
  1645. consume(_ASSIGNMENT);
  1646. p4:=comp_expr(true);
  1647. { create call to fpc_vararray_put }
  1648. paras:=ccallparanode.create(cordconstnode.create
  1649. (countindices,s32inttype,true),
  1650. ccallparanode.create(caddrnode.create_internal
  1651. (ctemprefnode.create(temp)),
  1652. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1653. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1654. ,nil))));
  1655. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1656. addstatement(newstatement,ctempdeletenode.create(temp));
  1657. end
  1658. else
  1659. begin
  1660. { create temp for result }
  1661. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1662. addstatement(newstatement,tempresultvariant);
  1663. { create call to fpc_vararray_get }
  1664. paras:=ccallparanode.create(cordconstnode.create
  1665. (countindices,s32inttype,true),
  1666. ccallparanode.create(caddrnode.create_internal
  1667. (ctemprefnode.create(temp)),
  1668. ccallparanode.create(p1,
  1669. ccallparanode.create(
  1670. ctemprefnode.create(tempresultvariant)
  1671. ,nil))));
  1672. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1673. addstatement(newstatement,ctempdeletenode.create(temp));
  1674. { the last statement should return the value as
  1675. location and type, this is done be referencing the
  1676. temp and converting it first from a persistent temp to
  1677. normal temp }
  1678. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1679. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1680. end;
  1681. p1:=newblock;
  1682. end;
  1683. var
  1684. protsym : tpropertysym;
  1685. p2,p3 : tnode;
  1686. srsym : tsym;
  1687. srsymtable : TSymtable;
  1688. classh : tobjectdef;
  1689. { shouldn't be used that often, so the extra overhead is ok to save
  1690. stack space }
  1691. dispatchstring : ansistring;
  1692. label
  1693. skipreckklammercheck;
  1694. begin
  1695. again:=true;
  1696. while again do
  1697. begin
  1698. { we need the resultdef }
  1699. do_typecheckpass(p1);
  1700. if codegenerror then
  1701. begin
  1702. recoverconsume_postfixops;
  1703. exit;
  1704. end;
  1705. { handle token }
  1706. case token of
  1707. _CARET:
  1708. begin
  1709. consume(_CARET);
  1710. { support tp/mac procvar^ if the procvar returns a
  1711. pointer type }
  1712. if ((m_tp_procvar in current_settings.modeswitches) or
  1713. (m_mac_procvar in current_settings.modeswitches)) and
  1714. (p1.resultdef.typ=procvardef) and
  1715. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1716. begin
  1717. p1:=ccallnode.create_procvar(nil,p1);
  1718. typecheckpass(p1);
  1719. end;
  1720. if (p1.resultdef.typ<>pointerdef) then
  1721. begin
  1722. { ^ as binary operator is a problem!!!! (FK) }
  1723. again:=false;
  1724. Message(parser_e_invalid_qualifier);
  1725. recoverconsume_postfixops;
  1726. p1.destroy;
  1727. p1:=cerrornode.create;
  1728. end
  1729. else
  1730. p1:=cderefnode.create(p1);
  1731. end;
  1732. _LECKKLAMMER:
  1733. begin
  1734. if is_class_or_interface_or_object(p1.resultdef) then
  1735. begin
  1736. { default property }
  1737. protsym:=search_default_property(tobjectdef(p1.resultdef));
  1738. if not(assigned(protsym)) then
  1739. begin
  1740. p1.destroy;
  1741. p1:=cerrornode.create;
  1742. again:=false;
  1743. message(parser_e_no_default_property_available);
  1744. end
  1745. else
  1746. begin
  1747. { The property symbol is referenced indirect }
  1748. protsym.IncRefCount;
  1749. handle_propertysym(protsym,protsym.owner,p1);
  1750. end;
  1751. end
  1752. else
  1753. begin
  1754. consume(_LECKKLAMMER);
  1755. repeat
  1756. case p1.resultdef.typ of
  1757. pointerdef:
  1758. begin
  1759. { support delphi autoderef }
  1760. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1761. (m_autoderef in current_settings.modeswitches) then
  1762. p1:=cderefnode.create(p1);
  1763. p2:=comp_expr(true);
  1764. { Support Pbytevar[0..9] which returns array [0..9].}
  1765. if try_to_consume(_POINTPOINT) then
  1766. p2:=crangenode.create(p2,comp_expr(true));
  1767. p1:=cvecnode.create(p1,p2);
  1768. end;
  1769. variantdef:
  1770. begin
  1771. handle_variantarray;
  1772. { the RECKKLAMMER is already read }
  1773. goto skipreckklammercheck;
  1774. end;
  1775. stringdef :
  1776. begin
  1777. p2:=comp_expr(true);
  1778. { Support string[0..9] which returns array [0..9] of char.}
  1779. if try_to_consume(_POINTPOINT) then
  1780. p2:=crangenode.create(p2,comp_expr(true));
  1781. p1:=cvecnode.create(p1,p2);
  1782. end;
  1783. arraydef:
  1784. begin
  1785. p2:=comp_expr(true);
  1786. { support SEG:OFS for go32v2 Mem[] }
  1787. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1788. (p1.nodetype=loadn) and
  1789. assigned(tloadnode(p1).symtableentry) and
  1790. assigned(tloadnode(p1).symtableentry.owner.name) and
  1791. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1792. ((tloadnode(p1).symtableentry.name='MEM') or
  1793. (tloadnode(p1).symtableentry.name='MEMW') or
  1794. (tloadnode(p1).symtableentry.name='MEML')) then
  1795. begin
  1796. if try_to_consume(_COLON) then
  1797. begin
  1798. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1799. p2:=comp_expr(true);
  1800. p2:=caddnode.create(addn,p2,p3);
  1801. if try_to_consume(_POINTPOINT) then
  1802. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1803. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true),p3.getcopy));
  1804. p1:=cvecnode.create(p1,p2);
  1805. include(tvecnode(p1).flags,nf_memseg);
  1806. include(tvecnode(p1).flags,nf_memindex);
  1807. end
  1808. else
  1809. begin
  1810. if try_to_consume(_POINTPOINT) then
  1811. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1812. p2:=crangenode.create(p2,comp_expr(true));
  1813. p1:=cvecnode.create(p1,p2);
  1814. include(tvecnode(p1).flags,nf_memindex);
  1815. end;
  1816. end
  1817. else
  1818. begin
  1819. if try_to_consume(_POINTPOINT) then
  1820. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1821. p2:=crangenode.create(p2,comp_expr(true));
  1822. p1:=cvecnode.create(p1,p2);
  1823. end;
  1824. end;
  1825. else
  1826. begin
  1827. if p1.resultdef.typ<>undefineddef then
  1828. Message(parser_e_invalid_qualifier);
  1829. p1.destroy;
  1830. p1:=cerrornode.create;
  1831. comp_expr(true);
  1832. again:=false;
  1833. end;
  1834. end;
  1835. do_typecheckpass(p1);
  1836. until not try_to_consume(_COMMA);
  1837. consume(_RECKKLAMMER);
  1838. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1839. skipreckklammercheck:
  1840. end;
  1841. end;
  1842. _POINT :
  1843. begin
  1844. consume(_POINT);
  1845. if (p1.resultdef.typ=pointerdef) and
  1846. (m_autoderef in current_settings.modeswitches) then
  1847. begin
  1848. p1:=cderefnode.create(p1);
  1849. do_typecheckpass(p1);
  1850. end;
  1851. case p1.resultdef.typ of
  1852. recorddef:
  1853. begin
  1854. if token=_ID then
  1855. begin
  1856. srsym:=tsym(trecorddef(p1.resultdef).symtable.Find(pattern));
  1857. if assigned(srsym) and
  1858. (srsym.typ=fieldvarsym) then
  1859. begin
  1860. check_hints(srsym,srsym.symoptions);
  1861. p1:=csubscriptnode.create(srsym,p1)
  1862. end
  1863. else
  1864. begin
  1865. Message1(sym_e_illegal_field,pattern);
  1866. p1.destroy;
  1867. p1:=cerrornode.create;
  1868. end;
  1869. end;
  1870. consume(_ID);
  1871. end;
  1872. variantdef:
  1873. begin
  1874. { dispatch call? }
  1875. if token=_ID then
  1876. begin
  1877. dispatchstring:=orgpattern;
  1878. consume(_ID);
  1879. if try_to_consume(_LKLAMMER) then
  1880. begin
  1881. p2:=parse_paras(false,true,_RKLAMMER);
  1882. consume(_RKLAMMER);
  1883. end
  1884. else
  1885. p2:=nil;
  1886. { property setter? }
  1887. if (token=_ASSIGNMENT) and not(afterassignment) then
  1888. begin
  1889. consume(_ASSIGNMENT);
  1890. { read the expression }
  1891. p3:=comp_expr(true);
  1892. { concat value parameter too }
  1893. p2:=ccallparanode.create(p3,p2);
  1894. { passing p3 here is only for information purposes }
  1895. p1:=translate_disp_call(p1,p2,p3,dispatchstring,0,false);
  1896. end
  1897. else
  1898. begin
  1899. p1:=translate_disp_call(p1,p2,nil,dispatchstring,0,
  1900. { this is only an approximation
  1901. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  1902. afterassignment or in_args or (token<>_SEMICOLON));
  1903. end;
  1904. end
  1905. else { Error }
  1906. Consume(_ID);
  1907. end;
  1908. classrefdef:
  1909. begin
  1910. if token=_ID then
  1911. begin
  1912. classh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  1913. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1914. if assigned(srsym) then
  1915. begin
  1916. check_hints(srsym,srsym.symoptions);
  1917. consume(_ID);
  1918. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1919. end
  1920. else
  1921. begin
  1922. Message1(sym_e_id_no_member,orgpattern);
  1923. p1.destroy;
  1924. p1:=cerrornode.create;
  1925. { try to clean up }
  1926. consume(_ID);
  1927. end;
  1928. end
  1929. else { Error }
  1930. Consume(_ID);
  1931. end;
  1932. objectdef:
  1933. begin
  1934. if token=_ID then
  1935. begin
  1936. classh:=tobjectdef(p1.resultdef);
  1937. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1938. if assigned(srsym) then
  1939. begin
  1940. check_hints(srsym,srsym.symoptions);
  1941. consume(_ID);
  1942. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1943. end
  1944. else
  1945. begin
  1946. Message1(sym_e_id_no_member,orgpattern);
  1947. p1.destroy;
  1948. p1:=cerrornode.create;
  1949. { try to clean up }
  1950. consume(_ID);
  1951. end;
  1952. end
  1953. else { Error }
  1954. Consume(_ID);
  1955. end;
  1956. pointerdef:
  1957. begin
  1958. Message(parser_e_invalid_qualifier);
  1959. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  1960. Message(parser_h_maybe_deref_caret_missing);
  1961. end;
  1962. else
  1963. begin
  1964. if p1.resultdef.typ<>undefineddef then
  1965. Message(parser_e_invalid_qualifier);
  1966. p1.destroy;
  1967. p1:=cerrornode.create;
  1968. { Error }
  1969. consume(_ID);
  1970. end;
  1971. end;
  1972. end;
  1973. else
  1974. begin
  1975. { is this a procedure variable ? }
  1976. if assigned(p1.resultdef) and
  1977. (p1.resultdef.typ=procvardef) then
  1978. begin
  1979. { Typenode for typecasting or expecting a procvar }
  1980. if (p1.nodetype=typen) or
  1981. (
  1982. assigned(getprocvardef) and
  1983. equal_defs(p1.resultdef,getprocvardef)
  1984. ) then
  1985. begin
  1986. if try_to_consume(_LKLAMMER) then
  1987. begin
  1988. p1:=comp_expr(true);
  1989. consume(_RKLAMMER);
  1990. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  1991. end
  1992. else
  1993. again:=false
  1994. end
  1995. else
  1996. begin
  1997. if try_to_consume(_LKLAMMER) then
  1998. begin
  1999. p2:=parse_paras(false,false,_RKLAMMER);
  2000. consume(_RKLAMMER);
  2001. p1:=ccallnode.create_procvar(p2,p1);
  2002. { proc():= is never possible }
  2003. if token=_ASSIGNMENT then
  2004. begin
  2005. Message(parser_e_illegal_expression);
  2006. p1.free;
  2007. p1:=cerrornode.create;
  2008. again:=false;
  2009. end;
  2010. end
  2011. else
  2012. again:=false;
  2013. end;
  2014. end
  2015. else
  2016. again:=false;
  2017. end;
  2018. end;
  2019. end; { while again }
  2020. end;
  2021. {---------------------------------------------
  2022. Factor (Main)
  2023. ---------------------------------------------}
  2024. var
  2025. l : longint;
  2026. ic : int64;
  2027. qc : qword;
  2028. oldp1,
  2029. p1 : tnode;
  2030. code : integer;
  2031. again : boolean;
  2032. srsym : tsym;
  2033. srsymtable : TSymtable;
  2034. pd : tprocdef;
  2035. hclassdef : tobjectdef;
  2036. d : bestreal;
  2037. cur : currency;
  2038. hs,hsorg : string;
  2039. hdef : tdef;
  2040. filepos : tfileposinfo;
  2041. begin
  2042. oldp1:=nil;
  2043. p1:=nil;
  2044. filepos:=current_tokenpos;
  2045. again:=false;
  2046. if token=_ID then
  2047. begin
  2048. again:=true;
  2049. { Handle references to self }
  2050. if (idtoken=_SELF) and
  2051. not(block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) and
  2052. assigned(current_objectdef) then
  2053. begin
  2054. p1:=load_self_node;
  2055. consume(_ID);
  2056. again:=true;
  2057. end
  2058. else
  2059. factor_read_id(p1,again);
  2060. if again then
  2061. begin
  2062. if (p1<>oldp1) then
  2063. begin
  2064. if assigned(p1) then
  2065. p1.fileinfo:=filepos;
  2066. oldp1:=p1;
  2067. filepos:=current_tokenpos;
  2068. end;
  2069. { handle post fix operators }
  2070. postfixoperators(p1,again);
  2071. end;
  2072. end
  2073. else
  2074. case token of
  2075. _RETURN :
  2076. begin
  2077. consume(_RETURN);
  2078. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2079. p1 := cexitnode.create(comp_expr(true))
  2080. else
  2081. p1 := cexitnode.create(nil);
  2082. end;
  2083. _INHERITED :
  2084. begin
  2085. again:=true;
  2086. consume(_INHERITED);
  2087. if assigned(current_procinfo) and
  2088. assigned(current_objectdef) then
  2089. begin
  2090. hclassdef:=current_objectdef.childof;
  2091. { if inherited; only then we need the method with
  2092. the same name }
  2093. if token in endtokens then
  2094. begin
  2095. hs:=current_procinfo.procdef.procsym.name;
  2096. hsorg:=current_procinfo.procdef.procsym.realname;
  2097. anon_inherited:=true;
  2098. { For message methods we need to search using the message
  2099. number or string }
  2100. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2101. srdef:=nil;
  2102. if (po_msgint in pd.procoptions) then
  2103. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2104. else
  2105. if (po_msgstr in pd.procoptions) then
  2106. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2107. else
  2108. searchsym_in_class(hclassdef,current_objectdef,hs,srsym,srsymtable);
  2109. end
  2110. else
  2111. begin
  2112. hs:=pattern;
  2113. hsorg:=orgpattern;
  2114. consume(_ID);
  2115. anon_inherited:=false;
  2116. searchsym_in_class(hclassdef,current_objectdef,hs,srsym,srsymtable);
  2117. end;
  2118. if assigned(srsym) then
  2119. begin
  2120. check_hints(srsym,srsym.symoptions);
  2121. { load the procdef from the inherited class and
  2122. not from self }
  2123. case srsym.typ of
  2124. procsym:
  2125. begin
  2126. hdef:=hclassdef;
  2127. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2128. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2129. hdef:=tclassrefdef.create(hdef);
  2130. p1:=ctypenode.create(hdef);
  2131. end;
  2132. propertysym:
  2133. ;
  2134. else
  2135. begin
  2136. Message(parser_e_methode_id_expected);
  2137. p1:=cerrornode.create;
  2138. end;
  2139. end;
  2140. do_member_read(hclassdef,getaddr,srsym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  2141. end
  2142. else
  2143. begin
  2144. if anon_inherited then
  2145. begin
  2146. { For message methods we need to call DefaultHandler }
  2147. if (po_msgint in pd.procoptions) or
  2148. (po_msgstr in pd.procoptions) then
  2149. begin
  2150. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable);
  2151. if not assigned(srsym) or
  2152. (srsym.typ<>procsym) then
  2153. internalerror(200303171);
  2154. p1:=nil;
  2155. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2156. end
  2157. else
  2158. begin
  2159. { we need to ignore the inherited; }
  2160. p1:=cnothingnode.create;
  2161. end;
  2162. end
  2163. else
  2164. begin
  2165. Message1(sym_e_id_no_member,hsorg);
  2166. p1:=cerrornode.create;
  2167. end;
  2168. again:=false;
  2169. end;
  2170. { turn auto inheriting off }
  2171. anon_inherited:=false;
  2172. end
  2173. else
  2174. begin
  2175. Message(parser_e_generic_methods_only_in_methods);
  2176. again:=false;
  2177. p1:=cerrornode.create;
  2178. end;
  2179. postfixoperators(p1,again);
  2180. end;
  2181. _INTCONST :
  2182. begin
  2183. {Try first wether the value fits in an int64.}
  2184. val(pattern,ic,code);
  2185. if code=0 then
  2186. begin
  2187. consume(_INTCONST);
  2188. int_to_type(ic,hdef);
  2189. p1:=cordconstnode.create(ic,hdef,true);
  2190. end
  2191. else
  2192. begin
  2193. { try qword next }
  2194. val(pattern,qc,code);
  2195. if code=0 then
  2196. begin
  2197. consume(_INTCONST);
  2198. int_to_type(qc,hdef);
  2199. p1:=cordconstnode.create(qc,hdef,true);
  2200. end;
  2201. end;
  2202. if code<>0 then
  2203. begin
  2204. { finally float }
  2205. val(pattern,d,code);
  2206. if code<>0 then
  2207. begin
  2208. Message(parser_e_invalid_integer);
  2209. consume(_INTCONST);
  2210. l:=1;
  2211. p1:=cordconstnode.create(l,sinttype,true);
  2212. end
  2213. else
  2214. begin
  2215. consume(_INTCONST);
  2216. p1:=crealconstnode.create(d,pbestrealtype^);
  2217. end;
  2218. end
  2219. else
  2220. { the necessary range checking has already been done by val }
  2221. tordconstnode(p1).rangecheck:=false;
  2222. end;
  2223. _REALNUMBER :
  2224. begin
  2225. val(pattern,d,code);
  2226. if code<>0 then
  2227. begin
  2228. Message(parser_e_error_in_real);
  2229. d:=1.0;
  2230. end;
  2231. consume(_REALNUMBER);
  2232. {$ifdef FPC_REAL2REAL_FIXED}
  2233. if current_settings.fputype=fpu_none then
  2234. Message(parser_e_unsupported_real);
  2235. if (current_settings.minfpconstprec=s32real) and
  2236. (d = single(d)) then
  2237. p1:=crealconstnode.create(d,s32floattype)
  2238. else if (current_settings.minfpconstprec=s64real) and
  2239. (d = double(d)) then
  2240. p1:=crealconstnode.create(d,s64floattype)
  2241. else
  2242. {$endif FPC_REAL2REAL_FIXED}
  2243. p1:=crealconstnode.create(d,pbestrealtype^);
  2244. {$ifdef FPC_HAS_STR_CURRENCY}
  2245. val(pattern,cur,code);
  2246. if code=0 then
  2247. trealconstnode(p1).value_currency:=cur;
  2248. {$endif FPC_HAS_STR_CURRENCY}
  2249. end;
  2250. _STRING :
  2251. begin
  2252. string_dec(hdef,true);
  2253. { STRING can be also a type cast }
  2254. if try_to_consume(_LKLAMMER) then
  2255. begin
  2256. p1:=comp_expr(true);
  2257. consume(_RKLAMMER);
  2258. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2259. { handle postfix operators here e.g. string(a)[10] }
  2260. again:=true;
  2261. postfixoperators(p1,again);
  2262. end
  2263. else
  2264. p1:=ctypenode.create(hdef);
  2265. end;
  2266. _FILE :
  2267. begin
  2268. hdef:=cfiletype;
  2269. consume(_FILE);
  2270. { FILE can be also a type cast }
  2271. if try_to_consume(_LKLAMMER) then
  2272. begin
  2273. p1:=comp_expr(true);
  2274. consume(_RKLAMMER);
  2275. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2276. { handle postfix operators here e.g. string(a)[10] }
  2277. again:=true;
  2278. postfixoperators(p1,again);
  2279. end
  2280. else
  2281. begin
  2282. p1:=ctypenode.create(hdef);
  2283. end;
  2284. end;
  2285. _CSTRING :
  2286. begin
  2287. p1:=cstringconstnode.createstr(pattern);
  2288. consume(_CSTRING);
  2289. end;
  2290. _CCHAR :
  2291. begin
  2292. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2293. consume(_CCHAR);
  2294. end;
  2295. _CWSTRING:
  2296. begin
  2297. p1:=cstringconstnode.createwstr(patternw);
  2298. consume(_CWSTRING);
  2299. end;
  2300. _CWCHAR:
  2301. begin
  2302. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2303. consume(_CWCHAR);
  2304. end;
  2305. _KLAMMERAFFE :
  2306. begin
  2307. consume(_KLAMMERAFFE);
  2308. got_addrn:=true;
  2309. { support both @<x> and @(<x>) }
  2310. if try_to_consume(_LKLAMMER) then
  2311. begin
  2312. p1:=factor(true);
  2313. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2314. begin
  2315. again:=true;
  2316. postfixoperators(p1,again);
  2317. end;
  2318. consume(_RKLAMMER);
  2319. end
  2320. else
  2321. p1:=factor(true);
  2322. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2323. begin
  2324. again:=true;
  2325. postfixoperators(p1,again);
  2326. end;
  2327. got_addrn:=false;
  2328. p1:=caddrnode.create(p1);
  2329. if cs_typed_addresses in current_settings.localswitches then
  2330. include(p1.flags,nf_typedaddr);
  2331. { Store the procvar that we are expecting, the
  2332. addrn will use the information to find the correct
  2333. procdef or it will return an error }
  2334. if assigned(getprocvardef) and
  2335. (taddrnode(p1).left.nodetype = loadn) then
  2336. taddrnode(p1).getprocvardef:=getprocvardef;
  2337. end;
  2338. _LKLAMMER :
  2339. begin
  2340. consume(_LKLAMMER);
  2341. p1:=comp_expr(true);
  2342. consume(_RKLAMMER);
  2343. { it's not a good solution }
  2344. { but (a+b)^ makes some problems }
  2345. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2346. begin
  2347. again:=true;
  2348. postfixoperators(p1,again);
  2349. end;
  2350. end;
  2351. _LECKKLAMMER :
  2352. begin
  2353. consume(_LECKKLAMMER);
  2354. p1:=factor_read_set;
  2355. consume(_RECKKLAMMER);
  2356. end;
  2357. _PLUS :
  2358. begin
  2359. consume(_PLUS);
  2360. p1:=factor(false);
  2361. { we must generate a new node to do 0+<p1> otherwise the + will
  2362. not be checked }
  2363. p1:=caddnode.create(addn,genintconstnode(0),p1);
  2364. end;
  2365. _MINUS :
  2366. begin
  2367. consume(_MINUS);
  2368. if (token = _INTCONST) then
  2369. begin
  2370. { ugly hack, but necessary to be able to parse }
  2371. { -9223372036854775808 as int64 (JM) }
  2372. pattern := '-'+pattern;
  2373. p1:=sub_expr(oppower,false);
  2374. { -1 ** 4 should be - (1 ** 4) and not
  2375. (-1) ** 4
  2376. This was the reason of tw0869.pp test failure PM }
  2377. if p1.nodetype=starstarn then
  2378. begin
  2379. if tbinarynode(p1).left.nodetype=ordconstn then
  2380. begin
  2381. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2382. p1:=cunaryminusnode.create(p1);
  2383. end
  2384. else if tbinarynode(p1).left.nodetype=realconstn then
  2385. begin
  2386. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2387. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2388. p1:=cunaryminusnode.create(p1);
  2389. end
  2390. else
  2391. internalerror(20021029);
  2392. end;
  2393. end
  2394. else
  2395. begin
  2396. p1:=sub_expr(oppower,false);
  2397. p1:=cunaryminusnode.create(p1);
  2398. end;
  2399. end;
  2400. _OP_NOT :
  2401. begin
  2402. consume(_OP_NOT);
  2403. p1:=factor(false);
  2404. p1:=cnotnode.create(p1);
  2405. end;
  2406. _TRUE :
  2407. begin
  2408. consume(_TRUE);
  2409. p1:=cordconstnode.create(1,booltype,false);
  2410. end;
  2411. _FALSE :
  2412. begin
  2413. consume(_FALSE);
  2414. p1:=cordconstnode.create(0,booltype,false);
  2415. end;
  2416. _NIL :
  2417. begin
  2418. consume(_NIL);
  2419. p1:=cnilnode.create;
  2420. { It's really ugly code nil^, but delphi allows it }
  2421. if token in [_CARET] then
  2422. begin
  2423. again:=true;
  2424. postfixoperators(p1,again);
  2425. end;
  2426. end;
  2427. else
  2428. begin
  2429. Message(parser_e_illegal_expression);
  2430. p1:=cerrornode.create;
  2431. { recover }
  2432. consume(token);
  2433. end;
  2434. end;
  2435. { generate error node if no node is created }
  2436. if not assigned(p1) then
  2437. begin
  2438. {$ifdef EXTDEBUG}
  2439. Comment(V_Warning,'factor: p1=nil');
  2440. {$endif}
  2441. p1:=cerrornode.create;
  2442. end;
  2443. { get the resultdef for the node }
  2444. if (not assigned(p1.resultdef)) then
  2445. do_typecheckpass(p1);
  2446. if assigned(p1) and
  2447. (p1<>oldp1) then
  2448. p1.fileinfo:=filepos;
  2449. factor:=p1;
  2450. end;
  2451. {$maxfpuregisters default}
  2452. {****************************************************************************
  2453. Sub_Expr
  2454. ****************************************************************************}
  2455. const
  2456. { Warning these stay be ordered !! }
  2457. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2458. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN],
  2459. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2460. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2461. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2462. [_STARSTAR] );
  2463. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2464. {Reads a subexpression while the operators are of the current precedence
  2465. level, or any higher level. Replaces the old term, simpl_expr and
  2466. simpl2_expr.}
  2467. var
  2468. p1,p2 : tnode;
  2469. oldt : Ttoken;
  2470. filepos : tfileposinfo;
  2471. begin
  2472. if pred_level=highest_precedence then
  2473. p1:=factor(false)
  2474. else
  2475. p1:=sub_expr(succ(pred_level),true);
  2476. repeat
  2477. if (token in operator_levels[pred_level]) and
  2478. ((token<>_EQUAL) or accept_equal) then
  2479. begin
  2480. oldt:=token;
  2481. filepos:=current_tokenpos;
  2482. consume(token);
  2483. if pred_level=highest_precedence then
  2484. p2:=factor(false)
  2485. else
  2486. p2:=sub_expr(succ(pred_level),true);
  2487. case oldt of
  2488. _PLUS :
  2489. p1:=caddnode.create(addn,p1,p2);
  2490. _MINUS :
  2491. p1:=caddnode.create(subn,p1,p2);
  2492. _STAR :
  2493. p1:=caddnode.create(muln,p1,p2);
  2494. _SLASH :
  2495. p1:=caddnode.create(slashn,p1,p2);
  2496. _EQUAL :
  2497. p1:=caddnode.create(equaln,p1,p2);
  2498. _GT :
  2499. p1:=caddnode.create(gtn,p1,p2);
  2500. _LT :
  2501. p1:=caddnode.create(ltn,p1,p2);
  2502. _GTE :
  2503. p1:=caddnode.create(gten,p1,p2);
  2504. _LTE :
  2505. p1:=caddnode.create(lten,p1,p2);
  2506. _SYMDIF :
  2507. p1:=caddnode.create(symdifn,p1,p2);
  2508. _STARSTAR :
  2509. p1:=caddnode.create(starstarn,p1,p2);
  2510. _OP_AS :
  2511. p1:=casnode.create(p1,p2);
  2512. _OP_IN :
  2513. p1:=cinnode.create(p1,p2);
  2514. _OP_IS :
  2515. p1:=cisnode.create(p1,p2);
  2516. _OP_OR,
  2517. _PIPE {macpas only} :
  2518. begin
  2519. p1:=caddnode.create(orn,p1,p2);
  2520. if (oldt = _PIPE) then
  2521. include(p1.flags,nf_short_bool);
  2522. end;
  2523. _OP_AND,
  2524. _AMPERSAND {macpas only} :
  2525. begin
  2526. p1:=caddnode.create(andn,p1,p2);
  2527. if (oldt = _AMPERSAND) then
  2528. include(p1.flags,nf_short_bool);
  2529. end;
  2530. _OP_DIV :
  2531. p1:=cmoddivnode.create(divn,p1,p2);
  2532. _OP_NOT :
  2533. p1:=cnotnode.create(p1);
  2534. _OP_MOD :
  2535. p1:=cmoddivnode.create(modn,p1,p2);
  2536. _OP_SHL :
  2537. p1:=cshlshrnode.create(shln,p1,p2);
  2538. _OP_SHR :
  2539. p1:=cshlshrnode.create(shrn,p1,p2);
  2540. _OP_XOR :
  2541. p1:=caddnode.create(xorn,p1,p2);
  2542. _ASSIGNMENT :
  2543. p1:=cassignmentnode.create(p1,p2);
  2544. _UNEQUAL :
  2545. p1:=caddnode.create(unequaln,p1,p2);
  2546. end;
  2547. p1.fileinfo:=filepos;
  2548. end
  2549. else
  2550. break;
  2551. until false;
  2552. sub_expr:=p1;
  2553. end;
  2554. function comp_expr(accept_equal : boolean):tnode;
  2555. var
  2556. oldafterassignment : boolean;
  2557. p1 : tnode;
  2558. begin
  2559. oldafterassignment:=afterassignment;
  2560. afterassignment:=true;
  2561. p1:=sub_expr(opcompare,accept_equal);
  2562. { get the resultdef for this expression }
  2563. if not assigned(p1.resultdef) then
  2564. do_typecheckpass(p1);
  2565. afterassignment:=oldafterassignment;
  2566. comp_expr:=p1;
  2567. end;
  2568. function expr : tnode;
  2569. var
  2570. p1,p2 : tnode;
  2571. oldafterassignment : boolean;
  2572. oldp1 : tnode;
  2573. filepos : tfileposinfo;
  2574. begin
  2575. oldafterassignment:=afterassignment;
  2576. p1:=sub_expr(opcompare,true);
  2577. { get the resultdef for this expression }
  2578. if not assigned(p1.resultdef) then
  2579. do_typecheckpass(p1);
  2580. filepos:=current_tokenpos;
  2581. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2582. afterassignment:=true;
  2583. oldp1:=p1;
  2584. case token of
  2585. _POINTPOINT :
  2586. begin
  2587. consume(_POINTPOINT);
  2588. p2:=sub_expr(opcompare,true);
  2589. p1:=crangenode.create(p1,p2);
  2590. end;
  2591. _ASSIGNMENT :
  2592. begin
  2593. consume(_ASSIGNMENT);
  2594. if (p1.resultdef.typ=procvardef) then
  2595. getprocvardef:=tprocvardef(p1.resultdef);
  2596. p2:=sub_expr(opcompare,true);
  2597. if assigned(getprocvardef) then
  2598. handle_procvar(getprocvardef,p2);
  2599. getprocvardef:=nil;
  2600. p1:=cassignmentnode.create(p1,p2);
  2601. end;
  2602. _PLUSASN :
  2603. begin
  2604. consume(_PLUSASN);
  2605. p2:=sub_expr(opcompare,true);
  2606. p1:=gen_c_style_operator(addn,p1,p2);
  2607. end;
  2608. _MINUSASN :
  2609. begin
  2610. consume(_MINUSASN);
  2611. p2:=sub_expr(opcompare,true);
  2612. p1:=gen_c_style_operator(subn,p1,p2);
  2613. end;
  2614. _STARASN :
  2615. begin
  2616. consume(_STARASN );
  2617. p2:=sub_expr(opcompare,true);
  2618. p1:=gen_c_style_operator(muln,p1,p2);
  2619. end;
  2620. _SLASHASN :
  2621. begin
  2622. consume(_SLASHASN );
  2623. p2:=sub_expr(opcompare,true);
  2624. p1:=gen_c_style_operator(slashn,p1,p2);
  2625. end;
  2626. end;
  2627. { get the resultdef for this expression }
  2628. if not assigned(p1.resultdef) then
  2629. do_typecheckpass(p1);
  2630. afterassignment:=oldafterassignment;
  2631. if p1<>oldp1 then
  2632. p1.fileinfo:=filepos;
  2633. expr:=p1;
  2634. end;
  2635. function get_intconst:TConstExprInt;
  2636. {Reads an expression, tries to evalute it and check if it is an integer
  2637. constant. Then the constant is returned.}
  2638. var
  2639. p:tnode;
  2640. begin
  2641. result:=0;
  2642. p:=comp_expr(true);
  2643. if not codegenerror then
  2644. begin
  2645. if (p.nodetype<>ordconstn) or
  2646. not(is_integer(p.resultdef)) then
  2647. Message(parser_e_illegal_expression)
  2648. else
  2649. result:=tordconstnode(p).value;
  2650. end;
  2651. p.free;
  2652. end;
  2653. function get_stringconst:string;
  2654. {Reads an expression, tries to evaluate it and checks if it is a string
  2655. constant. Then the constant is returned.}
  2656. var
  2657. p:tnode;
  2658. begin
  2659. get_stringconst:='';
  2660. p:=comp_expr(true);
  2661. if p.nodetype<>stringconstn then
  2662. begin
  2663. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2664. get_stringconst:=char(int64(tordconstnode(p).value))
  2665. else
  2666. Message(parser_e_illegal_expression);
  2667. end
  2668. else
  2669. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2670. p.free;
  2671. end;
  2672. end.