pexpr.pas 107 KB

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