pexpr.pas 107 KB

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