pexpr.pas 170 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing of expression for Free Pascal
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pexpr;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,symdef,symbase,
  22. node,ncal,
  23. tokens,globtype,globals,constexp,
  24. pgentype;
  25. { reads a whole expression }
  26. function expr(dotypecheck:boolean) : tnode;
  27. { reads an expression without assignements and .. }
  28. function comp_expr(accept_equal,typeonly:boolean):tnode;
  29. { reads a single factor }
  30. function factor(getaddr,typeonly,hadspecialize:boolean) : tnode;
  31. procedure string_dec(var def: tdef; allowtypedef: boolean);
  32. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  33. { the ID token has to be consumed before calling this function }
  34. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags;spezcontext:tspecializationcontext);
  35. function get_intconst:TConstExprInt;
  36. function get_stringconst:string;
  37. { Does some postprocessing for a generic type (especially when nested types
  38. of the specialization are used) }
  39. procedure post_comp_expr_gendef(var def: tdef);
  40. implementation
  41. uses
  42. { common }
  43. cutils,
  44. { global }
  45. verbose,
  46. systems,widestr,
  47. { symtable }
  48. symconst,symtable,symsym,symcpu,defutil,defcmp,
  49. { module }
  50. fmodule,ppu,
  51. { pass 1 }
  52. pass_1,htypechk,
  53. nmat,nadd,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,
  54. { parser }
  55. scanner,
  56. pbase,pinline,ptype,pgenutil,procinfo,cpuinfo
  57. ;
  58. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean;factornode:tnode):tnode;forward;
  59. const
  60. { true, if the inherited call is anonymous }
  61. anon_inherited : boolean = false;
  62. { last def found, only used by anon. inherited calls to insert proper type casts }
  63. srdef : tdef = nil;
  64. procedure string_dec(var def:tdef; allowtypedef: boolean);
  65. { reads a string type with optional length }
  66. { and returns a pointer to the string }
  67. { definition }
  68. var
  69. p : tnode;
  70. begin
  71. def:=cshortstringtype;
  72. consume(_STRING);
  73. if token=_LECKKLAMMER then
  74. begin
  75. if not(allowtypedef) then
  76. Message(parser_e_no_local_para_def);
  77. consume(_LECKKLAMMER);
  78. p:=comp_expr(true,false);
  79. if not is_constintnode(p) then
  80. begin
  81. Message(parser_e_illegal_expression);
  82. { error recovery }
  83. consume(_RECKKLAMMER);
  84. end
  85. else
  86. begin
  87. if (tordconstnode(p).value<=0) then
  88. begin
  89. Message(parser_e_invalid_string_size);
  90. tordconstnode(p).value:=255;
  91. end;
  92. if tordconstnode(p).value>255 then
  93. begin
  94. { longstring is currently unsupported (CEC)! }
  95. { t:=cstringdef.createlong(tordconstnode(p).value))}
  96. Message(parser_e_invalid_string_size);
  97. tordconstnode(p).value:=255;
  98. def:=cstringdef.createshort(int64(tordconstnode(p).value));
  99. end
  100. else
  101. if tordconstnode(p).value<>255 then
  102. def:=cstringdef.createshort(int64(tordconstnode(p).value));
  103. consume(_RECKKLAMMER);
  104. end;
  105. p.free;
  106. end
  107. else
  108. begin
  109. if cs_refcountedstrings in current_settings.localswitches then
  110. begin
  111. if m_default_unicodestring in current_settings.modeswitches then
  112. def:=cunicodestringtype
  113. else
  114. def:=cansistringtype
  115. end
  116. else
  117. def:=cshortstringtype;
  118. end;
  119. end;
  120. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  121. var
  122. p1,p2,argname : tnode;
  123. prev_in_args,
  124. old_named_args_allowed,
  125. old_allow_array_constructor : boolean;
  126. begin
  127. if token=end_of_paras then
  128. begin
  129. parse_paras:=nil;
  130. exit;
  131. end;
  132. { save old values }
  133. prev_in_args:=in_args;
  134. old_allow_array_constructor:=allow_array_constructor;
  135. old_named_args_allowed:=named_args_allowed;
  136. { set para parsing values }
  137. in_args:=true;
  138. named_args_allowed:=false;
  139. allow_array_constructor:=true;
  140. p2:=nil;
  141. repeat
  142. if __namedpara then
  143. begin
  144. if token=_COMMA then
  145. begin
  146. { empty parameter }
  147. p2:=ccallparanode.create(cnothingnode.create,p2);
  148. end
  149. else
  150. begin
  151. named_args_allowed:=true;
  152. p1:=comp_expr(true,false);
  153. named_args_allowed:=false;
  154. if found_arg_name then
  155. begin
  156. argname:=p1;
  157. p1:=comp_expr(true,false);
  158. p2:=ccallparanode.create(p1,p2);
  159. tcallparanode(p2).parametername:=argname;
  160. end
  161. else
  162. p2:=ccallparanode.create(p1,p2);
  163. found_arg_name:=false;
  164. end;
  165. end
  166. else
  167. begin
  168. p1:=comp_expr(true,false);
  169. p2:=ccallparanode.create(p1,p2);
  170. end;
  171. { it's for the str(l:5,s); }
  172. if __colon and (token=_COLON) then
  173. begin
  174. consume(_COLON);
  175. p1:=comp_expr(true,false);
  176. p2:=ccallparanode.create(p1,p2);
  177. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  178. if try_to_consume(_COLON) then
  179. begin
  180. p1:=comp_expr(true,false);
  181. p2:=ccallparanode.create(p1,p2);
  182. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  183. end
  184. end;
  185. until not try_to_consume(_COMMA);
  186. allow_array_constructor:=old_allow_array_constructor;
  187. in_args:=prev_in_args;
  188. named_args_allowed:=old_named_args_allowed;
  189. parse_paras:=p2;
  190. end;
  191. function gen_c_style_operator(ntyp:tnodetype;p1,p2:tnode) : tnode;
  192. var
  193. hp : tnode;
  194. hdef : tdef;
  195. temp : ttempcreatenode;
  196. newstatement : tstatementnode;
  197. begin
  198. { Properties are not allowed, because the write can
  199. be different from the read }
  200. if (nf_isproperty in p1.flags) then
  201. begin
  202. Message(type_e_variable_id_expected);
  203. { We can continue with the loading,
  204. it'll not create errors. Only the expected
  205. result can be wrong }
  206. end;
  207. hp:=p1;
  208. while assigned(hp) and
  209. (hp.nodetype in [derefn,subscriptn,vecn,typeconvn]) do
  210. hp:=tunarynode(hp).left;
  211. if not assigned(hp) then
  212. internalerror(200410121);
  213. if (hp.nodetype=calln) then
  214. begin
  215. typecheckpass(p1);
  216. result:=internalstatements(newstatement);
  217. hdef:=cpointerdef.getreusable(p1.resultdef);
  218. temp:=ctempcreatenode.create(hdef,sizeof(pint),tt_persistent,false);
  219. addstatement(newstatement,temp);
  220. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),caddrnode.create_internal(p1)));
  221. addstatement(newstatement,cassignmentnode.create(
  222. cderefnode.create(ctemprefnode.create(temp)),
  223. caddnode.create(ntyp,
  224. cderefnode.create(ctemprefnode.create(temp)),
  225. p2)));
  226. addstatement(newstatement,ctempdeletenode.create(temp));
  227. end
  228. else
  229. result:=cassignmentnode.create(p1,caddnode.create(ntyp,p1.getcopy,p2));
  230. end;
  231. function statement_syssym(l : byte) : tnode;
  232. var
  233. p1,p2,paras : tnode;
  234. err,
  235. prev_in_args : boolean;
  236. def : tdef;
  237. exit_procinfo: tprocinfo;
  238. begin
  239. prev_in_args:=in_args;
  240. case l of
  241. in_new_x :
  242. begin
  243. if afterassignment or in_args then
  244. statement_syssym:=new_function
  245. else
  246. statement_syssym:=new_dispose_statement(true);
  247. end;
  248. in_dispose_x :
  249. begin
  250. statement_syssym:=new_dispose_statement(false);
  251. end;
  252. in_ord_x :
  253. begin
  254. consume(_LKLAMMER);
  255. in_args:=true;
  256. p1:=comp_expr(true,false);
  257. consume(_RKLAMMER);
  258. p1:=geninlinenode(in_ord_x,false,p1);
  259. statement_syssym := p1;
  260. end;
  261. in_exit :
  262. begin
  263. statement_syssym:=nil;
  264. if try_to_consume(_LKLAMMER) then
  265. begin
  266. if not (m_mac in current_settings.modeswitches) then
  267. begin
  268. if not(try_to_consume(_RKLAMMER)) then
  269. begin
  270. p1:=comp_expr(true,false);
  271. consume(_RKLAMMER);
  272. if not assigned(current_procinfo) or
  273. (current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) or
  274. is_void(current_procinfo.procdef.returndef) then
  275. begin
  276. Message(parser_e_void_function);
  277. { recovery }
  278. p1.free;
  279. p1:=nil;
  280. end;
  281. end
  282. else
  283. p1:=nil;
  284. end
  285. else
  286. begin
  287. { non local exit ? }
  288. if current_procinfo.procdef.procsym.name<>pattern then
  289. begin
  290. exit_procinfo:=current_procinfo.parent;
  291. while assigned(exit_procinfo) do
  292. begin
  293. if exit_procinfo.procdef.procsym.name=pattern then
  294. break;
  295. exit_procinfo:=exit_procinfo.parent;
  296. end;
  297. if assigned(exit_procinfo) then
  298. begin
  299. if not(assigned(exit_procinfo.nestedexitlabel)) then
  300. begin
  301. include(current_procinfo.flags,pi_has_nested_exit);
  302. exclude(current_procinfo.procdef.procoptions,po_inline);
  303. exit_procinfo.nestedexitlabel:=clabelsym.create('$nestedexit');
  304. { the compiler is responsible to define this label }
  305. exit_procinfo.nestedexitlabel.defined:=true;
  306. exit_procinfo.nestedexitlabel.used:=true;
  307. exit_procinfo.nestedexitlabel.jumpbuf:=clocalvarsym.create('LABEL$_'+exit_procinfo.nestedexitlabel.name,vs_value,rec_jmp_buf,[]);
  308. exit_procinfo.procdef.localst.insert(exit_procinfo.nestedexitlabel);
  309. exit_procinfo.procdef.localst.insert(exit_procinfo.nestedexitlabel.jumpbuf);
  310. end;
  311. statement_syssym:=cgotonode.create(exit_procinfo.nestedexitlabel);
  312. tgotonode(statement_syssym).labelsym:=exit_procinfo.nestedexitlabel;
  313. end
  314. else
  315. Message(parser_e_macpas_exit_wrong_param);
  316. end;
  317. consume(_ID);
  318. consume(_RKLAMMER);
  319. p1:=nil;
  320. end
  321. end
  322. else
  323. p1:=nil;
  324. if not assigned(statement_syssym) then
  325. statement_syssym:=cexitnode.create(p1);
  326. end;
  327. in_break :
  328. begin
  329. statement_syssym:=cbreaknode.create
  330. end;
  331. in_continue :
  332. begin
  333. statement_syssym:=ccontinuenode.create
  334. end;
  335. in_leave :
  336. begin
  337. if m_mac in current_settings.modeswitches then
  338. statement_syssym:=cbreaknode.create
  339. else
  340. begin
  341. Message1(sym_e_id_not_found, orgpattern);
  342. statement_syssym:=cerrornode.create;
  343. end;
  344. end;
  345. in_cycle :
  346. begin
  347. if m_mac in current_settings.modeswitches then
  348. statement_syssym:=ccontinuenode.create
  349. else
  350. begin
  351. Message1(sym_e_id_not_found, orgpattern);
  352. statement_syssym:=cerrornode.create;
  353. end;
  354. end;
  355. in_typeof_x :
  356. begin
  357. consume(_LKLAMMER);
  358. in_args:=true;
  359. p1:=comp_expr(true,false);
  360. consume(_RKLAMMER);
  361. if p1.nodetype=typen then
  362. ttypenode(p1).allowed:=true;
  363. { Allow classrefdef, which is required for
  364. Typeof(self) in static class methods }
  365. if not(is_objc_class_or_protocol(p1.resultdef)) and
  366. not(is_java_class_or_interface(p1.resultdef)) and
  367. ((p1.resultdef.typ = objectdef) or
  368. (assigned(current_procinfo) and
  369. ((po_classmethod in current_procinfo.procdef.procoptions) or
  370. (po_staticmethod in current_procinfo.procdef.procoptions)) and
  371. (p1.resultdef.typ=classrefdef))) then
  372. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  373. else
  374. begin
  375. Message(parser_e_class_id_expected);
  376. p1.destroy;
  377. statement_syssym:=cerrornode.create;
  378. end;
  379. end;
  380. in_sizeof_x,
  381. in_bitsizeof_x :
  382. begin
  383. consume(_LKLAMMER);
  384. in_args:=true;
  385. p1:=comp_expr(true,false);
  386. consume(_RKLAMMER);
  387. if ((p1.nodetype<>typen) and
  388. (
  389. (is_object(p1.resultdef) and
  390. (oo_has_constructor in tobjectdef(p1.resultdef).objectoptions)) or
  391. is_open_array(p1.resultdef) or
  392. is_array_of_const(p1.resultdef) or
  393. is_open_string(p1.resultdef)
  394. )) or
  395. { keep the function call if it is a type parameter to avoid arithmetic errors due to constant folding }
  396. (p1.resultdef.typ=undefineddef) then
  397. begin
  398. statement_syssym:=geninlinenode(in_sizeof_x,false,p1);
  399. { no packed bit support for these things }
  400. if l=in_bitsizeof_x then
  401. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  402. end
  403. else
  404. begin
  405. { allow helpers for SizeOf and BitSizeOf }
  406. if p1.nodetype=typen then
  407. ttypenode(p1).helperallowed:=true;
  408. if (p1.resultdef.typ=forwarddef) then
  409. Message1(type_e_type_is_not_completly_defined,tforwarddef(p1.resultdef).tosymname^);
  410. if (l = in_sizeof_x) or
  411. (not((p1.nodetype = vecn) and
  412. is_packed_array(tvecnode(p1).left.resultdef)) and
  413. not((p1.nodetype = subscriptn) and
  414. is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
  415. begin
  416. statement_syssym:=cordconstnode.create(p1.resultdef.size,sinttype,true);
  417. if (l = in_bitsizeof_x) then
  418. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  419. end
  420. else
  421. statement_syssym:=cordconstnode.create(p1.resultdef.packedbitsize,sinttype,true);
  422. { p1 not needed !}
  423. p1.destroy;
  424. end;
  425. end;
  426. in_typeinfo_x,
  427. in_objc_encode_x :
  428. begin
  429. if (l=in_typeinfo_x) or
  430. (m_objectivec1 in current_settings.modeswitches) then
  431. begin
  432. consume(_LKLAMMER);
  433. in_args:=true;
  434. p1:=comp_expr(true,false);
  435. { When reading a class type it is parsed as loadvmtaddrn,
  436. typeinfo only needs the type so we remove the loadvmtaddrn }
  437. if p1.nodetype=loadvmtaddrn then
  438. begin
  439. p2:=tloadvmtaddrnode(p1).left;
  440. tloadvmtaddrnode(p1).left:=nil;
  441. p1.free;
  442. p1:=p2;
  443. end;
  444. if p1.nodetype=typen then
  445. begin
  446. ttypenode(p1).allowed:=true;
  447. { allow helpers for TypeInfo }
  448. if l=in_typeinfo_x then
  449. ttypenode(p1).helperallowed:=true;
  450. end;
  451. { else
  452. begin
  453. p1.destroy;
  454. p1:=cerrornode.create;
  455. Message(parser_e_illegal_parameter_list);
  456. end;}
  457. consume(_RKLAMMER);
  458. p2:=geninlinenode(l,false,p1);
  459. statement_syssym:=p2;
  460. end
  461. else
  462. begin
  463. Message1(sym_e_id_not_found, orgpattern);
  464. statement_syssym:=cerrornode.create;
  465. end;
  466. end;
  467. in_aligned_x,
  468. in_unaligned_x :
  469. begin
  470. err:=false;
  471. consume(_LKLAMMER);
  472. in_args:=true;
  473. p1:=comp_expr(true,false);
  474. p2:=ccallparanode.create(p1,nil);
  475. p2:=geninlinenode(l,false,p2);
  476. consume(_RKLAMMER);
  477. statement_syssym:=p2;
  478. end;
  479. in_assigned_x :
  480. begin
  481. err:=false;
  482. consume(_LKLAMMER);
  483. in_args:=true;
  484. p1:=comp_expr(true,false);
  485. { When reading a class type it is parsed as loadvmtaddrn,
  486. typeinfo only needs the type so we remove the loadvmtaddrn }
  487. if p1.nodetype=loadvmtaddrn then
  488. begin
  489. p2:=tloadvmtaddrnode(p1).left;
  490. tloadvmtaddrnode(p1).left:=nil;
  491. p1.free;
  492. p1:=p2;
  493. end;
  494. if not codegenerror then
  495. begin
  496. case p1.resultdef.typ of
  497. procdef, { procvar }
  498. pointerdef,
  499. procvardef,
  500. classrefdef : ;
  501. objectdef :
  502. if not is_implicit_pointer_object_type(p1.resultdef) then
  503. begin
  504. Message(parser_e_illegal_parameter_list);
  505. err:=true;
  506. end;
  507. arraydef :
  508. if not is_dynamic_array(p1.resultdef) then
  509. begin
  510. Message(parser_e_illegal_parameter_list);
  511. err:=true;
  512. end;
  513. else
  514. if p1.resultdef.typ<>undefineddef then
  515. begin
  516. Message(parser_e_illegal_parameter_list);
  517. err:=true;
  518. end;
  519. end;
  520. end
  521. else
  522. err:=true;
  523. if not err then
  524. begin
  525. p2:=ccallparanode.create(p1,nil);
  526. p2:=geninlinenode(in_assigned_x,false,p2);
  527. end
  528. else
  529. begin
  530. p1.free;
  531. p2:=cerrornode.create;
  532. end;
  533. consume(_RKLAMMER);
  534. statement_syssym:=p2;
  535. end;
  536. in_addr_x :
  537. begin
  538. consume(_LKLAMMER);
  539. in_args:=true;
  540. p1:=comp_expr(true,false);
  541. p1:=caddrnode.create(p1);
  542. consume(_RKLAMMER);
  543. statement_syssym:=p1;
  544. end;
  545. in_ofs_x :
  546. begin
  547. if target_info.system in systems_managed_vm then
  548. message(parser_e_feature_unsupported_for_vm);
  549. consume(_LKLAMMER);
  550. in_args:=true;
  551. p1:=comp_expr(true,false);
  552. p1:=caddrnode.create(p1);
  553. do_typecheckpass(p1);
  554. { Ofs() returns a cardinal/qword, not a pointer }
  555. p1.resultdef:=uinttype;
  556. consume(_RKLAMMER);
  557. statement_syssym:=p1;
  558. end;
  559. in_seg_x :
  560. begin
  561. consume(_LKLAMMER);
  562. in_args:=true;
  563. p1:=comp_expr(true,false);
  564. p1:=geninlinenode(in_seg_x,false,p1);
  565. consume(_RKLAMMER);
  566. statement_syssym:=p1;
  567. end;
  568. in_high_x,
  569. in_low_x :
  570. begin
  571. consume(_LKLAMMER);
  572. in_args:=true;
  573. p1:=comp_expr(true,false);
  574. p2:=geninlinenode(l,false,p1);
  575. consume(_RKLAMMER);
  576. statement_syssym:=p2;
  577. end;
  578. in_succ_x,
  579. in_pred_x :
  580. begin
  581. consume(_LKLAMMER);
  582. in_args:=true;
  583. p1:=comp_expr(true,false);
  584. p2:=geninlinenode(l,false,p1);
  585. consume(_RKLAMMER);
  586. statement_syssym:=p2;
  587. end;
  588. in_inc_x,
  589. in_dec_x :
  590. begin
  591. consume(_LKLAMMER);
  592. in_args:=true;
  593. p1:=comp_expr(true,false);
  594. if try_to_consume(_COMMA) then
  595. p2:=ccallparanode.create(comp_expr(true,false),nil)
  596. else
  597. p2:=nil;
  598. p2:=ccallparanode.create(p1,p2);
  599. statement_syssym:=geninlinenode(l,false,p2);
  600. consume(_RKLAMMER);
  601. end;
  602. in_slice_x:
  603. begin
  604. if not(in_args) then
  605. begin
  606. message(parser_e_illegal_slice);
  607. consume(_LKLAMMER);
  608. in_args:=true;
  609. comp_expr(true,false).free;
  610. if try_to_consume(_COMMA) then
  611. comp_expr(true,false).free;
  612. statement_syssym:=cerrornode.create;
  613. consume(_RKLAMMER);
  614. end
  615. else
  616. begin
  617. consume(_LKLAMMER);
  618. in_args:=true;
  619. p1:=comp_expr(true,false);
  620. Consume(_COMMA);
  621. if not(codegenerror) then
  622. p2:=ccallparanode.create(comp_expr(true,false),nil)
  623. else
  624. p2:=cerrornode.create;
  625. p2:=ccallparanode.create(p1,p2);
  626. statement_syssym:=geninlinenode(l,false,p2);
  627. consume(_RKLAMMER);
  628. end;
  629. end;
  630. in_initialize_x:
  631. begin
  632. statement_syssym:=inline_initialize;
  633. end;
  634. in_finalize_x:
  635. begin
  636. statement_syssym:=inline_finalize;
  637. end;
  638. in_copy_x:
  639. begin
  640. statement_syssym:=inline_copy;
  641. end;
  642. in_concat_x :
  643. begin
  644. consume(_LKLAMMER);
  645. in_args:=true;
  646. { Translate to x:=x+y[+z]. The addnode will do the
  647. type checking }
  648. p2:=nil;
  649. repeat
  650. p1:=comp_expr(true,false);
  651. if p2<>nil then
  652. p2:=caddnode.create(addn,p2,p1)
  653. else
  654. begin
  655. { Force string type if it isn't yet }
  656. if not(
  657. (p1.resultdef.typ=stringdef) or
  658. is_chararray(p1.resultdef) or
  659. is_char(p1.resultdef)
  660. ) then
  661. inserttypeconv(p1,cshortstringtype);
  662. p2:=p1;
  663. end;
  664. until not try_to_consume(_COMMA);
  665. consume(_RKLAMMER);
  666. statement_syssym:=p2;
  667. end;
  668. in_read_x,
  669. in_readln_x,
  670. in_readstr_x:
  671. begin
  672. if try_to_consume(_LKLAMMER) then
  673. begin
  674. paras:=parse_paras(false,false,_RKLAMMER);
  675. consume(_RKLAMMER);
  676. end
  677. else
  678. paras:=nil;
  679. p1:=geninlinenode(l,false,paras);
  680. statement_syssym := p1;
  681. end;
  682. in_setlength_x:
  683. begin
  684. statement_syssym := inline_setlength;
  685. end;
  686. in_objc_selector_x:
  687. begin
  688. if (m_objectivec1 in current_settings.modeswitches) then
  689. begin
  690. consume(_LKLAMMER);
  691. in_args:=true;
  692. { don't turn procsyms into calls (getaddr = true) }
  693. p1:=factor(true,false,false);
  694. p2:=geninlinenode(l,false,p1);
  695. consume(_RKLAMMER);
  696. statement_syssym:=p2;
  697. end
  698. else
  699. begin
  700. Message1(sym_e_id_not_found, orgpattern);
  701. statement_syssym:=cerrornode.create;
  702. end;
  703. end;
  704. in_length_x:
  705. begin
  706. consume(_LKLAMMER);
  707. in_args:=true;
  708. p1:=comp_expr(true,false);
  709. p2:=geninlinenode(l,false,p1);
  710. consume(_RKLAMMER);
  711. statement_syssym:=p2;
  712. end;
  713. in_write_x,
  714. in_writeln_x,
  715. in_writestr_x :
  716. begin
  717. if try_to_consume(_LKLAMMER) then
  718. begin
  719. paras:=parse_paras(true,false,_RKLAMMER);
  720. consume(_RKLAMMER);
  721. end
  722. else
  723. paras:=nil;
  724. p1 := geninlinenode(l,false,paras);
  725. statement_syssym := p1;
  726. end;
  727. in_str_x_string :
  728. begin
  729. consume(_LKLAMMER);
  730. paras:=parse_paras(true,false,_RKLAMMER);
  731. consume(_RKLAMMER);
  732. p1 := geninlinenode(l,false,paras);
  733. statement_syssym := p1;
  734. end;
  735. in_val_x:
  736. Begin
  737. consume(_LKLAMMER);
  738. in_args := true;
  739. p1:= ccallparanode.create(comp_expr(true,false), nil);
  740. consume(_COMMA);
  741. p2 := ccallparanode.create(comp_expr(true,false),p1);
  742. if try_to_consume(_COMMA) then
  743. p2 := ccallparanode.create(comp_expr(true,false),p2);
  744. consume(_RKLAMMER);
  745. p2 := geninlinenode(l,false,p2);
  746. statement_syssym := p2;
  747. End;
  748. in_include_x_y,
  749. in_exclude_x_y :
  750. begin
  751. consume(_LKLAMMER);
  752. in_args:=true;
  753. p1:=comp_expr(true,false);
  754. consume(_COMMA);
  755. p2:=comp_expr(true,false);
  756. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  757. consume(_RKLAMMER);
  758. end;
  759. in_pack_x_y_z,
  760. in_unpack_x_y_z :
  761. begin
  762. consume(_LKLAMMER);
  763. in_args:=true;
  764. p1:=comp_expr(true,false);
  765. consume(_COMMA);
  766. p2:=comp_expr(true,false);
  767. consume(_COMMA);
  768. paras:=comp_expr(true,false);
  769. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,ccallparanode.create(paras,nil))));
  770. consume(_RKLAMMER);
  771. end;
  772. in_assert_x_y :
  773. begin
  774. consume(_LKLAMMER);
  775. in_args:=true;
  776. p1:=comp_expr(true,false);
  777. if try_to_consume(_COMMA) then
  778. p2:=comp_expr(true,false)
  779. else
  780. begin
  781. { then insert an empty string }
  782. p2:=cstringconstnode.createstr('');
  783. end;
  784. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  785. consume(_RKLAMMER);
  786. end;
  787. in_get_frame:
  788. begin
  789. statement_syssym:=geninlinenode(l,false,nil);
  790. end;
  791. (*
  792. in_get_caller_frame:
  793. begin
  794. if try_to_consume(_LKLAMMER) then
  795. begin
  796. {You used to call get_caller_frame as get_caller_frame(get_frame),
  797. however, as a stack frame may not exist, it does more harm than
  798. good, so ignore it.}
  799. in_args:=true;
  800. p1:=comp_expr(true,false);
  801. p1.destroy;
  802. consume(_RKLAMMER);
  803. end;
  804. statement_syssym:=geninlinenode(l,false,nil);
  805. end;
  806. *)
  807. in_default_x:
  808. begin
  809. consume(_LKLAMMER);
  810. in_args:=true;
  811. def:=nil;
  812. single_type(def,[stoAllowSpecialization]);
  813. statement_syssym:=cerrornode.create;
  814. if def<>generrordef then
  815. { "type expected" error is already done by single_type }
  816. if def.typ=forwarddef then
  817. Message1(type_e_type_is_not_completly_defined,tforwarddef(def).tosymname^)
  818. else
  819. begin
  820. statement_syssym.free;
  821. statement_syssym:=geninlinenode(in_default_x,false,ctypenode.create(def));
  822. end;
  823. { consume the right bracket here for a nicer error position }
  824. consume(_RKLAMMER);
  825. end;
  826. in_setstring_x_y_z:
  827. begin
  828. statement_syssym := inline_setstring;
  829. end;
  830. else
  831. internalerror(15);
  832. end;
  833. in_args:=prev_in_args;
  834. end;
  835. function maybe_load_methodpointer(st:TSymtable;var p1:tnode):boolean;
  836. var
  837. pd: tprocdef;
  838. begin
  839. maybe_load_methodpointer:=false;
  840. if not assigned(p1) then
  841. begin
  842. case st.symtabletype of
  843. withsymtable :
  844. begin
  845. if (st.defowner.typ=objectdef) then
  846. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  847. end;
  848. ObjectSymtable,
  849. recordsymtable:
  850. begin
  851. { Escape nested procedures }
  852. if assigned(current_procinfo) then
  853. begin
  854. pd:=current_procinfo.get_normal_proc.procdef;
  855. { We are calling from the static class method which has no self node }
  856. if assigned(pd) and pd.no_self_node then
  857. if st.symtabletype=recordsymtable then
  858. p1:=ctypenode.create(pd.struct)
  859. else
  860. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  861. else
  862. p1:=load_self_node;
  863. end
  864. else
  865. p1:=load_self_node;
  866. { We are calling a member }
  867. maybe_load_methodpointer:=true;
  868. end;
  869. end;
  870. end;
  871. end;
  872. { reads the parameter for a subroutine call }
  873. procedure do_proc_call(sym:tsym;st:TSymtable;obj:tabstractrecorddef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags;spezcontext:tspecializationcontext);
  874. var
  875. membercall,
  876. prevafterassn : boolean;
  877. i : integer;
  878. para,p2 : tnode;
  879. currpara : tparavarsym;
  880. aprocdef : tprocdef;
  881. begin
  882. prevafterassn:=afterassignment;
  883. afterassignment:=false;
  884. membercall:=false;
  885. aprocdef:=nil;
  886. { when it is a call to a member we need to load the
  887. methodpointer first
  888. }
  889. membercall:=maybe_load_methodpointer(st,p1);
  890. { When we are expecting a procvar we also need
  891. to get the address in some cases }
  892. if assigned(getprocvardef) then
  893. begin
  894. if (block_type=bt_const) or
  895. getaddr then
  896. begin
  897. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  898. getaddr:=true;
  899. end
  900. else
  901. if ((m_tp_procvar in current_settings.modeswitches) or
  902. (m_mac_procvar in current_settings.modeswitches)) and
  903. not(token in [_CARET,_POINT,_LKLAMMER]) then
  904. begin
  905. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  906. if assigned(aprocdef) then
  907. getaddr:=true;
  908. end;
  909. end;
  910. { only need to get the address of the procedure? }
  911. if getaddr then
  912. begin
  913. { for now we don't support pointers to generic functions, but since
  914. this is only temporary we use a non translated message }
  915. if assigned(spezcontext) then
  916. begin
  917. comment(v_error, 'Pointers to generics functions not implemented');
  918. p1:=cerrornode.create;
  919. spezcontext.free;
  920. exit;
  921. end;
  922. { Retrieve info which procvar to call. For tp_procvar the
  923. aprocdef is already loaded above so we can reuse it }
  924. if not assigned(aprocdef) and
  925. assigned(getprocvardef) then
  926. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  927. { generate a methodcallnode or proccallnode }
  928. { we shouldn't convert things like @tcollection.load }
  929. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  930. if assigned(p1) then
  931. begin
  932. { for loading methodpointer of an inherited function
  933. we use self as instance and load the address of
  934. the function directly and not through the vmt (PFV) }
  935. if (cnf_inherited in callflags) then
  936. begin
  937. include(tloadnode(p2).loadnodeflags,loadnf_inherited);
  938. p1.free;
  939. p1:=load_self_node;
  940. end;
  941. if (p1.nodetype<>typen) then
  942. tloadnode(p2).set_mp(p1)
  943. else
  944. p1.free;
  945. end;
  946. p1:=p2;
  947. { no postfix operators }
  948. again:=false;
  949. end
  950. else
  951. begin
  952. para:=nil;
  953. if anon_inherited then
  954. begin
  955. if not assigned(current_procinfo) then
  956. internalerror(200305054);
  957. for i:=0 to current_procinfo.procdef.paras.count-1 do
  958. begin
  959. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  960. if not(vo_is_hidden_para in currpara.varoptions) then
  961. begin
  962. { inheritance by msgint? }
  963. if assigned(srdef) then
  964. { anonymous inherited via msgid calls only require a var parameter for
  965. both methods, so we need some type casting here }
  966. para:=ccallparanode.create(ctypeconvnode.create_internal(ctypeconvnode.create_internal(
  967. cloadnode.create(currpara,currpara.owner),cformaltype),tparavarsym(tprocdef(srdef).paras[i]).vardef),
  968. para)
  969. else
  970. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  971. end;
  972. end;
  973. end
  974. else
  975. begin
  976. if try_to_consume(_LKLAMMER) then
  977. begin
  978. para:=parse_paras(false,false,_RKLAMMER);
  979. consume(_RKLAMMER);
  980. end;
  981. end;
  982. { indicate if this call was generated by a member and
  983. no explicit self is used, this is needed to determine
  984. how to handle a destructor call (PFV) }
  985. if membercall then
  986. include(callflags,cnf_member_call);
  987. if assigned(obj) then
  988. begin
  989. if not (st.symtabletype in [ObjectSymtable,recordsymtable]) then
  990. internalerror(200310031);
  991. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags,spezcontext);
  992. end
  993. else
  994. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags,spezcontext);
  995. end;
  996. afterassignment:=prevafterassn;
  997. end;
  998. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  999. var
  1000. hp,hp2 : tnode;
  1001. hpp : ^tnode;
  1002. currprocdef : tprocdef;
  1003. begin
  1004. if not assigned(pv) then
  1005. internalerror(200301121);
  1006. if (m_tp_procvar in current_settings.modeswitches) or
  1007. (m_mac_procvar in current_settings.modeswitches) then
  1008. begin
  1009. hp:=p2;
  1010. hpp:=@p2;
  1011. while assigned(hp) and
  1012. (hp.nodetype=typeconvn) do
  1013. begin
  1014. hp:=ttypeconvnode(hp).left;
  1015. { save orignal address of the old tree so we can replace the node }
  1016. hpp:=@hp;
  1017. end;
  1018. if (hp.nodetype=calln) and
  1019. { a procvar can't have parameters! }
  1020. not assigned(tcallnode(hp).left) then
  1021. begin
  1022. currprocdef:=tcallnode(hp).symtableprocentry.Find_procdef_byprocvardef(pv);
  1023. if assigned(currprocdef) then
  1024. begin
  1025. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  1026. if (po_methodpointer in pv.procoptions) then
  1027. tloadnode(hp2).set_mp(tcallnode(hp).methodpointer.getcopy);
  1028. hp.destroy;
  1029. { replace the old callnode with the new loadnode }
  1030. hpp^:=hp2;
  1031. end;
  1032. end;
  1033. end;
  1034. end;
  1035. { the following procedure handles the access to a property symbol }
  1036. procedure handle_propertysym(propsym : tpropertysym;st : TSymtable;var p1 : tnode);
  1037. var
  1038. paras : tnode;
  1039. p2 : tnode;
  1040. membercall : boolean;
  1041. callflags : tcallnodeflags;
  1042. propaccesslist : tpropaccesslist;
  1043. sym: tsym;
  1044. begin
  1045. { property parameters? read them only if the property really }
  1046. { has parameters }
  1047. paras:=nil;
  1048. if (ppo_hasparameters in propsym.propoptions) then
  1049. begin
  1050. if try_to_consume(_LECKKLAMMER) then
  1051. begin
  1052. paras:=parse_paras(false,false,_RECKKLAMMER);
  1053. consume(_RECKKLAMMER);
  1054. end;
  1055. end;
  1056. { indexed property }
  1057. if (ppo_indexed in propsym.propoptions) then
  1058. begin
  1059. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  1060. paras:=ccallparanode.create(p2,paras);
  1061. end;
  1062. { we need only a write property if a := follows }
  1063. { if not(afterassignment) and not(in_args) then }
  1064. if token=_ASSIGNMENT then
  1065. begin
  1066. if propsym.getpropaccesslist(palt_write,propaccesslist) then
  1067. begin
  1068. sym:=propaccesslist.firstsym^.sym;
  1069. case sym.typ of
  1070. procsym :
  1071. begin
  1072. callflags:=[];
  1073. { generate the method call }
  1074. membercall:=maybe_load_methodpointer(st,p1);
  1075. if membercall then
  1076. include(callflags,cnf_member_call);
  1077. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags,nil);
  1078. addsymref(sym);
  1079. paras:=nil;
  1080. consume(_ASSIGNMENT);
  1081. { read the expression }
  1082. if propsym.propdef.typ=procvardef then
  1083. getprocvardef:=tprocvardef(propsym.propdef);
  1084. p2:=comp_expr(true,false);
  1085. if assigned(getprocvardef) then
  1086. handle_procvar(getprocvardef,p2);
  1087. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1088. { mark as property, both the tcallnode and the real call block }
  1089. include(p1.flags,nf_isproperty);
  1090. getprocvardef:=nil;
  1091. end;
  1092. fieldvarsym :
  1093. begin
  1094. { generate access code }
  1095. if not handle_staticfield_access(sym,false,p1) then
  1096. propaccesslist_to_node(p1,st,propaccesslist);
  1097. include(p1.flags,nf_isproperty);
  1098. consume(_ASSIGNMENT);
  1099. { read the expression }
  1100. p2:=comp_expr(true,false);
  1101. p1:=cassignmentnode.create(p1,p2);
  1102. end
  1103. else
  1104. begin
  1105. p1:=cerrornode.create;
  1106. Message(parser_e_no_procedure_to_access_property);
  1107. end;
  1108. end;
  1109. end
  1110. else
  1111. begin
  1112. p1:=cerrornode.create;
  1113. Message(parser_e_no_procedure_to_access_property);
  1114. end;
  1115. end
  1116. else
  1117. begin
  1118. if propsym.getpropaccesslist(palt_read,propaccesslist) then
  1119. begin
  1120. sym := propaccesslist.firstsym^.sym;
  1121. case sym.typ of
  1122. fieldvarsym :
  1123. begin
  1124. { generate access code }
  1125. if not handle_staticfield_access(sym,false,p1) then
  1126. propaccesslist_to_node(p1,st,propaccesslist);
  1127. include(p1.flags,nf_isproperty);
  1128. { catch expressions like "(propx):=1;" }
  1129. include(p1.flags,nf_no_lvalue);
  1130. end;
  1131. procsym :
  1132. begin
  1133. callflags:=[];
  1134. { generate the method call }
  1135. membercall:=maybe_load_methodpointer(st,p1);
  1136. if membercall then
  1137. include(callflags,cnf_member_call);
  1138. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags,nil);
  1139. paras:=nil;
  1140. include(p1.flags,nf_isproperty);
  1141. include(p1.flags,nf_no_lvalue);
  1142. end
  1143. else
  1144. begin
  1145. p1:=cerrornode.create;
  1146. Message(type_e_mismatch);
  1147. end;
  1148. end;
  1149. end
  1150. else
  1151. begin
  1152. { error, no function to read property }
  1153. p1:=cerrornode.create;
  1154. Message(parser_e_no_procedure_to_access_property);
  1155. end;
  1156. end;
  1157. { release paras if not used }
  1158. if assigned(paras) then
  1159. paras.free;
  1160. end;
  1161. { the ID token has to be consumed before calling this function }
  1162. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags;spezcontext:tspecializationcontext);
  1163. var
  1164. isclassref:boolean;
  1165. begin
  1166. if sym=nil then
  1167. begin
  1168. { pattern is still valid unless
  1169. there is another ID just after the ID of sym }
  1170. Message1(sym_e_id_no_member,orgpattern);
  1171. p1.free;
  1172. p1:=cerrornode.create;
  1173. { try to clean up }
  1174. spezcontext.free;
  1175. again:=false;
  1176. end
  1177. else
  1178. begin
  1179. if assigned(p1) then
  1180. begin
  1181. if not assigned(p1.resultdef) then
  1182. do_typecheckpass(p1);
  1183. isclassref:=(p1.resultdef.typ=classrefdef);
  1184. end
  1185. else
  1186. isclassref:=false;
  1187. if assigned(spezcontext) and not (sym.typ=procsym) then
  1188. internalerror(2015091801);
  1189. { we assume, that only procsyms and varsyms are in an object }
  1190. { symbol table, for classes, properties are allowed }
  1191. case sym.typ of
  1192. procsym:
  1193. begin
  1194. do_proc_call(sym,sym.owner,structh,
  1195. (getaddr and not(token in [_CARET,_POINT])),
  1196. again,p1,callflags,spezcontext);
  1197. { we need to know which procedure is called }
  1198. do_typecheckpass(p1);
  1199. { calling using classref? }
  1200. if isclassref and
  1201. (p1.nodetype=calln) and
  1202. assigned(tcallnode(p1).procdefinition) then
  1203. begin
  1204. if not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1205. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1206. Message(parser_e_only_class_members_via_class_ref);
  1207. { in Java, constructors are not automatically inherited
  1208. -> calling a constructor from a parent type will create
  1209. an instance of that parent type! }
  1210. if is_javaclass(structh) and
  1211. (tcallnode(p1).procdefinition.proctypeoption=potype_constructor) and
  1212. (tcallnode(p1).procdefinition.owner.defowner<>find_real_class_definition(tobjectdef(structh),false)) then
  1213. Message(parser_e_java_no_inherited_constructor);
  1214. { Provide a warning if we try to create an instance of a
  1215. abstract class using the type name of that class. We
  1216. must not provide a warning if we use a "class of"
  1217. variable of that type though as we don't know the
  1218. type of the class }
  1219. if (tcallnode(p1).procdefinition.proctypeoption=potype_constructor) and
  1220. (oo_is_abstract in structh.objectoptions) and
  1221. assigned(tcallnode(p1).methodpointer) and
  1222. (tcallnode(p1).methodpointer.nodetype=loadvmtaddrn) then
  1223. Message1(type_w_instance_abstract_class,structh.RttiName);
  1224. end;
  1225. end;
  1226. fieldvarsym:
  1227. begin
  1228. if not handle_staticfield_access(sym,true,p1) then
  1229. begin
  1230. if isclassref then
  1231. if assigned(p1) and
  1232. (
  1233. is_self_node(p1) or
  1234. (assigned(current_procinfo) and (current_procinfo.get_normal_proc.procdef.no_self_node) and
  1235. (current_procinfo.procdef.struct=structh))) then
  1236. Message(parser_e_only_class_members)
  1237. else
  1238. Message(parser_e_only_class_members_via_class_ref);
  1239. p1:=csubscriptnode.create(sym,p1);
  1240. end;
  1241. end;
  1242. propertysym:
  1243. begin
  1244. if isclassref and not (sp_static in sym.symoptions) then
  1245. Message(parser_e_only_class_members_via_class_ref);
  1246. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1247. end;
  1248. typesym:
  1249. begin
  1250. p1.free;
  1251. if try_to_consume(_LKLAMMER) then
  1252. begin
  1253. p1:=comp_expr(true,false);
  1254. consume(_RKLAMMER);
  1255. p1:=ctypeconvnode.create_explicit(p1,ttypesym(sym).typedef);
  1256. end
  1257. else
  1258. begin
  1259. p1:=ctypenode.create(ttypesym(sym).typedef);
  1260. if (is_class(ttypesym(sym).typedef) or
  1261. is_objcclass(ttypesym(sym).typedef) or
  1262. is_javaclass(ttypesym(sym).typedef)) and
  1263. not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1264. p1:=cloadvmtaddrnode.create(p1);
  1265. end;
  1266. end;
  1267. constsym:
  1268. begin
  1269. p1.free;
  1270. p1:=genconstsymtree(tconstsym(sym));
  1271. end;
  1272. staticvarsym:
  1273. begin
  1274. { typed constant is a staticvarsym
  1275. now they are absolutevarsym }
  1276. p1.free;
  1277. p1:=cloadnode.create(sym,sym.Owner);
  1278. end;
  1279. absolutevarsym:
  1280. begin
  1281. p1.free;
  1282. p1:=nil;
  1283. { typed constants are absolutebarsyms now to handle storage properly }
  1284. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1285. end
  1286. else
  1287. internalerror(16);
  1288. end;
  1289. end;
  1290. end;
  1291. function handle_specialize_inline_specialization(var srsym:tsym;out srsymtable:tsymtable;out spezcontext:tspecializationcontext):boolean;
  1292. var
  1293. spezdef : tdef;
  1294. begin
  1295. result:=false;
  1296. spezcontext:=nil;
  1297. srsymtable:=nil;
  1298. if not assigned(srsym) then
  1299. message1(sym_e_id_no_member,orgpattern)
  1300. else
  1301. if not (srsym.typ in [typesym,procsym]) then
  1302. message(type_e_type_id_expected)
  1303. else
  1304. begin
  1305. if srsym.typ=typesym then
  1306. spezdef:=ttypesym(srsym).typedef
  1307. else
  1308. spezdef:=tdef(tprocsym(srsym).procdeflist[0]);
  1309. spezdef:=generate_specialization_phase1(spezcontext,spezdef);
  1310. case spezdef.typ of
  1311. errordef:
  1312. begin
  1313. spezcontext.free;
  1314. spezcontext:=nil;
  1315. srsym:=generrorsym;
  1316. end;
  1317. procdef:
  1318. begin
  1319. if block_type<>bt_body then
  1320. begin
  1321. message(parser_e_illegal_expression);
  1322. spezcontext.free;
  1323. spezcontext:=nil;
  1324. srsym:=generrorsym;
  1325. end
  1326. else
  1327. begin
  1328. srsym:=tprocdef(spezdef).procsym;
  1329. srsymtable:=srsym.owner;
  1330. result:=true;
  1331. end;
  1332. end;
  1333. objectdef,
  1334. recorddef,
  1335. arraydef,
  1336. procvardef:
  1337. begin
  1338. spezdef:=generate_specialization_phase2(spezcontext,tstoreddef(spezdef),false,'');
  1339. spezcontext.free;
  1340. spezcontext:=nil;
  1341. srsym:=spezdef.typesym;
  1342. srsymtable:=srsym.owner;
  1343. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1344. result:=true;
  1345. end;
  1346. else
  1347. internalerror(2015070302);
  1348. end;
  1349. end;
  1350. end;
  1351. function handle_factor_typenode(hdef:tdef;getaddr:boolean;var again:boolean;sym:tsym;typeonly:boolean):tnode;
  1352. var
  1353. srsym : tsym;
  1354. srsymtable : tsymtable;
  1355. isspecialize : boolean;
  1356. spezcontext : tspecializationcontext;
  1357. begin
  1358. spezcontext:=nil;
  1359. if sym=nil then
  1360. sym:=hdef.typesym;
  1361. { allow Ordinal(Value) for type declarations since it
  1362. can be an enummeration declaration or a set lke:
  1363. (OrdinalType(const1)..OrdinalType(const2) }
  1364. if (not typeonly or is_ordinal(hdef)) and
  1365. try_to_consume(_LKLAMMER) then
  1366. begin
  1367. result:=comp_expr(true,false);
  1368. consume(_RKLAMMER);
  1369. { type casts to class helpers aren't allowed }
  1370. if is_objectpascal_helper(hdef) then
  1371. Message(parser_e_no_category_as_types)
  1372. { recovery by not creating a conversion node }
  1373. else
  1374. result:=ctypeconvnode.create_explicit(result,hdef);
  1375. end
  1376. { not LKLAMMER }
  1377. else if (token=_POINT) and
  1378. (is_object(hdef) or is_record(hdef)) then
  1379. begin
  1380. consume(_POINT);
  1381. { handles calling methods declared in parent objects
  1382. using "parentobject.methodname()" }
  1383. if assigned(current_structdef) and
  1384. not(getaddr) and
  1385. def_is_related(current_structdef,hdef) then
  1386. begin
  1387. result:=ctypenode.create(hdef);
  1388. ttypenode(result).typesym:=sym;
  1389. if not (m_delphi in current_settings.modeswitches) and
  1390. (block_type in [bt_type,bt_var_type,bt_const_type]) and
  1391. (token=_ID) and
  1392. (idtoken=_SPECIALIZE) then
  1393. begin
  1394. consume(_ID);
  1395. if token<>_ID then
  1396. message(type_e_type_id_expected);
  1397. isspecialize:=true;
  1398. end
  1399. else
  1400. isspecialize:=false;
  1401. { search also in inherited methods }
  1402. searchsym_in_class(tobjectdef(hdef),tobjectdef(current_structdef),pattern,srsym,srsymtable,[ssf_search_helper]);
  1403. if isspecialize then
  1404. begin
  1405. consume(_ID);
  1406. if not handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  1407. begin
  1408. result.free;
  1409. result:=cerrornode.create;
  1410. end;
  1411. end
  1412. else
  1413. begin
  1414. if assigned(srsym) then
  1415. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1416. consume(_ID);
  1417. end;
  1418. if result.nodetype<>errorn then
  1419. do_member_read(tabstractrecorddef(hdef),false,srsym,result,again,[],spezcontext)
  1420. else
  1421. spezcontext.free;
  1422. end
  1423. else
  1424. begin
  1425. { handles:
  1426. * @TObject.Load
  1427. * static methods and variables }
  1428. result:=ctypenode.create(hdef);
  1429. ttypenode(result).typesym:=sym;
  1430. if not (m_delphi in current_settings.modeswitches) and
  1431. (block_type in [bt_type,bt_var_type,bt_const_type]) and
  1432. (token=_ID) and
  1433. (idtoken=_SPECIALIZE) then
  1434. begin
  1435. consume(_ID);
  1436. if token<>_ID then
  1437. message(type_e_type_id_expected);
  1438. isspecialize:=true;
  1439. end
  1440. else
  1441. isspecialize:=false;
  1442. { TP allows also @TMenu.Load if Load is only }
  1443. { defined in an anchestor class }
  1444. srsym:=search_struct_member(tabstractrecorddef(hdef),pattern);
  1445. if isspecialize then
  1446. begin
  1447. consume(_ID);
  1448. if not handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  1449. begin
  1450. result.free;
  1451. result:=cerrornode.create;
  1452. end;
  1453. end
  1454. else
  1455. begin
  1456. if assigned(srsym) then
  1457. begin
  1458. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1459. consume(_ID);
  1460. end
  1461. else
  1462. Message1(sym_e_id_no_member,orgpattern);
  1463. end;
  1464. if (result.nodetype<>errorn) and assigned(srsym) then
  1465. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,result,again,[],spezcontext)
  1466. else
  1467. spezcontext.free;
  1468. end;
  1469. end
  1470. else
  1471. begin
  1472. { Normally here would be the check against the usage
  1473. of "TClassHelper.Something", but as that might be
  1474. used inside of system symbols like sizeof and
  1475. typeinfo this check is put into ttypenode.pass_1
  1476. (for "TClassHelper" alone) and tcallnode.pass_1
  1477. (for "TClassHelper.Something") }
  1478. { class reference ? }
  1479. if is_class(hdef) or
  1480. is_objcclass(hdef) or
  1481. { Java interfaces also can have loadvmtaddrnodes,
  1482. e.g. for expressions such as JLClass(intftype) }
  1483. is_java_class_or_interface(hdef) then
  1484. begin
  1485. if getaddr and (token=_POINT) and
  1486. not is_javainterface(hdef) then
  1487. begin
  1488. consume(_POINT);
  1489. { allows @Object.Method }
  1490. { also allows static methods and variables }
  1491. result:=ctypenode.create(hdef);
  1492. ttypenode(result).typesym:=sym;
  1493. { TP allows also @TMenu.Load if Load is only }
  1494. { defined in an anchestor class }
  1495. srsym:=search_struct_member(tobjectdef(hdef),pattern);
  1496. if assigned(srsym) then
  1497. begin
  1498. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1499. consume(_ID);
  1500. { in case of @Object.Method1.Method2, we have to call
  1501. Method1 -> create a loadvmtaddr node as self instead of
  1502. a typen (the typenode would be changed to self of the
  1503. current method in case Method1 is a constructor, see
  1504. mantis #24844) }
  1505. if not(block_type in [bt_type,bt_const_type,bt_var_type]) and
  1506. (srsym.typ=procsym) and
  1507. (token in [_CARET,_POINT]) then
  1508. result:=cloadvmtaddrnode.create(result);
  1509. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,result,again,[],nil);
  1510. end
  1511. else
  1512. begin
  1513. Message1(sym_e_id_no_member,orgpattern);
  1514. consume(_ID);
  1515. end;
  1516. end
  1517. else
  1518. begin
  1519. result:=ctypenode.create(hdef);
  1520. ttypenode(result).typesym:=sym;
  1521. { For a type block we simply return only
  1522. the type. For all other blocks we return
  1523. a loadvmt node }
  1524. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1525. result:=cloadvmtaddrnode.create(result);
  1526. end;
  1527. end
  1528. else
  1529. begin
  1530. result:=ctypenode.create(hdef);
  1531. ttypenode(result).typesym:=sym;
  1532. end;
  1533. end;
  1534. end;
  1535. {****************************************************************************
  1536. Factor
  1537. ****************************************************************************}
  1538. function real_const_node_from_pattern(s:string):tnode;
  1539. var
  1540. d : bestreal;
  1541. code : integer;
  1542. cur : currency;
  1543. begin
  1544. val(s,d,code);
  1545. if code<>0 then
  1546. begin
  1547. Message(parser_e_error_in_real);
  1548. d:=1.0;
  1549. end;
  1550. if current_settings.fputype=fpu_none then
  1551. Message(parser_e_unsupported_real);
  1552. if (current_settings.minfpconstprec=s32real) and
  1553. (d = single(d)) then
  1554. result:=crealconstnode.create(d,s32floattype)
  1555. else if (current_settings.minfpconstprec=s64real) and
  1556. (d = double(d)) then
  1557. result:=crealconstnode.create(d,s64floattype)
  1558. else
  1559. result:=crealconstnode.create(d,pbestrealtype^);
  1560. val(pattern,cur,code);
  1561. if code=0 then
  1562. trealconstnode(result).value_currency:=cur;
  1563. end;
  1564. {---------------------------------------------
  1565. PostFixOperators
  1566. ---------------------------------------------}
  1567. { returns whether or not p1 has been changed }
  1568. function postfixoperators(var p1:tnode;var again:boolean;getaddr:boolean): boolean;
  1569. { tries to avoid syntax errors after invalid qualifiers }
  1570. procedure recoverconsume_postfixops;
  1571. begin
  1572. repeat
  1573. if not try_to_consume(_CARET) then
  1574. if try_to_consume(_POINT) then
  1575. try_to_consume(_ID)
  1576. else if try_to_consume(_LECKKLAMMER) then
  1577. begin
  1578. repeat
  1579. comp_expr(true,false);
  1580. until not try_to_consume(_COMMA);
  1581. consume(_RECKKLAMMER);
  1582. end
  1583. else if try_to_consume(_LKLAMMER) then
  1584. begin
  1585. repeat
  1586. comp_expr(true,false);
  1587. until not try_to_consume(_COMMA);
  1588. consume(_RKLAMMER);
  1589. end
  1590. else
  1591. break;
  1592. until false;
  1593. end;
  1594. procedure handle_variantarray;
  1595. var
  1596. p4 : tnode;
  1597. newstatement : tstatementnode;
  1598. tempresultvariant,
  1599. temp : ttempcreatenode;
  1600. paras : tcallparanode;
  1601. newblock : tnode;
  1602. countindices : aint;
  1603. begin
  1604. { create statements with call initialize the arguments and
  1605. call fpc_dynarr_setlength }
  1606. newblock:=internalstatements(newstatement);
  1607. { get temp for array of indicies,
  1608. we set the real size later }
  1609. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1610. addstatement(newstatement,temp);
  1611. countindices:=0;
  1612. repeat
  1613. p4:=comp_expr(true,false);
  1614. addstatement(newstatement,cassignmentnode.create(
  1615. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1616. inc(countindices);
  1617. until not try_to_consume(_COMMA);
  1618. { set real size }
  1619. temp.size:=countindices*s32inttype.size;
  1620. consume(_RECKKLAMMER);
  1621. { we need only a write access if a := follows }
  1622. if token=_ASSIGNMENT then
  1623. begin
  1624. consume(_ASSIGNMENT);
  1625. p4:=comp_expr(true,false);
  1626. { create call to fpc_vararray_put }
  1627. paras:=ccallparanode.create(cordconstnode.create
  1628. (countindices,s32inttype,true),
  1629. ccallparanode.create(caddrnode.create_internal
  1630. (ctemprefnode.create(temp)),
  1631. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1632. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1633. ,nil))));
  1634. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1635. addstatement(newstatement,ctempdeletenode.create(temp));
  1636. end
  1637. else
  1638. begin
  1639. { create temp for result }
  1640. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1641. addstatement(newstatement,tempresultvariant);
  1642. { create call to fpc_vararray_get }
  1643. paras:=ccallparanode.create(cordconstnode.create
  1644. (countindices,s32inttype,true),
  1645. ccallparanode.create(caddrnode.create_internal
  1646. (ctemprefnode.create(temp)),
  1647. ccallparanode.create(p1,
  1648. ccallparanode.create(
  1649. ctemprefnode.create(tempresultvariant)
  1650. ,nil))));
  1651. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1652. addstatement(newstatement,ctempdeletenode.create(temp));
  1653. { the last statement should return the value as
  1654. location and type, this is done be referencing the
  1655. temp and converting it first from a persistent temp to
  1656. normal temp }
  1657. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1658. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1659. end;
  1660. p1:=newblock;
  1661. end;
  1662. function parse_array_constructor(arrdef:tarraydef): tnode;
  1663. var
  1664. newstatement,assstatement:tstatementnode;
  1665. arrnode:ttempcreatenode;
  1666. temp2:ttempcreatenode;
  1667. assnode:tnode;
  1668. paracount:integer;
  1669. begin
  1670. result:=internalstatements(newstatement);
  1671. { create temp for result }
  1672. arrnode:=ctempcreatenode.create(arrdef,arrdef.size,tt_persistent,true);
  1673. addstatement(newstatement,arrnode);
  1674. paracount:=0;
  1675. { check arguments and create an assignment calls }
  1676. if try_to_consume(_LKLAMMER) then
  1677. begin
  1678. assnode:=internalstatements(assstatement);
  1679. repeat
  1680. { arr[i] := param_i }
  1681. addstatement(assstatement,
  1682. cassignmentnode.create(
  1683. cvecnode.create(
  1684. ctemprefnode.create(arrnode),
  1685. cordconstnode.create(paracount,arrdef.rangedef,false)),
  1686. comp_expr(true,false)));
  1687. inc(paracount);
  1688. until not try_to_consume(_COMMA);
  1689. consume(_RKLAMMER);
  1690. end
  1691. else
  1692. assnode:=nil;
  1693. { get temp for array of lengths }
  1694. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1695. addstatement(newstatement,temp2);
  1696. { one dimensional }
  1697. addstatement(newstatement,cassignmentnode.create(
  1698. ctemprefnode.create_offset(temp2,0),
  1699. cordconstnode.create
  1700. (paracount,s32inttype,true)));
  1701. { create call to fpc_dynarr_setlength }
  1702. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1703. ccallparanode.create(caddrnode.create_internal
  1704. (ctemprefnode.create(temp2)),
  1705. ccallparanode.create(cordconstnode.create
  1706. (1,s32inttype,true),
  1707. ccallparanode.create(caddrnode.create_internal
  1708. (crttinode.create(tstoreddef(arrdef),initrtti,rdt_normal)),
  1709. ccallparanode.create(
  1710. ctypeconvnode.create_internal(
  1711. ctemprefnode.create(arrnode),voidpointertype),
  1712. nil))))
  1713. ));
  1714. { add assignment statememnts }
  1715. addstatement(newstatement,ctempdeletenode.create(temp2));
  1716. if assigned(assnode) then
  1717. addstatement(newstatement,assnode);
  1718. { the last statement should return the value as
  1719. location and type, this is done be referencing the
  1720. temp and converting it first from a persistent temp to
  1721. normal temp }
  1722. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1723. addstatement(newstatement,ctemprefnode.create(arrnode));
  1724. end;
  1725. function try_type_helper(var node:tnode;def:tdef):boolean;
  1726. var
  1727. srsym : tsym;
  1728. srsymtable : tsymtable;
  1729. n : tnode;
  1730. newstatement : tstatementnode;
  1731. temp : ttempcreatenode;
  1732. extdef : tdef;
  1733. begin
  1734. result:=false;
  1735. if (token=_ID) and (block_type in [bt_body,bt_general,bt_except,bt_const]) then
  1736. begin
  1737. if not assigned(def) then
  1738. if node.nodetype=addrn then
  1739. { always use the pointer type for addr nodes as otherwise
  1740. we'll have an anonymous pointertype with no name }
  1741. def:=voidpointertype
  1742. else
  1743. def:=node.resultdef;
  1744. result:=search_objectpascal_helper(def,nil,pattern,srsym,srsymtable);
  1745. if result then
  1746. begin
  1747. if not (srsymtable.symtabletype=objectsymtable) or
  1748. not is_objectpascal_helper(tdef(srsymtable.defowner)) then
  1749. internalerror(2013011401);
  1750. { convert const node to temp node of the extended type }
  1751. if node.nodetype in (nodetype_const+[niln,addrn]) then
  1752. begin
  1753. extdef:=tobjectdef(srsymtable.defowner).extendeddef;
  1754. newstatement:=nil;
  1755. n:=internalstatements(newstatement);
  1756. temp:=ctempcreatenode.create(extdef,extdef.size,tt_persistent,false);
  1757. addstatement(newstatement,temp);
  1758. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),node));
  1759. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1760. addstatement(newstatement,ctemprefnode.create(temp));
  1761. node:=n;
  1762. do_typecheckpass(node)
  1763. end;
  1764. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1765. consume(_ID);
  1766. do_member_read(nil,getaddr,srsym,node,again,[],nil);
  1767. end;
  1768. end;
  1769. end;
  1770. var
  1771. protsym : tpropertysym;
  1772. p2,p3 : tnode;
  1773. srsym : tsym;
  1774. srsymtable : TSymtable;
  1775. structh : tabstractrecorddef;
  1776. { shouldn't be used that often, so the extra overhead is ok to save
  1777. stack space }
  1778. dispatchstring : ansistring;
  1779. erroroutp1,
  1780. allowspecialize,
  1781. isspecialize,
  1782. found,
  1783. haderror,
  1784. nodechanged : boolean;
  1785. calltype: tdispcalltype;
  1786. valstr,expstr : string;
  1787. intval : qword;
  1788. code : integer;
  1789. strdef : tdef;
  1790. spezcontext : tspecializationcontext;
  1791. old_current_filepos : tfileposinfo;
  1792. label
  1793. skipreckklammercheck,
  1794. skippointdefcheck;
  1795. begin
  1796. result:=false;
  1797. again:=true;
  1798. while again do
  1799. begin
  1800. spezcontext:=nil;
  1801. { we need the resultdef }
  1802. do_typecheckpass_changed(p1,nodechanged);
  1803. result:=result or nodechanged;
  1804. if codegenerror then
  1805. begin
  1806. recoverconsume_postfixops;
  1807. exit;
  1808. end;
  1809. { handle token }
  1810. case token of
  1811. _CARET:
  1812. begin
  1813. consume(_CARET);
  1814. { support tp/mac procvar^ if the procvar returns a
  1815. pointer type }
  1816. if ((m_tp_procvar in current_settings.modeswitches) or
  1817. (m_mac_procvar in current_settings.modeswitches)) and
  1818. (p1.resultdef.typ=procvardef) and
  1819. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1820. begin
  1821. p1:=ccallnode.create_procvar(nil,p1);
  1822. typecheckpass(p1);
  1823. end;
  1824. { iso file buf access? }
  1825. if (m_iso in current_settings.modeswitches) and
  1826. (p1.resultdef.typ=filedef) then
  1827. begin
  1828. case tfiledef(p1.resultdef).filetyp of
  1829. ft_text:
  1830. begin
  1831. p1:=cderefnode.create(ccallnode.createintern('fpc_getbuf_text',ccallparanode.create(p1,nil)));
  1832. typecheckpass(p1);
  1833. end;
  1834. ft_typed:
  1835. begin
  1836. p1:=cderefnode.create(ctypeconvnode.create_internal(ccallnode.createintern('fpc_getbuf_typedfile',ccallparanode.create(p1,nil)),
  1837. cpointerdef.getreusable(tfiledef(p1.resultdef).typedfiledef)));
  1838. typecheckpass(p1);
  1839. end;
  1840. end;
  1841. end
  1842. else if (p1.resultdef.typ<>pointerdef) then
  1843. begin
  1844. { ^ as binary operator is a problem!!!! (FK) }
  1845. again:=false;
  1846. Message(parser_e_invalid_qualifier);
  1847. recoverconsume_postfixops;
  1848. p1.destroy;
  1849. p1:=cerrornode.create;
  1850. end
  1851. else
  1852. p1:=cderefnode.create(p1);
  1853. end;
  1854. _LECKKLAMMER:
  1855. begin
  1856. if is_class_or_interface_or_object(p1.resultdef) or
  1857. is_dispinterface(p1.resultdef) or
  1858. is_record(p1.resultdef) or
  1859. is_javaclass(p1.resultdef) then
  1860. begin
  1861. { default property }
  1862. protsym:=search_default_property(tabstractrecorddef(p1.resultdef));
  1863. if not(assigned(protsym)) then
  1864. begin
  1865. p1.destroy;
  1866. p1:=cerrornode.create;
  1867. again:=false;
  1868. message(parser_e_no_default_property_available);
  1869. end
  1870. else
  1871. begin
  1872. { The property symbol is referenced indirect }
  1873. protsym.IncRefCount;
  1874. handle_propertysym(protsym,protsym.owner,p1);
  1875. end;
  1876. end
  1877. else
  1878. begin
  1879. consume(_LECKKLAMMER);
  1880. repeat
  1881. { in all of the cases below, p1 is changed }
  1882. case p1.resultdef.typ of
  1883. pointerdef:
  1884. begin
  1885. { support delphi autoderef }
  1886. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1887. (m_autoderef in current_settings.modeswitches) then
  1888. p1:=cderefnode.create(p1);
  1889. p2:=comp_expr(true,false);
  1890. { Support Pbytevar[0..9] which returns array [0..9].}
  1891. if try_to_consume(_POINTPOINT) then
  1892. p2:=crangenode.create(p2,comp_expr(true,false));
  1893. p1:=cvecnode.create(p1,p2);
  1894. end;
  1895. variantdef:
  1896. begin
  1897. handle_variantarray;
  1898. { the RECKKLAMMER is already read }
  1899. goto skipreckklammercheck;
  1900. end;
  1901. stringdef :
  1902. begin
  1903. p2:=comp_expr(true,false);
  1904. { Support string[0..9] which returns array [0..9] of char.}
  1905. if try_to_consume(_POINTPOINT) then
  1906. p2:=crangenode.create(p2,comp_expr(true,false));
  1907. p1:=cvecnode.create(p1,p2);
  1908. end;
  1909. arraydef:
  1910. begin
  1911. p2:=comp_expr(true,false);
  1912. { support SEG:OFS for go32v2/msdos Mem[] }
  1913. if (target_info.system in [system_i386_go32v2,system_i386_watcom,system_i8086_msdos,system_i8086_win16]) and
  1914. (p1.nodetype=loadn) and
  1915. assigned(tloadnode(p1).symtableentry) and
  1916. assigned(tloadnode(p1).symtableentry.owner.name) and
  1917. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1918. ((tloadnode(p1).symtableentry.name='MEM') or
  1919. (tloadnode(p1).symtableentry.name='MEMW') or
  1920. (tloadnode(p1).symtableentry.name='MEML')) then
  1921. begin
  1922. {$if defined(i8086)}
  1923. consume(_COLON);
  1924. inserttypeconv(p2,u16inttype);
  1925. inserttypeconv_internal(p2,u32inttype);
  1926. p3:=cshlshrnode.create(shln,p2,cordconstnode.create($10,s16inttype,false));
  1927. p2:=comp_expr(true,false);
  1928. inserttypeconv(p2,u16inttype);
  1929. inserttypeconv_internal(p2,u32inttype);
  1930. p2:=caddnode.create(addn,p2,p3);
  1931. case tloadnode(p1).symtableentry.name of
  1932. 'MEM': p2:=ctypeconvnode.create_internal(p2,bytefarpointertype);
  1933. 'MEMW': p2:=ctypeconvnode.create_internal(p2,wordfarpointertype);
  1934. 'MEML': p2:=ctypeconvnode.create_internal(p2,longintfarpointertype);
  1935. else
  1936. internalerror(2013053102);
  1937. end;
  1938. p1:=cderefnode.create(p2);
  1939. {$elseif defined(i386)}
  1940. if try_to_consume(_COLON) then
  1941. begin
  1942. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1943. p2:=comp_expr(true,false);
  1944. p2:=caddnode.create(addn,p2,p3);
  1945. if try_to_consume(_POINTPOINT) then
  1946. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1947. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true,false),p3.getcopy));
  1948. p1:=cvecnode.create(p1,p2);
  1949. include(tvecnode(p1).flags,nf_memseg);
  1950. include(tvecnode(p1).flags,nf_memindex);
  1951. end
  1952. else
  1953. begin
  1954. if try_to_consume(_POINTPOINT) then
  1955. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1956. p2:=crangenode.create(p2,comp_expr(true,false));
  1957. p1:=cvecnode.create(p1,p2);
  1958. include(tvecnode(p1).flags,nf_memindex);
  1959. end;
  1960. {$else}
  1961. internalerror(2013053101);
  1962. {$endif}
  1963. end
  1964. else
  1965. begin
  1966. if try_to_consume(_POINTPOINT) then
  1967. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1968. p2:=crangenode.create(p2,comp_expr(true,false));
  1969. p1:=cvecnode.create(p1,p2);
  1970. end;
  1971. end;
  1972. else
  1973. begin
  1974. if p1.resultdef.typ<>undefineddef then
  1975. Message(parser_e_invalid_qualifier);
  1976. p1.destroy;
  1977. p1:=cerrornode.create;
  1978. comp_expr(true,false);
  1979. again:=false;
  1980. end;
  1981. end;
  1982. do_typecheckpass(p1);
  1983. until not try_to_consume(_COMMA);
  1984. consume(_RECKKLAMMER);
  1985. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1986. skipreckklammercheck:
  1987. end;
  1988. end;
  1989. _POINT :
  1990. begin
  1991. consume(_POINT);
  1992. allowspecialize:=not (m_delphi in current_settings.modeswitches) and (block_type in [bt_type,bt_var_type,bt_const_type]);
  1993. if allowspecialize and (token=_ID) and (idtoken=_SPECIALIZE) then
  1994. begin
  1995. //consume(_ID);
  1996. isspecialize:=true;
  1997. end
  1998. else
  1999. isspecialize:=false;
  2000. if (p1.resultdef.typ=pointerdef) and
  2001. (m_autoderef in current_settings.modeswitches) and
  2002. { don't auto-deref objc.id, because then the code
  2003. below for supporting id.anyobjcmethod isn't triggered }
  2004. (p1.resultdef<>objc_idtype) then
  2005. begin
  2006. p1:=cderefnode.create(p1);
  2007. do_typecheckpass(p1);
  2008. end;
  2009. { procvar.<something> can never mean anything so always
  2010. try to call it in case it returns a record/object/... }
  2011. maybe_call_procvar(p1,false);
  2012. if (p1.nodetype=ordconstn) and
  2013. not is_boolean(p1.resultdef) and
  2014. not is_enum(p1.resultdef) then
  2015. begin
  2016. { type helpers are checked first }
  2017. if (token=_ID) and try_type_helper(p1,nil) then
  2018. goto skippointdefcheck;
  2019. { only an "e" or "E" can follow an intconst with a ".", the
  2020. other case (another intconst) is handled by the scanner }
  2021. if (token=_ID) and (pattern[1]='E') then
  2022. begin
  2023. haderror:=false;
  2024. if length(pattern)>1 then
  2025. begin
  2026. expstr:=copy(pattern,2,length(pattern)-1);
  2027. val(expstr,intval,code);
  2028. if code<>0 then
  2029. begin
  2030. haderror:=true;
  2031. intval:=intval; // Hackfix the "var assigned but never used" note.
  2032. end;
  2033. end
  2034. else
  2035. expstr:='';
  2036. consume(token);
  2037. if tordconstnode(p1).value.signed then
  2038. str(tordconstnode(p1).value.svalue,valstr)
  2039. else
  2040. str(tordconstnode(p1).value.uvalue,valstr);
  2041. valstr:=valstr+'.0E';
  2042. if expstr='' then
  2043. case token of
  2044. _MINUS:
  2045. begin
  2046. consume(token);
  2047. if token=_INTCONST then
  2048. begin
  2049. valstr:=valstr+'-'+pattern;
  2050. consume(token);
  2051. end
  2052. else
  2053. haderror:=true;
  2054. end;
  2055. _PLUS:
  2056. begin
  2057. consume(token);
  2058. if token=_INTCONST then
  2059. begin
  2060. valstr:=valstr+pattern;
  2061. consume(token);
  2062. end
  2063. else
  2064. haderror:=true;
  2065. end;
  2066. _INTCONST:
  2067. begin
  2068. valstr:=valstr+pattern;
  2069. consume(_INTCONST);
  2070. end;
  2071. else
  2072. haderror:=true;
  2073. end
  2074. else
  2075. valstr:=valstr+expstr;
  2076. if haderror then
  2077. begin
  2078. Message(parser_e_error_in_real);
  2079. p2:=cerrornode.create;
  2080. end
  2081. else
  2082. p2:=real_const_node_from_pattern(valstr);
  2083. p1.free;
  2084. p1:=p2;
  2085. again:=false;
  2086. goto skippointdefcheck;
  2087. end
  2088. else
  2089. begin
  2090. { just convert the ordconst to a realconst }
  2091. p2:=crealconstnode.create(tordconstnode(p1).value,pbestrealtype^);
  2092. p1.free;
  2093. p1:=p2;
  2094. again:=false;
  2095. goto skippointdefcheck;
  2096. end;
  2097. end;
  2098. if (p1.nodetype=stringconstn) and (token=_ID) then
  2099. begin
  2100. { the def of a string const is an array }
  2101. case tstringconstnode(p1).cst_type of
  2102. cst_conststring:
  2103. if cs_refcountedstrings in current_settings.localswitches then
  2104. if m_default_unicodestring in current_settings.modeswitches then
  2105. strdef:=cunicodestringtype
  2106. else
  2107. strdef:=cansistringtype
  2108. else
  2109. strdef:=cshortstringtype;
  2110. cst_shortstring:
  2111. strdef:=cshortstringtype;
  2112. cst_ansistring:
  2113. { use getansistringdef? }
  2114. strdef:=cansistringtype;
  2115. cst_widestring:
  2116. strdef:=cwidestringtype;
  2117. cst_unicodestring:
  2118. strdef:=cunicodestringtype;
  2119. cst_longstring:
  2120. { let's see when someone stumbles upon this...}
  2121. internalerror(201301111);
  2122. else
  2123. internalerror(2013112903);
  2124. end;
  2125. if try_type_helper(p1,strdef) then
  2126. goto skippointdefcheck;
  2127. end;
  2128. { this is skipped if label skippointdefcheck is used }
  2129. case p1.resultdef.typ of
  2130. recorddef:
  2131. begin
  2132. if isspecialize or (token=_ID) then
  2133. begin
  2134. erroroutp1:=true;
  2135. srsym:=nil;
  2136. structh:=tabstractrecorddef(p1.resultdef);
  2137. if isspecialize then
  2138. begin
  2139. { consume the specialize }
  2140. consume(_ID);
  2141. if token<>_ID then
  2142. consume(_ID)
  2143. else
  2144. begin
  2145. searchsym_in_record(structh,pattern,srsym,srsymtable);
  2146. consume(_ID);
  2147. if handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  2148. erroroutp1:=false;
  2149. end;
  2150. end
  2151. else
  2152. begin
  2153. searchsym_in_record(structh,pattern,srsym,srsymtable);
  2154. if assigned(srsym) then
  2155. begin
  2156. old_current_filepos:=current_filepos;
  2157. consume(_ID);
  2158. if not (sp_generic_dummy in srsym.symoptions) or
  2159. not (token in [_LT,_LSHARPBRACKET]) then
  2160. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg,old_current_filepos)
  2161. else
  2162. p1:=cspecializenode.create(p1,getaddr,srsym);
  2163. erroroutp1:=false;
  2164. end
  2165. else
  2166. begin
  2167. Message1(sym_e_id_no_member,orgpattern);
  2168. { try to clean up }
  2169. consume(_ID);
  2170. end;
  2171. end;
  2172. if erroroutp1 then
  2173. begin
  2174. p1.free;
  2175. p1:=cerrornode.create;
  2176. end
  2177. else
  2178. if p1.nodetype<>specializen then
  2179. do_member_read(structh,getaddr,srsym,p1,again,[],spezcontext);
  2180. end
  2181. else
  2182. consume(_ID);
  2183. end;
  2184. enumdef:
  2185. begin
  2186. if token=_ID then
  2187. begin
  2188. srsym:=tsym(tenumdef(p1.resultdef).symtable.Find(pattern));
  2189. if assigned(srsym) and (srsym.typ=enumsym) and (p1.nodetype=typen) then
  2190. begin
  2191. p1.destroy;
  2192. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2193. p1:=genenumnode(tenumsym(srsym));
  2194. consume(_ID);
  2195. end
  2196. else
  2197. if not try_type_helper(p1,nil) then
  2198. begin
  2199. p1.destroy;
  2200. Message1(sym_e_id_no_member,orgpattern);
  2201. p1:=cerrornode.create;
  2202. consume(_ID);
  2203. end;
  2204. end;
  2205. end;
  2206. arraydef:
  2207. begin
  2208. if is_dynamic_array(p1.resultdef) then
  2209. begin
  2210. if (token=_ID) and not try_type_helper(p1,nil) then
  2211. begin
  2212. if pattern='CREATE' then
  2213. begin
  2214. consume(_ID);
  2215. p2:=parse_array_constructor(tarraydef(p1.resultdef));
  2216. p1.destroy;
  2217. p1:=p2;
  2218. end
  2219. else
  2220. begin
  2221. Message2(scan_f_syn_expected,'CREATE',pattern);
  2222. p1.destroy;
  2223. p1:=cerrornode.create;
  2224. consume(_ID);
  2225. end;
  2226. end;
  2227. end
  2228. else
  2229. begin
  2230. Message(parser_e_invalid_qualifier);
  2231. p1.destroy;
  2232. p1:=cerrornode.create;
  2233. consume(_ID);
  2234. end;
  2235. end;
  2236. variantdef:
  2237. begin
  2238. { dispatch call? }
  2239. { lhs := v.ident[parameters] -> property get
  2240. lhs := v.ident(parameters) -> method call
  2241. v.ident[parameters] := rhs -> property put
  2242. v.ident(parameters) := rhs -> also property put }
  2243. if token=_ID then
  2244. begin
  2245. if not try_type_helper(p1,nil) then
  2246. begin
  2247. dispatchstring:=orgpattern;
  2248. consume(_ID);
  2249. calltype:=dct_method;
  2250. if try_to_consume(_LKLAMMER) then
  2251. begin
  2252. p2:=parse_paras(false,true,_RKLAMMER);
  2253. consume(_RKLAMMER);
  2254. end
  2255. else if try_to_consume(_LECKKLAMMER) then
  2256. begin
  2257. p2:=parse_paras(false,true,_RECKKLAMMER);
  2258. consume(_RECKKLAMMER);
  2259. calltype:=dct_propget;
  2260. end
  2261. else
  2262. p2:=nil;
  2263. { property setter? }
  2264. if (token=_ASSIGNMENT) and not(afterassignment) then
  2265. begin
  2266. consume(_ASSIGNMENT);
  2267. { read the expression }
  2268. p3:=comp_expr(true,false);
  2269. { concat value parameter too }
  2270. p2:=ccallparanode.create(p3,p2);
  2271. p1:=translate_disp_call(p1,p2,dct_propput,dispatchstring,0,voidtype);
  2272. end
  2273. else
  2274. { this is only an approximation
  2275. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  2276. if afterassignment or in_args or (token<>_SEMICOLON) then
  2277. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,cvarianttype)
  2278. else
  2279. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,voidtype);
  2280. end;
  2281. end
  2282. else { Error }
  2283. Consume(_ID);
  2284. end;
  2285. classrefdef:
  2286. begin
  2287. if token=_ID then
  2288. begin
  2289. structh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  2290. if isspecialize then
  2291. begin
  2292. { consume the specialize }
  2293. consume(_ID);
  2294. if token<>_ID then
  2295. consume(_ID)
  2296. else
  2297. begin
  2298. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2299. consume(_ID);
  2300. if handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  2301. erroroutp1:=false;
  2302. end;
  2303. end
  2304. else
  2305. begin
  2306. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2307. if assigned(srsym) then
  2308. begin
  2309. old_current_filepos:=current_filepos;
  2310. consume(_ID);
  2311. if not (sp_generic_dummy in srsym.symoptions) or
  2312. not (token in [_LT,_LSHARPBRACKET]) then
  2313. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg,old_current_filepos)
  2314. else
  2315. p1:=cspecializenode.create(p1,getaddr,srsym);
  2316. erroroutp1:=false;
  2317. end
  2318. else
  2319. begin
  2320. Message1(sym_e_id_no_member,orgpattern);
  2321. { try to clean up }
  2322. consume(_ID);
  2323. end;
  2324. end;
  2325. if erroroutp1 then
  2326. begin
  2327. p1.free;
  2328. p1:=cerrornode.create;
  2329. end
  2330. else
  2331. if p1.nodetype<>specializen then
  2332. do_member_read(structh,getaddr,srsym,p1,again,[],spezcontext);
  2333. end
  2334. else { Error }
  2335. Consume(_ID);
  2336. end;
  2337. objectdef:
  2338. begin
  2339. if isspecialize or (token=_ID) then
  2340. begin
  2341. erroroutp1:=true;
  2342. srsym:=nil;
  2343. structh:=tobjectdef(p1.resultdef);
  2344. if isspecialize then
  2345. begin
  2346. { consume the "specialize" }
  2347. consume(_ID);
  2348. if token<>_ID then
  2349. consume(_ID)
  2350. else
  2351. begin
  2352. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2353. consume(_ID);
  2354. if handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  2355. erroroutp1:=false;
  2356. end;
  2357. end
  2358. else
  2359. begin
  2360. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2361. if assigned(srsym) then
  2362. begin
  2363. old_current_filepos:=current_filepos;
  2364. consume(_ID);
  2365. if not (sp_generic_dummy in srsym.symoptions) or
  2366. not (token in [_LT,_LSHARPBRACKET]) then
  2367. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg,old_current_filepos)
  2368. else
  2369. p1:=cspecializenode.create(p1,getaddr,srsym);
  2370. erroroutp1:=false;
  2371. end
  2372. else
  2373. begin
  2374. Message1(sym_e_id_no_member,orgpattern);
  2375. { try to clean up }
  2376. consume(_ID);
  2377. end;
  2378. end;
  2379. if erroroutp1 then
  2380. begin
  2381. p1.free;
  2382. p1:=cerrornode.create;
  2383. end
  2384. else
  2385. if p1.nodetype<>specializen then
  2386. do_member_read(structh,getaddr,srsym,p1,again,[],spezcontext);
  2387. end
  2388. else { Error }
  2389. Consume(_ID);
  2390. end;
  2391. pointerdef:
  2392. begin
  2393. if (p1.resultdef=objc_idtype) then
  2394. begin
  2395. { objc's id type can be used to call any
  2396. Objective-C method of any Objective-C class
  2397. type that's currently in scope }
  2398. if search_objc_method(pattern,srsym,srsymtable) then
  2399. begin
  2400. consume(_ID);
  2401. do_proc_call(srsym,srsymtable,nil,
  2402. (getaddr and not(token in [_CARET,_POINT])),
  2403. again,p1,[cnf_objc_id_call],nil);
  2404. { we need to know which procedure is called }
  2405. do_typecheckpass(p1);
  2406. end
  2407. else
  2408. begin
  2409. consume(_ID);
  2410. Message(parser_e_methode_id_expected);
  2411. end;
  2412. end
  2413. else
  2414. begin
  2415. if not try_type_helper(p1,nil) then
  2416. begin
  2417. Message(parser_e_invalid_qualifier);
  2418. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  2419. Message(parser_h_maybe_deref_caret_missing);
  2420. end;
  2421. end
  2422. end;
  2423. else
  2424. begin
  2425. found:=try_type_helper(p1,nil);
  2426. if not found then
  2427. begin
  2428. if p1.resultdef.typ<>undefineddef then
  2429. Message(parser_e_invalid_qualifier);
  2430. p1.destroy;
  2431. p1:=cerrornode.create;
  2432. { Error }
  2433. consume(_ID);
  2434. end;
  2435. end;
  2436. end;
  2437. { processing an ordconstnode avoids the resultdef check }
  2438. skippointdefcheck:
  2439. end;
  2440. else
  2441. begin
  2442. { is this a procedure variable ? }
  2443. if assigned(p1.resultdef) and
  2444. (p1.resultdef.typ=procvardef) then
  2445. begin
  2446. { Typenode for typecasting or expecting a procvar }
  2447. if (p1.nodetype=typen) or
  2448. (
  2449. assigned(getprocvardef) and
  2450. equal_defs(p1.resultdef,getprocvardef)
  2451. ) then
  2452. begin
  2453. if try_to_consume(_LKLAMMER) then
  2454. begin
  2455. p1:=comp_expr(true,false);
  2456. consume(_RKLAMMER);
  2457. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2458. end
  2459. else
  2460. again:=false
  2461. end
  2462. else
  2463. begin
  2464. if try_to_consume(_LKLAMMER) then
  2465. begin
  2466. p2:=parse_paras(false,false,_RKLAMMER);
  2467. consume(_RKLAMMER);
  2468. p1:=ccallnode.create_procvar(p2,p1);
  2469. { proc():= is never possible }
  2470. if token=_ASSIGNMENT then
  2471. begin
  2472. Message(parser_e_illegal_expression);
  2473. p1.free;
  2474. p1:=cerrornode.create;
  2475. again:=false;
  2476. end;
  2477. end
  2478. else
  2479. again:=false;
  2480. end;
  2481. end
  2482. else
  2483. again:=false;
  2484. end;
  2485. end;
  2486. { we only try again if p1 was changed }
  2487. if again or
  2488. (p1.nodetype=errorn) then
  2489. result:=true;
  2490. end; { while again }
  2491. end;
  2492. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  2493. out memberparentdef: tdef): boolean;
  2494. var
  2495. hdef : tdef;
  2496. begin
  2497. result:=true;
  2498. memberparentdef:=nil;
  2499. case st.symtabletype of
  2500. ObjectSymtable,
  2501. recordsymtable:
  2502. begin
  2503. memberparentdef:=tdef(st.defowner);
  2504. exit;
  2505. end;
  2506. WithSymtable:
  2507. begin
  2508. if assigned(p1) then
  2509. internalerror(2007012002);
  2510. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  2511. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  2512. if not(hdef.typ in [objectdef,classrefdef]) then
  2513. exit;
  2514. if (hdef.typ=classrefdef) then
  2515. hdef:=tclassrefdef(hdef).pointeddef;
  2516. memberparentdef:=hdef;
  2517. end;
  2518. else
  2519. result:=false;
  2520. end;
  2521. end;
  2522. {$maxfpuregisters 0}
  2523. function factor(getaddr,typeonly,hadspecialize:boolean) : tnode;
  2524. {---------------------------------------------
  2525. Factor_read_id
  2526. ---------------------------------------------}
  2527. procedure factor_read_id(out p1:tnode;var again:boolean);
  2528. function findwithsymtable : boolean;
  2529. var
  2530. hp : psymtablestackitem;
  2531. begin
  2532. result:=true;
  2533. hp:=symtablestack.stack;
  2534. while assigned(hp) do
  2535. begin
  2536. if hp^.symtable.symtabletype=withsymtable then
  2537. exit;
  2538. hp:=hp^.next;
  2539. end;
  2540. result:=false;
  2541. end;
  2542. var
  2543. srsym: tsym;
  2544. srsymtable: TSymtable;
  2545. hdef: tdef;
  2546. pd: tprocdef;
  2547. orgstoredpattern,
  2548. storedpattern: string;
  2549. callflags: tcallnodeflags;
  2550. t : ttoken;
  2551. allowspecialize,
  2552. isspecialize,
  2553. unit_found : boolean;
  2554. tokenpos: tfileposinfo;
  2555. begin
  2556. { allow post fix operators }
  2557. again:=true;
  2558. { preinitalize tokenpos }
  2559. tokenpos:=current_filepos;
  2560. p1:=nil;
  2561. allowspecialize:=not (m_delphi in current_settings.modeswitches) and
  2562. not hadspecialize and
  2563. (block_type in [bt_type,bt_var_type,bt_const_type]);
  2564. if allowspecialize and (token=_ID) and (idtoken=_SPECIALIZE) then
  2565. begin
  2566. consume(_ID);
  2567. isspecialize:=true;
  2568. end
  2569. else
  2570. isspecialize:=hadspecialize;
  2571. { first check for identifier }
  2572. if token<>_ID then
  2573. begin
  2574. srsym:=generrorsym;
  2575. srsymtable:=nil;
  2576. consume(_ID);
  2577. unit_found:=false;
  2578. end
  2579. else
  2580. begin
  2581. if typeonly then
  2582. searchsym_type(pattern,srsym,srsymtable)
  2583. else
  2584. searchsym(pattern,srsym,srsymtable);
  2585. { handle unit specification like System.Writeln }
  2586. if not isspecialize then
  2587. unit_found:=try_consume_unitsym(srsym,srsymtable,t,true,allowspecialize,isspecialize)
  2588. else
  2589. begin
  2590. unit_found:=false;
  2591. t:=_ID;
  2592. end;
  2593. storedpattern:=pattern;
  2594. orgstoredpattern:=orgpattern;
  2595. { store the position of the token before consuming it }
  2596. tokenpos:=current_filepos;
  2597. consume(t);
  2598. { named parameter support }
  2599. found_arg_name:=false;
  2600. if not(unit_found) and
  2601. not isspecialize and
  2602. named_args_allowed and
  2603. (token=_ASSIGNMENT) then
  2604. begin
  2605. found_arg_name:=true;
  2606. p1:=cstringconstnode.createstr(storedpattern);
  2607. consume(_ASSIGNMENT);
  2608. exit;
  2609. end;
  2610. if isspecialize then
  2611. begin
  2612. if not assigned(srsym) or
  2613. (srsym.typ<>typesym) then
  2614. begin
  2615. identifier_not_found(orgstoredpattern,tokenpos);
  2616. srsym:=generrorsym;
  2617. srsymtable:=nil;
  2618. end
  2619. else
  2620. begin
  2621. hdef:=ttypesym(srsym).typedef;
  2622. generate_specialization(hdef,false,'');
  2623. if hdef=generrordef then
  2624. begin
  2625. srsym:=generrorsym;
  2626. srsymtable:=nil;
  2627. end
  2628. else
  2629. begin
  2630. srsym:=hdef.typesym;
  2631. srsymtable:=srsym.owner;
  2632. end;
  2633. end;
  2634. end;
  2635. { check hints, but only if it isn't a potential generic symbol;
  2636. that is checked in sub_expr if it isn't a generic }
  2637. if assigned(srsym) and
  2638. not (
  2639. (srsym.typ=typesym) and
  2640. (ttypesym(srsym).typedef.typ in [recorddef,objectdef,arraydef,procvardef,undefineddef]) and
  2641. not (sp_generic_para in srsym.symoptions) and
  2642. (token in [_LT, _LSHARPBRACKET])
  2643. ) then
  2644. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2645. { if nothing found give error and return errorsym }
  2646. if not assigned(srsym) or
  2647. { is this a generic dummy symbol? }
  2648. ((srsym.typ=typesym) and
  2649. assigned(ttypesym(srsym).typedef) and
  2650. (ttypesym(srsym).typedef.typ=undefineddef) and
  2651. not (sp_generic_para in srsym.symoptions) and
  2652. not (token in [_LT, _LSHARPBRACKET]) and
  2653. not (
  2654. { in non-Delphi modes the generic class' name without a
  2655. "specialization" or "<T>" may be used to identify the
  2656. current class }
  2657. (sp_generic_dummy in srsym.symoptions) and
  2658. assigned(current_structdef) and
  2659. (df_generic in current_structdef.defoptions) and
  2660. not (m_delphi in current_settings.modeswitches) and
  2661. assigned(get_generic_in_hierarchy_by_name(srsym,current_structdef))
  2662. )) and
  2663. { it could be a rename of a generic para }
  2664. { Note: if this generates false positives we'll need to
  2665. include a "basesym" to tsym to track the original
  2666. symbol }
  2667. not (sp_explicitrename in srsym.symoptions) then
  2668. begin
  2669. { if a generic is parsed and when we are inside an with block,
  2670. a symbol might not be defined }
  2671. if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) and
  2672. findwithsymtable then
  2673. begin
  2674. { create dummy symbol, it will be freed later on }
  2675. srsym:=tsym.create(undefinedsym,'$undefinedsym');
  2676. srsymtable:=nil;
  2677. end
  2678. else
  2679. begin
  2680. identifier_not_found(orgstoredpattern,tokenpos);
  2681. srsym:=generrorsym;
  2682. srsymtable:=nil;
  2683. end;
  2684. end;
  2685. end;
  2686. { Access to funcret or need to call the function? }
  2687. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  2688. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  2689. { result(x) is not allowed }
  2690. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  2691. (
  2692. (token=_LKLAMMER) or
  2693. (
  2694. (([m_tp7,m_delphi,m_mac,m_iso] * current_settings.modeswitches) <> []) and
  2695. (afterassignment or in_args)
  2696. )
  2697. ) then
  2698. begin
  2699. hdef:=tdef(srsym.owner.defowner);
  2700. if assigned(hdef) and
  2701. (hdef.typ=procdef) then
  2702. srsym:=tprocdef(hdef).procsym
  2703. else
  2704. begin
  2705. Message(parser_e_illegal_expression);
  2706. srsym:=generrorsym;
  2707. end;
  2708. srsymtable:=srsym.owner;
  2709. end;
  2710. begin
  2711. case srsym.typ of
  2712. absolutevarsym :
  2713. begin
  2714. if (tabsolutevarsym(srsym).abstyp=tovar) then
  2715. begin
  2716. p1:=nil;
  2717. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  2718. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  2719. include(p1.flags,nf_absolute);
  2720. end
  2721. else
  2722. p1:=cloadnode.create(srsym,srsymtable);
  2723. end;
  2724. staticvarsym,
  2725. localvarsym,
  2726. paravarsym,
  2727. fieldvarsym :
  2728. begin
  2729. { check if we are reading a field of an object/class/ }
  2730. { record. is_member_read() will deal with withsymtables }
  2731. { if needed. }
  2732. p1:=nil;
  2733. if is_member_read(srsym,srsymtable,p1,hdef) then
  2734. begin
  2735. { if the field was originally found in an }
  2736. { objectsymtable, it means it's part of self }
  2737. { if only method from which it was called is }
  2738. { not class static }
  2739. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  2740. { if we are accessing a owner procsym from the nested }
  2741. { class we need to call it as a class member }
  2742. if assigned(current_structdef) and
  2743. (((current_structdef<>hdef) and is_owned_by(current_structdef,hdef)) or
  2744. (sp_static in srsym.symoptions)) then
  2745. if srsymtable.symtabletype=recordsymtable then
  2746. p1:=ctypenode.create(hdef)
  2747. else
  2748. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2749. else
  2750. begin
  2751. if assigned(current_procinfo) then
  2752. begin
  2753. pd:=current_procinfo.get_normal_proc.procdef;
  2754. if assigned(pd) and pd.no_self_node then
  2755. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  2756. else
  2757. p1:=load_self_node;
  2758. end
  2759. else
  2760. p1:=load_self_node;
  2761. end;
  2762. { now, if the field itself is part of an objectsymtab }
  2763. { (it can be even if it was found in a withsymtable, }
  2764. { e.g., "with classinstance do field := 5"), then }
  2765. { let do_member_read handle it }
  2766. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2767. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[],nil)
  2768. else
  2769. { otherwise it's a regular record subscript }
  2770. p1:=csubscriptnode.create(srsym,p1);
  2771. end
  2772. else
  2773. { regular non-field load }
  2774. p1:=cloadnode.create(srsym,srsymtable);
  2775. end;
  2776. syssym :
  2777. begin
  2778. p1:=statement_syssym(tsyssym(srsym).number);
  2779. end;
  2780. typesym :
  2781. begin
  2782. hdef:=ttypesym(srsym).typedef;
  2783. if not assigned(hdef) then
  2784. begin
  2785. again:=false;
  2786. end
  2787. else
  2788. begin
  2789. { We need to know if this unit uses Variants }
  2790. if ((hdef=cvarianttype) or (hdef=colevarianttype)) and
  2791. not(cs_compilesystem in current_settings.moduleswitches) then
  2792. current_module.flags:=current_module.flags or uf_uses_variants;
  2793. p1:=handle_factor_typenode(hdef,getaddr,again,srsym,typeonly);
  2794. end;
  2795. end;
  2796. enumsym :
  2797. begin
  2798. p1:=genenumnode(tenumsym(srsym));
  2799. end;
  2800. constsym :
  2801. begin
  2802. if tconstsym(srsym).consttyp=constresourcestring then
  2803. begin
  2804. p1:=cloadnode.create(srsym,srsymtable);
  2805. do_typecheckpass(p1);
  2806. p1.resultdef:=getansistringdef;
  2807. end
  2808. else
  2809. p1:=genconstsymtree(tconstsym(srsym));
  2810. end;
  2811. procsym :
  2812. begin
  2813. p1:=nil;
  2814. { check if it's a method/class method }
  2815. if is_member_read(srsym,srsymtable,p1,hdef) then
  2816. begin
  2817. { if we are accessing a owner procsym from the nested }
  2818. { class we need to call it as a class member }
  2819. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) and
  2820. assigned(current_structdef) and (current_structdef<>hdef) and is_owned_by(current_structdef,hdef) then
  2821. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef));
  2822. { not srsymtable.symtabletype since that can be }
  2823. { withsymtable as well }
  2824. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2825. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[],nil)
  2826. else
  2827. { no procsyms in records (yet) }
  2828. internalerror(2007012006);
  2829. end
  2830. else
  2831. begin
  2832. { regular procedure/function call }
  2833. if not unit_found then
  2834. callflags:=[]
  2835. else
  2836. callflags:=[cnf_unit_specified];
  2837. do_proc_call(srsym,srsymtable,nil,
  2838. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  2839. again,p1,callflags,nil);
  2840. end;
  2841. end;
  2842. propertysym :
  2843. begin
  2844. p1:=nil;
  2845. { property of a class/object? }
  2846. if is_member_read(srsym,srsymtable,p1,hdef) then
  2847. begin
  2848. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  2849. { if we are accessing a owner procsym from the nested }
  2850. { class or from a static class method we need to call }
  2851. { it as a class member }
  2852. if (assigned(current_structdef) and (current_structdef<>hdef) and is_owned_by(current_structdef,hdef)) or
  2853. (assigned(current_procinfo) and current_procinfo.get_normal_proc.procdef.no_self_node) then
  2854. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2855. else
  2856. p1:=load_self_node;
  2857. { not srsymtable.symtabletype since that can be }
  2858. { withsymtable as well }
  2859. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2860. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[],nil)
  2861. else
  2862. { no propertysyms in records (yet) }
  2863. internalerror(2009111510);
  2864. end
  2865. else
  2866. { no method pointer }
  2867. begin
  2868. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  2869. end;
  2870. end;
  2871. labelsym :
  2872. begin
  2873. { Support @label }
  2874. if getaddr then
  2875. begin
  2876. if srsym.owner<>current_procinfo.procdef.localst then
  2877. CGMessage(parser_e_label_outside_proc);
  2878. p1:=cloadnode.create(srsym,srsym.owner)
  2879. end
  2880. else
  2881. begin
  2882. consume(_COLON);
  2883. if tlabelsym(srsym).defined then
  2884. Message(sym_e_label_already_defined);
  2885. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  2886. begin
  2887. tlabelsym(srsym).nonlocal:=true;
  2888. exclude(current_procinfo.procdef.procoptions,po_inline);
  2889. end;
  2890. if tlabelsym(srsym).nonlocal and
  2891. (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  2892. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  2893. tlabelsym(srsym).defined:=true;
  2894. p1:=clabelnode.create(nil,tlabelsym(srsym));
  2895. tlabelsym(srsym).code:=p1;
  2896. end;
  2897. end;
  2898. undefinedsym :
  2899. begin
  2900. p1:=cnothingnode.Create;
  2901. p1.resultdef:=cundefineddef.create(true);
  2902. { clean up previously created dummy symbol }
  2903. srsym.free;
  2904. end;
  2905. errorsym :
  2906. begin
  2907. p1:=cerrornode.create;
  2908. if try_to_consume(_LKLAMMER) then
  2909. begin
  2910. parse_paras(false,false,_RKLAMMER);
  2911. consume(_RKLAMMER);
  2912. end;
  2913. end;
  2914. else
  2915. begin
  2916. p1:=cerrornode.create;
  2917. Message(parser_e_illegal_expression);
  2918. end;
  2919. end; { end case }
  2920. if assigned(p1) and (p1.nodetype<>errorn) then
  2921. p1.fileinfo:=tokenpos;
  2922. end;
  2923. end;
  2924. {---------------------------------------------
  2925. Factor_Read_Set
  2926. ---------------------------------------------}
  2927. { Read a set between [] }
  2928. function factor_read_set:tnode;
  2929. var
  2930. p1,p2 : tnode;
  2931. lastp,
  2932. buildp : tarrayconstructornode;
  2933. old_allow_array_constructor : boolean;
  2934. begin
  2935. buildp:=nil;
  2936. lastp:=nil;
  2937. { be sure that a least one arrayconstructn is used, also for an
  2938. empty [] }
  2939. if token=_RECKKLAMMER then
  2940. buildp:=carrayconstructornode.create(nil,buildp)
  2941. else
  2942. repeat
  2943. { nested array constructors are not allowed, see also tests/webtbs/tw17213.pp }
  2944. old_allow_array_constructor:=allow_array_constructor;
  2945. allow_array_constructor:=false;
  2946. p1:=comp_expr(true,false);
  2947. if try_to_consume(_POINTPOINT) then
  2948. begin
  2949. p2:=comp_expr(true,false);
  2950. p1:=carrayconstructorrangenode.create(p1,p2);
  2951. end;
  2952. { insert at the end of the tree, to get the correct order }
  2953. if not assigned(buildp) then
  2954. begin
  2955. buildp:=carrayconstructornode.create(p1,nil);
  2956. lastp:=buildp;
  2957. end
  2958. else
  2959. begin
  2960. lastp.right:=carrayconstructornode.create(p1,nil);
  2961. lastp:=tarrayconstructornode(lastp.right);
  2962. end;
  2963. allow_array_constructor:=old_allow_array_constructor;
  2964. { there could be more elements }
  2965. until not try_to_consume(_COMMA);
  2966. factor_read_set:=buildp;
  2967. end;
  2968. function can_load_self_node: boolean;
  2969. begin
  2970. result:=false;
  2971. if (block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) or
  2972. not assigned(current_structdef) or
  2973. not assigned(current_procinfo) then
  2974. exit;
  2975. result:=not current_procinfo.get_normal_proc.procdef.no_self_node;
  2976. end;
  2977. {---------------------------------------------
  2978. Factor (Main)
  2979. ---------------------------------------------}
  2980. var
  2981. l : longint;
  2982. ic : int64;
  2983. qc : qword;
  2984. p1 : tnode;
  2985. code : integer;
  2986. srsym : tsym;
  2987. srsymtable : TSymtable;
  2988. pd : tprocdef;
  2989. hclassdef : tobjectdef;
  2990. d : bestreal;
  2991. hs,hsorg : string;
  2992. hdef : tdef;
  2993. filepos : tfileposinfo;
  2994. callflags : tcallnodeflags;
  2995. idstr : tidstring;
  2996. dopostfix,
  2997. again,
  2998. updatefpos,
  2999. nodechanged : boolean;
  3000. begin
  3001. { can't keep a copy of p1 and compare pointers afterwards, because
  3002. p1 may be freed and reallocated in the same place! }
  3003. dopostfix:=true;
  3004. updatefpos:=false;
  3005. p1:=nil;
  3006. filepos:=current_tokenpos;
  3007. again:=false;
  3008. pd:=nil;
  3009. if token=_ID then
  3010. begin
  3011. again:=true;
  3012. { Handle references to self }
  3013. if (idtoken=_SELF) and can_load_self_node then
  3014. begin
  3015. p1:=load_self_node;
  3016. consume(_ID);
  3017. again:=true;
  3018. end
  3019. else
  3020. factor_read_id(p1,again);
  3021. if assigned(p1) then
  3022. begin
  3023. { factor_read_id will set the filepos to after the id,
  3024. and in case of _SELF the filepos will already be the
  3025. same as filepos (so setting it again doesn't hurt). }
  3026. p1.fileinfo:=filepos;
  3027. filepos:=current_tokenpos;
  3028. end;
  3029. { handle post fix operators }
  3030. if (p1.nodetype=specializen) then
  3031. { post fix operators are handled after specialization }
  3032. dopostfix:=false
  3033. else
  3034. if (m_delphi in current_settings.modeswitches) and
  3035. (block_type=bt_body) and
  3036. (token in [_LT,_LSHARPBRACKET]) then
  3037. begin
  3038. if p1.nodetype=typen then
  3039. idstr:=ttypenode(p1).typesym.name
  3040. else
  3041. if (p1.nodetype=loadvmtaddrn) and
  3042. (tloadvmtaddrnode(p1).left.nodetype=typen) then
  3043. idstr:=ttypenode(tloadvmtaddrnode(p1).left).typesym.name
  3044. else
  3045. if (p1.nodetype=loadn) then
  3046. idstr:=tloadnode(p1).symtableentry.name
  3047. else
  3048. idstr:='';
  3049. { if this is the case then the postfix handling is done in
  3050. sub_expr if necessary }
  3051. dopostfix:=not could_be_generic(idstr);
  3052. end;
  3053. { maybe an additional parameter instead of misusing hadspezialize? }
  3054. if dopostfix and not hadspecialize then
  3055. updatefpos:=postfixoperators(p1,again,getaddr);
  3056. end
  3057. else
  3058. begin
  3059. updatefpos:=true;
  3060. case token of
  3061. _RETURN :
  3062. begin
  3063. consume(_RETURN);
  3064. p1:=nil;
  3065. if not(token in [_SEMICOLON,_ELSE,_END]) then
  3066. begin
  3067. p1:=comp_expr(true,false);
  3068. if not assigned(current_procinfo) or
  3069. (current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) or
  3070. is_void(current_procinfo.procdef.returndef) then
  3071. begin
  3072. Message(parser_e_void_function);
  3073. { recovery }
  3074. p1.free;
  3075. p1:=nil;
  3076. end;
  3077. end;
  3078. p1 := cexitnode.create(p1);
  3079. end;
  3080. _INHERITED :
  3081. begin
  3082. again:=true;
  3083. consume(_INHERITED);
  3084. if assigned(current_procinfo) and
  3085. assigned(current_structdef) and
  3086. ((current_structdef.typ=objectdef) or
  3087. ((target_info.system in systems_jvm) and
  3088. (current_structdef.typ=recorddef)))then
  3089. begin
  3090. { for record helpers in mode Delphi "inherited" is not
  3091. allowed }
  3092. if is_objectpascal_helper(current_structdef) and
  3093. (m_delphi in current_settings.modeswitches) and
  3094. is_record(tobjectdef(current_structdef).extendeddef) then
  3095. Message(parser_e_inherited_not_in_record);
  3096. if (current_structdef.typ=objectdef) then
  3097. begin
  3098. hclassdef:=tobjectdef(current_structdef).childof;
  3099. { Objective-C categories *replace* methods in the class
  3100. they extend, or add methods to it. So calling an
  3101. inherited method always calls the method inherited from
  3102. the parent of the extended class }
  3103. if is_objccategory(current_structdef) then
  3104. hclassdef:=hclassdef.childof;
  3105. end
  3106. else if target_info.system in systems_jvm then
  3107. hclassdef:=java_fpcbaserecordtype
  3108. else
  3109. internalerror(2012012401);
  3110. { if inherited; only then we need the method with
  3111. the same name }
  3112. if token <> _ID then
  3113. begin
  3114. hs:=current_procinfo.procdef.procsym.name;
  3115. hsorg:=current_procinfo.procdef.procsym.realname;
  3116. anon_inherited:=true;
  3117. { For message methods we need to search using the message
  3118. number or string }
  3119. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  3120. srdef:=nil;
  3121. if (po_msgint in pd.procoptions) then
  3122. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  3123. else
  3124. if (po_msgstr in pd.procoptions) then
  3125. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  3126. else
  3127. { helpers have their own ways of dealing with inherited }
  3128. if is_objectpascal_helper(current_structdef) then
  3129. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,[ssf_has_inherited])
  3130. else
  3131. searchsym_in_class(hclassdef,current_structdef,hs,srsym,srsymtable,[ssf_search_helper]);
  3132. end
  3133. else
  3134. begin
  3135. hs:=pattern;
  3136. hsorg:=orgpattern;
  3137. consume(_ID);
  3138. anon_inherited:=false;
  3139. { helpers have their own ways of dealing with inherited }
  3140. if is_objectpascal_helper(current_structdef) then
  3141. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,[ssf_has_inherited])
  3142. else
  3143. searchsym_in_class(hclassdef,current_structdef,hs,srsym,srsymtable,[ssf_search_helper]);
  3144. end;
  3145. if assigned(srsym) then
  3146. begin
  3147. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  3148. { load the procdef from the inherited class and
  3149. not from self }
  3150. case srsym.typ of
  3151. procsym:
  3152. begin
  3153. if is_objectpascal_helper(current_structdef) then
  3154. begin
  3155. { for a helper load the procdef either from the
  3156. extended type, from the parent helper or from
  3157. the extended type of the parent helper
  3158. depending on the def the found symbol belongs
  3159. to }
  3160. if (srsym.Owner.defowner.typ=objectdef) and
  3161. is_objectpascal_helper(tobjectdef(srsym.Owner.defowner)) then
  3162. if def_is_related(current_structdef,tdef(srsym.Owner.defowner)) and
  3163. assigned(tobjectdef(current_structdef).childof) then
  3164. hdef:=tobjectdef(current_structdef).childof
  3165. else
  3166. hdef:=tobjectdef(srsym.Owner.defowner).extendeddef
  3167. else
  3168. hdef:=tdef(srsym.Owner.defowner);
  3169. end
  3170. else
  3171. hdef:=hclassdef;
  3172. if (po_classmethod in current_procinfo.procdef.procoptions) or
  3173. (po_staticmethod in current_procinfo.procdef.procoptions) then
  3174. hdef:=cclassrefdef.create(hdef);
  3175. p1:=ctypenode.create(hdef);
  3176. { we need to allow helpers here }
  3177. ttypenode(p1).helperallowed:=true;
  3178. end;
  3179. propertysym:
  3180. ;
  3181. else
  3182. begin
  3183. Message(parser_e_methode_id_expected);
  3184. p1:=cerrornode.create;
  3185. end;
  3186. end;
  3187. callflags:=[cnf_inherited];
  3188. include(current_procinfo.flags,pi_has_inherited);
  3189. if anon_inherited then
  3190. include(callflags,cnf_anon_inherited);
  3191. do_member_read(hclassdef,getaddr,srsym,p1,again,callflags,nil);
  3192. end
  3193. else
  3194. begin
  3195. if anon_inherited then
  3196. begin
  3197. { For message methods we need to call DefaultHandler }
  3198. if (po_msgint in pd.procoptions) or
  3199. (po_msgstr in pd.procoptions) then
  3200. begin
  3201. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable,[ssf_search_helper]);
  3202. if not assigned(srsym) or
  3203. (srsym.typ<>procsym) then
  3204. internalerror(200303171);
  3205. p1:=nil;
  3206. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[],nil);
  3207. end
  3208. else
  3209. begin
  3210. { we need to ignore the inherited; }
  3211. p1:=cnothingnode.create;
  3212. end;
  3213. end
  3214. else
  3215. begin
  3216. Message1(sym_e_id_no_member,hsorg);
  3217. p1:=cerrornode.create;
  3218. end;
  3219. again:=false;
  3220. end;
  3221. { turn auto inheriting off }
  3222. anon_inherited:=false;
  3223. end
  3224. else
  3225. begin
  3226. { in case of records we use a more clear error message }
  3227. if assigned(current_structdef) and
  3228. (current_structdef.typ=recorddef) then
  3229. Message(parser_e_inherited_not_in_record)
  3230. else
  3231. Message(parser_e_generic_methods_only_in_methods);
  3232. again:=false;
  3233. p1:=cerrornode.create;
  3234. end;
  3235. postfixoperators(p1,again,getaddr);
  3236. end;
  3237. _INTCONST :
  3238. begin
  3239. {Try first wether the value fits in an int64.}
  3240. val(pattern,ic,code);
  3241. if code=0 then
  3242. begin
  3243. consume(_INTCONST);
  3244. int_to_type(ic,hdef);
  3245. p1:=cordconstnode.create(ic,hdef,true);
  3246. end
  3247. else
  3248. begin
  3249. { try qword next }
  3250. val(pattern,qc,code);
  3251. if code=0 then
  3252. begin
  3253. consume(_INTCONST);
  3254. int_to_type(qc,hdef);
  3255. p1:=cordconstnode.create(qc,hdef,true);
  3256. end;
  3257. end;
  3258. if code<>0 then
  3259. begin
  3260. { finally float }
  3261. val(pattern,d,code);
  3262. if code<>0 then
  3263. begin
  3264. Message(parser_e_invalid_integer);
  3265. consume(_INTCONST);
  3266. l:=1;
  3267. p1:=cordconstnode.create(l,sinttype,true);
  3268. end
  3269. else
  3270. begin
  3271. consume(_INTCONST);
  3272. p1:=crealconstnode.create(d,pbestrealtype^);
  3273. end;
  3274. end
  3275. else
  3276. { the necessary range checking has already been done by val }
  3277. tordconstnode(p1).rangecheck:=false;
  3278. if token=_POINT then
  3279. begin
  3280. again:=true;
  3281. postfixoperators(p1,again,getaddr);
  3282. end;
  3283. end;
  3284. _REALNUMBER :
  3285. begin
  3286. p1:=real_const_node_from_pattern(pattern);
  3287. consume(_REALNUMBER);
  3288. if token=_POINT then
  3289. begin
  3290. again:=true;
  3291. postfixoperators(p1,again,getaddr);
  3292. end;
  3293. end;
  3294. _STRING :
  3295. begin
  3296. string_dec(hdef,true);
  3297. { STRING can be also a type cast }
  3298. if try_to_consume(_LKLAMMER) then
  3299. begin
  3300. p1:=comp_expr(true,false);
  3301. consume(_RKLAMMER);
  3302. p1:=ctypeconvnode.create_explicit(p1,hdef);
  3303. { handle postfix operators here e.g. string(a)[10] }
  3304. again:=true;
  3305. postfixoperators(p1,again,getaddr);
  3306. end
  3307. else
  3308. p1:=ctypenode.create(hdef);
  3309. end;
  3310. _FILE :
  3311. begin
  3312. hdef:=cfiletype;
  3313. consume(_FILE);
  3314. { FILE can be also a type cast }
  3315. if try_to_consume(_LKLAMMER) then
  3316. begin
  3317. p1:=comp_expr(true,false);
  3318. consume(_RKLAMMER);
  3319. p1:=ctypeconvnode.create_explicit(p1,hdef);
  3320. { handle postfix operators here e.g. string(a)[10] }
  3321. again:=true;
  3322. postfixoperators(p1,again,getaddr);
  3323. end
  3324. else
  3325. begin
  3326. p1:=ctypenode.create(hdef);
  3327. end;
  3328. end;
  3329. _CSTRING :
  3330. begin
  3331. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern),nil);
  3332. consume(_CSTRING);
  3333. if token in postfixoperator_tokens then
  3334. begin
  3335. again:=true;
  3336. postfixoperators(p1,again,getaddr);
  3337. end;
  3338. end;
  3339. _CCHAR :
  3340. begin
  3341. p1:=cordconstnode.create(ord(pattern[1]),cansichartype,true);
  3342. consume(_CCHAR);
  3343. if token=_POINT then
  3344. begin
  3345. again:=true;
  3346. postfixoperators(p1,again,getaddr);
  3347. end;
  3348. end;
  3349. _CWSTRING:
  3350. begin
  3351. p1:=cstringconstnode.createunistr(patternw);
  3352. consume(_CWSTRING);
  3353. if token in postfixoperator_tokens then
  3354. begin
  3355. again:=true;
  3356. postfixoperators(p1,again,getaddr);
  3357. end;
  3358. end;
  3359. _CWCHAR:
  3360. begin
  3361. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  3362. consume(_CWCHAR);
  3363. if token=_POINT then
  3364. begin
  3365. again:=true;
  3366. postfixoperators(p1,again,getaddr);
  3367. end;
  3368. end;
  3369. _KLAMMERAFFE :
  3370. begin
  3371. consume(_KLAMMERAFFE);
  3372. got_addrn:=true;
  3373. { support both @<x> and @(<x>) }
  3374. if try_to_consume(_LKLAMMER) then
  3375. begin
  3376. p1:=factor(true,false,false);
  3377. { inside parentheses a full expression is allowed, see also tests\webtbs\tb27517.pp }
  3378. if token<>_RKLAMMER then
  3379. p1:=sub_expr(opcompare,true,false,p1);
  3380. consume(_RKLAMMER);
  3381. end
  3382. else
  3383. p1:=factor(true,false,false);
  3384. if token in postfixoperator_tokens then
  3385. begin
  3386. again:=true;
  3387. postfixoperators(p1,again,getaddr);
  3388. end;
  3389. got_addrn:=false;
  3390. p1:=caddrnode.create(p1);
  3391. p1.fileinfo:=filepos;
  3392. if cs_typed_addresses in current_settings.localswitches then
  3393. include(p1.flags,nf_typedaddr);
  3394. { Store the procvar that we are expecting, the
  3395. addrn will use the information to find the correct
  3396. procdef or it will return an error }
  3397. if assigned(getprocvardef) and
  3398. (taddrnode(p1).left.nodetype = loadn) then
  3399. taddrnode(p1).getprocvardef:=getprocvardef;
  3400. end;
  3401. _LKLAMMER :
  3402. begin
  3403. consume(_LKLAMMER);
  3404. p1:=comp_expr(true,false);
  3405. consume(_RKLAMMER);
  3406. { it's not a good solution
  3407. but (a+b)^ makes some problems }
  3408. if token in postfixoperator_tokens then
  3409. begin
  3410. again:=true;
  3411. postfixoperators(p1,again,getaddr);
  3412. end;
  3413. end;
  3414. _LECKKLAMMER :
  3415. begin
  3416. consume(_LECKKLAMMER);
  3417. p1:=factor_read_set;
  3418. consume(_RECKKLAMMER);
  3419. end;
  3420. _PLUS :
  3421. begin
  3422. consume(_PLUS);
  3423. p1:=factor(false,false,false);
  3424. p1:=cunaryplusnode.create(p1);
  3425. end;
  3426. _MINUS :
  3427. begin
  3428. consume(_MINUS);
  3429. if (token = _INTCONST) and not(m_isolike_unary_minus in current_settings.modeswitches) then
  3430. begin
  3431. { ugly hack, but necessary to be able to parse }
  3432. { -9223372036854775808 as int64 (JM) }
  3433. pattern := '-'+pattern;
  3434. p1:=sub_expr(oppower,false,false,nil);
  3435. { -1 ** 4 should be - (1 ** 4) and not
  3436. (-1) ** 4
  3437. This was the reason of tw0869.pp test failure PM }
  3438. if p1.nodetype=starstarn then
  3439. begin
  3440. if tbinarynode(p1).left.nodetype=ordconstn then
  3441. begin
  3442. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  3443. p1:=cunaryminusnode.create(p1);
  3444. end
  3445. else if tbinarynode(p1).left.nodetype=realconstn then
  3446. begin
  3447. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  3448. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  3449. p1:=cunaryminusnode.create(p1);
  3450. end
  3451. else
  3452. internalerror(20021029);
  3453. end;
  3454. end
  3455. else
  3456. begin
  3457. if m_isolike_unary_minus in current_settings.modeswitches then
  3458. p1:=sub_expr(opmultiply,false,false,nil)
  3459. else
  3460. p1:=sub_expr(oppower,false,false,nil);
  3461. p1:=cunaryminusnode.create(p1);
  3462. end;
  3463. end;
  3464. _OP_NOT :
  3465. begin
  3466. consume(_OP_NOT);
  3467. p1:=factor(false,false,false);
  3468. p1:=cnotnode.create(p1);
  3469. end;
  3470. _NIL :
  3471. begin
  3472. consume(_NIL);
  3473. p1:=cnilnode.create;
  3474. { It's really ugly code nil^, but delphi allows it }
  3475. if token in [_CARET,_POINT] then
  3476. begin
  3477. again:=true;
  3478. postfixoperators(p1,again,getaddr);
  3479. end;
  3480. end;
  3481. _OBJCPROTOCOL:
  3482. begin
  3483. { The @protocol keyword is used in two ways in Objective-C:
  3484. 1) to declare protocols (~ Object Pascal interfaces)
  3485. 2) to obtain the metaclass (~ Object Pascal) "class of")
  3486. of a declared protocol
  3487. This code is for handling the second case. Because of 1),
  3488. we cannot simply use a system unit symbol.
  3489. }
  3490. consume(_OBJCPROTOCOL);
  3491. consume(_LKLAMMER);
  3492. p1:=factor(false,false,false);
  3493. consume(_RKLAMMER);
  3494. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  3495. end;
  3496. else
  3497. begin
  3498. Message(parser_e_illegal_expression);
  3499. p1:=cerrornode.create;
  3500. { recover }
  3501. consume(token);
  3502. end;
  3503. end;
  3504. end;
  3505. { generate error node if no node is created }
  3506. if not assigned(p1) then
  3507. begin
  3508. {$ifdef EXTDEBUG}
  3509. Comment(V_Warning,'factor: p1=nil');
  3510. {$endif}
  3511. p1:=cerrornode.create;
  3512. updatefpos:=true;
  3513. end;
  3514. { get the resultdef for the node if nothing stops us }
  3515. if (not assigned(p1.resultdef)) and dopostfix then
  3516. begin
  3517. do_typecheckpass_changed(p1,nodechanged);
  3518. updatefpos:=updatefpos or nodechanged;
  3519. end;
  3520. if assigned(p1) and
  3521. updatefpos then
  3522. p1.fileinfo:=filepos;
  3523. factor:=p1;
  3524. end;
  3525. {$maxfpuregisters default}
  3526. procedure post_comp_expr_gendef(var def: tdef);
  3527. var
  3528. p1 : tnode;
  3529. again : boolean;
  3530. begin
  3531. if not assigned(def) then
  3532. internalerror(2011053001);
  3533. again:=false;
  3534. { handle potential typecasts, etc }
  3535. p1:=handle_factor_typenode(def,false,again,nil,false);
  3536. { parse postfix operators }
  3537. postfixoperators(p1,again,false);
  3538. if assigned(p1) and (p1.nodetype=typen) then
  3539. def:=ttypenode(p1).typedef
  3540. else
  3541. def:=generrordef;
  3542. end;
  3543. {****************************************************************************
  3544. Sub_Expr
  3545. ****************************************************************************}
  3546. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean;factornode:tnode):tnode;
  3547. {Reads a subexpression while the operators are of the current precedence
  3548. level, or any higher level. Replaces the old term, simpl_expr and
  3549. simpl2_expr.}
  3550. function istypenode(n:tnode):boolean;inline;
  3551. { Checks whether the given node is a type node or a VMT node containing a
  3552. typenode. This is used in the code for inline specializations in the
  3553. _LT branch below }
  3554. begin
  3555. result:=assigned(n) and
  3556. (
  3557. (n.nodetype=typen) or
  3558. (
  3559. (n.nodetype=loadvmtaddrn) and
  3560. (tloadvmtaddrnode(n).left.nodetype=typen)
  3561. )
  3562. );
  3563. end;
  3564. (*function gettypedef(n:tnode):tdef;inline;
  3565. { This returns the typedef that belongs to the given typenode or
  3566. loadvmtaddrnode. n must not be Nil! }
  3567. begin
  3568. if n.nodetype=typen then
  3569. result:=ttypenode(n).typedef
  3570. else
  3571. result:=ttypenode(tloadvmtaddrnode(n).left).typedef;
  3572. end;*)
  3573. function gettypedef(sym:tsym):tdef;inline;
  3574. begin
  3575. result:=nil;
  3576. case sym.typ of
  3577. typesym:
  3578. result:=ttypesym(sym).typedef;
  3579. procsym:
  3580. result:=tdef(tprocsym(sym).procdeflist[0]);
  3581. else
  3582. internalerror(2015092701);
  3583. end;
  3584. end;
  3585. function getgenericsym(n:tnode;out srsym:tsym):boolean;
  3586. var
  3587. srsymtable : tsymtable;
  3588. begin
  3589. srsym:=nil;
  3590. case n.nodetype of
  3591. typen:
  3592. srsym:=ttypenode(n).typedef.typesym;
  3593. loadvmtaddrn:
  3594. srsym:=ttypenode(tloadvmtaddrnode(n).left).typedef.typesym;
  3595. loadn:
  3596. if not searchsym_with_symoption(tloadnode(n).symtableentry.Name,srsym,srsymtable,sp_generic_dummy) then
  3597. srsym:=nil;
  3598. specializen:
  3599. srsym:=tspecializenode(n).sym;
  3600. { TODO : handle const nodes }
  3601. end;
  3602. result:=assigned(srsym);
  3603. end;
  3604. function generate_inline_specialization(gendef:tdef;n:tnode;filepos:tfileposinfo;parseddef:tdef;gensym:tsym;p2:tnode):tnode;
  3605. var
  3606. again,
  3607. getaddr : boolean;
  3608. pload : tnode;
  3609. spezcontext : tspecializationcontext;
  3610. structdef : tabstractrecorddef;
  3611. begin
  3612. if n.nodetype=specializen then
  3613. begin
  3614. getaddr:=tspecializenode(n).getaddr;
  3615. pload:=tspecializenode(n).left;
  3616. tspecializenode(n).left:=nil;
  3617. end
  3618. else
  3619. begin
  3620. getaddr:=false;
  3621. pload:=nil;
  3622. end;
  3623. if assigned(parseddef) and assigned(gensym) and assigned(p2) then
  3624. gendef:=generate_specialization_phase1(spezcontext,gendef,parseddef,gensym.realname,p2.fileinfo)
  3625. else
  3626. gendef:=generate_specialization_phase1(spezcontext,gendef);
  3627. case gendef.typ of
  3628. errordef:
  3629. begin
  3630. spezcontext.free;
  3631. spezcontext:=nil;
  3632. gensym:=generrorsym;
  3633. end;
  3634. objectdef,
  3635. recorddef,
  3636. procvardef,
  3637. arraydef:
  3638. begin
  3639. gendef:=generate_specialization_phase2(spezcontext,tstoreddef(gendef),false,'');
  3640. spezcontext.free;
  3641. spezcontext:=nil;
  3642. gensym:=gendef.typesym;
  3643. end;
  3644. procdef:
  3645. begin
  3646. if block_type<>bt_body then
  3647. begin
  3648. message(parser_e_illegal_expression);
  3649. gensym:=generrorsym;
  3650. end
  3651. else
  3652. begin
  3653. gensym:=tprocdef(gendef).procsym;
  3654. end;
  3655. end;
  3656. else
  3657. internalerror(2015092702);
  3658. end;
  3659. { in case of a class or a record the specialized generic
  3660. is always a classrefdef }
  3661. again:=false;
  3662. if assigned(pload) then
  3663. begin
  3664. result:=pload;
  3665. structdef:=nil;
  3666. case result.resultdef.typ of
  3667. objectdef,
  3668. recorddef:
  3669. begin
  3670. structdef:=tabstractrecorddef(result.resultdef);
  3671. end;
  3672. classrefdef:
  3673. begin
  3674. structdef:=tabstractrecorddef(tclassrefdef(result.resultdef).pointeddef);
  3675. end;
  3676. else
  3677. internalerror(2015092703);
  3678. end;
  3679. do_member_read(structdef,getaddr,gensym,result,again,[],spezcontext);
  3680. end
  3681. else
  3682. begin
  3683. result:=nil;
  3684. { check if it's a method/class method }
  3685. if is_member_read(gensym,gensym.owner,result,parseddef) then
  3686. begin
  3687. { if we are accessing a owner procsym from the nested }
  3688. { class we need to call it as a class member }
  3689. if (gensym.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  3690. assigned(current_structdef) and (current_structdef<>parseddef) and is_owned_by(current_structdef,parseddef) then
  3691. begin
  3692. result:=cloadvmtaddrnode.create(ctypenode.create(parseddef));
  3693. { not srsymtable.symtabletype since that can be }
  3694. { withsymtable as well }
  3695. if (gensym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  3696. begin
  3697. do_member_read(tabstractrecorddef(parseddef),getaddr,gensym,result,again,[],spezcontext);
  3698. spezcontext:=nil;
  3699. end
  3700. else
  3701. { no procsyms in records (yet) }
  3702. internalerror(2015092704);
  3703. end
  3704. else
  3705. begin
  3706. { regular procedure/function call }
  3707. do_proc_call(gensym,gensym.owner,nil,
  3708. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  3709. again,result,[],spezcontext);
  3710. spezcontext:=nil;
  3711. end;
  3712. end
  3713. else
  3714. { handle potential typecasts, etc }
  3715. result:=handle_factor_typenode(gendef,false,again,nil,false);
  3716. end;
  3717. { parse postfix operators }
  3718. if postfixoperators(result,again,false) then
  3719. if assigned(result) then
  3720. result.fileinfo:=filepos
  3721. else
  3722. result:=cerrornode.create;
  3723. spezcontext.free;
  3724. end;
  3725. label
  3726. SubExprStart;
  3727. var
  3728. p1,p2,ptmp : tnode;
  3729. oldt : Ttoken;
  3730. filepos : tfileposinfo;
  3731. gendef,parseddef : tdef;
  3732. gensym : tsym;
  3733. begin
  3734. SubExprStart:
  3735. if pred_level=highest_precedence then
  3736. begin
  3737. if factornode=nil then
  3738. p1:=factor(false,typeonly,false)
  3739. else
  3740. p1:=factornode;
  3741. end
  3742. else
  3743. p1:=sub_expr(succ(pred_level),true,typeonly,factornode);
  3744. repeat
  3745. if (token in [NOTOKEN..last_operator]) and
  3746. (token in operator_levels[pred_level]) and
  3747. ((token<>_EQ) or accept_equal) then
  3748. begin
  3749. oldt:=token;
  3750. filepos:=current_tokenpos;
  3751. consume(token);
  3752. if pred_level=highest_precedence then
  3753. p2:=factor(false,false,false)
  3754. else
  3755. p2:=sub_expr(succ(pred_level),true,typeonly,nil);
  3756. case oldt of
  3757. _PLUS :
  3758. p1:=caddnode.create(addn,p1,p2);
  3759. _MINUS :
  3760. p1:=caddnode.create(subn,p1,p2);
  3761. _STAR :
  3762. p1:=caddnode.create(muln,p1,p2);
  3763. _SLASH :
  3764. p1:=caddnode.create(slashn,p1,p2);
  3765. _EQ:
  3766. p1:=caddnode.create(equaln,p1,p2);
  3767. _GT :
  3768. p1:=caddnode.create(gtn,p1,p2);
  3769. _LT :
  3770. begin
  3771. { we need to decice whether we have an inline specialization
  3772. (type nodes to the left and right of "<", mode Delphi and
  3773. ">" or "," following) or a normal "<" comparison }
  3774. { TODO : p1 could be a non type if e.g. a variable with the
  3775. same name is defined in the same unit where the
  3776. generic is defined (though "same unit" is not
  3777. necessarily needed) }
  3778. if getgenericsym(p1,gensym) and
  3779. { Attention: when nested specializations are supported
  3780. p2 could be a loadn if a "<" follows }
  3781. istypenode(p2) and
  3782. (m_delphi in current_settings.modeswitches) and
  3783. { TODO : add _LT, _LSHARPBRACKET for nested specializations }
  3784. (token in [_GT,_RSHARPBRACKET,_COMMA]) then
  3785. begin
  3786. { this is an inline specialization }
  3787. { retrieve the defs of two nodes }
  3788. if p1.nodetype=specializen then
  3789. gendef:=gettypedef(tspecializenode(p1).sym)
  3790. else
  3791. gendef:=nil;
  3792. parseddef:=gettypedef(p2);
  3793. { check the hints for parseddef }
  3794. check_hints(parseddef.typesym,parseddef.typesym.symoptions,parseddef.typesym.deprecatedmsg,p1.fileinfo);
  3795. ptmp:=generate_inline_specialization(gendef,p1,filepos,parseddef,gensym,p2);
  3796. { we don't need these nodes anymore }
  3797. p1.free;
  3798. p2.free;
  3799. p1:=ptmp;
  3800. { with p1 now set we are in reality directly behind the
  3801. call to "factor" thus we need to call down to that
  3802. again }
  3803. { This is disabled until specializations on the right
  3804. hand side work as well, because
  3805. "not working expressions" is better than "half working
  3806. expressions" }
  3807. {factornode:=p1;
  3808. goto SubExprStart;}
  3809. end
  3810. else
  3811. begin
  3812. { this is a normal "<" comparison }
  3813. { potential generic types that are followed by a "<": }
  3814. { a) might not have their resultdef set }
  3815. if not assigned(p1.resultdef) then
  3816. do_typecheckpass(p1);
  3817. { b) are not checked whether they are an undefined def,
  3818. but not a generic parameter }
  3819. if (p1.nodetype=typen) and
  3820. (ttypenode(p1).typedef.typ=undefineddef) and
  3821. assigned(ttypenode(p1).typedef.typesym) and
  3822. not (sp_generic_para in ttypenode(p1).typedef.typesym.symoptions) then
  3823. begin
  3824. identifier_not_found(ttypenode(p1).typedef.typesym.RealName);
  3825. p1.Free;
  3826. p1:=cerrornode.create;
  3827. end;
  3828. { c) don't have their hints checked }
  3829. if istypenode(p1) then
  3830. begin
  3831. gendef:=gettypedef(p1);
  3832. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  3833. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  3834. end;
  3835. { Note: the second part of the expression will be needed
  3836. for nested specializations }
  3837. if istypenode(p2) {and
  3838. not (token in [_LT, _LSHARPBRACKET])} then
  3839. begin
  3840. gendef:=gettypedef(p2);
  3841. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  3842. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  3843. end;
  3844. { create the comparison node for "<" }
  3845. p1:=caddnode.create(ltn,p1,p2)
  3846. end;
  3847. end;
  3848. _GTE :
  3849. p1:=caddnode.create(gten,p1,p2);
  3850. _LTE :
  3851. p1:=caddnode.create(lten,p1,p2);
  3852. _SYMDIF :
  3853. p1:=caddnode.create(symdifn,p1,p2);
  3854. _STARSTAR :
  3855. p1:=caddnode.create(starstarn,p1,p2);
  3856. _OP_AS,
  3857. _OP_IS :
  3858. begin
  3859. if (m_delphi in current_settings.modeswitches) and
  3860. (token in [_LT, _LSHARPBRACKET]) and
  3861. getgenericsym(p2,gensym) then
  3862. begin
  3863. { for now we're handling this as a generic declaration;
  3864. there could be cases though (because of operator
  3865. overloading) where this is the wrong decision... }
  3866. { TODO : here the same note as in _LT applies as p2 could
  3867. point to a variable, etc }
  3868. gendef:=gettypedef(p2);
  3869. ptmp:=generate_inline_specialization(gendef,p2,filepos,nil,nil,nil);
  3870. { we don't need the old p2 anymore }
  3871. p2.Free;
  3872. p2:=ptmp;
  3873. { here we don't need to call back down to "factor", thus
  3874. no "goto" }
  3875. end;
  3876. { now generate the "is" or "as" node }
  3877. case oldt of
  3878. _OP_AS:
  3879. p1:=casnode.create(p1,p2);
  3880. _OP_IS:
  3881. p1:=cisnode.create(p1,p2);
  3882. end;
  3883. end;
  3884. _OP_IN :
  3885. p1:=cinnode.create(p1,p2);
  3886. _OP_OR,
  3887. _PIPE {macpas only} :
  3888. begin
  3889. p1:=caddnode.create(orn,p1,p2);
  3890. if (oldt = _PIPE) then
  3891. include(p1.flags,nf_short_bool);
  3892. end;
  3893. _OP_AND,
  3894. _AMPERSAND {macpas only} :
  3895. begin
  3896. p1:=caddnode.create(andn,p1,p2);
  3897. if (oldt = _AMPERSAND) then
  3898. include(p1.flags,nf_short_bool);
  3899. end;
  3900. _OP_DIV :
  3901. p1:=cmoddivnode.create(divn,p1,p2);
  3902. _OP_NOT :
  3903. p1:=cnotnode.create(p1);
  3904. _OP_MOD :
  3905. begin
  3906. p1:=cmoddivnode.create(modn,p1,p2);
  3907. if m_iso in current_settings.modeswitches then
  3908. include(p1.flags,nf_isomod);
  3909. end;
  3910. _OP_SHL :
  3911. p1:=cshlshrnode.create(shln,p1,p2);
  3912. _OP_SHR :
  3913. p1:=cshlshrnode.create(shrn,p1,p2);
  3914. _OP_XOR :
  3915. p1:=caddnode.create(xorn,p1,p2);
  3916. _ASSIGNMENT :
  3917. p1:=cassignmentnode.create(p1,p2);
  3918. _NE :
  3919. p1:=caddnode.create(unequaln,p1,p2);
  3920. end;
  3921. p1.fileinfo:=filepos;
  3922. end
  3923. else
  3924. break;
  3925. until false;
  3926. sub_expr:=p1;
  3927. end;
  3928. function comp_expr(accept_equal,typeonly:boolean):tnode;
  3929. var
  3930. oldafterassignment : boolean;
  3931. p1 : tnode;
  3932. begin
  3933. oldafterassignment:=afterassignment;
  3934. afterassignment:=true;
  3935. p1:=sub_expr(opcompare,accept_equal,typeonly,nil);
  3936. { get the resultdef for this expression }
  3937. if not assigned(p1.resultdef) then
  3938. do_typecheckpass(p1);
  3939. afterassignment:=oldafterassignment;
  3940. comp_expr:=p1;
  3941. end;
  3942. function expr(dotypecheck : boolean) : tnode;
  3943. var
  3944. p1,p2 : tnode;
  3945. filepos : tfileposinfo;
  3946. oldafterassignment,
  3947. updatefpos : boolean;
  3948. begin
  3949. oldafterassignment:=afterassignment;
  3950. p1:=sub_expr(opcompare,true,false,nil);
  3951. { get the resultdef for this expression }
  3952. if not assigned(p1.resultdef) and
  3953. dotypecheck then
  3954. do_typecheckpass(p1);
  3955. filepos:=current_tokenpos;
  3956. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  3957. afterassignment:=true;
  3958. updatefpos:=true;
  3959. case token of
  3960. _POINTPOINT :
  3961. begin
  3962. consume(_POINTPOINT);
  3963. p2:=sub_expr(opcompare,true,false,nil);
  3964. p1:=crangenode.create(p1,p2);
  3965. end;
  3966. _ASSIGNMENT :
  3967. begin
  3968. consume(_ASSIGNMENT);
  3969. if assigned(p1.resultdef) and (p1.resultdef.typ=procvardef) then
  3970. getprocvardef:=tprocvardef(p1.resultdef);
  3971. p2:=sub_expr(opcompare,true,false,nil);
  3972. if assigned(getprocvardef) then
  3973. handle_procvar(getprocvardef,p2);
  3974. getprocvardef:=nil;
  3975. p1:=cassignmentnode.create(p1,p2);
  3976. end;
  3977. _PLUSASN :
  3978. begin
  3979. consume(_PLUSASN);
  3980. p2:=sub_expr(opcompare,true,false,nil);
  3981. p1:=gen_c_style_operator(addn,p1,p2);
  3982. end;
  3983. _MINUSASN :
  3984. begin
  3985. consume(_MINUSASN);
  3986. p2:=sub_expr(opcompare,true,false,nil);
  3987. p1:=gen_c_style_operator(subn,p1,p2);
  3988. end;
  3989. _STARASN :
  3990. begin
  3991. consume(_STARASN );
  3992. p2:=sub_expr(opcompare,true,false,nil);
  3993. p1:=gen_c_style_operator(muln,p1,p2);
  3994. end;
  3995. _SLASHASN :
  3996. begin
  3997. consume(_SLASHASN );
  3998. p2:=sub_expr(opcompare,true,false,nil);
  3999. p1:=gen_c_style_operator(slashn,p1,p2);
  4000. end;
  4001. else
  4002. updatefpos:=false;
  4003. end;
  4004. { get the resultdef for this expression }
  4005. if not assigned(p1.resultdef) and
  4006. dotypecheck then
  4007. do_typecheckpass(p1);
  4008. afterassignment:=oldafterassignment;
  4009. if updatefpos then
  4010. p1.fileinfo:=filepos;
  4011. expr:=p1;
  4012. end;
  4013. function get_intconst:TConstExprInt;
  4014. {Reads an expression, tries to evalute it and check if it is an integer
  4015. constant. Then the constant is returned.}
  4016. var
  4017. p:tnode;
  4018. begin
  4019. result:=0;
  4020. p:=comp_expr(true,false);
  4021. if not codegenerror then
  4022. begin
  4023. if (p.nodetype<>ordconstn) or
  4024. not(is_integer(p.resultdef)) then
  4025. Message(parser_e_illegal_expression)
  4026. else
  4027. result:=tordconstnode(p).value;
  4028. end;
  4029. p.free;
  4030. end;
  4031. function get_stringconst:string;
  4032. {Reads an expression, tries to evaluate it and checks if it is a string
  4033. constant. Then the constant is returned.}
  4034. var
  4035. p:tnode;
  4036. begin
  4037. get_stringconst:='';
  4038. p:=comp_expr(true,false);
  4039. if p.nodetype<>stringconstn then
  4040. begin
  4041. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  4042. get_stringconst:=char(int64(tordconstnode(p).value))
  4043. else
  4044. Message(parser_e_illegal_expression);
  4045. end
  4046. else
  4047. get_stringconst:=strpas(tstringconstnode(p).value_str);
  4048. p.free;
  4049. end;
  4050. end.